blob: 69f3af8cee2708a4c3f92baa4bfe828dd92179f7 [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));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000372static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
373static void list_glob_vars __ARGS((int *first));
374static void list_buf_vars __ARGS((int *first));
375static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000377static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000378#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000379static void list_vim_vars __ARGS((int *first));
380static void list_script_vars __ARGS((int *first));
381static void list_func_vars __ARGS((int *first));
382static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static 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));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000478static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000480#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000481static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000482static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
490static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000503static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000517static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000519static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000525static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000533static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000534static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000535static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000538static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000546static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000560static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000566static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000582static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000583static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000584static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000586static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000590#ifdef vim_mkdir
591static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
592#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000596static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000598static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000599static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000601static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000602static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000604static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000615static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000617static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000624static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000626static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000627static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000629static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000631static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000634static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000635static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000638static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639#ifdef HAVE_STRFTIME
640static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
641#endif
642static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000654static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000655static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000656static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000657static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000658static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000660static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000674static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000677static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000679static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000680static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static int get_env_len __ARGS((char_u **arg));
682static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000683static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000684static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
685#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
686#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
687 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000688static 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 +0000689static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000690static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000691static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
692static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static typval_T *alloc_tv __ARGS((void));
694static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void init_tv __ARGS((typval_T *varp));
696static long get_tv_number __ARGS((typval_T *varp));
697static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000698static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static char_u *get_tv_string __ARGS((typval_T *varp));
700static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000701static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000703static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
705static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
706static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000707static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
708static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
710static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000711static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000712static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000714static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
716static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
717static int eval_fname_script __ARGS((char_u *p));
718static int eval_fname_sid __ARGS((char_u *p));
719static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static ufunc_T *find_func __ARGS((char_u *name));
721static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000722static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000723#ifdef FEAT_PROFILE
724static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000725static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
726static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
727static int
728# ifdef __BORLANDC__
729 _RTLENTRYF
730# endif
731 prof_total_cmp __ARGS((const void *s1, const void *s2));
732static int
733# ifdef __BORLANDC__
734 _RTLENTRYF
735# endif
736 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000737#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000738static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000739static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000740static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void func_free __ARGS((ufunc_T *fp));
742static void func_unref __ARGS((char_u *name));
743static void func_ref __ARGS((char_u *name));
744static 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));
745static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000746static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
747static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000748static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000749static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000750static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000752/* Character used as separated in autoload function/variable names. */
753#define AUTOLOAD_CHAR '#'
754
Bram Moolenaar33570922005-01-25 22:26:29 +0000755/*
756 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000757 */
758 void
759eval_init()
760{
Bram Moolenaar33570922005-01-25 22:26:29 +0000761 int i;
762 struct vimvar *p;
763
764 init_var_dict(&globvardict, &globvars_var);
765 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000766 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000767 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768
769 for (i = 0; i < VV_LEN; ++i)
770 {
771 p = &vimvars[i];
772 STRCPY(p->vv_di.di_key, p->vv_name);
773 if (p->vv_flags & VV_RO)
774 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
775 else if (p->vv_flags & VV_RO_SBX)
776 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
777 else
778 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000779
780 /* add to v: scope dict, unless the value is not always available */
781 if (p->vv_type != VAR_UNKNOWN)
782 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000783 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000784 /* add to compat scope dict */
785 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000786 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000787}
788
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000789#if defined(EXITFREE) || defined(PROTO)
790 void
791eval_clear()
792{
793 int i;
794 struct vimvar *p;
795
796 for (i = 0; i < VV_LEN; ++i)
797 {
798 p = &vimvars[i];
799 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000800 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000801 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000802 p->vv_di.di_tv.vval.v_string = NULL;
803 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000804 }
805 hash_clear(&vimvarht);
806 hash_clear(&compat_hashtab);
807
808 /* script-local variables */
809 for (i = 1; i <= ga_scripts.ga_len; ++i)
810 vars_clear(&SCRIPT_VARS(i));
811 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000812 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000813
814 /* global variables */
815 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000816
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000817 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000818 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000819 hash_clear(&func_hashtab);
820
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000821 /* autoloaded script names */
822 ga_clear_strings(&ga_loaded);
823
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000824 /* unreferenced lists and dicts */
825 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000826}
827#endif
828
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 * Return the name of the executed function.
831 */
832 char_u *
833func_name(cookie)
834 void *cookie;
835{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000836 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837}
838
839/*
840 * Return the address holding the next breakpoint line for a funccall cookie.
841 */
842 linenr_T *
843func_breakpoint(cookie)
844 void *cookie;
845{
Bram Moolenaar33570922005-01-25 22:26:29 +0000846 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847}
848
849/*
850 * Return the address holding the debug tick for a funccall cookie.
851 */
852 int *
853func_dbg_tick(cookie)
854 void *cookie;
855{
Bram Moolenaar33570922005-01-25 22:26:29 +0000856 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857}
858
859/*
860 * Return the nesting level for a funccall cookie.
861 */
862 int
863func_level(cookie)
864 void *cookie;
865{
Bram Moolenaar33570922005-01-25 22:26:29 +0000866 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867}
868
869/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000870funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
872/*
873 * Return TRUE when a function was ended by a ":return" command.
874 */
875 int
876current_func_returned()
877{
878 return current_funccal->returned;
879}
880
881
882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 * Set an internal variable to a string value. Creates the variable if it does
884 * not already exist.
885 */
886 void
887set_internal_string_var(name, value)
888 char_u *name;
889 char_u *value;
890{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000891 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000892 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
894 val = vim_strsave(value);
895 if (val != NULL)
896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000897 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000898 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000900 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000901 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 }
903 }
904}
905
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000906static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000907static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000908static char_u *redir_endp = NULL;
909static char_u *redir_varname = NULL;
910
911/*
912 * Start recording command output to a variable
913 * Returns OK if successfully completed the setup. FAIL otherwise.
914 */
915 int
916var_redir_start(name, append)
917 char_u *name;
918 int append; /* append to an existing variable */
919{
920 int save_emsg;
921 int err;
922 typval_T tv;
923
924 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000925 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000926 {
927 EMSG(_(e_invarg));
928 return FAIL;
929 }
930
931 redir_varname = vim_strsave(name);
932 if (redir_varname == NULL)
933 return FAIL;
934
935 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
936 if (redir_lval == NULL)
937 {
938 var_redir_stop();
939 return FAIL;
940 }
941
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000942 /* The output is stored in growarray "redir_ga" until redirection ends. */
943 ga_init2(&redir_ga, (int)sizeof(char), 500);
944
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000946 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
947 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000948 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
949 {
950 if (redir_endp != NULL && *redir_endp != NUL)
951 /* Trailing characters are present after the variable name */
952 EMSG(_(e_trailing));
953 else
954 EMSG(_(e_invarg));
955 var_redir_stop();
956 return FAIL;
957 }
958
959 /* check if we can write to the variable: set it to or append an empty
960 * string */
961 save_emsg = did_emsg;
962 did_emsg = FALSE;
963 tv.v_type = VAR_STRING;
964 tv.vval.v_string = (char_u *)"";
965 if (append)
966 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
967 else
968 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
969 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000970 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000971 if (err)
972 {
973 var_redir_stop();
974 return FAIL;
975 }
976 if (redir_lval->ll_newkey != NULL)
977 {
978 /* Dictionary item was created, don't do it again. */
979 vim_free(redir_lval->ll_newkey);
980 redir_lval->ll_newkey = NULL;
981 }
982
983 return OK;
984}
985
986/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000987 * Append "value[value_len]" to the variable set by var_redir_start().
988 * The actual appending is postponed until redirection ends, because the value
989 * appended may in fact be the string we write to, changing it may cause freed
990 * memory to be used:
991 * :redir => foo
992 * :let foo
993 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000994 */
995 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000996var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000997 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000998 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000999{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001000 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001001
1002 if (redir_lval == NULL)
1003 return;
1004
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001005 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001006 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001007 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001008 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001009
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001010 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001011 {
1012 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001013 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001014 }
1015 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017}
1018
1019/*
1020 * Stop redirecting command output to a variable.
1021 */
1022 void
1023var_redir_stop()
1024{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001025 typval_T tv;
1026
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001027 if (redir_lval != NULL)
1028 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001029 /* Append the trailing NUL. */
1030 ga_append(&redir_ga, NUL);
1031
1032 /* Assign the text to the variable. */
1033 tv.v_type = VAR_STRING;
1034 tv.vval.v_string = redir_ga.ga_data;
1035 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1036 vim_free(tv.vval.v_string);
1037
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038 clear_lval(redir_lval);
1039 vim_free(redir_lval);
1040 redir_lval = NULL;
1041 }
1042 vim_free(redir_varname);
1043 redir_varname = NULL;
1044}
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046# if defined(FEAT_MBYTE) || defined(PROTO)
1047 int
1048eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1049 char_u *enc_from;
1050 char_u *enc_to;
1051 char_u *fname_from;
1052 char_u *fname_to;
1053{
1054 int err = FALSE;
1055
1056 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1057 set_vim_var_string(VV_CC_TO, enc_to, -1);
1058 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1059 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1060 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1061 err = TRUE;
1062 set_vim_var_string(VV_CC_FROM, NULL, -1);
1063 set_vim_var_string(VV_CC_TO, NULL, -1);
1064 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1065 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1066
1067 if (err)
1068 return FAIL;
1069 return OK;
1070}
1071# endif
1072
1073# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1074 int
1075eval_printexpr(fname, args)
1076 char_u *fname;
1077 char_u *args;
1078{
1079 int err = FALSE;
1080
1081 set_vim_var_string(VV_FNAME_IN, fname, -1);
1082 set_vim_var_string(VV_CMDARG, args, -1);
1083 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1084 err = TRUE;
1085 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1086 set_vim_var_string(VV_CMDARG, NULL, -1);
1087
1088 if (err)
1089 {
1090 mch_remove(fname);
1091 return FAIL;
1092 }
1093 return OK;
1094}
1095# endif
1096
1097# if defined(FEAT_DIFF) || defined(PROTO)
1098 void
1099eval_diff(origfile, newfile, outfile)
1100 char_u *origfile;
1101 char_u *newfile;
1102 char_u *outfile;
1103{
1104 int err = FALSE;
1105
1106 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1107 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1108 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1109 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1110 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1111 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1112 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1113}
1114
1115 void
1116eval_patch(origfile, difffile, outfile)
1117 char_u *origfile;
1118 char_u *difffile;
1119 char_u *outfile;
1120{
1121 int err;
1122
1123 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1124 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1125 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1126 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1127 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1128 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1129 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1130}
1131# endif
1132
1133/*
1134 * Top level evaluation function, returning a boolean.
1135 * Sets "error" to TRUE if there was an error.
1136 * Return TRUE or FALSE.
1137 */
1138 int
1139eval_to_bool(arg, error, nextcmd, skip)
1140 char_u *arg;
1141 int *error;
1142 char_u **nextcmd;
1143 int skip; /* only parse, don't execute */
1144{
Bram Moolenaar33570922005-01-25 22:26:29 +00001145 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 int retval = FALSE;
1147
1148 if (skip)
1149 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001150 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 else
1153 {
1154 *error = FALSE;
1155 if (!skip)
1156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001157 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001158 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 }
1160 }
1161 if (skip)
1162 --emsg_skip;
1163
1164 return retval;
1165}
1166
1167/*
1168 * Top level evaluation function, returning a string. If "skip" is TRUE,
1169 * only parsing to "nextcmd" is done, without reporting errors. Return
1170 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1171 */
1172 char_u *
1173eval_to_string_skip(arg, nextcmd, skip)
1174 char_u *arg;
1175 char_u **nextcmd;
1176 int skip; /* only parse, don't execute */
1177{
Bram Moolenaar33570922005-01-25 22:26:29 +00001178 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 char_u *retval;
1180
1181 if (skip)
1182 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 retval = NULL;
1185 else
1186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001187 retval = vim_strsave(get_tv_string(&tv));
1188 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 if (skip)
1191 --emsg_skip;
1192
1193 return retval;
1194}
1195
1196/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001197 * Skip over an expression at "*pp".
1198 * Return FAIL for an error, OK otherwise.
1199 */
1200 int
1201skip_expr(pp)
1202 char_u **pp;
1203{
Bram Moolenaar33570922005-01-25 22:26:29 +00001204 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001205
1206 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001208}
1209
1210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 * Top level evaluation function, returning a string.
1212 * Return pointer to allocated memory, or NULL for failure.
1213 */
1214 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001215eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 char_u *arg;
1217 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001218 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219{
Bram Moolenaar33570922005-01-25 22:26:29 +00001220 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001222 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001224 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 retval = NULL;
1226 else
1227 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001228 if (dolist && tv.v_type == VAR_LIST)
1229 {
1230 ga_init2(&ga, (int)sizeof(char), 80);
1231 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1232 ga_append(&ga, NUL);
1233 retval = (char_u *)ga.ga_data;
1234 }
1235 else
1236 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001237 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239
1240 return retval;
1241}
1242
1243/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001244 * Call eval_to_string() without using current local variables and using
1245 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 */
1247 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001248eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 char_u *arg;
1250 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001251 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
1253 char_u *retval;
1254 void *save_funccalp;
1255
1256 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001257 if (use_sandbox)
1258 ++sandbox;
1259 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001260 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001261 if (use_sandbox)
1262 --sandbox;
1263 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 restore_funccal(save_funccalp);
1265 return retval;
1266}
1267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268/*
1269 * Top level evaluation function, returning a number.
1270 * Evaluates "expr" silently.
1271 * Returns -1 for an error.
1272 */
1273 int
1274eval_to_number(expr)
1275 char_u *expr;
1276{
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001279 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
1281 ++emsg_off;
1282
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001283 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 retval = -1;
1285 else
1286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001287 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001288 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 --emsg_off;
1291
1292 return retval;
1293}
1294
Bram Moolenaara40058a2005-07-11 22:42:07 +00001295/*
1296 * Prepare v: variable "idx" to be used.
1297 * Save the current typeval in "save_tv".
1298 * When not used yet add the variable to the v: hashtable.
1299 */
1300 static void
1301prepare_vimvar(idx, save_tv)
1302 int idx;
1303 typval_T *save_tv;
1304{
1305 *save_tv = vimvars[idx].vv_tv;
1306 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1307 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1308}
1309
1310/*
1311 * Restore v: variable "idx" to typeval "save_tv".
1312 * When no longer defined, remove the variable from the v: hashtable.
1313 */
1314 static void
1315restore_vimvar(idx, save_tv)
1316 int idx;
1317 typval_T *save_tv;
1318{
1319 hashitem_T *hi;
1320
1321 clear_tv(&vimvars[idx].vv_tv);
1322 vimvars[idx].vv_tv = *save_tv;
1323 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1324 {
1325 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1326 if (HASHITEM_EMPTY(hi))
1327 EMSG2(_(e_intern2), "restore_vimvar()");
1328 else
1329 hash_remove(&vimvarht, hi);
1330 }
1331}
1332
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001333#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001334/*
1335 * Evaluate an expression to a list with suggestions.
1336 * For the "expr:" part of 'spellsuggest'.
1337 */
1338 list_T *
1339eval_spell_expr(badword, expr)
1340 char_u *badword;
1341 char_u *expr;
1342{
1343 typval_T save_val;
1344 typval_T rettv;
1345 list_T *list = NULL;
1346 char_u *p = skipwhite(expr);
1347
1348 /* Set "v:val" to the bad word. */
1349 prepare_vimvar(VV_VAL, &save_val);
1350 vimvars[VV_VAL].vv_type = VAR_STRING;
1351 vimvars[VV_VAL].vv_str = badword;
1352 if (p_verbose == 0)
1353 ++emsg_off;
1354
1355 if (eval1(&p, &rettv, TRUE) == OK)
1356 {
1357 if (rettv.v_type != VAR_LIST)
1358 clear_tv(&rettv);
1359 else
1360 list = rettv.vval.v_list;
1361 }
1362
1363 if (p_verbose == 0)
1364 --emsg_off;
1365 vimvars[VV_VAL].vv_str = NULL;
1366 restore_vimvar(VV_VAL, &save_val);
1367
1368 return list;
1369}
1370
1371/*
1372 * "list" is supposed to contain two items: a word and a number. Return the
1373 * word in "pp" and the number as the return value.
1374 * Return -1 if anything isn't right.
1375 * Used to get the good word and score from the eval_spell_expr() result.
1376 */
1377 int
1378get_spellword(list, pp)
1379 list_T *list;
1380 char_u **pp;
1381{
1382 listitem_T *li;
1383
1384 li = list->lv_first;
1385 if (li == NULL)
1386 return -1;
1387 *pp = get_tv_string(&li->li_tv);
1388
1389 li = li->li_next;
1390 if (li == NULL)
1391 return -1;
1392 return get_tv_number(&li->li_tv);
1393}
1394#endif
1395
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001396/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001397 * Top level evaluation function.
1398 * Returns an allocated typval_T with the result.
1399 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001400 */
1401 typval_T *
1402eval_expr(arg, nextcmd)
1403 char_u *arg;
1404 char_u **nextcmd;
1405{
1406 typval_T *tv;
1407
1408 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001409 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001410 {
1411 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001412 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001413 }
1414
1415 return tv;
1416}
1417
1418
Bram Moolenaar4f688582007-07-24 12:34:30 +00001419#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1420 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001422 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001424 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001426 static int
1427call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 char_u *func;
1429 int argc;
1430 char_u **argv;
1431 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
Bram Moolenaar33570922005-01-25 22:26:29 +00001434 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 long n;
1436 int len;
1437 int i;
1438 int doesrange;
1439 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001440 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001442 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001444 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446 for (i = 0; i < argc; i++)
1447 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001448 /* Pass a NULL or empty argument as an empty string */
1449 if (argv[i] == NULL || *argv[i] == NUL)
1450 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001451 argvars[i].v_type = VAR_STRING;
1452 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001453 continue;
1454 }
1455
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 /* Recognize a number argument, the others must be strings. */
1457 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1458 if (len != 0 && len == (int)STRLEN(argv[i]))
1459 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001460 argvars[i].v_type = VAR_NUMBER;
1461 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463 else
1464 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001465 argvars[i].v_type = VAR_STRING;
1466 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 }
1468 }
1469
1470 if (safe)
1471 {
1472 save_funccalp = save_funccal();
1473 ++sandbox;
1474 }
1475
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001476 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1477 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001479 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (safe)
1481 {
1482 --sandbox;
1483 restore_funccal(save_funccalp);
1484 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001485 vim_free(argvars);
1486
1487 if (ret == FAIL)
1488 clear_tv(rettv);
1489
1490 return ret;
1491}
1492
Bram Moolenaar4f688582007-07-24 12:34:30 +00001493# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001494/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001495 * Call vimL function "func" and return the result as a string.
1496 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001497 * Uses argv[argc] for the function arguments.
1498 */
1499 void *
1500call_func_retstr(func, argc, argv, safe)
1501 char_u *func;
1502 int argc;
1503 char_u **argv;
1504 int safe; /* use the sandbox */
1505{
1506 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001507 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001508
1509 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1510 return NULL;
1511
1512 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001513 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 return retval;
1515}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001516# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001517
Bram Moolenaar4f688582007-07-24 12:34:30 +00001518# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001519/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001520 * Call vimL function "func" and return the result as a number.
1521 * Returns -1 when calling the function fails.
1522 * Uses argv[argc] for the function arguments.
1523 */
1524 long
1525call_func_retnr(func, argc, argv, safe)
1526 char_u *func;
1527 int argc;
1528 char_u **argv;
1529 int safe; /* use the sandbox */
1530{
1531 typval_T rettv;
1532 long retval;
1533
1534 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1535 return -1;
1536
1537 retval = get_tv_number_chk(&rettv, NULL);
1538 clear_tv(&rettv);
1539 return retval;
1540}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001541# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001542
1543/*
1544 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001545 * Uses argv[argc] for the function arguments.
1546 */
1547 void *
1548call_func_retlist(func, argc, argv, safe)
1549 char_u *func;
1550 int argc;
1551 char_u **argv;
1552 int safe; /* use the sandbox */
1553{
1554 typval_T rettv;
1555
1556 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1557 return NULL;
1558
1559 if (rettv.v_type != VAR_LIST)
1560 {
1561 clear_tv(&rettv);
1562 return NULL;
1563 }
1564
1565 return rettv.vval.v_list;
1566}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567#endif
1568
Bram Moolenaar4f688582007-07-24 12:34:30 +00001569
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570/*
1571 * Save the current function call pointer, and set it to NULL.
1572 * Used when executing autocommands and for ":source".
1573 */
1574 void *
1575save_funccal()
1576{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001577 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 current_funccal = NULL;
1580 return (void *)fc;
1581}
1582
1583 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001584restore_funccal(vfc)
1585 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001587 funccall_T *fc = (funccall_T *)vfc;
1588
1589 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590}
1591
Bram Moolenaar05159a02005-02-26 23:04:13 +00001592#if defined(FEAT_PROFILE) || defined(PROTO)
1593/*
1594 * Prepare profiling for entering a child or something else that is not
1595 * counted for the script/function itself.
1596 * Should always be called in pair with prof_child_exit().
1597 */
1598 void
1599prof_child_enter(tm)
1600 proftime_T *tm; /* place to store waittime */
1601{
1602 funccall_T *fc = current_funccal;
1603
1604 if (fc != NULL && fc->func->uf_profiling)
1605 profile_start(&fc->prof_child);
1606 script_prof_save(tm);
1607}
1608
1609/*
1610 * Take care of time spent in a child.
1611 * Should always be called after prof_child_enter().
1612 */
1613 void
1614prof_child_exit(tm)
1615 proftime_T *tm; /* where waittime was stored */
1616{
1617 funccall_T *fc = current_funccal;
1618
1619 if (fc != NULL && fc->func->uf_profiling)
1620 {
1621 profile_end(&fc->prof_child);
1622 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1623 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1624 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1625 }
1626 script_prof_restore(tm);
1627}
1628#endif
1629
1630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631#ifdef FEAT_FOLDING
1632/*
1633 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1634 * it in "*cp". Doesn't give error messages.
1635 */
1636 int
1637eval_foldexpr(arg, cp)
1638 char_u *arg;
1639 int *cp;
1640{
Bram Moolenaar33570922005-01-25 22:26:29 +00001641 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 int retval;
1643 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001644 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1645 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
1647 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001648 if (use_sandbox)
1649 ++sandbox;
1650 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 retval = 0;
1654 else
1655 {
1656 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657 if (tv.v_type == VAR_NUMBER)
1658 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001659 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 retval = 0;
1661 else
1662 {
1663 /* If the result is a string, check if there is a non-digit before
1664 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001665 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (!VIM_ISDIGIT(*s) && *s != '-')
1667 *cp = *s++;
1668 retval = atol((char *)s);
1669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 }
1672 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001673 if (use_sandbox)
1674 --sandbox;
1675 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
1677 return retval;
1678}
1679#endif
1680
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682 * ":let" list all variable values
1683 * ":let var1 var2" list variable values
1684 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001685 * ":let var += expr" assignment command.
1686 * ":let var -= expr" assignment command.
1687 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001688 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 */
1690 void
1691ex_let(eap)
1692 exarg_T *eap;
1693{
1694 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001695 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001696 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001698 int var_count = 0;
1699 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001700 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001701 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001702 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaardb552d602006-03-23 22:59:57 +00001704 argend = skip_var_list(arg, &var_count, &semicolon);
1705 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001707 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1708 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001709 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001712 /*
1713 * ":let" without "=": list variables
1714 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 if (*arg == '[')
1716 EMSG(_(e_invarg));
1717 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001719 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001720 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001722 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001723 list_glob_vars(&first);
1724 list_buf_vars(&first);
1725 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001726#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001727 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001728#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001729 list_script_vars(&first);
1730 list_func_vars(&first);
1731 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 eap->nextcmd = check_nextcmd(arg);
1734 }
1735 else
1736 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001737 op[0] = '=';
1738 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001739 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001740 {
1741 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1742 op[0] = expr[-1]; /* +=, -= or .= */
1743 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001745
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 if (eap->skip)
1747 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (eap->skip)
1750 {
1751 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001752 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 --emsg_skip;
1754 }
1755 else if (i != FAIL)
1756 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761 }
1762}
1763
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764/*
1765 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1766 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001767 * When "nextchars" is not NULL it points to a string with characters that
1768 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1769 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770 * Returns OK or FAIL;
1771 */
1772 static int
1773ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1774 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001775 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776 int copy; /* copy values from "tv", don't move */
1777 int semicolon; /* from skip_var_list() */
1778 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001779 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780{
1781 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001782 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001784 listitem_T *item;
1785 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786
1787 if (*arg != '[')
1788 {
1789 /*
1790 * ":let var = expr" or ":for var in list"
1791 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001792 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 return FAIL;
1794 return OK;
1795 }
1796
1797 /*
1798 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1799 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001800 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801 {
1802 EMSG(_(e_listreq));
1803 return FAIL;
1804 }
1805
1806 i = list_len(l);
1807 if (semicolon == 0 && var_count < i)
1808 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001809 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 return FAIL;
1811 }
1812 if (var_count - semicolon > i)
1813 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001814 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815 return FAIL;
1816 }
1817
1818 item = l->lv_first;
1819 while (*arg != ']')
1820 {
1821 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001822 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 item = item->li_next;
1824 if (arg == NULL)
1825 return FAIL;
1826
1827 arg = skipwhite(arg);
1828 if (*arg == ';')
1829 {
1830 /* Put the rest of the list (may be empty) in the var after ';'.
1831 * Create a new list for this. */
1832 l = list_alloc();
1833 if (l == NULL)
1834 return FAIL;
1835 while (item != NULL)
1836 {
1837 list_append_tv(l, &item->li_tv);
1838 item = item->li_next;
1839 }
1840
1841 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001842 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 ltv.vval.v_list = l;
1844 l->lv_refcount = 1;
1845
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001846 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1847 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 clear_tv(&ltv);
1849 if (arg == NULL)
1850 return FAIL;
1851 break;
1852 }
1853 else if (*arg != ',' && *arg != ']')
1854 {
1855 EMSG2(_(e_intern2), "ex_let_vars()");
1856 return FAIL;
1857 }
1858 }
1859
1860 return OK;
1861}
1862
1863/*
1864 * Skip over assignable variable "var" or list of variables "[var, var]".
1865 * Used for ":let varvar = expr" and ":for varvar in expr".
1866 * For "[var, var]" increment "*var_count" for each variable.
1867 * for "[var, var; var]" set "semicolon".
1868 * Return NULL for an error.
1869 */
1870 static char_u *
1871skip_var_list(arg, var_count, semicolon)
1872 char_u *arg;
1873 int *var_count;
1874 int *semicolon;
1875{
1876 char_u *p, *s;
1877
1878 if (*arg == '[')
1879 {
1880 /* "[var, var]": find the matching ']'. */
1881 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001882 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 {
1884 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1885 s = skip_var_one(p);
1886 if (s == p)
1887 {
1888 EMSG2(_(e_invarg2), p);
1889 return NULL;
1890 }
1891 ++*var_count;
1892
1893 p = skipwhite(s);
1894 if (*p == ']')
1895 break;
1896 else if (*p == ';')
1897 {
1898 if (*semicolon == 1)
1899 {
1900 EMSG(_("Double ; in list of variables"));
1901 return NULL;
1902 }
1903 *semicolon = 1;
1904 }
1905 else if (*p != ',')
1906 {
1907 EMSG2(_(e_invarg2), p);
1908 return NULL;
1909 }
1910 }
1911 return p + 1;
1912 }
1913 else
1914 return skip_var_one(arg);
1915}
1916
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001917/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001918 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001919 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001920 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 static char_u *
1922skip_var_one(arg)
1923 char_u *arg;
1924{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001925 if (*arg == '@' && arg[1] != NUL)
1926 return arg + 2;
1927 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1928 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929}
1930
Bram Moolenaara7043832005-01-21 11:56:39 +00001931/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 * List variables for hashtab "ht" with prefix "prefix".
1933 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001934 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001936list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001938 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001940 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001941{
Bram Moolenaar33570922005-01-25 22:26:29 +00001942 hashitem_T *hi;
1943 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001944 int todo;
1945
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001946 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001947 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1948 {
1949 if (!HASHITEM_EMPTY(hi))
1950 {
1951 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001952 di = HI2DI(hi);
1953 if (empty || di->di_tv.v_type != VAR_STRING
1954 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001955 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001956 }
1957 }
1958}
1959
1960/*
1961 * List global variables.
1962 */
1963 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001964list_glob_vars(first)
1965 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001966{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001967 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001968}
1969
1970/*
1971 * List buffer variables.
1972 */
1973 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001974list_buf_vars(first)
1975 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001976{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001977 char_u numbuf[NUMBUFLEN];
1978
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001979 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
1980 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001981
1982 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001983 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1984 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001985}
1986
1987/*
1988 * List window variables.
1989 */
1990 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001991list_win_vars(first)
1992 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001993{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001994 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
1995 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001996}
1997
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001998#ifdef FEAT_WINDOWS
1999/*
2000 * List tab page variables.
2001 */
2002 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002003list_tab_vars(first)
2004 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002005{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002006 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2007 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002008}
2009#endif
2010
Bram Moolenaara7043832005-01-21 11:56:39 +00002011/*
2012 * List Vim variables.
2013 */
2014 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002015list_vim_vars(first)
2016 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002018 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019}
2020
2021/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002022 * List script-local variables, if there is a script.
2023 */
2024 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002025list_script_vars(first)
2026 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002027{
2028 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002029 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2030 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002031}
2032
2033/*
2034 * List function variables, if there is a function.
2035 */
2036 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002037list_func_vars(first)
2038 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002039{
2040 if (current_funccal != NULL)
2041 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002042 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002043}
2044
2045/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046 * List variables in "arg".
2047 */
2048 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002049list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050 exarg_T *eap;
2051 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002052 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002053{
2054 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002055 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002057 char_u *name_start;
2058 char_u *arg_subsc;
2059 char_u *tofree;
2060 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061
2062 while (!ends_excmd(*arg) && !got_int)
2063 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002064 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002065 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002066 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002067 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2068 {
2069 emsg_severe = TRUE;
2070 EMSG(_(e_trailing));
2071 break;
2072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002075 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076 /* get_name_len() takes care of expanding curly braces */
2077 name_start = name = arg;
2078 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2079 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002080 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002081 /* This is mainly to keep test 49 working: when expanding
2082 * curly braces fails overrule the exception error message. */
2083 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002084 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 emsg_severe = TRUE;
2086 EMSG2(_(e_invarg2), arg);
2087 break;
2088 }
2089 error = TRUE;
2090 }
2091 else
2092 {
2093 if (tofree != NULL)
2094 name = tofree;
2095 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 else
2098 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002099 /* handle d.key, l[idx], f(expr) */
2100 arg_subsc = arg;
2101 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002103 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002104 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002105 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002106 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002107 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002108 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 case 'g': list_glob_vars(first); break;
2110 case 'b': list_buf_vars(first); break;
2111 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002112#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002114#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115 case 'v': list_vim_vars(first); break;
2116 case 's': list_script_vars(first); break;
2117 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118 default:
2119 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002120 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 }
2122 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123 {
2124 char_u numbuf[NUMBUFLEN];
2125 char_u *tf;
2126 int c;
2127 char_u *s;
2128
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002129 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130 c = *arg;
2131 *arg = NUL;
2132 list_one_var_a((char_u *)"",
2133 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134 tv.v_type,
2135 s == NULL ? (char_u *)"" : s,
2136 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002137 *arg = c;
2138 vim_free(tf);
2139 }
2140 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002141 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002142 }
2143 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002144
2145 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002146 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002147
2148 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149 }
2150
2151 return arg;
2152}
2153
2154/*
2155 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2156 * Returns a pointer to the char just after the var name.
2157 * Returns NULL if there is an error.
2158 */
2159 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002160ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002162 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002164 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002165 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166{
2167 int c1;
2168 char_u *name;
2169 char_u *p;
2170 char_u *arg_end = NULL;
2171 int len;
2172 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002173 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174
2175 /*
2176 * ":let $VAR = expr": Set environment variable.
2177 */
2178 if (*arg == '$')
2179 {
2180 /* Find the end of the name. */
2181 ++arg;
2182 name = arg;
2183 len = get_env_len(&arg);
2184 if (len == 0)
2185 EMSG2(_(e_invarg2), name - 1);
2186 else
2187 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002188 if (op != NULL && (*op == '+' || *op == '-'))
2189 EMSG2(_(e_letwrong), op);
2190 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002191 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 EMSG(_(e_letunexp));
2193 else
2194 {
2195 c1 = name[len];
2196 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002197 p = get_tv_string_chk(tv);
2198 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002199 {
2200 int mustfree = FALSE;
2201 char_u *s = vim_getenv(name, &mustfree);
2202
2203 if (s != NULL)
2204 {
2205 p = tofree = concat_str(s, p);
2206 if (mustfree)
2207 vim_free(s);
2208 }
2209 }
2210 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002211 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002212 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002213 if (STRICMP(name, "HOME") == 0)
2214 init_homedir();
2215 else if (didset_vim && STRICMP(name, "VIM") == 0)
2216 didset_vim = FALSE;
2217 else if (didset_vimruntime
2218 && STRICMP(name, "VIMRUNTIME") == 0)
2219 didset_vimruntime = FALSE;
2220 arg_end = arg;
2221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002223 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 }
2225 }
2226 }
2227
2228 /*
2229 * ":let &option = expr": Set option value.
2230 * ":let &l:option = expr": Set local option value.
2231 * ":let &g:option = expr": Set global option value.
2232 */
2233 else if (*arg == '&')
2234 {
2235 /* Find the end of the name. */
2236 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002237 if (p == NULL || (endchars != NULL
2238 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 EMSG(_(e_letunexp));
2240 else
2241 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002242 long n;
2243 int opt_type;
2244 long numval;
2245 char_u *stringval = NULL;
2246 char_u *s;
2247
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 c1 = *p;
2249 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002250
2251 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002252 s = get_tv_string_chk(tv); /* != NULL if number or string */
2253 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002254 {
2255 opt_type = get_option_value(arg, &numval,
2256 &stringval, opt_flags);
2257 if ((opt_type == 1 && *op == '.')
2258 || (opt_type == 0 && *op != '.'))
2259 EMSG2(_(e_letwrong), op);
2260 else
2261 {
2262 if (opt_type == 1) /* number */
2263 {
2264 if (*op == '+')
2265 n = numval + n;
2266 else
2267 n = numval - n;
2268 }
2269 else if (opt_type == 0 && stringval != NULL) /* string */
2270 {
2271 s = concat_str(stringval, s);
2272 vim_free(stringval);
2273 stringval = s;
2274 }
2275 }
2276 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002277 if (s != NULL)
2278 {
2279 set_option_value(arg, n, s, opt_flags);
2280 arg_end = p;
2281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002283 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285 }
2286
2287 /*
2288 * ":let @r = expr": Set register contents.
2289 */
2290 else if (*arg == '@')
2291 {
2292 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002293 if (op != NULL && (*op == '+' || *op == '-'))
2294 EMSG2(_(e_letwrong), op);
2295 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002296 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 EMSG(_(e_letunexp));
2298 else
2299 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002300 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301 char_u *s;
2302
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002303 p = get_tv_string_chk(tv);
2304 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002305 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002306 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307 if (s != NULL)
2308 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002309 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 vim_free(s);
2311 }
2312 }
2313 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002314 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002316 arg_end = arg + 1;
2317 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002318 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 }
2320 }
2321
2322 /*
2323 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002326 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002328 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002330 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2334 EMSG(_(e_letunexp));
2335 else
2336 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 arg_end = p;
2339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002341 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 }
2343
2344 else
2345 EMSG2(_(e_invarg2), arg);
2346
2347 return arg_end;
2348}
2349
2350/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002351 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2352 */
2353 static int
2354check_changedtick(arg)
2355 char_u *arg;
2356{
2357 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2358 {
2359 EMSG2(_(e_readonlyvar), arg);
2360 return TRUE;
2361 }
2362 return FALSE;
2363}
2364
2365/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 * Get an lval: variable, Dict item or List item that can be assigned a value
2367 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2368 * "name.key", "name.key[expr]" etc.
2369 * Indexing only works if "name" is an existing List or Dictionary.
2370 * "name" points to the start of the name.
2371 * If "rettv" is not NULL it points to the value to be assigned.
2372 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2373 * wrong; must end in space or cmd separator.
2374 *
2375 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002376 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 */
2379 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002380get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002382 typval_T *rettv;
2383 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002384 int unlet;
2385 int skip;
2386 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002387 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 char_u *expr_start, *expr_end;
2391 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002392 dictitem_T *v;
2393 typval_T var1;
2394 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002396 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002397 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002398 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002399 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002402 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403
2404 if (skip)
2405 {
2406 /* When skipping just find the end of the name. */
2407 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002408 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 }
2410
2411 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002412 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 if (expr_start != NULL)
2414 {
2415 /* Don't expand the name when we already know there is an error. */
2416 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2417 && *p != '[' && *p != '.')
2418 {
2419 EMSG(_(e_trailing));
2420 return NULL;
2421 }
2422
2423 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2424 if (lp->ll_exp_name == NULL)
2425 {
2426 /* Report an invalid expression in braces, unless the
2427 * expression evaluation has been cancelled due to an
2428 * aborting error, an interrupt, or an exception. */
2429 if (!aborting() && !quiet)
2430 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002431 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 EMSG2(_(e_invarg2), name);
2433 return NULL;
2434 }
2435 }
2436 lp->ll_name = lp->ll_exp_name;
2437 }
2438 else
2439 lp->ll_name = name;
2440
2441 /* Without [idx] or .key we are done. */
2442 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2443 return p;
2444
2445 cc = *p;
2446 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002447 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (v == NULL && !quiet)
2449 EMSG2(_(e_undefvar), lp->ll_name);
2450 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 if (v == NULL)
2452 return NULL;
2453
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 /*
2455 * Loop until no more [idx] or .key is following.
2456 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002457 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002459 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2461 && !(lp->ll_tv->v_type == VAR_DICT
2462 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 if (!quiet)
2465 EMSG(_("E689: Can only index a List or Dictionary"));
2466 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (!quiet)
2471 EMSG(_("E708: [:] must come last"));
2472 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002474
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 len = -1;
2476 if (*p == '.')
2477 {
2478 key = p + 1;
2479 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2480 ;
2481 if (len == 0)
2482 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 if (!quiet)
2484 EMSG(_(e_emptykey));
2485 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 }
2487 p = key + len;
2488 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002489 else
2490 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002492 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 if (*p == ':')
2494 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002495 else
2496 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 empty1 = FALSE;
2498 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002500 if (get_tv_string_chk(&var1) == NULL)
2501 {
2502 /* not a number or string */
2503 clear_tv(&var1);
2504 return NULL;
2505 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 }
2507
2508 /* Optionally get the second index [ :expr]. */
2509 if (*p == ':')
2510 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002512 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002514 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002515 if (!empty1)
2516 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002518 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 if (rettv != NULL && (rettv->v_type != VAR_LIST
2520 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 if (!quiet)
2523 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 if (!empty1)
2525 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002527 }
2528 p = skipwhite(p + 1);
2529 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 else
2532 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2535 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 if (!empty1)
2537 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002540 if (get_tv_string_chk(&var2) == NULL)
2541 {
2542 /* not a number or string */
2543 if (!empty1)
2544 clear_tv(&var1);
2545 clear_tv(&var2);
2546 return NULL;
2547 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002548 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002550 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002553
Bram Moolenaar8c711452005-01-14 21:53:12 +00002554 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 if (!quiet)
2557 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002558 if (!empty1)
2559 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002563 }
2564
2565 /* Skip to past ']'. */
2566 ++p;
2567 }
2568
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 {
2571 if (len == -1)
2572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002574 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 if (*key == NUL)
2576 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 if (!quiet)
2578 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002581 }
2582 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002583 lp->ll_list = NULL;
2584 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002585 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002587 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002588 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002592 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 if (len == -1)
2594 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 }
2597 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 if (len == -1)
2602 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 p = NULL;
2605 break;
2606 }
2607 if (len == -1)
2608 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002610 }
2611 else
2612 {
2613 /*
2614 * Get the number and item for the only or first index of the List.
2615 */
2616 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002618 else
2619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002620 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 clear_tv(&var1);
2622 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002623 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 lp->ll_list = lp->ll_tv->vval.v_list;
2625 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2626 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002628 if (lp->ll_n1 < 0)
2629 {
2630 lp->ll_n1 = 0;
2631 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2632 }
2633 }
2634 if (lp->ll_li == NULL)
2635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 }
2640
2641 /*
2642 * May need to find the item or absolute index for the second
2643 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 * When no index given: "lp->ll_empty2" is TRUE.
2645 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002649 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 }
2658
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2660 if (lp->ll_n1 < 0)
2661 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2662 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 }
2665
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 }
2669
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 return p;
2671}
2672
2673/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002674 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 */
2676 static void
2677clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002678 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679{
2680 vim_free(lp->ll_exp_name);
2681 vim_free(lp->ll_newkey);
2682}
2683
2684/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002685 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002687 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 */
2689 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002690set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002691 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002693 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002695 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696{
2697 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002698 listitem_T *ri;
2699 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700
2701 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 cc = *endp;
2706 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002707 if (op != NULL && *op != '=')
2708 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002709 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002710
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002711 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002712 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002713 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002714 {
2715 if (tv_op(&tv, rettv, op) == OK)
2716 set_var(lp->ll_name, &tv, FALSE);
2717 clear_tv(&tv);
2718 }
2719 }
2720 else
2721 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002723 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002725 else if (tv_check_lock(lp->ll_newkey == NULL
2726 ? lp->ll_tv->v_lock
2727 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2728 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 else if (lp->ll_range)
2730 {
2731 /*
2732 * Assign the List values to the list items.
2733 */
2734 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002735 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002736 if (op != NULL && *op != '=')
2737 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2738 else
2739 {
2740 clear_tv(&lp->ll_li->li_tv);
2741 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2742 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 ri = ri->li_next;
2744 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2745 break;
2746 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002747 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002749 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 ri = NULL;
2752 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002753 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002754 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 lp->ll_li = lp->ll_li->li_next;
2756 ++lp->ll_n1;
2757 }
2758 if (ri != NULL)
2759 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002760 else if (lp->ll_empty2
2761 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 : lp->ll_n1 != lp->ll_n2)
2763 EMSG(_("E711: List value has not enough items"));
2764 }
2765 else
2766 {
2767 /*
2768 * Assign to a List or Dictionary item.
2769 */
2770 if (lp->ll_newkey != NULL)
2771 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 if (op != NULL && *op != '=')
2773 {
2774 EMSG2(_(e_letwrong), op);
2775 return;
2776 }
2777
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002779 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (di == NULL)
2781 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002782 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2783 {
2784 vim_free(di);
2785 return;
2786 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002788 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002789 else if (op != NULL && *op != '=')
2790 {
2791 tv_op(lp->ll_tv, rettv, op);
2792 return;
2793 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002794 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 /*
2798 * Assign the value to the variable or list item.
2799 */
2800 if (copy)
2801 copy_tv(rettv, lp->ll_tv);
2802 else
2803 {
2804 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002805 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002807 }
2808 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002809}
2810
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002811/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2813 * Returns OK or FAIL.
2814 */
2815 static int
2816tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002817 typval_T *tv1;
2818 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002819 char_u *op;
2820{
2821 long n;
2822 char_u numbuf[NUMBUFLEN];
2823 char_u *s;
2824
2825 /* Can't do anything with a Funcref or a Dict on the right. */
2826 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2827 {
2828 switch (tv1->v_type)
2829 {
2830 case VAR_DICT:
2831 case VAR_FUNC:
2832 break;
2833
2834 case VAR_LIST:
2835 if (*op != '+' || tv2->v_type != VAR_LIST)
2836 break;
2837 /* List += List */
2838 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2839 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2840 return OK;
2841
2842 case VAR_NUMBER:
2843 case VAR_STRING:
2844 if (tv2->v_type == VAR_LIST)
2845 break;
2846 if (*op == '+' || *op == '-')
2847 {
2848 /* nr += nr or nr -= nr*/
2849 n = get_tv_number(tv1);
2850 if (*op == '+')
2851 n += get_tv_number(tv2);
2852 else
2853 n -= get_tv_number(tv2);
2854 clear_tv(tv1);
2855 tv1->v_type = VAR_NUMBER;
2856 tv1->vval.v_number = n;
2857 }
2858 else
2859 {
2860 /* str .= str */
2861 s = get_tv_string(tv1);
2862 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2863 clear_tv(tv1);
2864 tv1->v_type = VAR_STRING;
2865 tv1->vval.v_string = s;
2866 }
2867 return OK;
2868 }
2869 }
2870
2871 EMSG2(_(e_letwrong), op);
2872 return FAIL;
2873}
2874
2875/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002876 * Add a watcher to a list.
2877 */
2878 static void
2879list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002880 list_T *l;
2881 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882{
2883 lw->lw_next = l->lv_watch;
2884 l->lv_watch = lw;
2885}
2886
2887/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002888 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002889 * No warning when it isn't found...
2890 */
2891 static void
2892list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002893 list_T *l;
2894 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002895{
Bram Moolenaar33570922005-01-25 22:26:29 +00002896 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002897
2898 lwp = &l->lv_watch;
2899 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2900 {
2901 if (lw == lwrem)
2902 {
2903 *lwp = lw->lw_next;
2904 break;
2905 }
2906 lwp = &lw->lw_next;
2907 }
2908}
2909
2910/*
2911 * Just before removing an item from a list: advance watchers to the next
2912 * item.
2913 */
2914 static void
2915list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 list_T *l;
2917 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002918{
Bram Moolenaar33570922005-01-25 22:26:29 +00002919 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002920
2921 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2922 if (lw->lw_item == item)
2923 lw->lw_item = item->li_next;
2924}
2925
2926/*
2927 * Evaluate the expression used in a ":for var in expr" command.
2928 * "arg" points to "var".
2929 * Set "*errp" to TRUE for an error, FALSE otherwise;
2930 * Return a pointer that holds the info. Null when there is an error.
2931 */
2932 void *
2933eval_for_line(arg, errp, nextcmdp, skip)
2934 char_u *arg;
2935 int *errp;
2936 char_u **nextcmdp;
2937 int skip;
2938{
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002940 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 typval_T tv;
2942 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002943
2944 *errp = TRUE; /* default: there is an error */
2945
Bram Moolenaar33570922005-01-25 22:26:29 +00002946 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002947 if (fi == NULL)
2948 return NULL;
2949
2950 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2951 if (expr == NULL)
2952 return fi;
2953
2954 expr = skipwhite(expr);
2955 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2956 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002957 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002958 return fi;
2959 }
2960
2961 if (skip)
2962 ++emsg_skip;
2963 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2964 {
2965 *errp = FALSE;
2966 if (!skip)
2967 {
2968 l = tv.vval.v_list;
2969 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002970 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002971 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002972 clear_tv(&tv);
2973 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002974 else
2975 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002976 /* No need to increment the refcount, it's already set for the
2977 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002978 fi->fi_list = l;
2979 list_add_watch(l, &fi->fi_lw);
2980 fi->fi_lw.lw_item = l->lv_first;
2981 }
2982 }
2983 }
2984 if (skip)
2985 --emsg_skip;
2986
2987 return fi;
2988}
2989
2990/*
2991 * Use the first item in a ":for" list. Advance to the next.
2992 * Assign the values to the variable (list). "arg" points to the first one.
2993 * Return TRUE when a valid item was found, FALSE when at end of list or
2994 * something wrong.
2995 */
2996 int
2997next_for_item(fi_void, arg)
2998 void *fi_void;
2999 char_u *arg;
3000{
Bram Moolenaar33570922005-01-25 22:26:29 +00003001 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003002 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003003 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003004
3005 item = fi->fi_lw.lw_item;
3006 if (item == NULL)
3007 result = FALSE;
3008 else
3009 {
3010 fi->fi_lw.lw_item = item->li_next;
3011 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3012 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3013 }
3014 return result;
3015}
3016
3017/*
3018 * Free the structure used to store info used by ":for".
3019 */
3020 void
3021free_for_info(fi_void)
3022 void *fi_void;
3023{
Bram Moolenaar33570922005-01-25 22:26:29 +00003024 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003025
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003026 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003027 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003028 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003029 list_unref(fi->fi_list);
3030 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003031 vim_free(fi);
3032}
3033
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3035
3036 void
3037set_context_for_expression(xp, arg, cmdidx)
3038 expand_T *xp;
3039 char_u *arg;
3040 cmdidx_T cmdidx;
3041{
3042 int got_eq = FALSE;
3043 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003044 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003046 if (cmdidx == CMD_let)
3047 {
3048 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003049 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003050 {
3051 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003052 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003053 {
3054 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003055 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003056 if (vim_iswhite(*p))
3057 break;
3058 }
3059 return;
3060 }
3061 }
3062 else
3063 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3064 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 while ((xp->xp_pattern = vim_strpbrk(arg,
3066 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3067 {
3068 c = *xp->xp_pattern;
3069 if (c == '&')
3070 {
3071 c = xp->xp_pattern[1];
3072 if (c == '&')
3073 {
3074 ++xp->xp_pattern;
3075 xp->xp_context = cmdidx != CMD_let || got_eq
3076 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3077 }
3078 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003079 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003081 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3082 xp->xp_pattern += 2;
3083
3084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 }
3086 else if (c == '$')
3087 {
3088 /* environment variable */
3089 xp->xp_context = EXPAND_ENV_VARS;
3090 }
3091 else if (c == '=')
3092 {
3093 got_eq = TRUE;
3094 xp->xp_context = EXPAND_EXPRESSION;
3095 }
3096 else if (c == '<'
3097 && xp->xp_context == EXPAND_FUNCTIONS
3098 && vim_strchr(xp->xp_pattern, '(') == NULL)
3099 {
3100 /* Function name can start with "<SNR>" */
3101 break;
3102 }
3103 else if (cmdidx != CMD_let || got_eq)
3104 {
3105 if (c == '"') /* string */
3106 {
3107 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3108 if (c == '\\' && xp->xp_pattern[1] != NUL)
3109 ++xp->xp_pattern;
3110 xp->xp_context = EXPAND_NOTHING;
3111 }
3112 else if (c == '\'') /* literal string */
3113 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003114 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3116 /* skip */ ;
3117 xp->xp_context = EXPAND_NOTHING;
3118 }
3119 else if (c == '|')
3120 {
3121 if (xp->xp_pattern[1] == '|')
3122 {
3123 ++xp->xp_pattern;
3124 xp->xp_context = EXPAND_EXPRESSION;
3125 }
3126 else
3127 xp->xp_context = EXPAND_COMMANDS;
3128 }
3129 else
3130 xp->xp_context = EXPAND_EXPRESSION;
3131 }
3132 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003133 /* Doesn't look like something valid, expand as an expression
3134 * anyway. */
3135 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 arg = xp->xp_pattern;
3137 if (*arg != NUL)
3138 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3139 /* skip */ ;
3140 }
3141 xp->xp_pattern = arg;
3142}
3143
3144#endif /* FEAT_CMDL_COMPL */
3145
3146/*
3147 * ":1,25call func(arg1, arg2)" function call.
3148 */
3149 void
3150ex_call(eap)
3151 exarg_T *eap;
3152{
3153 char_u *arg = eap->arg;
3154 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003156 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003158 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 linenr_T lnum;
3160 int doesrange;
3161 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003162 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003164 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003165 if (fudi.fd_newkey != NULL)
3166 {
3167 /* Still need to give an error message for missing key. */
3168 EMSG2(_(e_dictkey), fudi.fd_newkey);
3169 vim_free(fudi.fd_newkey);
3170 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003171 if (tofree == NULL)
3172 return;
3173
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003174 /* Increase refcount on dictionary, it could get deleted when evaluating
3175 * the arguments. */
3176 if (fudi.fd_dict != NULL)
3177 ++fudi.fd_dict->dv_refcount;
3178
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003179 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003180 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003181 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
Bram Moolenaar532c7802005-01-27 14:44:31 +00003183 /* Skip white space to allow ":call func ()". Not good, but required for
3184 * backward compatibility. */
3185 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003186 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188 if (*startarg != '(')
3189 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003190 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 goto end;
3192 }
3193
3194 /*
3195 * When skipping, evaluate the function once, to find the end of the
3196 * arguments.
3197 * When the function takes a range, this is discovered after the first
3198 * call, and the loop is broken.
3199 */
3200 if (eap->skip)
3201 {
3202 ++emsg_skip;
3203 lnum = eap->line2; /* do it once, also with an invalid range */
3204 }
3205 else
3206 lnum = eap->line1;
3207 for ( ; lnum <= eap->line2; ++lnum)
3208 {
3209 if (!eap->skip && eap->addr_count > 0)
3210 {
3211 curwin->w_cursor.lnum = lnum;
3212 curwin->w_cursor.col = 0;
3213 }
3214 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003215 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003216 eap->line1, eap->line2, &doesrange,
3217 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
3219 failed = TRUE;
3220 break;
3221 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003222
3223 /* Handle a function returning a Funcref, Dictionary or List. */
3224 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3225 {
3226 failed = TRUE;
3227 break;
3228 }
3229
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003230 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (doesrange || eap->skip)
3232 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003233
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003235 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003236 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003237 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 if (aborting())
3239 break;
3240 }
3241 if (eap->skip)
3242 --emsg_skip;
3243
3244 if (!failed)
3245 {
3246 /* Check for trailing illegal characters and a following command. */
3247 if (!ends_excmd(*arg))
3248 {
3249 emsg_severe = TRUE;
3250 EMSG(_(e_trailing));
3251 }
3252 else
3253 eap->nextcmd = check_nextcmd(arg);
3254 }
3255
3256end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003257 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003258 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259}
3260
3261/*
3262 * ":unlet[!] var1 ... " command.
3263 */
3264 void
3265ex_unlet(eap)
3266 exarg_T *eap;
3267{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003268 ex_unletlock(eap, eap->arg, 0);
3269}
3270
3271/*
3272 * ":lockvar" and ":unlockvar" commands
3273 */
3274 void
3275ex_lockvar(eap)
3276 exarg_T *eap;
3277{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003279 int deep = 2;
3280
3281 if (eap->forceit)
3282 deep = -1;
3283 else if (vim_isdigit(*arg))
3284 {
3285 deep = getdigits(&arg);
3286 arg = skipwhite(arg);
3287 }
3288
3289 ex_unletlock(eap, arg, deep);
3290}
3291
3292/*
3293 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3294 */
3295 static void
3296ex_unletlock(eap, argstart, deep)
3297 exarg_T *eap;
3298 char_u *argstart;
3299 int deep;
3300{
3301 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003304 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
3306 do
3307 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003308 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003309 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3310 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003311 if (lv.ll_name == NULL)
3312 error = TRUE; /* error but continue parsing */
3313 if (name_end == NULL || (!vim_iswhite(*name_end)
3314 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003316 if (name_end != NULL)
3317 {
3318 emsg_severe = TRUE;
3319 EMSG(_(e_trailing));
3320 }
3321 if (!(eap->skip || error))
3322 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 break;
3324 }
3325
3326 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003327 {
3328 if (eap->cmdidx == CMD_unlet)
3329 {
3330 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3331 error = TRUE;
3332 }
3333 else
3334 {
3335 if (do_lock_var(&lv, name_end, deep,
3336 eap->cmdidx == CMD_lockvar) == FAIL)
3337 error = TRUE;
3338 }
3339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003341 if (!eap->skip)
3342 clear_lval(&lv);
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 arg = skipwhite(name_end);
3345 } while (!ends_excmd(*arg));
3346
3347 eap->nextcmd = check_nextcmd(arg);
3348}
3349
Bram Moolenaar8c711452005-01-14 21:53:12 +00003350 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003351do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003352 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003353 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003354 int forceit;
3355{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003356 int ret = OK;
3357 int cc;
3358
3359 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003360 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003361 cc = *name_end;
3362 *name_end = NUL;
3363
3364 /* Normal name or expanded name. */
3365 if (check_changedtick(lp->ll_name))
3366 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003367 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003368 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003369 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003370 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003371 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3372 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003373 else if (lp->ll_range)
3374 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003375 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003376
3377 /* Delete a range of List items. */
3378 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3379 {
3380 li = lp->ll_li->li_next;
3381 listitem_remove(lp->ll_list, lp->ll_li);
3382 lp->ll_li = li;
3383 ++lp->ll_n1;
3384 }
3385 }
3386 else
3387 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003388 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003389 /* unlet a List item. */
3390 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003391 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003392 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003393 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003394 }
3395
3396 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003397}
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399/*
3400 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003401 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 */
3403 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003404do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003406 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
Bram Moolenaar33570922005-01-25 22:26:29 +00003408 hashtab_T *ht;
3409 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003410 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
Bram Moolenaar33570922005-01-25 22:26:29 +00003412 ht = find_var_ht(name, &varname);
3413 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003415 hi = hash_find(ht, varname);
3416 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003417 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003418 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3419 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003420 if (var_check_ro(HI2DI(hi)->di_flags, name))
3421 return FAIL;
3422 delete_var(ht, hi);
3423 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003426 if (forceit)
3427 return OK;
3428 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 return FAIL;
3430}
3431
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003432/*
3433 * Lock or unlock variable indicated by "lp".
3434 * "deep" is the levels to go (-1 for unlimited);
3435 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3436 */
3437 static int
3438do_lock_var(lp, name_end, deep, lock)
3439 lval_T *lp;
3440 char_u *name_end;
3441 int deep;
3442 int lock;
3443{
3444 int ret = OK;
3445 int cc;
3446 dictitem_T *di;
3447
3448 if (deep == 0) /* nothing to do */
3449 return OK;
3450
3451 if (lp->ll_tv == NULL)
3452 {
3453 cc = *name_end;
3454 *name_end = NUL;
3455
3456 /* Normal name or expanded name. */
3457 if (check_changedtick(lp->ll_name))
3458 ret = FAIL;
3459 else
3460 {
3461 di = find_var(lp->ll_name, NULL);
3462 if (di == NULL)
3463 ret = FAIL;
3464 else
3465 {
3466 if (lock)
3467 di->di_flags |= DI_FLAGS_LOCK;
3468 else
3469 di->di_flags &= ~DI_FLAGS_LOCK;
3470 item_lock(&di->di_tv, deep, lock);
3471 }
3472 }
3473 *name_end = cc;
3474 }
3475 else if (lp->ll_range)
3476 {
3477 listitem_T *li = lp->ll_li;
3478
3479 /* (un)lock a range of List items. */
3480 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3481 {
3482 item_lock(&li->li_tv, deep, lock);
3483 li = li->li_next;
3484 ++lp->ll_n1;
3485 }
3486 }
3487 else if (lp->ll_list != NULL)
3488 /* (un)lock a List item. */
3489 item_lock(&lp->ll_li->li_tv, deep, lock);
3490 else
3491 /* un(lock) a Dictionary item. */
3492 item_lock(&lp->ll_di->di_tv, deep, lock);
3493
3494 return ret;
3495}
3496
3497/*
3498 * Lock or unlock an item. "deep" is nr of levels to go.
3499 */
3500 static void
3501item_lock(tv, deep, lock)
3502 typval_T *tv;
3503 int deep;
3504 int lock;
3505{
3506 static int recurse = 0;
3507 list_T *l;
3508 listitem_T *li;
3509 dict_T *d;
3510 hashitem_T *hi;
3511 int todo;
3512
3513 if (recurse >= DICT_MAXNEST)
3514 {
3515 EMSG(_("E743: variable nested too deep for (un)lock"));
3516 return;
3517 }
3518 if (deep == 0)
3519 return;
3520 ++recurse;
3521
3522 /* lock/unlock the item itself */
3523 if (lock)
3524 tv->v_lock |= VAR_LOCKED;
3525 else
3526 tv->v_lock &= ~VAR_LOCKED;
3527
3528 switch (tv->v_type)
3529 {
3530 case VAR_LIST:
3531 if ((l = tv->vval.v_list) != NULL)
3532 {
3533 if (lock)
3534 l->lv_lock |= VAR_LOCKED;
3535 else
3536 l->lv_lock &= ~VAR_LOCKED;
3537 if (deep < 0 || deep > 1)
3538 /* recursive: lock/unlock the items the List contains */
3539 for (li = l->lv_first; li != NULL; li = li->li_next)
3540 item_lock(&li->li_tv, deep - 1, lock);
3541 }
3542 break;
3543 case VAR_DICT:
3544 if ((d = tv->vval.v_dict) != NULL)
3545 {
3546 if (lock)
3547 d->dv_lock |= VAR_LOCKED;
3548 else
3549 d->dv_lock &= ~VAR_LOCKED;
3550 if (deep < 0 || deep > 1)
3551 {
3552 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003553 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003554 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3555 {
3556 if (!HASHITEM_EMPTY(hi))
3557 {
3558 --todo;
3559 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3560 }
3561 }
3562 }
3563 }
3564 }
3565 --recurse;
3566}
3567
Bram Moolenaara40058a2005-07-11 22:42:07 +00003568/*
3569 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3570 * it refers to a List or Dictionary that is locked.
3571 */
3572 static int
3573tv_islocked(tv)
3574 typval_T *tv;
3575{
3576 return (tv->v_lock & VAR_LOCKED)
3577 || (tv->v_type == VAR_LIST
3578 && tv->vval.v_list != NULL
3579 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3580 || (tv->v_type == VAR_DICT
3581 && tv->vval.v_dict != NULL
3582 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3583}
3584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3586/*
3587 * Delete all "menutrans_" variables.
3588 */
3589 void
3590del_menutrans_vars()
3591{
Bram Moolenaar33570922005-01-25 22:26:29 +00003592 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003593 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
Bram Moolenaar33570922005-01-25 22:26:29 +00003595 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003596 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003597 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003598 {
3599 if (!HASHITEM_EMPTY(hi))
3600 {
3601 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003602 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3603 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003604 }
3605 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003606 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607}
3608#endif
3609
3610#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3611
3612/*
3613 * Local string buffer for the next two functions to store a variable name
3614 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3615 * get_user_var_name().
3616 */
3617
3618static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3619
3620static char_u *varnamebuf = NULL;
3621static int varnamebuflen = 0;
3622
3623/*
3624 * Function to concatenate a prefix and a variable name.
3625 */
3626 static char_u *
3627cat_prefix_varname(prefix, name)
3628 int prefix;
3629 char_u *name;
3630{
3631 int len;
3632
3633 len = (int)STRLEN(name) + 3;
3634 if (len > varnamebuflen)
3635 {
3636 vim_free(varnamebuf);
3637 len += 10; /* some additional space */
3638 varnamebuf = alloc(len);
3639 if (varnamebuf == NULL)
3640 {
3641 varnamebuflen = 0;
3642 return NULL;
3643 }
3644 varnamebuflen = len;
3645 }
3646 *varnamebuf = prefix;
3647 varnamebuf[1] = ':';
3648 STRCPY(varnamebuf + 2, name);
3649 return varnamebuf;
3650}
3651
3652/*
3653 * Function given to ExpandGeneric() to obtain the list of user defined
3654 * (global/buffer/window/built-in) variable names.
3655 */
3656/*ARGSUSED*/
3657 char_u *
3658get_user_var_name(xp, idx)
3659 expand_T *xp;
3660 int idx;
3661{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003662 static long_u gdone;
3663 static long_u bdone;
3664 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003665#ifdef FEAT_WINDOWS
3666 static long_u tdone;
3667#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003668 static int vidx;
3669 static hashitem_T *hi;
3670 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
3672 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003673 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003674 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003675#ifdef FEAT_WINDOWS
3676 tdone = 0;
3677#endif
3678 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003679
3680 /* Global variables */
3681 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003683 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003684 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003685 else
3686 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003687 while (HASHITEM_EMPTY(hi))
3688 ++hi;
3689 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3690 return cat_prefix_varname('g', hi->hi_key);
3691 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003693
3694 /* b: variables */
3695 ht = &curbuf->b_vars.dv_hashtab;
3696 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003698 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003699 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003700 else
3701 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003702 while (HASHITEM_EMPTY(hi))
3703 ++hi;
3704 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003706 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003708 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 return (char_u *)"b:changedtick";
3710 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003711
3712 /* w: variables */
3713 ht = &curwin->w_vars.dv_hashtab;
3714 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003716 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003717 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003718 else
3719 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003720 while (HASHITEM_EMPTY(hi))
3721 ++hi;
3722 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003724
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003725#ifdef FEAT_WINDOWS
3726 /* t: variables */
3727 ht = &curtab->tp_vars.dv_hashtab;
3728 if (tdone < ht->ht_used)
3729 {
3730 if (tdone++ == 0)
3731 hi = ht->ht_array;
3732 else
3733 ++hi;
3734 while (HASHITEM_EMPTY(hi))
3735 ++hi;
3736 return cat_prefix_varname('t', hi->hi_key);
3737 }
3738#endif
3739
Bram Moolenaar33570922005-01-25 22:26:29 +00003740 /* v: variables */
3741 if (vidx < VV_LEN)
3742 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
3744 vim_free(varnamebuf);
3745 varnamebuf = NULL;
3746 varnamebuflen = 0;
3747 return NULL;
3748}
3749
3750#endif /* FEAT_CMDL_COMPL */
3751
3752/*
3753 * types for expressions.
3754 */
3755typedef enum
3756{
3757 TYPE_UNKNOWN = 0
3758 , TYPE_EQUAL /* == */
3759 , TYPE_NEQUAL /* != */
3760 , TYPE_GREATER /* > */
3761 , TYPE_GEQUAL /* >= */
3762 , TYPE_SMALLER /* < */
3763 , TYPE_SEQUAL /* <= */
3764 , TYPE_MATCH /* =~ */
3765 , TYPE_NOMATCH /* !~ */
3766} exptype_T;
3767
3768/*
3769 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003770 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3772 */
3773
3774/*
3775 * Handle zero level expression.
3776 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003777 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003778 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 * Return OK or FAIL.
3780 */
3781 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003782eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 char_u **nextcmd;
3786 int evaluate;
3787{
3788 int ret;
3789 char_u *p;
3790
3791 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003792 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 if (ret == FAIL || !ends_excmd(*p))
3794 {
3795 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003796 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 /*
3798 * Report the invalid expression unless the expression evaluation has
3799 * been cancelled due to an aborting error, an interrupt, or an
3800 * exception.
3801 */
3802 if (!aborting())
3803 EMSG2(_(e_invexpr2), arg);
3804 ret = FAIL;
3805 }
3806 if (nextcmd != NULL)
3807 *nextcmd = check_nextcmd(p);
3808
3809 return ret;
3810}
3811
3812/*
3813 * Handle top level expression:
3814 * expr1 ? expr0 : expr0
3815 *
3816 * "arg" must point to the first non-white of the expression.
3817 * "arg" is advanced to the next non-white after the recognized expression.
3818 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003819 * Note: "rettv.v_lock" is not set.
3820 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 * Return OK or FAIL.
3822 */
3823 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003824eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 int evaluate;
3828{
3829 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003830 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
3832 /*
3833 * Get the first variable.
3834 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003835 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 return FAIL;
3837
3838 if ((*arg)[0] == '?')
3839 {
3840 result = FALSE;
3841 if (evaluate)
3842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003843 int error = FALSE;
3844
3845 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003847 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003848 if (error)
3849 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
3851
3852 /*
3853 * Get the second variable.
3854 */
3855 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003856 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 return FAIL;
3858
3859 /*
3860 * Check for the ":".
3861 */
3862 if ((*arg)[0] != ':')
3863 {
3864 EMSG(_("E109: Missing ':' after '?'"));
3865 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003866 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 return FAIL;
3868 }
3869
3870 /*
3871 * Get the third variable.
3872 */
3873 *arg = skipwhite(*arg + 1);
3874 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3875 {
3876 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003877 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 return FAIL;
3879 }
3880 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003881 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883
3884 return OK;
3885}
3886
3887/*
3888 * Handle first level expression:
3889 * expr2 || expr2 || expr2 logical OR
3890 *
3891 * "arg" must point to the first non-white of the expression.
3892 * "arg" is advanced to the next non-white after the recognized expression.
3893 *
3894 * Return OK or FAIL.
3895 */
3896 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003897eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 int evaluate;
3901{
Bram Moolenaar33570922005-01-25 22:26:29 +00003902 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 long result;
3904 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003905 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
3907 /*
3908 * Get the first variable.
3909 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003910 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 return FAIL;
3912
3913 /*
3914 * Repeat until there is no following "||".
3915 */
3916 first = TRUE;
3917 result = FALSE;
3918 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3919 {
3920 if (evaluate && first)
3921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003922 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003924 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003925 if (error)
3926 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 first = FALSE;
3928 }
3929
3930 /*
3931 * Get the second variable.
3932 */
3933 *arg = skipwhite(*arg + 2);
3934 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3935 return FAIL;
3936
3937 /*
3938 * Compute the result.
3939 */
3940 if (evaluate && !result)
3941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003942 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003944 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003945 if (error)
3946 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
3948 if (evaluate)
3949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003950 rettv->v_type = VAR_NUMBER;
3951 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
3953 }
3954
3955 return OK;
3956}
3957
3958/*
3959 * Handle second level expression:
3960 * expr3 && expr3 && expr3 logical AND
3961 *
3962 * "arg" must point to the first non-white of the expression.
3963 * "arg" is advanced to the next non-white after the recognized expression.
3964 *
3965 * Return OK or FAIL.
3966 */
3967 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003968eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 int evaluate;
3972{
Bram Moolenaar33570922005-01-25 22:26:29 +00003973 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 long result;
3975 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003976 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
3978 /*
3979 * Get the first variable.
3980 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003981 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 return FAIL;
3983
3984 /*
3985 * Repeat until there is no following "&&".
3986 */
3987 first = TRUE;
3988 result = TRUE;
3989 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3990 {
3991 if (evaluate && first)
3992 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003993 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003995 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003996 if (error)
3997 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 first = FALSE;
3999 }
4000
4001 /*
4002 * Get the second variable.
4003 */
4004 *arg = skipwhite(*arg + 2);
4005 if (eval4(arg, &var2, evaluate && result) == FAIL)
4006 return FAIL;
4007
4008 /*
4009 * Compute the result.
4010 */
4011 if (evaluate && result)
4012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004013 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004016 if (error)
4017 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019 if (evaluate)
4020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004021 rettv->v_type = VAR_NUMBER;
4022 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 }
4024 }
4025
4026 return OK;
4027}
4028
4029/*
4030 * Handle third level expression:
4031 * var1 == var2
4032 * var1 =~ var2
4033 * var1 != var2
4034 * var1 !~ var2
4035 * var1 > var2
4036 * var1 >= var2
4037 * var1 < var2
4038 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004039 * var1 is var2
4040 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 *
4042 * "arg" must point to the first non-white of the expression.
4043 * "arg" is advanced to the next non-white after the recognized expression.
4044 *
4045 * Return OK or FAIL.
4046 */
4047 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 int evaluate;
4052{
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 char_u *p;
4055 int i;
4056 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004057 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 int len = 2;
4059 long n1, n2;
4060 char_u *s1, *s2;
4061 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4062 regmatch_T regmatch;
4063 int ic;
4064 char_u *save_cpo;
4065
4066 /*
4067 * Get the first variable.
4068 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 return FAIL;
4071
4072 p = *arg;
4073 switch (p[0])
4074 {
4075 case '=': if (p[1] == '=')
4076 type = TYPE_EQUAL;
4077 else if (p[1] == '~')
4078 type = TYPE_MATCH;
4079 break;
4080 case '!': if (p[1] == '=')
4081 type = TYPE_NEQUAL;
4082 else if (p[1] == '~')
4083 type = TYPE_NOMATCH;
4084 break;
4085 case '>': if (p[1] != '=')
4086 {
4087 type = TYPE_GREATER;
4088 len = 1;
4089 }
4090 else
4091 type = TYPE_GEQUAL;
4092 break;
4093 case '<': if (p[1] != '=')
4094 {
4095 type = TYPE_SMALLER;
4096 len = 1;
4097 }
4098 else
4099 type = TYPE_SEQUAL;
4100 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004101 case 'i': if (p[1] == 's')
4102 {
4103 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4104 len = 5;
4105 if (!vim_isIDc(p[len]))
4106 {
4107 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4108 type_is = TRUE;
4109 }
4110 }
4111 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113
4114 /*
4115 * If there is a comparitive operator, use it.
4116 */
4117 if (type != TYPE_UNKNOWN)
4118 {
4119 /* extra question mark appended: ignore case */
4120 if (p[len] == '?')
4121 {
4122 ic = TRUE;
4123 ++len;
4124 }
4125 /* extra '#' appended: match case */
4126 else if (p[len] == '#')
4127 {
4128 ic = FALSE;
4129 ++len;
4130 }
4131 /* nothing appened: use 'ignorecase' */
4132 else
4133 ic = p_ic;
4134
4135 /*
4136 * Get the second variable.
4137 */
4138 *arg = skipwhite(p + len);
4139 if (eval5(arg, &var2, evaluate) == FAIL)
4140 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 return FAIL;
4143 }
4144
4145 if (evaluate)
4146 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004147 if (type_is && rettv->v_type != var2.v_type)
4148 {
4149 /* For "is" a different type always means FALSE, for "notis"
4150 * it means TRUE. */
4151 n1 = (type == TYPE_NEQUAL);
4152 }
4153 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4154 {
4155 if (type_is)
4156 {
4157 n1 = (rettv->v_type == var2.v_type
4158 && rettv->vval.v_list == var2.vval.v_list);
4159 if (type == TYPE_NEQUAL)
4160 n1 = !n1;
4161 }
4162 else if (rettv->v_type != var2.v_type
4163 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4164 {
4165 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004166 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004167 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004168 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004169 clear_tv(rettv);
4170 clear_tv(&var2);
4171 return FAIL;
4172 }
4173 else
4174 {
4175 /* Compare two Lists for being equal or unequal. */
4176 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4177 if (type == TYPE_NEQUAL)
4178 n1 = !n1;
4179 }
4180 }
4181
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004182 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4183 {
4184 if (type_is)
4185 {
4186 n1 = (rettv->v_type == var2.v_type
4187 && rettv->vval.v_dict == var2.vval.v_dict);
4188 if (type == TYPE_NEQUAL)
4189 n1 = !n1;
4190 }
4191 else if (rettv->v_type != var2.v_type
4192 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4193 {
4194 if (rettv->v_type != var2.v_type)
4195 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4196 else
4197 EMSG(_("E736: Invalid operation for Dictionary"));
4198 clear_tv(rettv);
4199 clear_tv(&var2);
4200 return FAIL;
4201 }
4202 else
4203 {
4204 /* Compare two Dictionaries for being equal or unequal. */
4205 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4206 if (type == TYPE_NEQUAL)
4207 n1 = !n1;
4208 }
4209 }
4210
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004211 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4212 {
4213 if (rettv->v_type != var2.v_type
4214 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4215 {
4216 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004217 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004218 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004219 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004220 clear_tv(rettv);
4221 clear_tv(&var2);
4222 return FAIL;
4223 }
4224 else
4225 {
4226 /* Compare two Funcrefs for being equal or unequal. */
4227 if (rettv->vval.v_string == NULL
4228 || var2.vval.v_string == NULL)
4229 n1 = FALSE;
4230 else
4231 n1 = STRCMP(rettv->vval.v_string,
4232 var2.vval.v_string) == 0;
4233 if (type == TYPE_NEQUAL)
4234 n1 = !n1;
4235 }
4236 }
4237
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 /*
4239 * If one of the two variables is a number, compare as a number.
4240 * When using "=~" or "!~", always compare as string.
4241 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004242 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4244 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 n1 = get_tv_number(rettv);
4246 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 switch (type)
4248 {
4249 case TYPE_EQUAL: n1 = (n1 == n2); break;
4250 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4251 case TYPE_GREATER: n1 = (n1 > n2); break;
4252 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4253 case TYPE_SMALLER: n1 = (n1 < n2); break;
4254 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4255 case TYPE_UNKNOWN:
4256 case TYPE_MATCH:
4257 case TYPE_NOMATCH: break; /* avoid gcc warning */
4258 }
4259 }
4260 else
4261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 s1 = get_tv_string_buf(rettv, buf1);
4263 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4265 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4266 else
4267 i = 0;
4268 n1 = FALSE;
4269 switch (type)
4270 {
4271 case TYPE_EQUAL: n1 = (i == 0); break;
4272 case TYPE_NEQUAL: n1 = (i != 0); break;
4273 case TYPE_GREATER: n1 = (i > 0); break;
4274 case TYPE_GEQUAL: n1 = (i >= 0); break;
4275 case TYPE_SMALLER: n1 = (i < 0); break;
4276 case TYPE_SEQUAL: n1 = (i <= 0); break;
4277
4278 case TYPE_MATCH:
4279 case TYPE_NOMATCH:
4280 /* avoid 'l' flag in 'cpoptions' */
4281 save_cpo = p_cpo;
4282 p_cpo = (char_u *)"";
4283 regmatch.regprog = vim_regcomp(s2,
4284 RE_MAGIC + RE_STRING);
4285 regmatch.rm_ic = ic;
4286 if (regmatch.regprog != NULL)
4287 {
4288 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4289 vim_free(regmatch.regprog);
4290 if (type == TYPE_NOMATCH)
4291 n1 = !n1;
4292 }
4293 p_cpo = save_cpo;
4294 break;
4295
4296 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4297 }
4298 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 clear_tv(rettv);
4300 clear_tv(&var2);
4301 rettv->v_type = VAR_NUMBER;
4302 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304 }
4305
4306 return OK;
4307}
4308
4309/*
4310 * Handle fourth level expression:
4311 * + number addition
4312 * - number subtraction
4313 * . string concatenation
4314 *
4315 * "arg" must point to the first non-white of the expression.
4316 * "arg" is advanced to the next non-white after the recognized expression.
4317 *
4318 * Return OK or FAIL.
4319 */
4320 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 int evaluate;
4325{
Bram Moolenaar33570922005-01-25 22:26:29 +00004326 typval_T var2;
4327 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 int op;
4329 long n1, n2;
4330 char_u *s1, *s2;
4331 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4332 char_u *p;
4333
4334 /*
4335 * Get the first variable.
4336 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 return FAIL;
4339
4340 /*
4341 * Repeat computing, until no '+', '-' or '.' is following.
4342 */
4343 for (;;)
4344 {
4345 op = **arg;
4346 if (op != '+' && op != '-' && op != '.')
4347 break;
4348
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349 if (op != '+' || rettv->v_type != VAR_LIST)
4350 {
4351 /* For "list + ...", an illegal use of the first operand as
4352 * a number cannot be determined before evaluating the 2nd
4353 * operand: if this is also a list, all is ok.
4354 * For "something . ...", "something - ..." or "non-list + ...",
4355 * we know that the first operand needs to be a string or number
4356 * without evaluating the 2nd operand. So check before to avoid
4357 * side effects after an error. */
4358 if (evaluate && get_tv_string_chk(rettv) == NULL)
4359 {
4360 clear_tv(rettv);
4361 return FAIL;
4362 }
4363 }
4364
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 /*
4366 * Get the second variable.
4367 */
4368 *arg = skipwhite(*arg + 1);
4369 if (eval6(arg, &var2, evaluate) == FAIL)
4370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004371 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 return FAIL;
4373 }
4374
4375 if (evaluate)
4376 {
4377 /*
4378 * Compute the result.
4379 */
4380 if (op == '.')
4381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004382 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4383 s2 = get_tv_string_buf_chk(&var2, buf2);
4384 if (s2 == NULL) /* type error ? */
4385 {
4386 clear_tv(rettv);
4387 clear_tv(&var2);
4388 return FAIL;
4389 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004390 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 clear_tv(rettv);
4392 rettv->v_type = VAR_STRING;
4393 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004395 else if (op == '+' && rettv->v_type == VAR_LIST
4396 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004397 {
4398 /* concatenate Lists */
4399 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4400 &var3) == FAIL)
4401 {
4402 clear_tv(rettv);
4403 clear_tv(&var2);
4404 return FAIL;
4405 }
4406 clear_tv(rettv);
4407 *rettv = var3;
4408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 else
4410 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004411 int error = FALSE;
4412
4413 n1 = get_tv_number_chk(rettv, &error);
4414 if (error)
4415 {
4416 /* This can only happen for "list + non-list".
4417 * For "non-list + ..." or "something - ...", we returned
4418 * before evaluating the 2nd operand. */
4419 clear_tv(rettv);
4420 return FAIL;
4421 }
4422 n2 = get_tv_number_chk(&var2, &error);
4423 if (error)
4424 {
4425 clear_tv(rettv);
4426 clear_tv(&var2);
4427 return FAIL;
4428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004429 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (op == '+')
4431 n1 = n1 + n2;
4432 else
4433 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004434 rettv->v_type = VAR_NUMBER;
4435 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
4439 }
4440 return OK;
4441}
4442
4443/*
4444 * Handle fifth level expression:
4445 * * number multiplication
4446 * / number division
4447 * % number modulo
4448 *
4449 * "arg" must point to the first non-white of the expression.
4450 * "arg" is advanced to the next non-white after the recognized expression.
4451 *
4452 * Return OK or FAIL.
4453 */
4454 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 int evaluate;
4459{
Bram Moolenaar33570922005-01-25 22:26:29 +00004460 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 int op;
4462 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004463 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
4465 /*
4466 * Get the first variable.
4467 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004468 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 return FAIL;
4470
4471 /*
4472 * Repeat computing, until no '*', '/' or '%' is following.
4473 */
4474 for (;;)
4475 {
4476 op = **arg;
4477 if (op != '*' && op != '/' && op != '%')
4478 break;
4479
4480 if (evaluate)
4481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004482 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004483 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004484 if (error)
4485 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487 else
4488 n1 = 0;
4489
4490 /*
4491 * Get the second variable.
4492 */
4493 *arg = skipwhite(*arg + 1);
4494 if (eval7(arg, &var2, evaluate) == FAIL)
4495 return FAIL;
4496
4497 if (evaluate)
4498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004499 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004500 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004501 if (error)
4502 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
4504 /*
4505 * Compute the result.
4506 */
4507 if (op == '*')
4508 n1 = n1 * n2;
4509 else if (op == '/')
4510 {
4511 if (n2 == 0) /* give an error message? */
4512 n1 = 0x7fffffffL;
4513 else
4514 n1 = n1 / n2;
4515 }
4516 else
4517 {
4518 if (n2 == 0) /* give an error message? */
4519 n1 = 0;
4520 else
4521 n1 = n1 % n2;
4522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004523 rettv->v_type = VAR_NUMBER;
4524 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526 }
4527
4528 return OK;
4529}
4530
4531/*
4532 * Handle sixth level expression:
4533 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004534 * "string" string constant
4535 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 * &option-name option value
4537 * @r register contents
4538 * identifier variable value
4539 * function() function call
4540 * $VAR environment variable
4541 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004542 * [expr, expr] List
4543 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 *
4545 * Also handle:
4546 * ! in front logical NOT
4547 * - in front unary minus
4548 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004549 * trailing [] subscript in String or List
4550 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 *
4552 * "arg" must point to the first non-white of the expression.
4553 * "arg" is advanced to the next non-white after the recognized expression.
4554 *
4555 * Return OK or FAIL.
4556 */
4557 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004558eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 int evaluate;
4562{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 long n;
4564 int len;
4565 char_u *s;
4566 int val;
4567 char_u *start_leader, *end_leader;
4568 int ret = OK;
4569 char_u *alias;
4570
4571 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004572 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004573 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576
4577 /*
4578 * Skip '!' and '-' characters. They are handled later.
4579 */
4580 start_leader = *arg;
4581 while (**arg == '!' || **arg == '-' || **arg == '+')
4582 *arg = skipwhite(*arg + 1);
4583 end_leader = *arg;
4584
4585 switch (**arg)
4586 {
4587 /*
4588 * Number constant.
4589 */
4590 case '0':
4591 case '1':
4592 case '2':
4593 case '3':
4594 case '4':
4595 case '5':
4596 case '6':
4597 case '7':
4598 case '8':
4599 case '9':
4600 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4601 *arg += len;
4602 if (evaluate)
4603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004604 rettv->v_type = VAR_NUMBER;
4605 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
4607 break;
4608
4609 /*
4610 * String constant: "string".
4611 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004612 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 break;
4614
4615 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004616 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004618 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 break;
4620
4621 /*
4622 * List: [expr, expr]
4623 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004624 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 break;
4626
4627 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004628 * Dictionary: {key: val, key: val}
4629 */
4630 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4631 break;
4632
4633 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004634 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004636 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 break;
4638
4639 /*
4640 * Environment variable: $VAR.
4641 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004642 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 break;
4644
4645 /*
4646 * Register contents: @r.
4647 */
4648 case '@': ++*arg;
4649 if (evaluate)
4650 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004651 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004652 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 }
4654 if (**arg != NUL)
4655 ++*arg;
4656 break;
4657
4658 /*
4659 * nested expression: (expression).
4660 */
4661 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004662 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 if (**arg == ')')
4664 ++*arg;
4665 else if (ret == OK)
4666 {
4667 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 ret = FAIL;
4670 }
4671 break;
4672
Bram Moolenaar8c711452005-01-14 21:53:12 +00004673 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 break;
4675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004676
4677 if (ret == NOTDONE)
4678 {
4679 /*
4680 * Must be a variable or function name.
4681 * Can also be a curly-braces kind of name: {expr}.
4682 */
4683 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004684 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004685 if (alias != NULL)
4686 s = alias;
4687
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004688 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689 ret = FAIL;
4690 else
4691 {
4692 if (**arg == '(') /* recursive! */
4693 {
4694 /* If "s" is the name of a variable of type VAR_FUNC
4695 * use its contents. */
4696 s = deref_func_name(s, &len);
4697
4698 /* Invoke the function. */
4699 ret = get_func_tv(s, len, rettv, arg,
4700 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004701 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004702 /* Stop the expression evaluation when immediately
4703 * aborting on error, or when an interrupt occurred or
4704 * an exception was thrown but not caught. */
4705 if (aborting())
4706 {
4707 if (ret == OK)
4708 clear_tv(rettv);
4709 ret = FAIL;
4710 }
4711 }
4712 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004713 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004714 else
4715 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004716 }
4717
4718 if (alias != NULL)
4719 vim_free(alias);
4720 }
4721
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 *arg = skipwhite(*arg);
4723
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004724 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4725 * expr(expr). */
4726 if (ret == OK)
4727 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728
4729 /*
4730 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4731 */
4732 if (ret == OK && evaluate && end_leader > start_leader)
4733 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004734 int error = FALSE;
4735
4736 val = get_tv_number_chk(rettv, &error);
4737 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004739 clear_tv(rettv);
4740 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004742 else
4743 {
4744 while (end_leader > start_leader)
4745 {
4746 --end_leader;
4747 if (*end_leader == '!')
4748 val = !val;
4749 else if (*end_leader == '-')
4750 val = -val;
4751 }
4752 clear_tv(rettv);
4753 rettv->v_type = VAR_NUMBER;
4754 rettv->vval.v_number = val;
4755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757
4758 return ret;
4759}
4760
4761/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004762 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4763 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4765 */
4766 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004767eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004769 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004771 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772{
4773 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004774 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004776 long len = -1;
4777 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004779 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004783 if (verbose)
4784 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004785 return FAIL;
4786 }
4787
Bram Moolenaar8c711452005-01-14 21:53:12 +00004788 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004789 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 /*
4791 * dict.name
4792 */
4793 key = *arg + 1;
4794 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4795 ;
4796 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004798 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799 }
4800 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004802 /*
4803 * something[idx]
4804 *
4805 * Get the (first) variable from inside the [].
4806 */
4807 *arg = skipwhite(*arg + 1);
4808 if (**arg == ':')
4809 empty1 = TRUE;
4810 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4811 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004812 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4813 {
4814 /* not a number or string */
4815 clear_tv(&var1);
4816 return FAIL;
4817 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004818
4819 /*
4820 * Get the second variable from inside the [:].
4821 */
4822 if (**arg == ':')
4823 {
4824 range = TRUE;
4825 *arg = skipwhite(*arg + 1);
4826 if (**arg == ']')
4827 empty2 = TRUE;
4828 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830 if (!empty1)
4831 clear_tv(&var1);
4832 return FAIL;
4833 }
4834 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4835 {
4836 /* not a number or string */
4837 if (!empty1)
4838 clear_tv(&var1);
4839 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004840 return FAIL;
4841 }
4842 }
4843
4844 /* Check for the ']'. */
4845 if (**arg != ']')
4846 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004847 if (verbose)
4848 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004849 clear_tv(&var1);
4850 if (range)
4851 clear_tv(&var2);
4852 return FAIL;
4853 }
4854 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 }
4856
4857 if (evaluate)
4858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004859 n1 = 0;
4860 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004862 n1 = get_tv_number(&var1);
4863 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004864 }
4865 if (range)
4866 {
4867 if (empty2)
4868 n2 = -1;
4869 else
4870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004871 n2 = get_tv_number(&var2);
4872 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004873 }
4874 }
4875
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004876 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004877 {
4878 case VAR_NUMBER:
4879 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004881 len = (long)STRLEN(s);
4882 if (range)
4883 {
4884 /* The resulting variable is a substring. If the indexes
4885 * are out of range the result is empty. */
4886 if (n1 < 0)
4887 {
4888 n1 = len + n1;
4889 if (n1 < 0)
4890 n1 = 0;
4891 }
4892 if (n2 < 0)
4893 n2 = len + n2;
4894 else if (n2 >= len)
4895 n2 = len;
4896 if (n1 >= len || n2 < 0 || n1 > n2)
4897 s = NULL;
4898 else
4899 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4900 }
4901 else
4902 {
4903 /* The resulting variable is a string of a single
4904 * character. If the index is too big or negative the
4905 * result is empty. */
4906 if (n1 >= len || n1 < 0)
4907 s = NULL;
4908 else
4909 s = vim_strnsave(s + n1, 1);
4910 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 clear_tv(rettv);
4912 rettv->v_type = VAR_STRING;
4913 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004914 break;
4915
4916 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004917 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004918 if (n1 < 0)
4919 n1 = len + n1;
4920 if (!empty1 && (n1 < 0 || n1 >= len))
4921 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004922 /* For a range we allow invalid values and return an empty
4923 * list. A list index out of range is an error. */
4924 if (!range)
4925 {
4926 if (verbose)
4927 EMSGN(_(e_listidx), n1);
4928 return FAIL;
4929 }
4930 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004931 }
4932 if (range)
4933 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004934 list_T *l;
4935 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004936
4937 if (n2 < 0)
4938 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004939 else if (n2 >= len)
4940 n2 = len - 1;
4941 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004942 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004943 l = list_alloc();
4944 if (l == NULL)
4945 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004946 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004947 n1 <= n2; ++n1)
4948 {
4949 if (list_append_tv(l, &item->li_tv) == FAIL)
4950 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004951 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004952 return FAIL;
4953 }
4954 item = item->li_next;
4955 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004956 clear_tv(rettv);
4957 rettv->v_type = VAR_LIST;
4958 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004959 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004960 }
4961 else
4962 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004963 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 clear_tv(rettv);
4965 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004966 }
4967 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968
4969 case VAR_DICT:
4970 if (range)
4971 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004972 if (verbose)
4973 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 if (len == -1)
4975 clear_tv(&var1);
4976 return FAIL;
4977 }
4978 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004979 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980
4981 if (len == -1)
4982 {
4983 key = get_tv_string(&var1);
4984 if (*key == NUL)
4985 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004986 if (verbose)
4987 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 clear_tv(&var1);
4989 return FAIL;
4990 }
4991 }
4992
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004993 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004995 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004996 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004997 if (len == -1)
4998 clear_tv(&var1);
4999 if (item == NULL)
5000 return FAIL;
5001
5002 copy_tv(&item->di_tv, &var1);
5003 clear_tv(rettv);
5004 *rettv = var1;
5005 }
5006 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005007 }
5008 }
5009
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005010 return OK;
5011}
5012
5013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 * Get an option value.
5015 * "arg" points to the '&' or '+' before the option name.
5016 * "arg" is advanced to character after the option name.
5017 * Return OK or FAIL.
5018 */
5019 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005020get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005022 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 int evaluate;
5024{
5025 char_u *option_end;
5026 long numval;
5027 char_u *stringval;
5028 int opt_type;
5029 int c;
5030 int working = (**arg == '+'); /* has("+option") */
5031 int ret = OK;
5032 int opt_flags;
5033
5034 /*
5035 * Isolate the option name and find its value.
5036 */
5037 option_end = find_option_end(arg, &opt_flags);
5038 if (option_end == NULL)
5039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005040 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 EMSG2(_("E112: Option name missing: %s"), *arg);
5042 return FAIL;
5043 }
5044
5045 if (!evaluate)
5046 {
5047 *arg = option_end;
5048 return OK;
5049 }
5050
5051 c = *option_end;
5052 *option_end = NUL;
5053 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005054 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055
5056 if (opt_type == -3) /* invalid name */
5057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005058 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 EMSG2(_("E113: Unknown option: %s"), *arg);
5060 ret = FAIL;
5061 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005062 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 if (opt_type == -2) /* hidden string option */
5065 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005066 rettv->v_type = VAR_STRING;
5067 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069 else if (opt_type == -1) /* hidden number option */
5070 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 rettv->v_type = VAR_NUMBER;
5072 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074 else if (opt_type == 1) /* number option */
5075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005076 rettv->v_type = VAR_NUMBER;
5077 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079 else /* string option */
5080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005081 rettv->v_type = VAR_STRING;
5082 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 }
5084 }
5085 else if (working && (opt_type == -2 || opt_type == -1))
5086 ret = FAIL;
5087
5088 *option_end = c; /* put back for error messages */
5089 *arg = option_end;
5090
5091 return ret;
5092}
5093
5094/*
5095 * Allocate a variable for a string constant.
5096 * Return OK or FAIL.
5097 */
5098 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005099get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 int evaluate;
5103{
5104 char_u *p;
5105 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 int extra = 0;
5107
5108 /*
5109 * Find the end of the string, skipping backslashed characters.
5110 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005111 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 if (*p == '\\' && p[1] != NUL)
5114 {
5115 ++p;
5116 /* A "\<x>" form occupies at least 4 characters, and produces up
5117 * to 6 characters: reserve space for 2 extra */
5118 if (*p == '<')
5119 extra += 2;
5120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 }
5122
5123 if (*p != '"')
5124 {
5125 EMSG2(_("E114: Missing quote: %s"), *arg);
5126 return FAIL;
5127 }
5128
5129 /* If only parsing, set *arg and return here */
5130 if (!evaluate)
5131 {
5132 *arg = p + 1;
5133 return OK;
5134 }
5135
5136 /*
5137 * Copy the string into allocated memory, handling backslashed
5138 * characters.
5139 */
5140 name = alloc((unsigned)(p - *arg + extra));
5141 if (name == NULL)
5142 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 rettv->v_type = VAR_STRING;
5144 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
5148 if (*p == '\\')
5149 {
5150 switch (*++p)
5151 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 case 'b': *name++ = BS; ++p; break;
5153 case 'e': *name++ = ESC; ++p; break;
5154 case 'f': *name++ = FF; ++p; break;
5155 case 'n': *name++ = NL; ++p; break;
5156 case 'r': *name++ = CAR; ++p; break;
5157 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158
5159 case 'X': /* hex: "\x1", "\x12" */
5160 case 'x':
5161 case 'u': /* Unicode: "\u0023" */
5162 case 'U':
5163 if (vim_isxdigit(p[1]))
5164 {
5165 int n, nr;
5166 int c = toupper(*p);
5167
5168 if (c == 'X')
5169 n = 2;
5170 else
5171 n = 4;
5172 nr = 0;
5173 while (--n >= 0 && vim_isxdigit(p[1]))
5174 {
5175 ++p;
5176 nr = (nr << 4) + hex2nr(*p);
5177 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179#ifdef FEAT_MBYTE
5180 /* For "\u" store the number according to
5181 * 'encoding'. */
5182 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 else
5185#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 break;
5189
5190 /* octal: "\1", "\12", "\123" */
5191 case '0':
5192 case '1':
5193 case '2':
5194 case '3':
5195 case '4':
5196 case '5':
5197 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005198 case '7': *name = *p++ - '0';
5199 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201 *name = (*name << 3) + *p++ - '0';
5202 if (*p >= '0' && *p <= '7')
5203 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005205 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 break;
5207
5208 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (extra != 0)
5211 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 break;
5214 }
5215 /* FALLTHROUGH */
5216
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 break;
5219 }
5220 }
5221 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 *arg = p + 1;
5227
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 return OK;
5229}
5230
5231/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 * Return OK or FAIL.
5234 */
5235 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005236get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 int evaluate;
5240{
5241 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005242 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005243 int reduce = 0;
5244
5245 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005247 */
5248 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5249 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005251 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005253 break;
5254 ++reduce;
5255 ++p;
5256 }
5257 }
5258
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005260 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005262 return FAIL;
5263 }
5264
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005266 if (!evaluate)
5267 {
5268 *arg = p + 1;
5269 return OK;
5270 }
5271
5272 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005274 */
5275 str = alloc((unsigned)((p - *arg) - reduce));
5276 if (str == NULL)
5277 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278 rettv->v_type = VAR_STRING;
5279 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005280
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005282 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005284 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005285 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005286 break;
5287 ++p;
5288 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005292 *arg = p + 1;
5293
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005294 return OK;
5295}
5296
5297/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 * Allocate a variable for a List and fill it from "*arg".
5299 * Return OK or FAIL.
5300 */
5301 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005302get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005304 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 int evaluate;
5306{
Bram Moolenaar33570922005-01-25 22:26:29 +00005307 list_T *l = NULL;
5308 typval_T tv;
5309 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310
5311 if (evaluate)
5312 {
5313 l = list_alloc();
5314 if (l == NULL)
5315 return FAIL;
5316 }
5317
5318 *arg = skipwhite(*arg + 1);
5319 while (**arg != ']' && **arg != NUL)
5320 {
5321 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5322 goto failret;
5323 if (evaluate)
5324 {
5325 item = listitem_alloc();
5326 if (item != NULL)
5327 {
5328 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005329 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330 list_append(l, item);
5331 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005332 else
5333 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 }
5335
5336 if (**arg == ']')
5337 break;
5338 if (**arg != ',')
5339 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005340 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 goto failret;
5342 }
5343 *arg = skipwhite(*arg + 1);
5344 }
5345
5346 if (**arg != ']')
5347 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349failret:
5350 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005351 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 return FAIL;
5353 }
5354
5355 *arg = skipwhite(*arg + 1);
5356 if (evaluate)
5357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005358 rettv->v_type = VAR_LIST;
5359 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 ++l->lv_refcount;
5361 }
5362
5363 return OK;
5364}
5365
5366/*
5367 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005368 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005370 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371list_alloc()
5372{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005373 list_T *l;
5374
5375 l = (list_T *)alloc_clear(sizeof(list_T));
5376 if (l != NULL)
5377 {
5378 /* Prepend the list to the list of lists for garbage collection. */
5379 if (first_list != NULL)
5380 first_list->lv_used_prev = l;
5381 l->lv_used_prev = NULL;
5382 l->lv_used_next = first_list;
5383 first_list = l;
5384 }
5385 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386}
5387
5388/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005389 * Allocate an empty list for a return value.
5390 * Returns OK or FAIL.
5391 */
5392 static int
5393rettv_list_alloc(rettv)
5394 typval_T *rettv;
5395{
5396 list_T *l = list_alloc();
5397
5398 if (l == NULL)
5399 return FAIL;
5400
5401 rettv->vval.v_list = l;
5402 rettv->v_type = VAR_LIST;
5403 ++l->lv_refcount;
5404 return OK;
5405}
5406
5407/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 * Unreference a list: decrement the reference count and free it when it
5409 * becomes zero.
5410 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005411 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005413 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005415 if (l != NULL && --l->lv_refcount <= 0)
5416 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417}
5418
5419/*
5420 * Free a list, including all items it points to.
5421 * Ignores the reference count.
5422 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005423 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005424list_free(l, recurse)
5425 list_T *l;
5426 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427{
Bram Moolenaar33570922005-01-25 22:26:29 +00005428 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005430 /* Remove the list from the list of lists for garbage collection. */
5431 if (l->lv_used_prev == NULL)
5432 first_list = l->lv_used_next;
5433 else
5434 l->lv_used_prev->lv_used_next = l->lv_used_next;
5435 if (l->lv_used_next != NULL)
5436 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5437
Bram Moolenaard9fba312005-06-26 22:34:35 +00005438 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005440 /* Remove the item before deleting it. */
5441 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005442 if (recurse || (item->li_tv.v_type != VAR_LIST
5443 && item->li_tv.v_type != VAR_DICT))
5444 clear_tv(&item->li_tv);
5445 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 }
5447 vim_free(l);
5448}
5449
5450/*
5451 * Allocate a list item.
5452 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005453 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454listitem_alloc()
5455{
Bram Moolenaar33570922005-01-25 22:26:29 +00005456 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457}
5458
5459/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005460 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 */
5462 static void
5463listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005464 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 vim_free(item);
5468}
5469
5470/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005471 * Remove a list item from a List and free it. Also clears the value.
5472 */
5473 static void
5474listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005475 list_T *l;
5476 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005477{
5478 list_remove(l, item, item);
5479 listitem_free(item);
5480}
5481
5482/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 * Get the number of items in a list.
5484 */
5485 static long
5486list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005487 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 if (l == NULL)
5490 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005491 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492}
5493
5494/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005495 * Return TRUE when two lists have exactly the same values.
5496 */
5497 static int
5498list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005499 list_T *l1;
5500 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005501 int ic; /* ignore case for strings */
5502{
Bram Moolenaar33570922005-01-25 22:26:29 +00005503 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005504
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005505 if (l1 == l2)
5506 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005507 if (list_len(l1) != list_len(l2))
5508 return FALSE;
5509
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005510 for (item1 = l1->lv_first, item2 = l2->lv_first;
5511 item1 != NULL && item2 != NULL;
5512 item1 = item1->li_next, item2 = item2->li_next)
5513 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5514 return FALSE;
5515 return item1 == NULL && item2 == NULL;
5516}
5517
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005518#if defined(FEAT_PYTHON) || defined(PROTO)
5519/*
5520 * Return the dictitem that an entry in a hashtable points to.
5521 */
5522 dictitem_T *
5523dict_lookup(hi)
5524 hashitem_T *hi;
5525{
5526 return HI2DI(hi);
5527}
5528#endif
5529
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005530/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005531 * Return TRUE when two dictionaries have exactly the same key/values.
5532 */
5533 static int
5534dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005535 dict_T *d1;
5536 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005537 int ic; /* ignore case for strings */
5538{
Bram Moolenaar33570922005-01-25 22:26:29 +00005539 hashitem_T *hi;
5540 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005541 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005542
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005543 if (d1 == d2)
5544 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005545 if (dict_len(d1) != dict_len(d2))
5546 return FALSE;
5547
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005548 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005549 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005550 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005551 if (!HASHITEM_EMPTY(hi))
5552 {
5553 item2 = dict_find(d2, hi->hi_key, -1);
5554 if (item2 == NULL)
5555 return FALSE;
5556 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5557 return FALSE;
5558 --todo;
5559 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005560 }
5561 return TRUE;
5562}
5563
5564/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005565 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005566 * Compares the items just like "==" would compare them, but strings and
5567 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005568 */
5569 static int
5570tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005571 typval_T *tv1;
5572 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005573 int ic; /* ignore case */
5574{
5575 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005576 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005577 static int recursive = 0; /* cach recursive loops */
5578 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005579
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005580 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005581 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005582 /* Catch lists and dicts that have an endless loop by limiting
5583 * recursiveness to 1000. We guess they are equal then. */
5584 if (recursive >= 1000)
5585 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005586
5587 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005588 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005589 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005590 ++recursive;
5591 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5592 --recursive;
5593 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005594
5595 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005596 ++recursive;
5597 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5598 --recursive;
5599 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005600
5601 case VAR_FUNC:
5602 return (tv1->vval.v_string != NULL
5603 && tv2->vval.v_string != NULL
5604 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5605
5606 case VAR_NUMBER:
5607 return tv1->vval.v_number == tv2->vval.v_number;
5608
5609 case VAR_STRING:
5610 s1 = get_tv_string_buf(tv1, buf1);
5611 s2 = get_tv_string_buf(tv2, buf2);
5612 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005613 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005614
5615 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005616 return TRUE;
5617}
5618
5619/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 * Locate item with index "n" in list "l" and return it.
5621 * A negative index is counted from the end; -1 is the last item.
5622 * Returns NULL when "n" is out of range.
5623 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005624 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005625list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005626 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 long n;
5628{
Bram Moolenaar33570922005-01-25 22:26:29 +00005629 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005630 long idx;
5631
5632 if (l == NULL)
5633 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005634
5635 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005636 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005637 n = l->lv_len + n;
5638
5639 /* Check for index out of range. */
5640 if (n < 0 || n >= l->lv_len)
5641 return NULL;
5642
5643 /* When there is a cached index may start search from there. */
5644 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005646 if (n < l->lv_idx / 2)
5647 {
5648 /* closest to the start of the list */
5649 item = l->lv_first;
5650 idx = 0;
5651 }
5652 else if (n > (l->lv_idx + l->lv_len) / 2)
5653 {
5654 /* closest to the end of the list */
5655 item = l->lv_last;
5656 idx = l->lv_len - 1;
5657 }
5658 else
5659 {
5660 /* closest to the cached index */
5661 item = l->lv_idx_item;
5662 idx = l->lv_idx;
5663 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005664 }
5665 else
5666 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005667 if (n < l->lv_len / 2)
5668 {
5669 /* closest to the start of the list */
5670 item = l->lv_first;
5671 idx = 0;
5672 }
5673 else
5674 {
5675 /* closest to the end of the list */
5676 item = l->lv_last;
5677 idx = l->lv_len - 1;
5678 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005680
5681 while (n > idx)
5682 {
5683 /* search forward */
5684 item = item->li_next;
5685 ++idx;
5686 }
5687 while (n < idx)
5688 {
5689 /* search backward */
5690 item = item->li_prev;
5691 --idx;
5692 }
5693
5694 /* cache the used index */
5695 l->lv_idx = idx;
5696 l->lv_idx_item = item;
5697
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698 return item;
5699}
5700
5701/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005702 * Get list item "l[idx]" as a number.
5703 */
5704 static long
5705list_find_nr(l, idx, errorp)
5706 list_T *l;
5707 long idx;
5708 int *errorp; /* set to TRUE when something wrong */
5709{
5710 listitem_T *li;
5711
5712 li = list_find(l, idx);
5713 if (li == NULL)
5714 {
5715 if (errorp != NULL)
5716 *errorp = TRUE;
5717 return -1L;
5718 }
5719 return get_tv_number_chk(&li->li_tv, errorp);
5720}
5721
5722/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005723 * Locate "item" list "l" and return its index.
5724 * Returns -1 when "item" is not in the list.
5725 */
5726 static long
5727list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005728 list_T *l;
5729 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005730{
5731 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005733
5734 if (l == NULL)
5735 return -1;
5736 idx = 0;
5737 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5738 ++idx;
5739 if (li == NULL)
5740 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005741 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005742}
5743
5744/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745 * Append item "item" to the end of list "l".
5746 */
5747 static void
5748list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005749 list_T *l;
5750 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005751{
5752 if (l->lv_last == NULL)
5753 {
5754 /* empty list */
5755 l->lv_first = item;
5756 l->lv_last = item;
5757 item->li_prev = NULL;
5758 }
5759 else
5760 {
5761 l->lv_last->li_next = item;
5762 item->li_prev = l->lv_last;
5763 l->lv_last = item;
5764 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005765 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 item->li_next = NULL;
5767}
5768
5769/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005770 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005771 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005772 */
5773 static int
5774list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005775 list_T *l;
5776 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005778 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779
Bram Moolenaar05159a02005-02-26 23:04:13 +00005780 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005781 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005782 copy_tv(tv, &li->li_tv);
5783 list_append(l, li);
5784 return OK;
5785}
5786
5787/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005788 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005789 * Return FAIL when out of memory.
5790 */
5791 int
5792list_append_dict(list, dict)
5793 list_T *list;
5794 dict_T *dict;
5795{
5796 listitem_T *li = listitem_alloc();
5797
5798 if (li == NULL)
5799 return FAIL;
5800 li->li_tv.v_type = VAR_DICT;
5801 li->li_tv.v_lock = 0;
5802 li->li_tv.vval.v_dict = dict;
5803 list_append(list, li);
5804 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805 return OK;
5806}
5807
5808/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005809 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005810 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005811 * Returns FAIL when out of memory.
5812 */
5813 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005814list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005815 list_T *l;
5816 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005817 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005818{
5819 listitem_T *li = listitem_alloc();
5820
5821 if (li == NULL)
5822 return FAIL;
5823 list_append(l, li);
5824 li->li_tv.v_type = VAR_STRING;
5825 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005826 if (str == NULL)
5827 li->li_tv.vval.v_string = NULL;
5828 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005829 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005830 return FAIL;
5831 return OK;
5832}
5833
5834/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005835 * Append "n" to list "l".
5836 * Returns FAIL when out of memory.
5837 */
5838 static int
5839list_append_number(l, n)
5840 list_T *l;
5841 varnumber_T n;
5842{
5843 listitem_T *li;
5844
5845 li = listitem_alloc();
5846 if (li == NULL)
5847 return FAIL;
5848 li->li_tv.v_type = VAR_NUMBER;
5849 li->li_tv.v_lock = 0;
5850 li->li_tv.vval.v_number = n;
5851 list_append(l, li);
5852 return OK;
5853}
5854
5855/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005856 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005857 * If "item" is NULL append at the end.
5858 * Return FAIL when out of memory.
5859 */
5860 static int
5861list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005862 list_T *l;
5863 typval_T *tv;
5864 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005865{
Bram Moolenaar33570922005-01-25 22:26:29 +00005866 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005867
5868 if (ni == NULL)
5869 return FAIL;
5870 copy_tv(tv, &ni->li_tv);
5871 if (item == NULL)
5872 /* Append new item at end of list. */
5873 list_append(l, ni);
5874 else
5875 {
5876 /* Insert new item before existing item. */
5877 ni->li_prev = item->li_prev;
5878 ni->li_next = item;
5879 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005880 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005881 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005882 ++l->lv_idx;
5883 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005884 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005885 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005886 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005887 l->lv_idx_item = NULL;
5888 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005889 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005890 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005891 }
5892 return OK;
5893}
5894
5895/*
5896 * Extend "l1" with "l2".
5897 * If "bef" is NULL append at the end, otherwise insert before this item.
5898 * Returns FAIL when out of memory.
5899 */
5900 static int
5901list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 list_T *l1;
5903 list_T *l2;
5904 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005905{
Bram Moolenaar33570922005-01-25 22:26:29 +00005906 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005907
5908 for (item = l2->lv_first; item != NULL; item = item->li_next)
5909 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5910 return FAIL;
5911 return OK;
5912}
5913
5914/*
5915 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5916 * Return FAIL when out of memory.
5917 */
5918 static int
5919list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 list_T *l1;
5921 list_T *l2;
5922 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005923{
Bram Moolenaar33570922005-01-25 22:26:29 +00005924 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005925
5926 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005927 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005928 if (l == NULL)
5929 return FAIL;
5930 tv->v_type = VAR_LIST;
5931 tv->vval.v_list = l;
5932
5933 /* append all items from the second list */
5934 return list_extend(l, l2, NULL);
5935}
5936
5937/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005938 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005940 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 * Returns NULL when out of memory.
5942 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005943 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005944list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005945 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005947 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948{
Bram Moolenaar33570922005-01-25 22:26:29 +00005949 list_T *copy;
5950 listitem_T *item;
5951 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952
5953 if (orig == NULL)
5954 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955
5956 copy = list_alloc();
5957 if (copy != NULL)
5958 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005959 if (copyID != 0)
5960 {
5961 /* Do this before adding the items, because one of the items may
5962 * refer back to this list. */
5963 orig->lv_copyID = copyID;
5964 orig->lv_copylist = copy;
5965 }
5966 for (item = orig->lv_first; item != NULL && !got_int;
5967 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 {
5969 ni = listitem_alloc();
5970 if (ni == NULL)
5971 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005972 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005973 {
5974 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5975 {
5976 vim_free(ni);
5977 break;
5978 }
5979 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005981 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982 list_append(copy, ni);
5983 }
5984 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005985 if (item != NULL)
5986 {
5987 list_unref(copy);
5988 copy = NULL;
5989 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 }
5991
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992 return copy;
5993}
5994
5995/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005996 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005997 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005998 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006000list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 list_T *l;
6002 listitem_T *item;
6003 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006004{
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007 /* notify watchers */
6008 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006010 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011 list_fix_watch(l, ip);
6012 if (ip == item2)
6013 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006015
6016 if (item2->li_next == NULL)
6017 l->lv_last = item->li_prev;
6018 else
6019 item2->li_next->li_prev = item->li_prev;
6020 if (item->li_prev == NULL)
6021 l->lv_first = item2->li_next;
6022 else
6023 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006024 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025}
6026
6027/*
6028 * Return an allocated string with the string representation of a list.
6029 * May return NULL.
6030 */
6031 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006032list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006034 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035{
6036 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037
6038 if (tv->vval.v_list == NULL)
6039 return NULL;
6040 ga_init2(&ga, (int)sizeof(char), 80);
6041 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006042 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006043 {
6044 vim_free(ga.ga_data);
6045 return NULL;
6046 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 ga_append(&ga, ']');
6048 ga_append(&ga, NUL);
6049 return (char_u *)ga.ga_data;
6050}
6051
6052/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006053 * Join list "l" into a string in "*gap", using separator "sep".
6054 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006055 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006056 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006057 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006058list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006059 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006061 char_u *sep;
6062 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006063 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006064{
6065 int first = TRUE;
6066 char_u *tofree;
6067 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006069 char_u *s;
6070
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006071 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006072 {
6073 if (first)
6074 first = FALSE;
6075 else
6076 ga_concat(gap, sep);
6077
6078 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006079 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006080 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006081 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006082 if (s != NULL)
6083 ga_concat(gap, s);
6084 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006085 if (s == NULL)
6086 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006087 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006088 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006089}
6090
6091/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006092 * Garbage collection for lists and dictionaries.
6093 *
6094 * We use reference counts to be able to free most items right away when they
6095 * are no longer used. But for composite items it's possible that it becomes
6096 * unused while the reference count is > 0: When there is a recursive
6097 * reference. Example:
6098 * :let l = [1, 2, 3]
6099 * :let d = {9: l}
6100 * :let l[1] = d
6101 *
6102 * Since this is quite unusual we handle this with garbage collection: every
6103 * once in a while find out which lists and dicts are not referenced from any
6104 * variable.
6105 *
6106 * Here is a good reference text about garbage collection (refers to Python
6107 * but it applies to all reference-counting mechanisms):
6108 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006109 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006110
6111/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006112 * Do garbage collection for lists and dicts.
6113 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006114 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006115 int
6116garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006117{
6118 dict_T *dd;
6119 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006120 int copyID = ++current_copyID;
6121 buf_T *buf;
6122 win_T *wp;
6123 int i;
6124 funccall_T *fc;
6125 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006126#ifdef FEAT_WINDOWS
6127 tabpage_T *tp;
6128#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006129
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006130 /* Only do this once. */
6131 want_garbage_collect = FALSE;
6132 may_garbage_collect = FALSE;
6133
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006134 /*
6135 * 1. Go through all accessible variables and mark all lists and dicts
6136 * with copyID.
6137 */
6138 /* script-local variables */
6139 for (i = 1; i <= ga_scripts.ga_len; ++i)
6140 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6141
6142 /* buffer-local variables */
6143 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6144 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6145
6146 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006147 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006148 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6149
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006150#ifdef FEAT_WINDOWS
6151 /* tabpage-local variables */
6152 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6153 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6154#endif
6155
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006156 /* global variables */
6157 set_ref_in_ht(&globvarht, copyID);
6158
6159 /* function-local variables */
6160 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6161 {
6162 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6163 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6164 }
6165
6166 /*
6167 * 2. Go through the list of dicts and free items without the copyID.
6168 */
6169 for (dd = first_dict; dd != NULL; )
6170 if (dd->dv_copyID != copyID)
6171 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006172 /* Free the Dictionary and ordinary items it contains, but don't
6173 * recurse into Lists and Dictionaries, they will be in the list
6174 * of dicts or list of lists. */
6175 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006176 did_free = TRUE;
6177
6178 /* restart, next dict may also have been freed */
6179 dd = first_dict;
6180 }
6181 else
6182 dd = dd->dv_used_next;
6183
6184 /*
6185 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006186 * But don't free a list that has a watcher (used in a for loop), these
6187 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006188 */
6189 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006190 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006191 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006192 /* Free the List and ordinary items it contains, but don't recurse
6193 * into Lists and Dictionaries, they will be in the list of dicts
6194 * or list of lists. */
6195 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006196 did_free = TRUE;
6197
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006198 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006199 ll = first_list;
6200 }
6201 else
6202 ll = ll->lv_used_next;
6203
6204 return did_free;
6205}
6206
6207/*
6208 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6209 */
6210 static void
6211set_ref_in_ht(ht, copyID)
6212 hashtab_T *ht;
6213 int copyID;
6214{
6215 int todo;
6216 hashitem_T *hi;
6217
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006218 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006219 for (hi = ht->ht_array; todo > 0; ++hi)
6220 if (!HASHITEM_EMPTY(hi))
6221 {
6222 --todo;
6223 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6224 }
6225}
6226
6227/*
6228 * Mark all lists and dicts referenced through list "l" with "copyID".
6229 */
6230 static void
6231set_ref_in_list(l, copyID)
6232 list_T *l;
6233 int copyID;
6234{
6235 listitem_T *li;
6236
6237 for (li = l->lv_first; li != NULL; li = li->li_next)
6238 set_ref_in_item(&li->li_tv, copyID);
6239}
6240
6241/*
6242 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6243 */
6244 static void
6245set_ref_in_item(tv, copyID)
6246 typval_T *tv;
6247 int copyID;
6248{
6249 dict_T *dd;
6250 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006251
6252 switch (tv->v_type)
6253 {
6254 case VAR_DICT:
6255 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006256 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006257 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006258 /* Didn't see this dict yet. */
6259 dd->dv_copyID = copyID;
6260 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006261 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006262 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006263
6264 case VAR_LIST:
6265 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006266 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006267 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006268 /* Didn't see this list yet. */
6269 ll->lv_copyID = copyID;
6270 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006271 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006272 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006273 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006274 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006275}
6276
6277/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006278 * Allocate an empty header for a dictionary.
6279 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006280 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006281dict_alloc()
6282{
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006284
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006286 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006287 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006288 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006289 if (first_dict != NULL)
6290 first_dict->dv_used_prev = d;
6291 d->dv_used_next = first_dict;
6292 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006293 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006294
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006296 d->dv_lock = 0;
6297 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006298 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006299 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006300 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006301}
6302
6303/*
6304 * Unreference a Dictionary: decrement the reference count and free it when it
6305 * becomes zero.
6306 */
6307 static void
6308dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006309 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006310{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006311 if (d != NULL && --d->dv_refcount <= 0)
6312 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006313}
6314
6315/*
6316 * Free a Dictionary, including all items it contains.
6317 * Ignores the reference count.
6318 */
6319 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006320dict_free(d, recurse)
6321 dict_T *d;
6322 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006323{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006324 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006326 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006327
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006328 /* Remove the dict from the list of dicts for garbage collection. */
6329 if (d->dv_used_prev == NULL)
6330 first_dict = d->dv_used_next;
6331 else
6332 d->dv_used_prev->dv_used_next = d->dv_used_next;
6333 if (d->dv_used_next != NULL)
6334 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6335
6336 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006337 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006338 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006340 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006341 if (!HASHITEM_EMPTY(hi))
6342 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006343 /* Remove the item before deleting it, just in case there is
6344 * something recursive causing trouble. */
6345 di = HI2DI(hi);
6346 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006347 if (recurse || (di->di_tv.v_type != VAR_LIST
6348 && di->di_tv.v_type != VAR_DICT))
6349 clear_tv(&di->di_tv);
6350 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006351 --todo;
6352 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006353 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006354 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006355 vim_free(d);
6356}
6357
6358/*
6359 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006360 * The "key" is copied to the new item.
6361 * Note that the value of the item "di_tv" still needs to be initialized!
6362 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006363 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006365dictitem_alloc(key)
6366 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006367{
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006369
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006370 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006371 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006372 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006373 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006374 di->di_flags = 0;
6375 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006376 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006377}
6378
6379/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006380 * Make a copy of a Dictionary item.
6381 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006383dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006385{
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006387
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006388 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6389 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006390 if (di != NULL)
6391 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006392 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006393 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006394 copy_tv(&org->di_tv, &di->di_tv);
6395 }
6396 return di;
6397}
6398
6399/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006400 * Remove item "item" from Dictionary "dict" and free it.
6401 */
6402 static void
6403dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 dict_T *dict;
6405 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006406{
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408
Bram Moolenaar33570922005-01-25 22:26:29 +00006409 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006410 if (HASHITEM_EMPTY(hi))
6411 EMSG2(_(e_intern2), "dictitem_remove()");
6412 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006414 dictitem_free(item);
6415}
6416
6417/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006418 * Free a dict item. Also clears the value.
6419 */
6420 static void
6421dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006423{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006424 clear_tv(&item->di_tv);
6425 vim_free(item);
6426}
6427
6428/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006429 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6430 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006431 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006432 * Returns NULL when out of memory.
6433 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006435dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006437 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006438 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006439{
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 dict_T *copy;
6441 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006442 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006444
6445 if (orig == NULL)
6446 return NULL;
6447
6448 copy = dict_alloc();
6449 if (copy != NULL)
6450 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006451 if (copyID != 0)
6452 {
6453 orig->dv_copyID = copyID;
6454 orig->dv_copydict = copy;
6455 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006456 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006457 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006458 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006459 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006460 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006461 --todo;
6462
6463 di = dictitem_alloc(hi->hi_key);
6464 if (di == NULL)
6465 break;
6466 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006467 {
6468 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6469 copyID) == FAIL)
6470 {
6471 vim_free(di);
6472 break;
6473 }
6474 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006475 else
6476 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6477 if (dict_add(copy, di) == FAIL)
6478 {
6479 dictitem_free(di);
6480 break;
6481 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006482 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006483 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006484
Bram Moolenaare9a41262005-01-15 22:18:47 +00006485 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006486 if (todo > 0)
6487 {
6488 dict_unref(copy);
6489 copy = NULL;
6490 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006491 }
6492
6493 return copy;
6494}
6495
6496/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006497 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006498 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006500 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006501dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006502 dict_T *d;
6503 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006504{
Bram Moolenaar33570922005-01-25 22:26:29 +00006505 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506}
6507
Bram Moolenaar8c711452005-01-14 21:53:12 +00006508/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006509 * Add a number or string entry to dictionary "d".
6510 * When "str" is NULL use number "nr", otherwise use "str".
6511 * Returns FAIL when out of memory and when key already exists.
6512 */
6513 int
6514dict_add_nr_str(d, key, nr, str)
6515 dict_T *d;
6516 char *key;
6517 long nr;
6518 char_u *str;
6519{
6520 dictitem_T *item;
6521
6522 item = dictitem_alloc((char_u *)key);
6523 if (item == NULL)
6524 return FAIL;
6525 item->di_tv.v_lock = 0;
6526 if (str == NULL)
6527 {
6528 item->di_tv.v_type = VAR_NUMBER;
6529 item->di_tv.vval.v_number = nr;
6530 }
6531 else
6532 {
6533 item->di_tv.v_type = VAR_STRING;
6534 item->di_tv.vval.v_string = vim_strsave(str);
6535 }
6536 if (dict_add(d, item) == FAIL)
6537 {
6538 dictitem_free(item);
6539 return FAIL;
6540 }
6541 return OK;
6542}
6543
6544/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006545 * Get the number of items in a Dictionary.
6546 */
6547 static long
6548dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006549 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006551 if (d == NULL)
6552 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006553 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554}
6555
6556/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006557 * Find item "key[len]" in Dictionary "d".
6558 * If "len" is negative use strlen(key).
6559 * Returns NULL when not found.
6560 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006562dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006564 char_u *key;
6565 int len;
6566{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006567#define AKEYLEN 200
6568 char_u buf[AKEYLEN];
6569 char_u *akey;
6570 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006571 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006572
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006573 if (len < 0)
6574 akey = key;
6575 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006576 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006577 tofree = akey = vim_strnsave(key, len);
6578 if (akey == NULL)
6579 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006580 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006581 else
6582 {
6583 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006584 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006585 akey = buf;
6586 }
6587
Bram Moolenaar33570922005-01-25 22:26:29 +00006588 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006589 vim_free(tofree);
6590 if (HASHITEM_EMPTY(hi))
6591 return NULL;
6592 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006593}
6594
6595/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006596 * Get a string item from a dictionary.
6597 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006598 * Returns NULL if the entry doesn't exist or out of memory.
6599 */
6600 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006601get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006602 dict_T *d;
6603 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006604 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006605{
6606 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006607 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006608
6609 di = dict_find(d, key, -1);
6610 if (di == NULL)
6611 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006612 s = get_tv_string(&di->di_tv);
6613 if (save && s != NULL)
6614 s = vim_strsave(s);
6615 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006616}
6617
6618/*
6619 * Get a number item from a dictionary.
6620 * Returns 0 if the entry doesn't exist or out of memory.
6621 */
6622 long
6623get_dict_number(d, key)
6624 dict_T *d;
6625 char_u *key;
6626{
6627 dictitem_T *di;
6628
6629 di = dict_find(d, key, -1);
6630 if (di == NULL)
6631 return 0;
6632 return get_tv_number(&di->di_tv);
6633}
6634
6635/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006636 * Return an allocated string with the string representation of a Dictionary.
6637 * May return NULL.
6638 */
6639 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006640dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006641 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006642 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006643{
6644 garray_T ga;
6645 int first = TRUE;
6646 char_u *tofree;
6647 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006648 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006649 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006650 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006651 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006652
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006653 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006654 return NULL;
6655 ga_init2(&ga, (int)sizeof(char), 80);
6656 ga_append(&ga, '{');
6657
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006658 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006659 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006661 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006662 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006663 --todo;
6664
6665 if (first)
6666 first = FALSE;
6667 else
6668 ga_concat(&ga, (char_u *)", ");
6669
6670 tofree = string_quote(hi->hi_key, FALSE);
6671 if (tofree != NULL)
6672 {
6673 ga_concat(&ga, tofree);
6674 vim_free(tofree);
6675 }
6676 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006677 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006678 if (s != NULL)
6679 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006680 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006681 if (s == NULL)
6682 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006684 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006685 if (todo > 0)
6686 {
6687 vim_free(ga.ga_data);
6688 return NULL;
6689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006690
6691 ga_append(&ga, '}');
6692 ga_append(&ga, NUL);
6693 return (char_u *)ga.ga_data;
6694}
6695
6696/*
6697 * Allocate a variable for a Dictionary and fill it from "*arg".
6698 * Return OK or FAIL. Returns NOTDONE for {expr}.
6699 */
6700 static int
6701get_dict_tv(arg, rettv, evaluate)
6702 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006703 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006704 int evaluate;
6705{
Bram Moolenaar33570922005-01-25 22:26:29 +00006706 dict_T *d = NULL;
6707 typval_T tvkey;
6708 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006709 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006710 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006711 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006712 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006713
6714 /*
6715 * First check if it's not a curly-braces thing: {expr}.
6716 * Must do this without evaluating, otherwise a function may be called
6717 * twice. Unfortunately this means we need to call eval1() twice for the
6718 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006719 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006720 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006721 if (*start != '}')
6722 {
6723 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6724 return FAIL;
6725 if (*start == '}')
6726 return NOTDONE;
6727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006728
6729 if (evaluate)
6730 {
6731 d = dict_alloc();
6732 if (d == NULL)
6733 return FAIL;
6734 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006735 tvkey.v_type = VAR_UNKNOWN;
6736 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006737
6738 *arg = skipwhite(*arg + 1);
6739 while (**arg != '}' && **arg != NUL)
6740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006741 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006742 goto failret;
6743 if (**arg != ':')
6744 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006745 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006746 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006747 goto failret;
6748 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006749 key = get_tv_string_buf_chk(&tvkey, buf);
6750 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006752 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6753 if (key != NULL)
6754 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006755 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006756 goto failret;
6757 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006758
6759 *arg = skipwhite(*arg + 1);
6760 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6761 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006762 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006763 goto failret;
6764 }
6765 if (evaluate)
6766 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006767 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006768 if (item != NULL)
6769 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006770 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006771 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006772 clear_tv(&tv);
6773 goto failret;
6774 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006775 item = dictitem_alloc(key);
6776 clear_tv(&tvkey);
6777 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006778 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006779 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006780 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006781 if (dict_add(d, item) == FAIL)
6782 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006783 }
6784 }
6785
6786 if (**arg == '}')
6787 break;
6788 if (**arg != ',')
6789 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006790 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006791 goto failret;
6792 }
6793 *arg = skipwhite(*arg + 1);
6794 }
6795
6796 if (**arg != '}')
6797 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006798 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006799failret:
6800 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006801 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006802 return FAIL;
6803 }
6804
6805 *arg = skipwhite(*arg + 1);
6806 if (evaluate)
6807 {
6808 rettv->v_type = VAR_DICT;
6809 rettv->vval.v_dict = d;
6810 ++d->dv_refcount;
6811 }
6812
6813 return OK;
6814}
6815
Bram Moolenaar8c711452005-01-14 21:53:12 +00006816/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006817 * Return a string with the string representation of a variable.
6818 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006819 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006820 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006821 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006822 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006823 */
6824 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006825echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006826 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006827 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006828 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006829 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006830{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006831 static int recurse = 0;
6832 char_u *r = NULL;
6833
Bram Moolenaar33570922005-01-25 22:26:29 +00006834 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006835 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006836 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006837 *tofree = NULL;
6838 return NULL;
6839 }
6840 ++recurse;
6841
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006842 switch (tv->v_type)
6843 {
6844 case VAR_FUNC:
6845 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006846 r = tv->vval.v_string;
6847 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006848
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006849 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006850 if (tv->vval.v_list == NULL)
6851 {
6852 *tofree = NULL;
6853 r = NULL;
6854 }
6855 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6856 {
6857 *tofree = NULL;
6858 r = (char_u *)"[...]";
6859 }
6860 else
6861 {
6862 tv->vval.v_list->lv_copyID = copyID;
6863 *tofree = list2string(tv, copyID);
6864 r = *tofree;
6865 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006866 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006867
Bram Moolenaar8c711452005-01-14 21:53:12 +00006868 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006869 if (tv->vval.v_dict == NULL)
6870 {
6871 *tofree = NULL;
6872 r = NULL;
6873 }
6874 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6875 {
6876 *tofree = NULL;
6877 r = (char_u *)"{...}";
6878 }
6879 else
6880 {
6881 tv->vval.v_dict->dv_copyID = copyID;
6882 *tofree = dict2string(tv, copyID);
6883 r = *tofree;
6884 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006885 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006886
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006887 case VAR_STRING:
6888 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006889 *tofree = NULL;
6890 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006891 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006892
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006893 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006894 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006895 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006896 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006897
6898 --recurse;
6899 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006900}
6901
6902/*
6903 * Return a string with the string representation of a variable.
6904 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6905 * "numbuf" is used for a number.
6906 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006907 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006908 */
6909 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006910tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006911 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006912 char_u **tofree;
6913 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006914 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006915{
6916 switch (tv->v_type)
6917 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006918 case VAR_FUNC:
6919 *tofree = string_quote(tv->vval.v_string, TRUE);
6920 return *tofree;
6921 case VAR_STRING:
6922 *tofree = string_quote(tv->vval.v_string, FALSE);
6923 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006924 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006925 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006926 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006927 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006928 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006929 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006930 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006931 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006932}
6933
6934/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006935 * Return string "str" in ' quotes, doubling ' characters.
6936 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006937 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006938 */
6939 static char_u *
6940string_quote(str, function)
6941 char_u *str;
6942 int function;
6943{
Bram Moolenaar33570922005-01-25 22:26:29 +00006944 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006945 char_u *p, *r, *s;
6946
Bram Moolenaar33570922005-01-25 22:26:29 +00006947 len = (function ? 13 : 3);
6948 if (str != NULL)
6949 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006950 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006951 for (p = str; *p != NUL; mb_ptr_adv(p))
6952 if (*p == '\'')
6953 ++len;
6954 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006955 s = r = alloc(len);
6956 if (r != NULL)
6957 {
6958 if (function)
6959 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006960 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006961 r += 10;
6962 }
6963 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006964 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006965 if (str != NULL)
6966 for (p = str; *p != NUL; )
6967 {
6968 if (*p == '\'')
6969 *r++ = '\'';
6970 MB_COPY_CHAR(p, r);
6971 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006972 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006973 if (function)
6974 *r++ = ')';
6975 *r++ = NUL;
6976 }
6977 return s;
6978}
6979
6980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 * Get the value of an environment variable.
6982 * "arg" is pointing to the '$'. It is advanced to after the name.
6983 * If the environment variable was not set, silently assume it is empty.
6984 * Always return OK.
6985 */
6986 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006987get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 int evaluate;
6991{
6992 char_u *string = NULL;
6993 int len;
6994 int cc;
6995 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006996 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
6998 ++*arg;
6999 name = *arg;
7000 len = get_env_len(arg);
7001 if (evaluate)
7002 {
7003 if (len != 0)
7004 {
7005 cc = name[len];
7006 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007007 /* first try vim_getenv(), fast for normal environment vars */
7008 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007010 {
7011 if (!mustfree)
7012 string = vim_strsave(string);
7013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 else
7015 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007016 if (mustfree)
7017 vim_free(string);
7018
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 /* next try expanding things like $VIM and ${HOME} */
7020 string = expand_env_save(name - 1);
7021 if (string != NULL && *string == '$')
7022 {
7023 vim_free(string);
7024 string = NULL;
7025 }
7026 }
7027 name[len] = cc;
7028 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007029 rettv->v_type = VAR_STRING;
7030 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 }
7032
7033 return OK;
7034}
7035
7036/*
7037 * Array with names and number of arguments of all internal functions
7038 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7039 */
7040static struct fst
7041{
7042 char *f_name; /* function name */
7043 char f_min_argc; /* minimal number of arguments */
7044 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007045 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007046 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047} functions[] =
7048{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007049 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {"append", 2, 2, f_append},
7051 {"argc", 0, 0, f_argc},
7052 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007053 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007055 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {"bufexists", 1, 1, f_bufexists},
7057 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7058 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7059 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7060 {"buflisted", 1, 1, f_buflisted},
7061 {"bufloaded", 1, 1, f_bufloaded},
7062 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007063 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 {"bufwinnr", 1, 1, f_bufwinnr},
7065 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007066 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007067 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007068 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 {"char2nr", 1, 1, f_char2nr},
7070 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007071 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007073#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007074 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007075 {"complete_add", 1, 1, f_complete_add},
7076 {"complete_check", 0, 0, f_complete_check},
7077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007079 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007080 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007082 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007083 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 {"delete", 1, 1, f_delete},
7085 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007086 {"diff_filler", 1, 1, f_diff_filler},
7087 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007088 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007090 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 {"eventhandler", 0, 0, f_eventhandler},
7092 {"executable", 1, 1, f_executable},
7093 {"exists", 1, 1, f_exists},
7094 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007095 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007096 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7098 {"filereadable", 1, 1, f_filereadable},
7099 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007100 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007101 {"finddir", 1, 3, f_finddir},
7102 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 {"fnamemodify", 2, 2, f_fnamemodify},
7104 {"foldclosed", 1, 1, f_foldclosed},
7105 {"foldclosedend", 1, 1, f_foldclosedend},
7106 {"foldlevel", 1, 1, f_foldlevel},
7107 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007108 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007110 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007111 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007112 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007113 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 {"getbufvar", 2, 2, f_getbufvar},
7115 {"getchar", 0, 1, f_getchar},
7116 {"getcharmod", 0, 0, f_getcharmod},
7117 {"getcmdline", 0, 0, f_getcmdline},
7118 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007119 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007121 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007122 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 {"getfsize", 1, 1, f_getfsize},
7124 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007125 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007126 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007127 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007128 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaara5525202006-03-02 22:52:09 +00007129 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007130 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007131 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007133 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {"getwinposx", 0, 0, f_getwinposx},
7135 {"getwinposy", 0, 0, f_getwinposy},
7136 {"getwinvar", 2, 2, f_getwinvar},
7137 {"glob", 1, 1, f_glob},
7138 {"globpath", 2, 2, f_globpath},
7139 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007140 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007141 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007142 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7144 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7145 {"histadd", 2, 2, f_histadd},
7146 {"histdel", 1, 2, f_histdel},
7147 {"histget", 1, 2, f_histget},
7148 {"histnr", 1, 1, f_histnr},
7149 {"hlID", 1, 1, f_hlID},
7150 {"hlexists", 1, 1, f_hlexists},
7151 {"hostname", 0, 0, f_hostname},
7152 {"iconv", 3, 3, f_iconv},
7153 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007154 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007155 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007157 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 {"inputrestore", 0, 0, f_inputrestore},
7159 {"inputsave", 0, 0, f_inputsave},
7160 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007161 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007163 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007164 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007165 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007166 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007168 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {"libcall", 3, 3, f_libcall},
7170 {"libcallnr", 3, 3, f_libcallnr},
7171 {"line", 1, 1, f_line},
7172 {"line2byte", 1, 1, f_line2byte},
7173 {"lispindent", 1, 1, f_lispindent},
7174 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007175 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007176 {"maparg", 1, 3, f_maparg},
7177 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007178 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007179 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007180 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007181 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007182 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007183 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007184 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007185 {"max", 1, 1, f_max},
7186 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007187#ifdef vim_mkdir
7188 {"mkdir", 1, 3, f_mkdir},
7189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 {"mode", 0, 0, f_mode},
7191 {"nextnonblank", 1, 1, f_nextnonblank},
7192 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007193 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007195 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007196 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007197 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007198 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007199 {"reltime", 0, 2, f_reltime},
7200 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 {"remote_expr", 2, 3, f_remote_expr},
7202 {"remote_foreground", 1, 1, f_remote_foreground},
7203 {"remote_peek", 1, 2, f_remote_peek},
7204 {"remote_read", 1, 1, f_remote_read},
7205 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007206 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007208 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007210 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007211 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007212 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007213 {"searchpair", 3, 6, f_searchpair},
7214 {"searchpairpos", 3, 6, f_searchpairpos},
7215 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 {"server2client", 2, 2, f_server2client},
7217 {"serverlist", 0, 0, f_serverlist},
7218 {"setbufvar", 3, 3, f_setbufvar},
7219 {"setcmdpos", 1, 1, f_setcmdpos},
7220 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007221 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007222 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007223 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007224 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007226 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007228 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007230 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007231 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007232 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007233 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007234 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007235 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236#ifdef HAVE_STRFTIME
7237 {"strftime", 1, 2, f_strftime},
7238#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007239 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007240 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 {"strlen", 1, 1, f_strlen},
7242 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007243 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 {"strtrans", 1, 1, f_strtrans},
7245 {"submatch", 1, 1, f_submatch},
7246 {"substitute", 4, 4, f_substitute},
7247 {"synID", 3, 3, f_synID},
7248 {"synIDattr", 2, 3, f_synIDattr},
7249 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007250 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007251 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007252 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007253 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007254 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007255 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007257 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {"tolower", 1, 1, f_tolower},
7259 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007260 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 {"virtcol", 1, 1, f_virtcol},
7264 {"visualmode", 0, 1, f_visualmode},
7265 {"winbufnr", 1, 1, f_winbufnr},
7266 {"wincol", 0, 0, f_wincol},
7267 {"winheight", 1, 1, f_winheight},
7268 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007269 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007271 {"winrestview", 1, 1, f_winrestview},
7272 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007274 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275};
7276
7277#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7278
7279/*
7280 * Function given to ExpandGeneric() to obtain the list of internal
7281 * or user defined function names.
7282 */
7283 char_u *
7284get_function_name(xp, idx)
7285 expand_T *xp;
7286 int idx;
7287{
7288 static int intidx = -1;
7289 char_u *name;
7290
7291 if (idx == 0)
7292 intidx = -1;
7293 if (intidx < 0)
7294 {
7295 name = get_user_func_name(xp, idx);
7296 if (name != NULL)
7297 return name;
7298 }
7299 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7300 {
7301 STRCPY(IObuff, functions[intidx].f_name);
7302 STRCAT(IObuff, "(");
7303 if (functions[intidx].f_max_argc == 0)
7304 STRCAT(IObuff, ")");
7305 return IObuff;
7306 }
7307
7308 return NULL;
7309}
7310
7311/*
7312 * Function given to ExpandGeneric() to obtain the list of internal or
7313 * user defined variable or function names.
7314 */
7315/*ARGSUSED*/
7316 char_u *
7317get_expr_name(xp, idx)
7318 expand_T *xp;
7319 int idx;
7320{
7321 static int intidx = -1;
7322 char_u *name;
7323
7324 if (idx == 0)
7325 intidx = -1;
7326 if (intidx < 0)
7327 {
7328 name = get_function_name(xp, idx);
7329 if (name != NULL)
7330 return name;
7331 }
7332 return get_user_var_name(xp, ++intidx);
7333}
7334
7335#endif /* FEAT_CMDL_COMPL */
7336
7337/*
7338 * Find internal function in table above.
7339 * Return index, or -1 if not found
7340 */
7341 static int
7342find_internal_func(name)
7343 char_u *name; /* name of the function */
7344{
7345 int first = 0;
7346 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7347 int cmp;
7348 int x;
7349
7350 /*
7351 * Find the function name in the table. Binary search.
7352 */
7353 while (first <= last)
7354 {
7355 x = first + ((unsigned)(last - first) >> 1);
7356 cmp = STRCMP(name, functions[x].f_name);
7357 if (cmp < 0)
7358 last = x - 1;
7359 else if (cmp > 0)
7360 first = x + 1;
7361 else
7362 return x;
7363 }
7364 return -1;
7365}
7366
7367/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007368 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7369 * name it contains, otherwise return "name".
7370 */
7371 static char_u *
7372deref_func_name(name, lenp)
7373 char_u *name;
7374 int *lenp;
7375{
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007377 int cc;
7378
7379 cc = name[*lenp];
7380 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007381 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007382 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007384 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007385 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007386 {
7387 *lenp = 0;
7388 return (char_u *)""; /* just in case */
7389 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007390 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007392 }
7393
7394 return name;
7395}
7396
7397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 * Allocate a variable for the result of a function.
7399 * Return OK or FAIL.
7400 */
7401 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007402get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7403 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 char_u *name; /* name of the function */
7405 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 char_u **arg; /* argument, pointing to the '(' */
7408 linenr_T firstline; /* first line of range */
7409 linenr_T lastline; /* last line of range */
7410 int *doesrange; /* return: function handled range */
7411 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007412 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413{
7414 char_u *argp;
7415 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007416 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 int argcount = 0; /* number of arguments found */
7418
7419 /*
7420 * Get the arguments.
7421 */
7422 argp = *arg;
7423 while (argcount < MAX_FUNC_ARGS)
7424 {
7425 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7426 if (*argp == ')' || *argp == ',' || *argp == NUL)
7427 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7429 {
7430 ret = FAIL;
7431 break;
7432 }
7433 ++argcount;
7434 if (*argp != ',')
7435 break;
7436 }
7437 if (*argp == ')')
7438 ++argp;
7439 else
7440 ret = FAIL;
7441
7442 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007443 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007444 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 {
7447 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007448 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007450 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452
7453 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007454 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455
7456 *arg = skipwhite(argp);
7457 return ret;
7458}
7459
7460
7461/*
7462 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007463 * Return OK when the function can't be called, FAIL otherwise.
7464 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 */
7466 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007467call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 char_u *name; /* name of the function */
7470 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007471 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007473 typval_T *argvars; /* vars for arguments, must have "argcount"
7474 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 linenr_T firstline; /* first line of range */
7476 linenr_T lastline; /* last line of range */
7477 int *doesrange; /* return: function handled range */
7478 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007479 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
7481 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482#define ERROR_UNKNOWN 0
7483#define ERROR_TOOMANY 1
7484#define ERROR_TOOFEW 2
7485#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007486#define ERROR_DICT 4
7487#define ERROR_NONE 5
7488#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 int error = ERROR_NONE;
7490 int i;
7491 int llen;
7492 ufunc_T *fp;
7493 int cc;
7494#define FLEN_FIXED 40
7495 char_u fname_buf[FLEN_FIXED + 1];
7496 char_u *fname;
7497
7498 /*
7499 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7500 * Change <SNR>123_name() to K_SNR 123_name().
7501 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7502 */
7503 cc = name[len];
7504 name[len] = NUL;
7505 llen = eval_fname_script(name);
7506 if (llen > 0)
7507 {
7508 fname_buf[0] = K_SPECIAL;
7509 fname_buf[1] = KS_EXTRA;
7510 fname_buf[2] = (int)KE_SNR;
7511 i = 3;
7512 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7513 {
7514 if (current_SID <= 0)
7515 error = ERROR_SCRIPT;
7516 else
7517 {
7518 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7519 i = (int)STRLEN(fname_buf);
7520 }
7521 }
7522 if (i + STRLEN(name + llen) < FLEN_FIXED)
7523 {
7524 STRCPY(fname_buf + i, name + llen);
7525 fname = fname_buf;
7526 }
7527 else
7528 {
7529 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7530 if (fname == NULL)
7531 error = ERROR_OTHER;
7532 else
7533 {
7534 mch_memmove(fname, fname_buf, (size_t)i);
7535 STRCPY(fname + i, name + llen);
7536 }
7537 }
7538 }
7539 else
7540 fname = name;
7541
7542 *doesrange = FALSE;
7543
7544
7545 /* execute the function if no errors detected and executing */
7546 if (evaluate && error == ERROR_NONE)
7547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007548 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 error = ERROR_UNKNOWN;
7550
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007551 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 {
7553 /*
7554 * User defined function.
7555 */
7556 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007557
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007559 /* Trigger FuncUndefined event, may load the function. */
7560 if (fp == NULL
7561 && apply_autocmds(EVENT_FUNCUNDEFINED,
7562 fname, fname, TRUE, NULL)
7563 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007565 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 fp = find_func(fname);
7567 }
7568#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007569 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007570 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007571 {
7572 /* loaded a package, search for the function again */
7573 fp = find_func(fname);
7574 }
7575
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 if (fp != NULL)
7577 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007578 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007580 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007582 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007584 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007585 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 else
7587 {
7588 /*
7589 * Call the user function.
7590 * Save and restore search patterns, script variables and
7591 * redo buffer.
7592 */
7593 save_search_patterns();
7594 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007595 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007596 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007597 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007598 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7599 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7600 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007601 /* Function was unreferenced while being used, free it
7602 * now. */
7603 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 restoreRedobuff();
7605 restore_search_patterns();
7606 error = ERROR_NONE;
7607 }
7608 }
7609 }
7610 else
7611 {
7612 /*
7613 * Find the function name in the table, call its implementation.
7614 */
7615 i = find_internal_func(fname);
7616 if (i >= 0)
7617 {
7618 if (argcount < functions[i].f_min_argc)
7619 error = ERROR_TOOFEW;
7620 else if (argcount > functions[i].f_max_argc)
7621 error = ERROR_TOOMANY;
7622 else
7623 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007624 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 error = ERROR_NONE;
7627 }
7628 }
7629 }
7630 /*
7631 * The function call (or "FuncUndefined" autocommand sequence) might
7632 * have been aborted by an error, an interrupt, or an explicitly thrown
7633 * exception that has not been caught so far. This situation can be
7634 * tested for by calling aborting(). For an error in an internal
7635 * function or for the "E132" error in call_user_func(), however, the
7636 * throw point at which the "force_abort" flag (temporarily reset by
7637 * emsg()) is normally updated has not been reached yet. We need to
7638 * update that flag first to make aborting() reliable.
7639 */
7640 update_force_abort();
7641 }
7642 if (error == ERROR_NONE)
7643 ret = OK;
7644
7645 /*
7646 * Report an error unless the argument evaluation or function call has been
7647 * cancelled due to an aborting error, an interrupt, or an exception.
7648 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007649 if (!aborting())
7650 {
7651 switch (error)
7652 {
7653 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007654 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655 break;
7656 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007657 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 break;
7659 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007660 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661 name);
7662 break;
7663 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007664 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 name);
7666 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007667 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007668 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007669 name);
7670 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671 }
7672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673
7674 name[len] = cc;
7675 if (fname != name && fname != fname_buf)
7676 vim_free(fname);
7677
7678 return ret;
7679}
7680
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007681/*
7682 * Give an error message with a function name. Handle <SNR> things.
7683 */
7684 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007685emsg_funcname(ermsg, name)
7686 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007687 char_u *name;
7688{
7689 char_u *p;
7690
7691 if (*name == K_SPECIAL)
7692 p = concat_str((char_u *)"<SNR>", name + 3);
7693 else
7694 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007695 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007696 if (p != name)
7697 vim_free(p);
7698}
7699
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700/*********************************************
7701 * Implementation of the built-in functions
7702 */
7703
7704/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007705 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 */
7707 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007708f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 typval_T *argvars;
7710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711{
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007714 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007715 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007717 if ((l = argvars[0].vval.v_list) != NULL
7718 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7719 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007720 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007721 }
7722 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007723 EMSG(_(e_listreq));
7724}
7725
7726/*
7727 * "append(lnum, string/list)" function
7728 */
7729 static void
7730f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007731 typval_T *argvars;
7732 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007733{
7734 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007735 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007736 list_T *l = NULL;
7737 listitem_T *li = NULL;
7738 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007739 long added = 0;
7740
Bram Moolenaar0d660222005-01-07 21:51:51 +00007741 lnum = get_tv_lnum(argvars);
7742 if (lnum >= 0
7743 && lnum <= curbuf->b_ml.ml_line_count
7744 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007745 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007746 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007747 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007748 l = argvars[1].vval.v_list;
7749 if (l == NULL)
7750 return;
7751 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007752 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007753 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007754 for (;;)
7755 {
7756 if (l == NULL)
7757 tv = &argvars[1]; /* append a string */
7758 else if (li == NULL)
7759 break; /* end of list */
7760 else
7761 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007762 line = get_tv_string_chk(tv);
7763 if (line == NULL) /* type error */
7764 {
7765 rettv->vval.v_number = 1; /* Failed */
7766 break;
7767 }
7768 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007769 ++added;
7770 if (l == NULL)
7771 break;
7772 li = li->li_next;
7773 }
7774
7775 appended_lines_mark(lnum, added);
7776 if (curwin->w_cursor.lnum > lnum)
7777 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007779 else
7780 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781}
7782
7783/*
7784 * "argc()" function
7785 */
7786/* ARGSUSED */
7787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007788f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 typval_T *argvars;
7790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007792 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793}
7794
7795/*
7796 * "argidx()" function
7797 */
7798/* ARGSUSED */
7799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007800f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007801 typval_T *argvars;
7802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805}
7806
7807/*
7808 * "argv(nr)" function
7809 */
7810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007811f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007812 typval_T *argvars;
7813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814{
7815 int idx;
7816
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007817 if (argvars[0].v_type != VAR_UNKNOWN)
7818 {
7819 idx = get_tv_number_chk(&argvars[0], NULL);
7820 if (idx >= 0 && idx < ARGCOUNT)
7821 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7822 else
7823 rettv->vval.v_string = NULL;
7824 rettv->v_type = VAR_STRING;
7825 }
7826 else if (rettv_list_alloc(rettv) == OK)
7827 for (idx = 0; idx < ARGCOUNT; ++idx)
7828 list_append_string(rettv->vval.v_list,
7829 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830}
7831
7832/*
7833 * "browse(save, title, initdir, default)" function
7834 */
7835/* ARGSUSED */
7836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007837f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007838 typval_T *argvars;
7839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840{
7841#ifdef FEAT_BROWSE
7842 int save;
7843 char_u *title;
7844 char_u *initdir;
7845 char_u *defname;
7846 char_u buf[NUMBUFLEN];
7847 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007848 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007850 save = get_tv_number_chk(&argvars[0], &error);
7851 title = get_tv_string_chk(&argvars[1]);
7852 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7853 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007855 if (error || title == NULL || initdir == NULL || defname == NULL)
7856 rettv->vval.v_string = NULL;
7857 else
7858 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007859 do_browse(save ? BROWSE_SAVE : 0,
7860 title, defname, NULL, initdir, NULL, curbuf);
7861#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007862 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007863#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007864 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007865}
7866
7867/*
7868 * "browsedir(title, initdir)" function
7869 */
7870/* ARGSUSED */
7871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007872f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007873 typval_T *argvars;
7874 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007875{
7876#ifdef FEAT_BROWSE
7877 char_u *title;
7878 char_u *initdir;
7879 char_u buf[NUMBUFLEN];
7880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007881 title = get_tv_string_chk(&argvars[0]);
7882 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007884 if (title == NULL || initdir == NULL)
7885 rettv->vval.v_string = NULL;
7886 else
7887 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007888 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007890 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007892 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893}
7894
Bram Moolenaar33570922005-01-25 22:26:29 +00007895static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007896
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897/*
7898 * Find a buffer by number or exact name.
7899 */
7900 static buf_T *
7901find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007902 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903{
7904 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007906 if (avar->v_type == VAR_NUMBER)
7907 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007908 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007910 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007911 if (buf == NULL)
7912 {
7913 /* No full path name match, try a match with a URL or a "nofile"
7914 * buffer, these don't use the full path. */
7915 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7916 if (buf->b_fname != NULL
7917 && (path_with_url(buf->b_fname)
7918#ifdef FEAT_QUICKFIX
7919 || bt_nofile(buf)
7920#endif
7921 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007922 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007923 break;
7924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 }
7926 return buf;
7927}
7928
7929/*
7930 * "bufexists(expr)" function
7931 */
7932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007933f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007934 typval_T *argvars;
7935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007937 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938}
7939
7940/*
7941 * "buflisted(expr)" function
7942 */
7943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007944f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 typval_T *argvars;
7946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947{
7948 buf_T *buf;
7949
7950 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007951 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952}
7953
7954/*
7955 * "bufloaded(expr)" function
7956 */
7957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007958f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007959 typval_T *argvars;
7960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961{
7962 buf_T *buf;
7963
7964 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966}
7967
Bram Moolenaar33570922005-01-25 22:26:29 +00007968static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007969
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970/*
7971 * Get buffer by number or pattern.
7972 */
7973 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007974get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007975 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007977 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 int save_magic;
7979 char_u *save_cpo;
7980 buf_T *buf;
7981
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007982 if (tv->v_type == VAR_NUMBER)
7983 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007984 if (tv->v_type != VAR_STRING)
7985 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 if (name == NULL || *name == NUL)
7987 return curbuf;
7988 if (name[0] == '$' && name[1] == NUL)
7989 return lastbuf;
7990
7991 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7992 save_magic = p_magic;
7993 p_magic = TRUE;
7994 save_cpo = p_cpo;
7995 p_cpo = (char_u *)"";
7996
7997 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7998 TRUE, FALSE));
7999
8000 p_magic = save_magic;
8001 p_cpo = save_cpo;
8002
8003 /* If not found, try expanding the name, like done for bufexists(). */
8004 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008005 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006
8007 return buf;
8008}
8009
8010/*
8011 * "bufname(expr)" function
8012 */
8013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008014f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008015 typval_T *argvars;
8016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017{
8018 buf_T *buf;
8019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008020 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008022 buf = get_buf_tv(&argvars[0]);
8023 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008025 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008027 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 --emsg_off;
8029}
8030
8031/*
8032 * "bufnr(expr)" function
8033 */
8034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008035f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008036 typval_T *argvars;
8037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038{
8039 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008040 int error = FALSE;
8041 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008043 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008045 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008046 --emsg_off;
8047
8048 /* If the buffer isn't found and the second argument is not zero create a
8049 * new buffer. */
8050 if (buf == NULL
8051 && argvars[1].v_type != VAR_UNKNOWN
8052 && get_tv_number_chk(&argvars[1], &error) != 0
8053 && !error
8054 && (name = get_tv_string_chk(&argvars[0])) != NULL
8055 && !error)
8056 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8057
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008059 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008061 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062}
8063
8064/*
8065 * "bufwinnr(nr)" function
8066 */
8067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008068f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008069 typval_T *argvars;
8070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071{
8072#ifdef FEAT_WINDOWS
8073 win_T *wp;
8074 int winnr = 0;
8075#endif
8076 buf_T *buf;
8077
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008078 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008080 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081#ifdef FEAT_WINDOWS
8082 for (wp = firstwin; wp; wp = wp->w_next)
8083 {
8084 ++winnr;
8085 if (wp->w_buffer == buf)
8086 break;
8087 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008088 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008090 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091#endif
8092 --emsg_off;
8093}
8094
8095/*
8096 * "byte2line(byte)" function
8097 */
8098/*ARGSUSED*/
8099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008100f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008101 typval_T *argvars;
8102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103{
8104#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008105 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106#else
8107 long boff = 0;
8108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008109 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008111 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008113 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 (linenr_T)0, &boff);
8115#endif
8116}
8117
8118/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008119 * "byteidx()" function
8120 */
8121/*ARGSUSED*/
8122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008123f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008124 typval_T *argvars;
8125 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008126{
8127#ifdef FEAT_MBYTE
8128 char_u *t;
8129#endif
8130 char_u *str;
8131 long idx;
8132
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008133 str = get_tv_string_chk(&argvars[0]);
8134 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008135 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008136 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008137 return;
8138
8139#ifdef FEAT_MBYTE
8140 t = str;
8141 for ( ; idx > 0; idx--)
8142 {
8143 if (*t == NUL) /* EOL reached */
8144 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008145 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008146 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008147 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008148#else
8149 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008150 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008151#endif
8152}
8153
8154/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008155 * "call(func, arglist)" function
8156 */
8157 static void
8158f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008159 typval_T *argvars;
8160 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008161{
8162 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008163 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008164 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008165 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008166 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008167 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008168
8169 rettv->vval.v_number = 0;
8170 if (argvars[1].v_type != VAR_LIST)
8171 {
8172 EMSG(_(e_listreq));
8173 return;
8174 }
8175 if (argvars[1].vval.v_list == NULL)
8176 return;
8177
8178 if (argvars[0].v_type == VAR_FUNC)
8179 func = argvars[0].vval.v_string;
8180 else
8181 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008182 if (*func == NUL)
8183 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008184
Bram Moolenaare9a41262005-01-15 22:18:47 +00008185 if (argvars[2].v_type != VAR_UNKNOWN)
8186 {
8187 if (argvars[2].v_type != VAR_DICT)
8188 {
8189 EMSG(_(e_dictreq));
8190 return;
8191 }
8192 selfdict = argvars[2].vval.v_dict;
8193 }
8194
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008195 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8196 item = item->li_next)
8197 {
8198 if (argc == MAX_FUNC_ARGS)
8199 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008200 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008201 break;
8202 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008203 /* Make a copy of each argument. This is needed to be able to set
8204 * v_lock to VAR_FIXED in the copy without changing the original list.
8205 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008206 copy_tv(&item->li_tv, &argv[argc++]);
8207 }
8208
8209 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008210 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008211 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8212 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008213
8214 /* Free the arguments. */
8215 while (argc > 0)
8216 clear_tv(&argv[--argc]);
8217}
8218
8219/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008220 * "changenr()" function
8221 */
8222/*ARGSUSED*/
8223 static void
8224f_changenr(argvars, rettv)
8225 typval_T *argvars;
8226 typval_T *rettv;
8227{
8228 rettv->vval.v_number = curbuf->b_u_seq_cur;
8229}
8230
8231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 * "char2nr(string)" function
8233 */
8234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008235f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008236 typval_T *argvars;
8237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238{
8239#ifdef FEAT_MBYTE
8240 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008241 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 else
8243#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008244 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245}
8246
8247/*
8248 * "cindent(lnum)" function
8249 */
8250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008251f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008252 typval_T *argvars;
8253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254{
8255#ifdef FEAT_CINDENT
8256 pos_T pos;
8257 linenr_T lnum;
8258
8259 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008260 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8262 {
8263 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008264 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 curwin->w_cursor = pos;
8266 }
8267 else
8268#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008269 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270}
8271
8272/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008273 * "clearmatches()" function
8274 */
8275/*ARGSUSED*/
8276 static void
8277f_clearmatches(argvars, rettv)
8278 typval_T *argvars;
8279 typval_T *rettv;
8280{
8281#ifdef FEAT_SEARCH_EXTRA
8282 clear_matches(curwin);
8283#endif
8284}
8285
8286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 * "col(string)" function
8288 */
8289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008290f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008291 typval_T *argvars;
8292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293{
8294 colnr_T col = 0;
8295 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008296 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008298 fp = var2fpos(&argvars[0], FALSE, &fnum);
8299 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {
8301 if (fp->col == MAXCOL)
8302 {
8303 /* '> can be MAXCOL, get the length of the line then */
8304 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008305 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 else
8307 col = MAXCOL;
8308 }
8309 else
8310 {
8311 col = fp->col + 1;
8312#ifdef FEAT_VIRTUALEDIT
8313 /* col(".") when the cursor is on the NUL at the end of the line
8314 * because of "coladd" can be seen as an extra column. */
8315 if (virtual_active() && fp == &curwin->w_cursor)
8316 {
8317 char_u *p = ml_get_cursor();
8318
8319 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8320 curwin->w_virtcol - curwin->w_cursor.coladd))
8321 {
8322# ifdef FEAT_MBYTE
8323 int l;
8324
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008325 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 col += l;
8327# else
8328 if (*p != NUL && p[1] == NUL)
8329 ++col;
8330# endif
8331 }
8332 }
8333#endif
8334 }
8335 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008336 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337}
8338
Bram Moolenaar572cb562005-08-05 21:35:02 +00008339#if defined(FEAT_INS_EXPAND)
8340/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008341 * "complete()" function
8342 */
8343/*ARGSUSED*/
8344 static void
8345f_complete(argvars, rettv)
8346 typval_T *argvars;
8347 typval_T *rettv;
8348{
8349 int startcol;
8350
8351 if ((State & INSERT) == 0)
8352 {
8353 EMSG(_("E785: complete() can only be used in Insert mode"));
8354 return;
8355 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008356
8357 /* Check for undo allowed here, because if something was already inserted
8358 * the line was already saved for undo and this check isn't done. */
8359 if (!undo_allowed())
8360 return;
8361
Bram Moolenaarade00832006-03-10 21:46:58 +00008362 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8363 {
8364 EMSG(_(e_invarg));
8365 return;
8366 }
8367
8368 startcol = get_tv_number_chk(&argvars[0], NULL);
8369 if (startcol <= 0)
8370 return;
8371
8372 set_completion(startcol - 1, argvars[1].vval.v_list);
8373}
8374
8375/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008376 * "complete_add()" function
8377 */
8378/*ARGSUSED*/
8379 static void
8380f_complete_add(argvars, rettv)
8381 typval_T *argvars;
8382 typval_T *rettv;
8383{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008384 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008385}
8386
8387/*
8388 * "complete_check()" function
8389 */
8390/*ARGSUSED*/
8391 static void
8392f_complete_check(argvars, rettv)
8393 typval_T *argvars;
8394 typval_T *rettv;
8395{
8396 int saved = RedrawingDisabled;
8397
8398 RedrawingDisabled = 0;
8399 ins_compl_check_keys(0);
8400 rettv->vval.v_number = compl_interrupted;
8401 RedrawingDisabled = saved;
8402}
8403#endif
8404
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405/*
8406 * "confirm(message, buttons[, default [, type]])" function
8407 */
8408/*ARGSUSED*/
8409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008410f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008411 typval_T *argvars;
8412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413{
8414#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8415 char_u *message;
8416 char_u *buttons = NULL;
8417 char_u buf[NUMBUFLEN];
8418 char_u buf2[NUMBUFLEN];
8419 int def = 1;
8420 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008421 char_u *typestr;
8422 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008424 message = get_tv_string_chk(&argvars[0]);
8425 if (message == NULL)
8426 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008427 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008429 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8430 if (buttons == NULL)
8431 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008432 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008434 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008435 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008437 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8438 if (typestr == NULL)
8439 error = TRUE;
8440 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008442 switch (TOUPPER_ASC(*typestr))
8443 {
8444 case 'E': type = VIM_ERROR; break;
8445 case 'Q': type = VIM_QUESTION; break;
8446 case 'I': type = VIM_INFO; break;
8447 case 'W': type = VIM_WARNING; break;
8448 case 'G': type = VIM_GENERIC; break;
8449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 }
8451 }
8452 }
8453 }
8454
8455 if (buttons == NULL || *buttons == NUL)
8456 buttons = (char_u *)_("&Ok");
8457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008458 if (error)
8459 rettv->vval.v_number = 0;
8460 else
8461 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 def, NULL);
8463#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008464 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465#endif
8466}
8467
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008468/*
8469 * "copy()" function
8470 */
8471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008472f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008473 typval_T *argvars;
8474 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008475{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008476 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008477}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478
8479/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008480 * "count()" function
8481 */
8482 static void
8483f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008484 typval_T *argvars;
8485 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008486{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008487 long n = 0;
8488 int ic = FALSE;
8489
Bram Moolenaare9a41262005-01-15 22:18:47 +00008490 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008491 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008492 listitem_T *li;
8493 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008494 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008495
Bram Moolenaare9a41262005-01-15 22:18:47 +00008496 if ((l = argvars[0].vval.v_list) != NULL)
8497 {
8498 li = l->lv_first;
8499 if (argvars[2].v_type != VAR_UNKNOWN)
8500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008501 int error = FALSE;
8502
8503 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008504 if (argvars[3].v_type != VAR_UNKNOWN)
8505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008506 idx = get_tv_number_chk(&argvars[3], &error);
8507 if (!error)
8508 {
8509 li = list_find(l, idx);
8510 if (li == NULL)
8511 EMSGN(_(e_listidx), idx);
8512 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008513 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008514 if (error)
8515 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008516 }
8517
8518 for ( ; li != NULL; li = li->li_next)
8519 if (tv_equal(&li->li_tv, &argvars[1], ic))
8520 ++n;
8521 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008522 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008523 else if (argvars[0].v_type == VAR_DICT)
8524 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008525 int todo;
8526 dict_T *d;
8527 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008528
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008529 if ((d = argvars[0].vval.v_dict) != NULL)
8530 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008531 int error = FALSE;
8532
Bram Moolenaare9a41262005-01-15 22:18:47 +00008533 if (argvars[2].v_type != VAR_UNKNOWN)
8534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008535 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008536 if (argvars[3].v_type != VAR_UNKNOWN)
8537 EMSG(_(e_invarg));
8538 }
8539
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008540 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008541 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008542 {
8543 if (!HASHITEM_EMPTY(hi))
8544 {
8545 --todo;
8546 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8547 ++n;
8548 }
8549 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008550 }
8551 }
8552 else
8553 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008554 rettv->vval.v_number = n;
8555}
8556
8557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8559 *
8560 * Checks the existence of a cscope connection.
8561 */
8562/*ARGSUSED*/
8563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008564f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008565 typval_T *argvars;
8566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567{
8568#ifdef FEAT_CSCOPE
8569 int num = 0;
8570 char_u *dbpath = NULL;
8571 char_u *prepend = NULL;
8572 char_u buf[NUMBUFLEN];
8573
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008574 if (argvars[0].v_type != VAR_UNKNOWN
8575 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008577 num = (int)get_tv_number(&argvars[0]);
8578 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008579 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 }
8582
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008583 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008585 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586#endif
8587}
8588
8589/*
8590 * "cursor(lnum, col)" function
8591 *
8592 * Moves the cursor to the specified line and column
8593 */
8594/*ARGSUSED*/
8595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008596f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008597 typval_T *argvars;
8598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599{
8600 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008601#ifdef FEAT_VIRTUALEDIT
8602 long coladd = 0;
8603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604
Bram Moolenaara5525202006-03-02 22:52:09 +00008605 if (argvars[1].v_type == VAR_UNKNOWN)
8606 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008607 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008608
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008609 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008610 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008611 line = pos.lnum;
8612 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008613#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008614 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008615#endif
8616 }
8617 else
8618 {
8619 line = get_tv_lnum(argvars);
8620 col = get_tv_number_chk(&argvars[1], NULL);
8621#ifdef FEAT_VIRTUALEDIT
8622 if (argvars[2].v_type != VAR_UNKNOWN)
8623 coladd = get_tv_number_chk(&argvars[2], NULL);
8624#endif
8625 }
8626 if (line < 0 || col < 0
8627#ifdef FEAT_VIRTUALEDIT
8628 || coladd < 0
8629#endif
8630 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008631 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 if (line > 0)
8633 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 if (col > 0)
8635 curwin->w_cursor.col = col - 1;
8636#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008637 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638#endif
8639
8640 /* Make sure the cursor is in a valid position. */
8641 check_cursor();
8642#ifdef FEAT_MBYTE
8643 /* Correct cursor for multi-byte character. */
8644 if (has_mbyte)
8645 mb_adjust_cursor();
8646#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008647
8648 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649}
8650
8651/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008652 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 */
8654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008655f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008656 typval_T *argvars;
8657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008659 int noref = 0;
8660
8661 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008662 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008663 if (noref < 0 || noref > 1)
8664 EMSG(_(e_invarg));
8665 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008666 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667}
8668
8669/*
8670 * "delete()" function
8671 */
8672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008673f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008674 typval_T *argvars;
8675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676{
8677 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008678 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008680 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681}
8682
8683/*
8684 * "did_filetype()" function
8685 */
8686/*ARGSUSED*/
8687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008688f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008689 typval_T *argvars;
8690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691{
8692#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008693 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696#endif
8697}
8698
8699/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008700 * "diff_filler()" function
8701 */
8702/*ARGSUSED*/
8703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008704f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008705 typval_T *argvars;
8706 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008707{
8708#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008709 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008710#endif
8711}
8712
8713/*
8714 * "diff_hlID()" function
8715 */
8716/*ARGSUSED*/
8717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008718f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008719 typval_T *argvars;
8720 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008721{
8722#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008723 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008724 static linenr_T prev_lnum = 0;
8725 static int changedtick = 0;
8726 static int fnum = 0;
8727 static int change_start = 0;
8728 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008729 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008730 int filler_lines;
8731 int col;
8732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008733 if (lnum < 0) /* ignore type error in {lnum} arg */
8734 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008735 if (lnum != prev_lnum
8736 || changedtick != curbuf->b_changedtick
8737 || fnum != curbuf->b_fnum)
8738 {
8739 /* New line, buffer, change: need to get the values. */
8740 filler_lines = diff_check(curwin, lnum);
8741 if (filler_lines < 0)
8742 {
8743 if (filler_lines == -1)
8744 {
8745 change_start = MAXCOL;
8746 change_end = -1;
8747 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8748 hlID = HLF_ADD; /* added line */
8749 else
8750 hlID = HLF_CHD; /* changed line */
8751 }
8752 else
8753 hlID = HLF_ADD; /* added line */
8754 }
8755 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008756 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008757 prev_lnum = lnum;
8758 changedtick = curbuf->b_changedtick;
8759 fnum = curbuf->b_fnum;
8760 }
8761
8762 if (hlID == HLF_CHD || hlID == HLF_TXD)
8763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008764 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008765 if (col >= change_start && col <= change_end)
8766 hlID = HLF_TXD; /* changed text */
8767 else
8768 hlID = HLF_CHD; /* changed line */
8769 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008770 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008771#endif
8772}
8773
8774/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008775 * "empty({expr})" function
8776 */
8777 static void
8778f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008779 typval_T *argvars;
8780 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008781{
8782 int n;
8783
8784 switch (argvars[0].v_type)
8785 {
8786 case VAR_STRING:
8787 case VAR_FUNC:
8788 n = argvars[0].vval.v_string == NULL
8789 || *argvars[0].vval.v_string == NUL;
8790 break;
8791 case VAR_NUMBER:
8792 n = argvars[0].vval.v_number == 0;
8793 break;
8794 case VAR_LIST:
8795 n = argvars[0].vval.v_list == NULL
8796 || argvars[0].vval.v_list->lv_first == NULL;
8797 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008798 case VAR_DICT:
8799 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008800 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008801 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008802 default:
8803 EMSG2(_(e_intern2), "f_empty()");
8804 n = 0;
8805 }
8806
8807 rettv->vval.v_number = n;
8808}
8809
8810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 * "escape({string}, {chars})" function
8812 */
8813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008815 typval_T *argvars;
8816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817{
8818 char_u buf[NUMBUFLEN];
8819
Bram Moolenaar758711c2005-02-02 23:11:38 +00008820 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8821 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008822 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823}
8824
8825/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008826 * "eval()" function
8827 */
8828/*ARGSUSED*/
8829 static void
8830f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008831 typval_T *argvars;
8832 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008833{
8834 char_u *s;
8835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008836 s = get_tv_string_chk(&argvars[0]);
8837 if (s != NULL)
8838 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008840 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8841 {
8842 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008843 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008844 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008845 else if (*s != NUL)
8846 EMSG(_(e_trailing));
8847}
8848
8849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 * "eventhandler()" function
8851 */
8852/*ARGSUSED*/
8853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008854f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008855 typval_T *argvars;
8856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008858 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859}
8860
8861/*
8862 * "executable()" function
8863 */
8864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 typval_T *argvars;
8867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008869 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870}
8871
8872/*
8873 * "exists()" function
8874 */
8875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008876f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008877 typval_T *argvars;
8878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879{
8880 char_u *p;
8881 char_u *name;
8882 int n = FALSE;
8883 int len = 0;
8884
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008885 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 if (*p == '$') /* environment variable */
8887 {
8888 /* first try "normal" environment variables (fast) */
8889 if (mch_getenv(p + 1) != NULL)
8890 n = TRUE;
8891 else
8892 {
8893 /* try expanding things like $VIM and ${HOME} */
8894 p = expand_env_save(p);
8895 if (p != NULL && *p != '$')
8896 n = TRUE;
8897 vim_free(p);
8898 }
8899 }
8900 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008903 if (*skipwhite(p) != NUL)
8904 n = FALSE; /* trailing garbage */
8905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 else if (*p == '*') /* internal or user defined function */
8907 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008908 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 }
8910 else if (*p == ':')
8911 {
8912 n = cmd_exists(p + 1);
8913 }
8914 else if (*p == '#')
8915 {
8916#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008917 if (p[1] == '#')
8918 n = autocmd_supported(p + 2);
8919 else
8920 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921#endif
8922 }
8923 else /* internal variable */
8924 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008925 char_u *tofree;
8926 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008928 /* get_name_len() takes care of expanding curly braces */
8929 name = p;
8930 len = get_name_len(&p, &tofree, TRUE, FALSE);
8931 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008933 if (tofree != NULL)
8934 name = tofree;
8935 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8936 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008938 /* handle d.key, l[idx], f(expr) */
8939 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8940 if (n)
8941 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 }
8943 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008944 if (*p != NUL)
8945 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008947 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 }
8949
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008950 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951}
8952
8953/*
8954 * "expand()" function
8955 */
8956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008958 typval_T *argvars;
8959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960{
8961 char_u *s;
8962 int len;
8963 char_u *errormsg;
8964 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8965 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008966 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968 rettv->v_type = VAR_STRING;
8969 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 if (*s == '%' || *s == '#' || *s == '<')
8971 {
8972 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008973 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 --emsg_off;
8975 }
8976 else
8977 {
8978 /* When the optional second argument is non-zero, don't remove matches
8979 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008980 if (argvars[1].v_type != VAR_UNKNOWN
8981 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008983 if (!error)
8984 {
8985 ExpandInit(&xpc);
8986 xpc.xp_context = EXPAND_FILES;
8987 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008988 }
8989 else
8990 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 }
8992}
8993
8994/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008995 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008996 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008997 */
8998 static void
8999f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009000 typval_T *argvars;
9001 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009002{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009003 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009004 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009005 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009006 list_T *l1, *l2;
9007 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009008 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009009 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009010
Bram Moolenaare9a41262005-01-15 22:18:47 +00009011 l1 = argvars[0].vval.v_list;
9012 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009013 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9014 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009015 {
9016 if (argvars[2].v_type != VAR_UNKNOWN)
9017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009018 before = get_tv_number_chk(&argvars[2], &error);
9019 if (error)
9020 return; /* type error; errmsg already given */
9021
Bram Moolenaar758711c2005-02-02 23:11:38 +00009022 if (before == l1->lv_len)
9023 item = NULL;
9024 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009025 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009026 item = list_find(l1, before);
9027 if (item == NULL)
9028 {
9029 EMSGN(_(e_listidx), before);
9030 return;
9031 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009032 }
9033 }
9034 else
9035 item = NULL;
9036 list_extend(l1, l2, item);
9037
Bram Moolenaare9a41262005-01-15 22:18:47 +00009038 copy_tv(&argvars[0], rettv);
9039 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009040 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009041 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9042 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009043 dict_T *d1, *d2;
9044 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009045 char_u *action;
9046 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009047 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009048 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009049
9050 d1 = argvars[0].vval.v_dict;
9051 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009052 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9053 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009054 {
9055 /* Check the third argument. */
9056 if (argvars[2].v_type != VAR_UNKNOWN)
9057 {
9058 static char *(av[]) = {"keep", "force", "error"};
9059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009060 action = get_tv_string_chk(&argvars[2]);
9061 if (action == NULL)
9062 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009063 for (i = 0; i < 3; ++i)
9064 if (STRCMP(action, av[i]) == 0)
9065 break;
9066 if (i == 3)
9067 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009068 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009069 return;
9070 }
9071 }
9072 else
9073 action = (char_u *)"force";
9074
9075 /* Go over all entries in the second dict and add them to the
9076 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009077 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009079 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009080 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009081 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009082 --todo;
9083 di1 = dict_find(d1, hi2->hi_key, -1);
9084 if (di1 == NULL)
9085 {
9086 di1 = dictitem_copy(HI2DI(hi2));
9087 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9088 dictitem_free(di1);
9089 }
9090 else if (*action == 'e')
9091 {
9092 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9093 break;
9094 }
9095 else if (*action == 'f')
9096 {
9097 clear_tv(&di1->di_tv);
9098 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9099 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009100 }
9101 }
9102
Bram Moolenaare9a41262005-01-15 22:18:47 +00009103 copy_tv(&argvars[0], rettv);
9104 }
9105 }
9106 else
9107 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009108}
9109
9110/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009111 * "feedkeys()" function
9112 */
9113/*ARGSUSED*/
9114 static void
9115f_feedkeys(argvars, rettv)
9116 typval_T *argvars;
9117 typval_T *rettv;
9118{
9119 int remap = TRUE;
9120 char_u *keys, *flags;
9121 char_u nbuf[NUMBUFLEN];
9122 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009123 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009124
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009125 /* This is not allowed in the sandbox. If the commands would still be
9126 * executed in the sandbox it would be OK, but it probably happens later,
9127 * when "sandbox" is no longer set. */
9128 if (check_secure())
9129 return;
9130
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009131 rettv->vval.v_number = 0;
9132 keys = get_tv_string(&argvars[0]);
9133 if (*keys != NUL)
9134 {
9135 if (argvars[1].v_type != VAR_UNKNOWN)
9136 {
9137 flags = get_tv_string_buf(&argvars[1], nbuf);
9138 for ( ; *flags != NUL; ++flags)
9139 {
9140 switch (*flags)
9141 {
9142 case 'n': remap = FALSE; break;
9143 case 'm': remap = TRUE; break;
9144 case 't': typed = TRUE; break;
9145 }
9146 }
9147 }
9148
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009149 /* Need to escape K_SPECIAL and CSI before putting the string in the
9150 * typeahead buffer. */
9151 keys_esc = vim_strsave_escape_csi(keys);
9152 if (keys_esc != NULL)
9153 {
9154 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009155 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009156 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009157 if (vgetc_busy)
9158 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009159 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009160 }
9161}
9162
9163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 * "filereadable()" function
9165 */
9166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009167f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009168 typval_T *argvars;
9169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170{
9171 FILE *fd;
9172 char_u *p;
9173 int n;
9174
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9177 {
9178 n = TRUE;
9179 fclose(fd);
9180 }
9181 else
9182 n = FALSE;
9183
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185}
9186
9187/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009188 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 * rights to write into.
9190 */
9191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009193 typval_T *argvars;
9194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009196 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197}
9198
Bram Moolenaar33570922005-01-25 22:26:29 +00009199static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009200
9201 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009202findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009203 typval_T *argvars;
9204 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009205 int dir;
9206{
9207#ifdef FEAT_SEARCHPATH
9208 char_u *fname;
9209 char_u *fresult = NULL;
9210 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9211 char_u *p;
9212 char_u pathbuf[NUMBUFLEN];
9213 int count = 1;
9214 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009215 int error = FALSE;
9216#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009217
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009218 rettv->vval.v_string = NULL;
9219 rettv->v_type = VAR_STRING;
9220
9221#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009222 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009223
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009224 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009226 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9227 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009228 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009229 else
9230 {
9231 if (*p != NUL)
9232 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009234 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009235 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009237 }
9238
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009239 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9240 error = TRUE;
9241
9242 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009244 do
9245 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009246 if (rettv->v_type == VAR_STRING)
9247 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009248 fresult = find_file_in_path_option(first ? fname : NULL,
9249 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar5b6b1ca2007-03-27 08:19:43 +00009250 0, first, path, dir, curbuf->b_ffname,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009251 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009252 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009253
9254 if (fresult != NULL && rettv->v_type == VAR_LIST)
9255 list_append_string(rettv->vval.v_list, fresult, -1);
9256
9257 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009259
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009260 if (rettv->v_type == VAR_STRING)
9261 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009262#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009263}
9264
Bram Moolenaar33570922005-01-25 22:26:29 +00009265static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9266static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009267
9268/*
9269 * Implementation of map() and filter().
9270 */
9271 static void
9272filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009273 typval_T *argvars;
9274 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009275 int map;
9276{
9277 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009278 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009279 listitem_T *li, *nli;
9280 list_T *l = NULL;
9281 dictitem_T *di;
9282 hashtab_T *ht;
9283 hashitem_T *hi;
9284 dict_T *d = NULL;
9285 typval_T save_val;
9286 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009287 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009288 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009289 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009290 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009291
9292 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009293 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009294 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009295 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009296 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009297 return;
9298 }
9299 else if (argvars[0].v_type == VAR_DICT)
9300 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009301 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009302 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009303 return;
9304 }
9305 else
9306 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009307 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009308 return;
9309 }
9310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009311 expr = get_tv_string_buf_chk(&argvars[1], buf);
9312 /* On type errors, the preceding call has already displayed an error
9313 * message. Avoid a misleading error message for an empty string that
9314 * was not passed as argument. */
9315 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009317 prepare_vimvar(VV_VAL, &save_val);
9318 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009319
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009320 /* We reset "did_emsg" to be able to detect whether an error
9321 * occurred during evaluation of the expression. */
9322 save_did_emsg = did_emsg;
9323 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009325 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009327 prepare_vimvar(VV_KEY, &save_key);
9328 vimvars[VV_KEY].vv_type = VAR_STRING;
9329
9330 ht = &d->dv_hashtab;
9331 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009332 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009333 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009335 if (!HASHITEM_EMPTY(hi))
9336 {
9337 --todo;
9338 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009339 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009340 break;
9341 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009342 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009343 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 break;
9345 if (!map && rem)
9346 dictitem_remove(d, di);
9347 clear_tv(&vimvars[VV_KEY].vv_tv);
9348 }
9349 }
9350 hash_unlock(ht);
9351
9352 restore_vimvar(VV_KEY, &save_key);
9353 }
9354 else
9355 {
9356 for (li = l->lv_first; li != NULL; li = nli)
9357 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009358 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009359 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009360 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009361 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009362 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009363 break;
9364 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009365 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009366 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009367 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009368
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009369 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009370
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009371 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009372 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009373
9374 copy_tv(&argvars[0], rettv);
9375}
9376
9377 static int
9378filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009379 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009380 char_u *expr;
9381 int map;
9382 int *remp;
9383{
Bram Moolenaar33570922005-01-25 22:26:29 +00009384 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009385 char_u *s;
9386
Bram Moolenaar33570922005-01-25 22:26:29 +00009387 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009388 s = expr;
9389 if (eval1(&s, &rettv, TRUE) == FAIL)
9390 return FAIL;
9391 if (*s != NUL) /* check for trailing chars after expr */
9392 {
9393 EMSG2(_(e_invexpr2), s);
9394 return FAIL;
9395 }
9396 if (map)
9397 {
9398 /* map(): replace the list item value */
9399 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009400 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009401 *tv = rettv;
9402 }
9403 else
9404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009405 int error = FALSE;
9406
Bram Moolenaare9a41262005-01-15 22:18:47 +00009407 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009408 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009409 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009410 /* On type error, nothing has been removed; return FAIL to stop the
9411 * loop. The error message was given by get_tv_number_chk(). */
9412 if (error)
9413 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009414 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009415 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009416 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009417}
9418
9419/*
9420 * "filter()" function
9421 */
9422 static void
9423f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009424 typval_T *argvars;
9425 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009426{
9427 filter_map(argvars, rettv, FALSE);
9428}
9429
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009430/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009431 * "finddir({fname}[, {path}[, {count}]])" function
9432 */
9433 static void
9434f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009435 typval_T *argvars;
9436 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009437{
9438 findfilendir(argvars, rettv, TRUE);
9439}
9440
9441/*
9442 * "findfile({fname}[, {path}[, {count}]])" function
9443 */
9444 static void
9445f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009446 typval_T *argvars;
9447 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009448{
9449 findfilendir(argvars, rettv, FALSE);
9450}
9451
9452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 * "fnamemodify({fname}, {mods})" function
9454 */
9455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009457 typval_T *argvars;
9458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459{
9460 char_u *fname;
9461 char_u *mods;
9462 int usedlen = 0;
9463 int len;
9464 char_u *fbuf = NULL;
9465 char_u buf[NUMBUFLEN];
9466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009467 fname = get_tv_string_chk(&argvars[0]);
9468 mods = get_tv_string_buf_chk(&argvars[1], buf);
9469 if (fname == NULL || mods == NULL)
9470 fname = NULL;
9471 else
9472 {
9473 len = (int)STRLEN(fname);
9474 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009481 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 vim_free(fbuf);
9483}
9484
Bram Moolenaar33570922005-01-25 22:26:29 +00009485static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486
9487/*
9488 * "foldclosed()" function
9489 */
9490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009492 typval_T *argvars;
9493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 int end;
9495{
9496#ifdef FEAT_FOLDING
9497 linenr_T lnum;
9498 linenr_T first, last;
9499
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009500 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9502 {
9503 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9504 {
9505 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009508 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 return;
9510 }
9511 }
9512#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514}
9515
9516/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009517 * "foldclosed()" function
9518 */
9519 static void
9520f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009521 typval_T *argvars;
9522 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009523{
9524 foldclosed_both(argvars, rettv, FALSE);
9525}
9526
9527/*
9528 * "foldclosedend()" function
9529 */
9530 static void
9531f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009532 typval_T *argvars;
9533 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009534{
9535 foldclosed_both(argvars, rettv, TRUE);
9536}
9537
9538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 * "foldlevel()" function
9540 */
9541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009542f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009543 typval_T *argvars;
9544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546#ifdef FEAT_FOLDING
9547 linenr_T lnum;
9548
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009551 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 else
9553#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555}
9556
9557/*
9558 * "foldtext()" function
9559 */
9560/*ARGSUSED*/
9561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009563 typval_T *argvars;
9564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565{
9566#ifdef FEAT_FOLDING
9567 linenr_T lnum;
9568 char_u *s;
9569 char_u *r;
9570 int len;
9571 char *txt;
9572#endif
9573
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 rettv->v_type = VAR_STRING;
9575 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009577 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9578 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9579 <= curbuf->b_ml.ml_line_count
9580 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 {
9582 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009583 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9584 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 {
9586 if (!linewhite(lnum))
9587 break;
9588 ++lnum;
9589 }
9590
9591 /* Find interesting text in this line. */
9592 s = skipwhite(ml_get(lnum));
9593 /* skip C comment-start */
9594 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009597 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009598 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009599 {
9600 s = skipwhite(ml_get(lnum + 1));
9601 if (*s == '*')
9602 s = skipwhite(s + 1);
9603 }
9604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 txt = _("+-%s%3ld lines: ");
9606 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009607 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 + 20 /* for %3ld */
9609 + STRLEN(s))); /* concatenated */
9610 if (r != NULL)
9611 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009612 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9613 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9614 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 len = (int)STRLEN(r);
9616 STRCAT(r, s);
9617 /* remove 'foldmarker' and 'commentstring' */
9618 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 }
9621 }
9622#endif
9623}
9624
9625/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009626 * "foldtextresult(lnum)" function
9627 */
9628/*ARGSUSED*/
9629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009631 typval_T *argvars;
9632 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009633{
9634#ifdef FEAT_FOLDING
9635 linenr_T lnum;
9636 char_u *text;
9637 char_u buf[51];
9638 foldinfo_T foldinfo;
9639 int fold_count;
9640#endif
9641
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642 rettv->v_type = VAR_STRING;
9643 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009644#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009646 /* treat illegal types and illegal string values for {lnum} the same */
9647 if (lnum < 0)
9648 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009649 fold_count = foldedCount(curwin, lnum, &foldinfo);
9650 if (fold_count > 0)
9651 {
9652 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9653 &foldinfo, buf);
9654 if (text == buf)
9655 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009656 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009657 }
9658#endif
9659}
9660
9661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 * "foreground()" function
9663 */
9664/*ARGSUSED*/
9665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009666f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009667 typval_T *argvars;
9668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671#ifdef FEAT_GUI
9672 if (gui.in_use)
9673 gui_mch_set_foreground();
9674#else
9675# ifdef WIN32
9676 win32_set_foreground();
9677# endif
9678#endif
9679}
9680
9681/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009682 * "function()" function
9683 */
9684/*ARGSUSED*/
9685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009687 typval_T *argvars;
9688 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009689{
9690 char_u *s;
9691
Bram Moolenaara7043832005-01-21 11:56:39 +00009692 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009693 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009694 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009695 EMSG2(_(e_invarg2), s);
9696 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009697 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009698 else
9699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->vval.v_string = vim_strsave(s);
9701 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009702 }
9703}
9704
9705/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009706 * "garbagecollect()" function
9707 */
9708/*ARGSUSED*/
9709 static void
9710f_garbagecollect(argvars, rettv)
9711 typval_T *argvars;
9712 typval_T *rettv;
9713{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009714 /* This is postponed until we are back at the toplevel, because we may be
9715 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9716 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009717}
9718
9719/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009720 * "get()" function
9721 */
9722 static void
9723f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009724 typval_T *argvars;
9725 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009726{
Bram Moolenaar33570922005-01-25 22:26:29 +00009727 listitem_T *li;
9728 list_T *l;
9729 dictitem_T *di;
9730 dict_T *d;
9731 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009732
Bram Moolenaare9a41262005-01-15 22:18:47 +00009733 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009734 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009735 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009736 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009737 int error = FALSE;
9738
9739 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9740 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009741 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009742 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009743 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009744 else if (argvars[0].v_type == VAR_DICT)
9745 {
9746 if ((d = argvars[0].vval.v_dict) != NULL)
9747 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009748 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009749 if (di != NULL)
9750 tv = &di->di_tv;
9751 }
9752 }
9753 else
9754 EMSG2(_(e_listdictarg), "get()");
9755
9756 if (tv == NULL)
9757 {
9758 if (argvars[2].v_type == VAR_UNKNOWN)
9759 rettv->vval.v_number = 0;
9760 else
9761 copy_tv(&argvars[2], rettv);
9762 }
9763 else
9764 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009765}
9766
Bram Moolenaar342337a2005-07-21 21:11:17 +00009767static 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 +00009768
9769/*
9770 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009771 * Return a range (from start to end) of lines in rettv from the specified
9772 * buffer.
9773 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009774 */
9775 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009776get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009777 buf_T *buf;
9778 linenr_T start;
9779 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009780 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009781 typval_T *rettv;
9782{
9783 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009784
Bram Moolenaar342337a2005-07-21 21:11:17 +00009785 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009786 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009787 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009788 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009789 }
9790 else
9791 rettv->vval.v_number = 0;
9792
9793 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9794 return;
9795
9796 if (!retlist)
9797 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009798 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9799 p = ml_get_buf(buf, start, FALSE);
9800 else
9801 p = (char_u *)"";
9802
9803 rettv->v_type = VAR_STRING;
9804 rettv->vval.v_string = vim_strsave(p);
9805 }
9806 else
9807 {
9808 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009809 return;
9810
9811 if (start < 1)
9812 start = 1;
9813 if (end > buf->b_ml.ml_line_count)
9814 end = buf->b_ml.ml_line_count;
9815 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009816 if (list_append_string(rettv->vval.v_list,
9817 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009818 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009819 }
9820}
9821
9822/*
9823 * "getbufline()" function
9824 */
9825 static void
9826f_getbufline(argvars, rettv)
9827 typval_T *argvars;
9828 typval_T *rettv;
9829{
9830 linenr_T lnum;
9831 linenr_T end;
9832 buf_T *buf;
9833
9834 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9835 ++emsg_off;
9836 buf = get_buf_tv(&argvars[0]);
9837 --emsg_off;
9838
Bram Moolenaar661b1822005-07-28 22:36:45 +00009839 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009840 if (argvars[2].v_type == VAR_UNKNOWN)
9841 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009842 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009843 end = get_tv_lnum_buf(&argvars[2], buf);
9844
Bram Moolenaar342337a2005-07-21 21:11:17 +00009845 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009846}
9847
Bram Moolenaar0d660222005-01-07 21:51:51 +00009848/*
9849 * "getbufvar()" function
9850 */
9851 static void
9852f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009853 typval_T *argvars;
9854 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009855{
9856 buf_T *buf;
9857 buf_T *save_curbuf;
9858 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009859 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009861 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9862 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009863 ++emsg_off;
9864 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009865
9866 rettv->v_type = VAR_STRING;
9867 rettv->vval.v_string = NULL;
9868
9869 if (buf != NULL && varname != NULL)
9870 {
9871 if (*varname == '&') /* buffer-local-option */
9872 {
9873 /* set curbuf to be our buf, temporarily */
9874 save_curbuf = curbuf;
9875 curbuf = buf;
9876
9877 get_option_tv(&varname, rettv, TRUE);
9878
9879 /* restore previous notion of curbuf */
9880 curbuf = save_curbuf;
9881 }
9882 else
9883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009884 if (*varname == NUL)
9885 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9886 * scope prefix before the NUL byte is required by
9887 * find_var_in_ht(). */
9888 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009889 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009890 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009891 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009892 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009893 }
9894 }
9895
9896 --emsg_off;
9897}
9898
9899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 * "getchar()" function
9901 */
9902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009903f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009904 typval_T *argvars;
9905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906{
9907 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009908 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009910 /* Position the cursor. Needed after a message that ends in a space. */
9911 windgoto(msg_row, msg_col);
9912
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 ++no_mapping;
9914 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00009915 for (;;)
9916 {
9917 if (argvars[0].v_type == VAR_UNKNOWN)
9918 /* getchar(): blocking wait. */
9919 n = safe_vgetc();
9920 else if (get_tv_number_chk(&argvars[0], &error) == 1)
9921 /* getchar(1): only check if char avail */
9922 n = vpeekc();
9923 else if (error || vpeekc() == NUL)
9924 /* illegal argument or getchar(0) and no char avail: return zero */
9925 n = 0;
9926 else
9927 /* getchar(0) and char avail: return char */
9928 n = safe_vgetc();
9929 if (n == K_IGNORE)
9930 continue;
9931 break;
9932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 --no_mapping;
9934 --allow_keys;
9935
Bram Moolenaar219b8702006-11-01 14:32:36 +00009936 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9937 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9938 vimvars[VV_MOUSE_COL].vv_nr = 0;
9939
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009940 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 if (IS_SPECIAL(n) || mod_mask != 0)
9942 {
9943 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9944 int i = 0;
9945
9946 /* Turn a special key into three bytes, plus modifier. */
9947 if (mod_mask != 0)
9948 {
9949 temp[i++] = K_SPECIAL;
9950 temp[i++] = KS_MODIFIER;
9951 temp[i++] = mod_mask;
9952 }
9953 if (IS_SPECIAL(n))
9954 {
9955 temp[i++] = K_SPECIAL;
9956 temp[i++] = K_SECOND(n);
9957 temp[i++] = K_THIRD(n);
9958 }
9959#ifdef FEAT_MBYTE
9960 else if (has_mbyte)
9961 i += (*mb_char2bytes)(n, temp + i);
9962#endif
9963 else
9964 temp[i++] = n;
9965 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966 rettv->v_type = VAR_STRING;
9967 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009968
9969#ifdef FEAT_MOUSE
9970 if (n == K_LEFTMOUSE
9971 || n == K_LEFTMOUSE_NM
9972 || n == K_LEFTDRAG
9973 || n == K_LEFTRELEASE
9974 || n == K_LEFTRELEASE_NM
9975 || n == K_MIDDLEMOUSE
9976 || n == K_MIDDLEDRAG
9977 || n == K_MIDDLERELEASE
9978 || n == K_RIGHTMOUSE
9979 || n == K_RIGHTDRAG
9980 || n == K_RIGHTRELEASE
9981 || n == K_X1MOUSE
9982 || n == K_X1DRAG
9983 || n == K_X1RELEASE
9984 || n == K_X2MOUSE
9985 || n == K_X2DRAG
9986 || n == K_X2RELEASE
9987 || n == K_MOUSEDOWN
9988 || n == K_MOUSEUP)
9989 {
9990 int row = mouse_row;
9991 int col = mouse_col;
9992 win_T *win;
9993 linenr_T lnum;
9994# ifdef FEAT_WINDOWS
9995 win_T *wp;
9996# endif
9997 int n = 1;
9998
9999 if (row >= 0 && col >= 0)
10000 {
10001 /* Find the window at the mouse coordinates and compute the
10002 * text position. */
10003 win = mouse_find_win(&row, &col);
10004 (void)mouse_comp_pos(win, &row, &col, &lnum);
10005# ifdef FEAT_WINDOWS
10006 for (wp = firstwin; wp != win; wp = wp->w_next)
10007 ++n;
10008# endif
10009 vimvars[VV_MOUSE_WIN].vv_nr = n;
10010 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10011 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10012 }
10013 }
10014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 }
10016}
10017
10018/*
10019 * "getcharmod()" function
10020 */
10021/*ARGSUSED*/
10022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010024 typval_T *argvars;
10025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028}
10029
10030/*
10031 * "getcmdline()" function
10032 */
10033/*ARGSUSED*/
10034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010035f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010036 typval_T *argvars;
10037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039 rettv->v_type = VAR_STRING;
10040 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041}
10042
10043/*
10044 * "getcmdpos()" function
10045 */
10046/*ARGSUSED*/
10047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010049 typval_T *argvars;
10050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053}
10054
10055/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010056 * "getcmdtype()" function
10057 */
10058/*ARGSUSED*/
10059 static void
10060f_getcmdtype(argvars, rettv)
10061 typval_T *argvars;
10062 typval_T *rettv;
10063{
10064 rettv->v_type = VAR_STRING;
10065 rettv->vval.v_string = alloc(2);
10066 if (rettv->vval.v_string != NULL)
10067 {
10068 rettv->vval.v_string[0] = get_cmdline_type();
10069 rettv->vval.v_string[1] = NUL;
10070 }
10071}
10072
10073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 * "getcwd()" function
10075 */
10076/*ARGSUSED*/
10077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010078f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010079 typval_T *argvars;
10080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081{
10082 char_u cwd[MAXPATHL];
10083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010086 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 else
10088 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010089 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010091 if (rettv->vval.v_string != NULL)
10092 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093#endif
10094 }
10095}
10096
10097/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010098 * "getfontname()" function
10099 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010100/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010103 typval_T *argvars;
10104 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010105{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010106 rettv->v_type = VAR_STRING;
10107 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010108#ifdef FEAT_GUI
10109 if (gui.in_use)
10110 {
10111 GuiFont font;
10112 char_u *name = NULL;
10113
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010114 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010115 {
10116 /* Get the "Normal" font. Either the name saved by
10117 * hl_set_font_name() or from the font ID. */
10118 font = gui.norm_font;
10119 name = hl_get_font_name();
10120 }
10121 else
10122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010124 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10125 return;
10126 font = gui_mch_get_font(name, FALSE);
10127 if (font == NOFONT)
10128 return; /* Invalid font name, return empty string. */
10129 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010130 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010131 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010132 gui_mch_free_font(font);
10133 }
10134#endif
10135}
10136
10137/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010138 * "getfperm({fname})" function
10139 */
10140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010141f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010142 typval_T *argvars;
10143 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010144{
10145 char_u *fname;
10146 struct stat st;
10147 char_u *perm = NULL;
10148 char_u flags[] = "rwx";
10149 int i;
10150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010154 if (mch_stat((char *)fname, &st) >= 0)
10155 {
10156 perm = vim_strsave((char_u *)"---------");
10157 if (perm != NULL)
10158 {
10159 for (i = 0; i < 9; i++)
10160 {
10161 if (st.st_mode & (1 << (8 - i)))
10162 perm[i] = flags[i % 3];
10163 }
10164 }
10165 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010166 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010167}
10168
10169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 * "getfsize({fname})" function
10171 */
10172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010173f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010174 typval_T *argvars;
10175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176{
10177 char_u *fname;
10178 struct stat st;
10179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010180 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010182 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183
10184 if (mch_stat((char *)fname, &st) >= 0)
10185 {
10186 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010187 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010190 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010191
10192 /* non-perfect check for overflow */
10193 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10194 rettv->vval.v_number = -2;
10195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 }
10197 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010198 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199}
10200
10201/*
10202 * "getftime({fname})" function
10203 */
10204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010205f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010206 typval_T *argvars;
10207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208{
10209 char_u *fname;
10210 struct stat st;
10211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010212 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213
10214 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010215 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010217 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218}
10219
10220/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010221 * "getftype({fname})" function
10222 */
10223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010224f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010225 typval_T *argvars;
10226 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010227{
10228 char_u *fname;
10229 struct stat st;
10230 char_u *type = NULL;
10231 char *t;
10232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010233 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010235 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010236 if (mch_lstat((char *)fname, &st) >= 0)
10237 {
10238#ifdef S_ISREG
10239 if (S_ISREG(st.st_mode))
10240 t = "file";
10241 else if (S_ISDIR(st.st_mode))
10242 t = "dir";
10243# ifdef S_ISLNK
10244 else if (S_ISLNK(st.st_mode))
10245 t = "link";
10246# endif
10247# ifdef S_ISBLK
10248 else if (S_ISBLK(st.st_mode))
10249 t = "bdev";
10250# endif
10251# ifdef S_ISCHR
10252 else if (S_ISCHR(st.st_mode))
10253 t = "cdev";
10254# endif
10255# ifdef S_ISFIFO
10256 else if (S_ISFIFO(st.st_mode))
10257 t = "fifo";
10258# endif
10259# ifdef S_ISSOCK
10260 else if (S_ISSOCK(st.st_mode))
10261 t = "fifo";
10262# endif
10263 else
10264 t = "other";
10265#else
10266# ifdef S_IFMT
10267 switch (st.st_mode & S_IFMT)
10268 {
10269 case S_IFREG: t = "file"; break;
10270 case S_IFDIR: t = "dir"; break;
10271# ifdef S_IFLNK
10272 case S_IFLNK: t = "link"; break;
10273# endif
10274# ifdef S_IFBLK
10275 case S_IFBLK: t = "bdev"; break;
10276# endif
10277# ifdef S_IFCHR
10278 case S_IFCHR: t = "cdev"; break;
10279# endif
10280# ifdef S_IFIFO
10281 case S_IFIFO: t = "fifo"; break;
10282# endif
10283# ifdef S_IFSOCK
10284 case S_IFSOCK: t = "socket"; break;
10285# endif
10286 default: t = "other";
10287 }
10288# else
10289 if (mch_isdir(fname))
10290 t = "dir";
10291 else
10292 t = "file";
10293# endif
10294#endif
10295 type = vim_strsave((char_u *)t);
10296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010297 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010298}
10299
10300/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010301 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010302 */
10303 static void
10304f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010305 typval_T *argvars;
10306 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010307{
10308 linenr_T lnum;
10309 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010310 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010311
10312 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010313 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010314 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010315 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010316 retlist = FALSE;
10317 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010318 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010319 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010320 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010321 retlist = TRUE;
10322 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010323
Bram Moolenaar342337a2005-07-21 21:11:17 +000010324 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010325}
10326
10327/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010328 * "getmatches()" function
10329 */
10330/*ARGSUSED*/
10331 static void
10332f_getmatches(argvars, rettv)
10333 typval_T *argvars;
10334 typval_T *rettv;
10335{
10336#ifdef FEAT_SEARCH_EXTRA
10337 dict_T *dict;
10338 matchitem_T *cur = curwin->w_match_head;
10339
10340 rettv->vval.v_number = 0;
10341
10342 if (rettv_list_alloc(rettv) == OK)
10343 {
10344 while (cur != NULL)
10345 {
10346 dict = dict_alloc();
10347 if (dict == NULL)
10348 return;
10349 ++dict->dv_refcount;
10350 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10351 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10352 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10353 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10354 list_append_dict(rettv->vval.v_list, dict);
10355 cur = cur->next;
10356 }
10357 }
10358#endif
10359}
10360
10361/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010362 * "getpos(string)" function
10363 */
10364 static void
10365f_getpos(argvars, rettv)
10366 typval_T *argvars;
10367 typval_T *rettv;
10368{
10369 pos_T *fp;
10370 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010371 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010372
10373 if (rettv_list_alloc(rettv) == OK)
10374 {
10375 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010376 fp = var2fpos(&argvars[0], TRUE, &fnum);
10377 if (fnum != -1)
10378 list_append_number(l, (varnumber_T)fnum);
10379 else
10380 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010381 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10382 : (varnumber_T)0);
10383 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10384 : (varnumber_T)0);
10385 list_append_number(l,
10386#ifdef FEAT_VIRTUALEDIT
10387 (fp != NULL) ? (varnumber_T)fp->coladd :
10388#endif
10389 (varnumber_T)0);
10390 }
10391 else
10392 rettv->vval.v_number = FALSE;
10393}
10394
10395/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010396 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010397 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010398/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010399 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010400f_getqflist(argvars, rettv)
10401 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010402 typval_T *rettv;
10403{
10404#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010405 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010406#endif
10407
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010408 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010409#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010410 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010411 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010412 wp = NULL;
10413 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10414 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010415 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010416 if (wp == NULL)
10417 return;
10418 }
10419
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010420 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010421 }
10422#endif
10423}
10424
10425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 * "getreg()" function
10427 */
10428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010430 typval_T *argvars;
10431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432{
10433 char_u *strregname;
10434 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010435 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010436 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010438 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 strregname = get_tv_string_chk(&argvars[0]);
10441 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010442 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010443 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010446 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 regname = (strregname == NULL ? '"' : *strregname);
10448 if (regname == 0)
10449 regname = '"';
10450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010451 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010452 rettv->vval.v_string = error ? NULL :
10453 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454}
10455
10456/*
10457 * "getregtype()" function
10458 */
10459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010460f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010461 typval_T *argvars;
10462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463{
10464 char_u *strregname;
10465 int regname;
10466 char_u buf[NUMBUFLEN + 2];
10467 long reglen = 0;
10468
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010469 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010470 {
10471 strregname = get_tv_string_chk(&argvars[0]);
10472 if (strregname == NULL) /* type error; errmsg already given */
10473 {
10474 rettv->v_type = VAR_STRING;
10475 rettv->vval.v_string = NULL;
10476 return;
10477 }
10478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 else
10480 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010481 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482
10483 regname = (strregname == NULL ? '"' : *strregname);
10484 if (regname == 0)
10485 regname = '"';
10486
10487 buf[0] = NUL;
10488 buf[1] = NUL;
10489 switch (get_reg_type(regname, &reglen))
10490 {
10491 case MLINE: buf[0] = 'V'; break;
10492 case MCHAR: buf[0] = 'v'; break;
10493#ifdef FEAT_VISUAL
10494 case MBLOCK:
10495 buf[0] = Ctrl_V;
10496 sprintf((char *)buf + 1, "%ld", reglen + 1);
10497 break;
10498#endif
10499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500 rettv->v_type = VAR_STRING;
10501 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502}
10503
10504/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010505 * "gettabwinvar()" function
10506 */
10507 static void
10508f_gettabwinvar(argvars, rettv)
10509 typval_T *argvars;
10510 typval_T *rettv;
10511{
10512 getwinvar(argvars, rettv, 1);
10513}
10514
10515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 * "getwinposx()" function
10517 */
10518/*ARGSUSED*/
10519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010520f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010521 typval_T *argvars;
10522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010524 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525#ifdef FEAT_GUI
10526 if (gui.in_use)
10527 {
10528 int x, y;
10529
10530 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010531 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 }
10533#endif
10534}
10535
10536/*
10537 * "getwinposy()" function
10538 */
10539/*ARGSUSED*/
10540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010541f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010542 typval_T *argvars;
10543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546#ifdef FEAT_GUI
10547 if (gui.in_use)
10548 {
10549 int x, y;
10550
10551 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010552 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 }
10554#endif
10555}
10556
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010557/*
10558 * Find window specifed by "vp" in tabpage "tp".
10559 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010560 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010561find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010562 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010563 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010564{
10565#ifdef FEAT_WINDOWS
10566 win_T *wp;
10567#endif
10568 int nr;
10569
10570 nr = get_tv_number_chk(vp, NULL);
10571
10572#ifdef FEAT_WINDOWS
10573 if (nr < 0)
10574 return NULL;
10575 if (nr == 0)
10576 return curwin;
10577
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010578 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10579 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010580 if (--nr <= 0)
10581 break;
10582 return wp;
10583#else
10584 if (nr == 0 || nr == 1)
10585 return curwin;
10586 return NULL;
10587#endif
10588}
10589
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590/*
10591 * "getwinvar()" function
10592 */
10593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010594f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010595 typval_T *argvars;
10596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010598 getwinvar(argvars, rettv, 0);
10599}
10600
10601/*
10602 * getwinvar() and gettabwinvar()
10603 */
10604 static void
10605getwinvar(argvars, rettv, off)
10606 typval_T *argvars;
10607 typval_T *rettv;
10608 int off; /* 1 for gettabwinvar() */
10609{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 win_T *win, *oldcurwin;
10611 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010612 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010613 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010615#ifdef FEAT_WINDOWS
10616 if (off == 1)
10617 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10618 else
10619 tp = curtab;
10620#endif
10621 win = find_win_by_nr(&argvars[off], tp);
10622 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010625 rettv->v_type = VAR_STRING;
10626 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627
10628 if (win != NULL && varname != NULL)
10629 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010630 /* Set curwin to be our win, temporarily. Also set curbuf, so
10631 * that we can get buffer-local options. */
10632 oldcurwin = curwin;
10633 curwin = win;
10634 curbuf = win->w_buffer;
10635
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010637 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 else
10639 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010640 if (*varname == NUL)
10641 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10642 * scope prefix before the NUL byte is required by
10643 * find_var_in_ht(). */
10644 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010646 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010648 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010650
10651 /* restore previous notion of curwin */
10652 curwin = oldcurwin;
10653 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 }
10655
10656 --emsg_off;
10657}
10658
10659/*
10660 * "glob()" function
10661 */
10662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010663f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010664 typval_T *argvars;
10665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666{
10667 expand_T xpc;
10668
10669 ExpandInit(&xpc);
10670 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010671 rettv->v_type = VAR_STRING;
10672 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674}
10675
10676/*
10677 * "globpath()" function
10678 */
10679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010680f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010681 typval_T *argvars;
10682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683{
10684 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010685 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010687 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010688 if (file == NULL)
10689 rettv->vval.v_string = NULL;
10690 else
10691 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692}
10693
10694/*
10695 * "has()" function
10696 */
10697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010698f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010699 typval_T *argvars;
10700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701{
10702 int i;
10703 char_u *name;
10704 int n = FALSE;
10705 static char *(has_list[]) =
10706 {
10707#ifdef AMIGA
10708 "amiga",
10709# ifdef FEAT_ARP
10710 "arp",
10711# endif
10712#endif
10713#ifdef __BEOS__
10714 "beos",
10715#endif
10716#ifdef MSDOS
10717# ifdef DJGPP
10718 "dos32",
10719# else
10720 "dos16",
10721# endif
10722#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010723#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 "mac",
10725#endif
10726#if defined(MACOS_X_UNIX)
10727 "macunix",
10728#endif
10729#ifdef OS2
10730 "os2",
10731#endif
10732#ifdef __QNX__
10733 "qnx",
10734#endif
10735#ifdef RISCOS
10736 "riscos",
10737#endif
10738#ifdef UNIX
10739 "unix",
10740#endif
10741#ifdef VMS
10742 "vms",
10743#endif
10744#ifdef WIN16
10745 "win16",
10746#endif
10747#ifdef WIN32
10748 "win32",
10749#endif
10750#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10751 "win32unix",
10752#endif
10753#ifdef WIN64
10754 "win64",
10755#endif
10756#ifdef EBCDIC
10757 "ebcdic",
10758#endif
10759#ifndef CASE_INSENSITIVE_FILENAME
10760 "fname_case",
10761#endif
10762#ifdef FEAT_ARABIC
10763 "arabic",
10764#endif
10765#ifdef FEAT_AUTOCMD
10766 "autocmd",
10767#endif
10768#ifdef FEAT_BEVAL
10769 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010770# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10771 "balloon_multiline",
10772# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773#endif
10774#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10775 "builtin_terms",
10776# ifdef ALL_BUILTIN_TCAPS
10777 "all_builtin_terms",
10778# endif
10779#endif
10780#ifdef FEAT_BYTEOFF
10781 "byte_offset",
10782#endif
10783#ifdef FEAT_CINDENT
10784 "cindent",
10785#endif
10786#ifdef FEAT_CLIENTSERVER
10787 "clientserver",
10788#endif
10789#ifdef FEAT_CLIPBOARD
10790 "clipboard",
10791#endif
10792#ifdef FEAT_CMDL_COMPL
10793 "cmdline_compl",
10794#endif
10795#ifdef FEAT_CMDHIST
10796 "cmdline_hist",
10797#endif
10798#ifdef FEAT_COMMENTS
10799 "comments",
10800#endif
10801#ifdef FEAT_CRYPT
10802 "cryptv",
10803#endif
10804#ifdef FEAT_CSCOPE
10805 "cscope",
10806#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010807#ifdef CURSOR_SHAPE
10808 "cursorshape",
10809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810#ifdef DEBUG
10811 "debug",
10812#endif
10813#ifdef FEAT_CON_DIALOG
10814 "dialog_con",
10815#endif
10816#ifdef FEAT_GUI_DIALOG
10817 "dialog_gui",
10818#endif
10819#ifdef FEAT_DIFF
10820 "diff",
10821#endif
10822#ifdef FEAT_DIGRAPHS
10823 "digraphs",
10824#endif
10825#ifdef FEAT_DND
10826 "dnd",
10827#endif
10828#ifdef FEAT_EMACS_TAGS
10829 "emacs_tags",
10830#endif
10831 "eval", /* always present, of course! */
10832#ifdef FEAT_EX_EXTRA
10833 "ex_extra",
10834#endif
10835#ifdef FEAT_SEARCH_EXTRA
10836 "extra_search",
10837#endif
10838#ifdef FEAT_FKMAP
10839 "farsi",
10840#endif
10841#ifdef FEAT_SEARCHPATH
10842 "file_in_path",
10843#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010844#if defined(UNIX) && !defined(USE_SYSTEM)
10845 "filterpipe",
10846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847#ifdef FEAT_FIND_ID
10848 "find_in_path",
10849#endif
10850#ifdef FEAT_FOLDING
10851 "folding",
10852#endif
10853#ifdef FEAT_FOOTER
10854 "footer",
10855#endif
10856#if !defined(USE_SYSTEM) && defined(UNIX)
10857 "fork",
10858#endif
10859#ifdef FEAT_GETTEXT
10860 "gettext",
10861#endif
10862#ifdef FEAT_GUI
10863 "gui",
10864#endif
10865#ifdef FEAT_GUI_ATHENA
10866# ifdef FEAT_GUI_NEXTAW
10867 "gui_neXtaw",
10868# else
10869 "gui_athena",
10870# endif
10871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872#ifdef FEAT_GUI_GTK
10873 "gui_gtk",
10874# ifdef HAVE_GTK2
10875 "gui_gtk2",
10876# endif
10877#endif
10878#ifdef FEAT_GUI_MAC
10879 "gui_mac",
10880#endif
10881#ifdef FEAT_GUI_MOTIF
10882 "gui_motif",
10883#endif
10884#ifdef FEAT_GUI_PHOTON
10885 "gui_photon",
10886#endif
10887#ifdef FEAT_GUI_W16
10888 "gui_win16",
10889#endif
10890#ifdef FEAT_GUI_W32
10891 "gui_win32",
10892#endif
10893#ifdef FEAT_HANGULIN
10894 "hangul_input",
10895#endif
10896#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10897 "iconv",
10898#endif
10899#ifdef FEAT_INS_EXPAND
10900 "insert_expand",
10901#endif
10902#ifdef FEAT_JUMPLIST
10903 "jumplist",
10904#endif
10905#ifdef FEAT_KEYMAP
10906 "keymap",
10907#endif
10908#ifdef FEAT_LANGMAP
10909 "langmap",
10910#endif
10911#ifdef FEAT_LIBCALL
10912 "libcall",
10913#endif
10914#ifdef FEAT_LINEBREAK
10915 "linebreak",
10916#endif
10917#ifdef FEAT_LISP
10918 "lispindent",
10919#endif
10920#ifdef FEAT_LISTCMDS
10921 "listcmds",
10922#endif
10923#ifdef FEAT_LOCALMAP
10924 "localmap",
10925#endif
10926#ifdef FEAT_MENU
10927 "menu",
10928#endif
10929#ifdef FEAT_SESSION
10930 "mksession",
10931#endif
10932#ifdef FEAT_MODIFY_FNAME
10933 "modify_fname",
10934#endif
10935#ifdef FEAT_MOUSE
10936 "mouse",
10937#endif
10938#ifdef FEAT_MOUSESHAPE
10939 "mouseshape",
10940#endif
10941#if defined(UNIX) || defined(VMS)
10942# ifdef FEAT_MOUSE_DEC
10943 "mouse_dec",
10944# endif
10945# ifdef FEAT_MOUSE_GPM
10946 "mouse_gpm",
10947# endif
10948# ifdef FEAT_MOUSE_JSB
10949 "mouse_jsbterm",
10950# endif
10951# ifdef FEAT_MOUSE_NET
10952 "mouse_netterm",
10953# endif
10954# ifdef FEAT_MOUSE_PTERM
10955 "mouse_pterm",
10956# endif
10957# ifdef FEAT_MOUSE_XTERM
10958 "mouse_xterm",
10959# endif
10960#endif
10961#ifdef FEAT_MBYTE
10962 "multi_byte",
10963#endif
10964#ifdef FEAT_MBYTE_IME
10965 "multi_byte_ime",
10966#endif
10967#ifdef FEAT_MULTI_LANG
10968 "multi_lang",
10969#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010970#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010971#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010972 "mzscheme",
10973#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975#ifdef FEAT_OLE
10976 "ole",
10977#endif
10978#ifdef FEAT_OSFILETYPE
10979 "osfiletype",
10980#endif
10981#ifdef FEAT_PATH_EXTRA
10982 "path_extra",
10983#endif
10984#ifdef FEAT_PERL
10985#ifndef DYNAMIC_PERL
10986 "perl",
10987#endif
10988#endif
10989#ifdef FEAT_PYTHON
10990#ifndef DYNAMIC_PYTHON
10991 "python",
10992#endif
10993#endif
10994#ifdef FEAT_POSTSCRIPT
10995 "postscript",
10996#endif
10997#ifdef FEAT_PRINTER
10998 "printer",
10999#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011000#ifdef FEAT_PROFILE
11001 "profile",
11002#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011003#ifdef FEAT_RELTIME
11004 "reltime",
11005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006#ifdef FEAT_QUICKFIX
11007 "quickfix",
11008#endif
11009#ifdef FEAT_RIGHTLEFT
11010 "rightleft",
11011#endif
11012#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11013 "ruby",
11014#endif
11015#ifdef FEAT_SCROLLBIND
11016 "scrollbind",
11017#endif
11018#ifdef FEAT_CMDL_INFO
11019 "showcmd",
11020 "cmdline_info",
11021#endif
11022#ifdef FEAT_SIGNS
11023 "signs",
11024#endif
11025#ifdef FEAT_SMARTINDENT
11026 "smartindent",
11027#endif
11028#ifdef FEAT_SNIFF
11029 "sniff",
11030#endif
11031#ifdef FEAT_STL_OPT
11032 "statusline",
11033#endif
11034#ifdef FEAT_SUN_WORKSHOP
11035 "sun_workshop",
11036#endif
11037#ifdef FEAT_NETBEANS_INTG
11038 "netbeans_intg",
11039#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011040#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011041 "spell",
11042#endif
11043#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 "syntax",
11045#endif
11046#if defined(USE_SYSTEM) || !defined(UNIX)
11047 "system",
11048#endif
11049#ifdef FEAT_TAG_BINS
11050 "tag_binary",
11051#endif
11052#ifdef FEAT_TAG_OLDSTATIC
11053 "tag_old_static",
11054#endif
11055#ifdef FEAT_TAG_ANYWHITE
11056 "tag_any_white",
11057#endif
11058#ifdef FEAT_TCL
11059# ifndef DYNAMIC_TCL
11060 "tcl",
11061# endif
11062#endif
11063#ifdef TERMINFO
11064 "terminfo",
11065#endif
11066#ifdef FEAT_TERMRESPONSE
11067 "termresponse",
11068#endif
11069#ifdef FEAT_TEXTOBJ
11070 "textobjects",
11071#endif
11072#ifdef HAVE_TGETENT
11073 "tgetent",
11074#endif
11075#ifdef FEAT_TITLE
11076 "title",
11077#endif
11078#ifdef FEAT_TOOLBAR
11079 "toolbar",
11080#endif
11081#ifdef FEAT_USR_CMDS
11082 "user-commands", /* was accidentally included in 5.4 */
11083 "user_commands",
11084#endif
11085#ifdef FEAT_VIMINFO
11086 "viminfo",
11087#endif
11088#ifdef FEAT_VERTSPLIT
11089 "vertsplit",
11090#endif
11091#ifdef FEAT_VIRTUALEDIT
11092 "virtualedit",
11093#endif
11094#ifdef FEAT_VISUAL
11095 "visual",
11096#endif
11097#ifdef FEAT_VISUALEXTRA
11098 "visualextra",
11099#endif
11100#ifdef FEAT_VREPLACE
11101 "vreplace",
11102#endif
11103#ifdef FEAT_WILDIGN
11104 "wildignore",
11105#endif
11106#ifdef FEAT_WILDMENU
11107 "wildmenu",
11108#endif
11109#ifdef FEAT_WINDOWS
11110 "windows",
11111#endif
11112#ifdef FEAT_WAK
11113 "winaltkeys",
11114#endif
11115#ifdef FEAT_WRITEBACKUP
11116 "writebackup",
11117#endif
11118#ifdef FEAT_XIM
11119 "xim",
11120#endif
11121#ifdef FEAT_XFONTSET
11122 "xfontset",
11123#endif
11124#ifdef USE_XSMP
11125 "xsmp",
11126#endif
11127#ifdef USE_XSMP_INTERACT
11128 "xsmp_interact",
11129#endif
11130#ifdef FEAT_XCLIPBOARD
11131 "xterm_clipboard",
11132#endif
11133#ifdef FEAT_XTERM_SAVE
11134 "xterm_save",
11135#endif
11136#if defined(UNIX) && defined(FEAT_X11)
11137 "X11",
11138#endif
11139 NULL
11140 };
11141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 for (i = 0; has_list[i] != NULL; ++i)
11144 if (STRICMP(name, has_list[i]) == 0)
11145 {
11146 n = TRUE;
11147 break;
11148 }
11149
11150 if (n == FALSE)
11151 {
11152 if (STRNICMP(name, "patch", 5) == 0)
11153 n = has_patch(atoi((char *)name + 5));
11154 else if (STRICMP(name, "vim_starting") == 0)
11155 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011156#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11157 else if (STRICMP(name, "balloon_multiline") == 0)
11158 n = multiline_balloon_available();
11159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160#ifdef DYNAMIC_TCL
11161 else if (STRICMP(name, "tcl") == 0)
11162 n = tcl_enabled(FALSE);
11163#endif
11164#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11165 else if (STRICMP(name, "iconv") == 0)
11166 n = iconv_enabled(FALSE);
11167#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011168#ifdef DYNAMIC_MZSCHEME
11169 else if (STRICMP(name, "mzscheme") == 0)
11170 n = mzscheme_enabled(FALSE);
11171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172#ifdef DYNAMIC_RUBY
11173 else if (STRICMP(name, "ruby") == 0)
11174 n = ruby_enabled(FALSE);
11175#endif
11176#ifdef DYNAMIC_PYTHON
11177 else if (STRICMP(name, "python") == 0)
11178 n = python_enabled(FALSE);
11179#endif
11180#ifdef DYNAMIC_PERL
11181 else if (STRICMP(name, "perl") == 0)
11182 n = perl_enabled(FALSE);
11183#endif
11184#ifdef FEAT_GUI
11185 else if (STRICMP(name, "gui_running") == 0)
11186 n = (gui.in_use || gui.starting);
11187# ifdef FEAT_GUI_W32
11188 else if (STRICMP(name, "gui_win32s") == 0)
11189 n = gui_is_win32s();
11190# endif
11191# ifdef FEAT_BROWSE
11192 else if (STRICMP(name, "browse") == 0)
11193 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11194# endif
11195#endif
11196#ifdef FEAT_SYN_HL
11197 else if (STRICMP(name, "syntax_items") == 0)
11198 n = syntax_present(curbuf);
11199#endif
11200#if defined(WIN3264)
11201 else if (STRICMP(name, "win95") == 0)
11202 n = mch_windows95();
11203#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011204#ifdef FEAT_NETBEANS_INTG
11205 else if (STRICMP(name, "netbeans_enabled") == 0)
11206 n = usingNetbeans;
11207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 }
11209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211}
11212
11213/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011214 * "has_key()" function
11215 */
11216 static void
11217f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011218 typval_T *argvars;
11219 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011220{
11221 rettv->vval.v_number = 0;
11222 if (argvars[0].v_type != VAR_DICT)
11223 {
11224 EMSG(_(e_dictreq));
11225 return;
11226 }
11227 if (argvars[0].vval.v_dict == NULL)
11228 return;
11229
11230 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011231 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011232}
11233
11234/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011235 * "haslocaldir()" function
11236 */
11237/*ARGSUSED*/
11238 static void
11239f_haslocaldir(argvars, rettv)
11240 typval_T *argvars;
11241 typval_T *rettv;
11242{
11243 rettv->vval.v_number = (curwin->w_localdir != NULL);
11244}
11245
11246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 * "hasmapto()" function
11248 */
11249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011251 typval_T *argvars;
11252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253{
11254 char_u *name;
11255 char_u *mode;
11256 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011257 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011260 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 mode = (char_u *)"nvo";
11262 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011265 if (argvars[2].v_type != VAR_UNKNOWN)
11266 abbr = get_tv_number(&argvars[2]);
11267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268
Bram Moolenaar2c932302006-03-18 21:42:09 +000011269 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273}
11274
11275/*
11276 * "histadd()" function
11277 */
11278/*ARGSUSED*/
11279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011281 typval_T *argvars;
11282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283{
11284#ifdef FEAT_CMDHIST
11285 int histype;
11286 char_u *str;
11287 char_u buf[NUMBUFLEN];
11288#endif
11289
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 if (check_restricted() || check_secure())
11292 return;
11293#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011294 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11295 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 if (histype >= 0)
11297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299 if (*str != NUL)
11300 {
11301 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011302 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 return;
11304 }
11305 }
11306#endif
11307}
11308
11309/*
11310 * "histdel()" function
11311 */
11312/*ARGSUSED*/
11313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011315 typval_T *argvars;
11316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317{
11318#ifdef FEAT_CMDHIST
11319 int n;
11320 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011321 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011323 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11324 if (str == NULL)
11325 n = 0;
11326 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011328 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011329 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011332 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 else
11334 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011335 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 get_tv_string_buf(&argvars[1], buf));
11337 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340#endif
11341}
11342
11343/*
11344 * "histget()" function
11345 */
11346/*ARGSUSED*/
11347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011348f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011349 typval_T *argvars;
11350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351{
11352#ifdef FEAT_CMDHIST
11353 int type;
11354 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011355 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011357 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11358 if (str == NULL)
11359 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011361 {
11362 type = get_histtype(str);
11363 if (argvars[1].v_type == VAR_UNKNOWN)
11364 idx = get_history_idx(type);
11365 else
11366 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11367 /* -1 on type error */
11368 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011371 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011373 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374}
11375
11376/*
11377 * "histnr()" function
11378 */
11379/*ARGSUSED*/
11380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011382 typval_T *argvars;
11383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384{
11385 int i;
11386
11387#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011388 char_u *history = get_tv_string_chk(&argvars[0]);
11389
11390 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391 if (i >= HIST_CMD && i < HIST_COUNT)
11392 i = get_history_idx(i);
11393 else
11394#endif
11395 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397}
11398
11399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400 * "highlightID(name)" function
11401 */
11402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011404 typval_T *argvars;
11405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408}
11409
11410/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011411 * "highlight_exists()" function
11412 */
11413 static void
11414f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011415 typval_T *argvars;
11416 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011417{
11418 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11419}
11420
11421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 * "hostname()" function
11423 */
11424/*ARGSUSED*/
11425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011427 typval_T *argvars;
11428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429{
11430 char_u hostname[256];
11431
11432 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011433 rettv->v_type = VAR_STRING;
11434 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435}
11436
11437/*
11438 * iconv() function
11439 */
11440/*ARGSUSED*/
11441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011443 typval_T *argvars;
11444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445{
11446#ifdef FEAT_MBYTE
11447 char_u buf1[NUMBUFLEN];
11448 char_u buf2[NUMBUFLEN];
11449 char_u *from, *to, *str;
11450 vimconv_T vimconv;
11451#endif
11452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453 rettv->v_type = VAR_STRING;
11454 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455
11456#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 str = get_tv_string(&argvars[0]);
11458 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11459 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460 vimconv.vc_type = CONV_NONE;
11461 convert_setup(&vimconv, from, to);
11462
11463 /* If the encodings are equal, no conversion needed. */
11464 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011465 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468
11469 convert_setup(&vimconv, NULL, NULL);
11470 vim_free(from);
11471 vim_free(to);
11472#endif
11473}
11474
11475/*
11476 * "indent()" function
11477 */
11478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011479f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011480 typval_T *argvars;
11481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482{
11483 linenr_T lnum;
11484
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011489 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490}
11491
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011492/*
11493 * "index()" function
11494 */
11495 static void
11496f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011497 typval_T *argvars;
11498 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011499{
Bram Moolenaar33570922005-01-25 22:26:29 +000011500 list_T *l;
11501 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011502 long idx = 0;
11503 int ic = FALSE;
11504
11505 rettv->vval.v_number = -1;
11506 if (argvars[0].v_type != VAR_LIST)
11507 {
11508 EMSG(_(e_listreq));
11509 return;
11510 }
11511 l = argvars[0].vval.v_list;
11512 if (l != NULL)
11513 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011514 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011515 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011517 int error = FALSE;
11518
Bram Moolenaar758711c2005-02-02 23:11:38 +000011519 /* Start at specified item. Use the cached index that list_find()
11520 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011521 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011522 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011523 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011524 ic = get_tv_number_chk(&argvars[3], &error);
11525 if (error)
11526 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011527 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011528
Bram Moolenaar758711c2005-02-02 23:11:38 +000011529 for ( ; item != NULL; item = item->li_next, ++idx)
11530 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011531 {
11532 rettv->vval.v_number = idx;
11533 break;
11534 }
11535 }
11536}
11537
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538static int inputsecret_flag = 0;
11539
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011540static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11541
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011543 * This function is used by f_input() and f_inputdialog() functions. The third
11544 * argument to f_input() specifies the type of completion to use at the
11545 * prompt. The third argument to f_inputdialog() specifies the value to return
11546 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547 */
11548 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011549get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011550 typval_T *argvars;
11551 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011552 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555 char_u *p = NULL;
11556 int c;
11557 char_u buf[NUMBUFLEN];
11558 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011560 int xp_type = EXPAND_NOTHING;
11561 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564
11565#ifdef NO_CONSOLE_INPUT
11566 /* While starting up, there is no place to enter text. */
11567 if (no_console_input())
11568 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011569 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570 return;
11571 }
11572#endif
11573
11574 cmd_silent = FALSE; /* Want to see the prompt. */
11575 if (prompt != NULL)
11576 {
11577 /* Only the part of the message after the last NL is considered as
11578 * prompt for the command line */
11579 p = vim_strrchr(prompt, '\n');
11580 if (p == NULL)
11581 p = prompt;
11582 else
11583 {
11584 ++p;
11585 c = *p;
11586 *p = NUL;
11587 msg_start();
11588 msg_clr_eos();
11589 msg_puts_attr(prompt, echo_attr);
11590 msg_didout = FALSE;
11591 msg_starthere();
11592 *p = c;
11593 }
11594 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011596 if (argvars[1].v_type != VAR_UNKNOWN)
11597 {
11598 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11599 if (defstr != NULL)
11600 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011602 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011603 {
11604 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011605 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011606 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011607
Bram Moolenaar4463f292005-09-25 22:20:24 +000011608 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011609
Bram Moolenaar4463f292005-09-25 22:20:24 +000011610 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11611 if (xp_name == NULL)
11612 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011613
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011614 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011615
Bram Moolenaar4463f292005-09-25 22:20:24 +000011616 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11617 &xp_arg) == FAIL)
11618 return;
11619 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011620 }
11621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011622 if (defstr != NULL)
11623 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011624 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11625 xp_type, xp_arg);
11626
11627 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011629 /* since the user typed this, no need to wait for return */
11630 need_wait_return = FALSE;
11631 msg_didout = FALSE;
11632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633 cmd_silent = cmd_silent_save;
11634}
11635
11636/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011637 * "input()" function
11638 * Also handles inputsecret() when inputsecret is set.
11639 */
11640 static void
11641f_input(argvars, rettv)
11642 typval_T *argvars;
11643 typval_T *rettv;
11644{
11645 get_user_input(argvars, rettv, FALSE);
11646}
11647
11648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 * "inputdialog()" function
11650 */
11651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011652f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011653 typval_T *argvars;
11654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655{
11656#if defined(FEAT_GUI_TEXTDIALOG)
11657 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11658 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11659 {
11660 char_u *message;
11661 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011662 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011664 message = get_tv_string_chk(&argvars[0]);
11665 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011666 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011667 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668 else
11669 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011670 if (message != NULL && defstr != NULL
11671 && do_dialog(VIM_QUESTION, NULL, message,
11672 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 else
11675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011676 if (message != NULL && defstr != NULL
11677 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011678 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011679 rettv->vval.v_string = vim_strsave(
11680 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011684 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685 }
11686 else
11687#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011688 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689}
11690
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011691/*
11692 * "inputlist()" function
11693 */
11694 static void
11695f_inputlist(argvars, rettv)
11696 typval_T *argvars;
11697 typval_T *rettv;
11698{
11699 listitem_T *li;
11700 int selected;
11701 int mouse_used;
11702
11703 rettv->vval.v_number = 0;
11704#ifdef NO_CONSOLE_INPUT
11705 /* While starting up, there is no place to enter text. */
11706 if (no_console_input())
11707 return;
11708#endif
11709 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11710 {
11711 EMSG2(_(e_listarg), "inputlist()");
11712 return;
11713 }
11714
11715 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011716 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011717 lines_left = Rows; /* avoid more prompt */
11718 msg_scroll = TRUE;
11719 msg_clr_eos();
11720
11721 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11722 {
11723 msg_puts(get_tv_string(&li->li_tv));
11724 msg_putchar('\n');
11725 }
11726
11727 /* Ask for choice. */
11728 selected = prompt_for_number(&mouse_used);
11729 if (mouse_used)
11730 selected -= lines_left;
11731
11732 rettv->vval.v_number = selected;
11733}
11734
11735
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11737
11738/*
11739 * "inputrestore()" function
11740 */
11741/*ARGSUSED*/
11742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011743f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011744 typval_T *argvars;
11745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746{
11747 if (ga_userinput.ga_len > 0)
11748 {
11749 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11751 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753 }
11754 else if (p_verbose > 1)
11755 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011756 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011757 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758 }
11759}
11760
11761/*
11762 * "inputsave()" function
11763 */
11764/*ARGSUSED*/
11765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011766f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011767 typval_T *argvars;
11768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769{
11770 /* Add an entry to the stack of typehead storage. */
11771 if (ga_grow(&ga_userinput, 1) == OK)
11772 {
11773 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11774 + ga_userinput.ga_len);
11775 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777 }
11778 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780}
11781
11782/*
11783 * "inputsecret()" function
11784 */
11785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011786f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011787 typval_T *argvars;
11788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789{
11790 ++cmdline_star;
11791 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011792 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 --cmdline_star;
11794 --inputsecret_flag;
11795}
11796
11797/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011798 * "insert()" function
11799 */
11800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011801f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011802 typval_T *argvars;
11803 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011804{
11805 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011806 listitem_T *item;
11807 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011809
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011810 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011811 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011812 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011813 else if ((l = argvars[0].vval.v_list) != NULL
11814 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011815 {
11816 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011817 before = get_tv_number_chk(&argvars[2], &error);
11818 if (error)
11819 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011820
Bram Moolenaar758711c2005-02-02 23:11:38 +000011821 if (before == l->lv_len)
11822 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011823 else
11824 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011825 item = list_find(l, before);
11826 if (item == NULL)
11827 {
11828 EMSGN(_(e_listidx), before);
11829 l = NULL;
11830 }
11831 }
11832 if (l != NULL)
11833 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011834 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011835 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011836 }
11837 }
11838}
11839
11840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 * "isdirectory()" function
11842 */
11843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011844f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011845 typval_T *argvars;
11846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011848 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849}
11850
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011851/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011852 * "islocked()" function
11853 */
11854 static void
11855f_islocked(argvars, rettv)
11856 typval_T *argvars;
11857 typval_T *rettv;
11858{
11859 lval_T lv;
11860 char_u *end;
11861 dictitem_T *di;
11862
11863 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011864 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11865 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011866 if (end != NULL && lv.ll_name != NULL)
11867 {
11868 if (*end != NUL)
11869 EMSG(_(e_trailing));
11870 else
11871 {
11872 if (lv.ll_tv == NULL)
11873 {
11874 if (check_changedtick(lv.ll_name))
11875 rettv->vval.v_number = 1; /* always locked */
11876 else
11877 {
11878 di = find_var(lv.ll_name, NULL);
11879 if (di != NULL)
11880 {
11881 /* Consider a variable locked when:
11882 * 1. the variable itself is locked
11883 * 2. the value of the variable is locked.
11884 * 3. the List or Dict value is locked.
11885 */
11886 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11887 || tv_islocked(&di->di_tv));
11888 }
11889 }
11890 }
11891 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011892 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011893 else if (lv.ll_newkey != NULL)
11894 EMSG2(_(e_dictkey), lv.ll_newkey);
11895 else if (lv.ll_list != NULL)
11896 /* List item. */
11897 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11898 else
11899 /* Dictionary item. */
11900 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11901 }
11902 }
11903
11904 clear_lval(&lv);
11905}
11906
Bram Moolenaar33570922005-01-25 22:26:29 +000011907static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011908
11909/*
11910 * Turn a dict into a list:
11911 * "what" == 0: list of keys
11912 * "what" == 1: list of values
11913 * "what" == 2: list of items
11914 */
11915 static void
11916dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011917 typval_T *argvars;
11918 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011919 int what;
11920{
Bram Moolenaar33570922005-01-25 22:26:29 +000011921 list_T *l2;
11922 dictitem_T *di;
11923 hashitem_T *hi;
11924 listitem_T *li;
11925 listitem_T *li2;
11926 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011927 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011928
11929 rettv->vval.v_number = 0;
11930 if (argvars[0].v_type != VAR_DICT)
11931 {
11932 EMSG(_(e_dictreq));
11933 return;
11934 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011935 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011936 return;
11937
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011938 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011939 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011940
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011941 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011942 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011943 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011944 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011945 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011946 --todo;
11947 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011948
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011949 li = listitem_alloc();
11950 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011951 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011952 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011953
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011954 if (what == 0)
11955 {
11956 /* keys() */
11957 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011958 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011959 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11960 }
11961 else if (what == 1)
11962 {
11963 /* values() */
11964 copy_tv(&di->di_tv, &li->li_tv);
11965 }
11966 else
11967 {
11968 /* items() */
11969 l2 = list_alloc();
11970 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011971 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011972 li->li_tv.vval.v_list = l2;
11973 if (l2 == NULL)
11974 break;
11975 ++l2->lv_refcount;
11976
11977 li2 = listitem_alloc();
11978 if (li2 == NULL)
11979 break;
11980 list_append(l2, li2);
11981 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011982 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011983 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11984
11985 li2 = listitem_alloc();
11986 if (li2 == NULL)
11987 break;
11988 list_append(l2, li2);
11989 copy_tv(&di->di_tv, &li2->li_tv);
11990 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011991 }
11992 }
11993}
11994
11995/*
11996 * "items(dict)" function
11997 */
11998 static void
11999f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012000 typval_T *argvars;
12001 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012002{
12003 dict_list(argvars, rettv, 2);
12004}
12005
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012007 * "join()" function
12008 */
12009 static void
12010f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012011 typval_T *argvars;
12012 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012013{
12014 garray_T ga;
12015 char_u *sep;
12016
12017 rettv->vval.v_number = 0;
12018 if (argvars[0].v_type != VAR_LIST)
12019 {
12020 EMSG(_(e_listreq));
12021 return;
12022 }
12023 if (argvars[0].vval.v_list == NULL)
12024 return;
12025 if (argvars[1].v_type == VAR_UNKNOWN)
12026 sep = (char_u *)" ";
12027 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012028 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012029
12030 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012031
12032 if (sep != NULL)
12033 {
12034 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012035 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012036 ga_append(&ga, NUL);
12037 rettv->vval.v_string = (char_u *)ga.ga_data;
12038 }
12039 else
12040 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012041}
12042
12043/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012044 * "keys()" function
12045 */
12046 static void
12047f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012048 typval_T *argvars;
12049 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012050{
12051 dict_list(argvars, rettv, 0);
12052}
12053
12054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 * "last_buffer_nr()" function.
12056 */
12057/*ARGSUSED*/
12058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012059f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012060 typval_T *argvars;
12061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062{
12063 int n = 0;
12064 buf_T *buf;
12065
12066 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12067 if (n < buf->b_fnum)
12068 n = buf->b_fnum;
12069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012070 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012071}
12072
12073/*
12074 * "len()" function
12075 */
12076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012077f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012078 typval_T *argvars;
12079 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012080{
12081 switch (argvars[0].v_type)
12082 {
12083 case VAR_STRING:
12084 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012085 rettv->vval.v_number = (varnumber_T)STRLEN(
12086 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012087 break;
12088 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012089 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012090 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012091 case VAR_DICT:
12092 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12093 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012094 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012095 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012096 break;
12097 }
12098}
12099
Bram Moolenaar33570922005-01-25 22:26:29 +000012100static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012101
12102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012104 typval_T *argvars;
12105 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012106 int type;
12107{
12108#ifdef FEAT_LIBCALL
12109 char_u *string_in;
12110 char_u **string_result;
12111 int nr_result;
12112#endif
12113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012114 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012115 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012116 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012117 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012118 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012119
12120 if (check_restricted() || check_secure())
12121 return;
12122
12123#ifdef FEAT_LIBCALL
12124 /* The first two args must be strings, otherwise its meaningless */
12125 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12126 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012127 string_in = NULL;
12128 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012129 string_in = argvars[2].vval.v_string;
12130 if (type == VAR_NUMBER)
12131 string_result = NULL;
12132 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012134 if (mch_libcall(argvars[0].vval.v_string,
12135 argvars[1].vval.v_string,
12136 string_in,
12137 argvars[2].vval.v_number,
12138 string_result,
12139 &nr_result) == OK
12140 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012141 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012142 }
12143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144}
12145
12146/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012147 * "libcall()" function
12148 */
12149 static void
12150f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012151 typval_T *argvars;
12152 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012153{
12154 libcall_common(argvars, rettv, VAR_STRING);
12155}
12156
12157/*
12158 * "libcallnr()" function
12159 */
12160 static void
12161f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012162 typval_T *argvars;
12163 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012164{
12165 libcall_common(argvars, rettv, VAR_NUMBER);
12166}
12167
12168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169 * "line(string)" function
12170 */
12171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012173 typval_T *argvars;
12174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175{
12176 linenr_T lnum = 0;
12177 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012178 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012180 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181 if (fp != NULL)
12182 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184}
12185
12186/*
12187 * "line2byte(lnum)" function
12188 */
12189/*ARGSUSED*/
12190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012191f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012192 typval_T *argvars;
12193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194{
12195#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012196 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197#else
12198 linenr_T lnum;
12199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012200 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012202 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12205 if (rettv->vval.v_number >= 0)
12206 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207#endif
12208}
12209
12210/*
12211 * "lispindent(lnum)" function
12212 */
12213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012214f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012215 typval_T *argvars;
12216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217{
12218#ifdef FEAT_LISP
12219 pos_T pos;
12220 linenr_T lnum;
12221
12222 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012223 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12225 {
12226 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012227 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228 curwin->w_cursor = pos;
12229 }
12230 else
12231#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012232 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233}
12234
12235/*
12236 * "localtime()" function
12237 */
12238/*ARGSUSED*/
12239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012240f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012241 typval_T *argvars;
12242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012244 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245}
12246
Bram Moolenaar33570922005-01-25 22:26:29 +000012247static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248
12249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012250get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012251 typval_T *argvars;
12252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253 int exact;
12254{
12255 char_u *keys;
12256 char_u *which;
12257 char_u buf[NUMBUFLEN];
12258 char_u *keys_buf = NULL;
12259 char_u *rhs;
12260 int mode;
12261 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012262 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263
12264 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012265 rettv->v_type = VAR_STRING;
12266 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012268 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269 if (*keys == NUL)
12270 return;
12271
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012272 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012274 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012275 if (argvars[2].v_type != VAR_UNKNOWN)
12276 abbr = get_tv_number(&argvars[2]);
12277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278 else
12279 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012280 if (which == NULL)
12281 return;
12282
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 mode = get_map_mode(&which, 0);
12284
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012285 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012286 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 vim_free(keys_buf);
12288 if (rhs != NULL)
12289 {
12290 ga_init(&ga);
12291 ga.ga_itemsize = 1;
12292 ga.ga_growsize = 40;
12293
12294 while (*rhs != NUL)
12295 ga_concat(&ga, str2special(&rhs, FALSE));
12296
12297 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012298 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299 }
12300}
12301
12302/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012303 * "map()" function
12304 */
12305 static void
12306f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012307 typval_T *argvars;
12308 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012309{
12310 filter_map(argvars, rettv, TRUE);
12311}
12312
12313/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012314 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315 */
12316 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012317f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012318 typval_T *argvars;
12319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012321 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322}
12323
12324/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012325 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 */
12327 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012328f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012329 typval_T *argvars;
12330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012332 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333}
12334
Bram Moolenaar33570922005-01-25 22:26:29 +000012335static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336
12337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012339 typval_T *argvars;
12340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341 int type;
12342{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012343 char_u *str = NULL;
12344 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 char_u *pat;
12346 regmatch_T regmatch;
12347 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012348 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 char_u *save_cpo;
12350 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012351 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012352 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012353 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012354 list_T *l = NULL;
12355 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012356 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012357 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358
12359 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12360 save_cpo = p_cpo;
12361 p_cpo = (char_u *)"";
12362
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012363 rettv->vval.v_number = -1;
12364 if (type == 3)
12365 {
12366 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012367 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012368 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012369 }
12370 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012372 rettv->v_type = VAR_STRING;
12373 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012376 if (argvars[0].v_type == VAR_LIST)
12377 {
12378 if ((l = argvars[0].vval.v_list) == NULL)
12379 goto theend;
12380 li = l->lv_first;
12381 }
12382 else
12383 expr = str = get_tv_string(&argvars[0]);
12384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012385 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12386 if (pat == NULL)
12387 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012388
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012389 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012391 int error = FALSE;
12392
12393 start = get_tv_number_chk(&argvars[2], &error);
12394 if (error)
12395 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012396 if (l != NULL)
12397 {
12398 li = list_find(l, start);
12399 if (li == NULL)
12400 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012401 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012402 }
12403 else
12404 {
12405 if (start < 0)
12406 start = 0;
12407 if (start > (long)STRLEN(str))
12408 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012409 /* When "count" argument is there ignore matches before "start",
12410 * otherwise skip part of the string. Differs when pattern is "^"
12411 * or "\<". */
12412 if (argvars[3].v_type != VAR_UNKNOWN)
12413 startcol = start;
12414 else
12415 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012416 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012417
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012418 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012419 nth = get_tv_number_chk(&argvars[3], &error);
12420 if (error)
12421 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422 }
12423
12424 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12425 if (regmatch.regprog != NULL)
12426 {
12427 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012428
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012429 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012430 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012431 if (l != NULL)
12432 {
12433 if (li == NULL)
12434 {
12435 match = FALSE;
12436 break;
12437 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012438 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012439 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012440 if (str == NULL)
12441 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012442 }
12443
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012444 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012445
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012446 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012447 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012448 if (l == NULL && !match)
12449 break;
12450
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012451 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012452 if (l != NULL)
12453 {
12454 li = li->li_next;
12455 ++idx;
12456 }
12457 else
12458 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012459#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012460 startcol = (colnr_T)(regmatch.startp[0]
12461 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012462#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012463 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012464#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012465 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012466 }
12467
12468 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012470 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012471 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012472 int i;
12473
12474 /* return list with matched string and submatches */
12475 for (i = 0; i < NSUBEXP; ++i)
12476 {
12477 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012478 {
12479 if (list_append_string(rettv->vval.v_list,
12480 (char_u *)"", 0) == FAIL)
12481 break;
12482 }
12483 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012484 regmatch.startp[i],
12485 (int)(regmatch.endp[i] - regmatch.startp[i]))
12486 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012487 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012488 }
12489 }
12490 else if (type == 2)
12491 {
12492 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012493 if (l != NULL)
12494 copy_tv(&li->li_tv, rettv);
12495 else
12496 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012498 }
12499 else if (l != NULL)
12500 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 else
12502 {
12503 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012504 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505 (varnumber_T)(regmatch.startp[0] - str);
12506 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012507 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012509 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510 }
12511 }
12512 vim_free(regmatch.regprog);
12513 }
12514
12515theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012516 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517 p_cpo = save_cpo;
12518}
12519
12520/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012521 * "match()" function
12522 */
12523 static void
12524f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012525 typval_T *argvars;
12526 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012527{
12528 find_some_match(argvars, rettv, 1);
12529}
12530
12531/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012532 * "matchadd()" function
12533 */
12534 static void
12535f_matchadd(argvars, rettv)
12536 typval_T *argvars;
12537 typval_T *rettv;
12538{
12539#ifdef FEAT_SEARCH_EXTRA
12540 char_u buf[NUMBUFLEN];
12541 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
12542 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
12543 int prio = 10; /* default priority */
12544 int id = -1;
12545 int error = FALSE;
12546
12547 rettv->vval.v_number = -1;
12548
12549 if (grp == NULL || pat == NULL)
12550 return;
12551 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012552 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012553 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012554 if (argvars[3].v_type != VAR_UNKNOWN)
12555 id = get_tv_number_chk(&argvars[3], &error);
12556 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012557 if (error == TRUE)
12558 return;
12559 if (id >= 1 && id <= 3)
12560 {
12561 EMSGN("E798: ID is reserved for \":match\": %ld", id);
12562 return;
12563 }
12564
12565 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
12566#endif
12567}
12568
12569/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012570 * "matcharg()" function
12571 */
12572 static void
12573f_matcharg(argvars, rettv)
12574 typval_T *argvars;
12575 typval_T *rettv;
12576{
12577 if (rettv_list_alloc(rettv) == OK)
12578 {
12579#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012580 int id = get_tv_number(&argvars[0]);
12581 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012582
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012583 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012584 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012585 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
12586 {
12587 list_append_string(rettv->vval.v_list,
12588 syn_id2name(m->hlg_id), -1);
12589 list_append_string(rettv->vval.v_list, m->pattern, -1);
12590 }
12591 else
12592 {
12593 list_append_string(rettv->vval.v_list, NUL, -1);
12594 list_append_string(rettv->vval.v_list, NUL, -1);
12595 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012596 }
12597#endif
12598 }
12599}
12600
12601/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012602 * "matchdelete()" function
12603 */
12604 static void
12605f_matchdelete(argvars, rettv)
12606 typval_T *argvars;
12607 typval_T *rettv;
12608{
12609#ifdef FEAT_SEARCH_EXTRA
12610 rettv->vval.v_number = match_delete(curwin,
12611 (int)get_tv_number(&argvars[0]), TRUE);
12612#endif
12613}
12614
12615/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012616 * "matchend()" function
12617 */
12618 static void
12619f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012620 typval_T *argvars;
12621 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012622{
12623 find_some_match(argvars, rettv, 0);
12624}
12625
12626/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012627 * "matchlist()" function
12628 */
12629 static void
12630f_matchlist(argvars, rettv)
12631 typval_T *argvars;
12632 typval_T *rettv;
12633{
12634 find_some_match(argvars, rettv, 3);
12635}
12636
12637/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012638 * "matchstr()" function
12639 */
12640 static void
12641f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012642 typval_T *argvars;
12643 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012644{
12645 find_some_match(argvars, rettv, 2);
12646}
12647
Bram Moolenaar33570922005-01-25 22:26:29 +000012648static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012649
12650 static void
12651max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012652 typval_T *argvars;
12653 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012654 int domax;
12655{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012656 long n = 0;
12657 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012658 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012659
12660 if (argvars[0].v_type == VAR_LIST)
12661 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012662 list_T *l;
12663 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012664
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012665 l = argvars[0].vval.v_list;
12666 if (l != NULL)
12667 {
12668 li = l->lv_first;
12669 if (li != NULL)
12670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012671 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012672 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012673 {
12674 li = li->li_next;
12675 if (li == NULL)
12676 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012677 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012678 if (domax ? i > n : i < n)
12679 n = i;
12680 }
12681 }
12682 }
12683 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012684 else if (argvars[0].v_type == VAR_DICT)
12685 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012686 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012687 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012688 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012689 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012690
12691 d = argvars[0].vval.v_dict;
12692 if (d != NULL)
12693 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012694 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012695 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012696 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012697 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012698 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012699 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012700 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012701 if (first)
12702 {
12703 n = i;
12704 first = FALSE;
12705 }
12706 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012707 n = i;
12708 }
12709 }
12710 }
12711 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012712 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012713 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012714 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012715}
12716
12717/*
12718 * "max()" function
12719 */
12720 static void
12721f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012722 typval_T *argvars;
12723 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012724{
12725 max_min(argvars, rettv, TRUE);
12726}
12727
12728/*
12729 * "min()" function
12730 */
12731 static void
12732f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012733 typval_T *argvars;
12734 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012735{
12736 max_min(argvars, rettv, FALSE);
12737}
12738
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012739static int mkdir_recurse __ARGS((char_u *dir, int prot));
12740
12741/*
12742 * Create the directory in which "dir" is located, and higher levels when
12743 * needed.
12744 */
12745 static int
12746mkdir_recurse(dir, prot)
12747 char_u *dir;
12748 int prot;
12749{
12750 char_u *p;
12751 char_u *updir;
12752 int r = FAIL;
12753
12754 /* Get end of directory name in "dir".
12755 * We're done when it's "/" or "c:/". */
12756 p = gettail_sep(dir);
12757 if (p <= get_past_head(dir))
12758 return OK;
12759
12760 /* If the directory exists we're done. Otherwise: create it.*/
12761 updir = vim_strnsave(dir, (int)(p - dir));
12762 if (updir == NULL)
12763 return FAIL;
12764 if (mch_isdir(updir))
12765 r = OK;
12766 else if (mkdir_recurse(updir, prot) == OK)
12767 r = vim_mkdir_emsg(updir, prot);
12768 vim_free(updir);
12769 return r;
12770}
12771
12772#ifdef vim_mkdir
12773/*
12774 * "mkdir()" function
12775 */
12776 static void
12777f_mkdir(argvars, rettv)
12778 typval_T *argvars;
12779 typval_T *rettv;
12780{
12781 char_u *dir;
12782 char_u buf[NUMBUFLEN];
12783 int prot = 0755;
12784
12785 rettv->vval.v_number = FAIL;
12786 if (check_restricted() || check_secure())
12787 return;
12788
12789 dir = get_tv_string_buf(&argvars[0], buf);
12790 if (argvars[1].v_type != VAR_UNKNOWN)
12791 {
12792 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012793 prot = get_tv_number_chk(&argvars[2], NULL);
12794 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012795 mkdir_recurse(dir, prot);
12796 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012797 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012798}
12799#endif
12800
Bram Moolenaar0d660222005-01-07 21:51:51 +000012801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802 * "mode()" function
12803 */
12804/*ARGSUSED*/
12805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012806f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012807 typval_T *argvars;
12808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809{
12810 char_u buf[2];
12811
12812#ifdef FEAT_VISUAL
12813 if (VIsual_active)
12814 {
12815 if (VIsual_select)
12816 buf[0] = VIsual_mode + 's' - 'v';
12817 else
12818 buf[0] = VIsual_mode;
12819 }
12820 else
12821#endif
12822 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12823 buf[0] = 'r';
12824 else if (State & INSERT)
12825 {
12826 if (State & REPLACE_FLAG)
12827 buf[0] = 'R';
12828 else
12829 buf[0] = 'i';
12830 }
12831 else if (State & CMDLINE)
12832 buf[0] = 'c';
12833 else
12834 buf[0] = 'n';
12835
12836 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 rettv->vval.v_string = vim_strsave(buf);
12838 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839}
12840
12841/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012842 * "nextnonblank()" function
12843 */
12844 static void
12845f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012846 typval_T *argvars;
12847 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012848{
12849 linenr_T lnum;
12850
12851 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012853 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012854 {
12855 lnum = 0;
12856 break;
12857 }
12858 if (*skipwhite(ml_get(lnum)) != NUL)
12859 break;
12860 }
12861 rettv->vval.v_number = lnum;
12862}
12863
12864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 * "nr2char()" function
12866 */
12867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012869 typval_T *argvars;
12870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871{
12872 char_u buf[NUMBUFLEN];
12873
12874#ifdef FEAT_MBYTE
12875 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012876 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 else
12878#endif
12879 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012880 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881 buf[1] = NUL;
12882 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 rettv->v_type = VAR_STRING;
12884 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012885}
12886
12887/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012888 * "pathshorten()" function
12889 */
12890 static void
12891f_pathshorten(argvars, rettv)
12892 typval_T *argvars;
12893 typval_T *rettv;
12894{
12895 char_u *p;
12896
12897 rettv->v_type = VAR_STRING;
12898 p = get_tv_string_chk(&argvars[0]);
12899 if (p == NULL)
12900 rettv->vval.v_string = NULL;
12901 else
12902 {
12903 p = vim_strsave(p);
12904 rettv->vval.v_string = p;
12905 if (p != NULL)
12906 shorten_dir(p);
12907 }
12908}
12909
12910/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012911 * "prevnonblank()" function
12912 */
12913 static void
12914f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012915 typval_T *argvars;
12916 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012917{
12918 linenr_T lnum;
12919
12920 lnum = get_tv_lnum(argvars);
12921 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12922 lnum = 0;
12923 else
12924 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12925 --lnum;
12926 rettv->vval.v_number = lnum;
12927}
12928
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012929#ifdef HAVE_STDARG_H
12930/* This dummy va_list is here because:
12931 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12932 * - locally in the function results in a "used before set" warning
12933 * - using va_start() to initialize it gives "function with fixed args" error */
12934static va_list ap;
12935#endif
12936
Bram Moolenaar8c711452005-01-14 21:53:12 +000012937/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012938 * "printf()" function
12939 */
12940 static void
12941f_printf(argvars, rettv)
12942 typval_T *argvars;
12943 typval_T *rettv;
12944{
12945 rettv->v_type = VAR_STRING;
12946 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012947#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012948 {
12949 char_u buf[NUMBUFLEN];
12950 int len;
12951 char_u *s;
12952 int saved_did_emsg = did_emsg;
12953 char *fmt;
12954
12955 /* Get the required length, allocate the buffer and do it for real. */
12956 did_emsg = FALSE;
12957 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012958 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012959 if (!did_emsg)
12960 {
12961 s = alloc(len + 1);
12962 if (s != NULL)
12963 {
12964 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012965 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012966 }
12967 }
12968 did_emsg |= saved_did_emsg;
12969 }
12970#endif
12971}
12972
12973/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012974 * "pumvisible()" function
12975 */
12976/*ARGSUSED*/
12977 static void
12978f_pumvisible(argvars, rettv)
12979 typval_T *argvars;
12980 typval_T *rettv;
12981{
12982 rettv->vval.v_number = 0;
12983#ifdef FEAT_INS_EXPAND
12984 if (pum_visible())
12985 rettv->vval.v_number = 1;
12986#endif
12987}
12988
12989/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012990 * "range()" function
12991 */
12992 static void
12993f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012994 typval_T *argvars;
12995 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012996{
12997 long start;
12998 long end;
12999 long stride = 1;
13000 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013001 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013003 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013004 if (argvars[1].v_type == VAR_UNKNOWN)
13005 {
13006 end = start - 1;
13007 start = 0;
13008 }
13009 else
13010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013011 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013012 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013013 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013014 }
13015
13016 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013017 if (error)
13018 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013019 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013020 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013021 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013022 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013023 else
13024 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013025 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013026 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013027 if (list_append_number(rettv->vval.v_list,
13028 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013029 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013030 }
13031}
13032
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013033/*
13034 * "readfile()" function
13035 */
13036 static void
13037f_readfile(argvars, rettv)
13038 typval_T *argvars;
13039 typval_T *rettv;
13040{
13041 int binary = FALSE;
13042 char_u *fname;
13043 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013044 listitem_T *li;
13045#define FREAD_SIZE 200 /* optimized for text lines */
13046 char_u buf[FREAD_SIZE];
13047 int readlen; /* size of last fread() */
13048 int buflen; /* nr of valid chars in buf[] */
13049 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13050 int tolist; /* first byte in buf[] still to be put in list */
13051 int chop; /* how many CR to chop off */
13052 char_u *prev = NULL; /* previously read bytes, if any */
13053 int prevlen = 0; /* length of "prev" if not NULL */
13054 char_u *s;
13055 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013056 long maxline = MAXLNUM;
13057 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013058
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013059 if (argvars[1].v_type != VAR_UNKNOWN)
13060 {
13061 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13062 binary = TRUE;
13063 if (argvars[2].v_type != VAR_UNKNOWN)
13064 maxline = get_tv_number(&argvars[2]);
13065 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013066
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013067 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013068 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013069
13070 /* Always open the file in binary mode, library functions have a mind of
13071 * their own about CR-LF conversion. */
13072 fname = get_tv_string(&argvars[0]);
13073 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13074 {
13075 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13076 return;
13077 }
13078
13079 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013080 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013081 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013082 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013083 buflen = filtd + readlen;
13084 tolist = 0;
13085 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13086 {
13087 if (buf[filtd] == '\n' || readlen <= 0)
13088 {
13089 /* Only when in binary mode add an empty list item when the
13090 * last line ends in a '\n'. */
13091 if (!binary && readlen == 0 && filtd == 0)
13092 break;
13093
13094 /* Found end-of-line or end-of-file: add a text line to the
13095 * list. */
13096 chop = 0;
13097 if (!binary)
13098 while (filtd - chop - 1 >= tolist
13099 && buf[filtd - chop - 1] == '\r')
13100 ++chop;
13101 len = filtd - tolist - chop;
13102 if (prev == NULL)
13103 s = vim_strnsave(buf + tolist, len);
13104 else
13105 {
13106 s = alloc((unsigned)(prevlen + len + 1));
13107 if (s != NULL)
13108 {
13109 mch_memmove(s, prev, prevlen);
13110 vim_free(prev);
13111 prev = NULL;
13112 mch_memmove(s + prevlen, buf + tolist, len);
13113 s[prevlen + len] = NUL;
13114 }
13115 }
13116 tolist = filtd + 1;
13117
13118 li = listitem_alloc();
13119 if (li == NULL)
13120 {
13121 vim_free(s);
13122 break;
13123 }
13124 li->li_tv.v_type = VAR_STRING;
13125 li->li_tv.v_lock = 0;
13126 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013127 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013128
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013129 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013130 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013131 if (readlen <= 0)
13132 break;
13133 }
13134 else if (buf[filtd] == NUL)
13135 buf[filtd] = '\n';
13136 }
13137 if (readlen <= 0)
13138 break;
13139
13140 if (tolist == 0)
13141 {
13142 /* "buf" is full, need to move text to an allocated buffer */
13143 if (prev == NULL)
13144 {
13145 prev = vim_strnsave(buf, buflen);
13146 prevlen = buflen;
13147 }
13148 else
13149 {
13150 s = alloc((unsigned)(prevlen + buflen));
13151 if (s != NULL)
13152 {
13153 mch_memmove(s, prev, prevlen);
13154 mch_memmove(s + prevlen, buf, buflen);
13155 vim_free(prev);
13156 prev = s;
13157 prevlen += buflen;
13158 }
13159 }
13160 filtd = 0;
13161 }
13162 else
13163 {
13164 mch_memmove(buf, buf + tolist, buflen - tolist);
13165 filtd -= tolist;
13166 }
13167 }
13168
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013169 /*
13170 * For a negative line count use only the lines at the end of the file,
13171 * free the rest.
13172 */
13173 if (maxline < 0)
13174 while (cnt > -maxline)
13175 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013176 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013177 --cnt;
13178 }
13179
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013180 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013181 fclose(fd);
13182}
13183
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013184#if defined(FEAT_RELTIME)
13185static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13186
13187/*
13188 * Convert a List to proftime_T.
13189 * Return FAIL when there is something wrong.
13190 */
13191 static int
13192list2proftime(arg, tm)
13193 typval_T *arg;
13194 proftime_T *tm;
13195{
13196 long n1, n2;
13197 int error = FALSE;
13198
13199 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13200 || arg->vval.v_list->lv_len != 2)
13201 return FAIL;
13202 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13203 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13204# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013205 tm->HighPart = n1;
13206 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013207# else
13208 tm->tv_sec = n1;
13209 tm->tv_usec = n2;
13210# endif
13211 return error ? FAIL : OK;
13212}
13213#endif /* FEAT_RELTIME */
13214
13215/*
13216 * "reltime()" function
13217 */
13218 static void
13219f_reltime(argvars, rettv)
13220 typval_T *argvars;
13221 typval_T *rettv;
13222{
13223#ifdef FEAT_RELTIME
13224 proftime_T res;
13225 proftime_T start;
13226
13227 if (argvars[0].v_type == VAR_UNKNOWN)
13228 {
13229 /* No arguments: get current time. */
13230 profile_start(&res);
13231 }
13232 else if (argvars[1].v_type == VAR_UNKNOWN)
13233 {
13234 if (list2proftime(&argvars[0], &res) == FAIL)
13235 return;
13236 profile_end(&res);
13237 }
13238 else
13239 {
13240 /* Two arguments: compute the difference. */
13241 if (list2proftime(&argvars[0], &start) == FAIL
13242 || list2proftime(&argvars[1], &res) == FAIL)
13243 return;
13244 profile_sub(&res, &start);
13245 }
13246
13247 if (rettv_list_alloc(rettv) == OK)
13248 {
13249 long n1, n2;
13250
13251# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013252 n1 = res.HighPart;
13253 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013254# else
13255 n1 = res.tv_sec;
13256 n2 = res.tv_usec;
13257# endif
13258 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13259 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13260 }
13261#endif
13262}
13263
13264/*
13265 * "reltimestr()" function
13266 */
13267 static void
13268f_reltimestr(argvars, rettv)
13269 typval_T *argvars;
13270 typval_T *rettv;
13271{
13272#ifdef FEAT_RELTIME
13273 proftime_T tm;
13274#endif
13275
13276 rettv->v_type = VAR_STRING;
13277 rettv->vval.v_string = NULL;
13278#ifdef FEAT_RELTIME
13279 if (list2proftime(&argvars[0], &tm) == OK)
13280 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13281#endif
13282}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013283
Bram Moolenaar0d660222005-01-07 21:51:51 +000013284#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13285static void make_connection __ARGS((void));
13286static int check_connection __ARGS((void));
13287
13288 static void
13289make_connection()
13290{
13291 if (X_DISPLAY == NULL
13292# ifdef FEAT_GUI
13293 && !gui.in_use
13294# endif
13295 )
13296 {
13297 x_force_connect = TRUE;
13298 setup_term_clip();
13299 x_force_connect = FALSE;
13300 }
13301}
13302
13303 static int
13304check_connection()
13305{
13306 make_connection();
13307 if (X_DISPLAY == NULL)
13308 {
13309 EMSG(_("E240: No connection to Vim server"));
13310 return FAIL;
13311 }
13312 return OK;
13313}
13314#endif
13315
13316#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013317static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013318
13319 static void
13320remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013321 typval_T *argvars;
13322 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013323 int expr;
13324{
13325 char_u *server_name;
13326 char_u *keys;
13327 char_u *r = NULL;
13328 char_u buf[NUMBUFLEN];
13329# ifdef WIN32
13330 HWND w;
13331# else
13332 Window w;
13333# endif
13334
13335 if (check_restricted() || check_secure())
13336 return;
13337
13338# ifdef FEAT_X11
13339 if (check_connection() == FAIL)
13340 return;
13341# endif
13342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013343 server_name = get_tv_string_chk(&argvars[0]);
13344 if (server_name == NULL)
13345 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013346 keys = get_tv_string_buf(&argvars[1], buf);
13347# ifdef WIN32
13348 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13349# else
13350 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13351 < 0)
13352# endif
13353 {
13354 if (r != NULL)
13355 EMSG(r); /* sending worked but evaluation failed */
13356 else
13357 EMSG2(_("E241: Unable to send to %s"), server_name);
13358 return;
13359 }
13360
13361 rettv->vval.v_string = r;
13362
13363 if (argvars[2].v_type != VAR_UNKNOWN)
13364 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013365 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013366 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013367 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013368
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013369 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013370 v.di_tv.v_type = VAR_STRING;
13371 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013372 idvar = get_tv_string_chk(&argvars[2]);
13373 if (idvar != NULL)
13374 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013375 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013376 }
13377}
13378#endif
13379
13380/*
13381 * "remote_expr()" function
13382 */
13383/*ARGSUSED*/
13384 static void
13385f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013386 typval_T *argvars;
13387 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013388{
13389 rettv->v_type = VAR_STRING;
13390 rettv->vval.v_string = NULL;
13391#ifdef FEAT_CLIENTSERVER
13392 remote_common(argvars, rettv, TRUE);
13393#endif
13394}
13395
13396/*
13397 * "remote_foreground()" function
13398 */
13399/*ARGSUSED*/
13400 static void
13401f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013402 typval_T *argvars;
13403 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013404{
13405 rettv->vval.v_number = 0;
13406#ifdef FEAT_CLIENTSERVER
13407# ifdef WIN32
13408 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013409 {
13410 char_u *server_name = get_tv_string_chk(&argvars[0]);
13411
13412 if (server_name != NULL)
13413 serverForeground(server_name);
13414 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013415# else
13416 /* Send a foreground() expression to the server. */
13417 argvars[1].v_type = VAR_STRING;
13418 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13419 argvars[2].v_type = VAR_UNKNOWN;
13420 remote_common(argvars, rettv, TRUE);
13421 vim_free(argvars[1].vval.v_string);
13422# endif
13423#endif
13424}
13425
13426/*ARGSUSED*/
13427 static void
13428f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013429 typval_T *argvars;
13430 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013431{
13432#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013433 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013434 char_u *s = NULL;
13435# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013436 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013437# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013438 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013439
13440 if (check_restricted() || check_secure())
13441 {
13442 rettv->vval.v_number = -1;
13443 return;
13444 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013445 serverid = get_tv_string_chk(&argvars[0]);
13446 if (serverid == NULL)
13447 {
13448 rettv->vval.v_number = -1;
13449 return; /* type error; errmsg already given */
13450 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013451# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013452 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013453 if (n == 0)
13454 rettv->vval.v_number = -1;
13455 else
13456 {
13457 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13458 rettv->vval.v_number = (s != NULL);
13459 }
13460# else
13461 rettv->vval.v_number = 0;
13462 if (check_connection() == FAIL)
13463 return;
13464
13465 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013466 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013467# endif
13468
13469 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013471 char_u *retvar;
13472
Bram Moolenaar33570922005-01-25 22:26:29 +000013473 v.di_tv.v_type = VAR_STRING;
13474 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013475 retvar = get_tv_string_chk(&argvars[1]);
13476 if (retvar != NULL)
13477 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013478 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013479 }
13480#else
13481 rettv->vval.v_number = -1;
13482#endif
13483}
13484
13485/*ARGSUSED*/
13486 static void
13487f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013488 typval_T *argvars;
13489 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013490{
13491 char_u *r = NULL;
13492
13493#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013494 char_u *serverid = get_tv_string_chk(&argvars[0]);
13495
13496 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013497 {
13498# ifdef WIN32
13499 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013500 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013501
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013502 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013503 if (n != 0)
13504 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13505 if (r == NULL)
13506# else
13507 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013508 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013509# endif
13510 EMSG(_("E277: Unable to read a server reply"));
13511 }
13512#endif
13513 rettv->v_type = VAR_STRING;
13514 rettv->vval.v_string = r;
13515}
13516
13517/*
13518 * "remote_send()" function
13519 */
13520/*ARGSUSED*/
13521 static void
13522f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013523 typval_T *argvars;
13524 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013525{
13526 rettv->v_type = VAR_STRING;
13527 rettv->vval.v_string = NULL;
13528#ifdef FEAT_CLIENTSERVER
13529 remote_common(argvars, rettv, FALSE);
13530#endif
13531}
13532
13533/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013534 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013535 */
13536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013537f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013538 typval_T *argvars;
13539 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013540{
Bram Moolenaar33570922005-01-25 22:26:29 +000013541 list_T *l;
13542 listitem_T *item, *item2;
13543 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013544 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013545 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013546 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013547 dict_T *d;
13548 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013549
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013550 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013551 if (argvars[0].v_type == VAR_DICT)
13552 {
13553 if (argvars[2].v_type != VAR_UNKNOWN)
13554 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013555 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013556 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013558 key = get_tv_string_chk(&argvars[1]);
13559 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013561 di = dict_find(d, key, -1);
13562 if (di == NULL)
13563 EMSG2(_(e_dictkey), key);
13564 else
13565 {
13566 *rettv = di->di_tv;
13567 init_tv(&di->di_tv);
13568 dictitem_remove(d, di);
13569 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013570 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013571 }
13572 }
13573 else if (argvars[0].v_type != VAR_LIST)
13574 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013575 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013576 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013577 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013578 int error = FALSE;
13579
13580 idx = get_tv_number_chk(&argvars[1], &error);
13581 if (error)
13582 ; /* type error: do nothing, errmsg already given */
13583 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013584 EMSGN(_(e_listidx), idx);
13585 else
13586 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013587 if (argvars[2].v_type == VAR_UNKNOWN)
13588 {
13589 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013590 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013591 *rettv = item->li_tv;
13592 vim_free(item);
13593 }
13594 else
13595 {
13596 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013597 end = get_tv_number_chk(&argvars[2], &error);
13598 if (error)
13599 ; /* type error: do nothing */
13600 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013601 EMSGN(_(e_listidx), end);
13602 else
13603 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013604 int cnt = 0;
13605
13606 for (li = item; li != NULL; li = li->li_next)
13607 {
13608 ++cnt;
13609 if (li == item2)
13610 break;
13611 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013612 if (li == NULL) /* didn't find "item2" after "item" */
13613 EMSG(_(e_invrange));
13614 else
13615 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013616 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013617 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013618 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013619 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013620 l->lv_first = item;
13621 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013622 item->li_prev = NULL;
13623 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013624 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013625 }
13626 }
13627 }
13628 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013629 }
13630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631}
13632
13633/*
13634 * "rename({from}, {to})" function
13635 */
13636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013638 typval_T *argvars;
13639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640{
13641 char_u buf[NUMBUFLEN];
13642
13643 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13647 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648}
13649
13650/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013651 * "repeat()" function
13652 */
13653/*ARGSUSED*/
13654 static void
13655f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013656 typval_T *argvars;
13657 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013658{
13659 char_u *p;
13660 int n;
13661 int slen;
13662 int len;
13663 char_u *r;
13664 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013665
13666 n = get_tv_number(&argvars[1]);
13667 if (argvars[0].v_type == VAR_LIST)
13668 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013669 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013670 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013671 if (list_extend(rettv->vval.v_list,
13672 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013673 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013674 }
13675 else
13676 {
13677 p = get_tv_string(&argvars[0]);
13678 rettv->v_type = VAR_STRING;
13679 rettv->vval.v_string = NULL;
13680
13681 slen = (int)STRLEN(p);
13682 len = slen * n;
13683 if (len <= 0)
13684 return;
13685
13686 r = alloc(len + 1);
13687 if (r != NULL)
13688 {
13689 for (i = 0; i < n; i++)
13690 mch_memmove(r + i * slen, p, (size_t)slen);
13691 r[len] = NUL;
13692 }
13693
13694 rettv->vval.v_string = r;
13695 }
13696}
13697
13698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699 * "resolve()" function
13700 */
13701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013702f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013703 typval_T *argvars;
13704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705{
13706 char_u *p;
13707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013708 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709#ifdef FEAT_SHORTCUT
13710 {
13711 char_u *v = NULL;
13712
13713 v = mch_resolve_shortcut(p);
13714 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013715 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013717 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 }
13719#else
13720# ifdef HAVE_READLINK
13721 {
13722 char_u buf[MAXPATHL + 1];
13723 char_u *cpy;
13724 int len;
13725 char_u *remain = NULL;
13726 char_u *q;
13727 int is_relative_to_current = FALSE;
13728 int has_trailing_pathsep = FALSE;
13729 int limit = 100;
13730
13731 p = vim_strsave(p);
13732
13733 if (p[0] == '.' && (vim_ispathsep(p[1])
13734 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13735 is_relative_to_current = TRUE;
13736
13737 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013738 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739 has_trailing_pathsep = TRUE;
13740
13741 q = getnextcomp(p);
13742 if (*q != NUL)
13743 {
13744 /* Separate the first path component in "p", and keep the
13745 * remainder (beginning with the path separator). */
13746 remain = vim_strsave(q - 1);
13747 q[-1] = NUL;
13748 }
13749
13750 for (;;)
13751 {
13752 for (;;)
13753 {
13754 len = readlink((char *)p, (char *)buf, MAXPATHL);
13755 if (len <= 0)
13756 break;
13757 buf[len] = NUL;
13758
13759 if (limit-- == 0)
13760 {
13761 vim_free(p);
13762 vim_free(remain);
13763 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013764 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765 goto fail;
13766 }
13767
13768 /* Ensure that the result will have a trailing path separator
13769 * if the argument has one. */
13770 if (remain == NULL && has_trailing_pathsep)
13771 add_pathsep(buf);
13772
13773 /* Separate the first path component in the link value and
13774 * concatenate the remainders. */
13775 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13776 if (*q != NUL)
13777 {
13778 if (remain == NULL)
13779 remain = vim_strsave(q - 1);
13780 else
13781 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013782 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783 if (cpy != NULL)
13784 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 vim_free(remain);
13786 remain = cpy;
13787 }
13788 }
13789 q[-1] = NUL;
13790 }
13791
13792 q = gettail(p);
13793 if (q > p && *q == NUL)
13794 {
13795 /* Ignore trailing path separator. */
13796 q[-1] = NUL;
13797 q = gettail(p);
13798 }
13799 if (q > p && !mch_isFullName(buf))
13800 {
13801 /* symlink is relative to directory of argument */
13802 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13803 if (cpy != NULL)
13804 {
13805 STRCPY(cpy, p);
13806 STRCPY(gettail(cpy), buf);
13807 vim_free(p);
13808 p = cpy;
13809 }
13810 }
13811 else
13812 {
13813 vim_free(p);
13814 p = vim_strsave(buf);
13815 }
13816 }
13817
13818 if (remain == NULL)
13819 break;
13820
13821 /* Append the first path component of "remain" to "p". */
13822 q = getnextcomp(remain + 1);
13823 len = q - remain - (*q != NUL);
13824 cpy = vim_strnsave(p, STRLEN(p) + len);
13825 if (cpy != NULL)
13826 {
13827 STRNCAT(cpy, remain, len);
13828 vim_free(p);
13829 p = cpy;
13830 }
13831 /* Shorten "remain". */
13832 if (*q != NUL)
Bram Moolenaar452a81b2007-08-06 20:28:43 +000013833 mch_memmove(remain, q - 1, STRLEN(q - 1) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 else
13835 {
13836 vim_free(remain);
13837 remain = NULL;
13838 }
13839 }
13840
13841 /* If the result is a relative path name, make it explicitly relative to
13842 * the current directory if and only if the argument had this form. */
13843 if (!vim_ispathsep(*p))
13844 {
13845 if (is_relative_to_current
13846 && *p != NUL
13847 && !(p[0] == '.'
13848 && (p[1] == NUL
13849 || vim_ispathsep(p[1])
13850 || (p[1] == '.'
13851 && (p[2] == NUL
13852 || vim_ispathsep(p[2]))))))
13853 {
13854 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013855 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856 if (cpy != NULL)
13857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858 vim_free(p);
13859 p = cpy;
13860 }
13861 }
13862 else if (!is_relative_to_current)
13863 {
13864 /* Strip leading "./". */
13865 q = p;
13866 while (q[0] == '.' && vim_ispathsep(q[1]))
13867 q += 2;
13868 if (q > p)
13869 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13870 }
13871 }
13872
13873 /* Ensure that the result will have no trailing path separator
13874 * if the argument had none. But keep "/" or "//". */
13875 if (!has_trailing_pathsep)
13876 {
13877 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013878 if (after_pathsep(p, q))
13879 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 }
13881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013882 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883 }
13884# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013885 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886# endif
13887#endif
13888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013889 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890
13891#ifdef HAVE_READLINK
13892fail:
13893#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013894 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895}
13896
13897/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013898 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 */
13900 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013901f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013902 typval_T *argvars;
13903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904{
Bram Moolenaar33570922005-01-25 22:26:29 +000013905 list_T *l;
13906 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907
Bram Moolenaar0d660222005-01-07 21:51:51 +000013908 rettv->vval.v_number = 0;
13909 if (argvars[0].v_type != VAR_LIST)
13910 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013911 else if ((l = argvars[0].vval.v_list) != NULL
13912 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013913 {
13914 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013915 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013916 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013917 while (li != NULL)
13918 {
13919 ni = li->li_prev;
13920 list_append(l, li);
13921 li = ni;
13922 }
13923 rettv->vval.v_list = l;
13924 rettv->v_type = VAR_LIST;
13925 ++l->lv_refcount;
13926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927}
13928
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013929#define SP_NOMOVE 0x01 /* don't move cursor */
13930#define SP_REPEAT 0x02 /* repeat to find outer pair */
13931#define SP_RETCOUNT 0x04 /* return matchcount */
13932#define SP_SETPCMARK 0x08 /* set previous context mark */
13933#define SP_START 0x10 /* accept match at start position */
13934#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13935#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013936
Bram Moolenaar33570922005-01-25 22:26:29 +000013937static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013938
13939/*
13940 * Get flags for a search function.
13941 * Possibly sets "p_ws".
13942 * Returns BACKWARD, FORWARD or zero (for an error).
13943 */
13944 static int
13945get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013946 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013947 int *flagsp;
13948{
13949 int dir = FORWARD;
13950 char_u *flags;
13951 char_u nbuf[NUMBUFLEN];
13952 int mask;
13953
13954 if (varp->v_type != VAR_UNKNOWN)
13955 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013956 flags = get_tv_string_buf_chk(varp, nbuf);
13957 if (flags == NULL)
13958 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013959 while (*flags != NUL)
13960 {
13961 switch (*flags)
13962 {
13963 case 'b': dir = BACKWARD; break;
13964 case 'w': p_ws = TRUE; break;
13965 case 'W': p_ws = FALSE; break;
13966 default: mask = 0;
13967 if (flagsp != NULL)
13968 switch (*flags)
13969 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013970 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013971 case 'e': mask = SP_END; break;
13972 case 'm': mask = SP_RETCOUNT; break;
13973 case 'n': mask = SP_NOMOVE; break;
13974 case 'p': mask = SP_SUBPAT; break;
13975 case 'r': mask = SP_REPEAT; break;
13976 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013977 }
13978 if (mask == 0)
13979 {
13980 EMSG2(_(e_invarg2), flags);
13981 dir = 0;
13982 }
13983 else
13984 *flagsp |= mask;
13985 }
13986 if (dir == 0)
13987 break;
13988 ++flags;
13989 }
13990 }
13991 return dir;
13992}
13993
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013995 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013997 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013998search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013999 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014000 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014001 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014003 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004 char_u *pat;
14005 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014006 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 int save_p_ws = p_ws;
14008 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014009 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014010 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014011 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014012 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014014 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014015 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014016 if (dir == 0)
14017 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014018 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014019 if (flags & SP_START)
14020 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014021 if (flags & SP_END)
14022 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014023
14024 /* Optional extra argument: line number to stop searching. */
14025 if (argvars[1].v_type != VAR_UNKNOWN
14026 && argvars[2].v_type != VAR_UNKNOWN)
14027 {
14028 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14029 if (lnum_stop < 0)
14030 goto theend;
14031 }
14032
Bram Moolenaar231334e2005-07-25 20:46:57 +000014033 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014034 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014035 * Check to make sure only those flags are set.
14036 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14037 * flags cannot be set. Check for that condition also.
14038 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014039 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014040 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014042 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014043 goto theend;
14044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014046 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014047 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
14048 options, RE_SEARCH, (linenr_T)lnum_stop);
14049 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014051 if (flags & SP_SUBPAT)
14052 retval = subpatnum;
14053 else
14054 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014055 if (flags & SP_SETPCMARK)
14056 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014058 if (match_pos != NULL)
14059 {
14060 /* Store the match cursor position */
14061 match_pos->lnum = pos.lnum;
14062 match_pos->col = pos.col + 1;
14063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064 /* "/$" will put the cursor after the end of the line, may need to
14065 * correct that here */
14066 check_cursor();
14067 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014068
14069 /* If 'n' flag is used: restore cursor position. */
14070 if (flags & SP_NOMOVE)
14071 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014072 else
14073 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014074theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014076
14077 return retval;
14078}
14079
14080/*
14081 * "search()" function
14082 */
14083 static void
14084f_search(argvars, rettv)
14085 typval_T *argvars;
14086 typval_T *rettv;
14087{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014088 int flags = 0;
14089
14090 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091}
14092
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014094 * "searchdecl()" function
14095 */
14096 static void
14097f_searchdecl(argvars, rettv)
14098 typval_T *argvars;
14099 typval_T *rettv;
14100{
14101 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014102 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014103 int error = FALSE;
14104 char_u *name;
14105
14106 rettv->vval.v_number = 1; /* default: FAIL */
14107
14108 name = get_tv_string_chk(&argvars[0]);
14109 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014110 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014111 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014112 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14113 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14114 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014115 if (!error && name != NULL)
14116 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014117 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014118}
14119
14120/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014121 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014123 static int
14124searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014125 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014126 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127{
14128 char_u *spat, *mpat, *epat;
14129 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131 int dir;
14132 int flags = 0;
14133 char_u nbuf1[NUMBUFLEN];
14134 char_u nbuf2[NUMBUFLEN];
14135 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014136 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014137 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014140 spat = get_tv_string_chk(&argvars[0]);
14141 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14142 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14143 if (spat == NULL || mpat == NULL || epat == NULL)
14144 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 /* Handle the optional fourth argument: flags */
14147 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014148 if (dir == 0)
14149 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014150
14151 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014152 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14153 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014154 if ((flags & (SP_END | SP_SUBPAT)) != 0
14155 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014156 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014157 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014158 goto theend;
14159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014161 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014162 if (argvars[3].v_type == VAR_UNKNOWN
14163 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164 skip = (char_u *)"";
14165 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014167 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014168 if (argvars[5].v_type != VAR_UNKNOWN)
14169 {
14170 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14171 if (lnum_stop < 0)
14172 goto theend;
14173 }
14174 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 if (skip == NULL)
14176 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014178 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
14179 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014180
14181theend:
14182 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014183
14184 return retval;
14185}
14186
14187/*
14188 * "searchpair()" function
14189 */
14190 static void
14191f_searchpair(argvars, rettv)
14192 typval_T *argvars;
14193 typval_T *rettv;
14194{
14195 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14196}
14197
14198/*
14199 * "searchpairpos()" function
14200 */
14201 static void
14202f_searchpairpos(argvars, rettv)
14203 typval_T *argvars;
14204 typval_T *rettv;
14205{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014206 pos_T match_pos;
14207 int lnum = 0;
14208 int col = 0;
14209
14210 rettv->vval.v_number = 0;
14211
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014212 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014213 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014214
14215 if (searchpair_cmn(argvars, &match_pos) > 0)
14216 {
14217 lnum = match_pos.lnum;
14218 col = match_pos.col;
14219 }
14220
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014221 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14222 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014223}
14224
14225/*
14226 * Search for a start/middle/end thing.
14227 * Used by searchpair(), see its documentation for the details.
14228 * Returns 0 or -1 for no match,
14229 */
14230 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014231do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014232 char_u *spat; /* start pattern */
14233 char_u *mpat; /* middle pattern */
14234 char_u *epat; /* end pattern */
14235 int dir; /* BACKWARD or FORWARD */
14236 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014237 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014238 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014239 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014240{
14241 char_u *save_cpo;
14242 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14243 long retval = 0;
14244 pos_T pos;
14245 pos_T firstpos;
14246 pos_T foundpos;
14247 pos_T save_cursor;
14248 pos_T save_pos;
14249 int n;
14250 int r;
14251 int nest = 1;
14252 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014253 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014254
14255 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14256 save_cpo = p_cpo;
14257 p_cpo = (char_u *)"";
14258
14259 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14260 * start/middle/end (pat3, for the top pair). */
14261 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14262 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14263 if (pat2 == NULL || pat3 == NULL)
14264 goto theend;
14265 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14266 if (*mpat == NUL)
14267 STRCPY(pat3, pat2);
14268 else
14269 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14270 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014271 if (flags & SP_START)
14272 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014273
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 save_cursor = curwin->w_cursor;
14275 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014276 clearpos(&firstpos);
14277 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 pat = pat3;
14279 for (;;)
14280 {
14281 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014282 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14284 /* didn't find it or found the first match again: FAIL */
14285 break;
14286
14287 if (firstpos.lnum == 0)
14288 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014289 if (equalpos(pos, foundpos))
14290 {
14291 /* Found the same position again. Can happen with a pattern that
14292 * has "\zs" at the end and searching backwards. Advance one
14293 * character and try again. */
14294 if (dir == BACKWARD)
14295 decl(&pos);
14296 else
14297 incl(&pos);
14298 }
14299 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300
14301 /* If the skip pattern matches, ignore this match. */
14302 if (*skip != NUL)
14303 {
14304 save_pos = curwin->w_cursor;
14305 curwin->w_cursor = pos;
14306 r = eval_to_bool(skip, &err, NULL, FALSE);
14307 curwin->w_cursor = save_pos;
14308 if (err)
14309 {
14310 /* Evaluating {skip} caused an error, break here. */
14311 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014312 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313 break;
14314 }
14315 if (r)
14316 continue;
14317 }
14318
14319 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14320 {
14321 /* Found end when searching backwards or start when searching
14322 * forward: nested pair. */
14323 ++nest;
14324 pat = pat2; /* nested, don't search for middle */
14325 }
14326 else
14327 {
14328 /* Found end when searching forward or start when searching
14329 * backward: end of (nested) pair; or found middle in outer pair. */
14330 if (--nest == 1)
14331 pat = pat3; /* outer level, search for middle */
14332 }
14333
14334 if (nest == 0)
14335 {
14336 /* Found the match: return matchcount or line number. */
14337 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014338 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014340 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014341 if (flags & SP_SETPCMARK)
14342 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343 curwin->w_cursor = pos;
14344 if (!(flags & SP_REPEAT))
14345 break;
14346 nest = 1; /* search for next unmatched */
14347 }
14348 }
14349
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014350 if (match_pos != NULL)
14351 {
14352 /* Store the match cursor position */
14353 match_pos->lnum = curwin->w_cursor.lnum;
14354 match_pos->col = curwin->w_cursor.col + 1;
14355 }
14356
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014358 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 curwin->w_cursor = save_cursor;
14360
14361theend:
14362 vim_free(pat2);
14363 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014365
14366 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367}
14368
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014369/*
14370 * "searchpos()" function
14371 */
14372 static void
14373f_searchpos(argvars, rettv)
14374 typval_T *argvars;
14375 typval_T *rettv;
14376{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014377 pos_T match_pos;
14378 int lnum = 0;
14379 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014380 int n;
14381 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014382
14383 rettv->vval.v_number = 0;
14384
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014385 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014386 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014387
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014388 n = search_cmn(argvars, &match_pos, &flags);
14389 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014390 {
14391 lnum = match_pos.lnum;
14392 col = match_pos.col;
14393 }
14394
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014395 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14396 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014397 if (flags & SP_SUBPAT)
14398 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014399}
14400
14401
Bram Moolenaar0d660222005-01-07 21:51:51 +000014402/*ARGSUSED*/
14403 static void
14404f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014405 typval_T *argvars;
14406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014408#ifdef FEAT_CLIENTSERVER
14409 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014410 char_u *server = get_tv_string_chk(&argvars[0]);
14411 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412
Bram Moolenaar0d660222005-01-07 21:51:51 +000014413 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014414 if (server == NULL || reply == NULL)
14415 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014416 if (check_restricted() || check_secure())
14417 return;
14418# ifdef FEAT_X11
14419 if (check_connection() == FAIL)
14420 return;
14421# endif
14422
14423 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014425 EMSG(_("E258: Unable to send to client"));
14426 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014428 rettv->vval.v_number = 0;
14429#else
14430 rettv->vval.v_number = -1;
14431#endif
14432}
14433
14434/*ARGSUSED*/
14435 static void
14436f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014437 typval_T *argvars;
14438 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014439{
14440 char_u *r = NULL;
14441
14442#ifdef FEAT_CLIENTSERVER
14443# ifdef WIN32
14444 r = serverGetVimNames();
14445# else
14446 make_connection();
14447 if (X_DISPLAY != NULL)
14448 r = serverGetVimNames(X_DISPLAY);
14449# endif
14450#endif
14451 rettv->v_type = VAR_STRING;
14452 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453}
14454
14455/*
14456 * "setbufvar()" function
14457 */
14458/*ARGSUSED*/
14459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014460f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014461 typval_T *argvars;
14462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463{
14464 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014467 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468 char_u nbuf[NUMBUFLEN];
14469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014470 rettv->vval.v_number = 0;
14471
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472 if (check_restricted() || check_secure())
14473 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014474 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14475 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014476 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 varp = &argvars[2];
14478
14479 if (buf != NULL && varname != NULL && varp != NULL)
14480 {
14481 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483
14484 if (*varname == '&')
14485 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014486 long numval;
14487 char_u *strval;
14488 int error = FALSE;
14489
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014491 numval = get_tv_number_chk(varp, &error);
14492 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014493 if (!error && strval != NULL)
14494 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495 }
14496 else
14497 {
14498 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14499 if (bufvarname != NULL)
14500 {
14501 STRCPY(bufvarname, "b:");
14502 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014503 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 vim_free(bufvarname);
14505 }
14506 }
14507
14508 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511}
14512
14513/*
14514 * "setcmdpos()" function
14515 */
14516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014517f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014518 typval_T *argvars;
14519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014521 int pos = (int)get_tv_number(&argvars[0]) - 1;
14522
14523 if (pos >= 0)
14524 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525}
14526
14527/*
14528 * "setline()" function
14529 */
14530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014531f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014532 typval_T *argvars;
14533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534{
14535 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014536 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014537 list_T *l = NULL;
14538 listitem_T *li = NULL;
14539 long added = 0;
14540 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014542 lnum = get_tv_lnum(&argvars[0]);
14543 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014545 l = argvars[1].vval.v_list;
14546 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014548 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014549 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014550
14551 rettv->vval.v_number = 0; /* OK */
14552 for (;;)
14553 {
14554 if (l != NULL)
14555 {
14556 /* list argument, get next string */
14557 if (li == NULL)
14558 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014559 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014560 li = li->li_next;
14561 }
14562
14563 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014564 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014565 break;
14566 if (lnum <= curbuf->b_ml.ml_line_count)
14567 {
14568 /* existing line, replace it */
14569 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14570 {
14571 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014572 if (lnum == curwin->w_cursor.lnum)
14573 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014574 rettv->vval.v_number = 0; /* OK */
14575 }
14576 }
14577 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14578 {
14579 /* lnum is one past the last line, append the line */
14580 ++added;
14581 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14582 rettv->vval.v_number = 0; /* OK */
14583 }
14584
14585 if (l == NULL) /* only one string argument */
14586 break;
14587 ++lnum;
14588 }
14589
14590 if (added > 0)
14591 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592}
14593
14594/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014595 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014596 */
14597/*ARGSUSED*/
14598 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014599set_qf_ll_list(wp, list_arg, action_arg, rettv)
14600 win_T *wp;
14601 typval_T *list_arg;
14602 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014603 typval_T *rettv;
14604{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014605#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014606 char_u *act;
14607 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014608#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014609
Bram Moolenaar2641f772005-03-25 21:58:17 +000014610 rettv->vval.v_number = -1;
14611
14612#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014613 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014614 EMSG(_(e_listreq));
14615 else
14616 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014617 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014618
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014619 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014620 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014621 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014622 if (act == NULL)
14623 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014624 if (*act == 'a' || *act == 'r')
14625 action = *act;
14626 }
14627
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014628 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014629 rettv->vval.v_number = 0;
14630 }
14631#endif
14632}
14633
14634/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014635 * "setloclist()" function
14636 */
14637/*ARGSUSED*/
14638 static void
14639f_setloclist(argvars, rettv)
14640 typval_T *argvars;
14641 typval_T *rettv;
14642{
14643 win_T *win;
14644
14645 rettv->vval.v_number = -1;
14646
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014647 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014648 if (win != NULL)
14649 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14650}
14651
14652/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014653 * "setmatches()" function
14654 */
14655 static void
14656f_setmatches(argvars, rettv)
14657 typval_T *argvars;
14658 typval_T *rettv;
14659{
14660#ifdef FEAT_SEARCH_EXTRA
14661 list_T *l;
14662 listitem_T *li;
14663 dict_T *d;
14664
14665 rettv->vval.v_number = -1;
14666 if (argvars[0].v_type != VAR_LIST)
14667 {
14668 EMSG(_(e_listreq));
14669 return;
14670 }
14671 if ((l = argvars[0].vval.v_list) != NULL)
14672 {
14673
14674 /* To some extent make sure that we are dealing with a list from
14675 * "getmatches()". */
14676 li = l->lv_first;
14677 while (li != NULL)
14678 {
14679 if (li->li_tv.v_type != VAR_DICT
14680 || (d = li->li_tv.vval.v_dict) == NULL)
14681 {
14682 EMSG(_(e_invarg));
14683 return;
14684 }
14685 if (!(dict_find(d, (char_u *)"group", -1) != NULL
14686 && dict_find(d, (char_u *)"pattern", -1) != NULL
14687 && dict_find(d, (char_u *)"priority", -1) != NULL
14688 && dict_find(d, (char_u *)"id", -1) != NULL))
14689 {
14690 EMSG(_(e_invarg));
14691 return;
14692 }
14693 li = li->li_next;
14694 }
14695
14696 clear_matches(curwin);
14697 li = l->lv_first;
14698 while (li != NULL)
14699 {
14700 d = li->li_tv.vval.v_dict;
14701 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
14702 get_dict_string(d, (char_u *)"pattern", FALSE),
14703 (int)get_dict_number(d, (char_u *)"priority"),
14704 (int)get_dict_number(d, (char_u *)"id"));
14705 li = li->li_next;
14706 }
14707 rettv->vval.v_number = 0;
14708 }
14709#endif
14710}
14711
14712/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014713 * "setpos()" function
14714 */
14715/*ARGSUSED*/
14716 static void
14717f_setpos(argvars, rettv)
14718 typval_T *argvars;
14719 typval_T *rettv;
14720{
14721 pos_T pos;
14722 int fnum;
14723 char_u *name;
14724
14725 name = get_tv_string_chk(argvars);
14726 if (name != NULL)
14727 {
14728 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14729 {
14730 --pos.col;
14731 if (name[0] == '.') /* cursor */
14732 {
14733 if (fnum == curbuf->b_fnum)
14734 {
14735 curwin->w_cursor = pos;
14736 check_cursor();
14737 }
14738 else
14739 EMSG(_(e_invarg));
14740 }
14741 else if (name[0] == '\'') /* mark */
14742 (void)setmark_pos(name[1], &pos, fnum);
14743 else
14744 EMSG(_(e_invarg));
14745 }
14746 }
14747}
14748
14749/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014750 * "setqflist()" function
14751 */
14752/*ARGSUSED*/
14753 static void
14754f_setqflist(argvars, rettv)
14755 typval_T *argvars;
14756 typval_T *rettv;
14757{
14758 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14759}
14760
14761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762 * "setreg()" function
14763 */
14764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014765f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014766 typval_T *argvars;
14767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768{
14769 int regname;
14770 char_u *strregname;
14771 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014772 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773 int append;
14774 char_u yank_type;
14775 long block_len;
14776
14777 block_len = -1;
14778 yank_type = MAUTO;
14779 append = FALSE;
14780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014781 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014782 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014784 if (strregname == NULL)
14785 return; /* type error; errmsg already given */
14786 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787 if (regname == 0 || regname == '@')
14788 regname = '"';
14789 else if (regname == '=')
14790 return;
14791
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014792 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014794 stropt = get_tv_string_chk(&argvars[2]);
14795 if (stropt == NULL)
14796 return; /* type error */
14797 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 switch (*stropt)
14799 {
14800 case 'a': case 'A': /* append */
14801 append = TRUE;
14802 break;
14803 case 'v': case 'c': /* character-wise selection */
14804 yank_type = MCHAR;
14805 break;
14806 case 'V': case 'l': /* line-wise selection */
14807 yank_type = MLINE;
14808 break;
14809#ifdef FEAT_VISUAL
14810 case 'b': case Ctrl_V: /* block-wise selection */
14811 yank_type = MBLOCK;
14812 if (VIM_ISDIGIT(stropt[1]))
14813 {
14814 ++stropt;
14815 block_len = getdigits(&stropt) - 1;
14816 --stropt;
14817 }
14818 break;
14819#endif
14820 }
14821 }
14822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014823 strval = get_tv_string_chk(&argvars[1]);
14824 if (strval != NULL)
14825 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828}
14829
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014830/*
14831 * "settabwinvar()" function
14832 */
14833 static void
14834f_settabwinvar(argvars, rettv)
14835 typval_T *argvars;
14836 typval_T *rettv;
14837{
14838 setwinvar(argvars, rettv, 1);
14839}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840
14841/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014842 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014845f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014846 typval_T *argvars;
14847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014849 setwinvar(argvars, rettv, 0);
14850}
14851
14852/*
14853 * "setwinvar()" and "settabwinvar()" functions
14854 */
14855 static void
14856setwinvar(argvars, rettv, off)
14857 typval_T *argvars;
14858 typval_T *rettv;
14859 int off;
14860{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014861 win_T *win;
14862#ifdef FEAT_WINDOWS
14863 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014864 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865#endif
14866 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014867 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014869 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014871 rettv->vval.v_number = 0;
14872
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873 if (check_restricted() || check_secure())
14874 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014875
14876#ifdef FEAT_WINDOWS
14877 if (off == 1)
14878 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14879 else
14880 tp = curtab;
14881#endif
14882 win = find_win_by_nr(&argvars[off], tp);
14883 varname = get_tv_string_chk(&argvars[off + 1]);
14884 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885
14886 if (win != NULL && varname != NULL && varp != NULL)
14887 {
14888#ifdef FEAT_WINDOWS
14889 /* set curwin to be our win, temporarily */
14890 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014891 save_curtab = curtab;
14892 goto_tabpage_tp(tp);
14893 if (!win_valid(win))
14894 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895 curwin = win;
14896 curbuf = curwin->w_buffer;
14897#endif
14898
14899 if (*varname == '&')
14900 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014901 long numval;
14902 char_u *strval;
14903 int error = FALSE;
14904
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014906 numval = get_tv_number_chk(varp, &error);
14907 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014908 if (!error && strval != NULL)
14909 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910 }
14911 else
14912 {
14913 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14914 if (winvarname != NULL)
14915 {
14916 STRCPY(winvarname, "w:");
14917 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014918 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919 vim_free(winvarname);
14920 }
14921 }
14922
14923#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014924 /* Restore current tabpage and window, if still valid (autocomands can
14925 * make them invalid). */
14926 if (valid_tabpage(save_curtab))
14927 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928 if (win_valid(save_curwin))
14929 {
14930 curwin = save_curwin;
14931 curbuf = curwin->w_buffer;
14932 }
14933#endif
14934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935}
14936
14937/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014938 * "shellescape({string})" function
14939 */
14940 static void
14941f_shellescape(argvars, rettv)
14942 typval_T *argvars;
14943 typval_T *rettv;
14944{
14945 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14946 rettv->v_type = VAR_STRING;
14947}
14948
14949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014950 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951 */
14952 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014953f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014954 typval_T *argvars;
14955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014957 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958
Bram Moolenaar0d660222005-01-07 21:51:51 +000014959 p = get_tv_string(&argvars[0]);
14960 rettv->vval.v_string = vim_strsave(p);
14961 simplify_filename(rettv->vval.v_string); /* simplify in place */
14962 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963}
14964
Bram Moolenaar0d660222005-01-07 21:51:51 +000014965static int
14966#ifdef __BORLANDC__
14967 _RTLENTRYF
14968#endif
14969 item_compare __ARGS((const void *s1, const void *s2));
14970static int
14971#ifdef __BORLANDC__
14972 _RTLENTRYF
14973#endif
14974 item_compare2 __ARGS((const void *s1, const void *s2));
14975
14976static int item_compare_ic;
14977static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014978static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014979#define ITEM_COMPARE_FAIL 999
14980
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014982 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984 static int
14985#ifdef __BORLANDC__
14986_RTLENTRYF
14987#endif
14988item_compare(s1, s2)
14989 const void *s1;
14990 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014992 char_u *p1, *p2;
14993 char_u *tofree1, *tofree2;
14994 int res;
14995 char_u numbuf1[NUMBUFLEN];
14996 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014998 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14999 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015000 if (p1 == NULL)
15001 p1 = (char_u *)"";
15002 if (p2 == NULL)
15003 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015004 if (item_compare_ic)
15005 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015007 res = STRCMP(p1, p2);
15008 vim_free(tofree1);
15009 vim_free(tofree2);
15010 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011}
15012
15013 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015014#ifdef __BORLANDC__
15015_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015017item_compare2(s1, s2)
15018 const void *s1;
15019 const void *s2;
15020{
15021 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015022 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015023 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015024 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015026 /* shortcut after failure in previous call; compare all items equal */
15027 if (item_compare_func_err)
15028 return 0;
15029
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015030 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15031 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015032 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15033 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015034
15035 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015036 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015037 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015038 clear_tv(&argv[0]);
15039 clear_tv(&argv[1]);
15040
15041 if (res == FAIL)
15042 res = ITEM_COMPARE_FAIL;
15043 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015044 /* return value has wrong type */
15045 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15046 if (item_compare_func_err)
15047 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015048 clear_tv(&rettv);
15049 return res;
15050}
15051
15052/*
15053 * "sort({list})" function
15054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015056f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015057 typval_T *argvars;
15058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059{
Bram Moolenaar33570922005-01-25 22:26:29 +000015060 list_T *l;
15061 listitem_T *li;
15062 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015063 long len;
15064 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015065
Bram Moolenaar0d660222005-01-07 21:51:51 +000015066 rettv->vval.v_number = 0;
15067 if (argvars[0].v_type != VAR_LIST)
15068 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015069 else
15070 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015071 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015072 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015073 return;
15074 rettv->vval.v_list = l;
15075 rettv->v_type = VAR_LIST;
15076 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077
Bram Moolenaar0d660222005-01-07 21:51:51 +000015078 len = list_len(l);
15079 if (len <= 1)
15080 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082 item_compare_ic = FALSE;
15083 item_compare_func = NULL;
15084 if (argvars[1].v_type != VAR_UNKNOWN)
15085 {
15086 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015087 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015088 else
15089 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015090 int error = FALSE;
15091
15092 i = get_tv_number_chk(&argvars[1], &error);
15093 if (error)
15094 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015095 if (i == 1)
15096 item_compare_ic = TRUE;
15097 else
15098 item_compare_func = get_tv_string(&argvars[1]);
15099 }
15100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101
Bram Moolenaar0d660222005-01-07 21:51:51 +000015102 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015103 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104 if (ptrs == NULL)
15105 return;
15106 i = 0;
15107 for (li = l->lv_first; li != NULL; li = li->li_next)
15108 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015110 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015111 /* test the compare function */
15112 if (item_compare_func != NULL
15113 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15114 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015115 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015117 {
15118 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015119 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015120 item_compare_func == NULL ? item_compare : item_compare2);
15121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015122 if (!item_compare_func_err)
15123 {
15124 /* Clear the List and append the items in the sorted order. */
15125 l->lv_first = l->lv_last = NULL;
15126 l->lv_len = 0;
15127 for (i = 0; i < len; ++i)
15128 list_append(l, ptrs[i]);
15129 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015130 }
15131
15132 vim_free(ptrs);
15133 }
15134}
15135
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015136/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015137 * "soundfold({word})" function
15138 */
15139 static void
15140f_soundfold(argvars, rettv)
15141 typval_T *argvars;
15142 typval_T *rettv;
15143{
15144 char_u *s;
15145
15146 rettv->v_type = VAR_STRING;
15147 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015148#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015149 rettv->vval.v_string = eval_soundfold(s);
15150#else
15151 rettv->vval.v_string = vim_strsave(s);
15152#endif
15153}
15154
15155/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015156 * "spellbadword()" function
15157 */
15158/* ARGSUSED */
15159 static void
15160f_spellbadword(argvars, rettv)
15161 typval_T *argvars;
15162 typval_T *rettv;
15163{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015164 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015165 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015166 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015167
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015168 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015169 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015170
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015171#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015172 if (argvars[0].v_type == VAR_UNKNOWN)
15173 {
15174 /* Find the start and length of the badly spelled word. */
15175 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15176 if (len != 0)
15177 word = ml_get_cursor();
15178 }
15179 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15180 {
15181 char_u *str = get_tv_string_chk(&argvars[0]);
15182 int capcol = -1;
15183
15184 if (str != NULL)
15185 {
15186 /* Check the argument for spelling. */
15187 while (*str != NUL)
15188 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015189 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015190 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015191 {
15192 word = str;
15193 break;
15194 }
15195 str += len;
15196 }
15197 }
15198 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015199#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015200
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015201 list_append_string(rettv->vval.v_list, word, len);
15202 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015203 attr == HLF_SPB ? "bad" :
15204 attr == HLF_SPR ? "rare" :
15205 attr == HLF_SPL ? "local" :
15206 attr == HLF_SPC ? "caps" :
15207 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015208}
15209
15210/*
15211 * "spellsuggest()" function
15212 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015213/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015214 static void
15215f_spellsuggest(argvars, rettv)
15216 typval_T *argvars;
15217 typval_T *rettv;
15218{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015219#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015220 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015221 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015222 int maxcount;
15223 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015224 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015225 listitem_T *li;
15226 int need_capital = FALSE;
15227#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015228
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015229 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015230 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015231
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015232#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015233 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15234 {
15235 str = get_tv_string(&argvars[0]);
15236 if (argvars[1].v_type != VAR_UNKNOWN)
15237 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015238 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015239 if (maxcount <= 0)
15240 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015241 if (argvars[2].v_type != VAR_UNKNOWN)
15242 {
15243 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15244 if (typeerr)
15245 return;
15246 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015247 }
15248 else
15249 maxcount = 25;
15250
Bram Moolenaar4770d092006-01-12 23:22:24 +000015251 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015252
15253 for (i = 0; i < ga.ga_len; ++i)
15254 {
15255 str = ((char_u **)ga.ga_data)[i];
15256
15257 li = listitem_alloc();
15258 if (li == NULL)
15259 vim_free(str);
15260 else
15261 {
15262 li->li_tv.v_type = VAR_STRING;
15263 li->li_tv.v_lock = 0;
15264 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015265 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015266 }
15267 }
15268 ga_clear(&ga);
15269 }
15270#endif
15271}
15272
Bram Moolenaar0d660222005-01-07 21:51:51 +000015273 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015274f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015275 typval_T *argvars;
15276 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015277{
15278 char_u *str;
15279 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015280 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015281 regmatch_T regmatch;
15282 char_u patbuf[NUMBUFLEN];
15283 char_u *save_cpo;
15284 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015285 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015286 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015287 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015288
15289 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15290 save_cpo = p_cpo;
15291 p_cpo = (char_u *)"";
15292
15293 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015294 if (argvars[1].v_type != VAR_UNKNOWN)
15295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015296 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15297 if (pat == NULL)
15298 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015299 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015300 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015301 }
15302 if (pat == NULL || *pat == NUL)
15303 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015304
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015305 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015307 if (typeerr)
15308 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309
Bram Moolenaar0d660222005-01-07 21:51:51 +000015310 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15311 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015313 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015314 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015315 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015316 if (*str == NUL)
15317 match = FALSE; /* empty item at the end */
15318 else
15319 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015320 if (match)
15321 end = regmatch.startp[0];
15322 else
15323 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015324 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15325 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015326 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015327 if (list_append_string(rettv->vval.v_list, str,
15328 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015329 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015330 }
15331 if (!match)
15332 break;
15333 /* Advance to just after the match. */
15334 if (regmatch.endp[0] > str)
15335 col = 0;
15336 else
15337 {
15338 /* Don't get stuck at the same match. */
15339#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015340 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015341#else
15342 col = 1;
15343#endif
15344 }
15345 str = regmatch.endp[0];
15346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347
Bram Moolenaar0d660222005-01-07 21:51:51 +000015348 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350
Bram Moolenaar0d660222005-01-07 21:51:51 +000015351 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352}
15353
Bram Moolenaar2c932302006-03-18 21:42:09 +000015354/*
15355 * "str2nr()" function
15356 */
15357 static void
15358f_str2nr(argvars, rettv)
15359 typval_T *argvars;
15360 typval_T *rettv;
15361{
15362 int base = 10;
15363 char_u *p;
15364 long n;
15365
15366 if (argvars[1].v_type != VAR_UNKNOWN)
15367 {
15368 base = get_tv_number(&argvars[1]);
15369 if (base != 8 && base != 10 && base != 16)
15370 {
15371 EMSG(_(e_invarg));
15372 return;
15373 }
15374 }
15375
15376 p = skipwhite(get_tv_string(&argvars[0]));
15377 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15378 rettv->vval.v_number = n;
15379}
15380
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381#ifdef HAVE_STRFTIME
15382/*
15383 * "strftime({format}[, {time}])" function
15384 */
15385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015386f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015387 typval_T *argvars;
15388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389{
15390 char_u result_buf[256];
15391 struct tm *curtime;
15392 time_t seconds;
15393 char_u *p;
15394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015395 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015398 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 seconds = time(NULL);
15400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015401 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402 curtime = localtime(&seconds);
15403 /* MSVC returns NULL for an invalid value of seconds. */
15404 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015405 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406 else
15407 {
15408# ifdef FEAT_MBYTE
15409 vimconv_T conv;
15410 char_u *enc;
15411
15412 conv.vc_type = CONV_NONE;
15413 enc = enc_locale();
15414 convert_setup(&conv, p_enc, enc);
15415 if (conv.vc_type != CONV_NONE)
15416 p = string_convert(&conv, p, NULL);
15417# endif
15418 if (p != NULL)
15419 (void)strftime((char *)result_buf, sizeof(result_buf),
15420 (char *)p, curtime);
15421 else
15422 result_buf[0] = NUL;
15423
15424# ifdef FEAT_MBYTE
15425 if (conv.vc_type != CONV_NONE)
15426 vim_free(p);
15427 convert_setup(&conv, enc, p_enc);
15428 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015429 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430 else
15431# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015432 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433
15434# ifdef FEAT_MBYTE
15435 /* Release conversion descriptors */
15436 convert_setup(&conv, NULL, NULL);
15437 vim_free(enc);
15438# endif
15439 }
15440}
15441#endif
15442
15443/*
15444 * "stridx()" function
15445 */
15446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015448 typval_T *argvars;
15449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450{
15451 char_u buf[NUMBUFLEN];
15452 char_u *needle;
15453 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015454 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015456 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015458 needle = get_tv_string_chk(&argvars[1]);
15459 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015460 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015461 if (needle == NULL || haystack == NULL)
15462 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463
Bram Moolenaar33570922005-01-25 22:26:29 +000015464 if (argvars[2].v_type != VAR_UNKNOWN)
15465 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015466 int error = FALSE;
15467
15468 start_idx = get_tv_number_chk(&argvars[2], &error);
15469 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015470 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015471 if (start_idx >= 0)
15472 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015473 }
15474
15475 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15476 if (pos != NULL)
15477 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478}
15479
15480/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015481 * "string()" function
15482 */
15483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015484f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015485 typval_T *argvars;
15486 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015487{
15488 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015489 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015491 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015492 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015493 /* Make a copy if we have a value but it's not in allocate memory. */
15494 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015495 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496}
15497
15498/*
15499 * "strlen()" function
15500 */
15501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015502f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015503 typval_T *argvars;
15504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015506 rettv->vval.v_number = (varnumber_T)(STRLEN(
15507 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508}
15509
15510/*
15511 * "strpart()" function
15512 */
15513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015514f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015515 typval_T *argvars;
15516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517{
15518 char_u *p;
15519 int n;
15520 int len;
15521 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015522 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015524 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 slen = (int)STRLEN(p);
15526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015527 n = get_tv_number_chk(&argvars[1], &error);
15528 if (error)
15529 len = 0;
15530 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015531 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532 else
15533 len = slen - n; /* default len: all bytes that are available. */
15534
15535 /*
15536 * Only return the overlap between the specified part and the actual
15537 * string.
15538 */
15539 if (n < 0)
15540 {
15541 len += n;
15542 n = 0;
15543 }
15544 else if (n > slen)
15545 n = slen;
15546 if (len < 0)
15547 len = 0;
15548 else if (n + len > slen)
15549 len = slen - n;
15550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015551 rettv->v_type = VAR_STRING;
15552 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553}
15554
15555/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015556 * "strridx()" function
15557 */
15558 static void
15559f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015560 typval_T *argvars;
15561 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015562{
15563 char_u buf[NUMBUFLEN];
15564 char_u *needle;
15565 char_u *haystack;
15566 char_u *rest;
15567 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015568 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015570 needle = get_tv_string_chk(&argvars[1]);
15571 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015572
15573 rettv->vval.v_number = -1;
15574 if (needle == NULL || haystack == NULL)
15575 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015576
15577 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015578 if (argvars[2].v_type != VAR_UNKNOWN)
15579 {
15580 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015581 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015582 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015583 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015584 }
15585 else
15586 end_idx = haystack_len;
15587
Bram Moolenaar0d660222005-01-07 21:51:51 +000015588 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015589 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015590 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015591 lastmatch = haystack + end_idx;
15592 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015593 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015594 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015595 for (rest = haystack; *rest != '\0'; ++rest)
15596 {
15597 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015598 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015599 break;
15600 lastmatch = rest;
15601 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015602 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015603
15604 if (lastmatch == NULL)
15605 rettv->vval.v_number = -1;
15606 else
15607 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15608}
15609
15610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611 * "strtrans()" function
15612 */
15613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015614f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015615 typval_T *argvars;
15616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015618 rettv->v_type = VAR_STRING;
15619 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620}
15621
15622/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015623 * "submatch()" function
15624 */
15625 static void
15626f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015627 typval_T *argvars;
15628 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015629{
15630 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015631 rettv->vval.v_string =
15632 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015633}
15634
15635/*
15636 * "substitute()" function
15637 */
15638 static void
15639f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015640 typval_T *argvars;
15641 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015642{
15643 char_u patbuf[NUMBUFLEN];
15644 char_u subbuf[NUMBUFLEN];
15645 char_u flagsbuf[NUMBUFLEN];
15646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015647 char_u *str = get_tv_string_chk(&argvars[0]);
15648 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15649 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15650 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15651
Bram Moolenaar0d660222005-01-07 21:51:51 +000015652 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015653 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15654 rettv->vval.v_string = NULL;
15655 else
15656 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015657}
15658
15659/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015660 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 */
15662/*ARGSUSED*/
15663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015664f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015665 typval_T *argvars;
15666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667{
15668 int id = 0;
15669#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015670 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 long col;
15672 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015673 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015675 lnum = get_tv_lnum(argvars); /* -1 on type error */
15676 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15677 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015679 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015680 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015681 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682#endif
15683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015684 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685}
15686
15687/*
15688 * "synIDattr(id, what [, mode])" function
15689 */
15690/*ARGSUSED*/
15691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015692f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015693 typval_T *argvars;
15694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695{
15696 char_u *p = NULL;
15697#ifdef FEAT_SYN_HL
15698 int id;
15699 char_u *what;
15700 char_u *mode;
15701 char_u modebuf[NUMBUFLEN];
15702 int modec;
15703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015704 id = get_tv_number(&argvars[0]);
15705 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015706 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015708 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 modec = TOLOWER_ASC(mode[0]);
15710 if (modec != 't' && modec != 'c'
15711#ifdef FEAT_GUI
15712 && modec != 'g'
15713#endif
15714 )
15715 modec = 0; /* replace invalid with current */
15716 }
15717 else
15718 {
15719#ifdef FEAT_GUI
15720 if (gui.in_use)
15721 modec = 'g';
15722 else
15723#endif
15724 if (t_colors > 1)
15725 modec = 'c';
15726 else
15727 modec = 't';
15728 }
15729
15730
15731 switch (TOLOWER_ASC(what[0]))
15732 {
15733 case 'b':
15734 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15735 p = highlight_color(id, what, modec);
15736 else /* bold */
15737 p = highlight_has_attr(id, HL_BOLD, modec);
15738 break;
15739
15740 case 'f': /* fg[#] */
15741 p = highlight_color(id, what, modec);
15742 break;
15743
15744 case 'i':
15745 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15746 p = highlight_has_attr(id, HL_INVERSE, modec);
15747 else /* italic */
15748 p = highlight_has_attr(id, HL_ITALIC, modec);
15749 break;
15750
15751 case 'n': /* name */
15752 p = get_highlight_name(NULL, id - 1);
15753 break;
15754
15755 case 'r': /* reverse */
15756 p = highlight_has_attr(id, HL_INVERSE, modec);
15757 break;
15758
15759 case 's': /* standout */
15760 p = highlight_has_attr(id, HL_STANDOUT, modec);
15761 break;
15762
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015763 case 'u':
15764 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15765 /* underline */
15766 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15767 else
15768 /* undercurl */
15769 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 break;
15771 }
15772
15773 if (p != NULL)
15774 p = vim_strsave(p);
15775#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015776 rettv->v_type = VAR_STRING;
15777 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778}
15779
15780/*
15781 * "synIDtrans(id)" function
15782 */
15783/*ARGSUSED*/
15784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015785f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015786 typval_T *argvars;
15787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788{
15789 int id;
15790
15791#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015792 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793
15794 if (id > 0)
15795 id = syn_get_final_id(id);
15796 else
15797#endif
15798 id = 0;
15799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015800 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801}
15802
15803/*
15804 * "system()" function
15805 */
15806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015807f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015808 typval_T *argvars;
15809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015811 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015813 char_u *infile = NULL;
15814 char_u buf[NUMBUFLEN];
15815 int err = FALSE;
15816 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015818 if (check_restricted() || check_secure())
15819 return;
15820
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015821 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015822 {
15823 /*
15824 * Write the string to a temp file, to be used for input of the shell
15825 * command.
15826 */
15827 if ((infile = vim_tempname('i')) == NULL)
15828 {
15829 EMSG(_(e_notmp));
15830 return;
15831 }
15832
15833 fd = mch_fopen((char *)infile, WRITEBIN);
15834 if (fd == NULL)
15835 {
15836 EMSG2(_(e_notopen), infile);
15837 goto done;
15838 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015839 p = get_tv_string_buf_chk(&argvars[1], buf);
15840 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015841 {
15842 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015843 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015844 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015845 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15846 err = TRUE;
15847 if (fclose(fd) != 0)
15848 err = TRUE;
15849 if (err)
15850 {
15851 EMSG(_("E677: Error writing temp file"));
15852 goto done;
15853 }
15854 }
15855
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015856 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15857 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015858
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859#ifdef USE_CR
15860 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015861 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862 {
15863 char_u *s;
15864
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015865 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866 {
15867 if (*s == CAR)
15868 *s = NL;
15869 }
15870 }
15871#else
15872# ifdef USE_CRNL
15873 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015874 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015875 {
15876 char_u *s, *d;
15877
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015878 d = res;
15879 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880 {
15881 if (s[0] == CAR && s[1] == NL)
15882 ++s;
15883 *d++ = *s;
15884 }
15885 *d = NUL;
15886 }
15887# endif
15888#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015889
15890done:
15891 if (infile != NULL)
15892 {
15893 mch_remove(infile);
15894 vim_free(infile);
15895 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015896 rettv->v_type = VAR_STRING;
15897 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898}
15899
15900/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015901 * "tabpagebuflist()" function
15902 */
15903/* ARGSUSED */
15904 static void
15905f_tabpagebuflist(argvars, rettv)
15906 typval_T *argvars;
15907 typval_T *rettv;
15908{
15909#ifndef FEAT_WINDOWS
15910 rettv->vval.v_number = 0;
15911#else
15912 tabpage_T *tp;
15913 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015914
15915 if (argvars[0].v_type == VAR_UNKNOWN)
15916 wp = firstwin;
15917 else
15918 {
15919 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15920 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015921 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015922 }
15923 if (wp == NULL)
15924 rettv->vval.v_number = 0;
15925 else
15926 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015927 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015928 rettv->vval.v_number = 0;
15929 else
15930 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015931 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015932 if (list_append_number(rettv->vval.v_list,
15933 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015934 break;
15935 }
15936 }
15937#endif
15938}
15939
15940
15941/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015942 * "tabpagenr()" function
15943 */
15944/* ARGSUSED */
15945 static void
15946f_tabpagenr(argvars, rettv)
15947 typval_T *argvars;
15948 typval_T *rettv;
15949{
15950 int nr = 1;
15951#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015952 char_u *arg;
15953
15954 if (argvars[0].v_type != VAR_UNKNOWN)
15955 {
15956 arg = get_tv_string_chk(&argvars[0]);
15957 nr = 0;
15958 if (arg != NULL)
15959 {
15960 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015961 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015962 else
15963 EMSG2(_(e_invexpr2), arg);
15964 }
15965 }
15966 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015967 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015968#endif
15969 rettv->vval.v_number = nr;
15970}
15971
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015972
15973#ifdef FEAT_WINDOWS
15974static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15975
15976/*
15977 * Common code for tabpagewinnr() and winnr().
15978 */
15979 static int
15980get_winnr(tp, argvar)
15981 tabpage_T *tp;
15982 typval_T *argvar;
15983{
15984 win_T *twin;
15985 int nr = 1;
15986 win_T *wp;
15987 char_u *arg;
15988
15989 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15990 if (argvar->v_type != VAR_UNKNOWN)
15991 {
15992 arg = get_tv_string_chk(argvar);
15993 if (arg == NULL)
15994 nr = 0; /* type error; errmsg already given */
15995 else if (STRCMP(arg, "$") == 0)
15996 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15997 else if (STRCMP(arg, "#") == 0)
15998 {
15999 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16000 if (twin == NULL)
16001 nr = 0;
16002 }
16003 else
16004 {
16005 EMSG2(_(e_invexpr2), arg);
16006 nr = 0;
16007 }
16008 }
16009
16010 if (nr > 0)
16011 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16012 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016013 {
16014 if (wp == NULL)
16015 {
16016 /* didn't find it in this tabpage */
16017 nr = 0;
16018 break;
16019 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016020 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016021 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016022 return nr;
16023}
16024#endif
16025
16026/*
16027 * "tabpagewinnr()" function
16028 */
16029/* ARGSUSED */
16030 static void
16031f_tabpagewinnr(argvars, rettv)
16032 typval_T *argvars;
16033 typval_T *rettv;
16034{
16035 int nr = 1;
16036#ifdef FEAT_WINDOWS
16037 tabpage_T *tp;
16038
16039 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16040 if (tp == NULL)
16041 nr = 0;
16042 else
16043 nr = get_winnr(tp, &argvars[1]);
16044#endif
16045 rettv->vval.v_number = nr;
16046}
16047
16048
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016049/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016050 * "tagfiles()" function
16051 */
16052/*ARGSUSED*/
16053 static void
16054f_tagfiles(argvars, rettv)
16055 typval_T *argvars;
16056 typval_T *rettv;
16057{
16058 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016059 tagname_T tn;
16060 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016061
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016062 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016063 {
16064 rettv->vval.v_number = 0;
16065 return;
16066 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016067
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016068 for (first = TRUE; ; first = FALSE)
16069 if (get_tagfname(&tn, first, fname) == FAIL
16070 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016071 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016072 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016073}
16074
16075/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016076 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016077 */
16078 static void
16079f_taglist(argvars, rettv)
16080 typval_T *argvars;
16081 typval_T *rettv;
16082{
16083 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016084
16085 tag_pattern = get_tv_string(&argvars[0]);
16086
16087 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016088 if (*tag_pattern == NUL)
16089 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016090
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016091 if (rettv_list_alloc(rettv) == OK)
16092 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016093}
16094
16095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096 * "tempname()" function
16097 */
16098/*ARGSUSED*/
16099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016100f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016101 typval_T *argvars;
16102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103{
16104 static int x = 'A';
16105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016106 rettv->v_type = VAR_STRING;
16107 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108
16109 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16110 * names. Skip 'I' and 'O', they are used for shell redirection. */
16111 do
16112 {
16113 if (x == 'Z')
16114 x = '0';
16115 else if (x == '9')
16116 x = 'A';
16117 else
16118 {
16119#ifdef EBCDIC
16120 if (x == 'I')
16121 x = 'J';
16122 else if (x == 'R')
16123 x = 'S';
16124 else
16125#endif
16126 ++x;
16127 }
16128 } while (x == 'I' || x == 'O');
16129}
16130
16131/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016132 * "test(list)" function: Just checking the walls...
16133 */
16134/*ARGSUSED*/
16135 static void
16136f_test(argvars, rettv)
16137 typval_T *argvars;
16138 typval_T *rettv;
16139{
16140 /* Used for unit testing. Change the code below to your liking. */
16141#if 0
16142 listitem_T *li;
16143 list_T *l;
16144 char_u *bad, *good;
16145
16146 if (argvars[0].v_type != VAR_LIST)
16147 return;
16148 l = argvars[0].vval.v_list;
16149 if (l == NULL)
16150 return;
16151 li = l->lv_first;
16152 if (li == NULL)
16153 return;
16154 bad = get_tv_string(&li->li_tv);
16155 li = li->li_next;
16156 if (li == NULL)
16157 return;
16158 good = get_tv_string(&li->li_tv);
16159 rettv->vval.v_number = test_edit_score(bad, good);
16160#endif
16161}
16162
16163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 * "tolower(string)" function
16165 */
16166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016167f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016168 typval_T *argvars;
16169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170{
16171 char_u *p;
16172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016173 p = vim_strsave(get_tv_string(&argvars[0]));
16174 rettv->v_type = VAR_STRING;
16175 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176
16177 if (p != NULL)
16178 while (*p != NUL)
16179 {
16180#ifdef FEAT_MBYTE
16181 int l;
16182
16183 if (enc_utf8)
16184 {
16185 int c, lc;
16186
16187 c = utf_ptr2char(p);
16188 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016189 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190 /* TODO: reallocate string when byte count changes. */
16191 if (utf_char2len(lc) == l)
16192 utf_char2bytes(lc, p);
16193 p += l;
16194 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016195 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 p += l; /* skip multi-byte character */
16197 else
16198#endif
16199 {
16200 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
16201 ++p;
16202 }
16203 }
16204}
16205
16206/*
16207 * "toupper(string)" function
16208 */
16209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016210f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016211 typval_T *argvars;
16212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016214 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016215 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216}
16217
16218/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016219 * "tr(string, fromstr, tostr)" function
16220 */
16221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016222f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016223 typval_T *argvars;
16224 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016225{
16226 char_u *instr;
16227 char_u *fromstr;
16228 char_u *tostr;
16229 char_u *p;
16230#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016231 int inlen;
16232 int fromlen;
16233 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016234 int idx;
16235 char_u *cpstr;
16236 int cplen;
16237 int first = TRUE;
16238#endif
16239 char_u buf[NUMBUFLEN];
16240 char_u buf2[NUMBUFLEN];
16241 garray_T ga;
16242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016243 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016244 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16245 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016246
16247 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016248 rettv->v_type = VAR_STRING;
16249 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016250 if (fromstr == NULL || tostr == NULL)
16251 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016252 ga_init2(&ga, (int)sizeof(char), 80);
16253
16254#ifdef FEAT_MBYTE
16255 if (!has_mbyte)
16256#endif
16257 /* not multi-byte: fromstr and tostr must be the same length */
16258 if (STRLEN(fromstr) != STRLEN(tostr))
16259 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016260#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016261error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016262#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016263 EMSG2(_(e_invarg2), fromstr);
16264 ga_clear(&ga);
16265 return;
16266 }
16267
16268 /* fromstr and tostr have to contain the same number of chars */
16269 while (*instr != NUL)
16270 {
16271#ifdef FEAT_MBYTE
16272 if (has_mbyte)
16273 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016274 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016275 cpstr = instr;
16276 cplen = inlen;
16277 idx = 0;
16278 for (p = fromstr; *p != NUL; p += fromlen)
16279 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016280 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016281 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16282 {
16283 for (p = tostr; *p != NUL; p += tolen)
16284 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016285 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016286 if (idx-- == 0)
16287 {
16288 cplen = tolen;
16289 cpstr = p;
16290 break;
16291 }
16292 }
16293 if (*p == NUL) /* tostr is shorter than fromstr */
16294 goto error;
16295 break;
16296 }
16297 ++idx;
16298 }
16299
16300 if (first && cpstr == instr)
16301 {
16302 /* Check that fromstr and tostr have the same number of
16303 * (multi-byte) characters. Done only once when a character
16304 * of instr doesn't appear in fromstr. */
16305 first = FALSE;
16306 for (p = tostr; *p != NUL; p += tolen)
16307 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016308 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016309 --idx;
16310 }
16311 if (idx != 0)
16312 goto error;
16313 }
16314
16315 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016316 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016317 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016318
16319 instr += inlen;
16320 }
16321 else
16322#endif
16323 {
16324 /* When not using multi-byte chars we can do it faster. */
16325 p = vim_strchr(fromstr, *instr);
16326 if (p != NULL)
16327 ga_append(&ga, tostr[p - fromstr]);
16328 else
16329 ga_append(&ga, *instr);
16330 ++instr;
16331 }
16332 }
16333
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016334 /* add a terminating NUL */
16335 ga_grow(&ga, 1);
16336 ga_append(&ga, NUL);
16337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016338 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016339}
16340
16341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342 * "type(expr)" function
16343 */
16344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016345f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016346 typval_T *argvars;
16347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016349 int n;
16350
16351 switch (argvars[0].v_type)
16352 {
16353 case VAR_NUMBER: n = 0; break;
16354 case VAR_STRING: n = 1; break;
16355 case VAR_FUNC: n = 2; break;
16356 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016357 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016358 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16359 }
16360 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361}
16362
16363/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016364 * "values(dict)" function
16365 */
16366 static void
16367f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016368 typval_T *argvars;
16369 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016370{
16371 dict_list(argvars, rettv, 1);
16372}
16373
16374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 * "virtcol(string)" function
16376 */
16377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016378f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016379 typval_T *argvars;
16380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381{
16382 colnr_T vcol = 0;
16383 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016384 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016386 fp = var2fpos(&argvars[0], FALSE, &fnum);
16387 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16388 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389 {
16390 getvvcol(curwin, fp, NULL, NULL, &vcol);
16391 ++vcol;
16392 }
16393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016394 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395}
16396
16397/*
16398 * "visualmode()" function
16399 */
16400/*ARGSUSED*/
16401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016402f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016403 typval_T *argvars;
16404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405{
16406#ifdef FEAT_VISUAL
16407 char_u str[2];
16408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016409 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 str[0] = curbuf->b_visual_mode_eval;
16411 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016412 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413
16414 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016415 if ((argvars[0].v_type == VAR_NUMBER
16416 && argvars[0].vval.v_number != 0)
16417 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016418 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 curbuf->b_visual_mode_eval = NUL;
16420#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016421 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422#endif
16423}
16424
16425/*
16426 * "winbufnr(nr)" function
16427 */
16428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016429f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016430 typval_T *argvars;
16431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432{
16433 win_T *wp;
16434
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016435 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016437 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016439 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440}
16441
16442/*
16443 * "wincol()" function
16444 */
16445/*ARGSUSED*/
16446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016447f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016448 typval_T *argvars;
16449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450{
16451 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016452 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453}
16454
16455/*
16456 * "winheight(nr)" function
16457 */
16458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016459f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016460 typval_T *argvars;
16461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462{
16463 win_T *wp;
16464
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016465 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016467 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016469 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470}
16471
16472/*
16473 * "winline()" function
16474 */
16475/*ARGSUSED*/
16476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016477f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016478 typval_T *argvars;
16479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480{
16481 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016482 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483}
16484
16485/*
16486 * "winnr()" function
16487 */
16488/* ARGSUSED */
16489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016490f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016491 typval_T *argvars;
16492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493{
16494 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016495
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016497 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016499 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500}
16501
16502/*
16503 * "winrestcmd()" function
16504 */
16505/* ARGSUSED */
16506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016507f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016508 typval_T *argvars;
16509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510{
16511#ifdef FEAT_WINDOWS
16512 win_T *wp;
16513 int winnr = 1;
16514 garray_T ga;
16515 char_u buf[50];
16516
16517 ga_init2(&ga, (int)sizeof(char), 70);
16518 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16519 {
16520 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16521 ga_concat(&ga, buf);
16522# ifdef FEAT_VERTSPLIT
16523 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16524 ga_concat(&ga, buf);
16525# endif
16526 ++winnr;
16527 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016528 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016530 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016532 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016534 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535}
16536
16537/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016538 * "winrestview()" function
16539 */
16540/* ARGSUSED */
16541 static void
16542f_winrestview(argvars, rettv)
16543 typval_T *argvars;
16544 typval_T *rettv;
16545{
16546 dict_T *dict;
16547
16548 if (argvars[0].v_type != VAR_DICT
16549 || (dict = argvars[0].vval.v_dict) == NULL)
16550 EMSG(_(e_invarg));
16551 else
16552 {
16553 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16554 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16555#ifdef FEAT_VIRTUALEDIT
16556 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16557#endif
16558 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016559 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016560
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016561 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016562#ifdef FEAT_DIFF
16563 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16564#endif
16565 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16566 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16567
16568 check_cursor();
16569 changed_cline_bef_curs();
16570 invalidate_botline();
16571 redraw_later(VALID);
16572
16573 if (curwin->w_topline == 0)
16574 curwin->w_topline = 1;
16575 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16576 curwin->w_topline = curbuf->b_ml.ml_line_count;
16577#ifdef FEAT_DIFF
16578 check_topfill(curwin, TRUE);
16579#endif
16580 }
16581}
16582
16583/*
16584 * "winsaveview()" function
16585 */
16586/* ARGSUSED */
16587 static void
16588f_winsaveview(argvars, rettv)
16589 typval_T *argvars;
16590 typval_T *rettv;
16591{
16592 dict_T *dict;
16593
16594 dict = dict_alloc();
16595 if (dict == NULL)
16596 return;
16597 rettv->v_type = VAR_DICT;
16598 rettv->vval.v_dict = dict;
16599 ++dict->dv_refcount;
16600
16601 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16602 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16603#ifdef FEAT_VIRTUALEDIT
16604 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16605#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016606 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016607 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16608
16609 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16610#ifdef FEAT_DIFF
16611 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16612#endif
16613 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16614 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16615}
16616
16617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618 * "winwidth(nr)" function
16619 */
16620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016621f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016622 typval_T *argvars;
16623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624{
16625 win_T *wp;
16626
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016627 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016629 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630 else
16631#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016632 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016633#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016634 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635#endif
16636}
16637
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016639 * "writefile()" function
16640 */
16641 static void
16642f_writefile(argvars, rettv)
16643 typval_T *argvars;
16644 typval_T *rettv;
16645{
16646 int binary = FALSE;
16647 char_u *fname;
16648 FILE *fd;
16649 listitem_T *li;
16650 char_u *s;
16651 int ret = 0;
16652 int c;
16653
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016654 if (check_restricted() || check_secure())
16655 return;
16656
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016657 if (argvars[0].v_type != VAR_LIST)
16658 {
16659 EMSG2(_(e_listarg), "writefile()");
16660 return;
16661 }
16662 if (argvars[0].vval.v_list == NULL)
16663 return;
16664
16665 if (argvars[2].v_type != VAR_UNKNOWN
16666 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16667 binary = TRUE;
16668
16669 /* Always open the file in binary mode, library functions have a mind of
16670 * their own about CR-LF conversion. */
16671 fname = get_tv_string(&argvars[1]);
16672 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16673 {
16674 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16675 ret = -1;
16676 }
16677 else
16678 {
16679 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16680 li = li->li_next)
16681 {
16682 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16683 {
16684 if (*s == '\n')
16685 c = putc(NUL, fd);
16686 else
16687 c = putc(*s, fd);
16688 if (c == EOF)
16689 {
16690 ret = -1;
16691 break;
16692 }
16693 }
16694 if (!binary || li->li_next != NULL)
16695 if (putc('\n', fd) == EOF)
16696 {
16697 ret = -1;
16698 break;
16699 }
16700 if (ret < 0)
16701 {
16702 EMSG(_(e_write));
16703 break;
16704 }
16705 }
16706 fclose(fd);
16707 }
16708
16709 rettv->vval.v_number = ret;
16710}
16711
16712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016713 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016714 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715 */
16716 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000016717var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016718 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016719 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016720 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016722 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016724 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725
Bram Moolenaara5525202006-03-02 22:52:09 +000016726 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016727 if (varp->v_type == VAR_LIST)
16728 {
16729 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016730 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016731 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016732 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016733
16734 l = varp->vval.v_list;
16735 if (l == NULL)
16736 return NULL;
16737
16738 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016739 pos.lnum = list_find_nr(l, 0L, &error);
16740 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016741 return NULL; /* invalid line number */
16742
16743 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016744 pos.col = list_find_nr(l, 1L, &error);
16745 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016746 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016747 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000016748
16749 /* We accept "$" for the column number: last column. */
16750 li = list_find(l, 1L);
16751 if (li != NULL && li->li_tv.v_type == VAR_STRING
16752 && li->li_tv.vval.v_string != NULL
16753 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
16754 pos.col = len + 1;
16755
Bram Moolenaara5525202006-03-02 22:52:09 +000016756 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016757 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016758 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016759 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016760
Bram Moolenaara5525202006-03-02 22:52:09 +000016761#ifdef FEAT_VIRTUALEDIT
16762 /* Get the virtual offset. Defaults to zero. */
16763 pos.coladd = list_find_nr(l, 2L, &error);
16764 if (error)
16765 pos.coladd = 0;
16766#endif
16767
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016768 return &pos;
16769 }
16770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016771 name = get_tv_string_chk(varp);
16772 if (name == NULL)
16773 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 if (name[0] == '.') /* cursor */
16775 return &curwin->w_cursor;
16776 if (name[0] == '\'') /* mark */
16777 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016778 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16780 return NULL;
16781 return pp;
16782 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016783
16784#ifdef FEAT_VIRTUALEDIT
16785 pos.coladd = 0;
16786#endif
16787
Bram Moolenaar477933c2007-07-17 14:32:23 +000016788 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016789 {
16790 pos.col = 0;
16791 if (name[1] == '0') /* "w0": first visible line */
16792 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016793 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016794 pos.lnum = curwin->w_topline;
16795 return &pos;
16796 }
16797 else if (name[1] == '$') /* "w$": last visible line */
16798 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016799 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016800 pos.lnum = curwin->w_botline - 1;
16801 return &pos;
16802 }
16803 }
16804 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000016806 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 {
16808 pos.lnum = curbuf->b_ml.ml_line_count;
16809 pos.col = 0;
16810 }
16811 else
16812 {
16813 pos.lnum = curwin->w_cursor.lnum;
16814 pos.col = (colnr_T)STRLEN(ml_get_curline());
16815 }
16816 return &pos;
16817 }
16818 return NULL;
16819}
16820
16821/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016822 * Convert list in "arg" into a position and optional file number.
16823 * When "fnump" is NULL there is no file number, only 3 items.
16824 * Note that the column is passed on as-is, the caller may want to decrement
16825 * it to use 1 for the first column.
16826 * Return FAIL when conversion is not possible, doesn't check the position for
16827 * validity.
16828 */
16829 static int
16830list2fpos(arg, posp, fnump)
16831 typval_T *arg;
16832 pos_T *posp;
16833 int *fnump;
16834{
16835 list_T *l = arg->vval.v_list;
16836 long i = 0;
16837 long n;
16838
Bram Moolenaarbde35262006-07-23 20:12:24 +000016839 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16840 * when "fnump" isn't NULL and "coladd" is optional. */
16841 if (arg->v_type != VAR_LIST
16842 || l == NULL
16843 || l->lv_len < (fnump == NULL ? 2 : 3)
16844 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016845 return FAIL;
16846
16847 if (fnump != NULL)
16848 {
16849 n = list_find_nr(l, i++, NULL); /* fnum */
16850 if (n < 0)
16851 return FAIL;
16852 if (n == 0)
16853 n = curbuf->b_fnum; /* current buffer */
16854 *fnump = n;
16855 }
16856
16857 n = list_find_nr(l, i++, NULL); /* lnum */
16858 if (n < 0)
16859 return FAIL;
16860 posp->lnum = n;
16861
16862 n = list_find_nr(l, i++, NULL); /* col */
16863 if (n < 0)
16864 return FAIL;
16865 posp->col = n;
16866
16867#ifdef FEAT_VIRTUALEDIT
16868 n = list_find_nr(l, i, NULL);
16869 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016870 posp->coladd = 0;
16871 else
16872 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016873#endif
16874
16875 return OK;
16876}
16877
16878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879 * Get the length of an environment variable name.
16880 * Advance "arg" to the first character after the name.
16881 * Return 0 for error.
16882 */
16883 static int
16884get_env_len(arg)
16885 char_u **arg;
16886{
16887 char_u *p;
16888 int len;
16889
16890 for (p = *arg; vim_isIDc(*p); ++p)
16891 ;
16892 if (p == *arg) /* no name found */
16893 return 0;
16894
16895 len = (int)(p - *arg);
16896 *arg = p;
16897 return len;
16898}
16899
16900/*
16901 * Get the length of the name of a function or internal variable.
16902 * "arg" is advanced to the first non-white character after the name.
16903 * Return 0 if something is wrong.
16904 */
16905 static int
16906get_id_len(arg)
16907 char_u **arg;
16908{
16909 char_u *p;
16910 int len;
16911
16912 /* Find the end of the name. */
16913 for (p = *arg; eval_isnamec(*p); ++p)
16914 ;
16915 if (p == *arg) /* no name found */
16916 return 0;
16917
16918 len = (int)(p - *arg);
16919 *arg = skipwhite(p);
16920
16921 return len;
16922}
16923
16924/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016925 * Get the length of the name of a variable or function.
16926 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016928 * Return -1 if curly braces expansion failed.
16929 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 * If the name contains 'magic' {}'s, expand them and return the
16931 * expanded name in an allocated string via 'alias' - caller must free.
16932 */
16933 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016934get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 char_u **arg;
16936 char_u **alias;
16937 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016938 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939{
16940 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941 char_u *p;
16942 char_u *expr_start;
16943 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944
16945 *alias = NULL; /* default to no alias */
16946
16947 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16948 && (*arg)[2] == (int)KE_SNR)
16949 {
16950 /* hard coded <SNR>, already translated */
16951 *arg += 3;
16952 return get_id_len(arg) + 3;
16953 }
16954 len = eval_fname_script(*arg);
16955 if (len > 0)
16956 {
16957 /* literal "<SID>", "s:" or "<SNR>" */
16958 *arg += len;
16959 }
16960
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016962 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016964 p = find_name_end(*arg, &expr_start, &expr_end,
16965 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966 if (expr_start != NULL)
16967 {
16968 char_u *temp_string;
16969
16970 if (!evaluate)
16971 {
16972 len += (int)(p - *arg);
16973 *arg = skipwhite(p);
16974 return len;
16975 }
16976
16977 /*
16978 * Include any <SID> etc in the expanded string:
16979 * Thus the -len here.
16980 */
16981 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16982 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016983 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 *alias = temp_string;
16985 *arg = skipwhite(p);
16986 return (int)STRLEN(temp_string);
16987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988
16989 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016990 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 EMSG2(_(e_invexpr2), *arg);
16992
16993 return len;
16994}
16995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016996/*
16997 * Find the end of a variable or function name, taking care of magic braces.
16998 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16999 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017000 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017001 * Return a pointer to just after the name. Equal to "arg" if there is no
17002 * valid name.
17003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017005find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 char_u *arg;
17007 char_u **expr_start;
17008 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017009 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017011 int mb_nest = 0;
17012 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 char_u *p;
17014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017015 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017017 *expr_start = NULL;
17018 *expr_end = NULL;
17019 }
17020
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017021 /* Quick check for valid starting character. */
17022 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17023 return arg;
17024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017025 for (p = arg; *p != NUL
17026 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017027 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017028 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017029 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017030 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017031 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017032 if (*p == '\'')
17033 {
17034 /* skip over 'string' to avoid counting [ and ] inside it. */
17035 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17036 ;
17037 if (*p == NUL)
17038 break;
17039 }
17040 else if (*p == '"')
17041 {
17042 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17043 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17044 if (*p == '\\' && p[1] != NUL)
17045 ++p;
17046 if (*p == NUL)
17047 break;
17048 }
17049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017050 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017052 if (*p == '[')
17053 ++br_nest;
17054 else if (*p == ']')
17055 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017058 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017060 if (*p == '{')
17061 {
17062 mb_nest++;
17063 if (expr_start != NULL && *expr_start == NULL)
17064 *expr_start = p;
17065 }
17066 else if (*p == '}')
17067 {
17068 mb_nest--;
17069 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17070 *expr_end = p;
17071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017073 }
17074
17075 return p;
17076}
17077
17078/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017079 * Expands out the 'magic' {}'s in a variable/function name.
17080 * Note that this can call itself recursively, to deal with
17081 * constructs like foo{bar}{baz}{bam}
17082 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17083 * "in_start" ^
17084 * "expr_start" ^
17085 * "expr_end" ^
17086 * "in_end" ^
17087 *
17088 * Returns a new allocated string, which the caller must free.
17089 * Returns NULL for failure.
17090 */
17091 static char_u *
17092make_expanded_name(in_start, expr_start, expr_end, in_end)
17093 char_u *in_start;
17094 char_u *expr_start;
17095 char_u *expr_end;
17096 char_u *in_end;
17097{
17098 char_u c1;
17099 char_u *retval = NULL;
17100 char_u *temp_result;
17101 char_u *nextcmd = NULL;
17102
17103 if (expr_end == NULL || in_end == NULL)
17104 return NULL;
17105 *expr_start = NUL;
17106 *expr_end = NUL;
17107 c1 = *in_end;
17108 *in_end = NUL;
17109
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017110 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017111 if (temp_result != NULL && nextcmd == NULL)
17112 {
17113 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17114 + (in_end - expr_end) + 1));
17115 if (retval != NULL)
17116 {
17117 STRCPY(retval, in_start);
17118 STRCAT(retval, temp_result);
17119 STRCAT(retval, expr_end + 1);
17120 }
17121 }
17122 vim_free(temp_result);
17123
17124 *in_end = c1; /* put char back for error messages */
17125 *expr_start = '{';
17126 *expr_end = '}';
17127
17128 if (retval != NULL)
17129 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017130 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017131 if (expr_start != NULL)
17132 {
17133 /* Further expansion! */
17134 temp_result = make_expanded_name(retval, expr_start,
17135 expr_end, temp_result);
17136 vim_free(retval);
17137 retval = temp_result;
17138 }
17139 }
17140
17141 return retval;
17142}
17143
17144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017146 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147 */
17148 static int
17149eval_isnamec(c)
17150 int c;
17151{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017152 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
17153}
17154
17155/*
17156 * Return TRUE if character "c" can be used as the first character in a
17157 * variable or function name (excluding '{' and '}').
17158 */
17159 static int
17160eval_isnamec1(c)
17161 int c;
17162{
17163 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164}
17165
17166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 * Set number v: variable to "val".
17168 */
17169 void
17170set_vim_var_nr(idx, val)
17171 int idx;
17172 long val;
17173{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017174 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175}
17176
17177/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017178 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 */
17180 long
17181get_vim_var_nr(idx)
17182 int idx;
17183{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017184 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185}
17186
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017187#if defined(FEAT_AUTOCMD) || defined(PROTO)
17188/*
17189 * Get string v: variable value. Uses a static buffer, can only be used once.
17190 */
17191 char_u *
17192get_vim_var_str(idx)
17193 int idx;
17194{
17195 return get_tv_string(&vimvars[idx].vv_tv);
17196}
17197#endif
17198
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199/*
17200 * Set v:count, v:count1 and v:prevcount.
17201 */
17202 void
17203set_vcount(count, count1)
17204 long count;
17205 long count1;
17206{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017207 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
17208 vimvars[VV_COUNT].vv_nr = count;
17209 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210}
17211
17212/*
17213 * Set string v: variable to a copy of "val".
17214 */
17215 void
17216set_vim_var_string(idx, val, len)
17217 int idx;
17218 char_u *val;
17219 int len; /* length of "val" to use or -1 (whole string) */
17220{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017221 /* Need to do this (at least) once, since we can't initialize a union.
17222 * Will always be invoked when "v:progname" is set. */
17223 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17224
Bram Moolenaare9a41262005-01-15 22:18:47 +000017225 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017227 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017229 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017231 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232}
17233
17234/*
17235 * Set v:register if needed.
17236 */
17237 void
17238set_reg_var(c)
17239 int c;
17240{
17241 char_u regname;
17242
17243 if (c == 0 || c == ' ')
17244 regname = '"';
17245 else
17246 regname = c;
17247 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017248 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 set_vim_var_string(VV_REG, &regname, 1);
17250}
17251
17252/*
17253 * Get or set v:exception. If "oldval" == NULL, return the current value.
17254 * Otherwise, restore the value to "oldval" and return NULL.
17255 * Must always be called in pairs to save and restore v:exception! Does not
17256 * take care of memory allocations.
17257 */
17258 char_u *
17259v_exception(oldval)
17260 char_u *oldval;
17261{
17262 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017263 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264
Bram Moolenaare9a41262005-01-15 22:18:47 +000017265 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 return NULL;
17267}
17268
17269/*
17270 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17271 * Otherwise, restore the value to "oldval" and return NULL.
17272 * Must always be called in pairs to save and restore v:throwpoint! Does not
17273 * take care of memory allocations.
17274 */
17275 char_u *
17276v_throwpoint(oldval)
17277 char_u *oldval;
17278{
17279 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017280 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281
Bram Moolenaare9a41262005-01-15 22:18:47 +000017282 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283 return NULL;
17284}
17285
17286#if defined(FEAT_AUTOCMD) || defined(PROTO)
17287/*
17288 * Set v:cmdarg.
17289 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17290 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17291 * Must always be called in pairs!
17292 */
17293 char_u *
17294set_cmdarg(eap, oldarg)
17295 exarg_T *eap;
17296 char_u *oldarg;
17297{
17298 char_u *oldval;
17299 char_u *newval;
17300 unsigned len;
17301
Bram Moolenaare9a41262005-01-15 22:18:47 +000017302 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017303 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017305 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017306 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017307 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308 }
17309
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017310 if (eap->force_bin == FORCE_BIN)
17311 len = 6;
17312 else if (eap->force_bin == FORCE_NOBIN)
17313 len = 8;
17314 else
17315 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017316
17317 if (eap->read_edit)
17318 len += 7;
17319
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017320 if (eap->force_ff != 0)
17321 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17322# ifdef FEAT_MBYTE
17323 if (eap->force_enc != 0)
17324 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017325 if (eap->bad_char != 0)
17326 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017327# endif
17328
17329 newval = alloc(len + 1);
17330 if (newval == NULL)
17331 return NULL;
17332
17333 if (eap->force_bin == FORCE_BIN)
17334 sprintf((char *)newval, " ++bin");
17335 else if (eap->force_bin == FORCE_NOBIN)
17336 sprintf((char *)newval, " ++nobin");
17337 else
17338 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017339
17340 if (eap->read_edit)
17341 STRCAT(newval, " ++edit");
17342
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017343 if (eap->force_ff != 0)
17344 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17345 eap->cmd + eap->force_ff);
17346# ifdef FEAT_MBYTE
17347 if (eap->force_enc != 0)
17348 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17349 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017350 if (eap->bad_char != 0)
17351 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17352 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017353# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017354 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017355 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356}
17357#endif
17358
17359/*
17360 * Get the value of internal variable "name".
17361 * Return OK or FAIL.
17362 */
17363 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017364get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365 char_u *name;
17366 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017367 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017368 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369{
17370 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017371 typval_T *tv = NULL;
17372 typval_T atv;
17373 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375
17376 /* truncate the name, so that we can use strcmp() */
17377 cc = name[len];
17378 name[len] = NUL;
17379
17380 /*
17381 * Check for "b:changedtick".
17382 */
17383 if (STRCMP(name, "b:changedtick") == 0)
17384 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017385 atv.v_type = VAR_NUMBER;
17386 atv.vval.v_number = curbuf->b_changedtick;
17387 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 }
17389
17390 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391 * Check for user-defined variables.
17392 */
17393 else
17394 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017395 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017397 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398 }
17399
Bram Moolenaare9a41262005-01-15 22:18:47 +000017400 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017402 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017403 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 ret = FAIL;
17405 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017406 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017407 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408
17409 name[len] = cc;
17410
17411 return ret;
17412}
17413
17414/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017415 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17416 * Also handle function call with Funcref variable: func(expr)
17417 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17418 */
17419 static int
17420handle_subscript(arg, rettv, evaluate, verbose)
17421 char_u **arg;
17422 typval_T *rettv;
17423 int evaluate; /* do more than finding the end */
17424 int verbose; /* give error messages */
17425{
17426 int ret = OK;
17427 dict_T *selfdict = NULL;
17428 char_u *s;
17429 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017430 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017431
17432 while (ret == OK
17433 && (**arg == '['
17434 || (**arg == '.' && rettv->v_type == VAR_DICT)
17435 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17436 && !vim_iswhite(*(*arg - 1)))
17437 {
17438 if (**arg == '(')
17439 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017440 /* need to copy the funcref so that we can clear rettv */
17441 functv = *rettv;
17442 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017443
17444 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017445 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017446 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017447 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17448 &len, evaluate, selfdict);
17449
17450 /* Clear the funcref afterwards, so that deleting it while
17451 * evaluating the arguments is possible (see test55). */
17452 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017453
17454 /* Stop the expression evaluation when immediately aborting on
17455 * error, or when an interrupt occurred or an exception was thrown
17456 * but not caught. */
17457 if (aborting())
17458 {
17459 if (ret == OK)
17460 clear_tv(rettv);
17461 ret = FAIL;
17462 }
17463 dict_unref(selfdict);
17464 selfdict = NULL;
17465 }
17466 else /* **arg == '[' || **arg == '.' */
17467 {
17468 dict_unref(selfdict);
17469 if (rettv->v_type == VAR_DICT)
17470 {
17471 selfdict = rettv->vval.v_dict;
17472 if (selfdict != NULL)
17473 ++selfdict->dv_refcount;
17474 }
17475 else
17476 selfdict = NULL;
17477 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17478 {
17479 clear_tv(rettv);
17480 ret = FAIL;
17481 }
17482 }
17483 }
17484 dict_unref(selfdict);
17485 return ret;
17486}
17487
17488/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017489 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17490 * value).
17491 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017492 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017493alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017494{
Bram Moolenaar33570922005-01-25 22:26:29 +000017495 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017496}
17497
17498/*
17499 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 * The string "s" must have been allocated, it is consumed.
17501 * Return NULL for out of memory, the variable otherwise.
17502 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017503 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017504alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 char_u *s;
17506{
Bram Moolenaar33570922005-01-25 22:26:29 +000017507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017509 rettv = alloc_tv();
17510 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017512 rettv->v_type = VAR_STRING;
17513 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 }
17515 else
17516 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017517 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518}
17519
17520/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017521 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017523 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017524free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017525 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526{
17527 if (varp != NULL)
17528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017529 switch (varp->v_type)
17530 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017531 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017532 func_unref(varp->vval.v_string);
17533 /*FALLTHROUGH*/
17534 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017535 vim_free(varp->vval.v_string);
17536 break;
17537 case VAR_LIST:
17538 list_unref(varp->vval.v_list);
17539 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017540 case VAR_DICT:
17541 dict_unref(varp->vval.v_dict);
17542 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017543 case VAR_NUMBER:
17544 case VAR_UNKNOWN:
17545 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017546 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017547 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017548 break;
17549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550 vim_free(varp);
17551 }
17552}
17553
17554/*
17555 * Free the memory for a variable value and set the value to NULL or 0.
17556 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017557 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017558clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017559 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560{
17561 if (varp != NULL)
17562 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017563 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017565 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017566 func_unref(varp->vval.v_string);
17567 /*FALLTHROUGH*/
17568 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017569 vim_free(varp->vval.v_string);
17570 varp->vval.v_string = NULL;
17571 break;
17572 case VAR_LIST:
17573 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017574 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017575 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017576 case VAR_DICT:
17577 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017578 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017579 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017580 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017581 varp->vval.v_number = 0;
17582 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017583 case VAR_UNKNOWN:
17584 break;
17585 default:
17586 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017588 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 }
17590}
17591
17592/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017593 * Set the value of a variable to NULL without freeing items.
17594 */
17595 static void
17596init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017597 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017598{
17599 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017600 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017601}
17602
17603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604 * Get the number value of a variable.
17605 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017606 * For incompatible types, return 0.
17607 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17608 * caller of incompatible types: it sets *denote to TRUE if "denote"
17609 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 */
17611 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017612get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017613 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017615 int error = FALSE;
17616
17617 return get_tv_number_chk(varp, &error); /* return 0L on error */
17618}
17619
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017620 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017621get_tv_number_chk(varp, denote)
17622 typval_T *varp;
17623 int *denote;
17624{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017625 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017627 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017629 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017630 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017631 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017632 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017633 break;
17634 case VAR_STRING:
17635 if (varp->vval.v_string != NULL)
17636 vim_str2nr(varp->vval.v_string, NULL, NULL,
17637 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017638 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017639 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017640 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017641 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017642 case VAR_DICT:
17643 EMSG(_("E728: Using a Dictionary as a number"));
17644 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017645 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017646 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017647 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017649 if (denote == NULL) /* useful for values that must be unsigned */
17650 n = -1;
17651 else
17652 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017653 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654}
17655
17656/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017657 * Get the lnum from the first argument.
17658 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017659 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 */
17661 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017662get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017663 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664{
Bram Moolenaar33570922005-01-25 22:26:29 +000017665 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 linenr_T lnum;
17667
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017668 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 if (lnum == 0) /* no valid number, try using line() */
17670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017671 rettv.v_type = VAR_NUMBER;
17672 f_line(argvars, &rettv);
17673 lnum = rettv.vval.v_number;
17674 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 }
17676 return lnum;
17677}
17678
17679/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017680 * Get the lnum from the first argument.
17681 * Also accepts "$", then "buf" is used.
17682 * Returns 0 on error.
17683 */
17684 static linenr_T
17685get_tv_lnum_buf(argvars, buf)
17686 typval_T *argvars;
17687 buf_T *buf;
17688{
17689 if (argvars[0].v_type == VAR_STRING
17690 && argvars[0].vval.v_string != NULL
17691 && argvars[0].vval.v_string[0] == '$'
17692 && buf != NULL)
17693 return buf->b_ml.ml_line_count;
17694 return get_tv_number_chk(&argvars[0], NULL);
17695}
17696
17697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698 * Get the string value of a variable.
17699 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017700 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17701 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702 * If the String variable has never been set, return an empty string.
17703 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017704 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17705 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 */
17707 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017708get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017709 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710{
17711 static char_u mybuf[NUMBUFLEN];
17712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714}
17715
17716 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017717get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017718 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017719 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017721 char_u *res = get_tv_string_buf_chk(varp, buf);
17722
17723 return res != NULL ? res : (char_u *)"";
17724}
17725
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017726 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017727get_tv_string_chk(varp)
17728 typval_T *varp;
17729{
17730 static char_u mybuf[NUMBUFLEN];
17731
17732 return get_tv_string_buf_chk(varp, mybuf);
17733}
17734
17735 static char_u *
17736get_tv_string_buf_chk(varp, buf)
17737 typval_T *varp;
17738 char_u *buf;
17739{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017740 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017742 case VAR_NUMBER:
17743 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17744 return buf;
17745 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017746 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017747 break;
17748 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017749 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017750 break;
17751 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017752 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017753 break;
17754 case VAR_STRING:
17755 if (varp->vval.v_string != NULL)
17756 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017757 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017758 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017759 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017760 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017762 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763}
17764
17765/*
17766 * Find variable "name" in the list of variables.
17767 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017768 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017769 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017770 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017772 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017773find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017775 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017778 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779
Bram Moolenaara7043832005-01-21 11:56:39 +000017780 ht = find_var_ht(name, &varname);
17781 if (htp != NULL)
17782 *htp = ht;
17783 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017785 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786}
17787
17788/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017789 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017790 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017792 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017793find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017794 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017795 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017796 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017797{
Bram Moolenaar33570922005-01-25 22:26:29 +000017798 hashitem_T *hi;
17799
17800 if (*varname == NUL)
17801 {
17802 /* Must be something like "s:", otherwise "ht" would be NULL. */
17803 switch (varname[-2])
17804 {
17805 case 's': return &SCRIPT_SV(current_SID).sv_var;
17806 case 'g': return &globvars_var;
17807 case 'v': return &vimvars_var;
17808 case 'b': return &curbuf->b_bufvar;
17809 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017810#ifdef FEAT_WINDOWS
17811 case 't': return &curtab->tp_winvar;
17812#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017813 case 'l': return current_funccal == NULL
17814 ? NULL : &current_funccal->l_vars_var;
17815 case 'a': return current_funccal == NULL
17816 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017817 }
17818 return NULL;
17819 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017820
17821 hi = hash_find(ht, varname);
17822 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017823 {
17824 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017825 * worked find the variable again. Don't auto-load a script if it was
17826 * loaded already, otherwise it would be loaded every time when
17827 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017828 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017829 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017830 hi = hash_find(ht, varname);
17831 if (HASHITEM_EMPTY(hi))
17832 return NULL;
17833 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017834 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017835}
17836
17837/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017838 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017839 * Set "varname" to the start of name without ':'.
17840 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017841 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017842find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843 char_u *name;
17844 char_u **varname;
17845{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017846 hashitem_T *hi;
17847
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848 if (name[1] != ':')
17849 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017850 /* The name must not start with a colon or #. */
17851 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 return NULL;
17853 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017854
17855 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017856 hi = hash_find(&compat_hashtab, name);
17857 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017858 return &compat_hashtab;
17859
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017861 return &globvarht; /* global variable */
17862 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863 }
17864 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017865 if (*name == 'g') /* global variable */
17866 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017867 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17868 */
17869 if (vim_strchr(name + 2, ':') != NULL
17870 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017871 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017873 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017875 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017876#ifdef FEAT_WINDOWS
17877 if (*name == 't') /* tab page variable */
17878 return &curtab->tp_vars.dv_hashtab;
17879#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017880 if (*name == 'v') /* v: variable */
17881 return &vimvarht;
17882 if (*name == 'a' && current_funccal != NULL) /* function argument */
17883 return &current_funccal->l_avars.dv_hashtab;
17884 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17885 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 if (*name == 's' /* script variable */
17887 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17888 return &SCRIPT_VARS(current_SID);
17889 return NULL;
17890}
17891
17892/*
17893 * Get the string value of a (global/local) variable.
17894 * Returns NULL when it doesn't exist.
17895 */
17896 char_u *
17897get_var_value(name)
17898 char_u *name;
17899{
Bram Moolenaar33570922005-01-25 22:26:29 +000017900 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901
Bram Moolenaara7043832005-01-21 11:56:39 +000017902 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 if (v == NULL)
17904 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017905 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906}
17907
17908/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017909 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910 * sourcing this script and when executing functions defined in the script.
17911 */
17912 void
17913new_script_vars(id)
17914 scid_T id;
17915{
Bram Moolenaara7043832005-01-21 11:56:39 +000017916 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017917 hashtab_T *ht;
17918 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017919
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17921 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017922 /* Re-allocating ga_data means that an ht_array pointing to
17923 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017924 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017925 for (i = 1; i <= ga_scripts.ga_len; ++i)
17926 {
17927 ht = &SCRIPT_VARS(i);
17928 if (ht->ht_mask == HT_INIT_SIZE - 1)
17929 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017930 sv = &SCRIPT_SV(i);
17931 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017932 }
17933
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 while (ga_scripts.ga_len < id)
17935 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017936 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17937 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 }
17940 }
17941}
17942
17943/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017944 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17945 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946 */
17947 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017948init_var_dict(dict, dict_var)
17949 dict_T *dict;
17950 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951{
Bram Moolenaar33570922005-01-25 22:26:29 +000017952 hash_init(&dict->dv_hashtab);
17953 dict->dv_refcount = 99999;
17954 dict_var->di_tv.vval.v_dict = dict;
17955 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017956 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017957 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17958 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959}
17960
17961/*
17962 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017963 * Frees all allocated variables and the value they contain.
17964 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017965 */
17966 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017967vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017968 hashtab_T *ht;
17969{
17970 vars_clear_ext(ht, TRUE);
17971}
17972
17973/*
17974 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17975 */
17976 static void
17977vars_clear_ext(ht, free_val)
17978 hashtab_T *ht;
17979 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980{
Bram Moolenaara7043832005-01-21 11:56:39 +000017981 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017982 hashitem_T *hi;
17983 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017984
Bram Moolenaar33570922005-01-25 22:26:29 +000017985 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017986 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017987 for (hi = ht->ht_array; todo > 0; ++hi)
17988 {
17989 if (!HASHITEM_EMPTY(hi))
17990 {
17991 --todo;
17992
Bram Moolenaar33570922005-01-25 22:26:29 +000017993 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017994 * ht_array might change then. hash_clear() takes care of it
17995 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017996 v = HI2DI(hi);
17997 if (free_val)
17998 clear_tv(&v->di_tv);
17999 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18000 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018001 }
18002 }
18003 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018004 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005}
18006
Bram Moolenaara7043832005-01-21 11:56:39 +000018007/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018008 * Delete a variable from hashtab "ht" at item "hi".
18009 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018012delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018013 hashtab_T *ht;
18014 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015{
Bram Moolenaar33570922005-01-25 22:26:29 +000018016 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018017
18018 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018019 clear_tv(&di->di_tv);
18020 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021}
18022
18023/*
18024 * List the value of one internal variable.
18025 */
18026 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018027list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018028 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018030 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018032 char_u *tofree;
18033 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018034 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018035
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018036 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018037 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018038 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018039 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040}
18041
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018043list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 char_u *prefix;
18045 char_u *name;
18046 int type;
18047 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018048 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049{
Bram Moolenaar31859182007-08-14 20:41:13 +000018050 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18051 msg_start();
18052 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 if (name != NULL) /* "a:" vars don't have a name stored */
18054 msg_puts(name);
18055 msg_putchar(' ');
18056 msg_advance(22);
18057 if (type == VAR_NUMBER)
18058 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018059 else if (type == VAR_FUNC)
18060 msg_putchar('*');
18061 else if (type == VAR_LIST)
18062 {
18063 msg_putchar('[');
18064 if (*string == '[')
18065 ++string;
18066 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018067 else if (type == VAR_DICT)
18068 {
18069 msg_putchar('{');
18070 if (*string == '{')
18071 ++string;
18072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073 else
18074 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018075
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018077
18078 if (type == VAR_FUNC)
18079 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018080 if (*first)
18081 {
18082 msg_clr_eos();
18083 *first = FALSE;
18084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085}
18086
18087/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018088 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 * If the variable already exists, the value is updated.
18090 * Otherwise the variable is created.
18091 */
18092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018093set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018095 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018096 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097{
Bram Moolenaar33570922005-01-25 22:26:29 +000018098 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018100 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018101 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018103 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018104 {
18105 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18106 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18107 ? name[2] : name[0]))
18108 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018109 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018110 return;
18111 }
18112 if (function_exists(name))
18113 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018114 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018115 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018116 return;
18117 }
18118 }
18119
Bram Moolenaara7043832005-01-21 11:56:39 +000018120 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018121 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000018122 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000018123 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000018124 return;
18125 }
18126
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018127 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000018128 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018130 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018131 if (var_check_ro(v->di_flags, name)
18132 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000018133 return;
18134 if (v->di_tv.v_type != tv->v_type
18135 && !((v->di_tv.v_type == VAR_STRING
18136 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018137 && (tv->v_type == VAR_STRING
18138 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018139 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018140 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018141 return;
18142 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018143
18144 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000018145 * Handle setting internal v: variables separately: we don't change
18146 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000018147 */
18148 if (ht == &vimvarht)
18149 {
18150 if (v->di_tv.v_type == VAR_STRING)
18151 {
18152 vim_free(v->di_tv.vval.v_string);
18153 if (copy || tv->v_type != VAR_STRING)
18154 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
18155 else
18156 {
18157 /* Take over the string to avoid an extra alloc/free. */
18158 v->di_tv.vval.v_string = tv->vval.v_string;
18159 tv->vval.v_string = NULL;
18160 }
18161 }
18162 else if (v->di_tv.v_type != VAR_NUMBER)
18163 EMSG2(_(e_intern2), "set_var()");
18164 else
18165 v->di_tv.vval.v_number = get_tv_number(tv);
18166 return;
18167 }
18168
18169 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 }
18171 else /* add a new variable */
18172 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000018173 /* Can't add "v:" variable. */
18174 if (ht == &vimvarht)
18175 {
18176 EMSG2(_(e_illvar), name);
18177 return;
18178 }
18179
Bram Moolenaar92124a32005-06-17 22:03:40 +000018180 /* Make sure the variable name is valid. */
18181 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000018182 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
18183 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000018184 {
18185 EMSG2(_(e_illvar), varname);
18186 return;
18187 }
18188
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018189 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18190 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000018191 if (v == NULL)
18192 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000018193 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018194 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018196 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197 return;
18198 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018199 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018202 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000018203 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018204 else
18205 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018206 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018207 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018208 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210}
18211
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018212/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018213 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000018214 * Also give an error message.
18215 */
18216 static int
18217var_check_ro(flags, name)
18218 int flags;
18219 char_u *name;
18220{
18221 if (flags & DI_FLAGS_RO)
18222 {
18223 EMSG2(_(e_readonlyvar), name);
18224 return TRUE;
18225 }
18226 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18227 {
18228 EMSG2(_(e_readonlysbx), name);
18229 return TRUE;
18230 }
18231 return FALSE;
18232}
18233
18234/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018235 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18236 * Also give an error message.
18237 */
18238 static int
18239var_check_fixed(flags, name)
18240 int flags;
18241 char_u *name;
18242{
18243 if (flags & DI_FLAGS_FIX)
18244 {
18245 EMSG2(_("E795: Cannot delete variable %s"), name);
18246 return TRUE;
18247 }
18248 return FALSE;
18249}
18250
18251/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018252 * Return TRUE if typeval "tv" is set to be locked (immutable).
18253 * Also give an error message, using "name".
18254 */
18255 static int
18256tv_check_lock(lock, name)
18257 int lock;
18258 char_u *name;
18259{
18260 if (lock & VAR_LOCKED)
18261 {
18262 EMSG2(_("E741: Value is locked: %s"),
18263 name == NULL ? (char_u *)_("Unknown") : name);
18264 return TRUE;
18265 }
18266 if (lock & VAR_FIXED)
18267 {
18268 EMSG2(_("E742: Cannot change value of %s"),
18269 name == NULL ? (char_u *)_("Unknown") : name);
18270 return TRUE;
18271 }
18272 return FALSE;
18273}
18274
18275/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018276 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018277 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018278 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018281copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018282 typval_T *from;
18283 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018285 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018286 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018287 switch (from->v_type)
18288 {
18289 case VAR_NUMBER:
18290 to->vval.v_number = from->vval.v_number;
18291 break;
18292 case VAR_STRING:
18293 case VAR_FUNC:
18294 if (from->vval.v_string == NULL)
18295 to->vval.v_string = NULL;
18296 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018297 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018298 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018299 if (from->v_type == VAR_FUNC)
18300 func_ref(to->vval.v_string);
18301 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018302 break;
18303 case VAR_LIST:
18304 if (from->vval.v_list == NULL)
18305 to->vval.v_list = NULL;
18306 else
18307 {
18308 to->vval.v_list = from->vval.v_list;
18309 ++to->vval.v_list->lv_refcount;
18310 }
18311 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018312 case VAR_DICT:
18313 if (from->vval.v_dict == NULL)
18314 to->vval.v_dict = NULL;
18315 else
18316 {
18317 to->vval.v_dict = from->vval.v_dict;
18318 ++to->vval.v_dict->dv_refcount;
18319 }
18320 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018321 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018322 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018323 break;
18324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325}
18326
18327/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018328 * Make a copy of an item.
18329 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018330 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18331 * reference to an already copied list/dict can be used.
18332 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018333 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018334 static int
18335item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018336 typval_T *from;
18337 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018338 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018339 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018340{
18341 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018342 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018343
Bram Moolenaar33570922005-01-25 22:26:29 +000018344 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018345 {
18346 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018347 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018348 }
18349 ++recurse;
18350
18351 switch (from->v_type)
18352 {
18353 case VAR_NUMBER:
18354 case VAR_STRING:
18355 case VAR_FUNC:
18356 copy_tv(from, to);
18357 break;
18358 case VAR_LIST:
18359 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018360 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018361 if (from->vval.v_list == NULL)
18362 to->vval.v_list = NULL;
18363 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18364 {
18365 /* use the copy made earlier */
18366 to->vval.v_list = from->vval.v_list->lv_copylist;
18367 ++to->vval.v_list->lv_refcount;
18368 }
18369 else
18370 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18371 if (to->vval.v_list == NULL)
18372 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018373 break;
18374 case VAR_DICT:
18375 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018376 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018377 if (from->vval.v_dict == NULL)
18378 to->vval.v_dict = NULL;
18379 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18380 {
18381 /* use the copy made earlier */
18382 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18383 ++to->vval.v_dict->dv_refcount;
18384 }
18385 else
18386 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18387 if (to->vval.v_dict == NULL)
18388 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018389 break;
18390 default:
18391 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018392 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018393 }
18394 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018395 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018396}
18397
18398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399 * ":echo expr1 ..." print each argument separated with a space, add a
18400 * newline at the end.
18401 * ":echon expr1 ..." print each argument plain.
18402 */
18403 void
18404ex_echo(eap)
18405 exarg_T *eap;
18406{
18407 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018408 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018409 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018410 char_u *p;
18411 int needclr = TRUE;
18412 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018413 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414
18415 if (eap->skip)
18416 ++emsg_skip;
18417 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18418 {
18419 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018420 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 {
18422 /*
18423 * Report the invalid expression unless the expression evaluation
18424 * has been cancelled due to an aborting error, an interrupt, or an
18425 * exception.
18426 */
18427 if (!aborting())
18428 EMSG2(_(e_invexpr2), p);
18429 break;
18430 }
18431 if (!eap->skip)
18432 {
18433 if (atstart)
18434 {
18435 atstart = FALSE;
18436 /* Call msg_start() after eval1(), evaluating the expression
18437 * may cause a message to appear. */
18438 if (eap->cmdidx == CMD_echo)
18439 msg_start();
18440 }
18441 else if (eap->cmdidx == CMD_echo)
18442 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018443 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018444 if (p != NULL)
18445 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018447 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018449 if (*p != TAB && needclr)
18450 {
18451 /* remove any text still there from the command */
18452 msg_clr_eos();
18453 needclr = FALSE;
18454 }
18455 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456 }
18457 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018458 {
18459#ifdef FEAT_MBYTE
18460 if (has_mbyte)
18461 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018462 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018463
18464 (void)msg_outtrans_len_attr(p, i, echo_attr);
18465 p += i - 1;
18466 }
18467 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018469 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018471 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018472 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018474 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 arg = skipwhite(arg);
18476 }
18477 eap->nextcmd = check_nextcmd(arg);
18478
18479 if (eap->skip)
18480 --emsg_skip;
18481 else
18482 {
18483 /* remove text that may still be there from the command */
18484 if (needclr)
18485 msg_clr_eos();
18486 if (eap->cmdidx == CMD_echo)
18487 msg_end();
18488 }
18489}
18490
18491/*
18492 * ":echohl {name}".
18493 */
18494 void
18495ex_echohl(eap)
18496 exarg_T *eap;
18497{
18498 int id;
18499
18500 id = syn_name2id(eap->arg);
18501 if (id == 0)
18502 echo_attr = 0;
18503 else
18504 echo_attr = syn_id2attr(id);
18505}
18506
18507/*
18508 * ":execute expr1 ..." execute the result of an expression.
18509 * ":echomsg expr1 ..." Print a message
18510 * ":echoerr expr1 ..." Print an error
18511 * Each gets spaces around each argument and a newline at the end for
18512 * echo commands
18513 */
18514 void
18515ex_execute(eap)
18516 exarg_T *eap;
18517{
18518 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018519 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520 int ret = OK;
18521 char_u *p;
18522 garray_T ga;
18523 int len;
18524 int save_did_emsg;
18525
18526 ga_init2(&ga, 1, 80);
18527
18528 if (eap->skip)
18529 ++emsg_skip;
18530 while (*arg != NUL && *arg != '|' && *arg != '\n')
18531 {
18532 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018533 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534 {
18535 /*
18536 * Report the invalid expression unless the expression evaluation
18537 * has been cancelled due to an aborting error, an interrupt, or an
18538 * exception.
18539 */
18540 if (!aborting())
18541 EMSG2(_(e_invexpr2), p);
18542 ret = FAIL;
18543 break;
18544 }
18545
18546 if (!eap->skip)
18547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018548 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 len = (int)STRLEN(p);
18550 if (ga_grow(&ga, len + 2) == FAIL)
18551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018552 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 ret = FAIL;
18554 break;
18555 }
18556 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 ga.ga_len += len;
18560 }
18561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018562 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 arg = skipwhite(arg);
18564 }
18565
18566 if (ret != FAIL && ga.ga_data != NULL)
18567 {
18568 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018571 out_flush();
18572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 else if (eap->cmdidx == CMD_echoerr)
18574 {
18575 /* We don't want to abort following commands, restore did_emsg. */
18576 save_did_emsg = did_emsg;
18577 EMSG((char_u *)ga.ga_data);
18578 if (!force_abort)
18579 did_emsg = save_did_emsg;
18580 }
18581 else if (eap->cmdidx == CMD_execute)
18582 do_cmdline((char_u *)ga.ga_data,
18583 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18584 }
18585
18586 ga_clear(&ga);
18587
18588 if (eap->skip)
18589 --emsg_skip;
18590
18591 eap->nextcmd = check_nextcmd(arg);
18592}
18593
18594/*
18595 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18596 * "arg" points to the "&" or '+' when called, to "option" when returning.
18597 * Returns NULL when no option name found. Otherwise pointer to the char
18598 * after the option name.
18599 */
18600 static char_u *
18601find_option_end(arg, opt_flags)
18602 char_u **arg;
18603 int *opt_flags;
18604{
18605 char_u *p = *arg;
18606
18607 ++p;
18608 if (*p == 'g' && p[1] == ':')
18609 {
18610 *opt_flags = OPT_GLOBAL;
18611 p += 2;
18612 }
18613 else if (*p == 'l' && p[1] == ':')
18614 {
18615 *opt_flags = OPT_LOCAL;
18616 p += 2;
18617 }
18618 else
18619 *opt_flags = 0;
18620
18621 if (!ASCII_ISALPHA(*p))
18622 return NULL;
18623 *arg = p;
18624
18625 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18626 p += 4; /* termcap option */
18627 else
18628 while (ASCII_ISALPHA(*p))
18629 ++p;
18630 return p;
18631}
18632
18633/*
18634 * ":function"
18635 */
18636 void
18637ex_function(eap)
18638 exarg_T *eap;
18639{
18640 char_u *theline;
18641 int j;
18642 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644 char_u *name = NULL;
18645 char_u *p;
18646 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018647 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648 garray_T newargs;
18649 garray_T newlines;
18650 int varargs = FALSE;
18651 int mustend = FALSE;
18652 int flags = 0;
18653 ufunc_T *fp;
18654 int indent;
18655 int nesting;
18656 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018657 dictitem_T *v;
18658 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018659 static int func_nr = 0; /* number for nameless function */
18660 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018661 hashtab_T *ht;
18662 int todo;
18663 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018664 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665
18666 /*
18667 * ":function" without argument: list functions.
18668 */
18669 if (ends_excmd(*eap->arg))
18670 {
18671 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018672 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018673 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018674 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018675 {
18676 if (!HASHITEM_EMPTY(hi))
18677 {
18678 --todo;
18679 fp = HI2UF(hi);
18680 if (!isdigit(*fp->uf_name))
18681 list_func_head(fp, FALSE);
18682 }
18683 }
18684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 eap->nextcmd = check_nextcmd(eap->arg);
18686 return;
18687 }
18688
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018689 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018690 * ":function /pat": list functions matching pattern.
18691 */
18692 if (*eap->arg == '/')
18693 {
18694 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18695 if (!eap->skip)
18696 {
18697 regmatch_T regmatch;
18698
18699 c = *p;
18700 *p = NUL;
18701 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18702 *p = c;
18703 if (regmatch.regprog != NULL)
18704 {
18705 regmatch.rm_ic = p_ic;
18706
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018707 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018708 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18709 {
18710 if (!HASHITEM_EMPTY(hi))
18711 {
18712 --todo;
18713 fp = HI2UF(hi);
18714 if (!isdigit(*fp->uf_name)
18715 && vim_regexec(&regmatch, fp->uf_name, 0))
18716 list_func_head(fp, FALSE);
18717 }
18718 }
18719 }
18720 }
18721 if (*p == '/')
18722 ++p;
18723 eap->nextcmd = check_nextcmd(p);
18724 return;
18725 }
18726
18727 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018728 * Get the function name. There are these situations:
18729 * func normal function name
18730 * "name" == func, "fudi.fd_dict" == NULL
18731 * dict.func new dictionary entry
18732 * "name" == NULL, "fudi.fd_dict" set,
18733 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18734 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018735 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018736 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18737 * dict.func existing dict entry that's not a Funcref
18738 * "name" == NULL, "fudi.fd_dict" set,
18739 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018742 name = trans_function_name(&p, eap->skip, 0, &fudi);
18743 paren = (vim_strchr(p, '(') != NULL);
18744 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745 {
18746 /*
18747 * Return on an invalid expression in braces, unless the expression
18748 * evaluation has been cancelled due to an aborting error, an
18749 * interrupt, or an exception.
18750 */
18751 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018752 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018753 if (!eap->skip && fudi.fd_newkey != NULL)
18754 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018755 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 else
18759 eap->skip = TRUE;
18760 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018761
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 /* An error in a function call during evaluation of an expression in magic
18763 * braces should not cause the function not to be defined. */
18764 saved_did_emsg = did_emsg;
18765 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766
18767 /*
18768 * ":function func" with only function name: list function.
18769 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018770 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771 {
18772 if (!ends_excmd(*skipwhite(p)))
18773 {
18774 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018775 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776 }
18777 eap->nextcmd = check_nextcmd(p);
18778 if (eap->nextcmd != NULL)
18779 *p = NUL;
18780 if (!eap->skip && !got_int)
18781 {
18782 fp = find_func(name);
18783 if (fp != NULL)
18784 {
18785 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018786 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018788 if (FUNCLINE(fp, j) == NULL)
18789 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 msg_putchar('\n');
18791 msg_outnum((long)(j + 1));
18792 if (j < 9)
18793 msg_putchar(' ');
18794 if (j < 99)
18795 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018796 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 out_flush(); /* show a line at a time */
18798 ui_breakcheck();
18799 }
18800 if (!got_int)
18801 {
18802 msg_putchar('\n');
18803 msg_puts((char_u *)" endfunction");
18804 }
18805 }
18806 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018807 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018809 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810 }
18811
18812 /*
18813 * ":function name(arg1, arg2)" Define function.
18814 */
18815 p = skipwhite(p);
18816 if (*p != '(')
18817 {
18818 if (!eap->skip)
18819 {
18820 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018821 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822 }
18823 /* attempt to continue by skipping some text */
18824 if (vim_strchr(p, '(') != NULL)
18825 p = vim_strchr(p, '(');
18826 }
18827 p = skipwhite(p + 1);
18828
18829 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18830 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18831
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018832 if (!eap->skip)
18833 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018834 /* Check the name of the function. Unless it's a dictionary function
18835 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018836 if (name != NULL)
18837 arg = name;
18838 else
18839 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018840 if (arg != NULL && (fudi.fd_di == NULL
18841 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018842 {
18843 if (*arg == K_SPECIAL)
18844 j = 3;
18845 else
18846 j = 0;
18847 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18848 : eval_isnamec(arg[j])))
18849 ++j;
18850 if (arg[j] != NUL)
18851 emsg_funcname(_(e_invarg2), arg);
18852 }
18853 }
18854
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855 /*
18856 * Isolate the arguments: "arg1, arg2, ...)"
18857 */
18858 while (*p != ')')
18859 {
18860 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18861 {
18862 varargs = TRUE;
18863 p += 3;
18864 mustend = TRUE;
18865 }
18866 else
18867 {
18868 arg = p;
18869 while (ASCII_ISALNUM(*p) || *p == '_')
18870 ++p;
18871 if (arg == p || isdigit(*arg)
18872 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18873 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18874 {
18875 if (!eap->skip)
18876 EMSG2(_("E125: Illegal argument: %s"), arg);
18877 break;
18878 }
18879 if (ga_grow(&newargs, 1) == FAIL)
18880 goto erret;
18881 c = *p;
18882 *p = NUL;
18883 arg = vim_strsave(arg);
18884 if (arg == NULL)
18885 goto erret;
18886 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18887 *p = c;
18888 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889 if (*p == ',')
18890 ++p;
18891 else
18892 mustend = TRUE;
18893 }
18894 p = skipwhite(p);
18895 if (mustend && *p != ')')
18896 {
18897 if (!eap->skip)
18898 EMSG2(_(e_invarg2), eap->arg);
18899 break;
18900 }
18901 }
18902 ++p; /* skip the ')' */
18903
Bram Moolenaare9a41262005-01-15 22:18:47 +000018904 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 for (;;)
18906 {
18907 p = skipwhite(p);
18908 if (STRNCMP(p, "range", 5) == 0)
18909 {
18910 flags |= FC_RANGE;
18911 p += 5;
18912 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018913 else if (STRNCMP(p, "dict", 4) == 0)
18914 {
18915 flags |= FC_DICT;
18916 p += 4;
18917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 else if (STRNCMP(p, "abort", 5) == 0)
18919 {
18920 flags |= FC_ABORT;
18921 p += 5;
18922 }
18923 else
18924 break;
18925 }
18926
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018927 /* When there is a line break use what follows for the function body.
18928 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18929 if (*p == '\n')
18930 line_arg = p + 1;
18931 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932 EMSG(_(e_trailing));
18933
18934 /*
18935 * Read the body of the function, until ":endfunction" is found.
18936 */
18937 if (KeyTyped)
18938 {
18939 /* Check if the function already exists, don't let the user type the
18940 * whole function before telling him it doesn't work! For a script we
18941 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018942 if (!eap->skip && !eap->forceit)
18943 {
18944 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18945 EMSG(_(e_funcdict));
18946 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018947 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018950 if (!eap->skip && did_emsg)
18951 goto erret;
18952
Bram Moolenaar071d4272004-06-13 20:20:40 +000018953 msg_putchar('\n'); /* don't overwrite the function name */
18954 cmdline_row = msg_row;
18955 }
18956
18957 indent = 2;
18958 nesting = 0;
18959 for (;;)
18960 {
18961 msg_scroll = TRUE;
18962 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018963 sourcing_lnum_off = sourcing_lnum;
18964
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018965 if (line_arg != NULL)
18966 {
18967 /* Use eap->arg, split up in parts by line breaks. */
18968 theline = line_arg;
18969 p = vim_strchr(theline, '\n');
18970 if (p == NULL)
18971 line_arg += STRLEN(line_arg);
18972 else
18973 {
18974 *p = NUL;
18975 line_arg = p + 1;
18976 }
18977 }
18978 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 theline = getcmdline(':', 0L, indent);
18980 else
18981 theline = eap->getline(':', eap->cookie, indent);
18982 if (KeyTyped)
18983 lines_left = Rows - 1;
18984 if (theline == NULL)
18985 {
18986 EMSG(_("E126: Missing :endfunction"));
18987 goto erret;
18988 }
18989
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018990 /* Detect line continuation: sourcing_lnum increased more than one. */
18991 if (sourcing_lnum > sourcing_lnum_off + 1)
18992 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18993 else
18994 sourcing_lnum_off = 0;
18995
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 if (skip_until != NULL)
18997 {
18998 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18999 * don't check for ":endfunc". */
19000 if (STRCMP(theline, skip_until) == 0)
19001 {
19002 vim_free(skip_until);
19003 skip_until = NULL;
19004 }
19005 }
19006 else
19007 {
19008 /* skip ':' and blanks*/
19009 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19010 ;
19011
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019012 /* Check for "endfunction". */
19013 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019015 if (line_arg == NULL)
19016 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017 break;
19018 }
19019
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019020 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021 * at "end". */
19022 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19023 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019024 else if (STRNCMP(p, "if", 2) == 0
19025 || STRNCMP(p, "wh", 2) == 0
19026 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027 || STRNCMP(p, "try", 3) == 0)
19028 indent += 2;
19029
19030 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019031 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019033 if (*p == '!')
19034 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 p += eval_fname_script(p);
19036 if (ASCII_ISALPHA(*p))
19037 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019038 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039 if (*skipwhite(p) == '(')
19040 {
19041 ++nesting;
19042 indent += 2;
19043 }
19044 }
19045 }
19046
19047 /* Check for ":append" or ":insert". */
19048 p = skip_range(p, NULL);
19049 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19050 || (p[0] == 'i'
19051 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19052 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19053 skip_until = vim_strsave((char_u *)".");
19054
19055 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19056 arg = skipwhite(skiptowhite(p));
19057 if (arg[0] == '<' && arg[1] =='<'
19058 && ((p[0] == 'p' && p[1] == 'y'
19059 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19060 || (p[0] == 'p' && p[1] == 'e'
19061 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19062 || (p[0] == 't' && p[1] == 'c'
19063 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19064 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19065 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019066 || (p[0] == 'm' && p[1] == 'z'
19067 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068 ))
19069 {
19070 /* ":python <<" continues until a dot, like ":append" */
19071 p = skipwhite(arg + 2);
19072 if (*p == NUL)
19073 skip_until = vim_strsave((char_u *)".");
19074 else
19075 skip_until = vim_strsave(p);
19076 }
19077 }
19078
19079 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019080 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019081 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019082 if (line_arg == NULL)
19083 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019085 }
19086
19087 /* Copy the line to newly allocated memory. get_one_sourceline()
19088 * allocates 250 bytes per line, this saves 80% on average. The cost
19089 * is an extra alloc/free. */
19090 p = vim_strsave(theline);
19091 if (p != NULL)
19092 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019093 if (line_arg == NULL)
19094 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019095 theline = p;
19096 }
19097
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019098 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
19099
19100 /* Add NULL lines for continuation lines, so that the line count is
19101 * equal to the index in the growarray. */
19102 while (sourcing_lnum_off-- > 0)
19103 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019104
19105 /* Check for end of eap->arg. */
19106 if (line_arg != NULL && *line_arg == NUL)
19107 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108 }
19109
19110 /* Don't define the function when skipping commands or when an error was
19111 * detected. */
19112 if (eap->skip || did_emsg)
19113 goto erret;
19114
19115 /*
19116 * If there are no errors, add the function
19117 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019118 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019119 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019120 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000019121 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019122 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019123 emsg_funcname("E707: Function name conflicts with variable: %s",
19124 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019125 goto erret;
19126 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019127
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019128 fp = find_func(name);
19129 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019131 if (!eap->forceit)
19132 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019133 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019134 goto erret;
19135 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019136 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019137 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019138 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019139 name);
19140 goto erret;
19141 }
19142 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019143 ga_clear_strings(&(fp->uf_args));
19144 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019145 vim_free(name);
19146 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148 }
19149 else
19150 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019151 char numbuf[20];
19152
19153 fp = NULL;
19154 if (fudi.fd_newkey == NULL && !eap->forceit)
19155 {
19156 EMSG(_(e_funcdict));
19157 goto erret;
19158 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000019159 if (fudi.fd_di == NULL)
19160 {
19161 /* Can't add a function to a locked dictionary */
19162 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
19163 goto erret;
19164 }
19165 /* Can't change an existing function if it is locked */
19166 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
19167 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019168
19169 /* Give the function a sequential number. Can only be used with a
19170 * Funcref! */
19171 vim_free(name);
19172 sprintf(numbuf, "%d", ++func_nr);
19173 name = vim_strsave((char_u *)numbuf);
19174 if (name == NULL)
19175 goto erret;
19176 }
19177
19178 if (fp == NULL)
19179 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019180 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019181 {
19182 int slen, plen;
19183 char_u *scriptname;
19184
19185 /* Check that the autoload name matches the script name. */
19186 j = FAIL;
19187 if (sourcing_name != NULL)
19188 {
19189 scriptname = autoload_name(name);
19190 if (scriptname != NULL)
19191 {
19192 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019193 plen = (int)STRLEN(p);
19194 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019195 if (slen > plen && fnamecmp(p,
19196 sourcing_name + slen - plen) == 0)
19197 j = OK;
19198 vim_free(scriptname);
19199 }
19200 }
19201 if (j == FAIL)
19202 {
19203 EMSG2(_("E746: Function name does not match script file name: %s"), name);
19204 goto erret;
19205 }
19206 }
19207
19208 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 if (fp == NULL)
19210 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019211
19212 if (fudi.fd_dict != NULL)
19213 {
19214 if (fudi.fd_di == NULL)
19215 {
19216 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019217 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019218 if (fudi.fd_di == NULL)
19219 {
19220 vim_free(fp);
19221 goto erret;
19222 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019223 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
19224 {
19225 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000019226 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019227 goto erret;
19228 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019229 }
19230 else
19231 /* overwrite existing dict entry */
19232 clear_tv(&fudi.fd_di->di_tv);
19233 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019234 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019235 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019236 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019237
19238 /* behave like "dict" was used */
19239 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019240 }
19241
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019243 STRCPY(fp->uf_name, name);
19244 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019246 fp->uf_args = newargs;
19247 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019248#ifdef FEAT_PROFILE
19249 fp->uf_tml_count = NULL;
19250 fp->uf_tml_total = NULL;
19251 fp->uf_tml_self = NULL;
19252 fp->uf_profiling = FALSE;
19253 if (prof_def_func())
19254 func_do_profile(fp);
19255#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019256 fp->uf_varargs = varargs;
19257 fp->uf_flags = flags;
19258 fp->uf_calls = 0;
19259 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019260 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261
19262erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019263 ga_clear_strings(&newargs);
19264 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019265ret_free:
19266 vim_free(skip_until);
19267 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270}
19271
19272/*
19273 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019274 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019276 * flags:
19277 * TFN_INT: internal function name OK
19278 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279 * Advances "pp" to just after the function name (if no error).
19280 */
19281 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019282trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019283 char_u **pp;
19284 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019285 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019286 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019288 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289 char_u *start;
19290 char_u *end;
19291 int lead;
19292 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019294 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019295
19296 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019297 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019299
19300 /* Check for hard coded <SNR>: already translated function ID (from a user
19301 * command). */
19302 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19303 && (*pp)[2] == (int)KE_SNR)
19304 {
19305 *pp += 3;
19306 len = get_id_len(pp) + 3;
19307 return vim_strnsave(start, len);
19308 }
19309
19310 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19311 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019313 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019315
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019316 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19317 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019318 if (end == start)
19319 {
19320 if (!skip)
19321 EMSG(_("E129: Function name required"));
19322 goto theend;
19323 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019324 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019325 {
19326 /*
19327 * Report an invalid expression in braces, unless the expression
19328 * evaluation has been cancelled due to an aborting error, an
19329 * interrupt, or an exception.
19330 */
19331 if (!aborting())
19332 {
19333 if (end != NULL)
19334 EMSG2(_(e_invarg2), start);
19335 }
19336 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019337 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019338 goto theend;
19339 }
19340
19341 if (lv.ll_tv != NULL)
19342 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019343 if (fdp != NULL)
19344 {
19345 fdp->fd_dict = lv.ll_dict;
19346 fdp->fd_newkey = lv.ll_newkey;
19347 lv.ll_newkey = NULL;
19348 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019349 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019350 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19351 {
19352 name = vim_strsave(lv.ll_tv->vval.v_string);
19353 *pp = end;
19354 }
19355 else
19356 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019357 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19358 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019359 EMSG(_(e_funcref));
19360 else
19361 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019362 name = NULL;
19363 }
19364 goto theend;
19365 }
19366
19367 if (lv.ll_name == NULL)
19368 {
19369 /* Error found, but continue after the function name. */
19370 *pp = end;
19371 goto theend;
19372 }
19373
Bram Moolenaar33e1a802007-09-06 12:26:44 +000019374 /* Check if the name is a Funcref. If so, use the value. */
19375 if (lv.ll_exp_name != NULL)
19376 {
19377 len = (int)STRLEN(lv.ll_exp_name);
19378 name = deref_func_name(lv.ll_exp_name, &len);
19379 if (name == lv.ll_exp_name)
19380 name = NULL;
19381 }
19382 else
19383 {
19384 len = (int)(end - *pp);
19385 name = deref_func_name(*pp, &len);
19386 if (name == *pp)
19387 name = NULL;
19388 }
19389 if (name != NULL)
19390 {
19391 name = vim_strsave(name);
19392 *pp = end;
19393 goto theend;
19394 }
19395
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019396 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019397 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019398 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019399 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19400 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19401 {
19402 /* When there was "s:" already or the name expanded to get a
19403 * leading "s:" then remove it. */
19404 lv.ll_name += 2;
19405 len -= 2;
19406 lead = 2;
19407 }
19408 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019409 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019410 {
19411 if (lead == 2) /* skip over "s:" */
19412 lv.ll_name += 2;
19413 len = (int)(end - lv.ll_name);
19414 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019415
19416 /*
19417 * Copy the function name to allocated memory.
19418 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19419 * Accept <SNR>123_name() outside a script.
19420 */
19421 if (skip)
19422 lead = 0; /* do nothing */
19423 else if (lead > 0)
19424 {
19425 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019426 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19427 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019428 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019429 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019430 if (current_SID <= 0)
19431 {
19432 EMSG(_(e_usingsid));
19433 goto theend;
19434 }
19435 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19436 lead += (int)STRLEN(sid_buf);
19437 }
19438 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019439 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019440 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019441 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019442 goto theend;
19443 }
19444 name = alloc((unsigned)(len + lead + 1));
19445 if (name != NULL)
19446 {
19447 if (lead > 0)
19448 {
19449 name[0] = K_SPECIAL;
19450 name[1] = KS_EXTRA;
19451 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019452 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019453 STRCPY(name + 3, sid_buf);
19454 }
19455 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19456 name[len + lead] = NUL;
19457 }
19458 *pp = end;
19459
19460theend:
19461 clear_lval(&lv);
19462 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463}
19464
19465/*
19466 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19467 * Return 2 if "p" starts with "s:".
19468 * Return 0 otherwise.
19469 */
19470 static int
19471eval_fname_script(p)
19472 char_u *p;
19473{
19474 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19475 || STRNICMP(p + 1, "SNR>", 4) == 0))
19476 return 5;
19477 if (p[0] == 's' && p[1] == ':')
19478 return 2;
19479 return 0;
19480}
19481
19482/*
19483 * Return TRUE if "p" starts with "<SID>" or "s:".
19484 * Only works if eval_fname_script() returned non-zero for "p"!
19485 */
19486 static int
19487eval_fname_sid(p)
19488 char_u *p;
19489{
19490 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19491}
19492
19493/*
19494 * List the head of the function: "name(arg1, arg2)".
19495 */
19496 static void
19497list_func_head(fp, indent)
19498 ufunc_T *fp;
19499 int indent;
19500{
19501 int j;
19502
19503 msg_start();
19504 if (indent)
19505 MSG_PUTS(" ");
19506 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019507 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508 {
19509 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019510 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 }
19512 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019513 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019515 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 {
19517 if (j)
19518 MSG_PUTS(", ");
19519 msg_puts(FUNCARG(fp, j));
19520 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019521 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 {
19523 if (j)
19524 MSG_PUTS(", ");
19525 MSG_PUTS("...");
19526 }
19527 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019528 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019529 if (p_verbose > 0)
19530 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531}
19532
19533/*
19534 * Find a function by name, return pointer to it in ufuncs.
19535 * Return NULL for unknown function.
19536 */
19537 static ufunc_T *
19538find_func(name)
19539 char_u *name;
19540{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019541 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019543 hi = hash_find(&func_hashtab, name);
19544 if (!HASHITEM_EMPTY(hi))
19545 return HI2UF(hi);
19546 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547}
19548
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019549#if defined(EXITFREE) || defined(PROTO)
19550 void
19551free_all_functions()
19552{
19553 hashitem_T *hi;
19554
19555 /* Need to start all over every time, because func_free() may change the
19556 * hash table. */
19557 while (func_hashtab.ht_used > 0)
19558 for (hi = func_hashtab.ht_array; ; ++hi)
19559 if (!HASHITEM_EMPTY(hi))
19560 {
19561 func_free(HI2UF(hi));
19562 break;
19563 }
19564}
19565#endif
19566
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019567/*
19568 * Return TRUE if a function "name" exists.
19569 */
19570 static int
19571function_exists(name)
19572 char_u *name;
19573{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019574 char_u *nm = name;
19575 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019576 int n = FALSE;
19577
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019578 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019579 nm = skipwhite(nm);
19580
19581 /* Only accept "funcname", "funcname ", "funcname (..." and
19582 * "funcname(...", not "funcname!...". */
19583 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019584 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019585 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019586 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019587 else
19588 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019589 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019590 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019591 return n;
19592}
19593
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019594/*
19595 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019596 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019597 */
19598 static int
19599builtin_function(name)
19600 char_u *name;
19601{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019602 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19603 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019604}
19605
Bram Moolenaar05159a02005-02-26 23:04:13 +000019606#if defined(FEAT_PROFILE) || defined(PROTO)
19607/*
19608 * Start profiling function "fp".
19609 */
19610 static void
19611func_do_profile(fp)
19612 ufunc_T *fp;
19613{
19614 fp->uf_tm_count = 0;
19615 profile_zero(&fp->uf_tm_self);
19616 profile_zero(&fp->uf_tm_total);
19617 if (fp->uf_tml_count == NULL)
19618 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19619 (sizeof(int) * fp->uf_lines.ga_len));
19620 if (fp->uf_tml_total == NULL)
19621 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19622 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19623 if (fp->uf_tml_self == NULL)
19624 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19625 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19626 fp->uf_tml_idx = -1;
19627 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19628 || fp->uf_tml_self == NULL)
19629 return; /* out of memory */
19630
19631 fp->uf_profiling = TRUE;
19632}
19633
19634/*
19635 * Dump the profiling results for all functions in file "fd".
19636 */
19637 void
19638func_dump_profile(fd)
19639 FILE *fd;
19640{
19641 hashitem_T *hi;
19642 int todo;
19643 ufunc_T *fp;
19644 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019645 ufunc_T **sorttab;
19646 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019647
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019648 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019649 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19650
Bram Moolenaar05159a02005-02-26 23:04:13 +000019651 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19652 {
19653 if (!HASHITEM_EMPTY(hi))
19654 {
19655 --todo;
19656 fp = HI2UF(hi);
19657 if (fp->uf_profiling)
19658 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019659 if (sorttab != NULL)
19660 sorttab[st_len++] = fp;
19661
Bram Moolenaar05159a02005-02-26 23:04:13 +000019662 if (fp->uf_name[0] == K_SPECIAL)
19663 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19664 else
19665 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19666 if (fp->uf_tm_count == 1)
19667 fprintf(fd, "Called 1 time\n");
19668 else
19669 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19670 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19671 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19672 fprintf(fd, "\n");
19673 fprintf(fd, "count total (s) self (s)\n");
19674
19675 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19676 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019677 if (FUNCLINE(fp, i) == NULL)
19678 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019679 prof_func_line(fd, fp->uf_tml_count[i],
19680 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019681 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19682 }
19683 fprintf(fd, "\n");
19684 }
19685 }
19686 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019687
19688 if (sorttab != NULL && st_len > 0)
19689 {
19690 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19691 prof_total_cmp);
19692 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19693 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19694 prof_self_cmp);
19695 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19696 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019697}
Bram Moolenaar73830342005-02-28 22:48:19 +000019698
19699 static void
19700prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19701 FILE *fd;
19702 ufunc_T **sorttab;
19703 int st_len;
19704 char *title;
19705 int prefer_self; /* when equal print only self time */
19706{
19707 int i;
19708 ufunc_T *fp;
19709
19710 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19711 fprintf(fd, "count total (s) self (s) function\n");
19712 for (i = 0; i < 20 && i < st_len; ++i)
19713 {
19714 fp = sorttab[i];
19715 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19716 prefer_self);
19717 if (fp->uf_name[0] == K_SPECIAL)
19718 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19719 else
19720 fprintf(fd, " %s()\n", fp->uf_name);
19721 }
19722 fprintf(fd, "\n");
19723}
19724
19725/*
19726 * Print the count and times for one function or function line.
19727 */
19728 static void
19729prof_func_line(fd, count, total, self, prefer_self)
19730 FILE *fd;
19731 int count;
19732 proftime_T *total;
19733 proftime_T *self;
19734 int prefer_self; /* when equal print only self time */
19735{
19736 if (count > 0)
19737 {
19738 fprintf(fd, "%5d ", count);
19739 if (prefer_self && profile_equal(total, self))
19740 fprintf(fd, " ");
19741 else
19742 fprintf(fd, "%s ", profile_msg(total));
19743 if (!prefer_self && profile_equal(total, self))
19744 fprintf(fd, " ");
19745 else
19746 fprintf(fd, "%s ", profile_msg(self));
19747 }
19748 else
19749 fprintf(fd, " ");
19750}
19751
19752/*
19753 * Compare function for total time sorting.
19754 */
19755 static int
19756#ifdef __BORLANDC__
19757_RTLENTRYF
19758#endif
19759prof_total_cmp(s1, s2)
19760 const void *s1;
19761 const void *s2;
19762{
19763 ufunc_T *p1, *p2;
19764
19765 p1 = *(ufunc_T **)s1;
19766 p2 = *(ufunc_T **)s2;
19767 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19768}
19769
19770/*
19771 * Compare function for self time sorting.
19772 */
19773 static int
19774#ifdef __BORLANDC__
19775_RTLENTRYF
19776#endif
19777prof_self_cmp(s1, s2)
19778 const void *s1;
19779 const void *s2;
19780{
19781 ufunc_T *p1, *p2;
19782
19783 p1 = *(ufunc_T **)s1;
19784 p2 = *(ufunc_T **)s2;
19785 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19786}
19787
Bram Moolenaar05159a02005-02-26 23:04:13 +000019788#endif
19789
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019790/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019791 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019792 * Return TRUE if a package was loaded.
19793 */
19794 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019795script_autoload(name, reload)
19796 char_u *name;
19797 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019798{
19799 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019800 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019801 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019802 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019803
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019804 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019805 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019806 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019807 return FALSE;
19808
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019809 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019810
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019811 /* Find the name in the list of previously loaded package names. Skip
19812 * "autoload/", it's always the same. */
19813 for (i = 0; i < ga_loaded.ga_len; ++i)
19814 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19815 break;
19816 if (!reload && i < ga_loaded.ga_len)
19817 ret = FALSE; /* was loaded already */
19818 else
19819 {
19820 /* Remember the name if it wasn't loaded already. */
19821 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19822 {
19823 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19824 tofree = NULL;
19825 }
19826
19827 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019828 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019829 ret = TRUE;
19830 }
19831
19832 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019833 return ret;
19834}
19835
19836/*
19837 * Return the autoload script name for a function or variable name.
19838 * Returns NULL when out of memory.
19839 */
19840 static char_u *
19841autoload_name(name)
19842 char_u *name;
19843{
19844 char_u *p;
19845 char_u *scriptname;
19846
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019847 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019848 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19849 if (scriptname == NULL)
19850 return FALSE;
19851 STRCPY(scriptname, "autoload/");
19852 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019853 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019854 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019855 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019856 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019857 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019858}
19859
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19861
19862/*
19863 * Function given to ExpandGeneric() to obtain the list of user defined
19864 * function names.
19865 */
19866 char_u *
19867get_user_func_name(xp, idx)
19868 expand_T *xp;
19869 int idx;
19870{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019871 static long_u done;
19872 static hashitem_T *hi;
19873 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874
19875 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019877 done = 0;
19878 hi = func_hashtab.ht_array;
19879 }
19880 if (done < func_hashtab.ht_used)
19881 {
19882 if (done++ > 0)
19883 ++hi;
19884 while (HASHITEM_EMPTY(hi))
19885 ++hi;
19886 fp = HI2UF(hi);
19887
19888 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19889 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890
19891 cat_func_name(IObuff, fp);
19892 if (xp->xp_context != EXPAND_USER_FUNC)
19893 {
19894 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019895 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896 STRCAT(IObuff, ")");
19897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898 return IObuff;
19899 }
19900 return NULL;
19901}
19902
19903#endif /* FEAT_CMDL_COMPL */
19904
19905/*
19906 * Copy the function name of "fp" to buffer "buf".
19907 * "buf" must be able to hold the function name plus three bytes.
19908 * Takes care of script-local function names.
19909 */
19910 static void
19911cat_func_name(buf, fp)
19912 char_u *buf;
19913 ufunc_T *fp;
19914{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019915 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916 {
19917 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019918 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919 }
19920 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019921 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922}
19923
19924/*
19925 * ":delfunction {name}"
19926 */
19927 void
19928ex_delfunction(eap)
19929 exarg_T *eap;
19930{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019931 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 char_u *p;
19933 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019934 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935
19936 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019937 name = trans_function_name(&p, eap->skip, 0, &fudi);
19938 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019940 {
19941 if (fudi.fd_dict != NULL && !eap->skip)
19942 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 if (!ends_excmd(*skipwhite(p)))
19946 {
19947 vim_free(name);
19948 EMSG(_(e_trailing));
19949 return;
19950 }
19951 eap->nextcmd = check_nextcmd(p);
19952 if (eap->nextcmd != NULL)
19953 *p = NUL;
19954
19955 if (!eap->skip)
19956 fp = find_func(name);
19957 vim_free(name);
19958
19959 if (!eap->skip)
19960 {
19961 if (fp == NULL)
19962 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019963 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 return;
19965 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019966 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 {
19968 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19969 return;
19970 }
19971
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019972 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019974 /* Delete the dict item that refers to the function, it will
19975 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019976 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019978 else
19979 func_free(fp);
19980 }
19981}
19982
19983/*
19984 * Free a function and remove it from the list of functions.
19985 */
19986 static void
19987func_free(fp)
19988 ufunc_T *fp;
19989{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019990 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019991
19992 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019993 ga_clear_strings(&(fp->uf_args));
19994 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019995#ifdef FEAT_PROFILE
19996 vim_free(fp->uf_tml_count);
19997 vim_free(fp->uf_tml_total);
19998 vim_free(fp->uf_tml_self);
19999#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020000
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020001 /* remove the function from the function hashtable */
20002 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20003 if (HASHITEM_EMPTY(hi))
20004 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020005 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020006 hash_remove(&func_hashtab, hi);
20007
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020008 vim_free(fp);
20009}
20010
20011/*
20012 * Unreference a Function: decrement the reference count and free it when it
20013 * becomes zero. Only for numbered functions.
20014 */
20015 static void
20016func_unref(name)
20017 char_u *name;
20018{
20019 ufunc_T *fp;
20020
20021 if (name != NULL && isdigit(*name))
20022 {
20023 fp = find_func(name);
20024 if (fp == NULL)
20025 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020026 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020027 {
20028 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020029 * when "uf_calls" becomes zero. */
20030 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020031 func_free(fp);
20032 }
20033 }
20034}
20035
20036/*
20037 * Count a reference to a Function.
20038 */
20039 static void
20040func_ref(name)
20041 char_u *name;
20042{
20043 ufunc_T *fp;
20044
20045 if (name != NULL && isdigit(*name))
20046 {
20047 fp = find_func(name);
20048 if (fp == NULL)
20049 EMSG2(_(e_intern2), "func_ref()");
20050 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020051 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052 }
20053}
20054
20055/*
20056 * Call a user function.
20057 */
20058 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020059call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 ufunc_T *fp; /* pointer to function */
20061 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020062 typval_T *argvars; /* arguments */
20063 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 linenr_T firstline; /* first line of range */
20065 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020066 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067{
Bram Moolenaar33570922005-01-25 22:26:29 +000020068 char_u *save_sourcing_name;
20069 linenr_T save_sourcing_lnum;
20070 scid_T save_current_SID;
20071 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020072 int save_did_emsg;
20073 static int depth = 0;
20074 dictitem_T *v;
20075 int fixvar_idx = 0; /* index in fixvar[] */
20076 int i;
20077 int ai;
20078 char_u numbuf[NUMBUFLEN];
20079 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020080#ifdef FEAT_PROFILE
20081 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020082 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084
20085 /* If depth of calling is getting too high, don't execute the function */
20086 if (depth >= p_mfd)
20087 {
20088 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020089 rettv->v_type = VAR_NUMBER;
20090 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 return;
20092 }
20093 ++depth;
20094
20095 line_breakcheck(); /* check for CTRL-C hit */
20096
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020097 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000020098 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020100 fc.rettv = rettv;
20101 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 fc.linenr = 0;
20103 fc.returned = FALSE;
20104 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020106 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 fc.dbg_tick = debug_tick;
20108
Bram Moolenaar33570922005-01-25 22:26:29 +000020109 /*
20110 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20111 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20112 * each argument variable and saves a lot of time.
20113 */
20114 /*
20115 * Init l: variables.
20116 */
20117 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000020118 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020119 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020120 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20121 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020122 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020123 name = v->di_key;
20124 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000020125 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
20126 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
20127 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020128 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020129 v->di_tv.vval.v_dict = selfdict;
20130 ++selfdict->dv_refcount;
20131 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020132
Bram Moolenaar33570922005-01-25 22:26:29 +000020133 /*
20134 * Init a: variables.
20135 * Set a:0 to "argcount".
20136 * Set a:000 to a list with room for the "..." arguments.
20137 */
20138 init_var_dict(&fc.l_avars, &fc.l_avars_var);
20139 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020140 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000020141 v = &fc.fixvar[fixvar_idx++].var;
20142 STRCPY(v->di_key, "000");
20143 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20144 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20145 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020146 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020147 v->di_tv.vval.v_list = &fc.l_varlist;
20148 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
20149 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000020150 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020151
20152 /*
20153 * Set a:firstline to "firstline" and a:lastline to "lastline".
20154 * Set a:name to named arguments.
20155 * Set a:N to the "..." arguments.
20156 */
20157 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
20158 (varnumber_T)firstline);
20159 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
20160 (varnumber_T)lastline);
20161 for (i = 0; i < argcount; ++i)
20162 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020163 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020164 if (ai < 0)
20165 /* named argument a:name */
20166 name = FUNCARG(fp, i);
20167 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020168 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020169 /* "..." argument a:1, a:2, etc. */
20170 sprintf((char *)numbuf, "%d", ai + 1);
20171 name = numbuf;
20172 }
20173 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
20174 {
20175 v = &fc.fixvar[fixvar_idx++].var;
20176 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20177 }
20178 else
20179 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020180 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20181 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000020182 if (v == NULL)
20183 break;
20184 v->di_flags = DI_FLAGS_RO;
20185 }
20186 STRCPY(v->di_key, name);
20187 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20188
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020189 /* Note: the values are copied directly to avoid alloc/free.
20190 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020191 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020192 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020193
20194 if (ai >= 0 && ai < MAX_FUNC_ARGS)
20195 {
20196 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
20197 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020198 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020199 }
20200 }
20201
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202 /* Don't redraw while executing the function. */
20203 ++RedrawingDisabled;
20204 save_sourcing_name = sourcing_name;
20205 save_sourcing_lnum = sourcing_lnum;
20206 sourcing_lnum = 1;
20207 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020208 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209 if (sourcing_name != NULL)
20210 {
20211 if (save_sourcing_name != NULL
20212 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
20213 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
20214 else
20215 STRCPY(sourcing_name, "function ");
20216 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
20217
20218 if (p_verbose >= 12)
20219 {
20220 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020221 verbose_enter_scroll();
20222
Bram Moolenaar555b2802005-05-19 21:08:39 +000020223 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 if (p_verbose >= 14)
20225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020227 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020228 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020229 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230
20231 msg_puts((char_u *)"(");
20232 for (i = 0; i < argcount; ++i)
20233 {
20234 if (i > 0)
20235 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020236 if (argvars[i].v_type == VAR_NUMBER)
20237 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 else
20239 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020240 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
20241 if (s != NULL)
20242 {
20243 trunc_string(s, buf, MSG_BUF_CLEN);
20244 msg_puts(buf);
20245 vim_free(tofree);
20246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247 }
20248 }
20249 msg_puts((char_u *)")");
20250 }
20251 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020252
20253 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254 --no_wait_return;
20255 }
20256 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020257#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020258 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020259 {
20260 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20261 func_do_profile(fp);
20262 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020263 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020264 {
20265 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020266 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020267 profile_zero(&fp->uf_tm_children);
20268 }
20269 script_prof_save(&wait_start);
20270 }
20271#endif
20272
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020274 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 save_did_emsg = did_emsg;
20276 did_emsg = FALSE;
20277
20278 /* call do_cmdline() to execute the lines */
20279 do_cmdline(NULL, get_func_line, (void *)&fc,
20280 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20281
20282 --RedrawingDisabled;
20283
20284 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020285 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020287 clear_tv(rettv);
20288 rettv->v_type = VAR_NUMBER;
20289 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 }
20291
Bram Moolenaar05159a02005-02-26 23:04:13 +000020292#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020293 if (do_profiling == PROF_YES && (fp->uf_profiling
20294 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020295 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020296 profile_end(&call_start);
20297 profile_sub_wait(&wait_start, &call_start);
20298 profile_add(&fp->uf_tm_total, &call_start);
20299 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020300 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020301 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020302 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20303 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020304 }
20305 }
20306#endif
20307
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 /* when being verbose, mention the return value */
20309 if (p_verbose >= 12)
20310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020312 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020315 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020316 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020317 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20318 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020319 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020321 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020322 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020323 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020324 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020325
Bram Moolenaar555b2802005-05-19 21:08:39 +000020326 /* The value may be very long. Skip the middle part, so that we
20327 * have some idea how it starts and ends. smsg() would always
20328 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020329 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
20330 if (s != NULL)
20331 {
20332 trunc_string(s, buf, MSG_BUF_CLEN);
20333 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
20334 vim_free(tofree);
20335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336 }
20337 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020338
20339 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 --no_wait_return;
20341 }
20342
20343 vim_free(sourcing_name);
20344 sourcing_name = save_sourcing_name;
20345 sourcing_lnum = save_sourcing_lnum;
20346 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020347#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020348 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020349 script_prof_restore(&wait_start);
20350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351
20352 if (p_verbose >= 12 && sourcing_name != NULL)
20353 {
20354 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020355 verbose_enter_scroll();
20356
Bram Moolenaar555b2802005-05-19 21:08:39 +000020357 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020359
20360 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 --no_wait_return;
20362 }
20363
20364 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020365 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366
Bram Moolenaar33570922005-01-25 22:26:29 +000020367 /* The a: variables typevals were not alloced, only free the allocated
20368 * variables. */
20369 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20370
20371 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372 --depth;
20373}
20374
20375/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020376 * Add a number variable "name" to dict "dp" with value "nr".
20377 */
20378 static void
20379add_nr_var(dp, v, name, nr)
20380 dict_T *dp;
20381 dictitem_T *v;
20382 char *name;
20383 varnumber_T nr;
20384{
20385 STRCPY(v->di_key, name);
20386 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20387 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20388 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020389 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020390 v->di_tv.vval.v_number = nr;
20391}
20392
20393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 * ":return [expr]"
20395 */
20396 void
20397ex_return(eap)
20398 exarg_T *eap;
20399{
20400 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020401 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020402 int returning = FALSE;
20403
20404 if (current_funccal == NULL)
20405 {
20406 EMSG(_("E133: :return not inside a function"));
20407 return;
20408 }
20409
20410 if (eap->skip)
20411 ++emsg_skip;
20412
20413 eap->nextcmd = NULL;
20414 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020415 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416 {
20417 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020418 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020420 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421 }
20422 /* It's safer to return also on error. */
20423 else if (!eap->skip)
20424 {
20425 /*
20426 * Return unless the expression evaluation has been cancelled due to an
20427 * aborting error, an interrupt, or an exception.
20428 */
20429 if (!aborting())
20430 returning = do_return(eap, FALSE, TRUE, NULL);
20431 }
20432
20433 /* When skipping or the return gets pending, advance to the next command
20434 * in this line (!returning). Otherwise, ignore the rest of the line.
20435 * Following lines will be ignored by get_func_line(). */
20436 if (returning)
20437 eap->nextcmd = NULL;
20438 else if (eap->nextcmd == NULL) /* no argument */
20439 eap->nextcmd = check_nextcmd(arg);
20440
20441 if (eap->skip)
20442 --emsg_skip;
20443}
20444
20445/*
20446 * Return from a function. Possibly makes the return pending. Also called
20447 * for a pending return at the ":endtry" or after returning from an extra
20448 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020449 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020450 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 * FALSE when the return gets pending.
20452 */
20453 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020454do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 exarg_T *eap;
20456 int reanimate;
20457 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020458 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459{
20460 int idx;
20461 struct condstack *cstack = eap->cstack;
20462
20463 if (reanimate)
20464 /* Undo the return. */
20465 current_funccal->returned = FALSE;
20466
20467 /*
20468 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20469 * not in its finally clause (which then is to be executed next) is found.
20470 * In this case, make the ":return" pending for execution at the ":endtry".
20471 * Otherwise, return normally.
20472 */
20473 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20474 if (idx >= 0)
20475 {
20476 cstack->cs_pending[idx] = CSTP_RETURN;
20477
20478 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020479 /* A pending return again gets pending. "rettv" points to an
20480 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020482 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 else
20484 {
20485 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020486 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020488 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020490 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491 {
20492 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020493 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020494 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 else
20496 EMSG(_(e_outofmem));
20497 }
20498 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020499 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500
20501 if (reanimate)
20502 {
20503 /* The pending return value could be overwritten by a ":return"
20504 * without argument in a finally clause; reset the default
20505 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020506 current_funccal->rettv->v_type = VAR_NUMBER;
20507 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 }
20509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020510 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 }
20512 else
20513 {
20514 current_funccal->returned = TRUE;
20515
20516 /* If the return is carried out now, store the return value. For
20517 * a return immediately after reanimation, the value is already
20518 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020519 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020521 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020522 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020524 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 }
20526 }
20527
20528 return idx < 0;
20529}
20530
20531/*
20532 * Free the variable with a pending return value.
20533 */
20534 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020535discard_pending_return(rettv)
20536 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537{
Bram Moolenaar33570922005-01-25 22:26:29 +000020538 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539}
20540
20541/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020542 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543 * is an allocated string. Used by report_pending() for verbose messages.
20544 */
20545 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020546get_return_cmd(rettv)
20547 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020549 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020550 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020551 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020553 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020554 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020555 if (s == NULL)
20556 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020557
20558 STRCPY(IObuff, ":return ");
20559 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20560 if (STRLEN(s) + 8 >= IOSIZE)
20561 STRCPY(IObuff + IOSIZE - 4, "...");
20562 vim_free(tofree);
20563 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564}
20565
20566/*
20567 * Get next function line.
20568 * Called by do_cmdline() to get the next line.
20569 * Returns allocated string, or NULL for end of function.
20570 */
20571/* ARGSUSED */
20572 char_u *
20573get_func_line(c, cookie, indent)
20574 int c; /* not used */
20575 void *cookie;
20576 int indent; /* not used */
20577{
Bram Moolenaar33570922005-01-25 22:26:29 +000020578 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020579 ufunc_T *fp = fcp->func;
20580 char_u *retval;
20581 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582
20583 /* If breakpoints have been added/deleted need to check for it. */
20584 if (fcp->dbg_tick != debug_tick)
20585 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020586 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587 sourcing_lnum);
20588 fcp->dbg_tick = debug_tick;
20589 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020590#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020591 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020592 func_line_end(cookie);
20593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594
Bram Moolenaar05159a02005-02-26 23:04:13 +000020595 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020596 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20597 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598 retval = NULL;
20599 else
20600 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020601 /* Skip NULL lines (continuation lines). */
20602 while (fcp->linenr < gap->ga_len
20603 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20604 ++fcp->linenr;
20605 if (fcp->linenr >= gap->ga_len)
20606 retval = NULL;
20607 else
20608 {
20609 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20610 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020611#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020612 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020613 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020614#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 }
20617
20618 /* Did we encounter a breakpoint? */
20619 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20620 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020621 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020623 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 sourcing_lnum);
20625 fcp->dbg_tick = debug_tick;
20626 }
20627
20628 return retval;
20629}
20630
Bram Moolenaar05159a02005-02-26 23:04:13 +000020631#if defined(FEAT_PROFILE) || defined(PROTO)
20632/*
20633 * Called when starting to read a function line.
20634 * "sourcing_lnum" must be correct!
20635 * When skipping lines it may not actually be executed, but we won't find out
20636 * until later and we need to store the time now.
20637 */
20638 void
20639func_line_start(cookie)
20640 void *cookie;
20641{
20642 funccall_T *fcp = (funccall_T *)cookie;
20643 ufunc_T *fp = fcp->func;
20644
20645 if (fp->uf_profiling && sourcing_lnum >= 1
20646 && sourcing_lnum <= fp->uf_lines.ga_len)
20647 {
20648 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020649 /* Skip continuation lines. */
20650 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20651 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020652 fp->uf_tml_execed = FALSE;
20653 profile_start(&fp->uf_tml_start);
20654 profile_zero(&fp->uf_tml_children);
20655 profile_get_wait(&fp->uf_tml_wait);
20656 }
20657}
20658
20659/*
20660 * Called when actually executing a function line.
20661 */
20662 void
20663func_line_exec(cookie)
20664 void *cookie;
20665{
20666 funccall_T *fcp = (funccall_T *)cookie;
20667 ufunc_T *fp = fcp->func;
20668
20669 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20670 fp->uf_tml_execed = TRUE;
20671}
20672
20673/*
20674 * Called when done with a function line.
20675 */
20676 void
20677func_line_end(cookie)
20678 void *cookie;
20679{
20680 funccall_T *fcp = (funccall_T *)cookie;
20681 ufunc_T *fp = fcp->func;
20682
20683 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20684 {
20685 if (fp->uf_tml_execed)
20686 {
20687 ++fp->uf_tml_count[fp->uf_tml_idx];
20688 profile_end(&fp->uf_tml_start);
20689 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020690 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020691 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20692 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020693 }
20694 fp->uf_tml_idx = -1;
20695 }
20696}
20697#endif
20698
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699/*
20700 * Return TRUE if the currently active function should be ended, because a
20701 * return was encountered or an error occured. Used inside a ":while".
20702 */
20703 int
20704func_has_ended(cookie)
20705 void *cookie;
20706{
Bram Moolenaar33570922005-01-25 22:26:29 +000020707 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708
20709 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20710 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020711 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712 || fcp->returned);
20713}
20714
20715/*
20716 * return TRUE if cookie indicates a function which "abort"s on errors.
20717 */
20718 int
20719func_has_abort(cookie)
20720 void *cookie;
20721{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020722 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723}
20724
20725#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20726typedef enum
20727{
20728 VAR_FLAVOUR_DEFAULT,
20729 VAR_FLAVOUR_SESSION,
20730 VAR_FLAVOUR_VIMINFO
20731} var_flavour_T;
20732
20733static var_flavour_T var_flavour __ARGS((char_u *varname));
20734
20735 static var_flavour_T
20736var_flavour(varname)
20737 char_u *varname;
20738{
20739 char_u *p = varname;
20740
20741 if (ASCII_ISUPPER(*p))
20742 {
20743 while (*(++p))
20744 if (ASCII_ISLOWER(*p))
20745 return VAR_FLAVOUR_SESSION;
20746 return VAR_FLAVOUR_VIMINFO;
20747 }
20748 else
20749 return VAR_FLAVOUR_DEFAULT;
20750}
20751#endif
20752
20753#if defined(FEAT_VIMINFO) || defined(PROTO)
20754/*
20755 * Restore global vars that start with a capital from the viminfo file
20756 */
20757 int
20758read_viminfo_varlist(virp, writing)
20759 vir_T *virp;
20760 int writing;
20761{
20762 char_u *tab;
20763 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020764 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765
20766 if (!writing && (find_viminfo_parameter('!') != NULL))
20767 {
20768 tab = vim_strchr(virp->vir_line + 1, '\t');
20769 if (tab != NULL)
20770 {
20771 *tab++ = '\0'; /* isolate the variable name */
20772 if (*tab == 'S') /* string var */
20773 is_string = TRUE;
20774
20775 tab = vim_strchr(tab, '\t');
20776 if (tab != NULL)
20777 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 if (is_string)
20779 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020780 tv.v_type = VAR_STRING;
20781 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783 }
20784 else
20785 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020786 tv.v_type = VAR_NUMBER;
20787 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020789 set_var(virp->vir_line + 1, &tv, FALSE);
20790 if (is_string)
20791 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792 }
20793 }
20794 }
20795
20796 return viminfo_readline(virp);
20797}
20798
20799/*
20800 * Write global vars that start with a capital to the viminfo file
20801 */
20802 void
20803write_viminfo_varlist(fp)
20804 FILE *fp;
20805{
Bram Moolenaar33570922005-01-25 22:26:29 +000020806 hashitem_T *hi;
20807 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020808 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020809 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020810 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020811 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020812 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813
20814 if (find_viminfo_parameter('!') == NULL)
20815 return;
20816
20817 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020818
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020819 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020820 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020822 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020824 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020825 this_var = HI2DI(hi);
20826 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020827 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020828 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020829 {
20830 case VAR_STRING: s = "STR"; break;
20831 case VAR_NUMBER: s = "NUM"; break;
20832 default: continue;
20833 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020834 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020835 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020836 if (p != NULL)
20837 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020838 vim_free(tofree);
20839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840 }
20841 }
20842}
20843#endif
20844
20845#if defined(FEAT_SESSION) || defined(PROTO)
20846 int
20847store_session_globals(fd)
20848 FILE *fd;
20849{
Bram Moolenaar33570922005-01-25 22:26:29 +000020850 hashitem_T *hi;
20851 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020852 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 char_u *p, *t;
20854
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020855 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020856 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020858 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020860 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020861 this_var = HI2DI(hi);
20862 if ((this_var->di_tv.v_type == VAR_NUMBER
20863 || this_var->di_tv.v_type == VAR_STRING)
20864 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020865 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020866 /* Escape special characters with a backslash. Turn a LF and
20867 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020868 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020869 (char_u *)"\\\"\n\r");
20870 if (p == NULL) /* out of memory */
20871 break;
20872 for (t = p; *t != NUL; ++t)
20873 if (*t == '\n')
20874 *t = 'n';
20875 else if (*t == '\r')
20876 *t = 'r';
20877 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020878 this_var->di_key,
20879 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20880 : ' ',
20881 p,
20882 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20883 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020884 || put_eol(fd) == FAIL)
20885 {
20886 vim_free(p);
20887 return FAIL;
20888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 vim_free(p);
20890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891 }
20892 }
20893 return OK;
20894}
20895#endif
20896
Bram Moolenaar661b1822005-07-28 22:36:45 +000020897/*
20898 * Display script name where an item was last set.
20899 * Should only be invoked when 'verbose' is non-zero.
20900 */
20901 void
20902last_set_msg(scriptID)
20903 scid_T scriptID;
20904{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020905 char_u *p;
20906
Bram Moolenaar661b1822005-07-28 22:36:45 +000020907 if (scriptID != 0)
20908 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020909 p = home_replace_save(NULL, get_scriptname(scriptID));
20910 if (p != NULL)
20911 {
20912 verbose_enter();
20913 MSG_PUTS(_("\n\tLast set from "));
20914 MSG_PUTS(p);
20915 vim_free(p);
20916 verbose_leave();
20917 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020918 }
20919}
20920
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921#endif /* FEAT_EVAL */
20922
20923#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20924
20925
20926#ifdef WIN3264
20927/*
20928 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20929 */
20930static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20931static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20932static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20933
20934/*
20935 * Get the short pathname of a file.
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020936 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937 */
20938 static int
20939get_short_pathname(fnamep, bufp, fnamelen)
20940 char_u **fnamep;
20941 char_u **bufp;
20942 int *fnamelen;
20943{
20944 int l,len;
20945 char_u *newbuf;
20946
20947 len = *fnamelen;
20948
20949 l = GetShortPathName(*fnamep, *fnamep, len);
20950 if (l > len - 1)
20951 {
20952 /* If that doesn't work (not enough space), then save the string
20953 * and try again with a new buffer big enough
20954 */
20955 newbuf = vim_strnsave(*fnamep, l);
20956 if (newbuf == NULL)
20957 return 0;
20958
20959 vim_free(*bufp);
20960 *fnamep = *bufp = newbuf;
20961
20962 l = GetShortPathName(*fnamep,*fnamep,l+1);
20963
20964 /* Really should always succeed, as the buffer is big enough */
20965 }
20966
20967 *fnamelen = l;
20968 return 1;
20969}
20970
20971/*
20972 * Create a short path name. Returns the length of the buffer it needs.
20973 * Doesn't copy over the end of the buffer passed in.
20974 */
20975 static int
20976shortpath_for_invalid_fname(fname, bufp, fnamelen)
20977 char_u **fname;
20978 char_u **bufp;
20979 int *fnamelen;
20980{
20981 char_u *s, *p, *pbuf2, *pbuf3;
20982 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020983 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984
20985 /* Make a copy */
20986 len2 = *fnamelen;
20987 pbuf2 = vim_strnsave(*fname, len2);
20988 pbuf3 = NULL;
20989
20990 s = pbuf2 + len2 - 1; /* Find the end */
20991 slen = 1;
20992 plen = len2;
20993
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020994 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 {
20996 --s;
20997 ++slen;
20998 --plen;
20999 }
21000
21001 do
21002 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021003 /* Go back one path-separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021004 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 {
21006 --s;
21007 ++slen;
21008 --plen;
21009 }
21010 if (s <= pbuf2)
21011 break;
21012
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021013 /* Remember the character that is about to be splatted */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014 ch = *s;
21015 *s = 0; /* get_short_pathname requires a null-terminated string */
21016
21017 /* Try it in situ */
21018 p = pbuf2;
21019 if (!get_short_pathname(&p, &pbuf3, &plen))
21020 {
21021 vim_free(pbuf2);
21022 return -1;
21023 }
21024 *s = ch; /* Preserve the string */
21025 } while (plen == 0);
21026
21027 if (plen > 0)
21028 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021029 /* Remember the length of the new string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030 *fnamelen = len = plen + slen;
21031 vim_free(*bufp);
21032 if (len > len2)
21033 {
21034 /* If there's not enough space in the currently allocated string,
21035 * then copy it to a buffer big enough.
21036 */
21037 *fname= *bufp = vim_strnsave(p, len);
21038 if (*fname == NULL)
21039 return -1;
21040 }
21041 else
21042 {
21043 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21044 *fname = *bufp = pbuf2;
21045 if (p != pbuf2)
21046 strncpy(*fname, p, plen);
21047 pbuf2 = NULL;
21048 }
21049 /* Concat the next bit */
21050 strncpy(*fname + plen, s, slen);
21051 (*fname)[len] = '\0';
21052 }
21053 vim_free(pbuf3);
21054 vim_free(pbuf2);
21055 return 0;
21056}
21057
21058/*
21059 * Get a pathname for a partial path.
21060 */
21061 static int
21062shortpath_for_partial(fnamep, bufp, fnamelen)
21063 char_u **fnamep;
21064 char_u **bufp;
21065 int *fnamelen;
21066{
21067 int sepcount, len, tflen;
21068 char_u *p;
21069 char_u *pbuf, *tfname;
21070 int hasTilde;
21071
21072 /* Count up the path seperators from the RHS.. so we know which part
21073 * of the path to return.
21074 */
21075 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021076 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 if (vim_ispathsep(*p))
21078 ++sepcount;
21079
21080 /* Need full path first (use expand_env() to remove a "~/") */
21081 hasTilde = (**fnamep == '~');
21082 if (hasTilde)
21083 pbuf = tfname = expand_env_save(*fnamep);
21084 else
21085 pbuf = tfname = FullName_save(*fnamep, FALSE);
21086
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021087 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088
21089 if (!get_short_pathname(&tfname, &pbuf, &len))
21090 return -1;
21091
21092 if (len == 0)
21093 {
21094 /* Don't have a valid filename, so shorten the rest of the
21095 * path if we can. This CAN give us invalid 8.3 filenames, but
21096 * there's not a lot of point in guessing what it might be.
21097 */
21098 len = tflen;
21099 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
21100 return -1;
21101 }
21102
21103 /* Count the paths backward to find the beginning of the desired string. */
21104 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021105 {
21106#ifdef FEAT_MBYTE
21107 if (has_mbyte)
21108 p -= mb_head_off(tfname, p);
21109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 if (vim_ispathsep(*p))
21111 {
21112 if (sepcount == 0 || (hasTilde && sepcount == 1))
21113 break;
21114 else
21115 sepcount --;
21116 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 if (hasTilde)
21119 {
21120 --p;
21121 if (p >= tfname)
21122 *p = '~';
21123 else
21124 return -1;
21125 }
21126 else
21127 ++p;
21128
21129 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21130 vim_free(*bufp);
21131 *fnamelen = (int)STRLEN(p);
21132 *bufp = pbuf;
21133 *fnamep = p;
21134
21135 return 0;
21136}
21137#endif /* WIN3264 */
21138
21139/*
21140 * Adjust a filename, according to a string of modifiers.
21141 * *fnamep must be NUL terminated when called. When returning, the length is
21142 * determined by *fnamelen.
21143 * Returns valid flags.
21144 * When there is an error, *fnamep is set to NULL.
21145 */
21146 int
21147modify_fname(src, usedlen, fnamep, bufp, fnamelen)
21148 char_u *src; /* string with modifiers */
21149 int *usedlen; /* characters after src that are used */
21150 char_u **fnamep; /* file name so far */
21151 char_u **bufp; /* buffer for allocated file name or NULL */
21152 int *fnamelen; /* length of fnamep */
21153{
21154 int valid = 0;
21155 char_u *tail;
21156 char_u *s, *p, *pbuf;
21157 char_u dirname[MAXPATHL];
21158 int c;
21159 int has_fullname = 0;
21160#ifdef WIN3264
21161 int has_shortname = 0;
21162#endif
21163
21164repeat:
21165 /* ":p" - full path/file_name */
21166 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
21167 {
21168 has_fullname = 1;
21169
21170 valid |= VALID_PATH;
21171 *usedlen += 2;
21172
21173 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21174 if ((*fnamep)[0] == '~'
21175#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21176 && ((*fnamep)[1] == '/'
21177# ifdef BACKSLASH_IN_FILENAME
21178 || (*fnamep)[1] == '\\'
21179# endif
21180 || (*fnamep)[1] == NUL)
21181
21182#endif
21183 )
21184 {
21185 *fnamep = expand_env_save(*fnamep);
21186 vim_free(*bufp); /* free any allocated file name */
21187 *bufp = *fnamep;
21188 if (*fnamep == NULL)
21189 return -1;
21190 }
21191
21192 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021193 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 {
21195 if (vim_ispathsep(*p)
21196 && p[1] == '.'
21197 && (p[2] == NUL
21198 || vim_ispathsep(p[2])
21199 || (p[2] == '.'
21200 && (p[3] == NUL || vim_ispathsep(p[3])))))
21201 break;
21202 }
21203
21204 /* FullName_save() is slow, don't use it when not needed. */
21205 if (*p != NUL || !vim_isAbsName(*fnamep))
21206 {
21207 *fnamep = FullName_save(*fnamep, *p != NUL);
21208 vim_free(*bufp); /* free any allocated file name */
21209 *bufp = *fnamep;
21210 if (*fnamep == NULL)
21211 return -1;
21212 }
21213
21214 /* Append a path separator to a directory. */
21215 if (mch_isdir(*fnamep))
21216 {
21217 /* Make room for one or two extra characters. */
21218 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
21219 vim_free(*bufp); /* free any allocated file name */
21220 *bufp = *fnamep;
21221 if (*fnamep == NULL)
21222 return -1;
21223 add_pathsep(*fnamep);
21224 }
21225 }
21226
21227 /* ":." - path relative to the current directory */
21228 /* ":~" - path relative to the home directory */
21229 /* ":8" - shortname path - postponed till after */
21230 while (src[*usedlen] == ':'
21231 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
21232 {
21233 *usedlen += 2;
21234 if (c == '8')
21235 {
21236#ifdef WIN3264
21237 has_shortname = 1; /* Postpone this. */
21238#endif
21239 continue;
21240 }
21241 pbuf = NULL;
21242 /* Need full path first (use expand_env() to remove a "~/") */
21243 if (!has_fullname)
21244 {
21245 if (c == '.' && **fnamep == '~')
21246 p = pbuf = expand_env_save(*fnamep);
21247 else
21248 p = pbuf = FullName_save(*fnamep, FALSE);
21249 }
21250 else
21251 p = *fnamep;
21252
21253 has_fullname = 0;
21254
21255 if (p != NULL)
21256 {
21257 if (c == '.')
21258 {
21259 mch_dirname(dirname, MAXPATHL);
21260 s = shorten_fname(p, dirname);
21261 if (s != NULL)
21262 {
21263 *fnamep = s;
21264 if (pbuf != NULL)
21265 {
21266 vim_free(*bufp); /* free any allocated file name */
21267 *bufp = pbuf;
21268 pbuf = NULL;
21269 }
21270 }
21271 }
21272 else
21273 {
21274 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21275 /* Only replace it when it starts with '~' */
21276 if (*dirname == '~')
21277 {
21278 s = vim_strsave(dirname);
21279 if (s != NULL)
21280 {
21281 *fnamep = s;
21282 vim_free(*bufp);
21283 *bufp = s;
21284 }
21285 }
21286 }
21287 vim_free(pbuf);
21288 }
21289 }
21290
21291 tail = gettail(*fnamep);
21292 *fnamelen = (int)STRLEN(*fnamep);
21293
21294 /* ":h" - head, remove "/file_name", can be repeated */
21295 /* Don't remove the first "/" or "c:\" */
21296 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21297 {
21298 valid |= VALID_HEAD;
21299 *usedlen += 2;
21300 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021301 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 --tail;
21303 *fnamelen = (int)(tail - *fnamep);
21304#ifdef VMS
21305 if (*fnamelen > 0)
21306 *fnamelen += 1; /* the path separator is part of the path */
21307#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021308 while (tail > s && !after_pathsep(s, tail))
21309 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 }
21311
21312 /* ":8" - shortname */
21313 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21314 {
21315 *usedlen += 2;
21316#ifdef WIN3264
21317 has_shortname = 1;
21318#endif
21319 }
21320
21321#ifdef WIN3264
21322 /* Check shortname after we have done 'heads' and before we do 'tails'
21323 */
21324 if (has_shortname)
21325 {
21326 pbuf = NULL;
21327 /* Copy the string if it is shortened by :h */
21328 if (*fnamelen < (int)STRLEN(*fnamep))
21329 {
21330 p = vim_strnsave(*fnamep, *fnamelen);
21331 if (p == 0)
21332 return -1;
21333 vim_free(*bufp);
21334 *bufp = *fnamep = p;
21335 }
21336
21337 /* Split into two implementations - makes it easier. First is where
21338 * there isn't a full name already, second is where there is.
21339 */
21340 if (!has_fullname && !vim_isAbsName(*fnamep))
21341 {
21342 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21343 return -1;
21344 }
21345 else
21346 {
21347 int l;
21348
21349 /* Simple case, already have the full-name
21350 * Nearly always shorter, so try first time. */
21351 l = *fnamelen;
21352 if (!get_short_pathname(fnamep, bufp, &l))
21353 return -1;
21354
21355 if (l == 0)
21356 {
21357 /* Couldn't find the filename.. search the paths.
21358 */
21359 l = *fnamelen;
21360 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21361 return -1;
21362 }
21363 *fnamelen = l;
21364 }
21365 }
21366#endif /* WIN3264 */
21367
21368 /* ":t" - tail, just the basename */
21369 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21370 {
21371 *usedlen += 2;
21372 *fnamelen -= (int)(tail - *fnamep);
21373 *fnamep = tail;
21374 }
21375
21376 /* ":e" - extension, can be repeated */
21377 /* ":r" - root, without extension, can be repeated */
21378 while (src[*usedlen] == ':'
21379 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21380 {
21381 /* find a '.' in the tail:
21382 * - for second :e: before the current fname
21383 * - otherwise: The last '.'
21384 */
21385 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21386 s = *fnamep - 2;
21387 else
21388 s = *fnamep + *fnamelen - 1;
21389 for ( ; s > tail; --s)
21390 if (s[0] == '.')
21391 break;
21392 if (src[*usedlen + 1] == 'e') /* :e */
21393 {
21394 if (s > tail)
21395 {
21396 *fnamelen += (int)(*fnamep - (s + 1));
21397 *fnamep = s + 1;
21398#ifdef VMS
21399 /* cut version from the extension */
21400 s = *fnamep + *fnamelen - 1;
21401 for ( ; s > *fnamep; --s)
21402 if (s[0] == ';')
21403 break;
21404 if (s > *fnamep)
21405 *fnamelen = s - *fnamep;
21406#endif
21407 }
21408 else if (*fnamep <= tail)
21409 *fnamelen = 0;
21410 }
21411 else /* :r */
21412 {
21413 if (s > tail) /* remove one extension */
21414 *fnamelen = (int)(s - *fnamep);
21415 }
21416 *usedlen += 2;
21417 }
21418
21419 /* ":s?pat?foo?" - substitute */
21420 /* ":gs?pat?foo?" - global substitute */
21421 if (src[*usedlen] == ':'
21422 && (src[*usedlen + 1] == 's'
21423 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21424 {
21425 char_u *str;
21426 char_u *pat;
21427 char_u *sub;
21428 int sep;
21429 char_u *flags;
21430 int didit = FALSE;
21431
21432 flags = (char_u *)"";
21433 s = src + *usedlen + 2;
21434 if (src[*usedlen + 1] == 'g')
21435 {
21436 flags = (char_u *)"g";
21437 ++s;
21438 }
21439
21440 sep = *s++;
21441 if (sep)
21442 {
21443 /* find end of pattern */
21444 p = vim_strchr(s, sep);
21445 if (p != NULL)
21446 {
21447 pat = vim_strnsave(s, (int)(p - s));
21448 if (pat != NULL)
21449 {
21450 s = p + 1;
21451 /* find end of substitution */
21452 p = vim_strchr(s, sep);
21453 if (p != NULL)
21454 {
21455 sub = vim_strnsave(s, (int)(p - s));
21456 str = vim_strnsave(*fnamep, *fnamelen);
21457 if (sub != NULL && str != NULL)
21458 {
21459 *usedlen = (int)(p + 1 - src);
21460 s = do_string_sub(str, pat, sub, flags);
21461 if (s != NULL)
21462 {
21463 *fnamep = s;
21464 *fnamelen = (int)STRLEN(s);
21465 vim_free(*bufp);
21466 *bufp = s;
21467 didit = TRUE;
21468 }
21469 }
21470 vim_free(sub);
21471 vim_free(str);
21472 }
21473 vim_free(pat);
21474 }
21475 }
21476 /* after using ":s", repeat all the modifiers */
21477 if (didit)
21478 goto repeat;
21479 }
21480 }
21481
21482 return valid;
21483}
21484
21485/*
21486 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21487 * "flags" can be "g" to do a global substitute.
21488 * Returns an allocated string, NULL for error.
21489 */
21490 char_u *
21491do_string_sub(str, pat, sub, flags)
21492 char_u *str;
21493 char_u *pat;
21494 char_u *sub;
21495 char_u *flags;
21496{
21497 int sublen;
21498 regmatch_T regmatch;
21499 int i;
21500 int do_all;
21501 char_u *tail;
21502 garray_T ga;
21503 char_u *ret;
21504 char_u *save_cpo;
21505
21506 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21507 save_cpo = p_cpo;
21508 p_cpo = (char_u *)"";
21509
21510 ga_init2(&ga, 1, 200);
21511
21512 do_all = (flags[0] == 'g');
21513
21514 regmatch.rm_ic = p_ic;
21515 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21516 if (regmatch.regprog != NULL)
21517 {
21518 tail = str;
21519 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21520 {
21521 /*
21522 * Get some space for a temporary buffer to do the substitution
21523 * into. It will contain:
21524 * - The text up to where the match is.
21525 * - The substituted text.
21526 * - The text after the match.
21527 */
21528 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21529 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21530 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21531 {
21532 ga_clear(&ga);
21533 break;
21534 }
21535
21536 /* copy the text up to where the match is */
21537 i = (int)(regmatch.startp[0] - tail);
21538 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21539 /* add the substituted text */
21540 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21541 + ga.ga_len + i, TRUE, TRUE, FALSE);
21542 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543 /* avoid getting stuck on a match with an empty string */
21544 if (tail == regmatch.endp[0])
21545 {
21546 if (*tail == NUL)
21547 break;
21548 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21549 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550 }
21551 else
21552 {
21553 tail = regmatch.endp[0];
21554 if (*tail == NUL)
21555 break;
21556 }
21557 if (!do_all)
21558 break;
21559 }
21560
21561 if (ga.ga_data != NULL)
21562 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21563
21564 vim_free(regmatch.regprog);
21565 }
21566
21567 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21568 ga_clear(&ga);
21569 p_cpo = save_cpo;
21570
21571 return ret;
21572}
21573
21574#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */