blob: efa04be1ac002470d3c2d9d48f1463ea32bb37eb [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
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 Moolenaar33570922005-01-25 22:26:29 +0000344};
345
346/* shorthand */
347#define vv_type vv_di.di_tv.v_type
348#define vv_nr vv_di.di_tv.vval.v_number
349#define vv_str vv_di.di_tv.vval.v_string
350#define vv_tv vv_di.di_tv
351
352/*
353 * The v: variables are stored in dictionary "vimvardict".
354 * "vimvars_var" is the variable that is used for the "l:" scope.
355 */
356static dict_T vimvardict;
357static dictitem_T vimvars_var;
358#define vimvarht vimvardict.dv_hashtab
359
Bram Moolenaara40058a2005-07-11 22:42:07 +0000360static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
361static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
362#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
363static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
364#endif
365static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
366static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
367static char_u *skip_var_one __ARGS((char_u *arg));
368static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
369static void list_glob_vars __ARGS((void));
370static void list_buf_vars __ARGS((void));
371static void list_win_vars __ARGS((void));
372static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000373static void list_script_vars __ARGS((void));
374static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000375static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
376static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
377static int check_changedtick __ARGS((char_u *arg));
378static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
379static void clear_lval __ARGS((lval_T *lp));
380static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
381static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
382static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
383static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
384static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
385static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
386static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
387static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
388static void item_lock __ARGS((typval_T *tv, int deep, int lock));
389static int tv_islocked __ARGS((typval_T *tv));
390
Bram Moolenaar33570922005-01-25 22:26:29 +0000391static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
392static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000399
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000400static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000401static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000405static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000406static listitem_T *listitem_alloc __ARGS((void));
407static void listitem_free __ARGS((listitem_T *item));
408static void listitem_remove __ARGS((list_T *l, listitem_T *item));
409static long list_len __ARGS((list_T *l));
410static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
411static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
412static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000414static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static void list_append __ARGS((list_T *l, listitem_T *item));
417static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000418static int list_append_string __ARGS((list_T *l, char_u *str, int len));
419static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
421static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
422static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000423static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000425static char_u *list2string __ARGS((typval_T *tv, int copyID));
426static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000427static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
428static void set_ref_in_list __ARGS((list_T *l, int copyID));
429static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static void dict_unref __ARGS((dict_T *d));
431static void dict_free __ARGS((dict_T *d));
432static dictitem_T *dictitem_alloc __ARGS((char_u *key));
433static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
434static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
435static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000436static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int dict_add __ARGS((dict_T *d, dictitem_T *item));
438static long dict_len __ARGS((dict_T *d));
439static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
443static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static char_u *string_quote __ARGS((char_u *str, int function));
445static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
446static int find_internal_func __ARGS((char_u *name));
447static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
448static 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));
449static 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 Moolenaar81bf7082005-02-12 14:31:42 +0000450static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451
452static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000468static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000472#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000473static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000474static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
476#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
482static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000508static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000510static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000516static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000524static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000525static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000548static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000554static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000571static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000575#ifdef vim_mkdir
576static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
577#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000582static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000583static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000585static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000586static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000599static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000601static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000608static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000609static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000610static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000615static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000616static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000619static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620#ifdef HAVE_STRFTIME
621static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
622#endif
623static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000635static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000636static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000637static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000638static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000639static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000641static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000655static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000660static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
661static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static int get_env_len __ARGS((char_u **arg));
663static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000664static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000665static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
666#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
667#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
668 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000669static 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 +0000670static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000671static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000672static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
673static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static typval_T *alloc_tv __ARGS((void));
675static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void init_tv __ARGS((typval_T *varp));
677static long get_tv_number __ARGS((typval_T *varp));
678static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000679static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static char_u *get_tv_string __ARGS((typval_T *varp));
681static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000682static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000684static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
686static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
687static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
688static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
689static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
690static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
691static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000692static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000694static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
696static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
697static int eval_fname_script __ARGS((char_u *p));
698static int eval_fname_sid __ARGS((char_u *p));
699static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static ufunc_T *find_func __ARGS((char_u *name));
701static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000702static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000703#ifdef FEAT_PROFILE
704static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000705static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
706static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
707static int
708# ifdef __BORLANDC__
709 _RTLENTRYF
710# endif
711 prof_total_cmp __ARGS((const void *s1, const void *s2));
712static int
713# ifdef __BORLANDC__
714 _RTLENTRYF
715# endif
716 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000717#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000718static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000719static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000720static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void func_free __ARGS((ufunc_T *fp));
722static void func_unref __ARGS((char_u *name));
723static void func_ref __ARGS((char_u *name));
724static 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));
725static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000726static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000727static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000728static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000729
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000730/* Character used as separated in autoload function/variable names. */
731#define AUTOLOAD_CHAR '#'
732
Bram Moolenaar33570922005-01-25 22:26:29 +0000733/*
734 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000735 */
736 void
737eval_init()
738{
Bram Moolenaar33570922005-01-25 22:26:29 +0000739 int i;
740 struct vimvar *p;
741
742 init_var_dict(&globvardict, &globvars_var);
743 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000744 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000745 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000746
747 for (i = 0; i < VV_LEN; ++i)
748 {
749 p = &vimvars[i];
750 STRCPY(p->vv_di.di_key, p->vv_name);
751 if (p->vv_flags & VV_RO)
752 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
753 else if (p->vv_flags & VV_RO_SBX)
754 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
755 else
756 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757
758 /* add to v: scope dict, unless the value is not always available */
759 if (p->vv_type != VAR_UNKNOWN)
760 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000761 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000762 /* add to compat scope dict */
763 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000764 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000765}
766
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000767#if defined(EXITFREE) || defined(PROTO)
768 void
769eval_clear()
770{
771 int i;
772 struct vimvar *p;
773
774 for (i = 0; i < VV_LEN; ++i)
775 {
776 p = &vimvars[i];
777 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000778 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000779 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000780 p->vv_di.di_tv.vval.v_string = NULL;
781 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000782 }
783 hash_clear(&vimvarht);
784 hash_clear(&compat_hashtab);
785
786 /* script-local variables */
787 for (i = 1; i <= ga_scripts.ga_len; ++i)
788 vars_clear(&SCRIPT_VARS(i));
789 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000790 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000791
792 /* global variables */
793 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000794
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000795 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000796 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000797 hash_clear(&func_hashtab);
798
799 /* unreferenced lists and dicts */
800 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000801}
802#endif
803
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 * Return the name of the executed function.
806 */
807 char_u *
808func_name(cookie)
809 void *cookie;
810{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000811 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812}
813
814/*
815 * Return the address holding the next breakpoint line for a funccall cookie.
816 */
817 linenr_T *
818func_breakpoint(cookie)
819 void *cookie;
820{
Bram Moolenaar33570922005-01-25 22:26:29 +0000821 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822}
823
824/*
825 * Return the address holding the debug tick for a funccall cookie.
826 */
827 int *
828func_dbg_tick(cookie)
829 void *cookie;
830{
Bram Moolenaar33570922005-01-25 22:26:29 +0000831 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
833
834/*
835 * Return the nesting level for a funccall cookie.
836 */
837 int
838func_level(cookie)
839 void *cookie;
840{
Bram Moolenaar33570922005-01-25 22:26:29 +0000841 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
844/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000845funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
847/*
848 * Return TRUE when a function was ended by a ":return" command.
849 */
850 int
851current_func_returned()
852{
853 return current_funccal->returned;
854}
855
856
857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 * Set an internal variable to a string value. Creates the variable if it does
859 * not already exist.
860 */
861 void
862set_internal_string_var(name, value)
863 char_u *name;
864 char_u *value;
865{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000866 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
869 val = vim_strsave(value);
870 if (val != NULL)
871 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000872 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000873 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000875 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000876 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 }
878 }
879}
880
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000881static lval_T *redir_lval = NULL;
882static char_u *redir_endp = NULL;
883static char_u *redir_varname = NULL;
884
885/*
886 * Start recording command output to a variable
887 * Returns OK if successfully completed the setup. FAIL otherwise.
888 */
889 int
890var_redir_start(name, append)
891 char_u *name;
892 int append; /* append to an existing variable */
893{
894 int save_emsg;
895 int err;
896 typval_T tv;
897
898 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000899 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000900 {
901 EMSG(_(e_invarg));
902 return FAIL;
903 }
904
905 redir_varname = vim_strsave(name);
906 if (redir_varname == NULL)
907 return FAIL;
908
909 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
910 if (redir_lval == NULL)
911 {
912 var_redir_stop();
913 return FAIL;
914 }
915
916 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000917 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
918 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000919 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
920 {
921 if (redir_endp != NULL && *redir_endp != NUL)
922 /* Trailing characters are present after the variable name */
923 EMSG(_(e_trailing));
924 else
925 EMSG(_(e_invarg));
926 var_redir_stop();
927 return FAIL;
928 }
929
930 /* check if we can write to the variable: set it to or append an empty
931 * string */
932 save_emsg = did_emsg;
933 did_emsg = FALSE;
934 tv.v_type = VAR_STRING;
935 tv.vval.v_string = (char_u *)"";
936 if (append)
937 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
938 else
939 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
940 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000941 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000942 if (err)
943 {
944 var_redir_stop();
945 return FAIL;
946 }
947 if (redir_lval->ll_newkey != NULL)
948 {
949 /* Dictionary item was created, don't do it again. */
950 vim_free(redir_lval->ll_newkey);
951 redir_lval->ll_newkey = NULL;
952 }
953
954 return OK;
955}
956
957/*
958 * Append "value[len]" to the variable set by var_redir_start().
959 */
960 void
961var_redir_str(value, len)
962 char_u *value;
963 int len;
964{
965 char_u *val;
966 typval_T tv;
967 int save_emsg;
968 int err;
969
970 if (redir_lval == NULL)
971 return;
972
973 if (len == -1)
974 /* Append the entire string */
975 val = vim_strsave(value);
976 else
977 /* Append only the specified number of characters */
978 val = vim_strnsave(value, len);
979 if (val == NULL)
980 return;
981
982 tv.v_type = VAR_STRING;
983 tv.vval.v_string = val;
984
985 save_emsg = did_emsg;
986 did_emsg = FALSE;
987 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
988 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000989 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000990 if (err)
991 var_redir_stop();
992
993 vim_free(tv.vval.v_string);
994}
995
996/*
997 * Stop redirecting command output to a variable.
998 */
999 void
1000var_redir_stop()
1001{
1002 if (redir_lval != NULL)
1003 {
1004 clear_lval(redir_lval);
1005 vim_free(redir_lval);
1006 redir_lval = NULL;
1007 }
1008 vim_free(redir_varname);
1009 redir_varname = NULL;
1010}
1011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012# if defined(FEAT_MBYTE) || defined(PROTO)
1013 int
1014eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1015 char_u *enc_from;
1016 char_u *enc_to;
1017 char_u *fname_from;
1018 char_u *fname_to;
1019{
1020 int err = FALSE;
1021
1022 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1023 set_vim_var_string(VV_CC_TO, enc_to, -1);
1024 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1025 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1026 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1027 err = TRUE;
1028 set_vim_var_string(VV_CC_FROM, NULL, -1);
1029 set_vim_var_string(VV_CC_TO, NULL, -1);
1030 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1031 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1032
1033 if (err)
1034 return FAIL;
1035 return OK;
1036}
1037# endif
1038
1039# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1040 int
1041eval_printexpr(fname, args)
1042 char_u *fname;
1043 char_u *args;
1044{
1045 int err = FALSE;
1046
1047 set_vim_var_string(VV_FNAME_IN, fname, -1);
1048 set_vim_var_string(VV_CMDARG, args, -1);
1049 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1050 err = TRUE;
1051 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1052 set_vim_var_string(VV_CMDARG, NULL, -1);
1053
1054 if (err)
1055 {
1056 mch_remove(fname);
1057 return FAIL;
1058 }
1059 return OK;
1060}
1061# endif
1062
1063# if defined(FEAT_DIFF) || defined(PROTO)
1064 void
1065eval_diff(origfile, newfile, outfile)
1066 char_u *origfile;
1067 char_u *newfile;
1068 char_u *outfile;
1069{
1070 int err = FALSE;
1071
1072 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1073 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1074 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1075 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1076 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1077 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1078 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1079}
1080
1081 void
1082eval_patch(origfile, difffile, outfile)
1083 char_u *origfile;
1084 char_u *difffile;
1085 char_u *outfile;
1086{
1087 int err;
1088
1089 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1090 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1091 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1092 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1093 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1094 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1095 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1096}
1097# endif
1098
1099/*
1100 * Top level evaluation function, returning a boolean.
1101 * Sets "error" to TRUE if there was an error.
1102 * Return TRUE or FALSE.
1103 */
1104 int
1105eval_to_bool(arg, error, nextcmd, skip)
1106 char_u *arg;
1107 int *error;
1108 char_u **nextcmd;
1109 int skip; /* only parse, don't execute */
1110{
Bram Moolenaar33570922005-01-25 22:26:29 +00001111 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 int retval = FALSE;
1113
1114 if (skip)
1115 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001116 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 else
1119 {
1120 *error = FALSE;
1121 if (!skip)
1122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001123 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001124 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126 }
1127 if (skip)
1128 --emsg_skip;
1129
1130 return retval;
1131}
1132
1133/*
1134 * Top level evaluation function, returning a string. If "skip" is TRUE,
1135 * only parsing to "nextcmd" is done, without reporting errors. Return
1136 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1137 */
1138 char_u *
1139eval_to_string_skip(arg, nextcmd, skip)
1140 char_u *arg;
1141 char_u **nextcmd;
1142 int skip; /* only parse, don't execute */
1143{
Bram Moolenaar33570922005-01-25 22:26:29 +00001144 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 char_u *retval;
1146
1147 if (skip)
1148 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001149 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 retval = NULL;
1151 else
1152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 retval = vim_strsave(get_tv_string(&tv));
1154 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 }
1156 if (skip)
1157 --emsg_skip;
1158
1159 return retval;
1160}
1161
1162/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001163 * Skip over an expression at "*pp".
1164 * Return FAIL for an error, OK otherwise.
1165 */
1166 int
1167skip_expr(pp)
1168 char_u **pp;
1169{
Bram Moolenaar33570922005-01-25 22:26:29 +00001170 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001171
1172 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001173 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001174}
1175
1176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 * Top level evaluation function, returning a string.
1178 * Return pointer to allocated memory, or NULL for failure.
1179 */
1180 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001181eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 char_u *arg;
1183 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001184 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185{
Bram Moolenaar33570922005-01-25 22:26:29 +00001186 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001188 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001190 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 retval = NULL;
1192 else
1193 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001194 if (dolist && tv.v_type == VAR_LIST)
1195 {
1196 ga_init2(&ga, (int)sizeof(char), 80);
1197 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1198 ga_append(&ga, NUL);
1199 retval = (char_u *)ga.ga_data;
1200 }
1201 else
1202 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205
1206 return retval;
1207}
1208
1209/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001210 * Call eval_to_string() without using current local variables and using
1211 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 */
1213 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001214eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 char_u *arg;
1216 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001217 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218{
1219 char_u *retval;
1220 void *save_funccalp;
1221
1222 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001223 if (use_sandbox)
1224 ++sandbox;
1225 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001226 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001227 if (use_sandbox)
1228 --sandbox;
1229 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 restore_funccal(save_funccalp);
1231 return retval;
1232}
1233
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234/*
1235 * Top level evaluation function, returning a number.
1236 * Evaluates "expr" silently.
1237 * Returns -1 for an error.
1238 */
1239 int
1240eval_to_number(expr)
1241 char_u *expr;
1242{
Bram Moolenaar33570922005-01-25 22:26:29 +00001243 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001245 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
1247 ++emsg_off;
1248
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001249 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 retval = -1;
1251 else
1252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001253 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001254 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 }
1256 --emsg_off;
1257
1258 return retval;
1259}
1260
Bram Moolenaara40058a2005-07-11 22:42:07 +00001261/*
1262 * Prepare v: variable "idx" to be used.
1263 * Save the current typeval in "save_tv".
1264 * When not used yet add the variable to the v: hashtable.
1265 */
1266 static void
1267prepare_vimvar(idx, save_tv)
1268 int idx;
1269 typval_T *save_tv;
1270{
1271 *save_tv = vimvars[idx].vv_tv;
1272 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1273 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1274}
1275
1276/*
1277 * Restore v: variable "idx" to typeval "save_tv".
1278 * When no longer defined, remove the variable from the v: hashtable.
1279 */
1280 static void
1281restore_vimvar(idx, save_tv)
1282 int idx;
1283 typval_T *save_tv;
1284{
1285 hashitem_T *hi;
1286
1287 clear_tv(&vimvars[idx].vv_tv);
1288 vimvars[idx].vv_tv = *save_tv;
1289 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1290 {
1291 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1292 if (HASHITEM_EMPTY(hi))
1293 EMSG2(_(e_intern2), "restore_vimvar()");
1294 else
1295 hash_remove(&vimvarht, hi);
1296 }
1297}
1298
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001299#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001300/*
1301 * Evaluate an expression to a list with suggestions.
1302 * For the "expr:" part of 'spellsuggest'.
1303 */
1304 list_T *
1305eval_spell_expr(badword, expr)
1306 char_u *badword;
1307 char_u *expr;
1308{
1309 typval_T save_val;
1310 typval_T rettv;
1311 list_T *list = NULL;
1312 char_u *p = skipwhite(expr);
1313
1314 /* Set "v:val" to the bad word. */
1315 prepare_vimvar(VV_VAL, &save_val);
1316 vimvars[VV_VAL].vv_type = VAR_STRING;
1317 vimvars[VV_VAL].vv_str = badword;
1318 if (p_verbose == 0)
1319 ++emsg_off;
1320
1321 if (eval1(&p, &rettv, TRUE) == OK)
1322 {
1323 if (rettv.v_type != VAR_LIST)
1324 clear_tv(&rettv);
1325 else
1326 list = rettv.vval.v_list;
1327 }
1328
1329 if (p_verbose == 0)
1330 --emsg_off;
1331 vimvars[VV_VAL].vv_str = NULL;
1332 restore_vimvar(VV_VAL, &save_val);
1333
1334 return list;
1335}
1336
1337/*
1338 * "list" is supposed to contain two items: a word and a number. Return the
1339 * word in "pp" and the number as the return value.
1340 * Return -1 if anything isn't right.
1341 * Used to get the good word and score from the eval_spell_expr() result.
1342 */
1343 int
1344get_spellword(list, pp)
1345 list_T *list;
1346 char_u **pp;
1347{
1348 listitem_T *li;
1349
1350 li = list->lv_first;
1351 if (li == NULL)
1352 return -1;
1353 *pp = get_tv_string(&li->li_tv);
1354
1355 li = li->li_next;
1356 if (li == NULL)
1357 return -1;
1358 return get_tv_number(&li->li_tv);
1359}
1360#endif
1361
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001362/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001363 * Top level evaluation function.
1364 * Returns an allocated typval_T with the result.
1365 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001366 */
1367 typval_T *
1368eval_expr(arg, nextcmd)
1369 char_u *arg;
1370 char_u **nextcmd;
1371{
1372 typval_T *tv;
1373
1374 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001375 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001376 {
1377 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001378 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001379 }
1380
1381 return tv;
1382}
1383
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1386/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001387 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001389 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001391 static int
1392call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 char_u *func;
1394 int argc;
1395 char_u **argv;
1396 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
Bram Moolenaar33570922005-01-25 22:26:29 +00001399 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 long n;
1401 int len;
1402 int i;
1403 int doesrange;
1404 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001405 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
Bram Moolenaar33570922005-01-25 22:26:29 +00001407 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001409 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
1411 for (i = 0; i < argc; i++)
1412 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001413 /* Pass a NULL or empty argument as an empty string */
1414 if (argv[i] == NULL || *argv[i] == NUL)
1415 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001416 argvars[i].v_type = VAR_STRING;
1417 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001418 continue;
1419 }
1420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 /* Recognize a number argument, the others must be strings. */
1422 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1423 if (len != 0 && len == (int)STRLEN(argv[i]))
1424 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001425 argvars[i].v_type = VAR_NUMBER;
1426 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
1428 else
1429 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001430 argvars[i].v_type = VAR_STRING;
1431 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433 }
1434
1435 if (safe)
1436 {
1437 save_funccalp = save_funccal();
1438 ++sandbox;
1439 }
1440
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001441 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1442 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001444 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 if (safe)
1446 {
1447 --sandbox;
1448 restore_funccal(save_funccalp);
1449 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001450 vim_free(argvars);
1451
1452 if (ret == FAIL)
1453 clear_tv(rettv);
1454
1455 return ret;
1456}
1457
1458/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001459 * Call vimL function "func" and return the result as a string.
1460 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001461 * Uses argv[argc] for the function arguments.
1462 */
1463 void *
1464call_func_retstr(func, argc, argv, safe)
1465 char_u *func;
1466 int argc;
1467 char_u **argv;
1468 int safe; /* use the sandbox */
1469{
1470 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001471 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001472
1473 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1474 return NULL;
1475
1476 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001477 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 return retval;
1479}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001480
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001481#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001482/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001483 * Call vimL function "func" and return the result as a number.
1484 * Returns -1 when calling the function fails.
1485 * Uses argv[argc] for the function arguments.
1486 */
1487 long
1488call_func_retnr(func, argc, argv, safe)
1489 char_u *func;
1490 int argc;
1491 char_u **argv;
1492 int safe; /* use the sandbox */
1493{
1494 typval_T rettv;
1495 long retval;
1496
1497 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1498 return -1;
1499
1500 retval = get_tv_number_chk(&rettv, NULL);
1501 clear_tv(&rettv);
1502 return retval;
1503}
1504#endif
1505
1506/*
1507 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001508 * Uses argv[argc] for the function arguments.
1509 */
1510 void *
1511call_func_retlist(func, argc, argv, safe)
1512 char_u *func;
1513 int argc;
1514 char_u **argv;
1515 int safe; /* use the sandbox */
1516{
1517 typval_T rettv;
1518
1519 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1520 return NULL;
1521
1522 if (rettv.v_type != VAR_LIST)
1523 {
1524 clear_tv(&rettv);
1525 return NULL;
1526 }
1527
1528 return rettv.vval.v_list;
1529}
1530
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531#endif
1532
1533/*
1534 * Save the current function call pointer, and set it to NULL.
1535 * Used when executing autocommands and for ":source".
1536 */
1537 void *
1538save_funccal()
1539{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001540 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 current_funccal = NULL;
1543 return (void *)fc;
1544}
1545
1546 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001547restore_funccal(vfc)
1548 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001550 funccall_T *fc = (funccall_T *)vfc;
1551
1552 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553}
1554
Bram Moolenaar05159a02005-02-26 23:04:13 +00001555#if defined(FEAT_PROFILE) || defined(PROTO)
1556/*
1557 * Prepare profiling for entering a child or something else that is not
1558 * counted for the script/function itself.
1559 * Should always be called in pair with prof_child_exit().
1560 */
1561 void
1562prof_child_enter(tm)
1563 proftime_T *tm; /* place to store waittime */
1564{
1565 funccall_T *fc = current_funccal;
1566
1567 if (fc != NULL && fc->func->uf_profiling)
1568 profile_start(&fc->prof_child);
1569 script_prof_save(tm);
1570}
1571
1572/*
1573 * Take care of time spent in a child.
1574 * Should always be called after prof_child_enter().
1575 */
1576 void
1577prof_child_exit(tm)
1578 proftime_T *tm; /* where waittime was stored */
1579{
1580 funccall_T *fc = current_funccal;
1581
1582 if (fc != NULL && fc->func->uf_profiling)
1583 {
1584 profile_end(&fc->prof_child);
1585 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1586 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1587 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1588 }
1589 script_prof_restore(tm);
1590}
1591#endif
1592
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594#ifdef FEAT_FOLDING
1595/*
1596 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1597 * it in "*cp". Doesn't give error messages.
1598 */
1599 int
1600eval_foldexpr(arg, cp)
1601 char_u *arg;
1602 int *cp;
1603{
Bram Moolenaar33570922005-01-25 22:26:29 +00001604 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 int retval;
1606 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001607 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1608 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
1610 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001611 if (use_sandbox)
1612 ++sandbox;
1613 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001615 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 retval = 0;
1617 else
1618 {
1619 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001620 if (tv.v_type == VAR_NUMBER)
1621 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001622 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 retval = 0;
1624 else
1625 {
1626 /* If the result is a string, check if there is a non-digit before
1627 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (!VIM_ISDIGIT(*s) && *s != '-')
1630 *cp = *s++;
1631 retval = atol((char *)s);
1632 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001633 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
1635 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001636 if (use_sandbox)
1637 --sandbox;
1638 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
1640 return retval;
1641}
1642#endif
1643
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001645 * ":let" list all variable values
1646 * ":let var1 var2" list variable values
1647 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001648 * ":let var += expr" assignment command.
1649 * ":let var -= expr" assignment command.
1650 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001651 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 */
1653 void
1654ex_let(eap)
1655 exarg_T *eap;
1656{
1657 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001659 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001661 int var_count = 0;
1662 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001663 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001664 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaardb552d602006-03-23 22:59:57 +00001666 argend = skip_var_list(arg, &var_count, &semicolon);
1667 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668 return;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001669 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001672 /*
1673 * ":let" without "=": list variables
1674 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 if (*arg == '[')
1676 EMSG(_(e_invarg));
1677 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001678 /* ":let var1 var2" */
1679 arg = list_arg_vars(eap, arg);
1680 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001683 list_glob_vars();
1684 list_buf_vars();
1685 list_win_vars();
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001686 list_script_vars();
1687 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001688 list_vim_vars();
1689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 eap->nextcmd = check_nextcmd(arg);
1691 }
1692 else
1693 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001694 op[0] = '=';
1695 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001696 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001697 {
1698 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1699 op[0] = expr[-1]; /* +=, -= or .= */
1700 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 if (eap->skip)
1704 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if (eap->skip)
1707 {
1708 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001709 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 --emsg_skip;
1711 }
1712 else if (i != FAIL)
1713 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001715 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001716 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 }
1718 }
1719}
1720
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001721/*
1722 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1723 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001724 * When "nextchars" is not NULL it points to a string with characters that
1725 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1726 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727 * Returns OK or FAIL;
1728 */
1729 static int
1730ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1731 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001732 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001733 int copy; /* copy values from "tv", don't move */
1734 int semicolon; /* from skip_var_list() */
1735 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001736 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001737{
1738 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001739 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001740 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001741 listitem_T *item;
1742 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001743
1744 if (*arg != '[')
1745 {
1746 /*
1747 * ":let var = expr" or ":for var in list"
1748 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001749 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 return FAIL;
1751 return OK;
1752 }
1753
1754 /*
1755 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1756 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001757 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001758 {
1759 EMSG(_(e_listreq));
1760 return FAIL;
1761 }
1762
1763 i = list_len(l);
1764 if (semicolon == 0 && var_count < i)
1765 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001766 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001767 return FAIL;
1768 }
1769 if (var_count - semicolon > i)
1770 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001771 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 return FAIL;
1773 }
1774
1775 item = l->lv_first;
1776 while (*arg != ']')
1777 {
1778 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001779 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 item = item->li_next;
1781 if (arg == NULL)
1782 return FAIL;
1783
1784 arg = skipwhite(arg);
1785 if (*arg == ';')
1786 {
1787 /* Put the rest of the list (may be empty) in the var after ';'.
1788 * Create a new list for this. */
1789 l = list_alloc();
1790 if (l == NULL)
1791 return FAIL;
1792 while (item != NULL)
1793 {
1794 list_append_tv(l, &item->li_tv);
1795 item = item->li_next;
1796 }
1797
1798 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001799 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001800 ltv.vval.v_list = l;
1801 l->lv_refcount = 1;
1802
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001803 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1804 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001805 clear_tv(&ltv);
1806 if (arg == NULL)
1807 return FAIL;
1808 break;
1809 }
1810 else if (*arg != ',' && *arg != ']')
1811 {
1812 EMSG2(_(e_intern2), "ex_let_vars()");
1813 return FAIL;
1814 }
1815 }
1816
1817 return OK;
1818}
1819
1820/*
1821 * Skip over assignable variable "var" or list of variables "[var, var]".
1822 * Used for ":let varvar = expr" and ":for varvar in expr".
1823 * For "[var, var]" increment "*var_count" for each variable.
1824 * for "[var, var; var]" set "semicolon".
1825 * Return NULL for an error.
1826 */
1827 static char_u *
1828skip_var_list(arg, var_count, semicolon)
1829 char_u *arg;
1830 int *var_count;
1831 int *semicolon;
1832{
1833 char_u *p, *s;
1834
1835 if (*arg == '[')
1836 {
1837 /* "[var, var]": find the matching ']'. */
1838 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001839 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001840 {
1841 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1842 s = skip_var_one(p);
1843 if (s == p)
1844 {
1845 EMSG2(_(e_invarg2), p);
1846 return NULL;
1847 }
1848 ++*var_count;
1849
1850 p = skipwhite(s);
1851 if (*p == ']')
1852 break;
1853 else if (*p == ';')
1854 {
1855 if (*semicolon == 1)
1856 {
1857 EMSG(_("Double ; in list of variables"));
1858 return NULL;
1859 }
1860 *semicolon = 1;
1861 }
1862 else if (*p != ',')
1863 {
1864 EMSG2(_(e_invarg2), p);
1865 return NULL;
1866 }
1867 }
1868 return p + 1;
1869 }
1870 else
1871 return skip_var_one(arg);
1872}
1873
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001874/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001875 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1876 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001877 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 static char_u *
1879skip_var_one(arg)
1880 char_u *arg;
1881{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001882 if (*arg == '@' && arg[1] != NUL)
1883 return arg + 2;
1884 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1885 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886}
1887
Bram Moolenaara7043832005-01-21 11:56:39 +00001888/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001889 * List variables for hashtab "ht" with prefix "prefix".
1890 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001891 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001893list_hashtable_vars(ht, prefix, empty)
1894 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001895 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001896 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001897{
Bram Moolenaar33570922005-01-25 22:26:29 +00001898 hashitem_T *hi;
1899 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001900 int todo;
1901
1902 todo = ht->ht_used;
1903 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1904 {
1905 if (!HASHITEM_EMPTY(hi))
1906 {
1907 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001908 di = HI2DI(hi);
1909 if (empty || di->di_tv.v_type != VAR_STRING
1910 || di->di_tv.vval.v_string != NULL)
1911 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001912 }
1913 }
1914}
1915
1916/*
1917 * List global variables.
1918 */
1919 static void
1920list_glob_vars()
1921{
Bram Moolenaar33570922005-01-25 22:26:29 +00001922 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001923}
1924
1925/*
1926 * List buffer variables.
1927 */
1928 static void
1929list_buf_vars()
1930{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001931 char_u numbuf[NUMBUFLEN];
1932
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001934
1935 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1936 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001937}
1938
1939/*
1940 * List window variables.
1941 */
1942 static void
1943list_win_vars()
1944{
Bram Moolenaar33570922005-01-25 22:26:29 +00001945 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001946}
1947
1948/*
1949 * List Vim variables.
1950 */
1951 static void
1952list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953{
Bram Moolenaar33570922005-01-25 22:26:29 +00001954 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955}
1956
1957/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001958 * List script-local variables, if there is a script.
1959 */
1960 static void
1961list_script_vars()
1962{
1963 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1964 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1965}
1966
1967/*
1968 * List function variables, if there is a function.
1969 */
1970 static void
1971list_func_vars()
1972{
1973 if (current_funccal != NULL)
1974 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
1975 (char_u *)"l:", FALSE);
1976}
1977
1978/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001979 * List variables in "arg".
1980 */
1981 static char_u *
1982list_arg_vars(eap, arg)
1983 exarg_T *eap;
1984 char_u *arg;
1985{
1986 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001987 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001989 char_u *name_start;
1990 char_u *arg_subsc;
1991 char_u *tofree;
1992 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001993
1994 while (!ends_excmd(*arg) && !got_int)
1995 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001996 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001997 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001998 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001999 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2000 {
2001 emsg_severe = TRUE;
2002 EMSG(_(e_trailing));
2003 break;
2004 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002005 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002006 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002007 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002008 /* get_name_len() takes care of expanding curly braces */
2009 name_start = name = arg;
2010 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2011 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002013 /* This is mainly to keep test 49 working: when expanding
2014 * curly braces fails overrule the exception error message. */
2015 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002016 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002017 emsg_severe = TRUE;
2018 EMSG2(_(e_invarg2), arg);
2019 break;
2020 }
2021 error = TRUE;
2022 }
2023 else
2024 {
2025 if (tofree != NULL)
2026 name = tofree;
2027 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002028 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002029 else
2030 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002031 /* handle d.key, l[idx], f(expr) */
2032 arg_subsc = arg;
2033 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002034 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002035 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002036 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002037 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002038 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002039 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002040 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002041 case 'g': list_glob_vars(); break;
2042 case 'b': list_buf_vars(); break;
2043 case 'w': list_win_vars(); break;
2044 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002045 case 's': list_script_vars(); break;
2046 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002047 default:
2048 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002049 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002050 }
2051 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002052 {
2053 char_u numbuf[NUMBUFLEN];
2054 char_u *tf;
2055 int c;
2056 char_u *s;
2057
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002058 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002059 c = *arg;
2060 *arg = NUL;
2061 list_one_var_a((char_u *)"",
2062 arg == arg_subsc ? name : name_start,
2063 tv.v_type, s == NULL ? (char_u *)"" : s);
2064 *arg = c;
2065 vim_free(tf);
2066 }
2067 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002068 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002069 }
2070 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002071
2072 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074
2075 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002076 }
2077
2078 return arg;
2079}
2080
2081/*
2082 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2083 * Returns a pointer to the char just after the var name.
2084 * Returns NULL if there is an error.
2085 */
2086 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002087ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002088 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002089 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002091 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002092 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002093{
2094 int c1;
2095 char_u *name;
2096 char_u *p;
2097 char_u *arg_end = NULL;
2098 int len;
2099 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002100 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002101
2102 /*
2103 * ":let $VAR = expr": Set environment variable.
2104 */
2105 if (*arg == '$')
2106 {
2107 /* Find the end of the name. */
2108 ++arg;
2109 name = arg;
2110 len = get_env_len(&arg);
2111 if (len == 0)
2112 EMSG2(_(e_invarg2), name - 1);
2113 else
2114 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002115 if (op != NULL && (*op == '+' || *op == '-'))
2116 EMSG2(_(e_letwrong), op);
2117 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002118 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002119 EMSG(_(e_letunexp));
2120 else
2121 {
2122 c1 = name[len];
2123 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002124 p = get_tv_string_chk(tv);
2125 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002126 {
2127 int mustfree = FALSE;
2128 char_u *s = vim_getenv(name, &mustfree);
2129
2130 if (s != NULL)
2131 {
2132 p = tofree = concat_str(s, p);
2133 if (mustfree)
2134 vim_free(s);
2135 }
2136 }
2137 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002138 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002139 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002140 if (STRICMP(name, "HOME") == 0)
2141 init_homedir();
2142 else if (didset_vim && STRICMP(name, "VIM") == 0)
2143 didset_vim = FALSE;
2144 else if (didset_vimruntime
2145 && STRICMP(name, "VIMRUNTIME") == 0)
2146 didset_vimruntime = FALSE;
2147 arg_end = arg;
2148 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002150 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002151 }
2152 }
2153 }
2154
2155 /*
2156 * ":let &option = expr": Set option value.
2157 * ":let &l:option = expr": Set local option value.
2158 * ":let &g:option = expr": Set global option value.
2159 */
2160 else if (*arg == '&')
2161 {
2162 /* Find the end of the name. */
2163 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002164 if (p == NULL || (endchars != NULL
2165 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166 EMSG(_(e_letunexp));
2167 else
2168 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002169 long n;
2170 int opt_type;
2171 long numval;
2172 char_u *stringval = NULL;
2173 char_u *s;
2174
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 c1 = *p;
2176 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002177
2178 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002179 s = get_tv_string_chk(tv); /* != NULL if number or string */
2180 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002181 {
2182 opt_type = get_option_value(arg, &numval,
2183 &stringval, opt_flags);
2184 if ((opt_type == 1 && *op == '.')
2185 || (opt_type == 0 && *op != '.'))
2186 EMSG2(_(e_letwrong), op);
2187 else
2188 {
2189 if (opt_type == 1) /* number */
2190 {
2191 if (*op == '+')
2192 n = numval + n;
2193 else
2194 n = numval - n;
2195 }
2196 else if (opt_type == 0 && stringval != NULL) /* string */
2197 {
2198 s = concat_str(stringval, s);
2199 vim_free(stringval);
2200 stringval = s;
2201 }
2202 }
2203 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002204 if (s != NULL)
2205 {
2206 set_option_value(arg, n, s, opt_flags);
2207 arg_end = p;
2208 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002210 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 }
2212 }
2213
2214 /*
2215 * ":let @r = expr": Set register contents.
2216 */
2217 else if (*arg == '@')
2218 {
2219 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002220 if (op != NULL && (*op == '+' || *op == '-'))
2221 EMSG2(_(e_letwrong), op);
2222 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002223 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 EMSG(_(e_letunexp));
2225 else
2226 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002227 char_u *tofree = NULL;
2228 char_u *s;
2229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002230 p = get_tv_string_chk(tv);
2231 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002232 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002233 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002234 if (s != NULL)
2235 {
2236 p = tofree = concat_str(s, p);
2237 vim_free(s);
2238 }
2239 }
2240 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002241 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002242 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002243 arg_end = arg + 1;
2244 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002245 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 }
2247 }
2248
2249 /*
2250 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002253 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002255 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002257 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2261 EMSG(_(e_letunexp));
2262 else
2263 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002264 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 arg_end = p;
2266 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 }
2270
2271 else
2272 EMSG2(_(e_invarg2), arg);
2273
2274 return arg_end;
2275}
2276
2277/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002278 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2279 */
2280 static int
2281check_changedtick(arg)
2282 char_u *arg;
2283{
2284 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2285 {
2286 EMSG2(_(e_readonlyvar), arg);
2287 return TRUE;
2288 }
2289 return FALSE;
2290}
2291
2292/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002293 * Get an lval: variable, Dict item or List item that can be assigned a value
2294 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2295 * "name.key", "name.key[expr]" etc.
2296 * Indexing only works if "name" is an existing List or Dictionary.
2297 * "name" points to the start of the name.
2298 * If "rettv" is not NULL it points to the value to be assigned.
2299 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2300 * wrong; must end in space or cmd separator.
2301 *
2302 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002303 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 */
2306 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002307get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002309 typval_T *rettv;
2310 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002311 int unlet;
2312 int skip;
2313 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002314 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002317 char_u *expr_start, *expr_end;
2318 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002319 dictitem_T *v;
2320 typval_T var1;
2321 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002323 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002324 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002325 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002326 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002328 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002329 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330
2331 if (skip)
2332 {
2333 /* When skipping just find the end of the name. */
2334 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002335 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 }
2337
2338 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002339 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 if (expr_start != NULL)
2341 {
2342 /* Don't expand the name when we already know there is an error. */
2343 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2344 && *p != '[' && *p != '.')
2345 {
2346 EMSG(_(e_trailing));
2347 return NULL;
2348 }
2349
2350 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2351 if (lp->ll_exp_name == NULL)
2352 {
2353 /* Report an invalid expression in braces, unless the
2354 * expression evaluation has been cancelled due to an
2355 * aborting error, an interrupt, or an exception. */
2356 if (!aborting() && !quiet)
2357 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002358 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 EMSG2(_(e_invarg2), name);
2360 return NULL;
2361 }
2362 }
2363 lp->ll_name = lp->ll_exp_name;
2364 }
2365 else
2366 lp->ll_name = name;
2367
2368 /* Without [idx] or .key we are done. */
2369 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2370 return p;
2371
2372 cc = *p;
2373 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002374 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 if (v == NULL && !quiet)
2376 EMSG2(_(e_undefvar), lp->ll_name);
2377 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 if (v == NULL)
2379 return NULL;
2380
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002381 /*
2382 * Loop until no more [idx] or .key is following.
2383 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002384 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002385 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2388 && !(lp->ll_tv->v_type == VAR_DICT
2389 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 if (!quiet)
2392 EMSG(_("E689: Can only index a List or Dictionary"));
2393 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002396 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 if (!quiet)
2398 EMSG(_("E708: [:] must come last"));
2399 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002401
Bram Moolenaar8c711452005-01-14 21:53:12 +00002402 len = -1;
2403 if (*p == '.')
2404 {
2405 key = p + 1;
2406 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2407 ;
2408 if (len == 0)
2409 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 if (!quiet)
2411 EMSG(_(e_emptykey));
2412 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002413 }
2414 p = key + len;
2415 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002416 else
2417 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002418 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002419 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002420 if (*p == ':')
2421 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002422 else
2423 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002424 empty1 = FALSE;
2425 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002426 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002427 if (get_tv_string_chk(&var1) == NULL)
2428 {
2429 /* not a number or string */
2430 clear_tv(&var1);
2431 return NULL;
2432 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002433 }
2434
2435 /* Optionally get the second index [ :expr]. */
2436 if (*p == ':')
2437 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002439 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002442 if (!empty1)
2443 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002445 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (rettv != NULL && (rettv->v_type != VAR_LIST
2447 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 if (!quiet)
2450 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 if (!empty1)
2452 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 }
2455 p = skipwhite(p + 1);
2456 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 else
2459 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2462 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 if (!empty1)
2464 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002466 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002467 if (get_tv_string_chk(&var2) == NULL)
2468 {
2469 /* not a number or string */
2470 if (!empty1)
2471 clear_tv(&var1);
2472 clear_tv(&var2);
2473 return NULL;
2474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002477 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002478 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002480
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002482 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 if (!quiet)
2484 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002485 if (!empty1)
2486 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 }
2491
2492 /* Skip to past ']'. */
2493 ++p;
2494 }
2495
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 {
2498 if (len == -1)
2499 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 if (*key == NUL)
2503 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 if (!quiet)
2505 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002508 }
2509 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 lp->ll_list = NULL;
2511 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002512 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002515 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002517 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002519 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 if (len == -1)
2521 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 }
2524 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 if (len == -1)
2529 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 p = NULL;
2532 break;
2533 }
2534 if (len == -1)
2535 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 }
2538 else
2539 {
2540 /*
2541 * Get the number and item for the only or first index of the List.
2542 */
2543 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002545 else
2546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002547 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002548 clear_tv(&var1);
2549 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002550 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 lp->ll_list = lp->ll_tv->vval.v_list;
2552 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2553 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002554 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (!quiet)
2556 EMSGN(_(e_listidx), lp->ll_n1);
2557 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002558 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 }
2561
2562 /*
2563 * May need to find the item or absolute index for the second
2564 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 * When no index given: "lp->ll_empty2" is TRUE.
2566 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002570 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002571 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 if (ni == NULL)
2576 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 if (!quiet)
2578 EMSGN(_(e_listidx), lp->ll_n2);
2579 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 }
2583
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2585 if (lp->ll_n1 < 0)
2586 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2587 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002588 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (!quiet)
2590 EMSGN(_(e_listidx), lp->ll_n2);
2591 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002592 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002593 }
2594
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002597 }
2598
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 return p;
2600}
2601
2602/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002603 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 */
2605 static void
2606clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002607 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608{
2609 vim_free(lp->ll_exp_name);
2610 vim_free(lp->ll_newkey);
2611}
2612
2613/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002614 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002616 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 */
2618 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002619set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002620 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002622 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002624 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625{
2626 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 listitem_T *ri;
2628 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629
2630 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 cc = *endp;
2635 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002636 if (op != NULL && *op != '=')
2637 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002638 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002639
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002640 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002641 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2642 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002643 {
2644 if (tv_op(&tv, rettv, op) == OK)
2645 set_var(lp->ll_name, &tv, FALSE);
2646 clear_tv(&tv);
2647 }
2648 }
2649 else
2650 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002654 else if (tv_check_lock(lp->ll_newkey == NULL
2655 ? lp->ll_tv->v_lock
2656 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2657 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 else if (lp->ll_range)
2659 {
2660 /*
2661 * Assign the List values to the list items.
2662 */
2663 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002665 if (op != NULL && *op != '=')
2666 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2667 else
2668 {
2669 clear_tv(&lp->ll_li->li_tv);
2670 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2671 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 ri = ri->li_next;
2673 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2674 break;
2675 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002678 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 ri = NULL;
2681 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002682 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 lp->ll_li = lp->ll_li->li_next;
2685 ++lp->ll_n1;
2686 }
2687 if (ri != NULL)
2688 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002689 else if (lp->ll_empty2
2690 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 : lp->ll_n1 != lp->ll_n2)
2692 EMSG(_("E711: List value has not enough items"));
2693 }
2694 else
2695 {
2696 /*
2697 * Assign to a List or Dictionary item.
2698 */
2699 if (lp->ll_newkey != NULL)
2700 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002701 if (op != NULL && *op != '=')
2702 {
2703 EMSG2(_(e_letwrong), op);
2704 return;
2705 }
2706
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002708 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 if (di == NULL)
2710 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002711 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2712 {
2713 vim_free(di);
2714 return;
2715 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002718 else if (op != NULL && *op != '=')
2719 {
2720 tv_op(lp->ll_tv, rettv, op);
2721 return;
2722 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002723 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 /*
2727 * Assign the value to the variable or list item.
2728 */
2729 if (copy)
2730 copy_tv(rettv, lp->ll_tv);
2731 else
2732 {
2733 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002734 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002736 }
2737 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002738}
2739
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002740/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002741 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2742 * Returns OK or FAIL.
2743 */
2744 static int
2745tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002746 typval_T *tv1;
2747 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002748 char_u *op;
2749{
2750 long n;
2751 char_u numbuf[NUMBUFLEN];
2752 char_u *s;
2753
2754 /* Can't do anything with a Funcref or a Dict on the right. */
2755 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2756 {
2757 switch (tv1->v_type)
2758 {
2759 case VAR_DICT:
2760 case VAR_FUNC:
2761 break;
2762
2763 case VAR_LIST:
2764 if (*op != '+' || tv2->v_type != VAR_LIST)
2765 break;
2766 /* List += List */
2767 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2768 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2769 return OK;
2770
2771 case VAR_NUMBER:
2772 case VAR_STRING:
2773 if (tv2->v_type == VAR_LIST)
2774 break;
2775 if (*op == '+' || *op == '-')
2776 {
2777 /* nr += nr or nr -= nr*/
2778 n = get_tv_number(tv1);
2779 if (*op == '+')
2780 n += get_tv_number(tv2);
2781 else
2782 n -= get_tv_number(tv2);
2783 clear_tv(tv1);
2784 tv1->v_type = VAR_NUMBER;
2785 tv1->vval.v_number = n;
2786 }
2787 else
2788 {
2789 /* str .= str */
2790 s = get_tv_string(tv1);
2791 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2792 clear_tv(tv1);
2793 tv1->v_type = VAR_STRING;
2794 tv1->vval.v_string = s;
2795 }
2796 return OK;
2797 }
2798 }
2799
2800 EMSG2(_(e_letwrong), op);
2801 return FAIL;
2802}
2803
2804/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002805 * Add a watcher to a list.
2806 */
2807 static void
2808list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002809 list_T *l;
2810 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002811{
2812 lw->lw_next = l->lv_watch;
2813 l->lv_watch = lw;
2814}
2815
2816/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002817 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002818 * No warning when it isn't found...
2819 */
2820 static void
2821list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002822 list_T *l;
2823 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002824{
Bram Moolenaar33570922005-01-25 22:26:29 +00002825 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002826
2827 lwp = &l->lv_watch;
2828 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2829 {
2830 if (lw == lwrem)
2831 {
2832 *lwp = lw->lw_next;
2833 break;
2834 }
2835 lwp = &lw->lw_next;
2836 }
2837}
2838
2839/*
2840 * Just before removing an item from a list: advance watchers to the next
2841 * item.
2842 */
2843 static void
2844list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002845 list_T *l;
2846 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002847{
Bram Moolenaar33570922005-01-25 22:26:29 +00002848 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002849
2850 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2851 if (lw->lw_item == item)
2852 lw->lw_item = item->li_next;
2853}
2854
2855/*
2856 * Evaluate the expression used in a ":for var in expr" command.
2857 * "arg" points to "var".
2858 * Set "*errp" to TRUE for an error, FALSE otherwise;
2859 * Return a pointer that holds the info. Null when there is an error.
2860 */
2861 void *
2862eval_for_line(arg, errp, nextcmdp, skip)
2863 char_u *arg;
2864 int *errp;
2865 char_u **nextcmdp;
2866 int skip;
2867{
Bram Moolenaar33570922005-01-25 22:26:29 +00002868 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002869 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002870 typval_T tv;
2871 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002872
2873 *errp = TRUE; /* default: there is an error */
2874
Bram Moolenaar33570922005-01-25 22:26:29 +00002875 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002876 if (fi == NULL)
2877 return NULL;
2878
2879 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2880 if (expr == NULL)
2881 return fi;
2882
2883 expr = skipwhite(expr);
2884 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2885 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002886 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002887 return fi;
2888 }
2889
2890 if (skip)
2891 ++emsg_skip;
2892 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2893 {
2894 *errp = FALSE;
2895 if (!skip)
2896 {
2897 l = tv.vval.v_list;
2898 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002899 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002900 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002901 clear_tv(&tv);
2902 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002903 else
2904 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002905 /* No need to increment the refcount, it's already set for the
2906 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002907 fi->fi_list = l;
2908 list_add_watch(l, &fi->fi_lw);
2909 fi->fi_lw.lw_item = l->lv_first;
2910 }
2911 }
2912 }
2913 if (skip)
2914 --emsg_skip;
2915
2916 return fi;
2917}
2918
2919/*
2920 * Use the first item in a ":for" list. Advance to the next.
2921 * Assign the values to the variable (list). "arg" points to the first one.
2922 * Return TRUE when a valid item was found, FALSE when at end of list or
2923 * something wrong.
2924 */
2925 int
2926next_for_item(fi_void, arg)
2927 void *fi_void;
2928 char_u *arg;
2929{
Bram Moolenaar33570922005-01-25 22:26:29 +00002930 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002931 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002932 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002933
2934 item = fi->fi_lw.lw_item;
2935 if (item == NULL)
2936 result = FALSE;
2937 else
2938 {
2939 fi->fi_lw.lw_item = item->li_next;
2940 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2941 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2942 }
2943 return result;
2944}
2945
2946/*
2947 * Free the structure used to store info used by ":for".
2948 */
2949 void
2950free_for_info(fi_void)
2951 void *fi_void;
2952{
Bram Moolenaar33570922005-01-25 22:26:29 +00002953 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002954
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002955 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002956 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002957 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002958 list_unref(fi->fi_list);
2959 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002960 vim_free(fi);
2961}
2962
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2964
2965 void
2966set_context_for_expression(xp, arg, cmdidx)
2967 expand_T *xp;
2968 char_u *arg;
2969 cmdidx_T cmdidx;
2970{
2971 int got_eq = FALSE;
2972 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002973 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002975 if (cmdidx == CMD_let)
2976 {
2977 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002978 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002979 {
2980 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002981 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002982 {
2983 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002984 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002985 if (vim_iswhite(*p))
2986 break;
2987 }
2988 return;
2989 }
2990 }
2991 else
2992 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2993 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 while ((xp->xp_pattern = vim_strpbrk(arg,
2995 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2996 {
2997 c = *xp->xp_pattern;
2998 if (c == '&')
2999 {
3000 c = xp->xp_pattern[1];
3001 if (c == '&')
3002 {
3003 ++xp->xp_pattern;
3004 xp->xp_context = cmdidx != CMD_let || got_eq
3005 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3006 }
3007 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003010 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3011 xp->xp_pattern += 2;
3012
3013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 }
3015 else if (c == '$')
3016 {
3017 /* environment variable */
3018 xp->xp_context = EXPAND_ENV_VARS;
3019 }
3020 else if (c == '=')
3021 {
3022 got_eq = TRUE;
3023 xp->xp_context = EXPAND_EXPRESSION;
3024 }
3025 else if (c == '<'
3026 && xp->xp_context == EXPAND_FUNCTIONS
3027 && vim_strchr(xp->xp_pattern, '(') == NULL)
3028 {
3029 /* Function name can start with "<SNR>" */
3030 break;
3031 }
3032 else if (cmdidx != CMD_let || got_eq)
3033 {
3034 if (c == '"') /* string */
3035 {
3036 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3037 if (c == '\\' && xp->xp_pattern[1] != NUL)
3038 ++xp->xp_pattern;
3039 xp->xp_context = EXPAND_NOTHING;
3040 }
3041 else if (c == '\'') /* literal string */
3042 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003043 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3045 /* skip */ ;
3046 xp->xp_context = EXPAND_NOTHING;
3047 }
3048 else if (c == '|')
3049 {
3050 if (xp->xp_pattern[1] == '|')
3051 {
3052 ++xp->xp_pattern;
3053 xp->xp_context = EXPAND_EXPRESSION;
3054 }
3055 else
3056 xp->xp_context = EXPAND_COMMANDS;
3057 }
3058 else
3059 xp->xp_context = EXPAND_EXPRESSION;
3060 }
3061 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003062 /* Doesn't look like something valid, expand as an expression
3063 * anyway. */
3064 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 arg = xp->xp_pattern;
3066 if (*arg != NUL)
3067 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3068 /* skip */ ;
3069 }
3070 xp->xp_pattern = arg;
3071}
3072
3073#endif /* FEAT_CMDL_COMPL */
3074
3075/*
3076 * ":1,25call func(arg1, arg2)" function call.
3077 */
3078 void
3079ex_call(eap)
3080 exarg_T *eap;
3081{
3082 char_u *arg = eap->arg;
3083 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003085 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003087 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 linenr_T lnum;
3089 int doesrange;
3090 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003091 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003093 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3094 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003095 if (tofree == NULL)
3096 return;
3097
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003098 /* Increase refcount on dictionary, it could get deleted when evaluating
3099 * the arguments. */
3100 if (fudi.fd_dict != NULL)
3101 ++fudi.fd_dict->dv_refcount;
3102
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003103 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3104 len = STRLEN(tofree);
3105 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106
Bram Moolenaar532c7802005-01-27 14:44:31 +00003107 /* Skip white space to allow ":call func ()". Not good, but required for
3108 * backward compatibility. */
3109 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003110 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111
3112 if (*startarg != '(')
3113 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003114 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 goto end;
3116 }
3117
3118 /*
3119 * When skipping, evaluate the function once, to find the end of the
3120 * arguments.
3121 * When the function takes a range, this is discovered after the first
3122 * call, and the loop is broken.
3123 */
3124 if (eap->skip)
3125 {
3126 ++emsg_skip;
3127 lnum = eap->line2; /* do it once, also with an invalid range */
3128 }
3129 else
3130 lnum = eap->line1;
3131 for ( ; lnum <= eap->line2; ++lnum)
3132 {
3133 if (!eap->skip && eap->addr_count > 0)
3134 {
3135 curwin->w_cursor.lnum = lnum;
3136 curwin->w_cursor.col = 0;
3137 }
3138 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003139 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003140 eap->line1, eap->line2, &doesrange,
3141 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
3143 failed = TRUE;
3144 break;
3145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003146 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 if (doesrange || eap->skip)
3148 break;
3149 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003150 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003151 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003152 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 if (aborting())
3154 break;
3155 }
3156 if (eap->skip)
3157 --emsg_skip;
3158
3159 if (!failed)
3160 {
3161 /* Check for trailing illegal characters and a following command. */
3162 if (!ends_excmd(*arg))
3163 {
3164 emsg_severe = TRUE;
3165 EMSG(_(e_trailing));
3166 }
3167 else
3168 eap->nextcmd = check_nextcmd(arg);
3169 }
3170
3171end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003172 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003173 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174}
3175
3176/*
3177 * ":unlet[!] var1 ... " command.
3178 */
3179 void
3180ex_unlet(eap)
3181 exarg_T *eap;
3182{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003183 ex_unletlock(eap, eap->arg, 0);
3184}
3185
3186/*
3187 * ":lockvar" and ":unlockvar" commands
3188 */
3189 void
3190ex_lockvar(eap)
3191 exarg_T *eap;
3192{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003194 int deep = 2;
3195
3196 if (eap->forceit)
3197 deep = -1;
3198 else if (vim_isdigit(*arg))
3199 {
3200 deep = getdigits(&arg);
3201 arg = skipwhite(arg);
3202 }
3203
3204 ex_unletlock(eap, arg, deep);
3205}
3206
3207/*
3208 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3209 */
3210 static void
3211ex_unletlock(eap, argstart, deep)
3212 exarg_T *eap;
3213 char_u *argstart;
3214 int deep;
3215{
3216 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003219 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220
3221 do
3222 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003223 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003224 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3225 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003226 if (lv.ll_name == NULL)
3227 error = TRUE; /* error but continue parsing */
3228 if (name_end == NULL || (!vim_iswhite(*name_end)
3229 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003231 if (name_end != NULL)
3232 {
3233 emsg_severe = TRUE;
3234 EMSG(_(e_trailing));
3235 }
3236 if (!(eap->skip || error))
3237 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 break;
3239 }
3240
3241 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003242 {
3243 if (eap->cmdidx == CMD_unlet)
3244 {
3245 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3246 error = TRUE;
3247 }
3248 else
3249 {
3250 if (do_lock_var(&lv, name_end, deep,
3251 eap->cmdidx == CMD_lockvar) == FAIL)
3252 error = TRUE;
3253 }
3254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003256 if (!eap->skip)
3257 clear_lval(&lv);
3258
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 arg = skipwhite(name_end);
3260 } while (!ends_excmd(*arg));
3261
3262 eap->nextcmd = check_nextcmd(arg);
3263}
3264
Bram Moolenaar8c711452005-01-14 21:53:12 +00003265 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003266do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003267 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003268 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003269 int forceit;
3270{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003271 int ret = OK;
3272 int cc;
3273
3274 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003275 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003276 cc = *name_end;
3277 *name_end = NUL;
3278
3279 /* Normal name or expanded name. */
3280 if (check_changedtick(lp->ll_name))
3281 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003282 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003283 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003284 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003285 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003286 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3287 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003288 else if (lp->ll_range)
3289 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003290 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003291
3292 /* Delete a range of List items. */
3293 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3294 {
3295 li = lp->ll_li->li_next;
3296 listitem_remove(lp->ll_list, lp->ll_li);
3297 lp->ll_li = li;
3298 ++lp->ll_n1;
3299 }
3300 }
3301 else
3302 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003303 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003304 /* unlet a List item. */
3305 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003306 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003307 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003308 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003309 }
3310
3311 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003312}
3313
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314/*
3315 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003316 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 */
3318 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003319do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003321 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
Bram Moolenaar33570922005-01-25 22:26:29 +00003323 hashtab_T *ht;
3324 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003325 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
Bram Moolenaar33570922005-01-25 22:26:29 +00003327 ht = find_var_ht(name, &varname);
3328 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003330 hi = hash_find(ht, varname);
3331 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003332 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003333 if (var_check_ro(HI2DI(hi)->di_flags, name))
3334 return FAIL;
3335 delete_var(ht, hi);
3336 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003339 if (forceit)
3340 return OK;
3341 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 return FAIL;
3343}
3344
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003345/*
3346 * Lock or unlock variable indicated by "lp".
3347 * "deep" is the levels to go (-1 for unlimited);
3348 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3349 */
3350 static int
3351do_lock_var(lp, name_end, deep, lock)
3352 lval_T *lp;
3353 char_u *name_end;
3354 int deep;
3355 int lock;
3356{
3357 int ret = OK;
3358 int cc;
3359 dictitem_T *di;
3360
3361 if (deep == 0) /* nothing to do */
3362 return OK;
3363
3364 if (lp->ll_tv == NULL)
3365 {
3366 cc = *name_end;
3367 *name_end = NUL;
3368
3369 /* Normal name or expanded name. */
3370 if (check_changedtick(lp->ll_name))
3371 ret = FAIL;
3372 else
3373 {
3374 di = find_var(lp->ll_name, NULL);
3375 if (di == NULL)
3376 ret = FAIL;
3377 else
3378 {
3379 if (lock)
3380 di->di_flags |= DI_FLAGS_LOCK;
3381 else
3382 di->di_flags &= ~DI_FLAGS_LOCK;
3383 item_lock(&di->di_tv, deep, lock);
3384 }
3385 }
3386 *name_end = cc;
3387 }
3388 else if (lp->ll_range)
3389 {
3390 listitem_T *li = lp->ll_li;
3391
3392 /* (un)lock a range of List items. */
3393 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3394 {
3395 item_lock(&li->li_tv, deep, lock);
3396 li = li->li_next;
3397 ++lp->ll_n1;
3398 }
3399 }
3400 else if (lp->ll_list != NULL)
3401 /* (un)lock a List item. */
3402 item_lock(&lp->ll_li->li_tv, deep, lock);
3403 else
3404 /* un(lock) a Dictionary item. */
3405 item_lock(&lp->ll_di->di_tv, deep, lock);
3406
3407 return ret;
3408}
3409
3410/*
3411 * Lock or unlock an item. "deep" is nr of levels to go.
3412 */
3413 static void
3414item_lock(tv, deep, lock)
3415 typval_T *tv;
3416 int deep;
3417 int lock;
3418{
3419 static int recurse = 0;
3420 list_T *l;
3421 listitem_T *li;
3422 dict_T *d;
3423 hashitem_T *hi;
3424 int todo;
3425
3426 if (recurse >= DICT_MAXNEST)
3427 {
3428 EMSG(_("E743: variable nested too deep for (un)lock"));
3429 return;
3430 }
3431 if (deep == 0)
3432 return;
3433 ++recurse;
3434
3435 /* lock/unlock the item itself */
3436 if (lock)
3437 tv->v_lock |= VAR_LOCKED;
3438 else
3439 tv->v_lock &= ~VAR_LOCKED;
3440
3441 switch (tv->v_type)
3442 {
3443 case VAR_LIST:
3444 if ((l = tv->vval.v_list) != NULL)
3445 {
3446 if (lock)
3447 l->lv_lock |= VAR_LOCKED;
3448 else
3449 l->lv_lock &= ~VAR_LOCKED;
3450 if (deep < 0 || deep > 1)
3451 /* recursive: lock/unlock the items the List contains */
3452 for (li = l->lv_first; li != NULL; li = li->li_next)
3453 item_lock(&li->li_tv, deep - 1, lock);
3454 }
3455 break;
3456 case VAR_DICT:
3457 if ((d = tv->vval.v_dict) != NULL)
3458 {
3459 if (lock)
3460 d->dv_lock |= VAR_LOCKED;
3461 else
3462 d->dv_lock &= ~VAR_LOCKED;
3463 if (deep < 0 || deep > 1)
3464 {
3465 /* recursive: lock/unlock the items the List contains */
3466 todo = d->dv_hashtab.ht_used;
3467 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3468 {
3469 if (!HASHITEM_EMPTY(hi))
3470 {
3471 --todo;
3472 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3473 }
3474 }
3475 }
3476 }
3477 }
3478 --recurse;
3479}
3480
Bram Moolenaara40058a2005-07-11 22:42:07 +00003481/*
3482 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3483 * it refers to a List or Dictionary that is locked.
3484 */
3485 static int
3486tv_islocked(tv)
3487 typval_T *tv;
3488{
3489 return (tv->v_lock & VAR_LOCKED)
3490 || (tv->v_type == VAR_LIST
3491 && tv->vval.v_list != NULL
3492 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3493 || (tv->v_type == VAR_DICT
3494 && tv->vval.v_dict != NULL
3495 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3496}
3497
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3499/*
3500 * Delete all "menutrans_" variables.
3501 */
3502 void
3503del_menutrans_vars()
3504{
Bram Moolenaar33570922005-01-25 22:26:29 +00003505 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003506 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
Bram Moolenaar33570922005-01-25 22:26:29 +00003508 hash_lock(&globvarht);
3509 todo = globvarht.ht_used;
3510 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003511 {
3512 if (!HASHITEM_EMPTY(hi))
3513 {
3514 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003515 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3516 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003517 }
3518 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003519 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520}
3521#endif
3522
3523#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3524
3525/*
3526 * Local string buffer for the next two functions to store a variable name
3527 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3528 * get_user_var_name().
3529 */
3530
3531static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3532
3533static char_u *varnamebuf = NULL;
3534static int varnamebuflen = 0;
3535
3536/*
3537 * Function to concatenate a prefix and a variable name.
3538 */
3539 static char_u *
3540cat_prefix_varname(prefix, name)
3541 int prefix;
3542 char_u *name;
3543{
3544 int len;
3545
3546 len = (int)STRLEN(name) + 3;
3547 if (len > varnamebuflen)
3548 {
3549 vim_free(varnamebuf);
3550 len += 10; /* some additional space */
3551 varnamebuf = alloc(len);
3552 if (varnamebuf == NULL)
3553 {
3554 varnamebuflen = 0;
3555 return NULL;
3556 }
3557 varnamebuflen = len;
3558 }
3559 *varnamebuf = prefix;
3560 varnamebuf[1] = ':';
3561 STRCPY(varnamebuf + 2, name);
3562 return varnamebuf;
3563}
3564
3565/*
3566 * Function given to ExpandGeneric() to obtain the list of user defined
3567 * (global/buffer/window/built-in) variable names.
3568 */
3569/*ARGSUSED*/
3570 char_u *
3571get_user_var_name(xp, idx)
3572 expand_T *xp;
3573 int idx;
3574{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003575 static long_u gdone;
3576 static long_u bdone;
3577 static long_u wdone;
3578 static int vidx;
3579 static hashitem_T *hi;
3580 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581
3582 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003583 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003584
3585 /* Global variables */
3586 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003588 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003589 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003590 else
3591 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003592 while (HASHITEM_EMPTY(hi))
3593 ++hi;
3594 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3595 return cat_prefix_varname('g', hi->hi_key);
3596 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003598
3599 /* b: variables */
3600 ht = &curbuf->b_vars.dv_hashtab;
3601 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003603 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003604 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003605 else
3606 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003607 while (HASHITEM_EMPTY(hi))
3608 ++hi;
3609 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003611 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003613 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 return (char_u *)"b:changedtick";
3615 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003616
3617 /* w: variables */
3618 ht = &curwin->w_vars.dv_hashtab;
3619 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003621 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003622 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003623 else
3624 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003625 while (HASHITEM_EMPTY(hi))
3626 ++hi;
3627 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003629
3630 /* v: variables */
3631 if (vidx < VV_LEN)
3632 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 vim_free(varnamebuf);
3635 varnamebuf = NULL;
3636 varnamebuflen = 0;
3637 return NULL;
3638}
3639
3640#endif /* FEAT_CMDL_COMPL */
3641
3642/*
3643 * types for expressions.
3644 */
3645typedef enum
3646{
3647 TYPE_UNKNOWN = 0
3648 , TYPE_EQUAL /* == */
3649 , TYPE_NEQUAL /* != */
3650 , TYPE_GREATER /* > */
3651 , TYPE_GEQUAL /* >= */
3652 , TYPE_SMALLER /* < */
3653 , TYPE_SEQUAL /* <= */
3654 , TYPE_MATCH /* =~ */
3655 , TYPE_NOMATCH /* !~ */
3656} exptype_T;
3657
3658/*
3659 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003660 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3662 */
3663
3664/*
3665 * Handle zero level expression.
3666 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003667 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003668 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 * Return OK or FAIL.
3670 */
3671 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003672eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 char_u **nextcmd;
3676 int evaluate;
3677{
3678 int ret;
3679 char_u *p;
3680
3681 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003682 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 if (ret == FAIL || !ends_excmd(*p))
3684 {
3685 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003686 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 /*
3688 * Report the invalid expression unless the expression evaluation has
3689 * been cancelled due to an aborting error, an interrupt, or an
3690 * exception.
3691 */
3692 if (!aborting())
3693 EMSG2(_(e_invexpr2), arg);
3694 ret = FAIL;
3695 }
3696 if (nextcmd != NULL)
3697 *nextcmd = check_nextcmd(p);
3698
3699 return ret;
3700}
3701
3702/*
3703 * Handle top level expression:
3704 * expr1 ? expr0 : expr0
3705 *
3706 * "arg" must point to the first non-white of the expression.
3707 * "arg" is advanced to the next non-white after the recognized expression.
3708 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003709 * Note: "rettv.v_lock" is not set.
3710 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 * Return OK or FAIL.
3712 */
3713 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003714eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 int evaluate;
3718{
3719 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003720 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
3722 /*
3723 * Get the first variable.
3724 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003725 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 return FAIL;
3727
3728 if ((*arg)[0] == '?')
3729 {
3730 result = FALSE;
3731 if (evaluate)
3732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003733 int error = FALSE;
3734
3735 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003737 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003738 if (error)
3739 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
3741
3742 /*
3743 * Get the second variable.
3744 */
3745 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003746 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 return FAIL;
3748
3749 /*
3750 * Check for the ":".
3751 */
3752 if ((*arg)[0] != ':')
3753 {
3754 EMSG(_("E109: Missing ':' after '?'"));
3755 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003756 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 return FAIL;
3758 }
3759
3760 /*
3761 * Get the third variable.
3762 */
3763 *arg = skipwhite(*arg + 1);
3764 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3765 {
3766 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003767 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 return FAIL;
3769 }
3770 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003771 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773
3774 return OK;
3775}
3776
3777/*
3778 * Handle first level expression:
3779 * expr2 || expr2 || expr2 logical OR
3780 *
3781 * "arg" must point to the first non-white of the expression.
3782 * "arg" is advanced to the next non-white after the recognized expression.
3783 *
3784 * Return OK or FAIL.
3785 */
3786 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003787eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 int evaluate;
3791{
Bram Moolenaar33570922005-01-25 22:26:29 +00003792 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 long result;
3794 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003795 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796
3797 /*
3798 * Get the first variable.
3799 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003800 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 return FAIL;
3802
3803 /*
3804 * Repeat until there is no following "||".
3805 */
3806 first = TRUE;
3807 result = FALSE;
3808 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3809 {
3810 if (evaluate && first)
3811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003812 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003814 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003815 if (error)
3816 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 first = FALSE;
3818 }
3819
3820 /*
3821 * Get the second variable.
3822 */
3823 *arg = skipwhite(*arg + 2);
3824 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3825 return FAIL;
3826
3827 /*
3828 * Compute the result.
3829 */
3830 if (evaluate && !result)
3831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003832 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003834 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003835 if (error)
3836 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838 if (evaluate)
3839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003840 rettv->v_type = VAR_NUMBER;
3841 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843 }
3844
3845 return OK;
3846}
3847
3848/*
3849 * Handle second level expression:
3850 * expr3 && expr3 && expr3 logical AND
3851 *
3852 * "arg" must point to the first non-white of the expression.
3853 * "arg" is advanced to the next non-white after the recognized expression.
3854 *
3855 * Return OK or FAIL.
3856 */
3857 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003858eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 int evaluate;
3862{
Bram Moolenaar33570922005-01-25 22:26:29 +00003863 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 long result;
3865 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003866 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
3868 /*
3869 * Get the first variable.
3870 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003871 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 return FAIL;
3873
3874 /*
3875 * Repeat until there is no following "&&".
3876 */
3877 first = TRUE;
3878 result = TRUE;
3879 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3880 {
3881 if (evaluate && first)
3882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003883 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003885 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003886 if (error)
3887 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 first = FALSE;
3889 }
3890
3891 /*
3892 * Get the second variable.
3893 */
3894 *arg = skipwhite(*arg + 2);
3895 if (eval4(arg, &var2, evaluate && result) == FAIL)
3896 return FAIL;
3897
3898 /*
3899 * Compute the result.
3900 */
3901 if (evaluate && result)
3902 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003903 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003905 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003906 if (error)
3907 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
3909 if (evaluate)
3910 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003911 rettv->v_type = VAR_NUMBER;
3912 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
3914 }
3915
3916 return OK;
3917}
3918
3919/*
3920 * Handle third level expression:
3921 * var1 == var2
3922 * var1 =~ var2
3923 * var1 != var2
3924 * var1 !~ var2
3925 * var1 > var2
3926 * var1 >= var2
3927 * var1 < var2
3928 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003929 * var1 is var2
3930 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 *
3932 * "arg" must point to the first non-white of the expression.
3933 * "arg" is advanced to the next non-white after the recognized expression.
3934 *
3935 * Return OK or FAIL.
3936 */
3937 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003938eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 int evaluate;
3942{
Bram Moolenaar33570922005-01-25 22:26:29 +00003943 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 char_u *p;
3945 int i;
3946 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003947 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 int len = 2;
3949 long n1, n2;
3950 char_u *s1, *s2;
3951 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3952 regmatch_T regmatch;
3953 int ic;
3954 char_u *save_cpo;
3955
3956 /*
3957 * Get the first variable.
3958 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003959 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 return FAIL;
3961
3962 p = *arg;
3963 switch (p[0])
3964 {
3965 case '=': if (p[1] == '=')
3966 type = TYPE_EQUAL;
3967 else if (p[1] == '~')
3968 type = TYPE_MATCH;
3969 break;
3970 case '!': if (p[1] == '=')
3971 type = TYPE_NEQUAL;
3972 else if (p[1] == '~')
3973 type = TYPE_NOMATCH;
3974 break;
3975 case '>': if (p[1] != '=')
3976 {
3977 type = TYPE_GREATER;
3978 len = 1;
3979 }
3980 else
3981 type = TYPE_GEQUAL;
3982 break;
3983 case '<': if (p[1] != '=')
3984 {
3985 type = TYPE_SMALLER;
3986 len = 1;
3987 }
3988 else
3989 type = TYPE_SEQUAL;
3990 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003991 case 'i': if (p[1] == 's')
3992 {
3993 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3994 len = 5;
3995 if (!vim_isIDc(p[len]))
3996 {
3997 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3998 type_is = TRUE;
3999 }
4000 }
4001 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 }
4003
4004 /*
4005 * If there is a comparitive operator, use it.
4006 */
4007 if (type != TYPE_UNKNOWN)
4008 {
4009 /* extra question mark appended: ignore case */
4010 if (p[len] == '?')
4011 {
4012 ic = TRUE;
4013 ++len;
4014 }
4015 /* extra '#' appended: match case */
4016 else if (p[len] == '#')
4017 {
4018 ic = FALSE;
4019 ++len;
4020 }
4021 /* nothing appened: use 'ignorecase' */
4022 else
4023 ic = p_ic;
4024
4025 /*
4026 * Get the second variable.
4027 */
4028 *arg = skipwhite(p + len);
4029 if (eval5(arg, &var2, evaluate) == FAIL)
4030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004031 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 return FAIL;
4033 }
4034
4035 if (evaluate)
4036 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004037 if (type_is && rettv->v_type != var2.v_type)
4038 {
4039 /* For "is" a different type always means FALSE, for "notis"
4040 * it means TRUE. */
4041 n1 = (type == TYPE_NEQUAL);
4042 }
4043 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4044 {
4045 if (type_is)
4046 {
4047 n1 = (rettv->v_type == var2.v_type
4048 && rettv->vval.v_list == var2.vval.v_list);
4049 if (type == TYPE_NEQUAL)
4050 n1 = !n1;
4051 }
4052 else if (rettv->v_type != var2.v_type
4053 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4054 {
4055 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004056 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004057 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004058 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004059 clear_tv(rettv);
4060 clear_tv(&var2);
4061 return FAIL;
4062 }
4063 else
4064 {
4065 /* Compare two Lists for being equal or unequal. */
4066 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4067 if (type == TYPE_NEQUAL)
4068 n1 = !n1;
4069 }
4070 }
4071
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004072 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4073 {
4074 if (type_is)
4075 {
4076 n1 = (rettv->v_type == var2.v_type
4077 && rettv->vval.v_dict == var2.vval.v_dict);
4078 if (type == TYPE_NEQUAL)
4079 n1 = !n1;
4080 }
4081 else if (rettv->v_type != var2.v_type
4082 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4083 {
4084 if (rettv->v_type != var2.v_type)
4085 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4086 else
4087 EMSG(_("E736: Invalid operation for Dictionary"));
4088 clear_tv(rettv);
4089 clear_tv(&var2);
4090 return FAIL;
4091 }
4092 else
4093 {
4094 /* Compare two Dictionaries for being equal or unequal. */
4095 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4096 if (type == TYPE_NEQUAL)
4097 n1 = !n1;
4098 }
4099 }
4100
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004101 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4102 {
4103 if (rettv->v_type != var2.v_type
4104 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4105 {
4106 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004107 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004108 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004109 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004110 clear_tv(rettv);
4111 clear_tv(&var2);
4112 return FAIL;
4113 }
4114 else
4115 {
4116 /* Compare two Funcrefs for being equal or unequal. */
4117 if (rettv->vval.v_string == NULL
4118 || var2.vval.v_string == NULL)
4119 n1 = FALSE;
4120 else
4121 n1 = STRCMP(rettv->vval.v_string,
4122 var2.vval.v_string) == 0;
4123 if (type == TYPE_NEQUAL)
4124 n1 = !n1;
4125 }
4126 }
4127
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 /*
4129 * If one of the two variables is a number, compare as a number.
4130 * When using "=~" or "!~", always compare as string.
4131 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004132 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4134 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135 n1 = get_tv_number(rettv);
4136 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 switch (type)
4138 {
4139 case TYPE_EQUAL: n1 = (n1 == n2); break;
4140 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4141 case TYPE_GREATER: n1 = (n1 > n2); break;
4142 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4143 case TYPE_SMALLER: n1 = (n1 < n2); break;
4144 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4145 case TYPE_UNKNOWN:
4146 case TYPE_MATCH:
4147 case TYPE_NOMATCH: break; /* avoid gcc warning */
4148 }
4149 }
4150 else
4151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152 s1 = get_tv_string_buf(rettv, buf1);
4153 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4155 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4156 else
4157 i = 0;
4158 n1 = FALSE;
4159 switch (type)
4160 {
4161 case TYPE_EQUAL: n1 = (i == 0); break;
4162 case TYPE_NEQUAL: n1 = (i != 0); break;
4163 case TYPE_GREATER: n1 = (i > 0); break;
4164 case TYPE_GEQUAL: n1 = (i >= 0); break;
4165 case TYPE_SMALLER: n1 = (i < 0); break;
4166 case TYPE_SEQUAL: n1 = (i <= 0); break;
4167
4168 case TYPE_MATCH:
4169 case TYPE_NOMATCH:
4170 /* avoid 'l' flag in 'cpoptions' */
4171 save_cpo = p_cpo;
4172 p_cpo = (char_u *)"";
4173 regmatch.regprog = vim_regcomp(s2,
4174 RE_MAGIC + RE_STRING);
4175 regmatch.rm_ic = ic;
4176 if (regmatch.regprog != NULL)
4177 {
4178 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4179 vim_free(regmatch.regprog);
4180 if (type == TYPE_NOMATCH)
4181 n1 = !n1;
4182 }
4183 p_cpo = save_cpo;
4184 break;
4185
4186 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4187 }
4188 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 clear_tv(rettv);
4190 clear_tv(&var2);
4191 rettv->v_type = VAR_NUMBER;
4192 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
4194 }
4195
4196 return OK;
4197}
4198
4199/*
4200 * Handle fourth level expression:
4201 * + number addition
4202 * - number subtraction
4203 * . string concatenation
4204 *
4205 * "arg" must point to the first non-white of the expression.
4206 * "arg" is advanced to the next non-white after the recognized expression.
4207 *
4208 * Return OK or FAIL.
4209 */
4210 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 int evaluate;
4215{
Bram Moolenaar33570922005-01-25 22:26:29 +00004216 typval_T var2;
4217 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 int op;
4219 long n1, n2;
4220 char_u *s1, *s2;
4221 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4222 char_u *p;
4223
4224 /*
4225 * Get the first variable.
4226 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004227 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 return FAIL;
4229
4230 /*
4231 * Repeat computing, until no '+', '-' or '.' is following.
4232 */
4233 for (;;)
4234 {
4235 op = **arg;
4236 if (op != '+' && op != '-' && op != '.')
4237 break;
4238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 if (op != '+' || rettv->v_type != VAR_LIST)
4240 {
4241 /* For "list + ...", an illegal use of the first operand as
4242 * a number cannot be determined before evaluating the 2nd
4243 * operand: if this is also a list, all is ok.
4244 * For "something . ...", "something - ..." or "non-list + ...",
4245 * we know that the first operand needs to be a string or number
4246 * without evaluating the 2nd operand. So check before to avoid
4247 * side effects after an error. */
4248 if (evaluate && get_tv_string_chk(rettv) == NULL)
4249 {
4250 clear_tv(rettv);
4251 return FAIL;
4252 }
4253 }
4254
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 /*
4256 * Get the second variable.
4257 */
4258 *arg = skipwhite(*arg + 1);
4259 if (eval6(arg, &var2, evaluate) == FAIL)
4260 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 return FAIL;
4263 }
4264
4265 if (evaluate)
4266 {
4267 /*
4268 * Compute the result.
4269 */
4270 if (op == '.')
4271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4273 s2 = get_tv_string_buf_chk(&var2, buf2);
4274 if (s2 == NULL) /* type error ? */
4275 {
4276 clear_tv(rettv);
4277 clear_tv(&var2);
4278 return FAIL;
4279 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004280 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 clear_tv(rettv);
4282 rettv->v_type = VAR_STRING;
4283 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004285 else if (op == '+' && rettv->v_type == VAR_LIST
4286 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004287 {
4288 /* concatenate Lists */
4289 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4290 &var3) == FAIL)
4291 {
4292 clear_tv(rettv);
4293 clear_tv(&var2);
4294 return FAIL;
4295 }
4296 clear_tv(rettv);
4297 *rettv = var3;
4298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 else
4300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004301 int error = FALSE;
4302
4303 n1 = get_tv_number_chk(rettv, &error);
4304 if (error)
4305 {
4306 /* This can only happen for "list + non-list".
4307 * For "non-list + ..." or "something - ...", we returned
4308 * before evaluating the 2nd operand. */
4309 clear_tv(rettv);
4310 return FAIL;
4311 }
4312 n2 = get_tv_number_chk(&var2, &error);
4313 if (error)
4314 {
4315 clear_tv(rettv);
4316 clear_tv(&var2);
4317 return FAIL;
4318 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 if (op == '+')
4321 n1 = n1 + n2;
4322 else
4323 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 rettv->v_type = VAR_NUMBER;
4325 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004327 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 }
4329 }
4330 return OK;
4331}
4332
4333/*
4334 * Handle fifth level expression:
4335 * * number multiplication
4336 * / number division
4337 * % number modulo
4338 *
4339 * "arg" must point to the first non-white of the expression.
4340 * "arg" is advanced to the next non-white after the recognized expression.
4341 *
4342 * Return OK or FAIL.
4343 */
4344 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 int evaluate;
4349{
Bram Moolenaar33570922005-01-25 22:26:29 +00004350 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 int op;
4352 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004353 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
4355 /*
4356 * Get the first variable.
4357 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004358 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 return FAIL;
4360
4361 /*
4362 * Repeat computing, until no '*', '/' or '%' is following.
4363 */
4364 for (;;)
4365 {
4366 op = **arg;
4367 if (op != '*' && op != '/' && op != '%')
4368 break;
4369
4370 if (evaluate)
4371 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004372 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004373 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004374 if (error)
4375 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
4377 else
4378 n1 = 0;
4379
4380 /*
4381 * Get the second variable.
4382 */
4383 *arg = skipwhite(*arg + 1);
4384 if (eval7(arg, &var2, evaluate) == FAIL)
4385 return FAIL;
4386
4387 if (evaluate)
4388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004389 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004390 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004391 if (error)
4392 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394 /*
4395 * Compute the result.
4396 */
4397 if (op == '*')
4398 n1 = n1 * n2;
4399 else if (op == '/')
4400 {
4401 if (n2 == 0) /* give an error message? */
4402 n1 = 0x7fffffffL;
4403 else
4404 n1 = n1 / n2;
4405 }
4406 else
4407 {
4408 if (n2 == 0) /* give an error message? */
4409 n1 = 0;
4410 else
4411 n1 = n1 % n2;
4412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004413 rettv->v_type = VAR_NUMBER;
4414 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416 }
4417
4418 return OK;
4419}
4420
4421/*
4422 * Handle sixth level expression:
4423 * number number constant
4424 * "string" string contstant
4425 * 'string' literal string contstant
4426 * &option-name option value
4427 * @r register contents
4428 * identifier variable value
4429 * function() function call
4430 * $VAR environment variable
4431 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004432 * [expr, expr] List
4433 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 *
4435 * Also handle:
4436 * ! in front logical NOT
4437 * - in front unary minus
4438 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004439 * trailing [] subscript in String or List
4440 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 *
4442 * "arg" must point to the first non-white of the expression.
4443 * "arg" is advanced to the next non-white after the recognized expression.
4444 *
4445 * Return OK or FAIL.
4446 */
4447 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004448eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 int evaluate;
4452{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 long n;
4454 int len;
4455 char_u *s;
4456 int val;
4457 char_u *start_leader, *end_leader;
4458 int ret = OK;
4459 char_u *alias;
4460
4461 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004462 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004463 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004465 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467 /*
4468 * Skip '!' and '-' characters. They are handled later.
4469 */
4470 start_leader = *arg;
4471 while (**arg == '!' || **arg == '-' || **arg == '+')
4472 *arg = skipwhite(*arg + 1);
4473 end_leader = *arg;
4474
4475 switch (**arg)
4476 {
4477 /*
4478 * Number constant.
4479 */
4480 case '0':
4481 case '1':
4482 case '2':
4483 case '3':
4484 case '4':
4485 case '5':
4486 case '6':
4487 case '7':
4488 case '8':
4489 case '9':
4490 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4491 *arg += len;
4492 if (evaluate)
4493 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004494 rettv->v_type = VAR_NUMBER;
4495 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
4497 break;
4498
4499 /*
4500 * String constant: "string".
4501 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004502 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 break;
4504
4505 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004506 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004508 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004509 break;
4510
4511 /*
4512 * List: [expr, expr]
4513 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004514 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 break;
4516
4517 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004518 * Dictionary: {key: val, key: val}
4519 */
4520 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4521 break;
4522
4523 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004524 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004526 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 break;
4528
4529 /*
4530 * Environment variable: $VAR.
4531 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004532 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 break;
4534
4535 /*
4536 * Register contents: @r.
4537 */
4538 case '@': ++*arg;
4539 if (evaluate)
4540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004541 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004542 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
4544 if (**arg != NUL)
4545 ++*arg;
4546 break;
4547
4548 /*
4549 * nested expression: (expression).
4550 */
4551 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004552 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 if (**arg == ')')
4554 ++*arg;
4555 else if (ret == OK)
4556 {
4557 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004558 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 ret = FAIL;
4560 }
4561 break;
4562
Bram Moolenaar8c711452005-01-14 21:53:12 +00004563 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 break;
4565 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004566
4567 if (ret == NOTDONE)
4568 {
4569 /*
4570 * Must be a variable or function name.
4571 * Can also be a curly-braces kind of name: {expr}.
4572 */
4573 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004574 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004575 if (alias != NULL)
4576 s = alias;
4577
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004578 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004579 ret = FAIL;
4580 else
4581 {
4582 if (**arg == '(') /* recursive! */
4583 {
4584 /* If "s" is the name of a variable of type VAR_FUNC
4585 * use its contents. */
4586 s = deref_func_name(s, &len);
4587
4588 /* Invoke the function. */
4589 ret = get_func_tv(s, len, rettv, arg,
4590 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004591 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004592 /* Stop the expression evaluation when immediately
4593 * aborting on error, or when an interrupt occurred or
4594 * an exception was thrown but not caught. */
4595 if (aborting())
4596 {
4597 if (ret == OK)
4598 clear_tv(rettv);
4599 ret = FAIL;
4600 }
4601 }
4602 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004603 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004604 else
4605 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 }
4607
4608 if (alias != NULL)
4609 vim_free(alias);
4610 }
4611
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 *arg = skipwhite(*arg);
4613
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004614 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4615 * expr(expr). */
4616 if (ret == OK)
4617 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
4619 /*
4620 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4621 */
4622 if (ret == OK && evaluate && end_leader > start_leader)
4623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004624 int error = FALSE;
4625
4626 val = get_tv_number_chk(rettv, &error);
4627 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004629 clear_tv(rettv);
4630 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004632 else
4633 {
4634 while (end_leader > start_leader)
4635 {
4636 --end_leader;
4637 if (*end_leader == '!')
4638 val = !val;
4639 else if (*end_leader == '-')
4640 val = -val;
4641 }
4642 clear_tv(rettv);
4643 rettv->v_type = VAR_NUMBER;
4644 rettv->vval.v_number = val;
4645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
4647
4648 return ret;
4649}
4650
4651/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004652 * Evaluate an "[expr]" or "[expr:expr]" index.
4653 * "*arg" points to the '['.
4654 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4655 */
4656 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004657eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004659 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004660 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004661 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004662{
4663 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004664 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004665 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004666 long len = -1;
4667 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004668 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004669 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004670
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004672 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004673 if (verbose)
4674 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004675 return FAIL;
4676 }
4677
Bram Moolenaar8c711452005-01-14 21:53:12 +00004678 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004680 /*
4681 * dict.name
4682 */
4683 key = *arg + 1;
4684 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4685 ;
4686 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004687 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004688 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004689 }
4690 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004691 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004692 /*
4693 * something[idx]
4694 *
4695 * Get the (first) variable from inside the [].
4696 */
4697 *arg = skipwhite(*arg + 1);
4698 if (**arg == ':')
4699 empty1 = TRUE;
4700 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4701 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004702 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4703 {
4704 /* not a number or string */
4705 clear_tv(&var1);
4706 return FAIL;
4707 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004708
4709 /*
4710 * Get the second variable from inside the [:].
4711 */
4712 if (**arg == ':')
4713 {
4714 range = TRUE;
4715 *arg = skipwhite(*arg + 1);
4716 if (**arg == ']')
4717 empty2 = TRUE;
4718 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 if (!empty1)
4721 clear_tv(&var1);
4722 return FAIL;
4723 }
4724 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4725 {
4726 /* not a number or string */
4727 if (!empty1)
4728 clear_tv(&var1);
4729 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004730 return FAIL;
4731 }
4732 }
4733
4734 /* Check for the ']'. */
4735 if (**arg != ']')
4736 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004737 if (verbose)
4738 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004739 clear_tv(&var1);
4740 if (range)
4741 clear_tv(&var2);
4742 return FAIL;
4743 }
4744 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 }
4746
4747 if (evaluate)
4748 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004749 n1 = 0;
4750 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004751 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752 n1 = get_tv_number(&var1);
4753 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754 }
4755 if (range)
4756 {
4757 if (empty2)
4758 n2 = -1;
4759 else
4760 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 n2 = get_tv_number(&var2);
4762 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004763 }
4764 }
4765
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004767 {
4768 case VAR_NUMBER:
4769 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771 len = (long)STRLEN(s);
4772 if (range)
4773 {
4774 /* The resulting variable is a substring. If the indexes
4775 * are out of range the result is empty. */
4776 if (n1 < 0)
4777 {
4778 n1 = len + n1;
4779 if (n1 < 0)
4780 n1 = 0;
4781 }
4782 if (n2 < 0)
4783 n2 = len + n2;
4784 else if (n2 >= len)
4785 n2 = len;
4786 if (n1 >= len || n2 < 0 || n1 > n2)
4787 s = NULL;
4788 else
4789 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4790 }
4791 else
4792 {
4793 /* The resulting variable is a string of a single
4794 * character. If the index is too big or negative the
4795 * result is empty. */
4796 if (n1 >= len || n1 < 0)
4797 s = NULL;
4798 else
4799 s = vim_strnsave(s + n1, 1);
4800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004801 clear_tv(rettv);
4802 rettv->v_type = VAR_STRING;
4803 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004804 break;
4805
4806 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004807 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004808 if (n1 < 0)
4809 n1 = len + n1;
4810 if (!empty1 && (n1 < 0 || n1 >= len))
4811 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004812 if (verbose)
4813 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004814 return FAIL;
4815 }
4816 if (range)
4817 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004818 list_T *l;
4819 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820
4821 if (n2 < 0)
4822 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004823 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004825 if (verbose)
4826 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004827 return FAIL;
4828 }
4829 l = list_alloc();
4830 if (l == NULL)
4831 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004832 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004833 n1 <= n2; ++n1)
4834 {
4835 if (list_append_tv(l, &item->li_tv) == FAIL)
4836 {
4837 list_free(l);
4838 return FAIL;
4839 }
4840 item = item->li_next;
4841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004842 clear_tv(rettv);
4843 rettv->v_type = VAR_LIST;
4844 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004845 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004846 }
4847 else
4848 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004849 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004850 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 clear_tv(rettv);
4852 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004853 }
4854 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004855
4856 case VAR_DICT:
4857 if (range)
4858 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004859 if (verbose)
4860 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004861 if (len == -1)
4862 clear_tv(&var1);
4863 return FAIL;
4864 }
4865 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004866 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004867
4868 if (len == -1)
4869 {
4870 key = get_tv_string(&var1);
4871 if (*key == NUL)
4872 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004873 if (verbose)
4874 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004875 clear_tv(&var1);
4876 return FAIL;
4877 }
4878 }
4879
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004880 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004881
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004882 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004883 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004884 if (len == -1)
4885 clear_tv(&var1);
4886 if (item == NULL)
4887 return FAIL;
4888
4889 copy_tv(&item->di_tv, &var1);
4890 clear_tv(rettv);
4891 *rettv = var1;
4892 }
4893 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004894 }
4895 }
4896
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004897 return OK;
4898}
4899
4900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 * Get an option value.
4902 * "arg" points to the '&' or '+' before the option name.
4903 * "arg" is advanced to character after the option name.
4904 * Return OK or FAIL.
4905 */
4906 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004907get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004909 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 int evaluate;
4911{
4912 char_u *option_end;
4913 long numval;
4914 char_u *stringval;
4915 int opt_type;
4916 int c;
4917 int working = (**arg == '+'); /* has("+option") */
4918 int ret = OK;
4919 int opt_flags;
4920
4921 /*
4922 * Isolate the option name and find its value.
4923 */
4924 option_end = find_option_end(arg, &opt_flags);
4925 if (option_end == NULL)
4926 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004927 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 EMSG2(_("E112: Option name missing: %s"), *arg);
4929 return FAIL;
4930 }
4931
4932 if (!evaluate)
4933 {
4934 *arg = option_end;
4935 return OK;
4936 }
4937
4938 c = *option_end;
4939 *option_end = NUL;
4940 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004941 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942
4943 if (opt_type == -3) /* invalid name */
4944 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004945 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 EMSG2(_("E113: Unknown option: %s"), *arg);
4947 ret = FAIL;
4948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004949 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
4951 if (opt_type == -2) /* hidden string option */
4952 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004953 rettv->v_type = VAR_STRING;
4954 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956 else if (opt_type == -1) /* hidden number option */
4957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004958 rettv->v_type = VAR_NUMBER;
4959 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 }
4961 else if (opt_type == 1) /* number option */
4962 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004963 rettv->v_type = VAR_NUMBER;
4964 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 }
4966 else /* string option */
4967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004968 rettv->v_type = VAR_STRING;
4969 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 }
4971 }
4972 else if (working && (opt_type == -2 || opt_type == -1))
4973 ret = FAIL;
4974
4975 *option_end = c; /* put back for error messages */
4976 *arg = option_end;
4977
4978 return ret;
4979}
4980
4981/*
4982 * Allocate a variable for a string constant.
4983 * Return OK or FAIL.
4984 */
4985 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004986get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 int evaluate;
4990{
4991 char_u *p;
4992 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 int extra = 0;
4994
4995 /*
4996 * Find the end of the string, skipping backslashed characters.
4997 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004998 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
5000 if (*p == '\\' && p[1] != NUL)
5001 {
5002 ++p;
5003 /* A "\<x>" form occupies at least 4 characters, and produces up
5004 * to 6 characters: reserve space for 2 extra */
5005 if (*p == '<')
5006 extra += 2;
5007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009
5010 if (*p != '"')
5011 {
5012 EMSG2(_("E114: Missing quote: %s"), *arg);
5013 return FAIL;
5014 }
5015
5016 /* If only parsing, set *arg and return here */
5017 if (!evaluate)
5018 {
5019 *arg = p + 1;
5020 return OK;
5021 }
5022
5023 /*
5024 * Copy the string into allocated memory, handling backslashed
5025 * characters.
5026 */
5027 name = alloc((unsigned)(p - *arg + extra));
5028 if (name == NULL)
5029 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 rettv->v_type = VAR_STRING;
5031 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
Bram Moolenaar8c711452005-01-14 21:53:12 +00005033 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
5035 if (*p == '\\')
5036 {
5037 switch (*++p)
5038 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 case 'b': *name++ = BS; ++p; break;
5040 case 'e': *name++ = ESC; ++p; break;
5041 case 'f': *name++ = FF; ++p; break;
5042 case 'n': *name++ = NL; ++p; break;
5043 case 'r': *name++ = CAR; ++p; break;
5044 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045
5046 case 'X': /* hex: "\x1", "\x12" */
5047 case 'x':
5048 case 'u': /* Unicode: "\u0023" */
5049 case 'U':
5050 if (vim_isxdigit(p[1]))
5051 {
5052 int n, nr;
5053 int c = toupper(*p);
5054
5055 if (c == 'X')
5056 n = 2;
5057 else
5058 n = 4;
5059 nr = 0;
5060 while (--n >= 0 && vim_isxdigit(p[1]))
5061 {
5062 ++p;
5063 nr = (nr << 4) + hex2nr(*p);
5064 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066#ifdef FEAT_MBYTE
5067 /* For "\u" store the number according to
5068 * 'encoding'. */
5069 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 else
5072#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005073 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 break;
5076
5077 /* octal: "\1", "\12", "\123" */
5078 case '0':
5079 case '1':
5080 case '2':
5081 case '3':
5082 case '4':
5083 case '5':
5084 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 case '7': *name = *p++ - '0';
5086 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 *name = (*name << 3) + *p++ - '0';
5089 if (*p >= '0' && *p <= '7')
5090 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 break;
5094
5095 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 if (extra != 0)
5098 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 break;
5101 }
5102 /* FALLTHROUGH */
5103
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 break;
5106 }
5107 }
5108 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005112 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 *arg = p + 1;
5114
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 return OK;
5116}
5117
5118/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 * Return OK or FAIL.
5121 */
5122 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005123get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 int evaluate;
5127{
5128 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005129 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005130 int reduce = 0;
5131
5132 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005134 */
5135 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5136 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005137 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005138 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005140 break;
5141 ++reduce;
5142 ++p;
5143 }
5144 }
5145
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005147 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005149 return FAIL;
5150 }
5151
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005153 if (!evaluate)
5154 {
5155 *arg = p + 1;
5156 return OK;
5157 }
5158
5159 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005161 */
5162 str = alloc((unsigned)((p - *arg) - reduce));
5163 if (str == NULL)
5164 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 rettv->v_type = VAR_STRING;
5166 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005167
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005169 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005170 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005171 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005173 break;
5174 ++p;
5175 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005177 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005179 *arg = p + 1;
5180
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005181 return OK;
5182}
5183
5184/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185 * Allocate a variable for a List and fill it from "*arg".
5186 * Return OK or FAIL.
5187 */
5188 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005190 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005191 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005192 int evaluate;
5193{
Bram Moolenaar33570922005-01-25 22:26:29 +00005194 list_T *l = NULL;
5195 typval_T tv;
5196 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005197
5198 if (evaluate)
5199 {
5200 l = list_alloc();
5201 if (l == NULL)
5202 return FAIL;
5203 }
5204
5205 *arg = skipwhite(*arg + 1);
5206 while (**arg != ']' && **arg != NUL)
5207 {
5208 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5209 goto failret;
5210 if (evaluate)
5211 {
5212 item = listitem_alloc();
5213 if (item != NULL)
5214 {
5215 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005216 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005217 list_append(l, item);
5218 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005219 else
5220 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005221 }
5222
5223 if (**arg == ']')
5224 break;
5225 if (**arg != ',')
5226 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 goto failret;
5229 }
5230 *arg = skipwhite(*arg + 1);
5231 }
5232
5233 if (**arg != ']')
5234 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236failret:
5237 if (evaluate)
5238 list_free(l);
5239 return FAIL;
5240 }
5241
5242 *arg = skipwhite(*arg + 1);
5243 if (evaluate)
5244 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005245 rettv->v_type = VAR_LIST;
5246 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 ++l->lv_refcount;
5248 }
5249
5250 return OK;
5251}
5252
5253/*
5254 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005255 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005257 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258list_alloc()
5259{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005260 list_T *l;
5261
5262 l = (list_T *)alloc_clear(sizeof(list_T));
5263 if (l != NULL)
5264 {
5265 /* Prepend the list to the list of lists for garbage collection. */
5266 if (first_list != NULL)
5267 first_list->lv_used_prev = l;
5268 l->lv_used_prev = NULL;
5269 l->lv_used_next = first_list;
5270 first_list = l;
5271 }
5272 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273}
5274
5275/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005276 * Allocate an empty list for a return value.
5277 * Returns OK or FAIL.
5278 */
5279 static int
5280rettv_list_alloc(rettv)
5281 typval_T *rettv;
5282{
5283 list_T *l = list_alloc();
5284
5285 if (l == NULL)
5286 return FAIL;
5287
5288 rettv->vval.v_list = l;
5289 rettv->v_type = VAR_LIST;
5290 ++l->lv_refcount;
5291 return OK;
5292}
5293
5294/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 * Unreference a list: decrement the reference count and free it when it
5296 * becomes zero.
5297 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005298 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005300 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005302 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5303 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304}
5305
5306/*
5307 * Free a list, including all items it points to.
5308 * Ignores the reference count.
5309 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005310 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005312 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005313{
Bram Moolenaar33570922005-01-25 22:26:29 +00005314 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315
Bram Moolenaard9fba312005-06-26 22:34:35 +00005316 /* Avoid that recursive reference to the list frees us again. */
5317 l->lv_refcount = DEL_REFCOUNT;
5318
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005319 /* Remove the list from the list of lists for garbage collection. */
5320 if (l->lv_used_prev == NULL)
5321 first_list = l->lv_used_next;
5322 else
5323 l->lv_used_prev->lv_used_next = l->lv_used_next;
5324 if (l->lv_used_next != NULL)
5325 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5326
Bram Moolenaard9fba312005-06-26 22:34:35 +00005327 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005329 /* Remove the item before deleting it. */
5330 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331 listitem_free(item);
5332 }
5333 vim_free(l);
5334}
5335
5336/*
5337 * Allocate a list item.
5338 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005339 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340listitem_alloc()
5341{
Bram Moolenaar33570922005-01-25 22:26:29 +00005342 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343}
5344
5345/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005346 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 */
5348 static void
5349listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005350 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005352 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 vim_free(item);
5354}
5355
5356/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005357 * Remove a list item from a List and free it. Also clears the value.
5358 */
5359 static void
5360listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005361 list_T *l;
5362 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005363{
5364 list_remove(l, item, item);
5365 listitem_free(item);
5366}
5367
5368/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 * Get the number of items in a list.
5370 */
5371 static long
5372list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005373 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 if (l == NULL)
5376 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005377 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378}
5379
5380/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005381 * Return TRUE when two lists have exactly the same values.
5382 */
5383 static int
5384list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005385 list_T *l1;
5386 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005387 int ic; /* ignore case for strings */
5388{
Bram Moolenaar33570922005-01-25 22:26:29 +00005389 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005390
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005391 if (list_len(l1) != list_len(l2))
5392 return FALSE;
5393
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005394 for (item1 = l1->lv_first, item2 = l2->lv_first;
5395 item1 != NULL && item2 != NULL;
5396 item1 = item1->li_next, item2 = item2->li_next)
5397 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5398 return FALSE;
5399 return item1 == NULL && item2 == NULL;
5400}
5401
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005402#if defined(FEAT_PYTHON) || defined(PROTO)
5403/*
5404 * Return the dictitem that an entry in a hashtable points to.
5405 */
5406 dictitem_T *
5407dict_lookup(hi)
5408 hashitem_T *hi;
5409{
5410 return HI2DI(hi);
5411}
5412#endif
5413
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005414/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005415 * Return TRUE when two dictionaries have exactly the same key/values.
5416 */
5417 static int
5418dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005419 dict_T *d1;
5420 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005421 int ic; /* ignore case for strings */
5422{
Bram Moolenaar33570922005-01-25 22:26:29 +00005423 hashitem_T *hi;
5424 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005425 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005426
5427 if (dict_len(d1) != dict_len(d2))
5428 return FALSE;
5429
Bram Moolenaar33570922005-01-25 22:26:29 +00005430 todo = d1->dv_hashtab.ht_used;
5431 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005432 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005433 if (!HASHITEM_EMPTY(hi))
5434 {
5435 item2 = dict_find(d2, hi->hi_key, -1);
5436 if (item2 == NULL)
5437 return FALSE;
5438 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5439 return FALSE;
5440 --todo;
5441 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005442 }
5443 return TRUE;
5444}
5445
5446/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005447 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005448 * Compares the items just like "==" would compare them, but strings and
5449 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005450 */
5451 static int
5452tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005453 typval_T *tv1;
5454 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005455 int ic; /* ignore case */
5456{
5457 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005458 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005459
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005460 if (tv1->v_type != tv2->v_type)
5461 return FALSE;
5462
5463 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005464 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005465 case VAR_LIST:
5466 /* recursive! */
5467 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5468
5469 case VAR_DICT:
5470 /* recursive! */
5471 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5472
5473 case VAR_FUNC:
5474 return (tv1->vval.v_string != NULL
5475 && tv2->vval.v_string != NULL
5476 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5477
5478 case VAR_NUMBER:
5479 return tv1->vval.v_number == tv2->vval.v_number;
5480
5481 case VAR_STRING:
5482 s1 = get_tv_string_buf(tv1, buf1);
5483 s2 = get_tv_string_buf(tv2, buf2);
5484 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005485 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005486
5487 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005488 return TRUE;
5489}
5490
5491/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 * Locate item with index "n" in list "l" and return it.
5493 * A negative index is counted from the end; -1 is the last item.
5494 * Returns NULL when "n" is out of range.
5495 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005496 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005497list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 long n;
5500{
Bram Moolenaar33570922005-01-25 22:26:29 +00005501 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 long idx;
5503
5504 if (l == NULL)
5505 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005506
5507 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005509 n = l->lv_len + n;
5510
5511 /* Check for index out of range. */
5512 if (n < 0 || n >= l->lv_len)
5513 return NULL;
5514
5515 /* When there is a cached index may start search from there. */
5516 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005518 if (n < l->lv_idx / 2)
5519 {
5520 /* closest to the start of the list */
5521 item = l->lv_first;
5522 idx = 0;
5523 }
5524 else if (n > (l->lv_idx + l->lv_len) / 2)
5525 {
5526 /* closest to the end of the list */
5527 item = l->lv_last;
5528 idx = l->lv_len - 1;
5529 }
5530 else
5531 {
5532 /* closest to the cached index */
5533 item = l->lv_idx_item;
5534 idx = l->lv_idx;
5535 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 }
5537 else
5538 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005539 if (n < l->lv_len / 2)
5540 {
5541 /* closest to the start of the list */
5542 item = l->lv_first;
5543 idx = 0;
5544 }
5545 else
5546 {
5547 /* closest to the end of the list */
5548 item = l->lv_last;
5549 idx = l->lv_len - 1;
5550 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005552
5553 while (n > idx)
5554 {
5555 /* search forward */
5556 item = item->li_next;
5557 ++idx;
5558 }
5559 while (n < idx)
5560 {
5561 /* search backward */
5562 item = item->li_prev;
5563 --idx;
5564 }
5565
5566 /* cache the used index */
5567 l->lv_idx = idx;
5568 l->lv_idx_item = item;
5569
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 return item;
5571}
5572
5573/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005574 * Get list item "l[idx]" as a number.
5575 */
5576 static long
5577list_find_nr(l, idx, errorp)
5578 list_T *l;
5579 long idx;
5580 int *errorp; /* set to TRUE when something wrong */
5581{
5582 listitem_T *li;
5583
5584 li = list_find(l, idx);
5585 if (li == NULL)
5586 {
5587 if (errorp != NULL)
5588 *errorp = TRUE;
5589 return -1L;
5590 }
5591 return get_tv_number_chk(&li->li_tv, errorp);
5592}
5593
5594/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005595 * Locate "item" list "l" and return its index.
5596 * Returns -1 when "item" is not in the list.
5597 */
5598 static long
5599list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 list_T *l;
5601 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005602{
5603 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005604 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005605
5606 if (l == NULL)
5607 return -1;
5608 idx = 0;
5609 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5610 ++idx;
5611 if (li == NULL)
5612 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005613 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005614}
5615
5616/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005617 * Append item "item" to the end of list "l".
5618 */
5619 static void
5620list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005621 list_T *l;
5622 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623{
5624 if (l->lv_last == NULL)
5625 {
5626 /* empty list */
5627 l->lv_first = item;
5628 l->lv_last = item;
5629 item->li_prev = NULL;
5630 }
5631 else
5632 {
5633 l->lv_last->li_next = item;
5634 item->li_prev = l->lv_last;
5635 l->lv_last = item;
5636 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005637 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005638 item->li_next = NULL;
5639}
5640
5641/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005642 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005643 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 */
5645 static int
5646list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005647 list_T *l;
5648 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005650 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005651
Bram Moolenaar05159a02005-02-26 23:04:13 +00005652 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005653 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005654 copy_tv(tv, &li->li_tv);
5655 list_append(l, li);
5656 return OK;
5657}
5658
5659/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005660 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005661 * Return FAIL when out of memory.
5662 */
5663 int
5664list_append_dict(list, dict)
5665 list_T *list;
5666 dict_T *dict;
5667{
5668 listitem_T *li = listitem_alloc();
5669
5670 if (li == NULL)
5671 return FAIL;
5672 li->li_tv.v_type = VAR_DICT;
5673 li->li_tv.v_lock = 0;
5674 li->li_tv.vval.v_dict = dict;
5675 list_append(list, li);
5676 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005677 return OK;
5678}
5679
5680/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005681 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005682 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005683 * Returns FAIL when out of memory.
5684 */
5685 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005686list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005687 list_T *l;
5688 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005689 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005690{
5691 listitem_T *li = listitem_alloc();
5692
5693 if (li == NULL)
5694 return FAIL;
5695 list_append(l, li);
5696 li->li_tv.v_type = VAR_STRING;
5697 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005698 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5699 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005700 return FAIL;
5701 return OK;
5702}
5703
5704/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005705 * Append "n" to list "l".
5706 * Returns FAIL when out of memory.
5707 */
5708 static int
5709list_append_number(l, n)
5710 list_T *l;
5711 varnumber_T n;
5712{
5713 listitem_T *li;
5714
5715 li = listitem_alloc();
5716 if (li == NULL)
5717 return FAIL;
5718 li->li_tv.v_type = VAR_NUMBER;
5719 li->li_tv.v_lock = 0;
5720 li->li_tv.vval.v_number = n;
5721 list_append(l, li);
5722 return OK;
5723}
5724
5725/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005726 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005727 * If "item" is NULL append at the end.
5728 * Return FAIL when out of memory.
5729 */
5730 static int
5731list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 list_T *l;
5733 typval_T *tv;
5734 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005735{
Bram Moolenaar33570922005-01-25 22:26:29 +00005736 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005737
5738 if (ni == NULL)
5739 return FAIL;
5740 copy_tv(tv, &ni->li_tv);
5741 if (item == NULL)
5742 /* Append new item at end of list. */
5743 list_append(l, ni);
5744 else
5745 {
5746 /* Insert new item before existing item. */
5747 ni->li_prev = item->li_prev;
5748 ni->li_next = item;
5749 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005750 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005751 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005752 ++l->lv_idx;
5753 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005754 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005755 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005756 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005757 l->lv_idx_item = NULL;
5758 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005759 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005760 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005761 }
5762 return OK;
5763}
5764
5765/*
5766 * Extend "l1" with "l2".
5767 * If "bef" is NULL append at the end, otherwise insert before this item.
5768 * Returns FAIL when out of memory.
5769 */
5770 static int
5771list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005772 list_T *l1;
5773 list_T *l2;
5774 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005775{
Bram Moolenaar33570922005-01-25 22:26:29 +00005776 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005777
5778 for (item = l2->lv_first; item != NULL; item = item->li_next)
5779 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5780 return FAIL;
5781 return OK;
5782}
5783
5784/*
5785 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5786 * Return FAIL when out of memory.
5787 */
5788 static int
5789list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005790 list_T *l1;
5791 list_T *l2;
5792 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005793{
Bram Moolenaar33570922005-01-25 22:26:29 +00005794 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005795
5796 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005797 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005798 if (l == NULL)
5799 return FAIL;
5800 tv->v_type = VAR_LIST;
5801 tv->vval.v_list = l;
5802
5803 /* append all items from the second list */
5804 return list_extend(l, l2, NULL);
5805}
5806
5807/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005808 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005809 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005810 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 * Returns NULL when out of memory.
5812 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005813 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005814list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005815 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005817 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818{
Bram Moolenaar33570922005-01-25 22:26:29 +00005819 list_T *copy;
5820 listitem_T *item;
5821 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822
5823 if (orig == NULL)
5824 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825
5826 copy = list_alloc();
5827 if (copy != NULL)
5828 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005829 if (copyID != 0)
5830 {
5831 /* Do this before adding the items, because one of the items may
5832 * refer back to this list. */
5833 orig->lv_copyID = copyID;
5834 orig->lv_copylist = copy;
5835 }
5836 for (item = orig->lv_first; item != NULL && !got_int;
5837 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005838 {
5839 ni = listitem_alloc();
5840 if (ni == NULL)
5841 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005842 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005843 {
5844 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5845 {
5846 vim_free(ni);
5847 break;
5848 }
5849 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005851 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 list_append(copy, ni);
5853 }
5854 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005855 if (item != NULL)
5856 {
5857 list_unref(copy);
5858 copy = NULL;
5859 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 }
5861
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 return copy;
5863}
5864
5865/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005866 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005867 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005869 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005870list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005871 list_T *l;
5872 listitem_T *item;
5873 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005874{
Bram Moolenaar33570922005-01-25 22:26:29 +00005875 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005877 /* notify watchers */
5878 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005880 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005881 list_fix_watch(l, ip);
5882 if (ip == item2)
5883 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005885
5886 if (item2->li_next == NULL)
5887 l->lv_last = item->li_prev;
5888 else
5889 item2->li_next->li_prev = item->li_prev;
5890 if (item->li_prev == NULL)
5891 l->lv_first = item2->li_next;
5892 else
5893 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005894 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895}
5896
5897/*
5898 * Return an allocated string with the string representation of a list.
5899 * May return NULL.
5900 */
5901 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005902list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005903 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005904 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905{
5906 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907
5908 if (tv->vval.v_list == NULL)
5909 return NULL;
5910 ga_init2(&ga, (int)sizeof(char), 80);
5911 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005912 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005913 {
5914 vim_free(ga.ga_data);
5915 return NULL;
5916 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917 ga_append(&ga, ']');
5918 ga_append(&ga, NUL);
5919 return (char_u *)ga.ga_data;
5920}
5921
5922/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005923 * Join list "l" into a string in "*gap", using separator "sep".
5924 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005925 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005927 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005928list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005929 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005930 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005931 char_u *sep;
5932 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005933 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934{
5935 int first = TRUE;
5936 char_u *tofree;
5937 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005938 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005939 char_u *s;
5940
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005941 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005942 {
5943 if (first)
5944 first = FALSE;
5945 else
5946 ga_concat(gap, sep);
5947
5948 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005949 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005950 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005951 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005952 if (s != NULL)
5953 ga_concat(gap, s);
5954 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005955 if (s == NULL)
5956 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005957 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005958 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005959}
5960
5961/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005962 * Garbage collection for lists and dictionaries.
5963 *
5964 * We use reference counts to be able to free most items right away when they
5965 * are no longer used. But for composite items it's possible that it becomes
5966 * unused while the reference count is > 0: When there is a recursive
5967 * reference. Example:
5968 * :let l = [1, 2, 3]
5969 * :let d = {9: l}
5970 * :let l[1] = d
5971 *
5972 * Since this is quite unusual we handle this with garbage collection: every
5973 * once in a while find out which lists and dicts are not referenced from any
5974 * variable.
5975 *
5976 * Here is a good reference text about garbage collection (refers to Python
5977 * but it applies to all reference-counting mechanisms):
5978 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005979 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005980
5981/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005982 * Do garbage collection for lists and dicts.
5983 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005984 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005985 int
5986garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005987{
5988 dict_T *dd;
5989 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005990 int copyID = ++current_copyID;
5991 buf_T *buf;
5992 win_T *wp;
5993 int i;
5994 funccall_T *fc;
5995 int did_free = FALSE;
5996
5997 /*
5998 * 1. Go through all accessible variables and mark all lists and dicts
5999 * with copyID.
6000 */
6001 /* script-local variables */
6002 for (i = 1; i <= ga_scripts.ga_len; ++i)
6003 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6004
6005 /* buffer-local variables */
6006 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6007 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6008
6009 /* window-local variables */
6010 FOR_ALL_WINDOWS(wp)
6011 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6012
6013 /* global variables */
6014 set_ref_in_ht(&globvarht, copyID);
6015
6016 /* function-local variables */
6017 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6018 {
6019 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6020 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6021 }
6022
6023 /*
6024 * 2. Go through the list of dicts and free items without the copyID.
6025 */
6026 for (dd = first_dict; dd != NULL; )
6027 if (dd->dv_copyID != copyID)
6028 {
6029 dict_free(dd);
6030 did_free = TRUE;
6031
6032 /* restart, next dict may also have been freed */
6033 dd = first_dict;
6034 }
6035 else
6036 dd = dd->dv_used_next;
6037
6038 /*
6039 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006040 * But don't free a list that has a watcher (used in a for loop), these
6041 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006042 */
6043 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006044 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006045 {
6046 list_free(ll);
6047 did_free = TRUE;
6048
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006049 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006050 ll = first_list;
6051 }
6052 else
6053 ll = ll->lv_used_next;
6054
6055 return did_free;
6056}
6057
6058/*
6059 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6060 */
6061 static void
6062set_ref_in_ht(ht, copyID)
6063 hashtab_T *ht;
6064 int copyID;
6065{
6066 int todo;
6067 hashitem_T *hi;
6068
6069 todo = ht->ht_used;
6070 for (hi = ht->ht_array; todo > 0; ++hi)
6071 if (!HASHITEM_EMPTY(hi))
6072 {
6073 --todo;
6074 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6075 }
6076}
6077
6078/*
6079 * Mark all lists and dicts referenced through list "l" with "copyID".
6080 */
6081 static void
6082set_ref_in_list(l, copyID)
6083 list_T *l;
6084 int copyID;
6085{
6086 listitem_T *li;
6087
6088 for (li = l->lv_first; li != NULL; li = li->li_next)
6089 set_ref_in_item(&li->li_tv, copyID);
6090}
6091
6092/*
6093 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6094 */
6095 static void
6096set_ref_in_item(tv, copyID)
6097 typval_T *tv;
6098 int copyID;
6099{
6100 dict_T *dd;
6101 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006102
6103 switch (tv->v_type)
6104 {
6105 case VAR_DICT:
6106 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006107 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006108 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006109 /* Didn't see this dict yet. */
6110 dd->dv_copyID = copyID;
6111 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006112 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006113 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006114
6115 case VAR_LIST:
6116 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006117 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006118 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006119 /* Didn't see this list yet. */
6120 ll->lv_copyID = copyID;
6121 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006122 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006123 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006124 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006125 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006126}
6127
6128/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006129 * Allocate an empty header for a dictionary.
6130 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006131 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006132dict_alloc()
6133{
Bram Moolenaar33570922005-01-25 22:26:29 +00006134 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006135
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006137 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006138 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006139 /* Add the list to the hashtable for garbage collection. */
6140 if (first_dict != NULL)
6141 first_dict->dv_used_prev = d;
6142 d->dv_used_next = first_dict;
6143 d->dv_used_prev = NULL;
6144
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006146 d->dv_lock = 0;
6147 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006148 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006149 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006150 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006151}
6152
6153/*
6154 * Unreference a Dictionary: decrement the reference count and free it when it
6155 * becomes zero.
6156 */
6157 static void
6158dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006159 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006160{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006161 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6162 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006163}
6164
6165/*
6166 * Free a Dictionary, including all items it contains.
6167 * Ignores the reference count.
6168 */
6169 static void
6170dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006171 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006172{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006173 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006174 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006175 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006176
Bram Moolenaard9fba312005-06-26 22:34:35 +00006177 /* Avoid that recursive reference to the dict frees us again. */
6178 d->dv_refcount = DEL_REFCOUNT;
6179
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006180 /* Remove the dict from the list of dicts for garbage collection. */
6181 if (d->dv_used_prev == NULL)
6182 first_dict = d->dv_used_next;
6183 else
6184 d->dv_used_prev->dv_used_next = d->dv_used_next;
6185 if (d->dv_used_next != NULL)
6186 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6187
6188 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006189 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006190 todo = d->dv_hashtab.ht_used;
6191 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006192 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006193 if (!HASHITEM_EMPTY(hi))
6194 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006195 /* Remove the item before deleting it, just in case there is
6196 * something recursive causing trouble. */
6197 di = HI2DI(hi);
6198 hash_remove(&d->dv_hashtab, hi);
6199 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006200 --todo;
6201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006202 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006203 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006204 vim_free(d);
6205}
6206
6207/*
6208 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006209 * The "key" is copied to the new item.
6210 * Note that the value of the item "di_tv" still needs to be initialized!
6211 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006212 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006213 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006214dictitem_alloc(key)
6215 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006216{
Bram Moolenaar33570922005-01-25 22:26:29 +00006217 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006218
Bram Moolenaar33570922005-01-25 22:26:29 +00006219 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006220 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006221 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006222 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006223 di->di_flags = 0;
6224 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006225 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006226}
6227
6228/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006229 * Make a copy of a Dictionary item.
6230 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006231 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006232dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006233 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006234{
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006236
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006238 if (di != NULL)
6239 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006240 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006241 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006242 copy_tv(&org->di_tv, &di->di_tv);
6243 }
6244 return di;
6245}
6246
6247/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006248 * Remove item "item" from Dictionary "dict" and free it.
6249 */
6250 static void
6251dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 dict_T *dict;
6253 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006254{
Bram Moolenaar33570922005-01-25 22:26:29 +00006255 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006256
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006258 if (HASHITEM_EMPTY(hi))
6259 EMSG2(_(e_intern2), "dictitem_remove()");
6260 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006261 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006262 dictitem_free(item);
6263}
6264
6265/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006266 * Free a dict item. Also clears the value.
6267 */
6268 static void
6269dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006271{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006272 clear_tv(&item->di_tv);
6273 vim_free(item);
6274}
6275
6276/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006277 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6278 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006279 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006280 * Returns NULL when out of memory.
6281 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006282 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006283dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006285 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006286 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006287{
Bram Moolenaar33570922005-01-25 22:26:29 +00006288 dict_T *copy;
6289 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006290 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006291 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006292
6293 if (orig == NULL)
6294 return NULL;
6295
6296 copy = dict_alloc();
6297 if (copy != NULL)
6298 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006299 if (copyID != 0)
6300 {
6301 orig->dv_copyID = copyID;
6302 orig->dv_copydict = copy;
6303 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006305 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006306 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006307 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006308 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006309 --todo;
6310
6311 di = dictitem_alloc(hi->hi_key);
6312 if (di == NULL)
6313 break;
6314 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006315 {
6316 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6317 copyID) == FAIL)
6318 {
6319 vim_free(di);
6320 break;
6321 }
6322 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006323 else
6324 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6325 if (dict_add(copy, di) == FAIL)
6326 {
6327 dictitem_free(di);
6328 break;
6329 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006330 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006331 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006332
Bram Moolenaare9a41262005-01-15 22:18:47 +00006333 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006334 if (todo > 0)
6335 {
6336 dict_unref(copy);
6337 copy = NULL;
6338 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006339 }
6340
6341 return copy;
6342}
6343
6344/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006345 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006346 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006347 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006348 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006349dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 dict_T *d;
6351 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006352{
Bram Moolenaar33570922005-01-25 22:26:29 +00006353 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006354}
6355
Bram Moolenaar8c711452005-01-14 21:53:12 +00006356/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006357 * Add a number or string entry to dictionary "d".
6358 * When "str" is NULL use number "nr", otherwise use "str".
6359 * Returns FAIL when out of memory and when key already exists.
6360 */
6361 int
6362dict_add_nr_str(d, key, nr, str)
6363 dict_T *d;
6364 char *key;
6365 long nr;
6366 char_u *str;
6367{
6368 dictitem_T *item;
6369
6370 item = dictitem_alloc((char_u *)key);
6371 if (item == NULL)
6372 return FAIL;
6373 item->di_tv.v_lock = 0;
6374 if (str == NULL)
6375 {
6376 item->di_tv.v_type = VAR_NUMBER;
6377 item->di_tv.vval.v_number = nr;
6378 }
6379 else
6380 {
6381 item->di_tv.v_type = VAR_STRING;
6382 item->di_tv.vval.v_string = vim_strsave(str);
6383 }
6384 if (dict_add(d, item) == FAIL)
6385 {
6386 dictitem_free(item);
6387 return FAIL;
6388 }
6389 return OK;
6390}
6391
6392/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006393 * Get the number of items in a Dictionary.
6394 */
6395 static long
6396dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006398{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006399 if (d == NULL)
6400 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006402}
6403
6404/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006405 * Find item "key[len]" in Dictionary "d".
6406 * If "len" is negative use strlen(key).
6407 * Returns NULL when not found.
6408 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006409 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006410dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006412 char_u *key;
6413 int len;
6414{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006415#define AKEYLEN 200
6416 char_u buf[AKEYLEN];
6417 char_u *akey;
6418 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006420
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006421 if (len < 0)
6422 akey = key;
6423 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006424 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006425 tofree = akey = vim_strnsave(key, len);
6426 if (akey == NULL)
6427 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006428 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006429 else
6430 {
6431 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006432 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006433 akey = buf;
6434 }
6435
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006437 vim_free(tofree);
6438 if (HASHITEM_EMPTY(hi))
6439 return NULL;
6440 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006441}
6442
6443/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006444 * Get a string item from a dictionary.
6445 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006446 * Returns NULL if the entry doesn't exist or out of memory.
6447 */
6448 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006449get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006450 dict_T *d;
6451 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006452 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006453{
6454 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006455 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006456
6457 di = dict_find(d, key, -1);
6458 if (di == NULL)
6459 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006460 s = get_tv_string(&di->di_tv);
6461 if (save && s != NULL)
6462 s = vim_strsave(s);
6463 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006464}
6465
6466/*
6467 * Get a number item from a dictionary.
6468 * Returns 0 if the entry doesn't exist or out of memory.
6469 */
6470 long
6471get_dict_number(d, key)
6472 dict_T *d;
6473 char_u *key;
6474{
6475 dictitem_T *di;
6476
6477 di = dict_find(d, key, -1);
6478 if (di == NULL)
6479 return 0;
6480 return get_tv_number(&di->di_tv);
6481}
6482
6483/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006484 * Return an allocated string with the string representation of a Dictionary.
6485 * May return NULL.
6486 */
6487 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006488dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006490 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006491{
6492 garray_T ga;
6493 int first = TRUE;
6494 char_u *tofree;
6495 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006496 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006497 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006498 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006499 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006500
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006501 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006502 return NULL;
6503 ga_init2(&ga, (int)sizeof(char), 80);
6504 ga_append(&ga, '{');
6505
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006507 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006508 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006509 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006510 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006511 --todo;
6512
6513 if (first)
6514 first = FALSE;
6515 else
6516 ga_concat(&ga, (char_u *)", ");
6517
6518 tofree = string_quote(hi->hi_key, FALSE);
6519 if (tofree != NULL)
6520 {
6521 ga_concat(&ga, tofree);
6522 vim_free(tofree);
6523 }
6524 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006525 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006526 if (s != NULL)
6527 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006528 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 if (s == NULL)
6530 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006531 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006532 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006533 if (todo > 0)
6534 {
6535 vim_free(ga.ga_data);
6536 return NULL;
6537 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006538
6539 ga_append(&ga, '}');
6540 ga_append(&ga, NUL);
6541 return (char_u *)ga.ga_data;
6542}
6543
6544/*
6545 * Allocate a variable for a Dictionary and fill it from "*arg".
6546 * Return OK or FAIL. Returns NOTDONE for {expr}.
6547 */
6548 static int
6549get_dict_tv(arg, rettv, evaluate)
6550 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006551 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006552 int evaluate;
6553{
Bram Moolenaar33570922005-01-25 22:26:29 +00006554 dict_T *d = NULL;
6555 typval_T tvkey;
6556 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006557 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006558 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006559 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006560 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006561
6562 /*
6563 * First check if it's not a curly-braces thing: {expr}.
6564 * Must do this without evaluating, otherwise a function may be called
6565 * twice. Unfortunately this means we need to call eval1() twice for the
6566 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006567 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006568 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006569 if (*start != '}')
6570 {
6571 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6572 return FAIL;
6573 if (*start == '}')
6574 return NOTDONE;
6575 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006576
6577 if (evaluate)
6578 {
6579 d = dict_alloc();
6580 if (d == NULL)
6581 return FAIL;
6582 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006583 tvkey.v_type = VAR_UNKNOWN;
6584 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006585
6586 *arg = skipwhite(*arg + 1);
6587 while (**arg != '}' && **arg != NUL)
6588 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006589 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006590 goto failret;
6591 if (**arg != ':')
6592 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006593 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006594 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006595 goto failret;
6596 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006597 key = get_tv_string_buf_chk(&tvkey, buf);
6598 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006600 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6601 if (key != NULL)
6602 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006603 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006604 goto failret;
6605 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006606
6607 *arg = skipwhite(*arg + 1);
6608 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6609 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006610 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006611 goto failret;
6612 }
6613 if (evaluate)
6614 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006615 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006616 if (item != NULL)
6617 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006618 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006619 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006620 clear_tv(&tv);
6621 goto failret;
6622 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006623 item = dictitem_alloc(key);
6624 clear_tv(&tvkey);
6625 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006627 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006628 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006629 if (dict_add(d, item) == FAIL)
6630 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006631 }
6632 }
6633
6634 if (**arg == '}')
6635 break;
6636 if (**arg != ',')
6637 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006638 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006639 goto failret;
6640 }
6641 *arg = skipwhite(*arg + 1);
6642 }
6643
6644 if (**arg != '}')
6645 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006646 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006647failret:
6648 if (evaluate)
6649 dict_free(d);
6650 return FAIL;
6651 }
6652
6653 *arg = skipwhite(*arg + 1);
6654 if (evaluate)
6655 {
6656 rettv->v_type = VAR_DICT;
6657 rettv->vval.v_dict = d;
6658 ++d->dv_refcount;
6659 }
6660
6661 return OK;
6662}
6663
Bram Moolenaar8c711452005-01-14 21:53:12 +00006664/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006665 * Return a string with the string representation of a variable.
6666 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006667 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006668 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006669 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670 * May return NULL;
6671 */
6672 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006673echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006674 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006676 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006677 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006679 static int recurse = 0;
6680 char_u *r = NULL;
6681
Bram Moolenaar33570922005-01-25 22:26:29 +00006682 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006683 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006684 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006685 *tofree = NULL;
6686 return NULL;
6687 }
6688 ++recurse;
6689
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006690 switch (tv->v_type)
6691 {
6692 case VAR_FUNC:
6693 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006694 r = tv->vval.v_string;
6695 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006696
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006697 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006698 if (tv->vval.v_list == NULL)
6699 {
6700 *tofree = NULL;
6701 r = NULL;
6702 }
6703 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6704 {
6705 *tofree = NULL;
6706 r = (char_u *)"[...]";
6707 }
6708 else
6709 {
6710 tv->vval.v_list->lv_copyID = copyID;
6711 *tofree = list2string(tv, copyID);
6712 r = *tofree;
6713 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006714 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006715
Bram Moolenaar8c711452005-01-14 21:53:12 +00006716 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006717 if (tv->vval.v_dict == NULL)
6718 {
6719 *tofree = NULL;
6720 r = NULL;
6721 }
6722 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6723 {
6724 *tofree = NULL;
6725 r = (char_u *)"{...}";
6726 }
6727 else
6728 {
6729 tv->vval.v_dict->dv_copyID = copyID;
6730 *tofree = dict2string(tv, copyID);
6731 r = *tofree;
6732 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006733 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006734
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006735 case VAR_STRING:
6736 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006737 *tofree = NULL;
6738 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006739 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006740
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006741 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006742 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006743 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006744 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006745
6746 --recurse;
6747 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006748}
6749
6750/*
6751 * Return a string with the string representation of a variable.
6752 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6753 * "numbuf" is used for a number.
6754 * Puts quotes around strings, so that they can be parsed back by eval().
6755 * May return NULL;
6756 */
6757 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006758tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006759 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006760 char_u **tofree;
6761 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006762 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006763{
6764 switch (tv->v_type)
6765 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006766 case VAR_FUNC:
6767 *tofree = string_quote(tv->vval.v_string, TRUE);
6768 return *tofree;
6769 case VAR_STRING:
6770 *tofree = string_quote(tv->vval.v_string, FALSE);
6771 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006772 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006773 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006774 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006775 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006777 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006778 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006779 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006780}
6781
6782/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006783 * Return string "str" in ' quotes, doubling ' characters.
6784 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006785 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006786 */
6787 static char_u *
6788string_quote(str, function)
6789 char_u *str;
6790 int function;
6791{
Bram Moolenaar33570922005-01-25 22:26:29 +00006792 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006793 char_u *p, *r, *s;
6794
Bram Moolenaar33570922005-01-25 22:26:29 +00006795 len = (function ? 13 : 3);
6796 if (str != NULL)
6797 {
6798 len += STRLEN(str);
6799 for (p = str; *p != NUL; mb_ptr_adv(p))
6800 if (*p == '\'')
6801 ++len;
6802 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 s = r = alloc(len);
6804 if (r != NULL)
6805 {
6806 if (function)
6807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006808 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006809 r += 10;
6810 }
6811 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006812 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006813 if (str != NULL)
6814 for (p = str; *p != NUL; )
6815 {
6816 if (*p == '\'')
6817 *r++ = '\'';
6818 MB_COPY_CHAR(p, r);
6819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006820 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006821 if (function)
6822 *r++ = ')';
6823 *r++ = NUL;
6824 }
6825 return s;
6826}
6827
6828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 * Get the value of an environment variable.
6830 * "arg" is pointing to the '$'. It is advanced to after the name.
6831 * If the environment variable was not set, silently assume it is empty.
6832 * Always return OK.
6833 */
6834 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006835get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 int evaluate;
6839{
6840 char_u *string = NULL;
6841 int len;
6842 int cc;
6843 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006844 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845
6846 ++*arg;
6847 name = *arg;
6848 len = get_env_len(arg);
6849 if (evaluate)
6850 {
6851 if (len != 0)
6852 {
6853 cc = name[len];
6854 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006855 /* first try vim_getenv(), fast for normal environment vars */
6856 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006858 {
6859 if (!mustfree)
6860 string = vim_strsave(string);
6861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 else
6863 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006864 if (mustfree)
6865 vim_free(string);
6866
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 /* next try expanding things like $VIM and ${HOME} */
6868 string = expand_env_save(name - 1);
6869 if (string != NULL && *string == '$')
6870 {
6871 vim_free(string);
6872 string = NULL;
6873 }
6874 }
6875 name[len] = cc;
6876 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006877 rettv->v_type = VAR_STRING;
6878 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 }
6880
6881 return OK;
6882}
6883
6884/*
6885 * Array with names and number of arguments of all internal functions
6886 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6887 */
6888static struct fst
6889{
6890 char *f_name; /* function name */
6891 char f_min_argc; /* minimal number of arguments */
6892 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006893 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006894 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895} functions[] =
6896{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006897 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 {"append", 2, 2, f_append},
6899 {"argc", 0, 0, f_argc},
6900 {"argidx", 0, 0, f_argidx},
6901 {"argv", 1, 1, f_argv},
6902 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006903 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {"bufexists", 1, 1, f_bufexists},
6905 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6906 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6907 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6908 {"buflisted", 1, 1, f_buflisted},
6909 {"bufloaded", 1, 1, f_bufloaded},
6910 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006911 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 {"bufwinnr", 1, 1, f_bufwinnr},
6913 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006914 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006915 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00006916 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {"char2nr", 1, 1, f_char2nr},
6918 {"cindent", 1, 1, f_cindent},
6919 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006920#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00006921 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006922 {"complete_add", 1, 1, f_complete_add},
6923 {"complete_check", 0, 0, f_complete_check},
6924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006926 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006927 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00006929 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006930 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {"delete", 1, 1, f_delete},
6932 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006933 {"diff_filler", 1, 1, f_diff_filler},
6934 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006935 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006937 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {"eventhandler", 0, 0, f_eventhandler},
6939 {"executable", 1, 1, f_executable},
6940 {"exists", 1, 1, f_exists},
6941 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006942 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6944 {"filereadable", 1, 1, f_filereadable},
6945 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006946 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006947 {"finddir", 1, 3, f_finddir},
6948 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 {"fnamemodify", 2, 2, f_fnamemodify},
6950 {"foldclosed", 1, 1, f_foldclosed},
6951 {"foldclosedend", 1, 1, f_foldclosedend},
6952 {"foldlevel", 1, 1, f_foldlevel},
6953 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006954 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006956 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006958 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006959 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 {"getbufvar", 2, 2, f_getbufvar},
6961 {"getchar", 0, 1, f_getchar},
6962 {"getcharmod", 0, 0, f_getcharmod},
6963 {"getcmdline", 0, 0, f_getcmdline},
6964 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006965 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006967 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006968 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 {"getfsize", 1, 1, f_getfsize},
6970 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006971 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006972 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006973 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00006974 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006975 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006976 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {"getregtype", 0, 1, f_getregtype},
6978 {"getwinposx", 0, 0, f_getwinposx},
6979 {"getwinposy", 0, 0, f_getwinposy},
6980 {"getwinvar", 2, 2, f_getwinvar},
6981 {"glob", 1, 1, f_glob},
6982 {"globpath", 2, 2, f_globpath},
6983 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006984 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00006985 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6987 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6988 {"histadd", 2, 2, f_histadd},
6989 {"histdel", 1, 2, f_histdel},
6990 {"histget", 1, 2, f_histget},
6991 {"histnr", 1, 1, f_histnr},
6992 {"hlID", 1, 1, f_hlID},
6993 {"hlexists", 1, 1, f_hlexists},
6994 {"hostname", 0, 0, f_hostname},
6995 {"iconv", 3, 3, f_iconv},
6996 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006997 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006998 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007000 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 {"inputrestore", 0, 0, f_inputrestore},
7002 {"inputsave", 0, 0, f_inputsave},
7003 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007004 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007006 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007007 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007008 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007009 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007011 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 {"libcall", 3, 3, f_libcall},
7013 {"libcallnr", 3, 3, f_libcallnr},
7014 {"line", 1, 1, f_line},
7015 {"line2byte", 1, 1, f_line2byte},
7016 {"lispindent", 1, 1, f_lispindent},
7017 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007018 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007019 {"maparg", 1, 3, f_maparg},
7020 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007021 {"match", 2, 4, f_match},
7022 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007023 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007024 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007025 {"max", 1, 1, f_max},
7026 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007027#ifdef vim_mkdir
7028 {"mkdir", 1, 3, f_mkdir},
7029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 {"mode", 0, 0, f_mode},
7031 {"nextnonblank", 1, 1, f_nextnonblank},
7032 {"nr2char", 1, 1, f_nr2char},
7033 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007034 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007035 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007036 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007037 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007038 {"reltime", 0, 2, f_reltime},
7039 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 {"remote_expr", 2, 3, f_remote_expr},
7041 {"remote_foreground", 1, 1, f_remote_foreground},
7042 {"remote_peek", 1, 2, f_remote_peek},
7043 {"remote_read", 1, 1, f_remote_read},
7044 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007045 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007047 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007049 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007050 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007051 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007052 {"searchpair", 3, 6, f_searchpair},
7053 {"searchpairpos", 3, 6, f_searchpairpos},
7054 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {"server2client", 2, 2, f_server2client},
7056 {"serverlist", 0, 0, f_serverlist},
7057 {"setbufvar", 3, 3, f_setbufvar},
7058 {"setcmdpos", 1, 1, f_setcmdpos},
7059 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007060 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007061 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007062 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {"setreg", 2, 3, f_setreg},
7064 {"setwinvar", 3, 3, f_setwinvar},
7065 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007066 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007067 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007068 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007069 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007070 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007071 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072#ifdef HAVE_STRFTIME
7073 {"strftime", 1, 2, f_strftime},
7074#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007075 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007076 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {"strlen", 1, 1, f_strlen},
7078 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007079 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {"strtrans", 1, 1, f_strtrans},
7081 {"submatch", 1, 1, f_submatch},
7082 {"substitute", 4, 4, f_substitute},
7083 {"synID", 3, 3, f_synID},
7084 {"synIDattr", 2, 3, f_synIDattr},
7085 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007086 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007087 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007088 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007089 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007090 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007091 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007093 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {"tolower", 1, 1, f_tolower},
7095 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007096 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007098 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {"virtcol", 1, 1, f_virtcol},
7100 {"visualmode", 0, 1, f_visualmode},
7101 {"winbufnr", 1, 1, f_winbufnr},
7102 {"wincol", 0, 0, f_wincol},
7103 {"winheight", 1, 1, f_winheight},
7104 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007105 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007107 {"winrestview", 1, 1, f_winrestview},
7108 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007110 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111};
7112
7113#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7114
7115/*
7116 * Function given to ExpandGeneric() to obtain the list of internal
7117 * or user defined function names.
7118 */
7119 char_u *
7120get_function_name(xp, idx)
7121 expand_T *xp;
7122 int idx;
7123{
7124 static int intidx = -1;
7125 char_u *name;
7126
7127 if (idx == 0)
7128 intidx = -1;
7129 if (intidx < 0)
7130 {
7131 name = get_user_func_name(xp, idx);
7132 if (name != NULL)
7133 return name;
7134 }
7135 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7136 {
7137 STRCPY(IObuff, functions[intidx].f_name);
7138 STRCAT(IObuff, "(");
7139 if (functions[intidx].f_max_argc == 0)
7140 STRCAT(IObuff, ")");
7141 return IObuff;
7142 }
7143
7144 return NULL;
7145}
7146
7147/*
7148 * Function given to ExpandGeneric() to obtain the list of internal or
7149 * user defined variable or function names.
7150 */
7151/*ARGSUSED*/
7152 char_u *
7153get_expr_name(xp, idx)
7154 expand_T *xp;
7155 int idx;
7156{
7157 static int intidx = -1;
7158 char_u *name;
7159
7160 if (idx == 0)
7161 intidx = -1;
7162 if (intidx < 0)
7163 {
7164 name = get_function_name(xp, idx);
7165 if (name != NULL)
7166 return name;
7167 }
7168 return get_user_var_name(xp, ++intidx);
7169}
7170
7171#endif /* FEAT_CMDL_COMPL */
7172
7173/*
7174 * Find internal function in table above.
7175 * Return index, or -1 if not found
7176 */
7177 static int
7178find_internal_func(name)
7179 char_u *name; /* name of the function */
7180{
7181 int first = 0;
7182 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7183 int cmp;
7184 int x;
7185
7186 /*
7187 * Find the function name in the table. Binary search.
7188 */
7189 while (first <= last)
7190 {
7191 x = first + ((unsigned)(last - first) >> 1);
7192 cmp = STRCMP(name, functions[x].f_name);
7193 if (cmp < 0)
7194 last = x - 1;
7195 else if (cmp > 0)
7196 first = x + 1;
7197 else
7198 return x;
7199 }
7200 return -1;
7201}
7202
7203/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007204 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7205 * name it contains, otherwise return "name".
7206 */
7207 static char_u *
7208deref_func_name(name, lenp)
7209 char_u *name;
7210 int *lenp;
7211{
Bram Moolenaar33570922005-01-25 22:26:29 +00007212 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007213 int cc;
7214
7215 cc = name[*lenp];
7216 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007217 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007218 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007219 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007220 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007221 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007222 {
7223 *lenp = 0;
7224 return (char_u *)""; /* just in case */
7225 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007226 *lenp = STRLEN(v->di_tv.vval.v_string);
7227 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007228 }
7229
7230 return name;
7231}
7232
7233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 * Allocate a variable for the result of a function.
7235 * Return OK or FAIL.
7236 */
7237 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007238get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7239 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 char_u *name; /* name of the function */
7241 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 char_u **arg; /* argument, pointing to the '(' */
7244 linenr_T firstline; /* first line of range */
7245 linenr_T lastline; /* last line of range */
7246 int *doesrange; /* return: function handled range */
7247 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007248 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249{
7250 char_u *argp;
7251 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 int argcount = 0; /* number of arguments found */
7254
7255 /*
7256 * Get the arguments.
7257 */
7258 argp = *arg;
7259 while (argcount < MAX_FUNC_ARGS)
7260 {
7261 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7262 if (*argp == ')' || *argp == ',' || *argp == NUL)
7263 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7265 {
7266 ret = FAIL;
7267 break;
7268 }
7269 ++argcount;
7270 if (*argp != ',')
7271 break;
7272 }
7273 if (*argp == ')')
7274 ++argp;
7275 else
7276 ret = FAIL;
7277
7278 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007279 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007280 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 {
7283 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007284 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007286 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288
7289 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007290 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291
7292 *arg = skipwhite(argp);
7293 return ret;
7294}
7295
7296
7297/*
7298 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007299 * Return OK when the function can't be called, FAIL otherwise.
7300 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 */
7302 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007303call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 char_u *name; /* name of the function */
7306 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007309 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 linenr_T firstline; /* first line of range */
7311 linenr_T lastline; /* last line of range */
7312 int *doesrange; /* return: function handled range */
7313 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315{
7316 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317#define ERROR_UNKNOWN 0
7318#define ERROR_TOOMANY 1
7319#define ERROR_TOOFEW 2
7320#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007321#define ERROR_DICT 4
7322#define ERROR_NONE 5
7323#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 int error = ERROR_NONE;
7325 int i;
7326 int llen;
7327 ufunc_T *fp;
7328 int cc;
7329#define FLEN_FIXED 40
7330 char_u fname_buf[FLEN_FIXED + 1];
7331 char_u *fname;
7332
7333 /*
7334 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7335 * Change <SNR>123_name() to K_SNR 123_name().
7336 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7337 */
7338 cc = name[len];
7339 name[len] = NUL;
7340 llen = eval_fname_script(name);
7341 if (llen > 0)
7342 {
7343 fname_buf[0] = K_SPECIAL;
7344 fname_buf[1] = KS_EXTRA;
7345 fname_buf[2] = (int)KE_SNR;
7346 i = 3;
7347 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7348 {
7349 if (current_SID <= 0)
7350 error = ERROR_SCRIPT;
7351 else
7352 {
7353 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7354 i = (int)STRLEN(fname_buf);
7355 }
7356 }
7357 if (i + STRLEN(name + llen) < FLEN_FIXED)
7358 {
7359 STRCPY(fname_buf + i, name + llen);
7360 fname = fname_buf;
7361 }
7362 else
7363 {
7364 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7365 if (fname == NULL)
7366 error = ERROR_OTHER;
7367 else
7368 {
7369 mch_memmove(fname, fname_buf, (size_t)i);
7370 STRCPY(fname + i, name + llen);
7371 }
7372 }
7373 }
7374 else
7375 fname = name;
7376
7377 *doesrange = FALSE;
7378
7379
7380 /* execute the function if no errors detected and executing */
7381 if (evaluate && error == ERROR_NONE)
7382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007383 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 error = ERROR_UNKNOWN;
7385
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007386 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 {
7388 /*
7389 * User defined function.
7390 */
7391 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007392
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007394 /* Trigger FuncUndefined event, may load the function. */
7395 if (fp == NULL
7396 && apply_autocmds(EVENT_FUNCUNDEFINED,
7397 fname, fname, TRUE, NULL)
7398 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007400 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 fp = find_func(fname);
7402 }
7403#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007404 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007405 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007406 {
7407 /* loaded a package, search for the function again */
7408 fp = find_func(fname);
7409 }
7410
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 if (fp != NULL)
7412 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007413 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007415 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007417 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007419 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007420 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 else
7422 {
7423 /*
7424 * Call the user function.
7425 * Save and restore search patterns, script variables and
7426 * redo buffer.
7427 */
7428 save_search_patterns();
7429 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007430 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007431 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007432 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007433 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7434 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7435 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007436 /* Function was unreferenced while being used, free it
7437 * now. */
7438 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 restoreRedobuff();
7440 restore_search_patterns();
7441 error = ERROR_NONE;
7442 }
7443 }
7444 }
7445 else
7446 {
7447 /*
7448 * Find the function name in the table, call its implementation.
7449 */
7450 i = find_internal_func(fname);
7451 if (i >= 0)
7452 {
7453 if (argcount < functions[i].f_min_argc)
7454 error = ERROR_TOOFEW;
7455 else if (argcount > functions[i].f_max_argc)
7456 error = ERROR_TOOMANY;
7457 else
7458 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007459 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007460 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 error = ERROR_NONE;
7462 }
7463 }
7464 }
7465 /*
7466 * The function call (or "FuncUndefined" autocommand sequence) might
7467 * have been aborted by an error, an interrupt, or an explicitly thrown
7468 * exception that has not been caught so far. This situation can be
7469 * tested for by calling aborting(). For an error in an internal
7470 * function or for the "E132" error in call_user_func(), however, the
7471 * throw point at which the "force_abort" flag (temporarily reset by
7472 * emsg()) is normally updated has not been reached yet. We need to
7473 * update that flag first to make aborting() reliable.
7474 */
7475 update_force_abort();
7476 }
7477 if (error == ERROR_NONE)
7478 ret = OK;
7479
7480 /*
7481 * Report an error unless the argument evaluation or function call has been
7482 * cancelled due to an aborting error, an interrupt, or an exception.
7483 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 if (!aborting())
7485 {
7486 switch (error)
7487 {
7488 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007489 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 break;
7491 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007492 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493 break;
7494 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007495 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496 name);
7497 break;
7498 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007499 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 name);
7501 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007502 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007503 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007504 name);
7505 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506 }
7507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508
7509 name[len] = cc;
7510 if (fname != name && fname != fname_buf)
7511 vim_free(fname);
7512
7513 return ret;
7514}
7515
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007516/*
7517 * Give an error message with a function name. Handle <SNR> things.
7518 */
7519 static void
7520emsg_funcname(msg, name)
7521 char *msg;
7522 char_u *name;
7523{
7524 char_u *p;
7525
7526 if (*name == K_SPECIAL)
7527 p = concat_str((char_u *)"<SNR>", name + 3);
7528 else
7529 p = name;
7530 EMSG2(_(msg), p);
7531 if (p != name)
7532 vim_free(p);
7533}
7534
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535/*********************************************
7536 * Implementation of the built-in functions
7537 */
7538
7539/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007540 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 */
7542 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007543f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 typval_T *argvars;
7545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546{
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007549 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007550 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007552 if ((l = argvars[0].vval.v_list) != NULL
7553 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7554 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007555 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007556 }
7557 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007558 EMSG(_(e_listreq));
7559}
7560
7561/*
7562 * "append(lnum, string/list)" function
7563 */
7564 static void
7565f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 typval_T *argvars;
7567 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007568{
7569 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007570 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007571 list_T *l = NULL;
7572 listitem_T *li = NULL;
7573 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007574 long added = 0;
7575
Bram Moolenaar0d660222005-01-07 21:51:51 +00007576 lnum = get_tv_lnum(argvars);
7577 if (lnum >= 0
7578 && lnum <= curbuf->b_ml.ml_line_count
7579 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007580 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007581 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007582 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007583 l = argvars[1].vval.v_list;
7584 if (l == NULL)
7585 return;
7586 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007587 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007588 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007589 for (;;)
7590 {
7591 if (l == NULL)
7592 tv = &argvars[1]; /* append a string */
7593 else if (li == NULL)
7594 break; /* end of list */
7595 else
7596 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007597 line = get_tv_string_chk(tv);
7598 if (line == NULL) /* type error */
7599 {
7600 rettv->vval.v_number = 1; /* Failed */
7601 break;
7602 }
7603 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007604 ++added;
7605 if (l == NULL)
7606 break;
7607 li = li->li_next;
7608 }
7609
7610 appended_lines_mark(lnum, added);
7611 if (curwin->w_cursor.lnum > lnum)
7612 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007614 else
7615 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616}
7617
7618/*
7619 * "argc()" function
7620 */
7621/* ARGSUSED */
7622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007623f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007624 typval_T *argvars;
7625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007627 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628}
7629
7630/*
7631 * "argidx()" function
7632 */
7633/* ARGSUSED */
7634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007635f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007636 typval_T *argvars;
7637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007639 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640}
7641
7642/*
7643 * "argv(nr)" function
7644 */
7645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007646f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007647 typval_T *argvars;
7648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649{
7650 int idx;
7651
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007652 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007654 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007656 rettv->vval.v_string = NULL;
7657 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658}
7659
7660/*
7661 * "browse(save, title, initdir, default)" function
7662 */
7663/* ARGSUSED */
7664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007665f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 typval_T *argvars;
7667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668{
7669#ifdef FEAT_BROWSE
7670 int save;
7671 char_u *title;
7672 char_u *initdir;
7673 char_u *defname;
7674 char_u buf[NUMBUFLEN];
7675 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007676 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007678 save = get_tv_number_chk(&argvars[0], &error);
7679 title = get_tv_string_chk(&argvars[1]);
7680 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7681 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007683 if (error || title == NULL || initdir == NULL || defname == NULL)
7684 rettv->vval.v_string = NULL;
7685 else
7686 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007687 do_browse(save ? BROWSE_SAVE : 0,
7688 title, defname, NULL, initdir, NULL, curbuf);
7689#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007690 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007691#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007692 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007693}
7694
7695/*
7696 * "browsedir(title, initdir)" function
7697 */
7698/* ARGSUSED */
7699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007700f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 typval_T *argvars;
7702 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007703{
7704#ifdef FEAT_BROWSE
7705 char_u *title;
7706 char_u *initdir;
7707 char_u buf[NUMBUFLEN];
7708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007709 title = get_tv_string_chk(&argvars[0]);
7710 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007712 if (title == NULL || initdir == NULL)
7713 rettv->vval.v_string = NULL;
7714 else
7715 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007716 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007718 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007720 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721}
7722
Bram Moolenaar33570922005-01-25 22:26:29 +00007723static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007724
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725/*
7726 * Find a buffer by number or exact name.
7727 */
7728 static buf_T *
7729find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007730 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731{
7732 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007734 if (avar->v_type == VAR_NUMBER)
7735 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007736 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007738 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007739 if (buf == NULL)
7740 {
7741 /* No full path name match, try a match with a URL or a "nofile"
7742 * buffer, these don't use the full path. */
7743 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7744 if (buf->b_fname != NULL
7745 && (path_with_url(buf->b_fname)
7746#ifdef FEAT_QUICKFIX
7747 || bt_nofile(buf)
7748#endif
7749 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007750 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007751 break;
7752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 }
7754 return buf;
7755}
7756
7757/*
7758 * "bufexists(expr)" function
7759 */
7760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007761f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007762 typval_T *argvars;
7763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766}
7767
7768/*
7769 * "buflisted(expr)" function
7770 */
7771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007772f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007773 typval_T *argvars;
7774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
7776 buf_T *buf;
7777
7778 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007779 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780}
7781
7782/*
7783 * "bufloaded(expr)" function
7784 */
7785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007786f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007787 typval_T *argvars;
7788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789{
7790 buf_T *buf;
7791
7792 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007793 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794}
7795
Bram Moolenaar33570922005-01-25 22:26:29 +00007796static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007797
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798/*
7799 * Get buffer by number or pattern.
7800 */
7801 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007803 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007805 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 int save_magic;
7807 char_u *save_cpo;
7808 buf_T *buf;
7809
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007810 if (tv->v_type == VAR_NUMBER)
7811 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007812 if (tv->v_type != VAR_STRING)
7813 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 if (name == NULL || *name == NUL)
7815 return curbuf;
7816 if (name[0] == '$' && name[1] == NUL)
7817 return lastbuf;
7818
7819 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7820 save_magic = p_magic;
7821 p_magic = TRUE;
7822 save_cpo = p_cpo;
7823 p_cpo = (char_u *)"";
7824
7825 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7826 TRUE, FALSE));
7827
7828 p_magic = save_magic;
7829 p_cpo = save_cpo;
7830
7831 /* If not found, try expanding the name, like done for bufexists(). */
7832 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007833 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834
7835 return buf;
7836}
7837
7838/*
7839 * "bufname(expr)" function
7840 */
7841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007842f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 typval_T *argvars;
7844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
7846 buf_T *buf;
7847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007848 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007850 buf = get_buf_tv(&argvars[0]);
7851 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007853 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007855 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 --emsg_off;
7857}
7858
7859/*
7860 * "bufnr(expr)" function
7861 */
7862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007863f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 typval_T *argvars;
7865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866{
7867 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007868 int error = FALSE;
7869 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007871 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007873 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007874 --emsg_off;
7875
7876 /* If the buffer isn't found and the second argument is not zero create a
7877 * new buffer. */
7878 if (buf == NULL
7879 && argvars[1].v_type != VAR_UNKNOWN
7880 && get_tv_number_chk(&argvars[1], &error) != 0
7881 && !error
7882 && (name = get_tv_string_chk(&argvars[0])) != NULL
7883 && !error)
7884 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7885
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007887 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007889 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890}
7891
7892/*
7893 * "bufwinnr(nr)" function
7894 */
7895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007896f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007897 typval_T *argvars;
7898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899{
7900#ifdef FEAT_WINDOWS
7901 win_T *wp;
7902 int winnr = 0;
7903#endif
7904 buf_T *buf;
7905
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007906 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007908 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909#ifdef FEAT_WINDOWS
7910 for (wp = firstwin; wp; wp = wp->w_next)
7911 {
7912 ++winnr;
7913 if (wp->w_buffer == buf)
7914 break;
7915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007916 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007918 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919#endif
7920 --emsg_off;
7921}
7922
7923/*
7924 * "byte2line(byte)" function
7925 */
7926/*ARGSUSED*/
7927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007928f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007929 typval_T *argvars;
7930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931{
7932#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007933 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934#else
7935 long boff = 0;
7936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007937 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007939 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 (linenr_T)0, &boff);
7943#endif
7944}
7945
7946/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007947 * "byteidx()" function
7948 */
7949/*ARGSUSED*/
7950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007951f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 typval_T *argvars;
7953 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007954{
7955#ifdef FEAT_MBYTE
7956 char_u *t;
7957#endif
7958 char_u *str;
7959 long idx;
7960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007961 str = get_tv_string_chk(&argvars[0]);
7962 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007963 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007964 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007965 return;
7966
7967#ifdef FEAT_MBYTE
7968 t = str;
7969 for ( ; idx > 0; idx--)
7970 {
7971 if (*t == NUL) /* EOL reached */
7972 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007973 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007974 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007975 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007976#else
7977 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007978 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007979#endif
7980}
7981
7982/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007983 * "call(func, arglist)" function
7984 */
7985 static void
7986f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007987 typval_T *argvars;
7988 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007989{
7990 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007991 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007992 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007993 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007994 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007995 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007996
7997 rettv->vval.v_number = 0;
7998 if (argvars[1].v_type != VAR_LIST)
7999 {
8000 EMSG(_(e_listreq));
8001 return;
8002 }
8003 if (argvars[1].vval.v_list == NULL)
8004 return;
8005
8006 if (argvars[0].v_type == VAR_FUNC)
8007 func = argvars[0].vval.v_string;
8008 else
8009 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008010 if (*func == NUL)
8011 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008012
Bram Moolenaare9a41262005-01-15 22:18:47 +00008013 if (argvars[2].v_type != VAR_UNKNOWN)
8014 {
8015 if (argvars[2].v_type != VAR_DICT)
8016 {
8017 EMSG(_(e_dictreq));
8018 return;
8019 }
8020 selfdict = argvars[2].vval.v_dict;
8021 }
8022
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008023 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8024 item = item->li_next)
8025 {
8026 if (argc == MAX_FUNC_ARGS)
8027 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008028 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008029 break;
8030 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008031 /* Make a copy of each argument. This is needed to be able to set
8032 * v_lock to VAR_FIXED in the copy without changing the original list.
8033 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008034 copy_tv(&item->li_tv, &argv[argc++]);
8035 }
8036
8037 if (item == NULL)
8038 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008039 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8040 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008041
8042 /* Free the arguments. */
8043 while (argc > 0)
8044 clear_tv(&argv[--argc]);
8045}
8046
8047/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008048 * "changenr()" function
8049 */
8050/*ARGSUSED*/
8051 static void
8052f_changenr(argvars, rettv)
8053 typval_T *argvars;
8054 typval_T *rettv;
8055{
8056 rettv->vval.v_number = curbuf->b_u_seq_cur;
8057}
8058
8059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 * "char2nr(string)" function
8061 */
8062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008063f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008064 typval_T *argvars;
8065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066{
8067#ifdef FEAT_MBYTE
8068 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008069 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 else
8071#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008072 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073}
8074
8075/*
8076 * "cindent(lnum)" function
8077 */
8078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008079f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008080 typval_T *argvars;
8081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082{
8083#ifdef FEAT_CINDENT
8084 pos_T pos;
8085 linenr_T lnum;
8086
8087 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008088 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8090 {
8091 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008092 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 curwin->w_cursor = pos;
8094 }
8095 else
8096#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008097 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098}
8099
8100/*
8101 * "col(string)" function
8102 */
8103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008104f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008105 typval_T *argvars;
8106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108 colnr_T col = 0;
8109 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008110 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008112 fp = var2fpos(&argvars[0], FALSE, &fnum);
8113 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {
8115 if (fp->col == MAXCOL)
8116 {
8117 /* '> can be MAXCOL, get the length of the line then */
8118 if (fp->lnum <= curbuf->b_ml.ml_line_count)
8119 col = STRLEN(ml_get(fp->lnum)) + 1;
8120 else
8121 col = MAXCOL;
8122 }
8123 else
8124 {
8125 col = fp->col + 1;
8126#ifdef FEAT_VIRTUALEDIT
8127 /* col(".") when the cursor is on the NUL at the end of the line
8128 * because of "coladd" can be seen as an extra column. */
8129 if (virtual_active() && fp == &curwin->w_cursor)
8130 {
8131 char_u *p = ml_get_cursor();
8132
8133 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8134 curwin->w_virtcol - curwin->w_cursor.coladd))
8135 {
8136# ifdef FEAT_MBYTE
8137 int l;
8138
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008139 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 col += l;
8141# else
8142 if (*p != NUL && p[1] == NUL)
8143 ++col;
8144# endif
8145 }
8146 }
8147#endif
8148 }
8149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008150 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151}
8152
Bram Moolenaar572cb562005-08-05 21:35:02 +00008153#if defined(FEAT_INS_EXPAND)
8154/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008155 * "complete()" function
8156 */
8157/*ARGSUSED*/
8158 static void
8159f_complete(argvars, rettv)
8160 typval_T *argvars;
8161 typval_T *rettv;
8162{
8163 int startcol;
8164
8165 if ((State & INSERT) == 0)
8166 {
8167 EMSG(_("E785: complete() can only be used in Insert mode"));
8168 return;
8169 }
8170 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8171 {
8172 EMSG(_(e_invarg));
8173 return;
8174 }
8175
8176 startcol = get_tv_number_chk(&argvars[0], NULL);
8177 if (startcol <= 0)
8178 return;
8179
8180 set_completion(startcol - 1, argvars[1].vval.v_list);
8181}
8182
8183/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008184 * "complete_add()" function
8185 */
8186/*ARGSUSED*/
8187 static void
8188f_complete_add(argvars, rettv)
8189 typval_T *argvars;
8190 typval_T *rettv;
8191{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008192 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008193}
8194
8195/*
8196 * "complete_check()" function
8197 */
8198/*ARGSUSED*/
8199 static void
8200f_complete_check(argvars, rettv)
8201 typval_T *argvars;
8202 typval_T *rettv;
8203{
8204 int saved = RedrawingDisabled;
8205
8206 RedrawingDisabled = 0;
8207 ins_compl_check_keys(0);
8208 rettv->vval.v_number = compl_interrupted;
8209 RedrawingDisabled = saved;
8210}
8211#endif
8212
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213/*
8214 * "confirm(message, buttons[, default [, type]])" function
8215 */
8216/*ARGSUSED*/
8217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008218f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008219 typval_T *argvars;
8220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221{
8222#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8223 char_u *message;
8224 char_u *buttons = NULL;
8225 char_u buf[NUMBUFLEN];
8226 char_u buf2[NUMBUFLEN];
8227 int def = 1;
8228 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008229 char_u *typestr;
8230 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008232 message = get_tv_string_chk(&argvars[0]);
8233 if (message == NULL)
8234 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008235 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008237 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8238 if (buttons == NULL)
8239 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008240 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008242 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008243 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008245 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8246 if (typestr == NULL)
8247 error = TRUE;
8248 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008250 switch (TOUPPER_ASC(*typestr))
8251 {
8252 case 'E': type = VIM_ERROR; break;
8253 case 'Q': type = VIM_QUESTION; break;
8254 case 'I': type = VIM_INFO; break;
8255 case 'W': type = VIM_WARNING; break;
8256 case 'G': type = VIM_GENERIC; break;
8257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 }
8259 }
8260 }
8261 }
8262
8263 if (buttons == NULL || *buttons == NUL)
8264 buttons = (char_u *)_("&Ok");
8265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008266 if (error)
8267 rettv->vval.v_number = 0;
8268 else
8269 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 def, NULL);
8271#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008272 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273#endif
8274}
8275
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008276/*
8277 * "copy()" function
8278 */
8279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008280f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008281 typval_T *argvars;
8282 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008283{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008284 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008285}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286
8287/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008288 * "count()" function
8289 */
8290 static void
8291f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008292 typval_T *argvars;
8293 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008294{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008295 long n = 0;
8296 int ic = FALSE;
8297
Bram Moolenaare9a41262005-01-15 22:18:47 +00008298 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008299 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008300 listitem_T *li;
8301 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008302 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008303
Bram Moolenaare9a41262005-01-15 22:18:47 +00008304 if ((l = argvars[0].vval.v_list) != NULL)
8305 {
8306 li = l->lv_first;
8307 if (argvars[2].v_type != VAR_UNKNOWN)
8308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008309 int error = FALSE;
8310
8311 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008312 if (argvars[3].v_type != VAR_UNKNOWN)
8313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008314 idx = get_tv_number_chk(&argvars[3], &error);
8315 if (!error)
8316 {
8317 li = list_find(l, idx);
8318 if (li == NULL)
8319 EMSGN(_(e_listidx), idx);
8320 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008321 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008322 if (error)
8323 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008324 }
8325
8326 for ( ; li != NULL; li = li->li_next)
8327 if (tv_equal(&li->li_tv, &argvars[1], ic))
8328 ++n;
8329 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008330 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008331 else if (argvars[0].v_type == VAR_DICT)
8332 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008333 int todo;
8334 dict_T *d;
8335 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008336
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008337 if ((d = argvars[0].vval.v_dict) != NULL)
8338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008339 int error = FALSE;
8340
Bram Moolenaare9a41262005-01-15 22:18:47 +00008341 if (argvars[2].v_type != VAR_UNKNOWN)
8342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008343 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008344 if (argvars[3].v_type != VAR_UNKNOWN)
8345 EMSG(_(e_invarg));
8346 }
8347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008348 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008350 {
8351 if (!HASHITEM_EMPTY(hi))
8352 {
8353 --todo;
8354 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8355 ++n;
8356 }
8357 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008358 }
8359 }
8360 else
8361 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008362 rettv->vval.v_number = n;
8363}
8364
8365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8367 *
8368 * Checks the existence of a cscope connection.
8369 */
8370/*ARGSUSED*/
8371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008372f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008373 typval_T *argvars;
8374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
8376#ifdef FEAT_CSCOPE
8377 int num = 0;
8378 char_u *dbpath = NULL;
8379 char_u *prepend = NULL;
8380 char_u buf[NUMBUFLEN];
8381
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008382 if (argvars[0].v_type != VAR_UNKNOWN
8383 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008385 num = (int)get_tv_number(&argvars[0]);
8386 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008387 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008388 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 }
8390
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008391 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008393 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394#endif
8395}
8396
8397/*
8398 * "cursor(lnum, col)" function
8399 *
8400 * Moves the cursor to the specified line and column
8401 */
8402/*ARGSUSED*/
8403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008404f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008405 typval_T *argvars;
8406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
8408 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008409#ifdef FEAT_VIRTUALEDIT
8410 long coladd = 0;
8411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412
Bram Moolenaara5525202006-03-02 22:52:09 +00008413 if (argvars[1].v_type == VAR_UNKNOWN)
8414 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008415 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008416
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008417 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008418 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008419 line = pos.lnum;
8420 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008421#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008422 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008423#endif
8424 }
8425 else
8426 {
8427 line = get_tv_lnum(argvars);
8428 col = get_tv_number_chk(&argvars[1], NULL);
8429#ifdef FEAT_VIRTUALEDIT
8430 if (argvars[2].v_type != VAR_UNKNOWN)
8431 coladd = get_tv_number_chk(&argvars[2], NULL);
8432#endif
8433 }
8434 if (line < 0 || col < 0
8435#ifdef FEAT_VIRTUALEDIT
8436 || coladd < 0
8437#endif
8438 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008439 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 if (line > 0)
8441 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 if (col > 0)
8443 curwin->w_cursor.col = col - 1;
8444#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008445 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446#endif
8447
8448 /* Make sure the cursor is in a valid position. */
8449 check_cursor();
8450#ifdef FEAT_MBYTE
8451 /* Correct cursor for multi-byte character. */
8452 if (has_mbyte)
8453 mb_adjust_cursor();
8454#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008455
8456 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457}
8458
8459/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008460 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 */
8462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008463f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008464 typval_T *argvars;
8465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008467 int noref = 0;
8468
8469 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008470 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008471 if (noref < 0 || noref > 1)
8472 EMSG(_(e_invarg));
8473 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008474 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475}
8476
8477/*
8478 * "delete()" function
8479 */
8480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008481f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008482 typval_T *argvars;
8483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
8485 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008486 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008488 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489}
8490
8491/*
8492 * "did_filetype()" function
8493 */
8494/*ARGSUSED*/
8495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008496f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008497 typval_T *argvars;
8498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499{
8500#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008501 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008503 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504#endif
8505}
8506
8507/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008508 * "diff_filler()" function
8509 */
8510/*ARGSUSED*/
8511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008512f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008513 typval_T *argvars;
8514 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008515{
8516#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008517 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008518#endif
8519}
8520
8521/*
8522 * "diff_hlID()" function
8523 */
8524/*ARGSUSED*/
8525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008526f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008527 typval_T *argvars;
8528 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008529{
8530#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008531 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008532 static linenr_T prev_lnum = 0;
8533 static int changedtick = 0;
8534 static int fnum = 0;
8535 static int change_start = 0;
8536 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008537 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008538 int filler_lines;
8539 int col;
8540
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008541 if (lnum < 0) /* ignore type error in {lnum} arg */
8542 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008543 if (lnum != prev_lnum
8544 || changedtick != curbuf->b_changedtick
8545 || fnum != curbuf->b_fnum)
8546 {
8547 /* New line, buffer, change: need to get the values. */
8548 filler_lines = diff_check(curwin, lnum);
8549 if (filler_lines < 0)
8550 {
8551 if (filler_lines == -1)
8552 {
8553 change_start = MAXCOL;
8554 change_end = -1;
8555 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8556 hlID = HLF_ADD; /* added line */
8557 else
8558 hlID = HLF_CHD; /* changed line */
8559 }
8560 else
8561 hlID = HLF_ADD; /* added line */
8562 }
8563 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008564 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008565 prev_lnum = lnum;
8566 changedtick = curbuf->b_changedtick;
8567 fnum = curbuf->b_fnum;
8568 }
8569
8570 if (hlID == HLF_CHD || hlID == HLF_TXD)
8571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008572 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008573 if (col >= change_start && col <= change_end)
8574 hlID = HLF_TXD; /* changed text */
8575 else
8576 hlID = HLF_CHD; /* changed line */
8577 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008578 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008579#endif
8580}
8581
8582/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008583 * "empty({expr})" function
8584 */
8585 static void
8586f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008587 typval_T *argvars;
8588 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008589{
8590 int n;
8591
8592 switch (argvars[0].v_type)
8593 {
8594 case VAR_STRING:
8595 case VAR_FUNC:
8596 n = argvars[0].vval.v_string == NULL
8597 || *argvars[0].vval.v_string == NUL;
8598 break;
8599 case VAR_NUMBER:
8600 n = argvars[0].vval.v_number == 0;
8601 break;
8602 case VAR_LIST:
8603 n = argvars[0].vval.v_list == NULL
8604 || argvars[0].vval.v_list->lv_first == NULL;
8605 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008606 case VAR_DICT:
8607 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008608 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008609 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008610 default:
8611 EMSG2(_(e_intern2), "f_empty()");
8612 n = 0;
8613 }
8614
8615 rettv->vval.v_number = n;
8616}
8617
8618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 * "escape({string}, {chars})" function
8620 */
8621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008623 typval_T *argvars;
8624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625{
8626 char_u buf[NUMBUFLEN];
8627
Bram Moolenaar758711c2005-02-02 23:11:38 +00008628 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8629 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008630 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631}
8632
8633/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008634 * "eval()" function
8635 */
8636/*ARGSUSED*/
8637 static void
8638f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008639 typval_T *argvars;
8640 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008641{
8642 char_u *s;
8643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008644 s = get_tv_string_chk(&argvars[0]);
8645 if (s != NULL)
8646 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008648 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8649 {
8650 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008651 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008652 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008653 else if (*s != NUL)
8654 EMSG(_(e_trailing));
8655}
8656
8657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 * "eventhandler()" function
8659 */
8660/*ARGSUSED*/
8661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008662f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008663 typval_T *argvars;
8664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008666 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667}
8668
8669/*
8670 * "executable()" function
8671 */
8672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008673f_executable(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008677 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678}
8679
8680/*
8681 * "exists()" function
8682 */
8683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008684f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008685 typval_T *argvars;
8686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687{
8688 char_u *p;
8689 char_u *name;
8690 int n = FALSE;
8691 int len = 0;
8692
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008693 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 if (*p == '$') /* environment variable */
8695 {
8696 /* first try "normal" environment variables (fast) */
8697 if (mch_getenv(p + 1) != NULL)
8698 n = TRUE;
8699 else
8700 {
8701 /* try expanding things like $VIM and ${HOME} */
8702 p = expand_env_save(p);
8703 if (p != NULL && *p != '$')
8704 n = TRUE;
8705 vim_free(p);
8706 }
8707 }
8708 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008709 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 else if (*p == '*') /* internal or user defined function */
8711 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008712 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 }
8714 else if (*p == ':')
8715 {
8716 n = cmd_exists(p + 1);
8717 }
8718 else if (*p == '#')
8719 {
8720#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008721 if (p[1] == '#')
8722 n = autocmd_supported(p + 2);
8723 else
8724 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725#endif
8726 }
8727 else /* internal variable */
8728 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008729 char_u *tofree;
8730 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008732 /* get_name_len() takes care of expanding curly braces */
8733 name = p;
8734 len = get_name_len(&p, &tofree, TRUE, FALSE);
8735 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008737 if (tofree != NULL)
8738 name = tofree;
8739 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8740 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008742 /* handle d.key, l[idx], f(expr) */
8743 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8744 if (n)
8745 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 }
8747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008749 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 }
8751
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008752 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753}
8754
8755/*
8756 * "expand()" function
8757 */
8758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008759f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008760 typval_T *argvars;
8761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762{
8763 char_u *s;
8764 int len;
8765 char_u *errormsg;
8766 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8767 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008768 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770 rettv->v_type = VAR_STRING;
8771 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 if (*s == '%' || *s == '#' || *s == '<')
8773 {
8774 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008775 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 --emsg_off;
8777 }
8778 else
8779 {
8780 /* When the optional second argument is non-zero, don't remove matches
8781 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008782 if (argvars[1].v_type != VAR_UNKNOWN
8783 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008785 if (!error)
8786 {
8787 ExpandInit(&xpc);
8788 xpc.xp_context = EXPAND_FILES;
8789 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8790 ExpandCleanup(&xpc);
8791 }
8792 else
8793 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 }
8795}
8796
8797/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008798 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008799 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008800 */
8801 static void
8802f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008803 typval_T *argvars;
8804 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008805{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008806 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008807 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008808 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008809 list_T *l1, *l2;
8810 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008811 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008812 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008813
Bram Moolenaare9a41262005-01-15 22:18:47 +00008814 l1 = argvars[0].vval.v_list;
8815 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008816 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8817 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008818 {
8819 if (argvars[2].v_type != VAR_UNKNOWN)
8820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008821 before = get_tv_number_chk(&argvars[2], &error);
8822 if (error)
8823 return; /* type error; errmsg already given */
8824
Bram Moolenaar758711c2005-02-02 23:11:38 +00008825 if (before == l1->lv_len)
8826 item = NULL;
8827 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008828 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008829 item = list_find(l1, before);
8830 if (item == NULL)
8831 {
8832 EMSGN(_(e_listidx), before);
8833 return;
8834 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008835 }
8836 }
8837 else
8838 item = NULL;
8839 list_extend(l1, l2, item);
8840
Bram Moolenaare9a41262005-01-15 22:18:47 +00008841 copy_tv(&argvars[0], rettv);
8842 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008843 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008844 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8845 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008846 dict_T *d1, *d2;
8847 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008848 char_u *action;
8849 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008851 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008852
8853 d1 = argvars[0].vval.v_dict;
8854 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008855 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8856 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008857 {
8858 /* Check the third argument. */
8859 if (argvars[2].v_type != VAR_UNKNOWN)
8860 {
8861 static char *(av[]) = {"keep", "force", "error"};
8862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008863 action = get_tv_string_chk(&argvars[2]);
8864 if (action == NULL)
8865 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008866 for (i = 0; i < 3; ++i)
8867 if (STRCMP(action, av[i]) == 0)
8868 break;
8869 if (i == 3)
8870 {
8871 EMSGN(_(e_invarg2), action);
8872 return;
8873 }
8874 }
8875 else
8876 action = (char_u *)"force";
8877
8878 /* Go over all entries in the second dict and add them to the
8879 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008880 todo = d2->dv_hashtab.ht_used;
8881 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008882 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008883 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008884 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008885 --todo;
8886 di1 = dict_find(d1, hi2->hi_key, -1);
8887 if (di1 == NULL)
8888 {
8889 di1 = dictitem_copy(HI2DI(hi2));
8890 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8891 dictitem_free(di1);
8892 }
8893 else if (*action == 'e')
8894 {
8895 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8896 break;
8897 }
8898 else if (*action == 'f')
8899 {
8900 clear_tv(&di1->di_tv);
8901 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8902 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008903 }
8904 }
8905
Bram Moolenaare9a41262005-01-15 22:18:47 +00008906 copy_tv(&argvars[0], rettv);
8907 }
8908 }
8909 else
8910 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008911}
8912
8913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 * "filereadable()" function
8915 */
8916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008917f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008918 typval_T *argvars;
8919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920{
8921 FILE *fd;
8922 char_u *p;
8923 int n;
8924
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008925 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8927 {
8928 n = TRUE;
8929 fclose(fd);
8930 }
8931 else
8932 n = FALSE;
8933
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935}
8936
8937/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008938 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 * rights to write into.
8940 */
8941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008942f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008943 typval_T *argvars;
8944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008946 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947}
8948
Bram Moolenaar33570922005-01-25 22:26:29 +00008949static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008950
8951 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008952findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008953 typval_T *argvars;
8954 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008955 int dir;
8956{
8957#ifdef FEAT_SEARCHPATH
8958 char_u *fname;
8959 char_u *fresult = NULL;
8960 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8961 char_u *p;
8962 char_u pathbuf[NUMBUFLEN];
8963 int count = 1;
8964 int first = TRUE;
8965
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008967
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008968 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008970 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8971 if (p == NULL)
8972 count = -1; /* error */
8973 else
8974 {
8975 if (*p != NUL)
8976 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008978 if (argvars[2].v_type != VAR_UNKNOWN)
8979 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8980 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008981 }
8982
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008983 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008984 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 do
8986 {
8987 vim_free(fresult);
8988 fresult = find_file_in_path_option(first ? fname : NULL,
8989 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008990 0, first, path, dir, NULL,
8991 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008992 first = FALSE;
8993 } while (--count > 0 && fresult != NULL);
8994 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008995
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008997#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008999#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009001}
9002
Bram Moolenaar33570922005-01-25 22:26:29 +00009003static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9004static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009005
9006/*
9007 * Implementation of map() and filter().
9008 */
9009 static void
9010filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009011 typval_T *argvars;
9012 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009013 int map;
9014{
9015 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009016 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009017 listitem_T *li, *nli;
9018 list_T *l = NULL;
9019 dictitem_T *di;
9020 hashtab_T *ht;
9021 hashitem_T *hi;
9022 dict_T *d = NULL;
9023 typval_T save_val;
9024 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009025 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009026 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009027 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009028 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009029
9030 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009031 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009032 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009033 if ((l = argvars[0].vval.v_list) == NULL
9034 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009035 return;
9036 }
9037 else if (argvars[0].v_type == VAR_DICT)
9038 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009039 if ((d = argvars[0].vval.v_dict) == NULL
9040 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009041 return;
9042 }
9043 else
9044 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009045 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009046 return;
9047 }
9048
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009049 expr = get_tv_string_buf_chk(&argvars[1], buf);
9050 /* On type errors, the preceding call has already displayed an error
9051 * message. Avoid a misleading error message for an empty string that
9052 * was not passed as argument. */
9053 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009055 prepare_vimvar(VV_VAL, &save_val);
9056 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009057
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009058 /* We reset "did_emsg" to be able to detect whether an error
9059 * occurred during evaluation of the expression. */
9060 save_did_emsg = did_emsg;
9061 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009063 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009064 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009065 prepare_vimvar(VV_KEY, &save_key);
9066 vimvars[VV_KEY].vv_type = VAR_STRING;
9067
9068 ht = &d->dv_hashtab;
9069 hash_lock(ht);
9070 todo = ht->ht_used;
9071 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009073 if (!HASHITEM_EMPTY(hi))
9074 {
9075 --todo;
9076 di = HI2DI(hi);
9077 if (tv_check_lock(di->di_tv.v_lock, msg))
9078 break;
9079 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009080 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009081 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009082 break;
9083 if (!map && rem)
9084 dictitem_remove(d, di);
9085 clear_tv(&vimvars[VV_KEY].vv_tv);
9086 }
9087 }
9088 hash_unlock(ht);
9089
9090 restore_vimvar(VV_KEY, &save_key);
9091 }
9092 else
9093 {
9094 for (li = l->lv_first; li != NULL; li = nli)
9095 {
9096 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009097 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009098 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009099 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009100 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009101 break;
9102 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009103 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009104 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009105 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009107 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009108
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009109 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009110 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009111
9112 copy_tv(&argvars[0], rettv);
9113}
9114
9115 static int
9116filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009117 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009118 char_u *expr;
9119 int map;
9120 int *remp;
9121{
Bram Moolenaar33570922005-01-25 22:26:29 +00009122 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009123 char_u *s;
9124
Bram Moolenaar33570922005-01-25 22:26:29 +00009125 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009126 s = expr;
9127 if (eval1(&s, &rettv, TRUE) == FAIL)
9128 return FAIL;
9129 if (*s != NUL) /* check for trailing chars after expr */
9130 {
9131 EMSG2(_(e_invexpr2), s);
9132 return FAIL;
9133 }
9134 if (map)
9135 {
9136 /* map(): replace the list item value */
9137 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009138 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009139 *tv = rettv;
9140 }
9141 else
9142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009143 int error = FALSE;
9144
Bram Moolenaare9a41262005-01-15 22:18:47 +00009145 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009146 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009147 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009148 /* On type error, nothing has been removed; return FAIL to stop the
9149 * loop. The error message was given by get_tv_number_chk(). */
9150 if (error)
9151 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009152 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009153 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009154 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009155}
9156
9157/*
9158 * "filter()" function
9159 */
9160 static void
9161f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009162 typval_T *argvars;
9163 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009164{
9165 filter_map(argvars, rettv, FALSE);
9166}
9167
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009168/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009169 * "finddir({fname}[, {path}[, {count}]])" function
9170 */
9171 static void
9172f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009173 typval_T *argvars;
9174 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009175{
9176 findfilendir(argvars, rettv, TRUE);
9177}
9178
9179/*
9180 * "findfile({fname}[, {path}[, {count}]])" function
9181 */
9182 static void
9183f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009184 typval_T *argvars;
9185 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009186{
9187 findfilendir(argvars, rettv, FALSE);
9188}
9189
9190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 * "fnamemodify({fname}, {mods})" function
9192 */
9193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009194f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009195 typval_T *argvars;
9196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197{
9198 char_u *fname;
9199 char_u *mods;
9200 int usedlen = 0;
9201 int len;
9202 char_u *fbuf = NULL;
9203 char_u buf[NUMBUFLEN];
9204
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009205 fname = get_tv_string_chk(&argvars[0]);
9206 mods = get_tv_string_buf_chk(&argvars[1], buf);
9207 if (fname == NULL || mods == NULL)
9208 fname = NULL;
9209 else
9210 {
9211 len = (int)STRLEN(fname);
9212 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009215 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009217 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 vim_free(fbuf);
9221}
9222
Bram Moolenaar33570922005-01-25 22:26:29 +00009223static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224
9225/*
9226 * "foldclosed()" function
9227 */
9228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009230 typval_T *argvars;
9231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 int end;
9233{
9234#ifdef FEAT_FOLDING
9235 linenr_T lnum;
9236 linenr_T first, last;
9237
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009238 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9240 {
9241 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9242 {
9243 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009244 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009246 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 return;
9248 }
9249 }
9250#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009251 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252}
9253
9254/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009255 * "foldclosed()" function
9256 */
9257 static void
9258f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009259 typval_T *argvars;
9260 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009261{
9262 foldclosed_both(argvars, rettv, FALSE);
9263}
9264
9265/*
9266 * "foldclosedend()" function
9267 */
9268 static void
9269f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009270 typval_T *argvars;
9271 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009272{
9273 foldclosed_both(argvars, rettv, TRUE);
9274}
9275
9276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 * "foldlevel()" function
9278 */
9279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009280f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009281 typval_T *argvars;
9282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283{
9284#ifdef FEAT_FOLDING
9285 linenr_T lnum;
9286
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009287 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009289 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 else
9291#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009292 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293}
9294
9295/*
9296 * "foldtext()" function
9297 */
9298/*ARGSUSED*/
9299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009300f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009301 typval_T *argvars;
9302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
9304#ifdef FEAT_FOLDING
9305 linenr_T lnum;
9306 char_u *s;
9307 char_u *r;
9308 int len;
9309 char *txt;
9310#endif
9311
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009312 rettv->v_type = VAR_STRING;
9313 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009315 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9316 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9317 <= curbuf->b_ml.ml_line_count
9318 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 {
9320 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009321 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9322 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 {
9324 if (!linewhite(lnum))
9325 break;
9326 ++lnum;
9327 }
9328
9329 /* Find interesting text in this line. */
9330 s = skipwhite(ml_get(lnum));
9331 /* skip C comment-start */
9332 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009333 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009335 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009336 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009337 {
9338 s = skipwhite(ml_get(lnum + 1));
9339 if (*s == '*')
9340 s = skipwhite(s + 1);
9341 }
9342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 txt = _("+-%s%3ld lines: ");
9344 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009345 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 + 20 /* for %3ld */
9347 + STRLEN(s))); /* concatenated */
9348 if (r != NULL)
9349 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009350 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9351 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9352 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 len = (int)STRLEN(r);
9354 STRCAT(r, s);
9355 /* remove 'foldmarker' and 'commentstring' */
9356 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009357 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 }
9359 }
9360#endif
9361}
9362
9363/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009364 * "foldtextresult(lnum)" function
9365 */
9366/*ARGSUSED*/
9367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009369 typval_T *argvars;
9370 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009371{
9372#ifdef FEAT_FOLDING
9373 linenr_T lnum;
9374 char_u *text;
9375 char_u buf[51];
9376 foldinfo_T foldinfo;
9377 int fold_count;
9378#endif
9379
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380 rettv->v_type = VAR_STRING;
9381 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009382#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009383 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009384 /* treat illegal types and illegal string values for {lnum} the same */
9385 if (lnum < 0)
9386 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009387 fold_count = foldedCount(curwin, lnum, &foldinfo);
9388 if (fold_count > 0)
9389 {
9390 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9391 &foldinfo, buf);
9392 if (text == buf)
9393 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009395 }
9396#endif
9397}
9398
9399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 * "foreground()" function
9401 */
9402/*ARGSUSED*/
9403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009405 typval_T *argvars;
9406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409#ifdef FEAT_GUI
9410 if (gui.in_use)
9411 gui_mch_set_foreground();
9412#else
9413# ifdef WIN32
9414 win32_set_foreground();
9415# endif
9416#endif
9417}
9418
9419/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009420 * "function()" function
9421 */
9422/*ARGSUSED*/
9423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009424f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009425 typval_T *argvars;
9426 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009427{
9428 char_u *s;
9429
Bram Moolenaara7043832005-01-21 11:56:39 +00009430 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009431 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009432 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009433 EMSG2(_(e_invarg2), s);
9434 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009435 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009436 else
9437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_string = vim_strsave(s);
9439 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009440 }
9441}
9442
9443/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009444 * "garbagecollect()" function
9445 */
9446/*ARGSUSED*/
9447 static void
9448f_garbagecollect(argvars, rettv)
9449 typval_T *argvars;
9450 typval_T *rettv;
9451{
9452 garbage_collect();
9453}
9454
9455/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009456 * "get()" function
9457 */
9458 static void
9459f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009460 typval_T *argvars;
9461 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009462{
Bram Moolenaar33570922005-01-25 22:26:29 +00009463 listitem_T *li;
9464 list_T *l;
9465 dictitem_T *di;
9466 dict_T *d;
9467 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009468
Bram Moolenaare9a41262005-01-15 22:18:47 +00009469 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009470 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009471 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009473 int error = FALSE;
9474
9475 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9476 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009477 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009478 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009479 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009480 else if (argvars[0].v_type == VAR_DICT)
9481 {
9482 if ((d = argvars[0].vval.v_dict) != NULL)
9483 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009484 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009485 if (di != NULL)
9486 tv = &di->di_tv;
9487 }
9488 }
9489 else
9490 EMSG2(_(e_listdictarg), "get()");
9491
9492 if (tv == NULL)
9493 {
9494 if (argvars[2].v_type == VAR_UNKNOWN)
9495 rettv->vval.v_number = 0;
9496 else
9497 copy_tv(&argvars[2], rettv);
9498 }
9499 else
9500 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009501}
9502
Bram Moolenaar342337a2005-07-21 21:11:17 +00009503static 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 +00009504
9505/*
9506 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009507 * Return a range (from start to end) of lines in rettv from the specified
9508 * buffer.
9509 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009510 */
9511 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009512get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009513 buf_T *buf;
9514 linenr_T start;
9515 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009516 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009517 typval_T *rettv;
9518{
9519 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009520
Bram Moolenaar342337a2005-07-21 21:11:17 +00009521 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009522 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009523 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009524 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009525 }
9526 else
9527 rettv->vval.v_number = 0;
9528
9529 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9530 return;
9531
9532 if (!retlist)
9533 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009534 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9535 p = ml_get_buf(buf, start, FALSE);
9536 else
9537 p = (char_u *)"";
9538
9539 rettv->v_type = VAR_STRING;
9540 rettv->vval.v_string = vim_strsave(p);
9541 }
9542 else
9543 {
9544 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009545 return;
9546
9547 if (start < 1)
9548 start = 1;
9549 if (end > buf->b_ml.ml_line_count)
9550 end = buf->b_ml.ml_line_count;
9551 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009552 if (list_append_string(rettv->vval.v_list,
9553 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009554 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009555 }
9556}
9557
9558/*
9559 * "getbufline()" function
9560 */
9561 static void
9562f_getbufline(argvars, rettv)
9563 typval_T *argvars;
9564 typval_T *rettv;
9565{
9566 linenr_T lnum;
9567 linenr_T end;
9568 buf_T *buf;
9569
9570 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9571 ++emsg_off;
9572 buf = get_buf_tv(&argvars[0]);
9573 --emsg_off;
9574
Bram Moolenaar661b1822005-07-28 22:36:45 +00009575 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009576 if (argvars[2].v_type == VAR_UNKNOWN)
9577 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009578 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009579 end = get_tv_lnum_buf(&argvars[2], buf);
9580
Bram Moolenaar342337a2005-07-21 21:11:17 +00009581 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009582}
9583
Bram Moolenaar0d660222005-01-07 21:51:51 +00009584/*
9585 * "getbufvar()" function
9586 */
9587 static void
9588f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009589 typval_T *argvars;
9590 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009591{
9592 buf_T *buf;
9593 buf_T *save_curbuf;
9594 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009595 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009596
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009597 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9598 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009599 ++emsg_off;
9600 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009601
9602 rettv->v_type = VAR_STRING;
9603 rettv->vval.v_string = NULL;
9604
9605 if (buf != NULL && varname != NULL)
9606 {
9607 if (*varname == '&') /* buffer-local-option */
9608 {
9609 /* set curbuf to be our buf, temporarily */
9610 save_curbuf = curbuf;
9611 curbuf = buf;
9612
9613 get_option_tv(&varname, rettv, TRUE);
9614
9615 /* restore previous notion of curbuf */
9616 curbuf = save_curbuf;
9617 }
9618 else
9619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009620 if (*varname == NUL)
9621 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9622 * scope prefix before the NUL byte is required by
9623 * find_var_in_ht(). */
9624 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009625 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009626 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009627 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009628 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009629 }
9630 }
9631
9632 --emsg_off;
9633}
9634
9635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 * "getchar()" function
9637 */
9638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009640 typval_T *argvars;
9641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642{
9643 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009644 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645
9646 ++no_mapping;
9647 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009648 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 /* getchar(): blocking wait. */
9650 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009651 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 /* getchar(1): only check if char avail */
9653 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009654 else if (error || vpeekc() == NUL)
9655 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 n = 0;
9657 else
9658 /* getchar(0) and char avail: return char */
9659 n = safe_vgetc();
9660 --no_mapping;
9661 --allow_keys;
9662
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009663 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 if (IS_SPECIAL(n) || mod_mask != 0)
9665 {
9666 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9667 int i = 0;
9668
9669 /* Turn a special key into three bytes, plus modifier. */
9670 if (mod_mask != 0)
9671 {
9672 temp[i++] = K_SPECIAL;
9673 temp[i++] = KS_MODIFIER;
9674 temp[i++] = mod_mask;
9675 }
9676 if (IS_SPECIAL(n))
9677 {
9678 temp[i++] = K_SPECIAL;
9679 temp[i++] = K_SECOND(n);
9680 temp[i++] = K_THIRD(n);
9681 }
9682#ifdef FEAT_MBYTE
9683 else if (has_mbyte)
9684 i += (*mb_char2bytes)(n, temp + i);
9685#endif
9686 else
9687 temp[i++] = n;
9688 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689 rettv->v_type = VAR_STRING;
9690 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 }
9692}
9693
9694/*
9695 * "getcharmod()" function
9696 */
9697/*ARGSUSED*/
9698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009700 typval_T *argvars;
9701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704}
9705
9706/*
9707 * "getcmdline()" function
9708 */
9709/*ARGSUSED*/
9710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009712 typval_T *argvars;
9713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009715 rettv->v_type = VAR_STRING;
9716 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717}
9718
9719/*
9720 * "getcmdpos()" function
9721 */
9722/*ARGSUSED*/
9723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009725 typval_T *argvars;
9726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009728 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729}
9730
9731/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009732 * "getcmdtype()" function
9733 */
9734/*ARGSUSED*/
9735 static void
9736f_getcmdtype(argvars, rettv)
9737 typval_T *argvars;
9738 typval_T *rettv;
9739{
9740 rettv->v_type = VAR_STRING;
9741 rettv->vval.v_string = alloc(2);
9742 if (rettv->vval.v_string != NULL)
9743 {
9744 rettv->vval.v_string[0] = get_cmdline_type();
9745 rettv->vval.v_string[1] = NUL;
9746 }
9747}
9748
9749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 * "getcwd()" function
9751 */
9752/*ARGSUSED*/
9753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009755 typval_T *argvars;
9756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757{
9758 char_u cwd[MAXPATHL];
9759
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 else
9764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009767 if (rettv->vval.v_string != NULL)
9768 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769#endif
9770 }
9771}
9772
9773/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009774 * "getfontname()" function
9775 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009776/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009778f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009779 typval_T *argvars;
9780 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009781{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782 rettv->v_type = VAR_STRING;
9783 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009784#ifdef FEAT_GUI
9785 if (gui.in_use)
9786 {
9787 GuiFont font;
9788 char_u *name = NULL;
9789
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009790 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009791 {
9792 /* Get the "Normal" font. Either the name saved by
9793 * hl_set_font_name() or from the font ID. */
9794 font = gui.norm_font;
9795 name = hl_get_font_name();
9796 }
9797 else
9798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009799 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009800 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9801 return;
9802 font = gui_mch_get_font(name, FALSE);
9803 if (font == NOFONT)
9804 return; /* Invalid font name, return empty string. */
9805 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009806 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009807 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009808 gui_mch_free_font(font);
9809 }
9810#endif
9811}
9812
9813/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009814 * "getfperm({fname})" function
9815 */
9816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009818 typval_T *argvars;
9819 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009820{
9821 char_u *fname;
9822 struct stat st;
9823 char_u *perm = NULL;
9824 char_u flags[] = "rwx";
9825 int i;
9826
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009828
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009830 if (mch_stat((char *)fname, &st) >= 0)
9831 {
9832 perm = vim_strsave((char_u *)"---------");
9833 if (perm != NULL)
9834 {
9835 for (i = 0; i < 9; i++)
9836 {
9837 if (st.st_mode & (1 << (8 - i)))
9838 perm[i] = flags[i % 3];
9839 }
9840 }
9841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009843}
9844
9845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 * "getfsize({fname})" function
9847 */
9848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009850 typval_T *argvars;
9851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852{
9853 char_u *fname;
9854 struct stat st;
9855
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859
9860 if (mch_stat((char *)fname, &st) >= 0)
9861 {
9862 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009865 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 }
9867 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869}
9870
9871/*
9872 * "getftime({fname})" function
9873 */
9874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009875f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009876 typval_T *argvars;
9877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878{
9879 char_u *fname;
9880 struct stat st;
9881
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009882 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883
9884 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009885 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888}
9889
9890/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009891 * "getftype({fname})" function
9892 */
9893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009895 typval_T *argvars;
9896 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009897{
9898 char_u *fname;
9899 struct stat st;
9900 char_u *type = NULL;
9901 char *t;
9902
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009903 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009904
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009905 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009906 if (mch_lstat((char *)fname, &st) >= 0)
9907 {
9908#ifdef S_ISREG
9909 if (S_ISREG(st.st_mode))
9910 t = "file";
9911 else if (S_ISDIR(st.st_mode))
9912 t = "dir";
9913# ifdef S_ISLNK
9914 else if (S_ISLNK(st.st_mode))
9915 t = "link";
9916# endif
9917# ifdef S_ISBLK
9918 else if (S_ISBLK(st.st_mode))
9919 t = "bdev";
9920# endif
9921# ifdef S_ISCHR
9922 else if (S_ISCHR(st.st_mode))
9923 t = "cdev";
9924# endif
9925# ifdef S_ISFIFO
9926 else if (S_ISFIFO(st.st_mode))
9927 t = "fifo";
9928# endif
9929# ifdef S_ISSOCK
9930 else if (S_ISSOCK(st.st_mode))
9931 t = "fifo";
9932# endif
9933 else
9934 t = "other";
9935#else
9936# ifdef S_IFMT
9937 switch (st.st_mode & S_IFMT)
9938 {
9939 case S_IFREG: t = "file"; break;
9940 case S_IFDIR: t = "dir"; break;
9941# ifdef S_IFLNK
9942 case S_IFLNK: t = "link"; break;
9943# endif
9944# ifdef S_IFBLK
9945 case S_IFBLK: t = "bdev"; break;
9946# endif
9947# ifdef S_IFCHR
9948 case S_IFCHR: t = "cdev"; break;
9949# endif
9950# ifdef S_IFIFO
9951 case S_IFIFO: t = "fifo"; break;
9952# endif
9953# ifdef S_IFSOCK
9954 case S_IFSOCK: t = "socket"; break;
9955# endif
9956 default: t = "other";
9957 }
9958# else
9959 if (mch_isdir(fname))
9960 t = "dir";
9961 else
9962 t = "file";
9963# endif
9964#endif
9965 type = vim_strsave((char_u *)t);
9966 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009968}
9969
9970/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009971 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009972 */
9973 static void
9974f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009975 typval_T *argvars;
9976 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009977{
9978 linenr_T lnum;
9979 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009980 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009981
9982 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009983 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009984 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009985 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009986 retlist = FALSE;
9987 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009988 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009989 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009990 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009991 retlist = TRUE;
9992 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009993
Bram Moolenaar342337a2005-07-21 21:11:17 +00009994 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009995}
9996
9997/*
Bram Moolenaara5525202006-03-02 22:52:09 +00009998 * "getpos(string)" function
9999 */
10000 static void
10001f_getpos(argvars, rettv)
10002 typval_T *argvars;
10003 typval_T *rettv;
10004{
10005 pos_T *fp;
10006 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010007 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010008
10009 if (rettv_list_alloc(rettv) == OK)
10010 {
10011 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010012 fp = var2fpos(&argvars[0], TRUE, &fnum);
10013 if (fnum != -1)
10014 list_append_number(l, (varnumber_T)fnum);
10015 else
10016 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010017 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10018 : (varnumber_T)0);
10019 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10020 : (varnumber_T)0);
10021 list_append_number(l,
10022#ifdef FEAT_VIRTUALEDIT
10023 (fp != NULL) ? (varnumber_T)fp->coladd :
10024#endif
10025 (varnumber_T)0);
10026 }
10027 else
10028 rettv->vval.v_number = FALSE;
10029}
10030
10031/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010032 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010033 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010034/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010035 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010036f_getqflist(argvars, rettv)
10037 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010038 typval_T *rettv;
10039{
10040#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010041 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010042#endif
10043
10044 rettv->vval.v_number = FALSE;
10045#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010046 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010047 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010048 wp = NULL;
10049 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10050 {
10051 wp = find_win_by_nr(&argvars[0]);
10052 if (wp == NULL)
10053 return;
10054 }
10055
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010056 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010057 }
10058#endif
10059}
10060
10061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 * "getreg()" function
10063 */
10064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010065f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010066 typval_T *argvars;
10067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068{
10069 char_u *strregname;
10070 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010071 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010072 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010074 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010076 strregname = get_tv_string_chk(&argvars[0]);
10077 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010078 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010079 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010082 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 regname = (strregname == NULL ? '"' : *strregname);
10084 if (regname == 0)
10085 regname = '"';
10086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010088 rettv->vval.v_string = error ? NULL :
10089 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090}
10091
10092/*
10093 * "getregtype()" function
10094 */
10095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010096f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010097 typval_T *argvars;
10098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099{
10100 char_u *strregname;
10101 int regname;
10102 char_u buf[NUMBUFLEN + 2];
10103 long reglen = 0;
10104
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010105 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010106 {
10107 strregname = get_tv_string_chk(&argvars[0]);
10108 if (strregname == NULL) /* type error; errmsg already given */
10109 {
10110 rettv->v_type = VAR_STRING;
10111 rettv->vval.v_string = NULL;
10112 return;
10113 }
10114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 else
10116 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010117 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118
10119 regname = (strregname == NULL ? '"' : *strregname);
10120 if (regname == 0)
10121 regname = '"';
10122
10123 buf[0] = NUL;
10124 buf[1] = NUL;
10125 switch (get_reg_type(regname, &reglen))
10126 {
10127 case MLINE: buf[0] = 'V'; break;
10128 case MCHAR: buf[0] = 'v'; break;
10129#ifdef FEAT_VISUAL
10130 case MBLOCK:
10131 buf[0] = Ctrl_V;
10132 sprintf((char *)buf + 1, "%ld", reglen + 1);
10133 break;
10134#endif
10135 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010136 rettv->v_type = VAR_STRING;
10137 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138}
10139
10140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 * "getwinposx()" function
10142 */
10143/*ARGSUSED*/
10144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010146 typval_T *argvars;
10147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010149 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150#ifdef FEAT_GUI
10151 if (gui.in_use)
10152 {
10153 int x, y;
10154
10155 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010156 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 }
10158#endif
10159}
10160
10161/*
10162 * "getwinposy()" function
10163 */
10164/*ARGSUSED*/
10165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010166f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010167 typval_T *argvars;
10168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010170 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171#ifdef FEAT_GUI
10172 if (gui.in_use)
10173 {
10174 int x, y;
10175
10176 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010177 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 }
10179#endif
10180}
10181
Bram Moolenaara40058a2005-07-11 22:42:07 +000010182 static win_T *
10183find_win_by_nr(vp)
10184 typval_T *vp;
10185{
10186#ifdef FEAT_WINDOWS
10187 win_T *wp;
10188#endif
10189 int nr;
10190
10191 nr = get_tv_number_chk(vp, NULL);
10192
10193#ifdef FEAT_WINDOWS
10194 if (nr < 0)
10195 return NULL;
10196 if (nr == 0)
10197 return curwin;
10198
10199 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10200 if (--nr <= 0)
10201 break;
10202 return wp;
10203#else
10204 if (nr == 0 || nr == 1)
10205 return curwin;
10206 return NULL;
10207#endif
10208}
10209
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210/*
10211 * "getwinvar()" function
10212 */
10213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010214f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010215 typval_T *argvars;
10216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217{
10218 win_T *win, *oldcurwin;
10219 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010220 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 varname = get_tv_string_chk(&argvars[1]);
10224 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010226 rettv->v_type = VAR_STRING;
10227 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228
10229 if (win != NULL && varname != NULL)
10230 {
10231 if (*varname == '&') /* window-local-option */
10232 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010233 /* Set curwin to be our win, temporarily. Also set curbuf, so
10234 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 oldcurwin = curwin;
10236 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010237 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010239 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240
10241 /* restore previous notion of curwin */
10242 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010243 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 }
10245 else
10246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010247 if (*varname == NUL)
10248 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10249 * scope prefix before the NUL byte is required by
10250 * find_var_in_ht(). */
10251 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010253 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010255 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 }
10257 }
10258
10259 --emsg_off;
10260}
10261
10262/*
10263 * "glob()" function
10264 */
10265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010266f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010267 typval_T *argvars;
10268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269{
10270 expand_T xpc;
10271
10272 ExpandInit(&xpc);
10273 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010274 rettv->v_type = VAR_STRING;
10275 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10277 ExpandCleanup(&xpc);
10278}
10279
10280/*
10281 * "globpath()" function
10282 */
10283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010284f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010285 typval_T *argvars;
10286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287{
10288 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010289 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010291 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010292 if (file == NULL)
10293 rettv->vval.v_string = NULL;
10294 else
10295 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296}
10297
10298/*
10299 * "has()" function
10300 */
10301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010303 typval_T *argvars;
10304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305{
10306 int i;
10307 char_u *name;
10308 int n = FALSE;
10309 static char *(has_list[]) =
10310 {
10311#ifdef AMIGA
10312 "amiga",
10313# ifdef FEAT_ARP
10314 "arp",
10315# endif
10316#endif
10317#ifdef __BEOS__
10318 "beos",
10319#endif
10320#ifdef MSDOS
10321# ifdef DJGPP
10322 "dos32",
10323# else
10324 "dos16",
10325# endif
10326#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010327#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 "mac",
10329#endif
10330#if defined(MACOS_X_UNIX)
10331 "macunix",
10332#endif
10333#ifdef OS2
10334 "os2",
10335#endif
10336#ifdef __QNX__
10337 "qnx",
10338#endif
10339#ifdef RISCOS
10340 "riscos",
10341#endif
10342#ifdef UNIX
10343 "unix",
10344#endif
10345#ifdef VMS
10346 "vms",
10347#endif
10348#ifdef WIN16
10349 "win16",
10350#endif
10351#ifdef WIN32
10352 "win32",
10353#endif
10354#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10355 "win32unix",
10356#endif
10357#ifdef WIN64
10358 "win64",
10359#endif
10360#ifdef EBCDIC
10361 "ebcdic",
10362#endif
10363#ifndef CASE_INSENSITIVE_FILENAME
10364 "fname_case",
10365#endif
10366#ifdef FEAT_ARABIC
10367 "arabic",
10368#endif
10369#ifdef FEAT_AUTOCMD
10370 "autocmd",
10371#endif
10372#ifdef FEAT_BEVAL
10373 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010374# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10375 "balloon_multiline",
10376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377#endif
10378#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10379 "builtin_terms",
10380# ifdef ALL_BUILTIN_TCAPS
10381 "all_builtin_terms",
10382# endif
10383#endif
10384#ifdef FEAT_BYTEOFF
10385 "byte_offset",
10386#endif
10387#ifdef FEAT_CINDENT
10388 "cindent",
10389#endif
10390#ifdef FEAT_CLIENTSERVER
10391 "clientserver",
10392#endif
10393#ifdef FEAT_CLIPBOARD
10394 "clipboard",
10395#endif
10396#ifdef FEAT_CMDL_COMPL
10397 "cmdline_compl",
10398#endif
10399#ifdef FEAT_CMDHIST
10400 "cmdline_hist",
10401#endif
10402#ifdef FEAT_COMMENTS
10403 "comments",
10404#endif
10405#ifdef FEAT_CRYPT
10406 "cryptv",
10407#endif
10408#ifdef FEAT_CSCOPE
10409 "cscope",
10410#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010411#ifdef CURSOR_SHAPE
10412 "cursorshape",
10413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414#ifdef DEBUG
10415 "debug",
10416#endif
10417#ifdef FEAT_CON_DIALOG
10418 "dialog_con",
10419#endif
10420#ifdef FEAT_GUI_DIALOG
10421 "dialog_gui",
10422#endif
10423#ifdef FEAT_DIFF
10424 "diff",
10425#endif
10426#ifdef FEAT_DIGRAPHS
10427 "digraphs",
10428#endif
10429#ifdef FEAT_DND
10430 "dnd",
10431#endif
10432#ifdef FEAT_EMACS_TAGS
10433 "emacs_tags",
10434#endif
10435 "eval", /* always present, of course! */
10436#ifdef FEAT_EX_EXTRA
10437 "ex_extra",
10438#endif
10439#ifdef FEAT_SEARCH_EXTRA
10440 "extra_search",
10441#endif
10442#ifdef FEAT_FKMAP
10443 "farsi",
10444#endif
10445#ifdef FEAT_SEARCHPATH
10446 "file_in_path",
10447#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010448#if defined(UNIX) && !defined(USE_SYSTEM)
10449 "filterpipe",
10450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451#ifdef FEAT_FIND_ID
10452 "find_in_path",
10453#endif
10454#ifdef FEAT_FOLDING
10455 "folding",
10456#endif
10457#ifdef FEAT_FOOTER
10458 "footer",
10459#endif
10460#if !defined(USE_SYSTEM) && defined(UNIX)
10461 "fork",
10462#endif
10463#ifdef FEAT_GETTEXT
10464 "gettext",
10465#endif
10466#ifdef FEAT_GUI
10467 "gui",
10468#endif
10469#ifdef FEAT_GUI_ATHENA
10470# ifdef FEAT_GUI_NEXTAW
10471 "gui_neXtaw",
10472# else
10473 "gui_athena",
10474# endif
10475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476#ifdef FEAT_GUI_GTK
10477 "gui_gtk",
10478# ifdef HAVE_GTK2
10479 "gui_gtk2",
10480# endif
10481#endif
10482#ifdef FEAT_GUI_MAC
10483 "gui_mac",
10484#endif
10485#ifdef FEAT_GUI_MOTIF
10486 "gui_motif",
10487#endif
10488#ifdef FEAT_GUI_PHOTON
10489 "gui_photon",
10490#endif
10491#ifdef FEAT_GUI_W16
10492 "gui_win16",
10493#endif
10494#ifdef FEAT_GUI_W32
10495 "gui_win32",
10496#endif
10497#ifdef FEAT_HANGULIN
10498 "hangul_input",
10499#endif
10500#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10501 "iconv",
10502#endif
10503#ifdef FEAT_INS_EXPAND
10504 "insert_expand",
10505#endif
10506#ifdef FEAT_JUMPLIST
10507 "jumplist",
10508#endif
10509#ifdef FEAT_KEYMAP
10510 "keymap",
10511#endif
10512#ifdef FEAT_LANGMAP
10513 "langmap",
10514#endif
10515#ifdef FEAT_LIBCALL
10516 "libcall",
10517#endif
10518#ifdef FEAT_LINEBREAK
10519 "linebreak",
10520#endif
10521#ifdef FEAT_LISP
10522 "lispindent",
10523#endif
10524#ifdef FEAT_LISTCMDS
10525 "listcmds",
10526#endif
10527#ifdef FEAT_LOCALMAP
10528 "localmap",
10529#endif
10530#ifdef FEAT_MENU
10531 "menu",
10532#endif
10533#ifdef FEAT_SESSION
10534 "mksession",
10535#endif
10536#ifdef FEAT_MODIFY_FNAME
10537 "modify_fname",
10538#endif
10539#ifdef FEAT_MOUSE
10540 "mouse",
10541#endif
10542#ifdef FEAT_MOUSESHAPE
10543 "mouseshape",
10544#endif
10545#if defined(UNIX) || defined(VMS)
10546# ifdef FEAT_MOUSE_DEC
10547 "mouse_dec",
10548# endif
10549# ifdef FEAT_MOUSE_GPM
10550 "mouse_gpm",
10551# endif
10552# ifdef FEAT_MOUSE_JSB
10553 "mouse_jsbterm",
10554# endif
10555# ifdef FEAT_MOUSE_NET
10556 "mouse_netterm",
10557# endif
10558# ifdef FEAT_MOUSE_PTERM
10559 "mouse_pterm",
10560# endif
10561# ifdef FEAT_MOUSE_XTERM
10562 "mouse_xterm",
10563# endif
10564#endif
10565#ifdef FEAT_MBYTE
10566 "multi_byte",
10567#endif
10568#ifdef FEAT_MBYTE_IME
10569 "multi_byte_ime",
10570#endif
10571#ifdef FEAT_MULTI_LANG
10572 "multi_lang",
10573#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010574#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010575#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010576 "mzscheme",
10577#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579#ifdef FEAT_OLE
10580 "ole",
10581#endif
10582#ifdef FEAT_OSFILETYPE
10583 "osfiletype",
10584#endif
10585#ifdef FEAT_PATH_EXTRA
10586 "path_extra",
10587#endif
10588#ifdef FEAT_PERL
10589#ifndef DYNAMIC_PERL
10590 "perl",
10591#endif
10592#endif
10593#ifdef FEAT_PYTHON
10594#ifndef DYNAMIC_PYTHON
10595 "python",
10596#endif
10597#endif
10598#ifdef FEAT_POSTSCRIPT
10599 "postscript",
10600#endif
10601#ifdef FEAT_PRINTER
10602 "printer",
10603#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010604#ifdef FEAT_PROFILE
10605 "profile",
10606#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010607#ifdef FEAT_RELTIME
10608 "reltime",
10609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610#ifdef FEAT_QUICKFIX
10611 "quickfix",
10612#endif
10613#ifdef FEAT_RIGHTLEFT
10614 "rightleft",
10615#endif
10616#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10617 "ruby",
10618#endif
10619#ifdef FEAT_SCROLLBIND
10620 "scrollbind",
10621#endif
10622#ifdef FEAT_CMDL_INFO
10623 "showcmd",
10624 "cmdline_info",
10625#endif
10626#ifdef FEAT_SIGNS
10627 "signs",
10628#endif
10629#ifdef FEAT_SMARTINDENT
10630 "smartindent",
10631#endif
10632#ifdef FEAT_SNIFF
10633 "sniff",
10634#endif
10635#ifdef FEAT_STL_OPT
10636 "statusline",
10637#endif
10638#ifdef FEAT_SUN_WORKSHOP
10639 "sun_workshop",
10640#endif
10641#ifdef FEAT_NETBEANS_INTG
10642 "netbeans_intg",
10643#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010644#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010645 "spell",
10646#endif
10647#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 "syntax",
10649#endif
10650#if defined(USE_SYSTEM) || !defined(UNIX)
10651 "system",
10652#endif
10653#ifdef FEAT_TAG_BINS
10654 "tag_binary",
10655#endif
10656#ifdef FEAT_TAG_OLDSTATIC
10657 "tag_old_static",
10658#endif
10659#ifdef FEAT_TAG_ANYWHITE
10660 "tag_any_white",
10661#endif
10662#ifdef FEAT_TCL
10663# ifndef DYNAMIC_TCL
10664 "tcl",
10665# endif
10666#endif
10667#ifdef TERMINFO
10668 "terminfo",
10669#endif
10670#ifdef FEAT_TERMRESPONSE
10671 "termresponse",
10672#endif
10673#ifdef FEAT_TEXTOBJ
10674 "textobjects",
10675#endif
10676#ifdef HAVE_TGETENT
10677 "tgetent",
10678#endif
10679#ifdef FEAT_TITLE
10680 "title",
10681#endif
10682#ifdef FEAT_TOOLBAR
10683 "toolbar",
10684#endif
10685#ifdef FEAT_USR_CMDS
10686 "user-commands", /* was accidentally included in 5.4 */
10687 "user_commands",
10688#endif
10689#ifdef FEAT_VIMINFO
10690 "viminfo",
10691#endif
10692#ifdef FEAT_VERTSPLIT
10693 "vertsplit",
10694#endif
10695#ifdef FEAT_VIRTUALEDIT
10696 "virtualedit",
10697#endif
10698#ifdef FEAT_VISUAL
10699 "visual",
10700#endif
10701#ifdef FEAT_VISUALEXTRA
10702 "visualextra",
10703#endif
10704#ifdef FEAT_VREPLACE
10705 "vreplace",
10706#endif
10707#ifdef FEAT_WILDIGN
10708 "wildignore",
10709#endif
10710#ifdef FEAT_WILDMENU
10711 "wildmenu",
10712#endif
10713#ifdef FEAT_WINDOWS
10714 "windows",
10715#endif
10716#ifdef FEAT_WAK
10717 "winaltkeys",
10718#endif
10719#ifdef FEAT_WRITEBACKUP
10720 "writebackup",
10721#endif
10722#ifdef FEAT_XIM
10723 "xim",
10724#endif
10725#ifdef FEAT_XFONTSET
10726 "xfontset",
10727#endif
10728#ifdef USE_XSMP
10729 "xsmp",
10730#endif
10731#ifdef USE_XSMP_INTERACT
10732 "xsmp_interact",
10733#endif
10734#ifdef FEAT_XCLIPBOARD
10735 "xterm_clipboard",
10736#endif
10737#ifdef FEAT_XTERM_SAVE
10738 "xterm_save",
10739#endif
10740#if defined(UNIX) && defined(FEAT_X11)
10741 "X11",
10742#endif
10743 NULL
10744 };
10745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010746 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 for (i = 0; has_list[i] != NULL; ++i)
10748 if (STRICMP(name, has_list[i]) == 0)
10749 {
10750 n = TRUE;
10751 break;
10752 }
10753
10754 if (n == FALSE)
10755 {
10756 if (STRNICMP(name, "patch", 5) == 0)
10757 n = has_patch(atoi((char *)name + 5));
10758 else if (STRICMP(name, "vim_starting") == 0)
10759 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010760#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10761 else if (STRICMP(name, "balloon_multiline") == 0)
10762 n = multiline_balloon_available();
10763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764#ifdef DYNAMIC_TCL
10765 else if (STRICMP(name, "tcl") == 0)
10766 n = tcl_enabled(FALSE);
10767#endif
10768#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10769 else if (STRICMP(name, "iconv") == 0)
10770 n = iconv_enabled(FALSE);
10771#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010772#ifdef DYNAMIC_MZSCHEME
10773 else if (STRICMP(name, "mzscheme") == 0)
10774 n = mzscheme_enabled(FALSE);
10775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776#ifdef DYNAMIC_RUBY
10777 else if (STRICMP(name, "ruby") == 0)
10778 n = ruby_enabled(FALSE);
10779#endif
10780#ifdef DYNAMIC_PYTHON
10781 else if (STRICMP(name, "python") == 0)
10782 n = python_enabled(FALSE);
10783#endif
10784#ifdef DYNAMIC_PERL
10785 else if (STRICMP(name, "perl") == 0)
10786 n = perl_enabled(FALSE);
10787#endif
10788#ifdef FEAT_GUI
10789 else if (STRICMP(name, "gui_running") == 0)
10790 n = (gui.in_use || gui.starting);
10791# ifdef FEAT_GUI_W32
10792 else if (STRICMP(name, "gui_win32s") == 0)
10793 n = gui_is_win32s();
10794# endif
10795# ifdef FEAT_BROWSE
10796 else if (STRICMP(name, "browse") == 0)
10797 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10798# endif
10799#endif
10800#ifdef FEAT_SYN_HL
10801 else if (STRICMP(name, "syntax_items") == 0)
10802 n = syntax_present(curbuf);
10803#endif
10804#if defined(WIN3264)
10805 else if (STRICMP(name, "win95") == 0)
10806 n = mch_windows95();
10807#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010808#ifdef FEAT_NETBEANS_INTG
10809 else if (STRICMP(name, "netbeans_enabled") == 0)
10810 n = usingNetbeans;
10811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 }
10813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010814 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815}
10816
10817/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010818 * "has_key()" function
10819 */
10820 static void
10821f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010822 typval_T *argvars;
10823 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010824{
10825 rettv->vval.v_number = 0;
10826 if (argvars[0].v_type != VAR_DICT)
10827 {
10828 EMSG(_(e_dictreq));
10829 return;
10830 }
10831 if (argvars[0].vval.v_dict == NULL)
10832 return;
10833
10834 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010835 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010836}
10837
10838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 * "hasmapto()" function
10840 */
10841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010842f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010843 typval_T *argvars;
10844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845{
10846 char_u *name;
10847 char_u *mode;
10848 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000010849 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010851 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010852 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 mode = (char_u *)"nvo";
10854 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000010855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000010857 if (argvars[2].v_type != VAR_UNKNOWN)
10858 abbr = get_tv_number(&argvars[2]);
10859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860
Bram Moolenaar2c932302006-03-18 21:42:09 +000010861 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010862 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010864 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865}
10866
10867/*
10868 * "histadd()" function
10869 */
10870/*ARGSUSED*/
10871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010873 typval_T *argvars;
10874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875{
10876#ifdef FEAT_CMDHIST
10877 int histype;
10878 char_u *str;
10879 char_u buf[NUMBUFLEN];
10880#endif
10881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010882 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 if (check_restricted() || check_secure())
10884 return;
10885#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010886 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10887 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 if (histype >= 0)
10889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010890 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 if (*str != NUL)
10892 {
10893 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010894 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 return;
10896 }
10897 }
10898#endif
10899}
10900
10901/*
10902 * "histdel()" function
10903 */
10904/*ARGSUSED*/
10905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010906f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010907 typval_T *argvars;
10908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909{
10910#ifdef FEAT_CMDHIST
10911 int n;
10912 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010913 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10916 if (str == NULL)
10917 n = 0;
10918 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010920 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010921 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010923 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010924 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 else
10926 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010927 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010928 get_tv_string_buf(&argvars[1], buf));
10929 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010931 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932#endif
10933}
10934
10935/*
10936 * "histget()" function
10937 */
10938/*ARGSUSED*/
10939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010940f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010941 typval_T *argvars;
10942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943{
10944#ifdef FEAT_CMDHIST
10945 int type;
10946 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010947 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010949 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10950 if (str == NULL)
10951 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010953 {
10954 type = get_histtype(str);
10955 if (argvars[1].v_type == VAR_UNKNOWN)
10956 idx = get_history_idx(type);
10957 else
10958 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10959 /* -1 on type error */
10960 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010963 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966}
10967
10968/*
10969 * "histnr()" function
10970 */
10971/*ARGSUSED*/
10972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010973f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010974 typval_T *argvars;
10975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976{
10977 int i;
10978
10979#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010980 char_u *history = get_tv_string_chk(&argvars[0]);
10981
10982 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 if (i >= HIST_CMD && i < HIST_COUNT)
10984 i = get_history_idx(i);
10985 else
10986#endif
10987 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010988 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989}
10990
10991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 * "highlightID(name)" function
10993 */
10994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010995f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010996 typval_T *argvars;
10997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000}
11001
11002/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011003 * "highlight_exists()" function
11004 */
11005 static void
11006f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011007 typval_T *argvars;
11008 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011009{
11010 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11011}
11012
11013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 * "hostname()" function
11015 */
11016/*ARGSUSED*/
11017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011018f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011019 typval_T *argvars;
11020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021{
11022 char_u hostname[256];
11023
11024 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 rettv->v_type = VAR_STRING;
11026 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027}
11028
11029/*
11030 * iconv() function
11031 */
11032/*ARGSUSED*/
11033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011034f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011035 typval_T *argvars;
11036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037{
11038#ifdef FEAT_MBYTE
11039 char_u buf1[NUMBUFLEN];
11040 char_u buf2[NUMBUFLEN];
11041 char_u *from, *to, *str;
11042 vimconv_T vimconv;
11043#endif
11044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045 rettv->v_type = VAR_STRING;
11046 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047
11048#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011049 str = get_tv_string(&argvars[0]);
11050 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11051 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 vimconv.vc_type = CONV_NONE;
11053 convert_setup(&vimconv, from, to);
11054
11055 /* If the encodings are equal, no conversion needed. */
11056 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060
11061 convert_setup(&vimconv, NULL, NULL);
11062 vim_free(from);
11063 vim_free(to);
11064#endif
11065}
11066
11067/*
11068 * "indent()" function
11069 */
11070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011072 typval_T *argvars;
11073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074{
11075 linenr_T lnum;
11076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011079 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011081 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082}
11083
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011084/*
11085 * "index()" function
11086 */
11087 static void
11088f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011089 typval_T *argvars;
11090 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011091{
Bram Moolenaar33570922005-01-25 22:26:29 +000011092 list_T *l;
11093 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011094 long idx = 0;
11095 int ic = FALSE;
11096
11097 rettv->vval.v_number = -1;
11098 if (argvars[0].v_type != VAR_LIST)
11099 {
11100 EMSG(_(e_listreq));
11101 return;
11102 }
11103 l = argvars[0].vval.v_list;
11104 if (l != NULL)
11105 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011106 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011107 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 int error = FALSE;
11110
Bram Moolenaar758711c2005-02-02 23:11:38 +000011111 /* Start at specified item. Use the cached index that list_find()
11112 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011114 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011115 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011116 ic = get_tv_number_chk(&argvars[3], &error);
11117 if (error)
11118 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011119 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011120
Bram Moolenaar758711c2005-02-02 23:11:38 +000011121 for ( ; item != NULL; item = item->li_next, ++idx)
11122 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011123 {
11124 rettv->vval.v_number = idx;
11125 break;
11126 }
11127 }
11128}
11129
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130static int inputsecret_flag = 0;
11131
11132/*
11133 * "input()" function
11134 * Also handles inputsecret() when inputsecret is set.
11135 */
11136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011138 typval_T *argvars;
11139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011141 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 char_u *p = NULL;
11143 int c;
11144 char_u buf[NUMBUFLEN];
11145 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011146 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011147 int xp_type = EXPAND_NOTHING;
11148 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151
11152#ifdef NO_CONSOLE_INPUT
11153 /* While starting up, there is no place to enter text. */
11154 if (no_console_input())
11155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011156 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 return;
11158 }
11159#endif
11160
11161 cmd_silent = FALSE; /* Want to see the prompt. */
11162 if (prompt != NULL)
11163 {
11164 /* Only the part of the message after the last NL is considered as
11165 * prompt for the command line */
11166 p = vim_strrchr(prompt, '\n');
11167 if (p == NULL)
11168 p = prompt;
11169 else
11170 {
11171 ++p;
11172 c = *p;
11173 *p = NUL;
11174 msg_start();
11175 msg_clr_eos();
11176 msg_puts_attr(prompt, echo_attr);
11177 msg_didout = FALSE;
11178 msg_starthere();
11179 *p = c;
11180 }
11181 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011183 if (argvars[1].v_type != VAR_UNKNOWN)
11184 {
11185 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11186 if (defstr != NULL)
11187 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188
Bram Moolenaar4463f292005-09-25 22:20:24 +000011189 if (argvars[2].v_type != VAR_UNKNOWN)
11190 {
11191 char_u *xp_name;
11192 int xp_namelen;
11193 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011194
Bram Moolenaar4463f292005-09-25 22:20:24 +000011195 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011196
Bram Moolenaar4463f292005-09-25 22:20:24 +000011197 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11198 if (xp_name == NULL)
11199 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011200
Bram Moolenaar4463f292005-09-25 22:20:24 +000011201 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011202
Bram Moolenaar4463f292005-09-25 22:20:24 +000011203 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11204 &xp_arg) == FAIL)
11205 return;
11206 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011207 }
11208
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011209 if (defstr != NULL)
11210 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011211 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11212 xp_type, xp_arg);
11213
11214 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216 /* since the user typed this, no need to wait for return */
11217 need_wait_return = FALSE;
11218 msg_didout = FALSE;
11219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 cmd_silent = cmd_silent_save;
11221}
11222
11223/*
11224 * "inputdialog()" function
11225 */
11226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011227f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011228 typval_T *argvars;
11229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230{
11231#if defined(FEAT_GUI_TEXTDIALOG)
11232 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11233 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11234 {
11235 char_u *message;
11236 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011237 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011239 message = get_tv_string_chk(&argvars[0]);
11240 if (argvars[1].v_type != VAR_UNKNOWN
11241 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011242 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 else
11244 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011245 if (message != NULL && defstr != NULL
11246 && do_dialog(VIM_QUESTION, NULL, message,
11247 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011248 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 else
11250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011251 if (message != NULL && defstr != NULL
11252 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011253 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 rettv->vval.v_string = vim_strsave(
11255 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 }
11261 else
11262#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011263 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264}
11265
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011266/*
11267 * "inputlist()" function
11268 */
11269 static void
11270f_inputlist(argvars, rettv)
11271 typval_T *argvars;
11272 typval_T *rettv;
11273{
11274 listitem_T *li;
11275 int selected;
11276 int mouse_used;
11277
11278 rettv->vval.v_number = 0;
11279#ifdef NO_CONSOLE_INPUT
11280 /* While starting up, there is no place to enter text. */
11281 if (no_console_input())
11282 return;
11283#endif
11284 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11285 {
11286 EMSG2(_(e_listarg), "inputlist()");
11287 return;
11288 }
11289
11290 msg_start();
11291 lines_left = Rows; /* avoid more prompt */
11292 msg_scroll = TRUE;
11293 msg_clr_eos();
11294
11295 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11296 {
11297 msg_puts(get_tv_string(&li->li_tv));
11298 msg_putchar('\n');
11299 }
11300
11301 /* Ask for choice. */
11302 selected = prompt_for_number(&mouse_used);
11303 if (mouse_used)
11304 selected -= lines_left;
11305
11306 rettv->vval.v_number = selected;
11307}
11308
11309
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11311
11312/*
11313 * "inputrestore()" function
11314 */
11315/*ARGSUSED*/
11316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011317f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011318 typval_T *argvars;
11319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320{
11321 if (ga_userinput.ga_len > 0)
11322 {
11323 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11325 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011326 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 }
11328 else if (p_verbose > 1)
11329 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011330 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 }
11333}
11334
11335/*
11336 * "inputsave()" function
11337 */
11338/*ARGSUSED*/
11339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011341 typval_T *argvars;
11342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343{
11344 /* Add an entry to the stack of typehead storage. */
11345 if (ga_grow(&ga_userinput, 1) == OK)
11346 {
11347 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11348 + ga_userinput.ga_len);
11349 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351 }
11352 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354}
11355
11356/*
11357 * "inputsecret()" function
11358 */
11359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011360f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011361 typval_T *argvars;
11362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363{
11364 ++cmdline_star;
11365 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011366 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 --cmdline_star;
11368 --inputsecret_flag;
11369}
11370
11371/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011372 * "insert()" function
11373 */
11374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011376 typval_T *argvars;
11377 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011378{
11379 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011380 listitem_T *item;
11381 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011382 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011383
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011384 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011385 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011386 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011387 else if ((l = argvars[0].vval.v_list) != NULL
11388 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011389 {
11390 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011391 before = get_tv_number_chk(&argvars[2], &error);
11392 if (error)
11393 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011394
Bram Moolenaar758711c2005-02-02 23:11:38 +000011395 if (before == l->lv_len)
11396 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011397 else
11398 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011399 item = list_find(l, before);
11400 if (item == NULL)
11401 {
11402 EMSGN(_(e_listidx), before);
11403 l = NULL;
11404 }
11405 }
11406 if (l != NULL)
11407 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011408 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011409 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011410 }
11411 }
11412}
11413
11414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 * "isdirectory()" function
11416 */
11417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011419 typval_T *argvars;
11420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011422 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423}
11424
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011425/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011426 * "islocked()" function
11427 */
11428 static void
11429f_islocked(argvars, rettv)
11430 typval_T *argvars;
11431 typval_T *rettv;
11432{
11433 lval_T lv;
11434 char_u *end;
11435 dictitem_T *di;
11436
11437 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011438 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11439 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011440 if (end != NULL && lv.ll_name != NULL)
11441 {
11442 if (*end != NUL)
11443 EMSG(_(e_trailing));
11444 else
11445 {
11446 if (lv.ll_tv == NULL)
11447 {
11448 if (check_changedtick(lv.ll_name))
11449 rettv->vval.v_number = 1; /* always locked */
11450 else
11451 {
11452 di = find_var(lv.ll_name, NULL);
11453 if (di != NULL)
11454 {
11455 /* Consider a variable locked when:
11456 * 1. the variable itself is locked
11457 * 2. the value of the variable is locked.
11458 * 3. the List or Dict value is locked.
11459 */
11460 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11461 || tv_islocked(&di->di_tv));
11462 }
11463 }
11464 }
11465 else if (lv.ll_range)
11466 EMSG(_("E745: Range not allowed"));
11467 else if (lv.ll_newkey != NULL)
11468 EMSG2(_(e_dictkey), lv.ll_newkey);
11469 else if (lv.ll_list != NULL)
11470 /* List item. */
11471 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11472 else
11473 /* Dictionary item. */
11474 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11475 }
11476 }
11477
11478 clear_lval(&lv);
11479}
11480
Bram Moolenaar33570922005-01-25 22:26:29 +000011481static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011482
11483/*
11484 * Turn a dict into a list:
11485 * "what" == 0: list of keys
11486 * "what" == 1: list of values
11487 * "what" == 2: list of items
11488 */
11489 static void
11490dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011491 typval_T *argvars;
11492 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011493 int what;
11494{
Bram Moolenaar33570922005-01-25 22:26:29 +000011495 list_T *l2;
11496 dictitem_T *di;
11497 hashitem_T *hi;
11498 listitem_T *li;
11499 listitem_T *li2;
11500 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011501 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011502
11503 rettv->vval.v_number = 0;
11504 if (argvars[0].v_type != VAR_DICT)
11505 {
11506 EMSG(_(e_dictreq));
11507 return;
11508 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011509 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011510 return;
11511
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011512 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011513 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011514
Bram Moolenaar33570922005-01-25 22:26:29 +000011515 todo = d->dv_hashtab.ht_used;
11516 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011517 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011518 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011519 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011520 --todo;
11521 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011522
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011523 li = listitem_alloc();
11524 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011525 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011526 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011527
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011528 if (what == 0)
11529 {
11530 /* keys() */
11531 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011532 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011533 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11534 }
11535 else if (what == 1)
11536 {
11537 /* values() */
11538 copy_tv(&di->di_tv, &li->li_tv);
11539 }
11540 else
11541 {
11542 /* items() */
11543 l2 = list_alloc();
11544 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011545 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011546 li->li_tv.vval.v_list = l2;
11547 if (l2 == NULL)
11548 break;
11549 ++l2->lv_refcount;
11550
11551 li2 = listitem_alloc();
11552 if (li2 == NULL)
11553 break;
11554 list_append(l2, li2);
11555 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011556 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011557 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11558
11559 li2 = listitem_alloc();
11560 if (li2 == NULL)
11561 break;
11562 list_append(l2, li2);
11563 copy_tv(&di->di_tv, &li2->li_tv);
11564 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011565 }
11566 }
11567}
11568
11569/*
11570 * "items(dict)" function
11571 */
11572 static void
11573f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011574 typval_T *argvars;
11575 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011576{
11577 dict_list(argvars, rettv, 2);
11578}
11579
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011581 * "join()" function
11582 */
11583 static void
11584f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011585 typval_T *argvars;
11586 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011587{
11588 garray_T ga;
11589 char_u *sep;
11590
11591 rettv->vval.v_number = 0;
11592 if (argvars[0].v_type != VAR_LIST)
11593 {
11594 EMSG(_(e_listreq));
11595 return;
11596 }
11597 if (argvars[0].vval.v_list == NULL)
11598 return;
11599 if (argvars[1].v_type == VAR_UNKNOWN)
11600 sep = (char_u *)" ";
11601 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011602 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011603
11604 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011605
11606 if (sep != NULL)
11607 {
11608 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011609 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011610 ga_append(&ga, NUL);
11611 rettv->vval.v_string = (char_u *)ga.ga_data;
11612 }
11613 else
11614 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011615}
11616
11617/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011618 * "keys()" function
11619 */
11620 static void
11621f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011622 typval_T *argvars;
11623 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011624{
11625 dict_list(argvars, rettv, 0);
11626}
11627
11628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629 * "last_buffer_nr()" function.
11630 */
11631/*ARGSUSED*/
11632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011633f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011634 typval_T *argvars;
11635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636{
11637 int n = 0;
11638 buf_T *buf;
11639
11640 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11641 if (n < buf->b_fnum)
11642 n = buf->b_fnum;
11643
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011644 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011645}
11646
11647/*
11648 * "len()" function
11649 */
11650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011652 typval_T *argvars;
11653 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011654{
11655 switch (argvars[0].v_type)
11656 {
11657 case VAR_STRING:
11658 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659 rettv->vval.v_number = (varnumber_T)STRLEN(
11660 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011661 break;
11662 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011663 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011664 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011665 case VAR_DICT:
11666 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11667 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011668 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011669 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011670 break;
11671 }
11672}
11673
Bram Moolenaar33570922005-01-25 22:26:29 +000011674static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011675
11676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011677libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011678 typval_T *argvars;
11679 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011680 int type;
11681{
11682#ifdef FEAT_LIBCALL
11683 char_u *string_in;
11684 char_u **string_result;
11685 int nr_result;
11686#endif
11687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011688 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011689 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011690 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011691 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011692 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011693
11694 if (check_restricted() || check_secure())
11695 return;
11696
11697#ifdef FEAT_LIBCALL
11698 /* The first two args must be strings, otherwise its meaningless */
11699 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11700 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011701 string_in = NULL;
11702 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011703 string_in = argvars[2].vval.v_string;
11704 if (type == VAR_NUMBER)
11705 string_result = NULL;
11706 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011707 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011708 if (mch_libcall(argvars[0].vval.v_string,
11709 argvars[1].vval.v_string,
11710 string_in,
11711 argvars[2].vval.v_number,
11712 string_result,
11713 &nr_result) == OK
11714 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011715 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011716 }
11717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718}
11719
11720/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011721 * "libcall()" function
11722 */
11723 static void
11724f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011725 typval_T *argvars;
11726 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011727{
11728 libcall_common(argvars, rettv, VAR_STRING);
11729}
11730
11731/*
11732 * "libcallnr()" function
11733 */
11734 static void
11735f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011736 typval_T *argvars;
11737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011738{
11739 libcall_common(argvars, rettv, VAR_NUMBER);
11740}
11741
11742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011743 * "line(string)" function
11744 */
11745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011746f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011747 typval_T *argvars;
11748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749{
11750 linenr_T lnum = 0;
11751 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011752 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011754 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755 if (fp != NULL)
11756 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011757 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758}
11759
11760/*
11761 * "line2byte(lnum)" function
11762 */
11763/*ARGSUSED*/
11764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011765f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011766 typval_T *argvars;
11767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768{
11769#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011770 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771#else
11772 linenr_T lnum;
11773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011774 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11779 if (rettv->vval.v_number >= 0)
11780 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781#endif
11782}
11783
11784/*
11785 * "lispindent(lnum)" function
11786 */
11787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011789 typval_T *argvars;
11790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791{
11792#ifdef FEAT_LISP
11793 pos_T pos;
11794 linenr_T lnum;
11795
11796 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011797 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11799 {
11800 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011801 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802 curwin->w_cursor = pos;
11803 }
11804 else
11805#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011806 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807}
11808
11809/*
11810 * "localtime()" function
11811 */
11812/*ARGSUSED*/
11813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011814f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011815 typval_T *argvars;
11816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819}
11820
Bram Moolenaar33570922005-01-25 22:26:29 +000011821static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822
11823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011824get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011825 typval_T *argvars;
11826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827 int exact;
11828{
11829 char_u *keys;
11830 char_u *which;
11831 char_u buf[NUMBUFLEN];
11832 char_u *keys_buf = NULL;
11833 char_u *rhs;
11834 int mode;
11835 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000011836 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837
11838 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011839 rettv->v_type = VAR_STRING;
11840 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 if (*keys == NUL)
11844 return;
11845
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011846 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000011847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011848 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011849 if (argvars[2].v_type != VAR_UNKNOWN)
11850 abbr = get_tv_number(&argvars[2]);
11851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 else
11853 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011854 if (which == NULL)
11855 return;
11856
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857 mode = get_map_mode(&which, 0);
11858
11859 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011860 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861 vim_free(keys_buf);
11862 if (rhs != NULL)
11863 {
11864 ga_init(&ga);
11865 ga.ga_itemsize = 1;
11866 ga.ga_growsize = 40;
11867
11868 while (*rhs != NUL)
11869 ga_concat(&ga, str2special(&rhs, FALSE));
11870
11871 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011872 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 }
11874}
11875
11876/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011877 * "map()" function
11878 */
11879 static void
11880f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011881 typval_T *argvars;
11882 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011883{
11884 filter_map(argvars, rettv, TRUE);
11885}
11886
11887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011888 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889 */
11890 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011891f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011892 typval_T *argvars;
11893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011895 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896}
11897
11898/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011899 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900 */
11901 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011902f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011903 typval_T *argvars;
11904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011906 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907}
11908
Bram Moolenaar33570922005-01-25 22:26:29 +000011909static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910
11911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011912find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011913 typval_T *argvars;
11914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 int type;
11916{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011917 char_u *str = NULL;
11918 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919 char_u *pat;
11920 regmatch_T regmatch;
11921 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011922 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923 char_u *save_cpo;
11924 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011925 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011926 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011927 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011928 list_T *l = NULL;
11929 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011930 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011931 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932
11933 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11934 save_cpo = p_cpo;
11935 p_cpo = (char_u *)"";
11936
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011937 rettv->vval.v_number = -1;
11938 if (type == 3)
11939 {
11940 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011941 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011942 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011943 }
11944 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011946 rettv->v_type = VAR_STRING;
11947 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011950 if (argvars[0].v_type == VAR_LIST)
11951 {
11952 if ((l = argvars[0].vval.v_list) == NULL)
11953 goto theend;
11954 li = l->lv_first;
11955 }
11956 else
11957 expr = str = get_tv_string(&argvars[0]);
11958
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011959 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11960 if (pat == NULL)
11961 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011962
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011963 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011965 int error = FALSE;
11966
11967 start = get_tv_number_chk(&argvars[2], &error);
11968 if (error)
11969 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011970 if (l != NULL)
11971 {
11972 li = list_find(l, start);
11973 if (li == NULL)
11974 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011975 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011976 }
11977 else
11978 {
11979 if (start < 0)
11980 start = 0;
11981 if (start > (long)STRLEN(str))
11982 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011983 /* When "count" argument is there ignore matches before "start",
11984 * otherwise skip part of the string. Differs when pattern is "^"
11985 * or "\<". */
11986 if (argvars[3].v_type != VAR_UNKNOWN)
11987 startcol = start;
11988 else
11989 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011990 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011991
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011992 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011993 nth = get_tv_number_chk(&argvars[3], &error);
11994 if (error)
11995 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 }
11997
11998 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11999 if (regmatch.regprog != NULL)
12000 {
12001 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012002
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012003 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012004 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012005 if (l != NULL)
12006 {
12007 if (li == NULL)
12008 {
12009 match = FALSE;
12010 break;
12011 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012012 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012013 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012014 if (str == NULL)
12015 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012016 }
12017
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012018 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012019
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012020 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012021 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012022 if (l == NULL && !match)
12023 break;
12024
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012025 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012026 if (l != NULL)
12027 {
12028 li = li->li_next;
12029 ++idx;
12030 }
12031 else
12032 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012033#ifdef FEAT_MBYTE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012034 startcol = regmatch.startp[0]
12035 + (*mb_ptr2len)(regmatch.startp[0]) - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012036#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012037 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012038#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012039 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012040 }
12041
12042 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012044 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012045 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012046 int i;
12047
12048 /* return list with matched string and submatches */
12049 for (i = 0; i < NSUBEXP; ++i)
12050 {
12051 if (regmatch.endp[i] == NULL)
12052 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012053 if (list_append_string(rettv->vval.v_list,
12054 regmatch.startp[i],
12055 (int)(regmatch.endp[i] - regmatch.startp[i]))
12056 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012057 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012058 }
12059 }
12060 else if (type == 2)
12061 {
12062 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012063 if (l != NULL)
12064 copy_tv(&li->li_tv, rettv);
12065 else
12066 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012068 }
12069 else if (l != NULL)
12070 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 else
12072 {
12073 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012074 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075 (varnumber_T)(regmatch.startp[0] - str);
12076 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012077 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012079 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 }
12081 }
12082 vim_free(regmatch.regprog);
12083 }
12084
12085theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012086 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087 p_cpo = save_cpo;
12088}
12089
12090/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012091 * "match()" function
12092 */
12093 static void
12094f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012095 typval_T *argvars;
12096 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012097{
12098 find_some_match(argvars, rettv, 1);
12099}
12100
12101/*
12102 * "matchend()" function
12103 */
12104 static void
12105f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012106 typval_T *argvars;
12107 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012108{
12109 find_some_match(argvars, rettv, 0);
12110}
12111
12112/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012113 * "matchlist()" function
12114 */
12115 static void
12116f_matchlist(argvars, rettv)
12117 typval_T *argvars;
12118 typval_T *rettv;
12119{
12120 find_some_match(argvars, rettv, 3);
12121}
12122
12123/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012124 * "matchstr()" function
12125 */
12126 static void
12127f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012128 typval_T *argvars;
12129 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012130{
12131 find_some_match(argvars, rettv, 2);
12132}
12133
Bram Moolenaar33570922005-01-25 22:26:29 +000012134static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012135
12136 static void
12137max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012138 typval_T *argvars;
12139 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012140 int domax;
12141{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012142 long n = 0;
12143 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012144 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012145
12146 if (argvars[0].v_type == VAR_LIST)
12147 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012148 list_T *l;
12149 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012150
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012151 l = argvars[0].vval.v_list;
12152 if (l != NULL)
12153 {
12154 li = l->lv_first;
12155 if (li != NULL)
12156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012157 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012158 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012159 {
12160 li = li->li_next;
12161 if (li == NULL)
12162 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012163 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012164 if (domax ? i > n : i < n)
12165 n = i;
12166 }
12167 }
12168 }
12169 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012170 else if (argvars[0].v_type == VAR_DICT)
12171 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012172 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012173 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012174 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012175 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012176
12177 d = argvars[0].vval.v_dict;
12178 if (d != NULL)
12179 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012180 todo = d->dv_hashtab.ht_used;
12181 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012182 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012183 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012184 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012185 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012186 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012187 if (first)
12188 {
12189 n = i;
12190 first = FALSE;
12191 }
12192 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012193 n = i;
12194 }
12195 }
12196 }
12197 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012198 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012199 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012200 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012201}
12202
12203/*
12204 * "max()" function
12205 */
12206 static void
12207f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012208 typval_T *argvars;
12209 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012210{
12211 max_min(argvars, rettv, TRUE);
12212}
12213
12214/*
12215 * "min()" function
12216 */
12217 static void
12218f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012219 typval_T *argvars;
12220 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012221{
12222 max_min(argvars, rettv, FALSE);
12223}
12224
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012225static int mkdir_recurse __ARGS((char_u *dir, int prot));
12226
12227/*
12228 * Create the directory in which "dir" is located, and higher levels when
12229 * needed.
12230 */
12231 static int
12232mkdir_recurse(dir, prot)
12233 char_u *dir;
12234 int prot;
12235{
12236 char_u *p;
12237 char_u *updir;
12238 int r = FAIL;
12239
12240 /* Get end of directory name in "dir".
12241 * We're done when it's "/" or "c:/". */
12242 p = gettail_sep(dir);
12243 if (p <= get_past_head(dir))
12244 return OK;
12245
12246 /* If the directory exists we're done. Otherwise: create it.*/
12247 updir = vim_strnsave(dir, (int)(p - dir));
12248 if (updir == NULL)
12249 return FAIL;
12250 if (mch_isdir(updir))
12251 r = OK;
12252 else if (mkdir_recurse(updir, prot) == OK)
12253 r = vim_mkdir_emsg(updir, prot);
12254 vim_free(updir);
12255 return r;
12256}
12257
12258#ifdef vim_mkdir
12259/*
12260 * "mkdir()" function
12261 */
12262 static void
12263f_mkdir(argvars, rettv)
12264 typval_T *argvars;
12265 typval_T *rettv;
12266{
12267 char_u *dir;
12268 char_u buf[NUMBUFLEN];
12269 int prot = 0755;
12270
12271 rettv->vval.v_number = FAIL;
12272 if (check_restricted() || check_secure())
12273 return;
12274
12275 dir = get_tv_string_buf(&argvars[0], buf);
12276 if (argvars[1].v_type != VAR_UNKNOWN)
12277 {
12278 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012279 prot = get_tv_number_chk(&argvars[2], NULL);
12280 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012281 mkdir_recurse(dir, prot);
12282 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012283 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012284}
12285#endif
12286
Bram Moolenaar0d660222005-01-07 21:51:51 +000012287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 * "mode()" function
12289 */
12290/*ARGSUSED*/
12291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012292f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012293 typval_T *argvars;
12294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295{
12296 char_u buf[2];
12297
12298#ifdef FEAT_VISUAL
12299 if (VIsual_active)
12300 {
12301 if (VIsual_select)
12302 buf[0] = VIsual_mode + 's' - 'v';
12303 else
12304 buf[0] = VIsual_mode;
12305 }
12306 else
12307#endif
12308 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12309 buf[0] = 'r';
12310 else if (State & INSERT)
12311 {
12312 if (State & REPLACE_FLAG)
12313 buf[0] = 'R';
12314 else
12315 buf[0] = 'i';
12316 }
12317 else if (State & CMDLINE)
12318 buf[0] = 'c';
12319 else
12320 buf[0] = 'n';
12321
12322 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012323 rettv->vval.v_string = vim_strsave(buf);
12324 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325}
12326
12327/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012328 * "nextnonblank()" function
12329 */
12330 static void
12331f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012332 typval_T *argvars;
12333 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012334{
12335 linenr_T lnum;
12336
12337 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012339 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012340 {
12341 lnum = 0;
12342 break;
12343 }
12344 if (*skipwhite(ml_get(lnum)) != NUL)
12345 break;
12346 }
12347 rettv->vval.v_number = lnum;
12348}
12349
12350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351 * "nr2char()" function
12352 */
12353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012354f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012355 typval_T *argvars;
12356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357{
12358 char_u buf[NUMBUFLEN];
12359
12360#ifdef FEAT_MBYTE
12361 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012362 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363 else
12364#endif
12365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012366 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367 buf[1] = NUL;
12368 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012369 rettv->v_type = VAR_STRING;
12370 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012371}
12372
12373/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012374 * "prevnonblank()" function
12375 */
12376 static void
12377f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012378 typval_T *argvars;
12379 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012380{
12381 linenr_T lnum;
12382
12383 lnum = get_tv_lnum(argvars);
12384 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12385 lnum = 0;
12386 else
12387 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12388 --lnum;
12389 rettv->vval.v_number = lnum;
12390}
12391
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012392#ifdef HAVE_STDARG_H
12393/* This dummy va_list is here because:
12394 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12395 * - locally in the function results in a "used before set" warning
12396 * - using va_start() to initialize it gives "function with fixed args" error */
12397static va_list ap;
12398#endif
12399
Bram Moolenaar8c711452005-01-14 21:53:12 +000012400/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012401 * "printf()" function
12402 */
12403 static void
12404f_printf(argvars, rettv)
12405 typval_T *argvars;
12406 typval_T *rettv;
12407{
12408 rettv->v_type = VAR_STRING;
12409 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012410#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012411 {
12412 char_u buf[NUMBUFLEN];
12413 int len;
12414 char_u *s;
12415 int saved_did_emsg = did_emsg;
12416 char *fmt;
12417
12418 /* Get the required length, allocate the buffer and do it for real. */
12419 did_emsg = FALSE;
12420 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012421 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012422 if (!did_emsg)
12423 {
12424 s = alloc(len + 1);
12425 if (s != NULL)
12426 {
12427 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012428 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012429 }
12430 }
12431 did_emsg |= saved_did_emsg;
12432 }
12433#endif
12434}
12435
12436/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012437 * "pumvisible()" function
12438 */
12439/*ARGSUSED*/
12440 static void
12441f_pumvisible(argvars, rettv)
12442 typval_T *argvars;
12443 typval_T *rettv;
12444{
12445 rettv->vval.v_number = 0;
12446#ifdef FEAT_INS_EXPAND
12447 if (pum_visible())
12448 rettv->vval.v_number = 1;
12449#endif
12450}
12451
12452/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012453 * "range()" function
12454 */
12455 static void
12456f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012457 typval_T *argvars;
12458 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012459{
12460 long start;
12461 long end;
12462 long stride = 1;
12463 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012464 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012466 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012467 if (argvars[1].v_type == VAR_UNKNOWN)
12468 {
12469 end = start - 1;
12470 start = 0;
12471 }
12472 else
12473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012474 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012475 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012476 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012477 }
12478
12479 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012480 if (error)
12481 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012482 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012483 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012484 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012485 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012486 else
12487 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012488 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012489 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012490 if (list_append_number(rettv->vval.v_list,
12491 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012492 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012493 }
12494}
12495
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012496/*
12497 * "readfile()" function
12498 */
12499 static void
12500f_readfile(argvars, rettv)
12501 typval_T *argvars;
12502 typval_T *rettv;
12503{
12504 int binary = FALSE;
12505 char_u *fname;
12506 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012507 listitem_T *li;
12508#define FREAD_SIZE 200 /* optimized for text lines */
12509 char_u buf[FREAD_SIZE];
12510 int readlen; /* size of last fread() */
12511 int buflen; /* nr of valid chars in buf[] */
12512 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12513 int tolist; /* first byte in buf[] still to be put in list */
12514 int chop; /* how many CR to chop off */
12515 char_u *prev = NULL; /* previously read bytes, if any */
12516 int prevlen = 0; /* length of "prev" if not NULL */
12517 char_u *s;
12518 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012519 long maxline = MAXLNUM;
12520 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012521
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012522 if (argvars[1].v_type != VAR_UNKNOWN)
12523 {
12524 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12525 binary = TRUE;
12526 if (argvars[2].v_type != VAR_UNKNOWN)
12527 maxline = get_tv_number(&argvars[2]);
12528 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012529
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012530 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012531 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012532
12533 /* Always open the file in binary mode, library functions have a mind of
12534 * their own about CR-LF conversion. */
12535 fname = get_tv_string(&argvars[0]);
12536 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12537 {
12538 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12539 return;
12540 }
12541
12542 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012543 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012544 {
12545 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12546 buflen = filtd + readlen;
12547 tolist = 0;
12548 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12549 {
12550 if (buf[filtd] == '\n' || readlen <= 0)
12551 {
12552 /* Only when in binary mode add an empty list item when the
12553 * last line ends in a '\n'. */
12554 if (!binary && readlen == 0 && filtd == 0)
12555 break;
12556
12557 /* Found end-of-line or end-of-file: add a text line to the
12558 * list. */
12559 chop = 0;
12560 if (!binary)
12561 while (filtd - chop - 1 >= tolist
12562 && buf[filtd - chop - 1] == '\r')
12563 ++chop;
12564 len = filtd - tolist - chop;
12565 if (prev == NULL)
12566 s = vim_strnsave(buf + tolist, len);
12567 else
12568 {
12569 s = alloc((unsigned)(prevlen + len + 1));
12570 if (s != NULL)
12571 {
12572 mch_memmove(s, prev, prevlen);
12573 vim_free(prev);
12574 prev = NULL;
12575 mch_memmove(s + prevlen, buf + tolist, len);
12576 s[prevlen + len] = NUL;
12577 }
12578 }
12579 tolist = filtd + 1;
12580
12581 li = listitem_alloc();
12582 if (li == NULL)
12583 {
12584 vim_free(s);
12585 break;
12586 }
12587 li->li_tv.v_type = VAR_STRING;
12588 li->li_tv.v_lock = 0;
12589 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012590 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012591
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012592 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012593 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012594 if (readlen <= 0)
12595 break;
12596 }
12597 else if (buf[filtd] == NUL)
12598 buf[filtd] = '\n';
12599 }
12600 if (readlen <= 0)
12601 break;
12602
12603 if (tolist == 0)
12604 {
12605 /* "buf" is full, need to move text to an allocated buffer */
12606 if (prev == NULL)
12607 {
12608 prev = vim_strnsave(buf, buflen);
12609 prevlen = buflen;
12610 }
12611 else
12612 {
12613 s = alloc((unsigned)(prevlen + buflen));
12614 if (s != NULL)
12615 {
12616 mch_memmove(s, prev, prevlen);
12617 mch_memmove(s + prevlen, buf, buflen);
12618 vim_free(prev);
12619 prev = s;
12620 prevlen += buflen;
12621 }
12622 }
12623 filtd = 0;
12624 }
12625 else
12626 {
12627 mch_memmove(buf, buf + tolist, buflen - tolist);
12628 filtd -= tolist;
12629 }
12630 }
12631
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012632 /*
12633 * For a negative line count use only the lines at the end of the file,
12634 * free the rest.
12635 */
12636 if (maxline < 0)
12637 while (cnt > -maxline)
12638 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012639 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012640 --cnt;
12641 }
12642
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012643 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012644 fclose(fd);
12645}
12646
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012647#if defined(FEAT_RELTIME)
12648static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12649
12650/*
12651 * Convert a List to proftime_T.
12652 * Return FAIL when there is something wrong.
12653 */
12654 static int
12655list2proftime(arg, tm)
12656 typval_T *arg;
12657 proftime_T *tm;
12658{
12659 long n1, n2;
12660 int error = FALSE;
12661
12662 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12663 || arg->vval.v_list->lv_len != 2)
12664 return FAIL;
12665 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12666 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12667# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012668 tm->HighPart = n1;
12669 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012670# else
12671 tm->tv_sec = n1;
12672 tm->tv_usec = n2;
12673# endif
12674 return error ? FAIL : OK;
12675}
12676#endif /* FEAT_RELTIME */
12677
12678/*
12679 * "reltime()" function
12680 */
12681 static void
12682f_reltime(argvars, rettv)
12683 typval_T *argvars;
12684 typval_T *rettv;
12685{
12686#ifdef FEAT_RELTIME
12687 proftime_T res;
12688 proftime_T start;
12689
12690 if (argvars[0].v_type == VAR_UNKNOWN)
12691 {
12692 /* No arguments: get current time. */
12693 profile_start(&res);
12694 }
12695 else if (argvars[1].v_type == VAR_UNKNOWN)
12696 {
12697 if (list2proftime(&argvars[0], &res) == FAIL)
12698 return;
12699 profile_end(&res);
12700 }
12701 else
12702 {
12703 /* Two arguments: compute the difference. */
12704 if (list2proftime(&argvars[0], &start) == FAIL
12705 || list2proftime(&argvars[1], &res) == FAIL)
12706 return;
12707 profile_sub(&res, &start);
12708 }
12709
12710 if (rettv_list_alloc(rettv) == OK)
12711 {
12712 long n1, n2;
12713
12714# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012715 n1 = res.HighPart;
12716 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012717# else
12718 n1 = res.tv_sec;
12719 n2 = res.tv_usec;
12720# endif
12721 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12722 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12723 }
12724#endif
12725}
12726
12727/*
12728 * "reltimestr()" function
12729 */
12730 static void
12731f_reltimestr(argvars, rettv)
12732 typval_T *argvars;
12733 typval_T *rettv;
12734{
12735#ifdef FEAT_RELTIME
12736 proftime_T tm;
12737#endif
12738
12739 rettv->v_type = VAR_STRING;
12740 rettv->vval.v_string = NULL;
12741#ifdef FEAT_RELTIME
12742 if (list2proftime(&argvars[0], &tm) == OK)
12743 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
12744#endif
12745}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012746
Bram Moolenaar0d660222005-01-07 21:51:51 +000012747#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12748static void make_connection __ARGS((void));
12749static int check_connection __ARGS((void));
12750
12751 static void
12752make_connection()
12753{
12754 if (X_DISPLAY == NULL
12755# ifdef FEAT_GUI
12756 && !gui.in_use
12757# endif
12758 )
12759 {
12760 x_force_connect = TRUE;
12761 setup_term_clip();
12762 x_force_connect = FALSE;
12763 }
12764}
12765
12766 static int
12767check_connection()
12768{
12769 make_connection();
12770 if (X_DISPLAY == NULL)
12771 {
12772 EMSG(_("E240: No connection to Vim server"));
12773 return FAIL;
12774 }
12775 return OK;
12776}
12777#endif
12778
12779#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012780static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012781
12782 static void
12783remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012784 typval_T *argvars;
12785 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012786 int expr;
12787{
12788 char_u *server_name;
12789 char_u *keys;
12790 char_u *r = NULL;
12791 char_u buf[NUMBUFLEN];
12792# ifdef WIN32
12793 HWND w;
12794# else
12795 Window w;
12796# endif
12797
12798 if (check_restricted() || check_secure())
12799 return;
12800
12801# ifdef FEAT_X11
12802 if (check_connection() == FAIL)
12803 return;
12804# endif
12805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012806 server_name = get_tv_string_chk(&argvars[0]);
12807 if (server_name == NULL)
12808 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012809 keys = get_tv_string_buf(&argvars[1], buf);
12810# ifdef WIN32
12811 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12812# else
12813 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12814 < 0)
12815# endif
12816 {
12817 if (r != NULL)
12818 EMSG(r); /* sending worked but evaluation failed */
12819 else
12820 EMSG2(_("E241: Unable to send to %s"), server_name);
12821 return;
12822 }
12823
12824 rettv->vval.v_string = r;
12825
12826 if (argvars[2].v_type != VAR_UNKNOWN)
12827 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012828 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012829 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012830 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012831
12832 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012833 v.di_tv.v_type = VAR_STRING;
12834 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012835 idvar = get_tv_string_chk(&argvars[2]);
12836 if (idvar != NULL)
12837 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012838 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012839 }
12840}
12841#endif
12842
12843/*
12844 * "remote_expr()" function
12845 */
12846/*ARGSUSED*/
12847 static void
12848f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012849 typval_T *argvars;
12850 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012851{
12852 rettv->v_type = VAR_STRING;
12853 rettv->vval.v_string = NULL;
12854#ifdef FEAT_CLIENTSERVER
12855 remote_common(argvars, rettv, TRUE);
12856#endif
12857}
12858
12859/*
12860 * "remote_foreground()" function
12861 */
12862/*ARGSUSED*/
12863 static void
12864f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012865 typval_T *argvars;
12866 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012867{
12868 rettv->vval.v_number = 0;
12869#ifdef FEAT_CLIENTSERVER
12870# ifdef WIN32
12871 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012872 {
12873 char_u *server_name = get_tv_string_chk(&argvars[0]);
12874
12875 if (server_name != NULL)
12876 serverForeground(server_name);
12877 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012878# else
12879 /* Send a foreground() expression to the server. */
12880 argvars[1].v_type = VAR_STRING;
12881 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12882 argvars[2].v_type = VAR_UNKNOWN;
12883 remote_common(argvars, rettv, TRUE);
12884 vim_free(argvars[1].vval.v_string);
12885# endif
12886#endif
12887}
12888
12889/*ARGSUSED*/
12890 static void
12891f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012892 typval_T *argvars;
12893 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012894{
12895#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012896 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012897 char_u *s = NULL;
12898# ifdef WIN32
12899 int n = 0;
12900# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012901 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012902
12903 if (check_restricted() || check_secure())
12904 {
12905 rettv->vval.v_number = -1;
12906 return;
12907 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012908 serverid = get_tv_string_chk(&argvars[0]);
12909 if (serverid == NULL)
12910 {
12911 rettv->vval.v_number = -1;
12912 return; /* type error; errmsg already given */
12913 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012914# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012915 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012916 if (n == 0)
12917 rettv->vval.v_number = -1;
12918 else
12919 {
12920 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12921 rettv->vval.v_number = (s != NULL);
12922 }
12923# else
12924 rettv->vval.v_number = 0;
12925 if (check_connection() == FAIL)
12926 return;
12927
12928 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012929 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012930# endif
12931
12932 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12933 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012934 char_u *retvar;
12935
Bram Moolenaar33570922005-01-25 22:26:29 +000012936 v.di_tv.v_type = VAR_STRING;
12937 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012938 retvar = get_tv_string_chk(&argvars[1]);
12939 if (retvar != NULL)
12940 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012941 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012942 }
12943#else
12944 rettv->vval.v_number = -1;
12945#endif
12946}
12947
12948/*ARGSUSED*/
12949 static void
12950f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012951 typval_T *argvars;
12952 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012953{
12954 char_u *r = NULL;
12955
12956#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012957 char_u *serverid = get_tv_string_chk(&argvars[0]);
12958
12959 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012960 {
12961# ifdef WIN32
12962 /* The server's HWND is encoded in the 'id' parameter */
12963 int n = 0;
12964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012965 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012966 if (n != 0)
12967 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12968 if (r == NULL)
12969# else
12970 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012971 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012972# endif
12973 EMSG(_("E277: Unable to read a server reply"));
12974 }
12975#endif
12976 rettv->v_type = VAR_STRING;
12977 rettv->vval.v_string = r;
12978}
12979
12980/*
12981 * "remote_send()" function
12982 */
12983/*ARGSUSED*/
12984 static void
12985f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012986 typval_T *argvars;
12987 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012988{
12989 rettv->v_type = VAR_STRING;
12990 rettv->vval.v_string = NULL;
12991#ifdef FEAT_CLIENTSERVER
12992 remote_common(argvars, rettv, FALSE);
12993#endif
12994}
12995
12996/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012997 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012998 */
12999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013000f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013001 typval_T *argvars;
13002 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013003{
Bram Moolenaar33570922005-01-25 22:26:29 +000013004 list_T *l;
13005 listitem_T *item, *item2;
13006 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013007 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013008 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013009 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013010 dict_T *d;
13011 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013012
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013013 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013014 if (argvars[0].v_type == VAR_DICT)
13015 {
13016 if (argvars[2].v_type != VAR_UNKNOWN)
13017 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013018 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000013019 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013020 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013021 key = get_tv_string_chk(&argvars[1]);
13022 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013024 di = dict_find(d, key, -1);
13025 if (di == NULL)
13026 EMSG2(_(e_dictkey), key);
13027 else
13028 {
13029 *rettv = di->di_tv;
13030 init_tv(&di->di_tv);
13031 dictitem_remove(d, di);
13032 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013033 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013034 }
13035 }
13036 else if (argvars[0].v_type != VAR_LIST)
13037 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013038 else if ((l = argvars[0].vval.v_list) != NULL
13039 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013041 int error = FALSE;
13042
13043 idx = get_tv_number_chk(&argvars[1], &error);
13044 if (error)
13045 ; /* type error: do nothing, errmsg already given */
13046 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013047 EMSGN(_(e_listidx), idx);
13048 else
13049 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013050 if (argvars[2].v_type == VAR_UNKNOWN)
13051 {
13052 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013053 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013054 *rettv = item->li_tv;
13055 vim_free(item);
13056 }
13057 else
13058 {
13059 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013060 end = get_tv_number_chk(&argvars[2], &error);
13061 if (error)
13062 ; /* type error: do nothing */
13063 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013064 EMSGN(_(e_listidx), end);
13065 else
13066 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013067 int cnt = 0;
13068
13069 for (li = item; li != NULL; li = li->li_next)
13070 {
13071 ++cnt;
13072 if (li == item2)
13073 break;
13074 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013075 if (li == NULL) /* didn't find "item2" after "item" */
13076 EMSG(_(e_invrange));
13077 else
13078 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013079 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013080 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013081 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013082 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013083 l->lv_first = item;
13084 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013085 item->li_prev = NULL;
13086 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013087 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013088 }
13089 }
13090 }
13091 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013092 }
13093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094}
13095
13096/*
13097 * "rename({from}, {to})" function
13098 */
13099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013100f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013101 typval_T *argvars;
13102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103{
13104 char_u buf[NUMBUFLEN];
13105
13106 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013109 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13110 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111}
13112
13113/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013114 * "repeat()" function
13115 */
13116/*ARGSUSED*/
13117 static void
13118f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013119 typval_T *argvars;
13120 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013121{
13122 char_u *p;
13123 int n;
13124 int slen;
13125 int len;
13126 char_u *r;
13127 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013128
13129 n = get_tv_number(&argvars[1]);
13130 if (argvars[0].v_type == VAR_LIST)
13131 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013132 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013133 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013134 if (list_extend(rettv->vval.v_list,
13135 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013136 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013137 }
13138 else
13139 {
13140 p = get_tv_string(&argvars[0]);
13141 rettv->v_type = VAR_STRING;
13142 rettv->vval.v_string = NULL;
13143
13144 slen = (int)STRLEN(p);
13145 len = slen * n;
13146 if (len <= 0)
13147 return;
13148
13149 r = alloc(len + 1);
13150 if (r != NULL)
13151 {
13152 for (i = 0; i < n; i++)
13153 mch_memmove(r + i * slen, p, (size_t)slen);
13154 r[len] = NUL;
13155 }
13156
13157 rettv->vval.v_string = r;
13158 }
13159}
13160
13161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 * "resolve()" function
13163 */
13164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013165f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013166 typval_T *argvars;
13167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168{
13169 char_u *p;
13170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013171 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172#ifdef FEAT_SHORTCUT
13173 {
13174 char_u *v = NULL;
13175
13176 v = mch_resolve_shortcut(p);
13177 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013178 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013180 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181 }
13182#else
13183# ifdef HAVE_READLINK
13184 {
13185 char_u buf[MAXPATHL + 1];
13186 char_u *cpy;
13187 int len;
13188 char_u *remain = NULL;
13189 char_u *q;
13190 int is_relative_to_current = FALSE;
13191 int has_trailing_pathsep = FALSE;
13192 int limit = 100;
13193
13194 p = vim_strsave(p);
13195
13196 if (p[0] == '.' && (vim_ispathsep(p[1])
13197 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13198 is_relative_to_current = TRUE;
13199
13200 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013201 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202 has_trailing_pathsep = TRUE;
13203
13204 q = getnextcomp(p);
13205 if (*q != NUL)
13206 {
13207 /* Separate the first path component in "p", and keep the
13208 * remainder (beginning with the path separator). */
13209 remain = vim_strsave(q - 1);
13210 q[-1] = NUL;
13211 }
13212
13213 for (;;)
13214 {
13215 for (;;)
13216 {
13217 len = readlink((char *)p, (char *)buf, MAXPATHL);
13218 if (len <= 0)
13219 break;
13220 buf[len] = NUL;
13221
13222 if (limit-- == 0)
13223 {
13224 vim_free(p);
13225 vim_free(remain);
13226 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013227 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228 goto fail;
13229 }
13230
13231 /* Ensure that the result will have a trailing path separator
13232 * if the argument has one. */
13233 if (remain == NULL && has_trailing_pathsep)
13234 add_pathsep(buf);
13235
13236 /* Separate the first path component in the link value and
13237 * concatenate the remainders. */
13238 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13239 if (*q != NUL)
13240 {
13241 if (remain == NULL)
13242 remain = vim_strsave(q - 1);
13243 else
13244 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013245 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246 if (cpy != NULL)
13247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 vim_free(remain);
13249 remain = cpy;
13250 }
13251 }
13252 q[-1] = NUL;
13253 }
13254
13255 q = gettail(p);
13256 if (q > p && *q == NUL)
13257 {
13258 /* Ignore trailing path separator. */
13259 q[-1] = NUL;
13260 q = gettail(p);
13261 }
13262 if (q > p && !mch_isFullName(buf))
13263 {
13264 /* symlink is relative to directory of argument */
13265 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13266 if (cpy != NULL)
13267 {
13268 STRCPY(cpy, p);
13269 STRCPY(gettail(cpy), buf);
13270 vim_free(p);
13271 p = cpy;
13272 }
13273 }
13274 else
13275 {
13276 vim_free(p);
13277 p = vim_strsave(buf);
13278 }
13279 }
13280
13281 if (remain == NULL)
13282 break;
13283
13284 /* Append the first path component of "remain" to "p". */
13285 q = getnextcomp(remain + 1);
13286 len = q - remain - (*q != NUL);
13287 cpy = vim_strnsave(p, STRLEN(p) + len);
13288 if (cpy != NULL)
13289 {
13290 STRNCAT(cpy, remain, len);
13291 vim_free(p);
13292 p = cpy;
13293 }
13294 /* Shorten "remain". */
13295 if (*q != NUL)
13296 STRCPY(remain, q - 1);
13297 else
13298 {
13299 vim_free(remain);
13300 remain = NULL;
13301 }
13302 }
13303
13304 /* If the result is a relative path name, make it explicitly relative to
13305 * the current directory if and only if the argument had this form. */
13306 if (!vim_ispathsep(*p))
13307 {
13308 if (is_relative_to_current
13309 && *p != NUL
13310 && !(p[0] == '.'
13311 && (p[1] == NUL
13312 || vim_ispathsep(p[1])
13313 || (p[1] == '.'
13314 && (p[2] == NUL
13315 || vim_ispathsep(p[2]))))))
13316 {
13317 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013318 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319 if (cpy != NULL)
13320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321 vim_free(p);
13322 p = cpy;
13323 }
13324 }
13325 else if (!is_relative_to_current)
13326 {
13327 /* Strip leading "./". */
13328 q = p;
13329 while (q[0] == '.' && vim_ispathsep(q[1]))
13330 q += 2;
13331 if (q > p)
13332 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13333 }
13334 }
13335
13336 /* Ensure that the result will have no trailing path separator
13337 * if the argument had none. But keep "/" or "//". */
13338 if (!has_trailing_pathsep)
13339 {
13340 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013341 if (after_pathsep(p, q))
13342 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343 }
13344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013345 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346 }
13347# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013348 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349# endif
13350#endif
13351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013352 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353
13354#ifdef HAVE_READLINK
13355fail:
13356#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013357 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358}
13359
13360/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013361 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 */
13363 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013364f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013365 typval_T *argvars;
13366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367{
Bram Moolenaar33570922005-01-25 22:26:29 +000013368 list_T *l;
13369 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370
Bram Moolenaar0d660222005-01-07 21:51:51 +000013371 rettv->vval.v_number = 0;
13372 if (argvars[0].v_type != VAR_LIST)
13373 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013374 else if ((l = argvars[0].vval.v_list) != NULL
13375 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013376 {
13377 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013378 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013379 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013380 while (li != NULL)
13381 {
13382 ni = li->li_prev;
13383 list_append(l, li);
13384 li = ni;
13385 }
13386 rettv->vval.v_list = l;
13387 rettv->v_type = VAR_LIST;
13388 ++l->lv_refcount;
13389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390}
13391
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013392#define SP_NOMOVE 0x01 /* don't move cursor */
13393#define SP_REPEAT 0x02 /* repeat to find outer pair */
13394#define SP_RETCOUNT 0x04 /* return matchcount */
13395#define SP_SETPCMARK 0x08 /* set previous context mark */
13396#define SP_START 0x10 /* accept match at start position */
13397#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13398#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013399
Bram Moolenaar33570922005-01-25 22:26:29 +000013400static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013401
13402/*
13403 * Get flags for a search function.
13404 * Possibly sets "p_ws".
13405 * Returns BACKWARD, FORWARD or zero (for an error).
13406 */
13407 static int
13408get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013409 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013410 int *flagsp;
13411{
13412 int dir = FORWARD;
13413 char_u *flags;
13414 char_u nbuf[NUMBUFLEN];
13415 int mask;
13416
13417 if (varp->v_type != VAR_UNKNOWN)
13418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013419 flags = get_tv_string_buf_chk(varp, nbuf);
13420 if (flags == NULL)
13421 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013422 while (*flags != NUL)
13423 {
13424 switch (*flags)
13425 {
13426 case 'b': dir = BACKWARD; break;
13427 case 'w': p_ws = TRUE; break;
13428 case 'W': p_ws = FALSE; break;
13429 default: mask = 0;
13430 if (flagsp != NULL)
13431 switch (*flags)
13432 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013433 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013434 case 'e': mask = SP_END; break;
13435 case 'm': mask = SP_RETCOUNT; break;
13436 case 'n': mask = SP_NOMOVE; break;
13437 case 'p': mask = SP_SUBPAT; break;
13438 case 'r': mask = SP_REPEAT; break;
13439 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013440 }
13441 if (mask == 0)
13442 {
13443 EMSG2(_(e_invarg2), flags);
13444 dir = 0;
13445 }
13446 else
13447 *flagsp |= mask;
13448 }
13449 if (dir == 0)
13450 break;
13451 ++flags;
13452 }
13453 }
13454 return dir;
13455}
13456
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013458 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013460 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013461search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013462 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013463 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013464 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013466 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 char_u *pat;
13468 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013469 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470 int save_p_ws = p_ws;
13471 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013472 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013473 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013474 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013475 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013478 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013479 if (dir == 0)
13480 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013481 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013482 if (flags & SP_START)
13483 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013484 if (flags & SP_END)
13485 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013486
13487 /* Optional extra argument: line number to stop searching. */
13488 if (argvars[1].v_type != VAR_UNKNOWN
13489 && argvars[2].v_type != VAR_UNKNOWN)
13490 {
13491 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13492 if (lnum_stop < 0)
13493 goto theend;
13494 }
13495
Bram Moolenaar231334e2005-07-25 20:46:57 +000013496 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013497 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013498 * Check to make sure only those flags are set.
13499 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13500 * flags cannot be set. Check for that condition also.
13501 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013502 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013503 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013504 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013506 goto theend;
13507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013509 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013510 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13511 options, RE_SEARCH, (linenr_T)lnum_stop);
13512 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013514 if (flags & SP_SUBPAT)
13515 retval = subpatnum;
13516 else
13517 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013518 if (flags & SP_SETPCMARK)
13519 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013521 if (match_pos != NULL)
13522 {
13523 /* Store the match cursor position */
13524 match_pos->lnum = pos.lnum;
13525 match_pos->col = pos.col + 1;
13526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527 /* "/$" will put the cursor after the end of the line, may need to
13528 * correct that here */
13529 check_cursor();
13530 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013531
13532 /* If 'n' flag is used: restore cursor position. */
13533 if (flags & SP_NOMOVE)
13534 curwin->w_cursor = save_cursor;
13535theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013537
13538 return retval;
13539}
13540
13541/*
13542 * "search()" function
13543 */
13544 static void
13545f_search(argvars, rettv)
13546 typval_T *argvars;
13547 typval_T *rettv;
13548{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013549 int flags = 0;
13550
13551 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552}
13553
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013555 * "searchdecl()" function
13556 */
13557 static void
13558f_searchdecl(argvars, rettv)
13559 typval_T *argvars;
13560 typval_T *rettv;
13561{
13562 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013563 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013564 int error = FALSE;
13565 char_u *name;
13566
13567 rettv->vval.v_number = 1; /* default: FAIL */
13568
13569 name = get_tv_string_chk(&argvars[0]);
13570 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013571 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013572 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013573 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13574 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13575 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013576 if (!error && name != NULL)
13577 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013578 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013579}
13580
13581/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013582 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013584 static int
13585searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013586 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013587 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588{
13589 char_u *spat, *mpat, *epat;
13590 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592 int dir;
13593 int flags = 0;
13594 char_u nbuf1[NUMBUFLEN];
13595 char_u nbuf2[NUMBUFLEN];
13596 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013597 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013598 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013601 spat = get_tv_string_chk(&argvars[0]);
13602 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13603 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13604 if (spat == NULL || mpat == NULL || epat == NULL)
13605 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607 /* Handle the optional fourth argument: flags */
13608 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013609 if (dir == 0)
13610 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013611
13612 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013613 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13614 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013615 if ((flags & (SP_END | SP_SUBPAT)) != 0
13616 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013617 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013618 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013619 goto theend;
13620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013622 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013623 if (argvars[3].v_type == VAR_UNKNOWN
13624 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625 skip = (char_u *)"";
13626 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013628 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013629 if (argvars[5].v_type != VAR_UNKNOWN)
13630 {
13631 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13632 if (lnum_stop < 0)
13633 goto theend;
13634 }
13635 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013636 if (skip == NULL)
13637 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013639 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13640 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013641
13642theend:
13643 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013644
13645 return retval;
13646}
13647
13648/*
13649 * "searchpair()" function
13650 */
13651 static void
13652f_searchpair(argvars, rettv)
13653 typval_T *argvars;
13654 typval_T *rettv;
13655{
13656 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13657}
13658
13659/*
13660 * "searchpairpos()" function
13661 */
13662 static void
13663f_searchpairpos(argvars, rettv)
13664 typval_T *argvars;
13665 typval_T *rettv;
13666{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013667 pos_T match_pos;
13668 int lnum = 0;
13669 int col = 0;
13670
13671 rettv->vval.v_number = 0;
13672
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013673 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013674 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013675
13676 if (searchpair_cmn(argvars, &match_pos) > 0)
13677 {
13678 lnum = match_pos.lnum;
13679 col = match_pos.col;
13680 }
13681
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013682 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13683 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013684}
13685
13686/*
13687 * Search for a start/middle/end thing.
13688 * Used by searchpair(), see its documentation for the details.
13689 * Returns 0 or -1 for no match,
13690 */
13691 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013692do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013693 char_u *spat; /* start pattern */
13694 char_u *mpat; /* middle pattern */
13695 char_u *epat; /* end pattern */
13696 int dir; /* BACKWARD or FORWARD */
13697 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013698 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013699 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013700 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013701{
13702 char_u *save_cpo;
13703 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13704 long retval = 0;
13705 pos_T pos;
13706 pos_T firstpos;
13707 pos_T foundpos;
13708 pos_T save_cursor;
13709 pos_T save_pos;
13710 int n;
13711 int r;
13712 int nest = 1;
13713 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013714 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013715
13716 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13717 save_cpo = p_cpo;
13718 p_cpo = (char_u *)"";
13719
13720 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13721 * start/middle/end (pat3, for the top pair). */
13722 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13723 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13724 if (pat2 == NULL || pat3 == NULL)
13725 goto theend;
13726 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13727 if (*mpat == NUL)
13728 STRCPY(pat3, pat2);
13729 else
13730 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13731 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013732 if (flags & SP_START)
13733 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013734
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735 save_cursor = curwin->w_cursor;
13736 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013737 clearpos(&firstpos);
13738 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739 pat = pat3;
13740 for (;;)
13741 {
13742 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013743 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13745 /* didn't find it or found the first match again: FAIL */
13746 break;
13747
13748 if (firstpos.lnum == 0)
13749 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013750 if (equalpos(pos, foundpos))
13751 {
13752 /* Found the same position again. Can happen with a pattern that
13753 * has "\zs" at the end and searching backwards. Advance one
13754 * character and try again. */
13755 if (dir == BACKWARD)
13756 decl(&pos);
13757 else
13758 incl(&pos);
13759 }
13760 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761
13762 /* If the skip pattern matches, ignore this match. */
13763 if (*skip != NUL)
13764 {
13765 save_pos = curwin->w_cursor;
13766 curwin->w_cursor = pos;
13767 r = eval_to_bool(skip, &err, NULL, FALSE);
13768 curwin->w_cursor = save_pos;
13769 if (err)
13770 {
13771 /* Evaluating {skip} caused an error, break here. */
13772 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013773 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 break;
13775 }
13776 if (r)
13777 continue;
13778 }
13779
13780 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13781 {
13782 /* Found end when searching backwards or start when searching
13783 * forward: nested pair. */
13784 ++nest;
13785 pat = pat2; /* nested, don't search for middle */
13786 }
13787 else
13788 {
13789 /* Found end when searching forward or start when searching
13790 * backward: end of (nested) pair; or found middle in outer pair. */
13791 if (--nest == 1)
13792 pat = pat3; /* outer level, search for middle */
13793 }
13794
13795 if (nest == 0)
13796 {
13797 /* Found the match: return matchcount or line number. */
13798 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013799 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013801 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013802 if (flags & SP_SETPCMARK)
13803 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 curwin->w_cursor = pos;
13805 if (!(flags & SP_REPEAT))
13806 break;
13807 nest = 1; /* search for next unmatched */
13808 }
13809 }
13810
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013811 if (match_pos != NULL)
13812 {
13813 /* Store the match cursor position */
13814 match_pos->lnum = curwin->w_cursor.lnum;
13815 match_pos->col = curwin->w_cursor.col + 1;
13816 }
13817
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013819 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 curwin->w_cursor = save_cursor;
13821
13822theend:
13823 vim_free(pat2);
13824 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013826
13827 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828}
13829
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013830/*
13831 * "searchpos()" function
13832 */
13833 static void
13834f_searchpos(argvars, rettv)
13835 typval_T *argvars;
13836 typval_T *rettv;
13837{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013838 pos_T match_pos;
13839 int lnum = 0;
13840 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013841 int n;
13842 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013843
13844 rettv->vval.v_number = 0;
13845
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013846 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013847 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013848
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013849 n = search_cmn(argvars, &match_pos, &flags);
13850 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013851 {
13852 lnum = match_pos.lnum;
13853 col = match_pos.col;
13854 }
13855
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013856 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13857 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013858 if (flags & SP_SUBPAT)
13859 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013860}
13861
13862
Bram Moolenaar0d660222005-01-07 21:51:51 +000013863/*ARGSUSED*/
13864 static void
13865f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013866 typval_T *argvars;
13867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013869#ifdef FEAT_CLIENTSERVER
13870 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013871 char_u *server = get_tv_string_chk(&argvars[0]);
13872 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873
Bram Moolenaar0d660222005-01-07 21:51:51 +000013874 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013875 if (server == NULL || reply == NULL)
13876 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013877 if (check_restricted() || check_secure())
13878 return;
13879# ifdef FEAT_X11
13880 if (check_connection() == FAIL)
13881 return;
13882# endif
13883
13884 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013886 EMSG(_("E258: Unable to send to client"));
13887 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013889 rettv->vval.v_number = 0;
13890#else
13891 rettv->vval.v_number = -1;
13892#endif
13893}
13894
13895/*ARGSUSED*/
13896 static void
13897f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013898 typval_T *argvars;
13899 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013900{
13901 char_u *r = NULL;
13902
13903#ifdef FEAT_CLIENTSERVER
13904# ifdef WIN32
13905 r = serverGetVimNames();
13906# else
13907 make_connection();
13908 if (X_DISPLAY != NULL)
13909 r = serverGetVimNames(X_DISPLAY);
13910# endif
13911#endif
13912 rettv->v_type = VAR_STRING;
13913 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914}
13915
13916/*
13917 * "setbufvar()" function
13918 */
13919/*ARGSUSED*/
13920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013921f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013922 typval_T *argvars;
13923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924{
13925 buf_T *buf;
13926#ifdef FEAT_AUTOCMD
13927 aco_save_T aco;
13928#else
13929 buf_T *save_curbuf;
13930#endif
13931 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013932 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 char_u nbuf[NUMBUFLEN];
13934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013935 rettv->vval.v_number = 0;
13936
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937 if (check_restricted() || check_secure())
13938 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013939 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13940 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013941 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942 varp = &argvars[2];
13943
13944 if (buf != NULL && varname != NULL && varp != NULL)
13945 {
13946 /* set curbuf to be our buf, temporarily */
13947#ifdef FEAT_AUTOCMD
13948 aucmd_prepbuf(&aco, buf);
13949#else
13950 save_curbuf = curbuf;
13951 curbuf = buf;
13952#endif
13953
13954 if (*varname == '&')
13955 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013956 long numval;
13957 char_u *strval;
13958 int error = FALSE;
13959
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013961 numval = get_tv_number_chk(varp, &error);
13962 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013963 if (!error && strval != NULL)
13964 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 }
13966 else
13967 {
13968 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13969 if (bufvarname != NULL)
13970 {
13971 STRCPY(bufvarname, "b:");
13972 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013973 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 vim_free(bufvarname);
13975 }
13976 }
13977
13978 /* reset notion of buffer */
13979#ifdef FEAT_AUTOCMD
13980 aucmd_restbuf(&aco);
13981#else
13982 curbuf = save_curbuf;
13983#endif
13984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985}
13986
13987/*
13988 * "setcmdpos()" function
13989 */
13990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013991f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013992 typval_T *argvars;
13993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013995 int pos = (int)get_tv_number(&argvars[0]) - 1;
13996
13997 if (pos >= 0)
13998 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999}
14000
14001/*
14002 * "setline()" function
14003 */
14004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014005f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014006 typval_T *argvars;
14007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008{
14009 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014010 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014011 list_T *l = NULL;
14012 listitem_T *li = NULL;
14013 long added = 0;
14014 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014016 lnum = get_tv_lnum(&argvars[0]);
14017 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014019 l = argvars[1].vval.v_list;
14020 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014022 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014023 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014024
14025 rettv->vval.v_number = 0; /* OK */
14026 for (;;)
14027 {
14028 if (l != NULL)
14029 {
14030 /* list argument, get next string */
14031 if (li == NULL)
14032 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014033 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014034 li = li->li_next;
14035 }
14036
14037 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014038 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014039 break;
14040 if (lnum <= curbuf->b_ml.ml_line_count)
14041 {
14042 /* existing line, replace it */
14043 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14044 {
14045 changed_bytes(lnum, 0);
14046 check_cursor_col();
14047 rettv->vval.v_number = 0; /* OK */
14048 }
14049 }
14050 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14051 {
14052 /* lnum is one past the last line, append the line */
14053 ++added;
14054 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14055 rettv->vval.v_number = 0; /* OK */
14056 }
14057
14058 if (l == NULL) /* only one string argument */
14059 break;
14060 ++lnum;
14061 }
14062
14063 if (added > 0)
14064 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065}
14066
14067/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014068 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014069 */
14070/*ARGSUSED*/
14071 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014072set_qf_ll_list(wp, list_arg, action_arg, rettv)
14073 win_T *wp;
14074 typval_T *list_arg;
14075 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014076 typval_T *rettv;
14077{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014078#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014079 char_u *act;
14080 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014081#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014082
Bram Moolenaar2641f772005-03-25 21:58:17 +000014083 rettv->vval.v_number = -1;
14084
14085#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014086 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014087 EMSG(_(e_listreq));
14088 else
14089 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014090 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014091
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014092 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014093 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014094 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014095 if (act == NULL)
14096 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014097 if (*act == 'a' || *act == 'r')
14098 action = *act;
14099 }
14100
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014101 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014102 rettv->vval.v_number = 0;
14103 }
14104#endif
14105}
14106
14107/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014108 * "setloclist()" function
14109 */
14110/*ARGSUSED*/
14111 static void
14112f_setloclist(argvars, rettv)
14113 typval_T *argvars;
14114 typval_T *rettv;
14115{
14116 win_T *win;
14117
14118 rettv->vval.v_number = -1;
14119
14120 win = find_win_by_nr(&argvars[0]);
14121 if (win != NULL)
14122 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14123}
14124
14125/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014126 * "setpos()" function
14127 */
14128/*ARGSUSED*/
14129 static void
14130f_setpos(argvars, rettv)
14131 typval_T *argvars;
14132 typval_T *rettv;
14133{
14134 pos_T pos;
14135 int fnum;
14136 char_u *name;
14137
14138 name = get_tv_string_chk(argvars);
14139 if (name != NULL)
14140 {
14141 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14142 {
14143 --pos.col;
14144 if (name[0] == '.') /* cursor */
14145 {
14146 if (fnum == curbuf->b_fnum)
14147 {
14148 curwin->w_cursor = pos;
14149 check_cursor();
14150 }
14151 else
14152 EMSG(_(e_invarg));
14153 }
14154 else if (name[0] == '\'') /* mark */
14155 (void)setmark_pos(name[1], &pos, fnum);
14156 else
14157 EMSG(_(e_invarg));
14158 }
14159 }
14160}
14161
14162/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014163 * "setqflist()" function
14164 */
14165/*ARGSUSED*/
14166 static void
14167f_setqflist(argvars, rettv)
14168 typval_T *argvars;
14169 typval_T *rettv;
14170{
14171 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14172}
14173
14174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 * "setreg()" function
14176 */
14177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014178f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014179 typval_T *argvars;
14180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181{
14182 int regname;
14183 char_u *strregname;
14184 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014185 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186 int append;
14187 char_u yank_type;
14188 long block_len;
14189
14190 block_len = -1;
14191 yank_type = MAUTO;
14192 append = FALSE;
14193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014194 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014195 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 if (strregname == NULL)
14198 return; /* type error; errmsg already given */
14199 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200 if (regname == 0 || regname == '@')
14201 regname = '"';
14202 else if (regname == '=')
14203 return;
14204
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014205 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 stropt = get_tv_string_chk(&argvars[2]);
14208 if (stropt == NULL)
14209 return; /* type error */
14210 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211 switch (*stropt)
14212 {
14213 case 'a': case 'A': /* append */
14214 append = TRUE;
14215 break;
14216 case 'v': case 'c': /* character-wise selection */
14217 yank_type = MCHAR;
14218 break;
14219 case 'V': case 'l': /* line-wise selection */
14220 yank_type = MLINE;
14221 break;
14222#ifdef FEAT_VISUAL
14223 case 'b': case Ctrl_V: /* block-wise selection */
14224 yank_type = MBLOCK;
14225 if (VIM_ISDIGIT(stropt[1]))
14226 {
14227 ++stropt;
14228 block_len = getdigits(&stropt) - 1;
14229 --stropt;
14230 }
14231 break;
14232#endif
14233 }
14234 }
14235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236 strval = get_tv_string_chk(&argvars[1]);
14237 if (strval != NULL)
14238 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014240 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241}
14242
14243
14244/*
14245 * "setwinvar(expr)" function
14246 */
14247/*ARGSUSED*/
14248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014249f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014250 typval_T *argvars;
14251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252{
14253 win_T *win;
14254#ifdef FEAT_WINDOWS
14255 win_T *save_curwin;
14256#endif
14257 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014258 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259 char_u nbuf[NUMBUFLEN];
14260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261 rettv->vval.v_number = 0;
14262
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263 if (check_restricted() || check_secure())
14264 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014265 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014266 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267 varp = &argvars[2];
14268
14269 if (win != NULL && varname != NULL && varp != NULL)
14270 {
14271#ifdef FEAT_WINDOWS
14272 /* set curwin to be our win, temporarily */
14273 save_curwin = curwin;
14274 curwin = win;
14275 curbuf = curwin->w_buffer;
14276#endif
14277
14278 if (*varname == '&')
14279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014280 long numval;
14281 char_u *strval;
14282 int error = FALSE;
14283
Bram Moolenaar071d4272004-06-13 20:20:40 +000014284 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014285 numval = get_tv_number_chk(varp, &error);
14286 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014287 if (!error && strval != NULL)
14288 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 }
14290 else
14291 {
14292 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14293 if (winvarname != NULL)
14294 {
14295 STRCPY(winvarname, "w:");
14296 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014297 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298 vim_free(winvarname);
14299 }
14300 }
14301
14302#ifdef FEAT_WINDOWS
14303 /* Restore current window, if it's still valid (autocomands can make
14304 * it invalid). */
14305 if (win_valid(save_curwin))
14306 {
14307 curwin = save_curwin;
14308 curbuf = curwin->w_buffer;
14309 }
14310#endif
14311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312}
14313
14314/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014315 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 */
14317 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014318f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014319 typval_T *argvars;
14320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014322 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323
Bram Moolenaar0d660222005-01-07 21:51:51 +000014324 p = get_tv_string(&argvars[0]);
14325 rettv->vval.v_string = vim_strsave(p);
14326 simplify_filename(rettv->vval.v_string); /* simplify in place */
14327 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328}
14329
Bram Moolenaar0d660222005-01-07 21:51:51 +000014330static int
14331#ifdef __BORLANDC__
14332 _RTLENTRYF
14333#endif
14334 item_compare __ARGS((const void *s1, const void *s2));
14335static int
14336#ifdef __BORLANDC__
14337 _RTLENTRYF
14338#endif
14339 item_compare2 __ARGS((const void *s1, const void *s2));
14340
14341static int item_compare_ic;
14342static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014344#define ITEM_COMPARE_FAIL 999
14345
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014347 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014349 static int
14350#ifdef __BORLANDC__
14351_RTLENTRYF
14352#endif
14353item_compare(s1, s2)
14354 const void *s1;
14355 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014357 char_u *p1, *p2;
14358 char_u *tofree1, *tofree2;
14359 int res;
14360 char_u numbuf1[NUMBUFLEN];
14361 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014363 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14364 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014365 if (item_compare_ic)
14366 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014368 res = STRCMP(p1, p2);
14369 vim_free(tofree1);
14370 vim_free(tofree2);
14371 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372}
14373
14374 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014375#ifdef __BORLANDC__
14376_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014378item_compare2(s1, s2)
14379 const void *s1;
14380 const void *s2;
14381{
14382 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014383 typval_T rettv;
14384 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014385 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014387 /* shortcut after failure in previous call; compare all items equal */
14388 if (item_compare_func_err)
14389 return 0;
14390
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014391 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14392 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014393 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14394 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014395
14396 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14397 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014398 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014399 clear_tv(&argv[0]);
14400 clear_tv(&argv[1]);
14401
14402 if (res == FAIL)
14403 res = ITEM_COMPARE_FAIL;
14404 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014405 /* return value has wrong type */
14406 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14407 if (item_compare_func_err)
14408 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014409 clear_tv(&rettv);
14410 return res;
14411}
14412
14413/*
14414 * "sort({list})" function
14415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014417f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014418 typval_T *argvars;
14419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420{
Bram Moolenaar33570922005-01-25 22:26:29 +000014421 list_T *l;
14422 listitem_T *li;
14423 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014424 long len;
14425 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426
Bram Moolenaar0d660222005-01-07 21:51:51 +000014427 rettv->vval.v_number = 0;
14428 if (argvars[0].v_type != VAR_LIST)
14429 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 else
14431 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014432 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014433 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014434 return;
14435 rettv->vval.v_list = l;
14436 rettv->v_type = VAR_LIST;
14437 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438
Bram Moolenaar0d660222005-01-07 21:51:51 +000014439 len = list_len(l);
14440 if (len <= 1)
14441 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442
Bram Moolenaar0d660222005-01-07 21:51:51 +000014443 item_compare_ic = FALSE;
14444 item_compare_func = NULL;
14445 if (argvars[1].v_type != VAR_UNKNOWN)
14446 {
14447 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014448 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014449 else
14450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014451 int error = FALSE;
14452
14453 i = get_tv_number_chk(&argvars[1], &error);
14454 if (error)
14455 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014456 if (i == 1)
14457 item_compare_ic = TRUE;
14458 else
14459 item_compare_func = get_tv_string(&argvars[1]);
14460 }
14461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462
Bram Moolenaar0d660222005-01-07 21:51:51 +000014463 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014464 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014465 if (ptrs == NULL)
14466 return;
14467 i = 0;
14468 for (li = l->lv_first; li != NULL; li = li->li_next)
14469 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014471 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014472 /* test the compare function */
14473 if (item_compare_func != NULL
14474 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14475 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014476 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014478 {
14479 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014480 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014481 item_compare_func == NULL ? item_compare : item_compare2);
14482
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014483 if (!item_compare_func_err)
14484 {
14485 /* Clear the List and append the items in the sorted order. */
14486 l->lv_first = l->lv_last = NULL;
14487 l->lv_len = 0;
14488 for (i = 0; i < len; ++i)
14489 list_append(l, ptrs[i]);
14490 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014491 }
14492
14493 vim_free(ptrs);
14494 }
14495}
14496
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014497/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014498 * "soundfold({word})" function
14499 */
14500 static void
14501f_soundfold(argvars, rettv)
14502 typval_T *argvars;
14503 typval_T *rettv;
14504{
14505 char_u *s;
14506
14507 rettv->v_type = VAR_STRING;
14508 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014509#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014510 rettv->vval.v_string = eval_soundfold(s);
14511#else
14512 rettv->vval.v_string = vim_strsave(s);
14513#endif
14514}
14515
14516/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014517 * "spellbadword()" function
14518 */
14519/* ARGSUSED */
14520 static void
14521f_spellbadword(argvars, rettv)
14522 typval_T *argvars;
14523 typval_T *rettv;
14524{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014525 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014526 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014527 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014528
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014529 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014530 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014531
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014532#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014533 if (argvars[0].v_type == VAR_UNKNOWN)
14534 {
14535 /* Find the start and length of the badly spelled word. */
14536 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14537 if (len != 0)
14538 word = ml_get_cursor();
14539 }
14540 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14541 {
14542 char_u *str = get_tv_string_chk(&argvars[0]);
14543 int capcol = -1;
14544
14545 if (str != NULL)
14546 {
14547 /* Check the argument for spelling. */
14548 while (*str != NUL)
14549 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014550 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014551 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014552 {
14553 word = str;
14554 break;
14555 }
14556 str += len;
14557 }
14558 }
14559 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014560#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014561
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014562 list_append_string(rettv->vval.v_list, word, len);
14563 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014564 attr == HLF_SPB ? "bad" :
14565 attr == HLF_SPR ? "rare" :
14566 attr == HLF_SPL ? "local" :
14567 attr == HLF_SPC ? "caps" :
14568 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014569}
14570
14571/*
14572 * "spellsuggest()" function
14573 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014574/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014575 static void
14576f_spellsuggest(argvars, rettv)
14577 typval_T *argvars;
14578 typval_T *rettv;
14579{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014580#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014581 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014582 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014583 int maxcount;
14584 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014585 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014586 listitem_T *li;
14587 int need_capital = FALSE;
14588#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014589
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014590 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014591 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014592
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014593#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014594 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14595 {
14596 str = get_tv_string(&argvars[0]);
14597 if (argvars[1].v_type != VAR_UNKNOWN)
14598 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014599 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014600 if (maxcount <= 0)
14601 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014602 if (argvars[2].v_type != VAR_UNKNOWN)
14603 {
14604 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14605 if (typeerr)
14606 return;
14607 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014608 }
14609 else
14610 maxcount = 25;
14611
Bram Moolenaar4770d092006-01-12 23:22:24 +000014612 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014613
14614 for (i = 0; i < ga.ga_len; ++i)
14615 {
14616 str = ((char_u **)ga.ga_data)[i];
14617
14618 li = listitem_alloc();
14619 if (li == NULL)
14620 vim_free(str);
14621 else
14622 {
14623 li->li_tv.v_type = VAR_STRING;
14624 li->li_tv.v_lock = 0;
14625 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014626 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014627 }
14628 }
14629 ga_clear(&ga);
14630 }
14631#endif
14632}
14633
Bram Moolenaar0d660222005-01-07 21:51:51 +000014634 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014635f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014636 typval_T *argvars;
14637 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638{
14639 char_u *str;
14640 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014641 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014642 regmatch_T regmatch;
14643 char_u patbuf[NUMBUFLEN];
14644 char_u *save_cpo;
14645 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014646 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014647 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014648 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649
14650 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14651 save_cpo = p_cpo;
14652 p_cpo = (char_u *)"";
14653
14654 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014655 if (argvars[1].v_type != VAR_UNKNOWN)
14656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014657 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14658 if (pat == NULL)
14659 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014660 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014661 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014662 }
14663 if (pat == NULL || *pat == NUL)
14664 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014665
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014666 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014668 if (typeerr)
14669 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670
Bram Moolenaar0d660222005-01-07 21:51:51 +000014671 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14672 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014674 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014675 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014676 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014677 if (*str == NUL)
14678 match = FALSE; /* empty item at the end */
14679 else
14680 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014681 if (match)
14682 end = regmatch.startp[0];
14683 else
14684 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014685 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14686 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014687 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014688 if (list_append_string(rettv->vval.v_list, str,
14689 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014690 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014691 }
14692 if (!match)
14693 break;
14694 /* Advance to just after the match. */
14695 if (regmatch.endp[0] > str)
14696 col = 0;
14697 else
14698 {
14699 /* Don't get stuck at the same match. */
14700#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014701 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014702#else
14703 col = 1;
14704#endif
14705 }
14706 str = regmatch.endp[0];
14707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708
Bram Moolenaar0d660222005-01-07 21:51:51 +000014709 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711
Bram Moolenaar0d660222005-01-07 21:51:51 +000014712 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713}
14714
Bram Moolenaar2c932302006-03-18 21:42:09 +000014715/*
14716 * "str2nr()" function
14717 */
14718 static void
14719f_str2nr(argvars, rettv)
14720 typval_T *argvars;
14721 typval_T *rettv;
14722{
14723 int base = 10;
14724 char_u *p;
14725 long n;
14726
14727 if (argvars[1].v_type != VAR_UNKNOWN)
14728 {
14729 base = get_tv_number(&argvars[1]);
14730 if (base != 8 && base != 10 && base != 16)
14731 {
14732 EMSG(_(e_invarg));
14733 return;
14734 }
14735 }
14736
14737 p = skipwhite(get_tv_string(&argvars[0]));
14738 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
14739 rettv->vval.v_number = n;
14740}
14741
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742#ifdef HAVE_STRFTIME
14743/*
14744 * "strftime({format}[, {time}])" function
14745 */
14746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014747f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014748 typval_T *argvars;
14749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750{
14751 char_u result_buf[256];
14752 struct tm *curtime;
14753 time_t seconds;
14754 char_u *p;
14755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014756 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014758 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014759 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 seconds = time(NULL);
14761 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014762 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763 curtime = localtime(&seconds);
14764 /* MSVC returns NULL for an invalid value of seconds. */
14765 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014766 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 else
14768 {
14769# ifdef FEAT_MBYTE
14770 vimconv_T conv;
14771 char_u *enc;
14772
14773 conv.vc_type = CONV_NONE;
14774 enc = enc_locale();
14775 convert_setup(&conv, p_enc, enc);
14776 if (conv.vc_type != CONV_NONE)
14777 p = string_convert(&conv, p, NULL);
14778# endif
14779 if (p != NULL)
14780 (void)strftime((char *)result_buf, sizeof(result_buf),
14781 (char *)p, curtime);
14782 else
14783 result_buf[0] = NUL;
14784
14785# ifdef FEAT_MBYTE
14786 if (conv.vc_type != CONV_NONE)
14787 vim_free(p);
14788 convert_setup(&conv, enc, p_enc);
14789 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014790 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791 else
14792# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014793 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794
14795# ifdef FEAT_MBYTE
14796 /* Release conversion descriptors */
14797 convert_setup(&conv, NULL, NULL);
14798 vim_free(enc);
14799# endif
14800 }
14801}
14802#endif
14803
14804/*
14805 * "stridx()" function
14806 */
14807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014808f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014809 typval_T *argvars;
14810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811{
14812 char_u buf[NUMBUFLEN];
14813 char_u *needle;
14814 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014815 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014817 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014819 needle = get_tv_string_chk(&argvars[1]);
14820 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014821 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014822 if (needle == NULL || haystack == NULL)
14823 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824
Bram Moolenaar33570922005-01-25 22:26:29 +000014825 if (argvars[2].v_type != VAR_UNKNOWN)
14826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014827 int error = FALSE;
14828
14829 start_idx = get_tv_number_chk(&argvars[2], &error);
14830 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014831 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014832 if (start_idx >= 0)
14833 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014834 }
14835
14836 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14837 if (pos != NULL)
14838 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839}
14840
14841/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014842 * "string()" function
14843 */
14844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014845f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014846 typval_T *argvars;
14847 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014848{
14849 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014850 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014852 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014853 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014854 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014855 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856}
14857
14858/*
14859 * "strlen()" function
14860 */
14861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014862f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014863 typval_T *argvars;
14864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014866 rettv->vval.v_number = (varnumber_T)(STRLEN(
14867 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868}
14869
14870/*
14871 * "strpart()" function
14872 */
14873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014874f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014875 typval_T *argvars;
14876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014877{
14878 char_u *p;
14879 int n;
14880 int len;
14881 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014882 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014884 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885 slen = (int)STRLEN(p);
14886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014887 n = get_tv_number_chk(&argvars[1], &error);
14888 if (error)
14889 len = 0;
14890 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014891 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892 else
14893 len = slen - n; /* default len: all bytes that are available. */
14894
14895 /*
14896 * Only return the overlap between the specified part and the actual
14897 * string.
14898 */
14899 if (n < 0)
14900 {
14901 len += n;
14902 n = 0;
14903 }
14904 else if (n > slen)
14905 n = slen;
14906 if (len < 0)
14907 len = 0;
14908 else if (n + len > slen)
14909 len = slen - n;
14910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014911 rettv->v_type = VAR_STRING;
14912 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913}
14914
14915/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014916 * "strridx()" function
14917 */
14918 static void
14919f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014920 typval_T *argvars;
14921 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014922{
14923 char_u buf[NUMBUFLEN];
14924 char_u *needle;
14925 char_u *haystack;
14926 char_u *rest;
14927 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014928 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014930 needle = get_tv_string_chk(&argvars[1]);
14931 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014932 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014933
14934 rettv->vval.v_number = -1;
14935 if (needle == NULL || haystack == NULL)
14936 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014937 if (argvars[2].v_type != VAR_UNKNOWN)
14938 {
14939 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014940 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014941 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014942 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014943 }
14944 else
14945 end_idx = haystack_len;
14946
Bram Moolenaar0d660222005-01-07 21:51:51 +000014947 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014948 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014949 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014950 lastmatch = haystack + end_idx;
14951 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014952 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014953 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014954 for (rest = haystack; *rest != '\0'; ++rest)
14955 {
14956 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014957 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014958 break;
14959 lastmatch = rest;
14960 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014961 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014962
14963 if (lastmatch == NULL)
14964 rettv->vval.v_number = -1;
14965 else
14966 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14967}
14968
14969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970 * "strtrans()" function
14971 */
14972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014973f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014974 typval_T *argvars;
14975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014977 rettv->v_type = VAR_STRING;
14978 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979}
14980
14981/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014982 * "submatch()" function
14983 */
14984 static void
14985f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014986 typval_T *argvars;
14987 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014988{
14989 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014990 rettv->vval.v_string =
14991 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014992}
14993
14994/*
14995 * "substitute()" function
14996 */
14997 static void
14998f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014999 typval_T *argvars;
15000 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015001{
15002 char_u patbuf[NUMBUFLEN];
15003 char_u subbuf[NUMBUFLEN];
15004 char_u flagsbuf[NUMBUFLEN];
15005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015006 char_u *str = get_tv_string_chk(&argvars[0]);
15007 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15008 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15009 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15010
Bram Moolenaar0d660222005-01-07 21:51:51 +000015011 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015012 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15013 rettv->vval.v_string = NULL;
15014 else
15015 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015016}
15017
15018/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015019 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 */
15021/*ARGSUSED*/
15022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015023f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015024 typval_T *argvars;
15025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026{
15027 int id = 0;
15028#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015029 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030 long col;
15031 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015032 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015034 lnum = get_tv_lnum(argvars); /* -1 on type error */
15035 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15036 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015038 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015039 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015040 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041#endif
15042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015043 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044}
15045
15046/*
15047 * "synIDattr(id, what [, mode])" function
15048 */
15049/*ARGSUSED*/
15050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015051f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015052 typval_T *argvars;
15053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054{
15055 char_u *p = NULL;
15056#ifdef FEAT_SYN_HL
15057 int id;
15058 char_u *what;
15059 char_u *mode;
15060 char_u modebuf[NUMBUFLEN];
15061 int modec;
15062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015063 id = get_tv_number(&argvars[0]);
15064 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015065 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015067 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068 modec = TOLOWER_ASC(mode[0]);
15069 if (modec != 't' && modec != 'c'
15070#ifdef FEAT_GUI
15071 && modec != 'g'
15072#endif
15073 )
15074 modec = 0; /* replace invalid with current */
15075 }
15076 else
15077 {
15078#ifdef FEAT_GUI
15079 if (gui.in_use)
15080 modec = 'g';
15081 else
15082#endif
15083 if (t_colors > 1)
15084 modec = 'c';
15085 else
15086 modec = 't';
15087 }
15088
15089
15090 switch (TOLOWER_ASC(what[0]))
15091 {
15092 case 'b':
15093 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15094 p = highlight_color(id, what, modec);
15095 else /* bold */
15096 p = highlight_has_attr(id, HL_BOLD, modec);
15097 break;
15098
15099 case 'f': /* fg[#] */
15100 p = highlight_color(id, what, modec);
15101 break;
15102
15103 case 'i':
15104 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15105 p = highlight_has_attr(id, HL_INVERSE, modec);
15106 else /* italic */
15107 p = highlight_has_attr(id, HL_ITALIC, modec);
15108 break;
15109
15110 case 'n': /* name */
15111 p = get_highlight_name(NULL, id - 1);
15112 break;
15113
15114 case 'r': /* reverse */
15115 p = highlight_has_attr(id, HL_INVERSE, modec);
15116 break;
15117
15118 case 's': /* standout */
15119 p = highlight_has_attr(id, HL_STANDOUT, modec);
15120 break;
15121
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015122 case 'u':
15123 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15124 /* underline */
15125 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15126 else
15127 /* undercurl */
15128 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129 break;
15130 }
15131
15132 if (p != NULL)
15133 p = vim_strsave(p);
15134#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015135 rettv->v_type = VAR_STRING;
15136 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137}
15138
15139/*
15140 * "synIDtrans(id)" function
15141 */
15142/*ARGSUSED*/
15143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015144f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015145 typval_T *argvars;
15146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147{
15148 int id;
15149
15150#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015151 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152
15153 if (id > 0)
15154 id = syn_get_final_id(id);
15155 else
15156#endif
15157 id = 0;
15158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015159 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160}
15161
15162/*
15163 * "system()" function
15164 */
15165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015166f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015167 typval_T *argvars;
15168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015170 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015172 char_u *infile = NULL;
15173 char_u buf[NUMBUFLEN];
15174 int err = FALSE;
15175 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015177 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015178 {
15179 /*
15180 * Write the string to a temp file, to be used for input of the shell
15181 * command.
15182 */
15183 if ((infile = vim_tempname('i')) == NULL)
15184 {
15185 EMSG(_(e_notmp));
15186 return;
15187 }
15188
15189 fd = mch_fopen((char *)infile, WRITEBIN);
15190 if (fd == NULL)
15191 {
15192 EMSG2(_(e_notopen), infile);
15193 goto done;
15194 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015195 p = get_tv_string_buf_chk(&argvars[1], buf);
15196 if (p == NULL)
15197 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015198 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15199 err = TRUE;
15200 if (fclose(fd) != 0)
15201 err = TRUE;
15202 if (err)
15203 {
15204 EMSG(_("E677: Error writing temp file"));
15205 goto done;
15206 }
15207 }
15208
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015209 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15210 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015211
Bram Moolenaar071d4272004-06-13 20:20:40 +000015212#ifdef USE_CR
15213 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015214 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215 {
15216 char_u *s;
15217
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015218 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219 {
15220 if (*s == CAR)
15221 *s = NL;
15222 }
15223 }
15224#else
15225# ifdef USE_CRNL
15226 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015227 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228 {
15229 char_u *s, *d;
15230
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015231 d = res;
15232 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233 {
15234 if (s[0] == CAR && s[1] == NL)
15235 ++s;
15236 *d++ = *s;
15237 }
15238 *d = NUL;
15239 }
15240# endif
15241#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015242
15243done:
15244 if (infile != NULL)
15245 {
15246 mch_remove(infile);
15247 vim_free(infile);
15248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015249 rettv->v_type = VAR_STRING;
15250 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251}
15252
15253/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015254 * "tabpagebuflist()" function
15255 */
15256/* ARGSUSED */
15257 static void
15258f_tabpagebuflist(argvars, rettv)
15259 typval_T *argvars;
15260 typval_T *rettv;
15261{
15262#ifndef FEAT_WINDOWS
15263 rettv->vval.v_number = 0;
15264#else
15265 tabpage_T *tp;
15266 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015267
15268 if (argvars[0].v_type == VAR_UNKNOWN)
15269 wp = firstwin;
15270 else
15271 {
15272 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15273 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015274 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015275 }
15276 if (wp == NULL)
15277 rettv->vval.v_number = 0;
15278 else
15279 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015280 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015281 rettv->vval.v_number = 0;
15282 else
15283 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015284 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015285 if (list_append_number(rettv->vval.v_list,
15286 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015287 break;
15288 }
15289 }
15290#endif
15291}
15292
15293
15294/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015295 * "tabpagenr()" function
15296 */
15297/* ARGSUSED */
15298 static void
15299f_tabpagenr(argvars, rettv)
15300 typval_T *argvars;
15301 typval_T *rettv;
15302{
15303 int nr = 1;
15304#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015305 char_u *arg;
15306
15307 if (argvars[0].v_type != VAR_UNKNOWN)
15308 {
15309 arg = get_tv_string_chk(&argvars[0]);
15310 nr = 0;
15311 if (arg != NULL)
15312 {
15313 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015314 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015315 else
15316 EMSG2(_(e_invexpr2), arg);
15317 }
15318 }
15319 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015320 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015321#endif
15322 rettv->vval.v_number = nr;
15323}
15324
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015325
15326#ifdef FEAT_WINDOWS
15327static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15328
15329/*
15330 * Common code for tabpagewinnr() and winnr().
15331 */
15332 static int
15333get_winnr(tp, argvar)
15334 tabpage_T *tp;
15335 typval_T *argvar;
15336{
15337 win_T *twin;
15338 int nr = 1;
15339 win_T *wp;
15340 char_u *arg;
15341
15342 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15343 if (argvar->v_type != VAR_UNKNOWN)
15344 {
15345 arg = get_tv_string_chk(argvar);
15346 if (arg == NULL)
15347 nr = 0; /* type error; errmsg already given */
15348 else if (STRCMP(arg, "$") == 0)
15349 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15350 else if (STRCMP(arg, "#") == 0)
15351 {
15352 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15353 if (twin == NULL)
15354 nr = 0;
15355 }
15356 else
15357 {
15358 EMSG2(_(e_invexpr2), arg);
15359 nr = 0;
15360 }
15361 }
15362
15363 if (nr > 0)
15364 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15365 wp != twin; wp = wp->w_next)
15366 ++nr;
15367 return nr;
15368}
15369#endif
15370
15371/*
15372 * "tabpagewinnr()" function
15373 */
15374/* ARGSUSED */
15375 static void
15376f_tabpagewinnr(argvars, rettv)
15377 typval_T *argvars;
15378 typval_T *rettv;
15379{
15380 int nr = 1;
15381#ifdef FEAT_WINDOWS
15382 tabpage_T *tp;
15383
15384 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15385 if (tp == NULL)
15386 nr = 0;
15387 else
15388 nr = get_winnr(tp, &argvars[1]);
15389#endif
15390 rettv->vval.v_number = nr;
15391}
15392
15393
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015394/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015395 * "tagfiles()" function
15396 */
15397/*ARGSUSED*/
15398 static void
15399f_tagfiles(argvars, rettv)
15400 typval_T *argvars;
15401 typval_T *rettv;
15402{
15403 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015404 tagname_T tn;
15405 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015406
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015407 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015408 {
15409 rettv->vval.v_number = 0;
15410 return;
15411 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015412
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015413 for (first = TRUE; ; first = FALSE)
15414 if (get_tagfname(&tn, first, fname) == FAIL
15415 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015416 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015417 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015418}
15419
15420/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015421 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015422 */
15423 static void
15424f_taglist(argvars, rettv)
15425 typval_T *argvars;
15426 typval_T *rettv;
15427{
15428 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015429
15430 tag_pattern = get_tv_string(&argvars[0]);
15431
15432 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015433 if (*tag_pattern == NUL)
15434 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015435
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015436 if (rettv_list_alloc(rettv) == OK)
15437 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015438}
15439
15440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441 * "tempname()" function
15442 */
15443/*ARGSUSED*/
15444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015445f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015446 typval_T *argvars;
15447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448{
15449 static int x = 'A';
15450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015451 rettv->v_type = VAR_STRING;
15452 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453
15454 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15455 * names. Skip 'I' and 'O', they are used for shell redirection. */
15456 do
15457 {
15458 if (x == 'Z')
15459 x = '0';
15460 else if (x == '9')
15461 x = 'A';
15462 else
15463 {
15464#ifdef EBCDIC
15465 if (x == 'I')
15466 x = 'J';
15467 else if (x == 'R')
15468 x = 'S';
15469 else
15470#endif
15471 ++x;
15472 }
15473 } while (x == 'I' || x == 'O');
15474}
15475
15476/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015477 * "test(list)" function: Just checking the walls...
15478 */
15479/*ARGSUSED*/
15480 static void
15481f_test(argvars, rettv)
15482 typval_T *argvars;
15483 typval_T *rettv;
15484{
15485 /* Used for unit testing. Change the code below to your liking. */
15486#if 0
15487 listitem_T *li;
15488 list_T *l;
15489 char_u *bad, *good;
15490
15491 if (argvars[0].v_type != VAR_LIST)
15492 return;
15493 l = argvars[0].vval.v_list;
15494 if (l == NULL)
15495 return;
15496 li = l->lv_first;
15497 if (li == NULL)
15498 return;
15499 bad = get_tv_string(&li->li_tv);
15500 li = li->li_next;
15501 if (li == NULL)
15502 return;
15503 good = get_tv_string(&li->li_tv);
15504 rettv->vval.v_number = test_edit_score(bad, good);
15505#endif
15506}
15507
15508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 * "tolower(string)" function
15510 */
15511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015512f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015513 typval_T *argvars;
15514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515{
15516 char_u *p;
15517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015518 p = vim_strsave(get_tv_string(&argvars[0]));
15519 rettv->v_type = VAR_STRING;
15520 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521
15522 if (p != NULL)
15523 while (*p != NUL)
15524 {
15525#ifdef FEAT_MBYTE
15526 int l;
15527
15528 if (enc_utf8)
15529 {
15530 int c, lc;
15531
15532 c = utf_ptr2char(p);
15533 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015534 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 /* TODO: reallocate string when byte count changes. */
15536 if (utf_char2len(lc) == l)
15537 utf_char2bytes(lc, p);
15538 p += l;
15539 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015540 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541 p += l; /* skip multi-byte character */
15542 else
15543#endif
15544 {
15545 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15546 ++p;
15547 }
15548 }
15549}
15550
15551/*
15552 * "toupper(string)" function
15553 */
15554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015555f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015556 typval_T *argvars;
15557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015559 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015560 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561}
15562
15563/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015564 * "tr(string, fromstr, tostr)" function
15565 */
15566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015567f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015568 typval_T *argvars;
15569 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015570{
15571 char_u *instr;
15572 char_u *fromstr;
15573 char_u *tostr;
15574 char_u *p;
15575#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015576 int inlen;
15577 int fromlen;
15578 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015579 int idx;
15580 char_u *cpstr;
15581 int cplen;
15582 int first = TRUE;
15583#endif
15584 char_u buf[NUMBUFLEN];
15585 char_u buf2[NUMBUFLEN];
15586 garray_T ga;
15587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015588 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015589 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15590 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015591
15592 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015593 rettv->v_type = VAR_STRING;
15594 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015595 if (fromstr == NULL || tostr == NULL)
15596 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015597 ga_init2(&ga, (int)sizeof(char), 80);
15598
15599#ifdef FEAT_MBYTE
15600 if (!has_mbyte)
15601#endif
15602 /* not multi-byte: fromstr and tostr must be the same length */
15603 if (STRLEN(fromstr) != STRLEN(tostr))
15604 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015605#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015606error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015607#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015608 EMSG2(_(e_invarg2), fromstr);
15609 ga_clear(&ga);
15610 return;
15611 }
15612
15613 /* fromstr and tostr have to contain the same number of chars */
15614 while (*instr != NUL)
15615 {
15616#ifdef FEAT_MBYTE
15617 if (has_mbyte)
15618 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015619 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015620 cpstr = instr;
15621 cplen = inlen;
15622 idx = 0;
15623 for (p = fromstr; *p != NUL; p += fromlen)
15624 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015625 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015626 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15627 {
15628 for (p = tostr; *p != NUL; p += tolen)
15629 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015630 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015631 if (idx-- == 0)
15632 {
15633 cplen = tolen;
15634 cpstr = p;
15635 break;
15636 }
15637 }
15638 if (*p == NUL) /* tostr is shorter than fromstr */
15639 goto error;
15640 break;
15641 }
15642 ++idx;
15643 }
15644
15645 if (first && cpstr == instr)
15646 {
15647 /* Check that fromstr and tostr have the same number of
15648 * (multi-byte) characters. Done only once when a character
15649 * of instr doesn't appear in fromstr. */
15650 first = FALSE;
15651 for (p = tostr; *p != NUL; p += tolen)
15652 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015653 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015654 --idx;
15655 }
15656 if (idx != 0)
15657 goto error;
15658 }
15659
15660 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015661 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015662 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015663
15664 instr += inlen;
15665 }
15666 else
15667#endif
15668 {
15669 /* When not using multi-byte chars we can do it faster. */
15670 p = vim_strchr(fromstr, *instr);
15671 if (p != NULL)
15672 ga_append(&ga, tostr[p - fromstr]);
15673 else
15674 ga_append(&ga, *instr);
15675 ++instr;
15676 }
15677 }
15678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015679 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015680}
15681
15682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683 * "type(expr)" function
15684 */
15685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015686f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015687 typval_T *argvars;
15688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015690 int n;
15691
15692 switch (argvars[0].v_type)
15693 {
15694 case VAR_NUMBER: n = 0; break;
15695 case VAR_STRING: n = 1; break;
15696 case VAR_FUNC: n = 2; break;
15697 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015698 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015699 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15700 }
15701 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702}
15703
15704/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015705 * "values(dict)" function
15706 */
15707 static void
15708f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015709 typval_T *argvars;
15710 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015711{
15712 dict_list(argvars, rettv, 1);
15713}
15714
15715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 * "virtcol(string)" function
15717 */
15718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015719f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015720 typval_T *argvars;
15721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722{
15723 colnr_T vcol = 0;
15724 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015725 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015727 fp = var2fpos(&argvars[0], FALSE, &fnum);
15728 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15729 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730 {
15731 getvvcol(curwin, fp, NULL, NULL, &vcol);
15732 ++vcol;
15733 }
15734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015735 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736}
15737
15738/*
15739 * "visualmode()" function
15740 */
15741/*ARGSUSED*/
15742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015743f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015744 typval_T *argvars;
15745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746{
15747#ifdef FEAT_VISUAL
15748 char_u str[2];
15749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015750 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751 str[0] = curbuf->b_visual_mode_eval;
15752 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015753 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754
15755 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015756 if ((argvars[0].v_type == VAR_NUMBER
15757 && argvars[0].vval.v_number != 0)
15758 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015759 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 curbuf->b_visual_mode_eval = NUL;
15761#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015762 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763#endif
15764}
15765
15766/*
15767 * "winbufnr(nr)" function
15768 */
15769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015770f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015771 typval_T *argvars;
15772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773{
15774 win_T *wp;
15775
15776 wp = find_win_by_nr(&argvars[0]);
15777 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015778 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015780 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781}
15782
15783/*
15784 * "wincol()" function
15785 */
15786/*ARGSUSED*/
15787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015788f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015789 typval_T *argvars;
15790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791{
15792 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015793 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794}
15795
15796/*
15797 * "winheight(nr)" function
15798 */
15799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015800f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015801 typval_T *argvars;
15802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803{
15804 win_T *wp;
15805
15806 wp = find_win_by_nr(&argvars[0]);
15807 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015808 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015810 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015811}
15812
15813/*
15814 * "winline()" function
15815 */
15816/*ARGSUSED*/
15817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015818f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015819 typval_T *argvars;
15820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821{
15822 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015823 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824}
15825
15826/*
15827 * "winnr()" function
15828 */
15829/* ARGSUSED */
15830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015831f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015832 typval_T *argvars;
15833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015834{
15835 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015836
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015838 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015840 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841}
15842
15843/*
15844 * "winrestcmd()" function
15845 */
15846/* ARGSUSED */
15847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015848f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015849 typval_T *argvars;
15850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851{
15852#ifdef FEAT_WINDOWS
15853 win_T *wp;
15854 int winnr = 1;
15855 garray_T ga;
15856 char_u buf[50];
15857
15858 ga_init2(&ga, (int)sizeof(char), 70);
15859 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15860 {
15861 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15862 ga_concat(&ga, buf);
15863# ifdef FEAT_VERTSPLIT
15864 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15865 ga_concat(&ga, buf);
15866# endif
15867 ++winnr;
15868 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015869 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015871 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015873 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015875 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876}
15877
15878/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015879 * "winrestview()" function
15880 */
15881/* ARGSUSED */
15882 static void
15883f_winrestview(argvars, rettv)
15884 typval_T *argvars;
15885 typval_T *rettv;
15886{
15887 dict_T *dict;
15888
15889 if (argvars[0].v_type != VAR_DICT
15890 || (dict = argvars[0].vval.v_dict) == NULL)
15891 EMSG(_(e_invarg));
15892 else
15893 {
15894 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
15895 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
15896#ifdef FEAT_VIRTUALEDIT
15897 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
15898#endif
15899 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015900 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015901
15902 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
15903#ifdef FEAT_DIFF
15904 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
15905#endif
15906 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
15907 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
15908
15909 check_cursor();
15910 changed_cline_bef_curs();
15911 invalidate_botline();
15912 redraw_later(VALID);
15913
15914 if (curwin->w_topline == 0)
15915 curwin->w_topline = 1;
15916 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15917 curwin->w_topline = curbuf->b_ml.ml_line_count;
15918#ifdef FEAT_DIFF
15919 check_topfill(curwin, TRUE);
15920#endif
15921 }
15922}
15923
15924/*
15925 * "winsaveview()" function
15926 */
15927/* ARGSUSED */
15928 static void
15929f_winsaveview(argvars, rettv)
15930 typval_T *argvars;
15931 typval_T *rettv;
15932{
15933 dict_T *dict;
15934
15935 dict = dict_alloc();
15936 if (dict == NULL)
15937 return;
15938 rettv->v_type = VAR_DICT;
15939 rettv->vval.v_dict = dict;
15940 ++dict->dv_refcount;
15941
15942 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
15943 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
15944#ifdef FEAT_VIRTUALEDIT
15945 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
15946#endif
15947 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
15948
15949 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
15950#ifdef FEAT_DIFF
15951 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
15952#endif
15953 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
15954 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
15955}
15956
15957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958 * "winwidth(nr)" function
15959 */
15960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015961f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015962 typval_T *argvars;
15963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964{
15965 win_T *wp;
15966
15967 wp = find_win_by_nr(&argvars[0]);
15968 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015969 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015970 else
15971#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015972 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015974 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015975#endif
15976}
15977
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015979 * "writefile()" function
15980 */
15981 static void
15982f_writefile(argvars, rettv)
15983 typval_T *argvars;
15984 typval_T *rettv;
15985{
15986 int binary = FALSE;
15987 char_u *fname;
15988 FILE *fd;
15989 listitem_T *li;
15990 char_u *s;
15991 int ret = 0;
15992 int c;
15993
15994 if (argvars[0].v_type != VAR_LIST)
15995 {
15996 EMSG2(_(e_listarg), "writefile()");
15997 return;
15998 }
15999 if (argvars[0].vval.v_list == NULL)
16000 return;
16001
16002 if (argvars[2].v_type != VAR_UNKNOWN
16003 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16004 binary = TRUE;
16005
16006 /* Always open the file in binary mode, library functions have a mind of
16007 * their own about CR-LF conversion. */
16008 fname = get_tv_string(&argvars[1]);
16009 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16010 {
16011 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16012 ret = -1;
16013 }
16014 else
16015 {
16016 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16017 li = li->li_next)
16018 {
16019 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16020 {
16021 if (*s == '\n')
16022 c = putc(NUL, fd);
16023 else
16024 c = putc(*s, fd);
16025 if (c == EOF)
16026 {
16027 ret = -1;
16028 break;
16029 }
16030 }
16031 if (!binary || li->li_next != NULL)
16032 if (putc('\n', fd) == EOF)
16033 {
16034 ret = -1;
16035 break;
16036 }
16037 if (ret < 0)
16038 {
16039 EMSG(_(e_write));
16040 break;
16041 }
16042 }
16043 fclose(fd);
16044 }
16045
16046 rettv->vval.v_number = ret;
16047}
16048
16049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016051 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052 */
16053 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016054var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016055 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016057 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016059 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016061 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062
Bram Moolenaara5525202006-03-02 22:52:09 +000016063 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016064 if (varp->v_type == VAR_LIST)
16065 {
16066 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016067 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016068 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016069
16070 l = varp->vval.v_list;
16071 if (l == NULL)
16072 return NULL;
16073
16074 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016075 pos.lnum = list_find_nr(l, 0L, &error);
16076 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016077 return NULL; /* invalid line number */
16078
16079 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016080 pos.col = list_find_nr(l, 1L, &error);
16081 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016082 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016083 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016084 /* Accept a position up to the NUL after the line. */
16085 if (pos.col <= 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016086 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016087 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016088
Bram Moolenaara5525202006-03-02 22:52:09 +000016089#ifdef FEAT_VIRTUALEDIT
16090 /* Get the virtual offset. Defaults to zero. */
16091 pos.coladd = list_find_nr(l, 2L, &error);
16092 if (error)
16093 pos.coladd = 0;
16094#endif
16095
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016096 return &pos;
16097 }
16098
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016099 name = get_tv_string_chk(varp);
16100 if (name == NULL)
16101 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102 if (name[0] == '.') /* cursor */
16103 return &curwin->w_cursor;
16104 if (name[0] == '\'') /* mark */
16105 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016106 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16108 return NULL;
16109 return pp;
16110 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016111
16112#ifdef FEAT_VIRTUALEDIT
16113 pos.coladd = 0;
16114#endif
16115
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016116 if (name[0] == 'w' && lnum)
16117 {
16118 pos.col = 0;
16119 if (name[1] == '0') /* "w0": first visible line */
16120 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016121 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016122 pos.lnum = curwin->w_topline;
16123 return &pos;
16124 }
16125 else if (name[1] == '$') /* "w$": last visible line */
16126 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016127 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016128 pos.lnum = curwin->w_botline - 1;
16129 return &pos;
16130 }
16131 }
16132 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133 {
16134 if (lnum)
16135 {
16136 pos.lnum = curbuf->b_ml.ml_line_count;
16137 pos.col = 0;
16138 }
16139 else
16140 {
16141 pos.lnum = curwin->w_cursor.lnum;
16142 pos.col = (colnr_T)STRLEN(ml_get_curline());
16143 }
16144 return &pos;
16145 }
16146 return NULL;
16147}
16148
16149/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016150 * Convert list in "arg" into a position and optional file number.
16151 * When "fnump" is NULL there is no file number, only 3 items.
16152 * Note that the column is passed on as-is, the caller may want to decrement
16153 * it to use 1 for the first column.
16154 * Return FAIL when conversion is not possible, doesn't check the position for
16155 * validity.
16156 */
16157 static int
16158list2fpos(arg, posp, fnump)
16159 typval_T *arg;
16160 pos_T *posp;
16161 int *fnump;
16162{
16163 list_T *l = arg->vval.v_list;
16164 long i = 0;
16165 long n;
16166
16167 /* List must be: [fnum, lnum, col, coladd] */
16168 if (arg->v_type != VAR_LIST || l == NULL
16169 || l->lv_len != (fnump == NULL ? 3 : 4))
16170 return FAIL;
16171
16172 if (fnump != NULL)
16173 {
16174 n = list_find_nr(l, i++, NULL); /* fnum */
16175 if (n < 0)
16176 return FAIL;
16177 if (n == 0)
16178 n = curbuf->b_fnum; /* current buffer */
16179 *fnump = n;
16180 }
16181
16182 n = list_find_nr(l, i++, NULL); /* lnum */
16183 if (n < 0)
16184 return FAIL;
16185 posp->lnum = n;
16186
16187 n = list_find_nr(l, i++, NULL); /* col */
16188 if (n < 0)
16189 return FAIL;
16190 posp->col = n;
16191
16192#ifdef FEAT_VIRTUALEDIT
16193 n = list_find_nr(l, i, NULL);
16194 if (n < 0)
16195 return FAIL;
16196 posp->coladd = n;
16197#endif
16198
16199 return OK;
16200}
16201
16202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203 * Get the length of an environment variable name.
16204 * Advance "arg" to the first character after the name.
16205 * Return 0 for error.
16206 */
16207 static int
16208get_env_len(arg)
16209 char_u **arg;
16210{
16211 char_u *p;
16212 int len;
16213
16214 for (p = *arg; vim_isIDc(*p); ++p)
16215 ;
16216 if (p == *arg) /* no name found */
16217 return 0;
16218
16219 len = (int)(p - *arg);
16220 *arg = p;
16221 return len;
16222}
16223
16224/*
16225 * Get the length of the name of a function or internal variable.
16226 * "arg" is advanced to the first non-white character after the name.
16227 * Return 0 if something is wrong.
16228 */
16229 static int
16230get_id_len(arg)
16231 char_u **arg;
16232{
16233 char_u *p;
16234 int len;
16235
16236 /* Find the end of the name. */
16237 for (p = *arg; eval_isnamec(*p); ++p)
16238 ;
16239 if (p == *arg) /* no name found */
16240 return 0;
16241
16242 len = (int)(p - *arg);
16243 *arg = skipwhite(p);
16244
16245 return len;
16246}
16247
16248/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016249 * Get the length of the name of a variable or function.
16250 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016252 * Return -1 if curly braces expansion failed.
16253 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 * If the name contains 'magic' {}'s, expand them and return the
16255 * expanded name in an allocated string via 'alias' - caller must free.
16256 */
16257 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016258get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259 char_u **arg;
16260 char_u **alias;
16261 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016262 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263{
16264 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265 char_u *p;
16266 char_u *expr_start;
16267 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268
16269 *alias = NULL; /* default to no alias */
16270
16271 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16272 && (*arg)[2] == (int)KE_SNR)
16273 {
16274 /* hard coded <SNR>, already translated */
16275 *arg += 3;
16276 return get_id_len(arg) + 3;
16277 }
16278 len = eval_fname_script(*arg);
16279 if (len > 0)
16280 {
16281 /* literal "<SID>", "s:" or "<SNR>" */
16282 *arg += len;
16283 }
16284
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016286 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016288 p = find_name_end(*arg, &expr_start, &expr_end,
16289 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290 if (expr_start != NULL)
16291 {
16292 char_u *temp_string;
16293
16294 if (!evaluate)
16295 {
16296 len += (int)(p - *arg);
16297 *arg = skipwhite(p);
16298 return len;
16299 }
16300
16301 /*
16302 * Include any <SID> etc in the expanded string:
16303 * Thus the -len here.
16304 */
16305 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16306 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016307 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 *alias = temp_string;
16309 *arg = skipwhite(p);
16310 return (int)STRLEN(temp_string);
16311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312
16313 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016314 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315 EMSG2(_(e_invexpr2), *arg);
16316
16317 return len;
16318}
16319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016320/*
16321 * Find the end of a variable or function name, taking care of magic braces.
16322 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16323 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016324 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016325 * Return a pointer to just after the name. Equal to "arg" if there is no
16326 * valid name.
16327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016329find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 char_u *arg;
16331 char_u **expr_start;
16332 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016333 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016335 int mb_nest = 0;
16336 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 char_u *p;
16338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016339 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016341 *expr_start = NULL;
16342 *expr_end = NULL;
16343 }
16344
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016345 /* Quick check for valid starting character. */
16346 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16347 return arg;
16348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016349 for (p = arg; *p != NUL
16350 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016351 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016352 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016353 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016354 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016355 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016356 if (*p == '\'')
16357 {
16358 /* skip over 'string' to avoid counting [ and ] inside it. */
16359 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16360 ;
16361 if (*p == NUL)
16362 break;
16363 }
16364 else if (*p == '"')
16365 {
16366 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16367 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16368 if (*p == '\\' && p[1] != NUL)
16369 ++p;
16370 if (*p == NUL)
16371 break;
16372 }
16373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016374 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016376 if (*p == '[')
16377 ++br_nest;
16378 else if (*p == ']')
16379 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016382 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016384 if (*p == '{')
16385 {
16386 mb_nest++;
16387 if (expr_start != NULL && *expr_start == NULL)
16388 *expr_start = p;
16389 }
16390 else if (*p == '}')
16391 {
16392 mb_nest--;
16393 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16394 *expr_end = p;
16395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397 }
16398
16399 return p;
16400}
16401
16402/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016403 * Expands out the 'magic' {}'s in a variable/function name.
16404 * Note that this can call itself recursively, to deal with
16405 * constructs like foo{bar}{baz}{bam}
16406 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16407 * "in_start" ^
16408 * "expr_start" ^
16409 * "expr_end" ^
16410 * "in_end" ^
16411 *
16412 * Returns a new allocated string, which the caller must free.
16413 * Returns NULL for failure.
16414 */
16415 static char_u *
16416make_expanded_name(in_start, expr_start, expr_end, in_end)
16417 char_u *in_start;
16418 char_u *expr_start;
16419 char_u *expr_end;
16420 char_u *in_end;
16421{
16422 char_u c1;
16423 char_u *retval = NULL;
16424 char_u *temp_result;
16425 char_u *nextcmd = NULL;
16426
16427 if (expr_end == NULL || in_end == NULL)
16428 return NULL;
16429 *expr_start = NUL;
16430 *expr_end = NUL;
16431 c1 = *in_end;
16432 *in_end = NUL;
16433
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016434 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016435 if (temp_result != NULL && nextcmd == NULL)
16436 {
16437 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16438 + (in_end - expr_end) + 1));
16439 if (retval != NULL)
16440 {
16441 STRCPY(retval, in_start);
16442 STRCAT(retval, temp_result);
16443 STRCAT(retval, expr_end + 1);
16444 }
16445 }
16446 vim_free(temp_result);
16447
16448 *in_end = c1; /* put char back for error messages */
16449 *expr_start = '{';
16450 *expr_end = '}';
16451
16452 if (retval != NULL)
16453 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016454 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016455 if (expr_start != NULL)
16456 {
16457 /* Further expansion! */
16458 temp_result = make_expanded_name(retval, expr_start,
16459 expr_end, temp_result);
16460 vim_free(retval);
16461 retval = temp_result;
16462 }
16463 }
16464
16465 return retval;
16466}
16467
16468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016470 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 */
16472 static int
16473eval_isnamec(c)
16474 int c;
16475{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016476 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16477}
16478
16479/*
16480 * Return TRUE if character "c" can be used as the first character in a
16481 * variable or function name (excluding '{' and '}').
16482 */
16483 static int
16484eval_isnamec1(c)
16485 int c;
16486{
16487 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488}
16489
16490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491 * Set number v: variable to "val".
16492 */
16493 void
16494set_vim_var_nr(idx, val)
16495 int idx;
16496 long val;
16497{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016498 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499}
16500
16501/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016502 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 */
16504 long
16505get_vim_var_nr(idx)
16506 int idx;
16507{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016508 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509}
16510
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016511#if defined(FEAT_AUTOCMD) || defined(PROTO)
16512/*
16513 * Get string v: variable value. Uses a static buffer, can only be used once.
16514 */
16515 char_u *
16516get_vim_var_str(idx)
16517 int idx;
16518{
16519 return get_tv_string(&vimvars[idx].vv_tv);
16520}
16521#endif
16522
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523/*
16524 * Set v:count, v:count1 and v:prevcount.
16525 */
16526 void
16527set_vcount(count, count1)
16528 long count;
16529 long count1;
16530{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016531 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16532 vimvars[VV_COUNT].vv_nr = count;
16533 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534}
16535
16536/*
16537 * Set string v: variable to a copy of "val".
16538 */
16539 void
16540set_vim_var_string(idx, val, len)
16541 int idx;
16542 char_u *val;
16543 int len; /* length of "val" to use or -1 (whole string) */
16544{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016545 /* Need to do this (at least) once, since we can't initialize a union.
16546 * Will always be invoked when "v:progname" is set. */
16547 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16548
Bram Moolenaare9a41262005-01-15 22:18:47 +000016549 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016551 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016553 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016555 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556}
16557
16558/*
16559 * Set v:register if needed.
16560 */
16561 void
16562set_reg_var(c)
16563 int c;
16564{
16565 char_u regname;
16566
16567 if (c == 0 || c == ' ')
16568 regname = '"';
16569 else
16570 regname = c;
16571 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016572 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573 set_vim_var_string(VV_REG, &regname, 1);
16574}
16575
16576/*
16577 * Get or set v:exception. If "oldval" == NULL, return the current value.
16578 * Otherwise, restore the value to "oldval" and return NULL.
16579 * Must always be called in pairs to save and restore v:exception! Does not
16580 * take care of memory allocations.
16581 */
16582 char_u *
16583v_exception(oldval)
16584 char_u *oldval;
16585{
16586 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016587 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588
Bram Moolenaare9a41262005-01-15 22:18:47 +000016589 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590 return NULL;
16591}
16592
16593/*
16594 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16595 * Otherwise, restore the value to "oldval" and return NULL.
16596 * Must always be called in pairs to save and restore v:throwpoint! Does not
16597 * take care of memory allocations.
16598 */
16599 char_u *
16600v_throwpoint(oldval)
16601 char_u *oldval;
16602{
16603 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016604 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605
Bram Moolenaare9a41262005-01-15 22:18:47 +000016606 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607 return NULL;
16608}
16609
16610#if defined(FEAT_AUTOCMD) || defined(PROTO)
16611/*
16612 * Set v:cmdarg.
16613 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16614 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16615 * Must always be called in pairs!
16616 */
16617 char_u *
16618set_cmdarg(eap, oldarg)
16619 exarg_T *eap;
16620 char_u *oldarg;
16621{
16622 char_u *oldval;
16623 char_u *newval;
16624 unsigned len;
16625
Bram Moolenaare9a41262005-01-15 22:18:47 +000016626 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016627 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016629 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016630 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016631 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 }
16633
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016634 if (eap->force_bin == FORCE_BIN)
16635 len = 6;
16636 else if (eap->force_bin == FORCE_NOBIN)
16637 len = 8;
16638 else
16639 len = 0;
16640 if (eap->force_ff != 0)
16641 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16642# ifdef FEAT_MBYTE
16643 if (eap->force_enc != 0)
16644 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016645 if (eap->bad_char != 0)
16646 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016647# endif
16648
16649 newval = alloc(len + 1);
16650 if (newval == NULL)
16651 return NULL;
16652
16653 if (eap->force_bin == FORCE_BIN)
16654 sprintf((char *)newval, " ++bin");
16655 else if (eap->force_bin == FORCE_NOBIN)
16656 sprintf((char *)newval, " ++nobin");
16657 else
16658 *newval = NUL;
16659 if (eap->force_ff != 0)
16660 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16661 eap->cmd + eap->force_ff);
16662# ifdef FEAT_MBYTE
16663 if (eap->force_enc != 0)
16664 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16665 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016666 if (eap->bad_char != 0)
16667 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16668 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016669# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016670 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016671 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672}
16673#endif
16674
16675/*
16676 * Get the value of internal variable "name".
16677 * Return OK or FAIL.
16678 */
16679 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016680get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681 char_u *name;
16682 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016683 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016684 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685{
16686 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016687 typval_T *tv = NULL;
16688 typval_T atv;
16689 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016690 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691
16692 /* truncate the name, so that we can use strcmp() */
16693 cc = name[len];
16694 name[len] = NUL;
16695
16696 /*
16697 * Check for "b:changedtick".
16698 */
16699 if (STRCMP(name, "b:changedtick") == 0)
16700 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016701 atv.v_type = VAR_NUMBER;
16702 atv.vval.v_number = curbuf->b_changedtick;
16703 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 }
16705
16706 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707 * Check for user-defined variables.
16708 */
16709 else
16710 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016711 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016713 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714 }
16715
Bram Moolenaare9a41262005-01-15 22:18:47 +000016716 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016718 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016719 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 ret = FAIL;
16721 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016722 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016723 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724
16725 name[len] = cc;
16726
16727 return ret;
16728}
16729
16730/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016731 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16732 * Also handle function call with Funcref variable: func(expr)
16733 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16734 */
16735 static int
16736handle_subscript(arg, rettv, evaluate, verbose)
16737 char_u **arg;
16738 typval_T *rettv;
16739 int evaluate; /* do more than finding the end */
16740 int verbose; /* give error messages */
16741{
16742 int ret = OK;
16743 dict_T *selfdict = NULL;
16744 char_u *s;
16745 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016746 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016747
16748 while (ret == OK
16749 && (**arg == '['
16750 || (**arg == '.' && rettv->v_type == VAR_DICT)
16751 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16752 && !vim_iswhite(*(*arg - 1)))
16753 {
16754 if (**arg == '(')
16755 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016756 /* need to copy the funcref so that we can clear rettv */
16757 functv = *rettv;
16758 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016759
16760 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016761 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016762 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016763 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16764 &len, evaluate, selfdict);
16765
16766 /* Clear the funcref afterwards, so that deleting it while
16767 * evaluating the arguments is possible (see test55). */
16768 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016769
16770 /* Stop the expression evaluation when immediately aborting on
16771 * error, or when an interrupt occurred or an exception was thrown
16772 * but not caught. */
16773 if (aborting())
16774 {
16775 if (ret == OK)
16776 clear_tv(rettv);
16777 ret = FAIL;
16778 }
16779 dict_unref(selfdict);
16780 selfdict = NULL;
16781 }
16782 else /* **arg == '[' || **arg == '.' */
16783 {
16784 dict_unref(selfdict);
16785 if (rettv->v_type == VAR_DICT)
16786 {
16787 selfdict = rettv->vval.v_dict;
16788 if (selfdict != NULL)
16789 ++selfdict->dv_refcount;
16790 }
16791 else
16792 selfdict = NULL;
16793 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16794 {
16795 clear_tv(rettv);
16796 ret = FAIL;
16797 }
16798 }
16799 }
16800 dict_unref(selfdict);
16801 return ret;
16802}
16803
16804/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016805 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16806 * value).
16807 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016808 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016809alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016810{
Bram Moolenaar33570922005-01-25 22:26:29 +000016811 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016812}
16813
16814/*
16815 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 * The string "s" must have been allocated, it is consumed.
16817 * Return NULL for out of memory, the variable otherwise.
16818 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016819 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016820alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 char_u *s;
16822{
Bram Moolenaar33570922005-01-25 22:26:29 +000016823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016825 rettv = alloc_tv();
16826 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016828 rettv->v_type = VAR_STRING;
16829 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830 }
16831 else
16832 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016833 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834}
16835
16836/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016837 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016839 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016840free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016841 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842{
16843 if (varp != NULL)
16844 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016845 switch (varp->v_type)
16846 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016847 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016848 func_unref(varp->vval.v_string);
16849 /*FALLTHROUGH*/
16850 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016851 vim_free(varp->vval.v_string);
16852 break;
16853 case VAR_LIST:
16854 list_unref(varp->vval.v_list);
16855 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016856 case VAR_DICT:
16857 dict_unref(varp->vval.v_dict);
16858 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016859 case VAR_NUMBER:
16860 case VAR_UNKNOWN:
16861 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016862 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016863 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016864 break;
16865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866 vim_free(varp);
16867 }
16868}
16869
16870/*
16871 * Free the memory for a variable value and set the value to NULL or 0.
16872 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016873 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016874clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016875 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876{
16877 if (varp != NULL)
16878 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016879 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016881 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016882 func_unref(varp->vval.v_string);
16883 /*FALLTHROUGH*/
16884 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016885 vim_free(varp->vval.v_string);
16886 varp->vval.v_string = NULL;
16887 break;
16888 case VAR_LIST:
16889 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016890 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016891 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016892 case VAR_DICT:
16893 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016894 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016895 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016896 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016897 varp->vval.v_number = 0;
16898 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016899 case VAR_UNKNOWN:
16900 break;
16901 default:
16902 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016904 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905 }
16906}
16907
16908/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016909 * Set the value of a variable to NULL without freeing items.
16910 */
16911 static void
16912init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016913 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016914{
16915 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016916 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016917}
16918
16919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920 * Get the number value of a variable.
16921 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016922 * For incompatible types, return 0.
16923 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16924 * caller of incompatible types: it sets *denote to TRUE if "denote"
16925 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926 */
16927 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016928get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016929 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016931 int error = FALSE;
16932
16933 return get_tv_number_chk(varp, &error); /* return 0L on error */
16934}
16935
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016936 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016937get_tv_number_chk(varp, denote)
16938 typval_T *varp;
16939 int *denote;
16940{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016941 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016943 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016945 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016946 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016947 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016948 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016949 break;
16950 case VAR_STRING:
16951 if (varp->vval.v_string != NULL)
16952 vim_str2nr(varp->vval.v_string, NULL, NULL,
16953 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016954 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016955 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016956 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016957 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016958 case VAR_DICT:
16959 EMSG(_("E728: Using a Dictionary as a number"));
16960 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016961 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016962 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016963 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016965 if (denote == NULL) /* useful for values that must be unsigned */
16966 n = -1;
16967 else
16968 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016969 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970}
16971
16972/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016973 * Get the lnum from the first argument.
16974 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016975 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 */
16977 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016978get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016979 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980{
Bram Moolenaar33570922005-01-25 22:26:29 +000016981 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982 linenr_T lnum;
16983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016984 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985 if (lnum == 0) /* no valid number, try using line() */
16986 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016987 rettv.v_type = VAR_NUMBER;
16988 f_line(argvars, &rettv);
16989 lnum = rettv.vval.v_number;
16990 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 }
16992 return lnum;
16993}
16994
16995/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016996 * Get the lnum from the first argument.
16997 * Also accepts "$", then "buf" is used.
16998 * Returns 0 on error.
16999 */
17000 static linenr_T
17001get_tv_lnum_buf(argvars, buf)
17002 typval_T *argvars;
17003 buf_T *buf;
17004{
17005 if (argvars[0].v_type == VAR_STRING
17006 && argvars[0].vval.v_string != NULL
17007 && argvars[0].vval.v_string[0] == '$'
17008 && buf != NULL)
17009 return buf->b_ml.ml_line_count;
17010 return get_tv_number_chk(&argvars[0], NULL);
17011}
17012
17013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014 * Get the string value of a variable.
17015 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017016 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17017 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 * If the String variable has never been set, return an empty string.
17019 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017020 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17021 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022 */
17023 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017024get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017025 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026{
17027 static char_u mybuf[NUMBUFLEN];
17028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017029 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030}
17031
17032 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017033get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017034 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017035 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017037 char_u *res = get_tv_string_buf_chk(varp, buf);
17038
17039 return res != NULL ? res : (char_u *)"";
17040}
17041
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017042 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017043get_tv_string_chk(varp)
17044 typval_T *varp;
17045{
17046 static char_u mybuf[NUMBUFLEN];
17047
17048 return get_tv_string_buf_chk(varp, mybuf);
17049}
17050
17051 static char_u *
17052get_tv_string_buf_chk(varp, buf)
17053 typval_T *varp;
17054 char_u *buf;
17055{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017056 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017058 case VAR_NUMBER:
17059 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17060 return buf;
17061 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017062 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017063 break;
17064 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017065 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017066 break;
17067 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017068 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017069 break;
17070 case VAR_STRING:
17071 if (varp->vval.v_string != NULL)
17072 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017073 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017074 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017075 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017077 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017078 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079}
17080
17081/*
17082 * Find variable "name" in the list of variables.
17083 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017084 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017085 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017086 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017088 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017089find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017091 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017094 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095
Bram Moolenaara7043832005-01-21 11:56:39 +000017096 ht = find_var_ht(name, &varname);
17097 if (htp != NULL)
17098 *htp = ht;
17099 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017101 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102}
17103
17104/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017105 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017106 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017108 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017109find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017110 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017111 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017112 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017113{
Bram Moolenaar33570922005-01-25 22:26:29 +000017114 hashitem_T *hi;
17115
17116 if (*varname == NUL)
17117 {
17118 /* Must be something like "s:", otherwise "ht" would be NULL. */
17119 switch (varname[-2])
17120 {
17121 case 's': return &SCRIPT_SV(current_SID).sv_var;
17122 case 'g': return &globvars_var;
17123 case 'v': return &vimvars_var;
17124 case 'b': return &curbuf->b_bufvar;
17125 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017126 case 'l': return current_funccal == NULL
17127 ? NULL : &current_funccal->l_vars_var;
17128 case 'a': return current_funccal == NULL
17129 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017130 }
17131 return NULL;
17132 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017133
17134 hi = hash_find(ht, varname);
17135 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017136 {
17137 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017138 * worked find the variable again. Don't auto-load a script if it was
17139 * loaded already, otherwise it would be loaded every time when
17140 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017141 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017142 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017143 hi = hash_find(ht, varname);
17144 if (HASHITEM_EMPTY(hi))
17145 return NULL;
17146 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017147 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017148}
17149
17150/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017151 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017152 * Set "varname" to the start of name without ':'.
17153 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017154 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017155find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156 char_u *name;
17157 char_u **varname;
17158{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017159 hashitem_T *hi;
17160
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161 if (name[1] != ':')
17162 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017163 /* The name must not start with a colon or #. */
17164 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165 return NULL;
17166 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017167
17168 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017169 hi = hash_find(&compat_hashtab, name);
17170 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017171 return &compat_hashtab;
17172
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017174 return &globvarht; /* global variable */
17175 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 }
17177 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017178 if (*name == 'g') /* global variable */
17179 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017180 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17181 */
17182 if (vim_strchr(name + 2, ':') != NULL
17183 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017184 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017186 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017188 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000017189 if (*name == 'v') /* v: variable */
17190 return &vimvarht;
17191 if (*name == 'a' && current_funccal != NULL) /* function argument */
17192 return &current_funccal->l_avars.dv_hashtab;
17193 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17194 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195 if (*name == 's' /* script variable */
17196 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17197 return &SCRIPT_VARS(current_SID);
17198 return NULL;
17199}
17200
17201/*
17202 * Get the string value of a (global/local) variable.
17203 * Returns NULL when it doesn't exist.
17204 */
17205 char_u *
17206get_var_value(name)
17207 char_u *name;
17208{
Bram Moolenaar33570922005-01-25 22:26:29 +000017209 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210
Bram Moolenaara7043832005-01-21 11:56:39 +000017211 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212 if (v == NULL)
17213 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017214 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215}
17216
17217/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017218 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219 * sourcing this script and when executing functions defined in the script.
17220 */
17221 void
17222new_script_vars(id)
17223 scid_T id;
17224{
Bram Moolenaara7043832005-01-21 11:56:39 +000017225 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017226 hashtab_T *ht;
17227 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017228
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17230 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017231 /* Re-allocating ga_data means that an ht_array pointing to
17232 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017233 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017234 for (i = 1; i <= ga_scripts.ga_len; ++i)
17235 {
17236 ht = &SCRIPT_VARS(i);
17237 if (ht->ht_mask == HT_INIT_SIZE - 1)
17238 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017239 sv = &SCRIPT_SV(i);
17240 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017241 }
17242
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243 while (ga_scripts.ga_len < id)
17244 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017245 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17246 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017247 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 }
17249 }
17250}
17251
17252/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017253 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17254 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017255 */
17256 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017257init_var_dict(dict, dict_var)
17258 dict_T *dict;
17259 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260{
Bram Moolenaar33570922005-01-25 22:26:29 +000017261 hash_init(&dict->dv_hashtab);
17262 dict->dv_refcount = 99999;
17263 dict_var->di_tv.vval.v_dict = dict;
17264 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017265 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017266 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17267 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268}
17269
17270/*
17271 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017272 * Frees all allocated variables and the value they contain.
17273 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 */
17275 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017276vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017277 hashtab_T *ht;
17278{
17279 vars_clear_ext(ht, TRUE);
17280}
17281
17282/*
17283 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17284 */
17285 static void
17286vars_clear_ext(ht, free_val)
17287 hashtab_T *ht;
17288 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289{
Bram Moolenaara7043832005-01-21 11:56:39 +000017290 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017291 hashitem_T *hi;
17292 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293
Bram Moolenaar33570922005-01-25 22:26:29 +000017294 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000017295 todo = ht->ht_used;
17296 for (hi = ht->ht_array; todo > 0; ++hi)
17297 {
17298 if (!HASHITEM_EMPTY(hi))
17299 {
17300 --todo;
17301
Bram Moolenaar33570922005-01-25 22:26:29 +000017302 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017303 * ht_array might change then. hash_clear() takes care of it
17304 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017305 v = HI2DI(hi);
17306 if (free_val)
17307 clear_tv(&v->di_tv);
17308 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17309 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017310 }
17311 }
17312 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017313 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314}
17315
Bram Moolenaara7043832005-01-21 11:56:39 +000017316/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017317 * Delete a variable from hashtab "ht" at item "hi".
17318 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017321delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017322 hashtab_T *ht;
17323 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324{
Bram Moolenaar33570922005-01-25 22:26:29 +000017325 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017326
17327 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017328 clear_tv(&di->di_tv);
17329 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330}
17331
17332/*
17333 * List the value of one internal variable.
17334 */
17335 static void
17336list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017337 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338 char_u *prefix;
17339{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017340 char_u *tofree;
17341 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017342 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017343
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017344 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017345 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017346 s == NULL ? (char_u *)"" : s);
17347 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348}
17349
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350 static void
17351list_one_var_a(prefix, name, type, string)
17352 char_u *prefix;
17353 char_u *name;
17354 int type;
17355 char_u *string;
17356{
17357 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17358 if (name != NULL) /* "a:" vars don't have a name stored */
17359 msg_puts(name);
17360 msg_putchar(' ');
17361 msg_advance(22);
17362 if (type == VAR_NUMBER)
17363 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017364 else if (type == VAR_FUNC)
17365 msg_putchar('*');
17366 else if (type == VAR_LIST)
17367 {
17368 msg_putchar('[');
17369 if (*string == '[')
17370 ++string;
17371 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017372 else if (type == VAR_DICT)
17373 {
17374 msg_putchar('{');
17375 if (*string == '{')
17376 ++string;
17377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378 else
17379 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017380
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017382
17383 if (type == VAR_FUNC)
17384 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385}
17386
17387/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 * If the variable already exists, the value is updated.
17390 * Otherwise the variable is created.
17391 */
17392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017393set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017395 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017396 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397{
Bram Moolenaar33570922005-01-25 22:26:29 +000017398 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017400 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017401 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017403 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017404 {
17405 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17406 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17407 ? name[2] : name[0]))
17408 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017409 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017410 return;
17411 }
17412 if (function_exists(name))
17413 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017414 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017415 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017416 return;
17417 }
17418 }
17419
Bram Moolenaara7043832005-01-21 11:56:39 +000017420 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017421 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017422 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017423 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017424 return;
17425 }
17426
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017427 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017428 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017430 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017431 if (var_check_ro(v->di_flags, name)
17432 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017433 return;
17434 if (v->di_tv.v_type != tv->v_type
17435 && !((v->di_tv.v_type == VAR_STRING
17436 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017437 && (tv->v_type == VAR_STRING
17438 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017439 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017440 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017441 return;
17442 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017443
17444 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017445 * Handle setting internal v: variables separately: we don't change
17446 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017447 */
17448 if (ht == &vimvarht)
17449 {
17450 if (v->di_tv.v_type == VAR_STRING)
17451 {
17452 vim_free(v->di_tv.vval.v_string);
17453 if (copy || tv->v_type != VAR_STRING)
17454 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17455 else
17456 {
17457 /* Take over the string to avoid an extra alloc/free. */
17458 v->di_tv.vval.v_string = tv->vval.v_string;
17459 tv->vval.v_string = NULL;
17460 }
17461 }
17462 else if (v->di_tv.v_type != VAR_NUMBER)
17463 EMSG2(_(e_intern2), "set_var()");
17464 else
17465 v->di_tv.vval.v_number = get_tv_number(tv);
17466 return;
17467 }
17468
17469 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017470 }
17471 else /* add a new variable */
17472 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017473 /* Make sure the variable name is valid. */
17474 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017475 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17476 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017477 {
17478 EMSG2(_(e_illvar), varname);
17479 return;
17480 }
17481
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017482 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17483 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017484 if (v == NULL)
17485 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017486 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017487 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017489 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490 return;
17491 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017492 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017494
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017495 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017496 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017497 else
17498 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017499 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017500 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017501 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503}
17504
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017505/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017506 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17507 * Also give an error message.
17508 */
17509 static int
17510var_check_ro(flags, name)
17511 int flags;
17512 char_u *name;
17513{
17514 if (flags & DI_FLAGS_RO)
17515 {
17516 EMSG2(_(e_readonlyvar), name);
17517 return TRUE;
17518 }
17519 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17520 {
17521 EMSG2(_(e_readonlysbx), name);
17522 return TRUE;
17523 }
17524 return FALSE;
17525}
17526
17527/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017528 * Return TRUE if typeval "tv" is set to be locked (immutable).
17529 * Also give an error message, using "name".
17530 */
17531 static int
17532tv_check_lock(lock, name)
17533 int lock;
17534 char_u *name;
17535{
17536 if (lock & VAR_LOCKED)
17537 {
17538 EMSG2(_("E741: Value is locked: %s"),
17539 name == NULL ? (char_u *)_("Unknown") : name);
17540 return TRUE;
17541 }
17542 if (lock & VAR_FIXED)
17543 {
17544 EMSG2(_("E742: Cannot change value of %s"),
17545 name == NULL ? (char_u *)_("Unknown") : name);
17546 return TRUE;
17547 }
17548 return FALSE;
17549}
17550
17551/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017552 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017553 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017554 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017557copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017558 typval_T *from;
17559 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017561 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017562 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017563 switch (from->v_type)
17564 {
17565 case VAR_NUMBER:
17566 to->vval.v_number = from->vval.v_number;
17567 break;
17568 case VAR_STRING:
17569 case VAR_FUNC:
17570 if (from->vval.v_string == NULL)
17571 to->vval.v_string = NULL;
17572 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017573 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017574 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017575 if (from->v_type == VAR_FUNC)
17576 func_ref(to->vval.v_string);
17577 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017578 break;
17579 case VAR_LIST:
17580 if (from->vval.v_list == NULL)
17581 to->vval.v_list = NULL;
17582 else
17583 {
17584 to->vval.v_list = from->vval.v_list;
17585 ++to->vval.v_list->lv_refcount;
17586 }
17587 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017588 case VAR_DICT:
17589 if (from->vval.v_dict == NULL)
17590 to->vval.v_dict = NULL;
17591 else
17592 {
17593 to->vval.v_dict = from->vval.v_dict;
17594 ++to->vval.v_dict->dv_refcount;
17595 }
17596 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017597 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017598 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017599 break;
17600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601}
17602
17603/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017604 * Make a copy of an item.
17605 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017606 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17607 * reference to an already copied list/dict can be used.
17608 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017609 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017610 static int
17611item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017612 typval_T *from;
17613 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017614 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017615 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017616{
17617 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017618 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017619
Bram Moolenaar33570922005-01-25 22:26:29 +000017620 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017621 {
17622 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017623 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017624 }
17625 ++recurse;
17626
17627 switch (from->v_type)
17628 {
17629 case VAR_NUMBER:
17630 case VAR_STRING:
17631 case VAR_FUNC:
17632 copy_tv(from, to);
17633 break;
17634 case VAR_LIST:
17635 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017636 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017637 if (from->vval.v_list == NULL)
17638 to->vval.v_list = NULL;
17639 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17640 {
17641 /* use the copy made earlier */
17642 to->vval.v_list = from->vval.v_list->lv_copylist;
17643 ++to->vval.v_list->lv_refcount;
17644 }
17645 else
17646 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17647 if (to->vval.v_list == NULL)
17648 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017649 break;
17650 case VAR_DICT:
17651 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017652 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017653 if (from->vval.v_dict == NULL)
17654 to->vval.v_dict = NULL;
17655 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17656 {
17657 /* use the copy made earlier */
17658 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17659 ++to->vval.v_dict->dv_refcount;
17660 }
17661 else
17662 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17663 if (to->vval.v_dict == NULL)
17664 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017665 break;
17666 default:
17667 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017668 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017669 }
17670 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017671 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017672}
17673
17674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 * ":echo expr1 ..." print each argument separated with a space, add a
17676 * newline at the end.
17677 * ":echon expr1 ..." print each argument plain.
17678 */
17679 void
17680ex_echo(eap)
17681 exarg_T *eap;
17682{
17683 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017684 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017685 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 char_u *p;
17687 int needclr = TRUE;
17688 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017689 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690
17691 if (eap->skip)
17692 ++emsg_skip;
17693 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17694 {
17695 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017696 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697 {
17698 /*
17699 * Report the invalid expression unless the expression evaluation
17700 * has been cancelled due to an aborting error, an interrupt, or an
17701 * exception.
17702 */
17703 if (!aborting())
17704 EMSG2(_(e_invexpr2), p);
17705 break;
17706 }
17707 if (!eap->skip)
17708 {
17709 if (atstart)
17710 {
17711 atstart = FALSE;
17712 /* Call msg_start() after eval1(), evaluating the expression
17713 * may cause a message to appear. */
17714 if (eap->cmdidx == CMD_echo)
17715 msg_start();
17716 }
17717 else if (eap->cmdidx == CMD_echo)
17718 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017719 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017720 if (p != NULL)
17721 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017723 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017725 if (*p != TAB && needclr)
17726 {
17727 /* remove any text still there from the command */
17728 msg_clr_eos();
17729 needclr = FALSE;
17730 }
17731 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732 }
17733 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017734 {
17735#ifdef FEAT_MBYTE
17736 if (has_mbyte)
17737 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017738 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017739
17740 (void)msg_outtrans_len_attr(p, i, echo_attr);
17741 p += i - 1;
17742 }
17743 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017745 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017748 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017750 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 arg = skipwhite(arg);
17752 }
17753 eap->nextcmd = check_nextcmd(arg);
17754
17755 if (eap->skip)
17756 --emsg_skip;
17757 else
17758 {
17759 /* remove text that may still be there from the command */
17760 if (needclr)
17761 msg_clr_eos();
17762 if (eap->cmdidx == CMD_echo)
17763 msg_end();
17764 }
17765}
17766
17767/*
17768 * ":echohl {name}".
17769 */
17770 void
17771ex_echohl(eap)
17772 exarg_T *eap;
17773{
17774 int id;
17775
17776 id = syn_name2id(eap->arg);
17777 if (id == 0)
17778 echo_attr = 0;
17779 else
17780 echo_attr = syn_id2attr(id);
17781}
17782
17783/*
17784 * ":execute expr1 ..." execute the result of an expression.
17785 * ":echomsg expr1 ..." Print a message
17786 * ":echoerr expr1 ..." Print an error
17787 * Each gets spaces around each argument and a newline at the end for
17788 * echo commands
17789 */
17790 void
17791ex_execute(eap)
17792 exarg_T *eap;
17793{
17794 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017795 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 int ret = OK;
17797 char_u *p;
17798 garray_T ga;
17799 int len;
17800 int save_did_emsg;
17801
17802 ga_init2(&ga, 1, 80);
17803
17804 if (eap->skip)
17805 ++emsg_skip;
17806 while (*arg != NUL && *arg != '|' && *arg != '\n')
17807 {
17808 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017809 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810 {
17811 /*
17812 * Report the invalid expression unless the expression evaluation
17813 * has been cancelled due to an aborting error, an interrupt, or an
17814 * exception.
17815 */
17816 if (!aborting())
17817 EMSG2(_(e_invexpr2), p);
17818 ret = FAIL;
17819 break;
17820 }
17821
17822 if (!eap->skip)
17823 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017824 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825 len = (int)STRLEN(p);
17826 if (ga_grow(&ga, len + 2) == FAIL)
17827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017828 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829 ret = FAIL;
17830 break;
17831 }
17832 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835 ga.ga_len += len;
17836 }
17837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017838 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839 arg = skipwhite(arg);
17840 }
17841
17842 if (ret != FAIL && ga.ga_data != NULL)
17843 {
17844 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017845 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017847 out_flush();
17848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849 else if (eap->cmdidx == CMD_echoerr)
17850 {
17851 /* We don't want to abort following commands, restore did_emsg. */
17852 save_did_emsg = did_emsg;
17853 EMSG((char_u *)ga.ga_data);
17854 if (!force_abort)
17855 did_emsg = save_did_emsg;
17856 }
17857 else if (eap->cmdidx == CMD_execute)
17858 do_cmdline((char_u *)ga.ga_data,
17859 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17860 }
17861
17862 ga_clear(&ga);
17863
17864 if (eap->skip)
17865 --emsg_skip;
17866
17867 eap->nextcmd = check_nextcmd(arg);
17868}
17869
17870/*
17871 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17872 * "arg" points to the "&" or '+' when called, to "option" when returning.
17873 * Returns NULL when no option name found. Otherwise pointer to the char
17874 * after the option name.
17875 */
17876 static char_u *
17877find_option_end(arg, opt_flags)
17878 char_u **arg;
17879 int *opt_flags;
17880{
17881 char_u *p = *arg;
17882
17883 ++p;
17884 if (*p == 'g' && p[1] == ':')
17885 {
17886 *opt_flags = OPT_GLOBAL;
17887 p += 2;
17888 }
17889 else if (*p == 'l' && p[1] == ':')
17890 {
17891 *opt_flags = OPT_LOCAL;
17892 p += 2;
17893 }
17894 else
17895 *opt_flags = 0;
17896
17897 if (!ASCII_ISALPHA(*p))
17898 return NULL;
17899 *arg = p;
17900
17901 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17902 p += 4; /* termcap option */
17903 else
17904 while (ASCII_ISALPHA(*p))
17905 ++p;
17906 return p;
17907}
17908
17909/*
17910 * ":function"
17911 */
17912 void
17913ex_function(eap)
17914 exarg_T *eap;
17915{
17916 char_u *theline;
17917 int j;
17918 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920 char_u *name = NULL;
17921 char_u *p;
17922 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017923 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924 garray_T newargs;
17925 garray_T newlines;
17926 int varargs = FALSE;
17927 int mustend = FALSE;
17928 int flags = 0;
17929 ufunc_T *fp;
17930 int indent;
17931 int nesting;
17932 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017933 dictitem_T *v;
17934 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017935 static int func_nr = 0; /* number for nameless function */
17936 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017937 hashtab_T *ht;
17938 int todo;
17939 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017940 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017941
17942 /*
17943 * ":function" without argument: list functions.
17944 */
17945 if (ends_excmd(*eap->arg))
17946 {
17947 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017948 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017949 todo = func_hashtab.ht_used;
17950 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017951 {
17952 if (!HASHITEM_EMPTY(hi))
17953 {
17954 --todo;
17955 fp = HI2UF(hi);
17956 if (!isdigit(*fp->uf_name))
17957 list_func_head(fp, FALSE);
17958 }
17959 }
17960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961 eap->nextcmd = check_nextcmd(eap->arg);
17962 return;
17963 }
17964
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017965 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017966 * ":function /pat": list functions matching pattern.
17967 */
17968 if (*eap->arg == '/')
17969 {
17970 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17971 if (!eap->skip)
17972 {
17973 regmatch_T regmatch;
17974
17975 c = *p;
17976 *p = NUL;
17977 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17978 *p = c;
17979 if (regmatch.regprog != NULL)
17980 {
17981 regmatch.rm_ic = p_ic;
17982
17983 todo = func_hashtab.ht_used;
17984 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17985 {
17986 if (!HASHITEM_EMPTY(hi))
17987 {
17988 --todo;
17989 fp = HI2UF(hi);
17990 if (!isdigit(*fp->uf_name)
17991 && vim_regexec(&regmatch, fp->uf_name, 0))
17992 list_func_head(fp, FALSE);
17993 }
17994 }
17995 }
17996 }
17997 if (*p == '/')
17998 ++p;
17999 eap->nextcmd = check_nextcmd(p);
18000 return;
18001 }
18002
18003 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018004 * Get the function name. There are these situations:
18005 * func normal function name
18006 * "name" == func, "fudi.fd_dict" == NULL
18007 * dict.func new dictionary entry
18008 * "name" == NULL, "fudi.fd_dict" set,
18009 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18010 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018011 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018012 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18013 * dict.func existing dict entry that's not a Funcref
18014 * "name" == NULL, "fudi.fd_dict" set,
18015 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018018 name = trans_function_name(&p, eap->skip, 0, &fudi);
18019 paren = (vim_strchr(p, '(') != NULL);
18020 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 {
18022 /*
18023 * Return on an invalid expression in braces, unless the expression
18024 * evaluation has been cancelled due to an aborting error, an
18025 * interrupt, or an exception.
18026 */
18027 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018028 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018029 if (!eap->skip && fudi.fd_newkey != NULL)
18030 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018031 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034 else
18035 eap->skip = TRUE;
18036 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018037
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038 /* An error in a function call during evaluation of an expression in magic
18039 * braces should not cause the function not to be defined. */
18040 saved_did_emsg = did_emsg;
18041 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042
18043 /*
18044 * ":function func" with only function name: list function.
18045 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018046 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 {
18048 if (!ends_excmd(*skipwhite(p)))
18049 {
18050 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018051 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 }
18053 eap->nextcmd = check_nextcmd(p);
18054 if (eap->nextcmd != NULL)
18055 *p = NUL;
18056 if (!eap->skip && !got_int)
18057 {
18058 fp = find_func(name);
18059 if (fp != NULL)
18060 {
18061 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018062 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018064 if (FUNCLINE(fp, j) == NULL)
18065 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 msg_putchar('\n');
18067 msg_outnum((long)(j + 1));
18068 if (j < 9)
18069 msg_putchar(' ');
18070 if (j < 99)
18071 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018072 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073 out_flush(); /* show a line at a time */
18074 ui_breakcheck();
18075 }
18076 if (!got_int)
18077 {
18078 msg_putchar('\n');
18079 msg_puts((char_u *)" endfunction");
18080 }
18081 }
18082 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018083 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018085 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 }
18087
18088 /*
18089 * ":function name(arg1, arg2)" Define function.
18090 */
18091 p = skipwhite(p);
18092 if (*p != '(')
18093 {
18094 if (!eap->skip)
18095 {
18096 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018097 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 }
18099 /* attempt to continue by skipping some text */
18100 if (vim_strchr(p, '(') != NULL)
18101 p = vim_strchr(p, '(');
18102 }
18103 p = skipwhite(p + 1);
18104
18105 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18106 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18107
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018108 if (!eap->skip)
18109 {
18110 /* Check the name of the function. */
18111 if (name != NULL)
18112 arg = name;
18113 else
18114 arg = fudi.fd_newkey;
18115 if (arg != NULL)
18116 {
18117 if (*arg == K_SPECIAL)
18118 j = 3;
18119 else
18120 j = 0;
18121 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18122 : eval_isnamec(arg[j])))
18123 ++j;
18124 if (arg[j] != NUL)
18125 emsg_funcname(_(e_invarg2), arg);
18126 }
18127 }
18128
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 /*
18130 * Isolate the arguments: "arg1, arg2, ...)"
18131 */
18132 while (*p != ')')
18133 {
18134 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18135 {
18136 varargs = TRUE;
18137 p += 3;
18138 mustend = TRUE;
18139 }
18140 else
18141 {
18142 arg = p;
18143 while (ASCII_ISALNUM(*p) || *p == '_')
18144 ++p;
18145 if (arg == p || isdigit(*arg)
18146 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18147 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18148 {
18149 if (!eap->skip)
18150 EMSG2(_("E125: Illegal argument: %s"), arg);
18151 break;
18152 }
18153 if (ga_grow(&newargs, 1) == FAIL)
18154 goto erret;
18155 c = *p;
18156 *p = NUL;
18157 arg = vim_strsave(arg);
18158 if (arg == NULL)
18159 goto erret;
18160 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18161 *p = c;
18162 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 if (*p == ',')
18164 ++p;
18165 else
18166 mustend = TRUE;
18167 }
18168 p = skipwhite(p);
18169 if (mustend && *p != ')')
18170 {
18171 if (!eap->skip)
18172 EMSG2(_(e_invarg2), eap->arg);
18173 break;
18174 }
18175 }
18176 ++p; /* skip the ')' */
18177
Bram Moolenaare9a41262005-01-15 22:18:47 +000018178 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179 for (;;)
18180 {
18181 p = skipwhite(p);
18182 if (STRNCMP(p, "range", 5) == 0)
18183 {
18184 flags |= FC_RANGE;
18185 p += 5;
18186 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018187 else if (STRNCMP(p, "dict", 4) == 0)
18188 {
18189 flags |= FC_DICT;
18190 p += 4;
18191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192 else if (STRNCMP(p, "abort", 5) == 0)
18193 {
18194 flags |= FC_ABORT;
18195 p += 5;
18196 }
18197 else
18198 break;
18199 }
18200
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018201 /* When there is a line break use what follows for the function body.
18202 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18203 if (*p == '\n')
18204 line_arg = p + 1;
18205 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 EMSG(_(e_trailing));
18207
18208 /*
18209 * Read the body of the function, until ":endfunction" is found.
18210 */
18211 if (KeyTyped)
18212 {
18213 /* Check if the function already exists, don't let the user type the
18214 * whole function before telling him it doesn't work! For a script we
18215 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018216 if (!eap->skip && !eap->forceit)
18217 {
18218 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18219 EMSG(_(e_funcdict));
18220 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018221 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018224 if (!eap->skip && did_emsg)
18225 goto erret;
18226
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227 msg_putchar('\n'); /* don't overwrite the function name */
18228 cmdline_row = msg_row;
18229 }
18230
18231 indent = 2;
18232 nesting = 0;
18233 for (;;)
18234 {
18235 msg_scroll = TRUE;
18236 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018237 sourcing_lnum_off = sourcing_lnum;
18238
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018239 if (line_arg != NULL)
18240 {
18241 /* Use eap->arg, split up in parts by line breaks. */
18242 theline = line_arg;
18243 p = vim_strchr(theline, '\n');
18244 if (p == NULL)
18245 line_arg += STRLEN(line_arg);
18246 else
18247 {
18248 *p = NUL;
18249 line_arg = p + 1;
18250 }
18251 }
18252 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 theline = getcmdline(':', 0L, indent);
18254 else
18255 theline = eap->getline(':', eap->cookie, indent);
18256 if (KeyTyped)
18257 lines_left = Rows - 1;
18258 if (theline == NULL)
18259 {
18260 EMSG(_("E126: Missing :endfunction"));
18261 goto erret;
18262 }
18263
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018264 /* Detect line continuation: sourcing_lnum increased more than one. */
18265 if (sourcing_lnum > sourcing_lnum_off + 1)
18266 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18267 else
18268 sourcing_lnum_off = 0;
18269
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270 if (skip_until != NULL)
18271 {
18272 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18273 * don't check for ":endfunc". */
18274 if (STRCMP(theline, skip_until) == 0)
18275 {
18276 vim_free(skip_until);
18277 skip_until = NULL;
18278 }
18279 }
18280 else
18281 {
18282 /* skip ':' and blanks*/
18283 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18284 ;
18285
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018286 /* Check for "endfunction". */
18287 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018289 if (line_arg == NULL)
18290 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291 break;
18292 }
18293
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018294 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295 * at "end". */
18296 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18297 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018298 else if (STRNCMP(p, "if", 2) == 0
18299 || STRNCMP(p, "wh", 2) == 0
18300 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 || STRNCMP(p, "try", 3) == 0)
18302 indent += 2;
18303
18304 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018305 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018307 if (*p == '!')
18308 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309 p += eval_fname_script(p);
18310 if (ASCII_ISALPHA(*p))
18311 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018312 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313 if (*skipwhite(p) == '(')
18314 {
18315 ++nesting;
18316 indent += 2;
18317 }
18318 }
18319 }
18320
18321 /* Check for ":append" or ":insert". */
18322 p = skip_range(p, NULL);
18323 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18324 || (p[0] == 'i'
18325 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18326 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18327 skip_until = vim_strsave((char_u *)".");
18328
18329 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18330 arg = skipwhite(skiptowhite(p));
18331 if (arg[0] == '<' && arg[1] =='<'
18332 && ((p[0] == 'p' && p[1] == 'y'
18333 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18334 || (p[0] == 'p' && p[1] == 'e'
18335 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18336 || (p[0] == 't' && p[1] == 'c'
18337 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18338 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18339 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018340 || (p[0] == 'm' && p[1] == 'z'
18341 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 ))
18343 {
18344 /* ":python <<" continues until a dot, like ":append" */
18345 p = skipwhite(arg + 2);
18346 if (*p == NUL)
18347 skip_until = vim_strsave((char_u *)".");
18348 else
18349 skip_until = vim_strsave(p);
18350 }
18351 }
18352
18353 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018354 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018355 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018356 if (line_arg == NULL)
18357 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018359 }
18360
18361 /* Copy the line to newly allocated memory. get_one_sourceline()
18362 * allocates 250 bytes per line, this saves 80% on average. The cost
18363 * is an extra alloc/free. */
18364 p = vim_strsave(theline);
18365 if (p != NULL)
18366 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018367 if (line_arg == NULL)
18368 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018369 theline = p;
18370 }
18371
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018372 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18373
18374 /* Add NULL lines for continuation lines, so that the line count is
18375 * equal to the index in the growarray. */
18376 while (sourcing_lnum_off-- > 0)
18377 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018378
18379 /* Check for end of eap->arg. */
18380 if (line_arg != NULL && *line_arg == NUL)
18381 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 }
18383
18384 /* Don't define the function when skipping commands or when an error was
18385 * detected. */
18386 if (eap->skip || did_emsg)
18387 goto erret;
18388
18389 /*
18390 * If there are no errors, add the function
18391 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018392 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018393 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018394 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018395 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018396 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018397 emsg_funcname("E707: Function name conflicts with variable: %s",
18398 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018399 goto erret;
18400 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018401
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018402 fp = find_func(name);
18403 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018405 if (!eap->forceit)
18406 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018407 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018408 goto erret;
18409 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018410 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018411 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018412 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018413 name);
18414 goto erret;
18415 }
18416 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018417 ga_clear_strings(&(fp->uf_args));
18418 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018419 vim_free(name);
18420 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018422 }
18423 else
18424 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018425 char numbuf[20];
18426
18427 fp = NULL;
18428 if (fudi.fd_newkey == NULL && !eap->forceit)
18429 {
18430 EMSG(_(e_funcdict));
18431 goto erret;
18432 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018433 if (fudi.fd_di == NULL)
18434 {
18435 /* Can't add a function to a locked dictionary */
18436 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18437 goto erret;
18438 }
18439 /* Can't change an existing function if it is locked */
18440 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18441 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018442
18443 /* Give the function a sequential number. Can only be used with a
18444 * Funcref! */
18445 vim_free(name);
18446 sprintf(numbuf, "%d", ++func_nr);
18447 name = vim_strsave((char_u *)numbuf);
18448 if (name == NULL)
18449 goto erret;
18450 }
18451
18452 if (fp == NULL)
18453 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018454 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018455 {
18456 int slen, plen;
18457 char_u *scriptname;
18458
18459 /* Check that the autoload name matches the script name. */
18460 j = FAIL;
18461 if (sourcing_name != NULL)
18462 {
18463 scriptname = autoload_name(name);
18464 if (scriptname != NULL)
18465 {
18466 p = vim_strchr(scriptname, '/');
18467 plen = STRLEN(p);
18468 slen = STRLEN(sourcing_name);
18469 if (slen > plen && fnamecmp(p,
18470 sourcing_name + slen - plen) == 0)
18471 j = OK;
18472 vim_free(scriptname);
18473 }
18474 }
18475 if (j == FAIL)
18476 {
18477 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18478 goto erret;
18479 }
18480 }
18481
18482 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 if (fp == NULL)
18484 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018485
18486 if (fudi.fd_dict != NULL)
18487 {
18488 if (fudi.fd_di == NULL)
18489 {
18490 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018491 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018492 if (fudi.fd_di == NULL)
18493 {
18494 vim_free(fp);
18495 goto erret;
18496 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018497 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18498 {
18499 vim_free(fudi.fd_di);
18500 goto erret;
18501 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018502 }
18503 else
18504 /* overwrite existing dict entry */
18505 clear_tv(&fudi.fd_di->di_tv);
18506 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018507 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018508 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018509 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018510 }
18511
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018513 STRCPY(fp->uf_name, name);
18514 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018516 fp->uf_args = newargs;
18517 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018518#ifdef FEAT_PROFILE
18519 fp->uf_tml_count = NULL;
18520 fp->uf_tml_total = NULL;
18521 fp->uf_tml_self = NULL;
18522 fp->uf_profiling = FALSE;
18523 if (prof_def_func())
18524 func_do_profile(fp);
18525#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018526 fp->uf_varargs = varargs;
18527 fp->uf_flags = flags;
18528 fp->uf_calls = 0;
18529 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018530 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531
18532erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533 ga_clear_strings(&newargs);
18534 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018535ret_free:
18536 vim_free(skip_until);
18537 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540}
18541
18542/*
18543 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018544 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018546 * flags:
18547 * TFN_INT: internal function name OK
18548 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 * Advances "pp" to just after the function name (if no error).
18550 */
18551 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018552trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 char_u **pp;
18554 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018555 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018556 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018558 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 char_u *start;
18560 char_u *end;
18561 int lead;
18562 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018564 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018565
18566 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018567 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018569
18570 /* Check for hard coded <SNR>: already translated function ID (from a user
18571 * command). */
18572 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18573 && (*pp)[2] == (int)KE_SNR)
18574 {
18575 *pp += 3;
18576 len = get_id_len(pp) + 3;
18577 return vim_strnsave(start, len);
18578 }
18579
18580 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18581 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018583 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018585
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018586 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18587 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018588 if (end == start)
18589 {
18590 if (!skip)
18591 EMSG(_("E129: Function name required"));
18592 goto theend;
18593 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018594 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018595 {
18596 /*
18597 * Report an invalid expression in braces, unless the expression
18598 * evaluation has been cancelled due to an aborting error, an
18599 * interrupt, or an exception.
18600 */
18601 if (!aborting())
18602 {
18603 if (end != NULL)
18604 EMSG2(_(e_invarg2), start);
18605 }
18606 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018607 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018608 goto theend;
18609 }
18610
18611 if (lv.ll_tv != NULL)
18612 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018613 if (fdp != NULL)
18614 {
18615 fdp->fd_dict = lv.ll_dict;
18616 fdp->fd_newkey = lv.ll_newkey;
18617 lv.ll_newkey = NULL;
18618 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018619 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018620 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18621 {
18622 name = vim_strsave(lv.ll_tv->vval.v_string);
18623 *pp = end;
18624 }
18625 else
18626 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018627 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18628 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018629 EMSG(_(e_funcref));
18630 else
18631 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018632 name = NULL;
18633 }
18634 goto theend;
18635 }
18636
18637 if (lv.ll_name == NULL)
18638 {
18639 /* Error found, but continue after the function name. */
18640 *pp = end;
18641 goto theend;
18642 }
18643
18644 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018646 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018647 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18648 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18649 {
18650 /* When there was "s:" already or the name expanded to get a
18651 * leading "s:" then remove it. */
18652 lv.ll_name += 2;
18653 len -= 2;
18654 lead = 2;
18655 }
18656 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018657 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018658 {
18659 if (lead == 2) /* skip over "s:" */
18660 lv.ll_name += 2;
18661 len = (int)(end - lv.ll_name);
18662 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018663
18664 /*
18665 * Copy the function name to allocated memory.
18666 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18667 * Accept <SNR>123_name() outside a script.
18668 */
18669 if (skip)
18670 lead = 0; /* do nothing */
18671 else if (lead > 0)
18672 {
18673 lead = 3;
18674 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18675 {
18676 if (current_SID <= 0)
18677 {
18678 EMSG(_(e_usingsid));
18679 goto theend;
18680 }
18681 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18682 lead += (int)STRLEN(sid_buf);
18683 }
18684 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018685 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018686 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018687 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018688 goto theend;
18689 }
18690 name = alloc((unsigned)(len + lead + 1));
18691 if (name != NULL)
18692 {
18693 if (lead > 0)
18694 {
18695 name[0] = K_SPECIAL;
18696 name[1] = KS_EXTRA;
18697 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018698 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018699 STRCPY(name + 3, sid_buf);
18700 }
18701 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18702 name[len + lead] = NUL;
18703 }
18704 *pp = end;
18705
18706theend:
18707 clear_lval(&lv);
18708 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709}
18710
18711/*
18712 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18713 * Return 2 if "p" starts with "s:".
18714 * Return 0 otherwise.
18715 */
18716 static int
18717eval_fname_script(p)
18718 char_u *p;
18719{
18720 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18721 || STRNICMP(p + 1, "SNR>", 4) == 0))
18722 return 5;
18723 if (p[0] == 's' && p[1] == ':')
18724 return 2;
18725 return 0;
18726}
18727
18728/*
18729 * Return TRUE if "p" starts with "<SID>" or "s:".
18730 * Only works if eval_fname_script() returned non-zero for "p"!
18731 */
18732 static int
18733eval_fname_sid(p)
18734 char_u *p;
18735{
18736 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18737}
18738
18739/*
18740 * List the head of the function: "name(arg1, arg2)".
18741 */
18742 static void
18743list_func_head(fp, indent)
18744 ufunc_T *fp;
18745 int indent;
18746{
18747 int j;
18748
18749 msg_start();
18750 if (indent)
18751 MSG_PUTS(" ");
18752 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018753 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 {
18755 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018756 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 }
18758 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018759 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018761 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 {
18763 if (j)
18764 MSG_PUTS(", ");
18765 msg_puts(FUNCARG(fp, j));
18766 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018767 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 {
18769 if (j)
18770 MSG_PUTS(", ");
18771 MSG_PUTS("...");
18772 }
18773 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018774 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018775 if (p_verbose > 0)
18776 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777}
18778
18779/*
18780 * Find a function by name, return pointer to it in ufuncs.
18781 * Return NULL for unknown function.
18782 */
18783 static ufunc_T *
18784find_func(name)
18785 char_u *name;
18786{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018787 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018789 hi = hash_find(&func_hashtab, name);
18790 if (!HASHITEM_EMPTY(hi))
18791 return HI2UF(hi);
18792 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793}
18794
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018795#if defined(EXITFREE) || defined(PROTO)
18796 void
18797free_all_functions()
18798{
18799 hashitem_T *hi;
18800
18801 /* Need to start all over every time, because func_free() may change the
18802 * hash table. */
18803 while (func_hashtab.ht_used > 0)
18804 for (hi = func_hashtab.ht_array; ; ++hi)
18805 if (!HASHITEM_EMPTY(hi))
18806 {
18807 func_free(HI2UF(hi));
18808 break;
18809 }
18810}
18811#endif
18812
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018813/*
18814 * Return TRUE if a function "name" exists.
18815 */
18816 static int
18817function_exists(name)
18818 char_u *name;
18819{
18820 char_u *p = name;
18821 int n = FALSE;
18822
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018823 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018824 if (p != NULL)
18825 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018826 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018827 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018828 else
18829 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018830 vim_free(p);
18831 }
18832 return n;
18833}
18834
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018835/*
18836 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018837 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018838 */
18839 static int
18840builtin_function(name)
18841 char_u *name;
18842{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018843 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18844 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018845}
18846
Bram Moolenaar05159a02005-02-26 23:04:13 +000018847#if defined(FEAT_PROFILE) || defined(PROTO)
18848/*
18849 * Start profiling function "fp".
18850 */
18851 static void
18852func_do_profile(fp)
18853 ufunc_T *fp;
18854{
18855 fp->uf_tm_count = 0;
18856 profile_zero(&fp->uf_tm_self);
18857 profile_zero(&fp->uf_tm_total);
18858 if (fp->uf_tml_count == NULL)
18859 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18860 (sizeof(int) * fp->uf_lines.ga_len));
18861 if (fp->uf_tml_total == NULL)
18862 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18863 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18864 if (fp->uf_tml_self == NULL)
18865 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18866 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18867 fp->uf_tml_idx = -1;
18868 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18869 || fp->uf_tml_self == NULL)
18870 return; /* out of memory */
18871
18872 fp->uf_profiling = TRUE;
18873}
18874
18875/*
18876 * Dump the profiling results for all functions in file "fd".
18877 */
18878 void
18879func_dump_profile(fd)
18880 FILE *fd;
18881{
18882 hashitem_T *hi;
18883 int todo;
18884 ufunc_T *fp;
18885 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018886 ufunc_T **sorttab;
18887 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018888
18889 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018890 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18891
Bram Moolenaar05159a02005-02-26 23:04:13 +000018892 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18893 {
18894 if (!HASHITEM_EMPTY(hi))
18895 {
18896 --todo;
18897 fp = HI2UF(hi);
18898 if (fp->uf_profiling)
18899 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018900 if (sorttab != NULL)
18901 sorttab[st_len++] = fp;
18902
Bram Moolenaar05159a02005-02-26 23:04:13 +000018903 if (fp->uf_name[0] == K_SPECIAL)
18904 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18905 else
18906 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18907 if (fp->uf_tm_count == 1)
18908 fprintf(fd, "Called 1 time\n");
18909 else
18910 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18911 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18912 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18913 fprintf(fd, "\n");
18914 fprintf(fd, "count total (s) self (s)\n");
18915
18916 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18917 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018918 if (FUNCLINE(fp, i) == NULL)
18919 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000018920 prof_func_line(fd, fp->uf_tml_count[i],
18921 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018922 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18923 }
18924 fprintf(fd, "\n");
18925 }
18926 }
18927 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018928
18929 if (sorttab != NULL && st_len > 0)
18930 {
18931 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18932 prof_total_cmp);
18933 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18934 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18935 prof_self_cmp);
18936 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18937 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018938}
Bram Moolenaar73830342005-02-28 22:48:19 +000018939
18940 static void
18941prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18942 FILE *fd;
18943 ufunc_T **sorttab;
18944 int st_len;
18945 char *title;
18946 int prefer_self; /* when equal print only self time */
18947{
18948 int i;
18949 ufunc_T *fp;
18950
18951 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18952 fprintf(fd, "count total (s) self (s) function\n");
18953 for (i = 0; i < 20 && i < st_len; ++i)
18954 {
18955 fp = sorttab[i];
18956 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18957 prefer_self);
18958 if (fp->uf_name[0] == K_SPECIAL)
18959 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18960 else
18961 fprintf(fd, " %s()\n", fp->uf_name);
18962 }
18963 fprintf(fd, "\n");
18964}
18965
18966/*
18967 * Print the count and times for one function or function line.
18968 */
18969 static void
18970prof_func_line(fd, count, total, self, prefer_self)
18971 FILE *fd;
18972 int count;
18973 proftime_T *total;
18974 proftime_T *self;
18975 int prefer_self; /* when equal print only self time */
18976{
18977 if (count > 0)
18978 {
18979 fprintf(fd, "%5d ", count);
18980 if (prefer_self && profile_equal(total, self))
18981 fprintf(fd, " ");
18982 else
18983 fprintf(fd, "%s ", profile_msg(total));
18984 if (!prefer_self && profile_equal(total, self))
18985 fprintf(fd, " ");
18986 else
18987 fprintf(fd, "%s ", profile_msg(self));
18988 }
18989 else
18990 fprintf(fd, " ");
18991}
18992
18993/*
18994 * Compare function for total time sorting.
18995 */
18996 static int
18997#ifdef __BORLANDC__
18998_RTLENTRYF
18999#endif
19000prof_total_cmp(s1, s2)
19001 const void *s1;
19002 const void *s2;
19003{
19004 ufunc_T *p1, *p2;
19005
19006 p1 = *(ufunc_T **)s1;
19007 p2 = *(ufunc_T **)s2;
19008 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19009}
19010
19011/*
19012 * Compare function for self time sorting.
19013 */
19014 static int
19015#ifdef __BORLANDC__
19016_RTLENTRYF
19017#endif
19018prof_self_cmp(s1, s2)
19019 const void *s1;
19020 const void *s2;
19021{
19022 ufunc_T *p1, *p2;
19023
19024 p1 = *(ufunc_T **)s1;
19025 p2 = *(ufunc_T **)s2;
19026 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19027}
19028
Bram Moolenaar05159a02005-02-26 23:04:13 +000019029#endif
19030
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019031/* The names of packages that once were loaded is remembered. */
19032static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19033
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019034/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019035 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019036 * Return TRUE if a package was loaded.
19037 */
19038 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019039script_autoload(name, reload)
19040 char_u *name;
19041 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019042{
19043 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019044 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019045 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019046 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019047
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019048 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019049 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019050 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019051 return FALSE;
19052
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019053 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019054
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019055 /* Find the name in the list of previously loaded package names. Skip
19056 * "autoload/", it's always the same. */
19057 for (i = 0; i < ga_loaded.ga_len; ++i)
19058 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19059 break;
19060 if (!reload && i < ga_loaded.ga_len)
19061 ret = FALSE; /* was loaded already */
19062 else
19063 {
19064 /* Remember the name if it wasn't loaded already. */
19065 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19066 {
19067 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19068 tofree = NULL;
19069 }
19070
19071 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019072 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019073 ret = TRUE;
19074 }
19075
19076 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019077 return ret;
19078}
19079
19080/*
19081 * Return the autoload script name for a function or variable name.
19082 * Returns NULL when out of memory.
19083 */
19084 static char_u *
19085autoload_name(name)
19086 char_u *name;
19087{
19088 char_u *p;
19089 char_u *scriptname;
19090
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019091 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019092 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19093 if (scriptname == NULL)
19094 return FALSE;
19095 STRCPY(scriptname, "autoload/");
19096 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019097 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019098 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019099 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019100 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019101 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019102}
19103
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19105
19106/*
19107 * Function given to ExpandGeneric() to obtain the list of user defined
19108 * function names.
19109 */
19110 char_u *
19111get_user_func_name(xp, idx)
19112 expand_T *xp;
19113 int idx;
19114{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019115 static long_u done;
19116 static hashitem_T *hi;
19117 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118
19119 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019121 done = 0;
19122 hi = func_hashtab.ht_array;
19123 }
19124 if (done < func_hashtab.ht_used)
19125 {
19126 if (done++ > 0)
19127 ++hi;
19128 while (HASHITEM_EMPTY(hi))
19129 ++hi;
19130 fp = HI2UF(hi);
19131
19132 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19133 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134
19135 cat_func_name(IObuff, fp);
19136 if (xp->xp_context != EXPAND_USER_FUNC)
19137 {
19138 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019139 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140 STRCAT(IObuff, ")");
19141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 return IObuff;
19143 }
19144 return NULL;
19145}
19146
19147#endif /* FEAT_CMDL_COMPL */
19148
19149/*
19150 * Copy the function name of "fp" to buffer "buf".
19151 * "buf" must be able to hold the function name plus three bytes.
19152 * Takes care of script-local function names.
19153 */
19154 static void
19155cat_func_name(buf, fp)
19156 char_u *buf;
19157 ufunc_T *fp;
19158{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019159 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 {
19161 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019162 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 }
19164 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019165 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166}
19167
19168/*
19169 * ":delfunction {name}"
19170 */
19171 void
19172ex_delfunction(eap)
19173 exarg_T *eap;
19174{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019175 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176 char_u *p;
19177 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019178 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179
19180 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019181 name = trans_function_name(&p, eap->skip, 0, &fudi);
19182 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019184 {
19185 if (fudi.fd_dict != NULL && !eap->skip)
19186 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189 if (!ends_excmd(*skipwhite(p)))
19190 {
19191 vim_free(name);
19192 EMSG(_(e_trailing));
19193 return;
19194 }
19195 eap->nextcmd = check_nextcmd(p);
19196 if (eap->nextcmd != NULL)
19197 *p = NUL;
19198
19199 if (!eap->skip)
19200 fp = find_func(name);
19201 vim_free(name);
19202
19203 if (!eap->skip)
19204 {
19205 if (fp == NULL)
19206 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019207 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208 return;
19209 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019210 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211 {
19212 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19213 return;
19214 }
19215
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019216 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019218 /* Delete the dict item that refers to the function, it will
19219 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019220 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019222 else
19223 func_free(fp);
19224 }
19225}
19226
19227/*
19228 * Free a function and remove it from the list of functions.
19229 */
19230 static void
19231func_free(fp)
19232 ufunc_T *fp;
19233{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019234 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019235
19236 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019237 ga_clear_strings(&(fp->uf_args));
19238 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019239#ifdef FEAT_PROFILE
19240 vim_free(fp->uf_tml_count);
19241 vim_free(fp->uf_tml_total);
19242 vim_free(fp->uf_tml_self);
19243#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019244
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019245 /* remove the function from the function hashtable */
19246 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19247 if (HASHITEM_EMPTY(hi))
19248 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019249 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019250 hash_remove(&func_hashtab, hi);
19251
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019252 vim_free(fp);
19253}
19254
19255/*
19256 * Unreference a Function: decrement the reference count and free it when it
19257 * becomes zero. Only for numbered functions.
19258 */
19259 static void
19260func_unref(name)
19261 char_u *name;
19262{
19263 ufunc_T *fp;
19264
19265 if (name != NULL && isdigit(*name))
19266 {
19267 fp = find_func(name);
19268 if (fp == NULL)
19269 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019270 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019271 {
19272 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019273 * when "uf_calls" becomes zero. */
19274 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019275 func_free(fp);
19276 }
19277 }
19278}
19279
19280/*
19281 * Count a reference to a Function.
19282 */
19283 static void
19284func_ref(name)
19285 char_u *name;
19286{
19287 ufunc_T *fp;
19288
19289 if (name != NULL && isdigit(*name))
19290 {
19291 fp = find_func(name);
19292 if (fp == NULL)
19293 EMSG2(_(e_intern2), "func_ref()");
19294 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019295 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 }
19297}
19298
19299/*
19300 * Call a user function.
19301 */
19302 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019303call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 ufunc_T *fp; /* pointer to function */
19305 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019306 typval_T *argvars; /* arguments */
19307 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019308 linenr_T firstline; /* first line of range */
19309 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019310 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311{
Bram Moolenaar33570922005-01-25 22:26:29 +000019312 char_u *save_sourcing_name;
19313 linenr_T save_sourcing_lnum;
19314 scid_T save_current_SID;
19315 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019316 int save_did_emsg;
19317 static int depth = 0;
19318 dictitem_T *v;
19319 int fixvar_idx = 0; /* index in fixvar[] */
19320 int i;
19321 int ai;
19322 char_u numbuf[NUMBUFLEN];
19323 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019324#ifdef FEAT_PROFILE
19325 proftime_T wait_start;
19326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327
19328 /* If depth of calling is getting too high, don't execute the function */
19329 if (depth >= p_mfd)
19330 {
19331 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019332 rettv->v_type = VAR_NUMBER;
19333 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019334 return;
19335 }
19336 ++depth;
19337
19338 line_breakcheck(); /* check for CTRL-C hit */
19339
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019340 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019341 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019343 fc.rettv = rettv;
19344 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 fc.linenr = 0;
19346 fc.returned = FALSE;
19347 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019348 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019349 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350 fc.dbg_tick = debug_tick;
19351
Bram Moolenaar33570922005-01-25 22:26:29 +000019352 /*
19353 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19354 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19355 * each argument variable and saves a lot of time.
19356 */
19357 /*
19358 * Init l: variables.
19359 */
19360 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019361 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019362 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019363 /* Set l:self to "selfdict". */
19364 v = &fc.fixvar[fixvar_idx++].var;
19365 STRCPY(v->di_key, "self");
19366 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19367 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19368 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019369 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019370 v->di_tv.vval.v_dict = selfdict;
19371 ++selfdict->dv_refcount;
19372 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019373
Bram Moolenaar33570922005-01-25 22:26:29 +000019374 /*
19375 * Init a: variables.
19376 * Set a:0 to "argcount".
19377 * Set a:000 to a list with room for the "..." arguments.
19378 */
19379 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19380 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019381 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019382 v = &fc.fixvar[fixvar_idx++].var;
19383 STRCPY(v->di_key, "000");
19384 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19385 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19386 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019387 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019388 v->di_tv.vval.v_list = &fc.l_varlist;
19389 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19390 fc.l_varlist.lv_refcount = 99999;
19391
19392 /*
19393 * Set a:firstline to "firstline" and a:lastline to "lastline".
19394 * Set a:name to named arguments.
19395 * Set a:N to the "..." arguments.
19396 */
19397 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19398 (varnumber_T)firstline);
19399 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19400 (varnumber_T)lastline);
19401 for (i = 0; i < argcount; ++i)
19402 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019403 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019404 if (ai < 0)
19405 /* named argument a:name */
19406 name = FUNCARG(fp, i);
19407 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019408 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019409 /* "..." argument a:1, a:2, etc. */
19410 sprintf((char *)numbuf, "%d", ai + 1);
19411 name = numbuf;
19412 }
19413 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19414 {
19415 v = &fc.fixvar[fixvar_idx++].var;
19416 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19417 }
19418 else
19419 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019420 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19421 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019422 if (v == NULL)
19423 break;
19424 v->di_flags = DI_FLAGS_RO;
19425 }
19426 STRCPY(v->di_key, name);
19427 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19428
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019429 /* Note: the values are copied directly to avoid alloc/free.
19430 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019431 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019432 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019433
19434 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19435 {
19436 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19437 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019438 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019439 }
19440 }
19441
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442 /* Don't redraw while executing the function. */
19443 ++RedrawingDisabled;
19444 save_sourcing_name = sourcing_name;
19445 save_sourcing_lnum = sourcing_lnum;
19446 sourcing_lnum = 1;
19447 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019448 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 if (sourcing_name != NULL)
19450 {
19451 if (save_sourcing_name != NULL
19452 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19453 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19454 else
19455 STRCPY(sourcing_name, "function ");
19456 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19457
19458 if (p_verbose >= 12)
19459 {
19460 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019461 verbose_enter_scroll();
19462
Bram Moolenaar555b2802005-05-19 21:08:39 +000019463 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464 if (p_verbose >= 14)
19465 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019467 char_u numbuf[NUMBUFLEN];
19468 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469
19470 msg_puts((char_u *)"(");
19471 for (i = 0; i < argcount; ++i)
19472 {
19473 if (i > 0)
19474 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019475 if (argvars[i].v_type == VAR_NUMBER)
19476 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 else
19478 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019479 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019480 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019482 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483 }
19484 }
19485 msg_puts((char_u *)")");
19486 }
19487 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019488
19489 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 --no_wait_return;
19491 }
19492 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019493#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019494 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019495 {
19496 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19497 func_do_profile(fp);
19498 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019499 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019500 {
19501 ++fp->uf_tm_count;
19502 profile_start(&fp->uf_tm_start);
19503 profile_zero(&fp->uf_tm_children);
19504 }
19505 script_prof_save(&wait_start);
19506 }
19507#endif
19508
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019510 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 save_did_emsg = did_emsg;
19512 did_emsg = FALSE;
19513
19514 /* call do_cmdline() to execute the lines */
19515 do_cmdline(NULL, get_func_line, (void *)&fc,
19516 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19517
19518 --RedrawingDisabled;
19519
19520 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019521 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019523 clear_tv(rettv);
19524 rettv->v_type = VAR_NUMBER;
19525 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526 }
19527
Bram Moolenaar05159a02005-02-26 23:04:13 +000019528#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019529 if (do_profiling == PROF_YES && (fp->uf_profiling
19530 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019531 {
19532 profile_end(&fp->uf_tm_start);
19533 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19534 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019535 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019536 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019537 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019538 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19539 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019540 }
19541 }
19542#endif
19543
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 /* when being verbose, mention the return value */
19545 if (p_verbose >= 12)
19546 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019548 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019549
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019551 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019552 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019553 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19554 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019555 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019557 char_u buf[MSG_BUF_LEN];
19558 char_u numbuf[NUMBUFLEN];
19559 char_u *tofree;
19560
Bram Moolenaar555b2802005-05-19 21:08:39 +000019561 /* The value may be very long. Skip the middle part, so that we
19562 * have some idea how it starts and ends. smsg() would always
19563 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019564 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019565 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019566 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019567 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568 }
19569 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019570
19571 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 --no_wait_return;
19573 }
19574
19575 vim_free(sourcing_name);
19576 sourcing_name = save_sourcing_name;
19577 sourcing_lnum = save_sourcing_lnum;
19578 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019579#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019580 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019581 script_prof_restore(&wait_start);
19582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583
19584 if (p_verbose >= 12 && sourcing_name != NULL)
19585 {
19586 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019587 verbose_enter_scroll();
19588
Bram Moolenaar555b2802005-05-19 21:08:39 +000019589 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019591
19592 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 --no_wait_return;
19594 }
19595
19596 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019597 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598
Bram Moolenaar33570922005-01-25 22:26:29 +000019599 /* The a: variables typevals were not alloced, only free the allocated
19600 * variables. */
19601 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19602
19603 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604 --depth;
19605}
19606
19607/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019608 * Add a number variable "name" to dict "dp" with value "nr".
19609 */
19610 static void
19611add_nr_var(dp, v, name, nr)
19612 dict_T *dp;
19613 dictitem_T *v;
19614 char *name;
19615 varnumber_T nr;
19616{
19617 STRCPY(v->di_key, name);
19618 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19619 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19620 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019621 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019622 v->di_tv.vval.v_number = nr;
19623}
19624
19625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 * ":return [expr]"
19627 */
19628 void
19629ex_return(eap)
19630 exarg_T *eap;
19631{
19632 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019633 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 int returning = FALSE;
19635
19636 if (current_funccal == NULL)
19637 {
19638 EMSG(_("E133: :return not inside a function"));
19639 return;
19640 }
19641
19642 if (eap->skip)
19643 ++emsg_skip;
19644
19645 eap->nextcmd = NULL;
19646 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019647 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648 {
19649 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019650 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019652 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 }
19654 /* It's safer to return also on error. */
19655 else if (!eap->skip)
19656 {
19657 /*
19658 * Return unless the expression evaluation has been cancelled due to an
19659 * aborting error, an interrupt, or an exception.
19660 */
19661 if (!aborting())
19662 returning = do_return(eap, FALSE, TRUE, NULL);
19663 }
19664
19665 /* When skipping or the return gets pending, advance to the next command
19666 * in this line (!returning). Otherwise, ignore the rest of the line.
19667 * Following lines will be ignored by get_func_line(). */
19668 if (returning)
19669 eap->nextcmd = NULL;
19670 else if (eap->nextcmd == NULL) /* no argument */
19671 eap->nextcmd = check_nextcmd(arg);
19672
19673 if (eap->skip)
19674 --emsg_skip;
19675}
19676
19677/*
19678 * Return from a function. Possibly makes the return pending. Also called
19679 * for a pending return at the ":endtry" or after returning from an extra
19680 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019681 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019682 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 * FALSE when the return gets pending.
19684 */
19685 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019686do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687 exarg_T *eap;
19688 int reanimate;
19689 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019690 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691{
19692 int idx;
19693 struct condstack *cstack = eap->cstack;
19694
19695 if (reanimate)
19696 /* Undo the return. */
19697 current_funccal->returned = FALSE;
19698
19699 /*
19700 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19701 * not in its finally clause (which then is to be executed next) is found.
19702 * In this case, make the ":return" pending for execution at the ":endtry".
19703 * Otherwise, return normally.
19704 */
19705 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19706 if (idx >= 0)
19707 {
19708 cstack->cs_pending[idx] = CSTP_RETURN;
19709
19710 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019711 /* A pending return again gets pending. "rettv" points to an
19712 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019714 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715 else
19716 {
19717 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019718 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019720 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019722 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723 {
19724 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019725 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019726 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 else
19728 EMSG(_(e_outofmem));
19729 }
19730 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019731 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732
19733 if (reanimate)
19734 {
19735 /* The pending return value could be overwritten by a ":return"
19736 * without argument in a finally clause; reset the default
19737 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019738 current_funccal->rettv->v_type = VAR_NUMBER;
19739 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740 }
19741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019742 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743 }
19744 else
19745 {
19746 current_funccal->returned = TRUE;
19747
19748 /* If the return is carried out now, store the return value. For
19749 * a return immediately after reanimation, the value is already
19750 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019751 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019753 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019754 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019756 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757 }
19758 }
19759
19760 return idx < 0;
19761}
19762
19763/*
19764 * Free the variable with a pending return value.
19765 */
19766 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019767discard_pending_return(rettv)
19768 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769{
Bram Moolenaar33570922005-01-25 22:26:29 +000019770 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771}
19772
19773/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019774 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 * is an allocated string. Used by report_pending() for verbose messages.
19776 */
19777 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019778get_return_cmd(rettv)
19779 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019781 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019782 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019783 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019785 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019786 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019787 if (s == NULL)
19788 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019789
19790 STRCPY(IObuff, ":return ");
19791 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19792 if (STRLEN(s) + 8 >= IOSIZE)
19793 STRCPY(IObuff + IOSIZE - 4, "...");
19794 vim_free(tofree);
19795 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796}
19797
19798/*
19799 * Get next function line.
19800 * Called by do_cmdline() to get the next line.
19801 * Returns allocated string, or NULL for end of function.
19802 */
19803/* ARGSUSED */
19804 char_u *
19805get_func_line(c, cookie, indent)
19806 int c; /* not used */
19807 void *cookie;
19808 int indent; /* not used */
19809{
Bram Moolenaar33570922005-01-25 22:26:29 +000019810 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019811 ufunc_T *fp = fcp->func;
19812 char_u *retval;
19813 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814
19815 /* If breakpoints have been added/deleted need to check for it. */
19816 if (fcp->dbg_tick != debug_tick)
19817 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019818 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 sourcing_lnum);
19820 fcp->dbg_tick = debug_tick;
19821 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019822#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019823 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019824 func_line_end(cookie);
19825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826
Bram Moolenaar05159a02005-02-26 23:04:13 +000019827 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019828 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
19829 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830 retval = NULL;
19831 else
19832 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019833 /* Skip NULL lines (continuation lines). */
19834 while (fcp->linenr < gap->ga_len
19835 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
19836 ++fcp->linenr;
19837 if (fcp->linenr >= gap->ga_len)
19838 retval = NULL;
19839 else
19840 {
19841 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19842 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019843#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019844 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019845 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019846#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848 }
19849
19850 /* Did we encounter a breakpoint? */
19851 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19852 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019853 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019855 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856 sourcing_lnum);
19857 fcp->dbg_tick = debug_tick;
19858 }
19859
19860 return retval;
19861}
19862
Bram Moolenaar05159a02005-02-26 23:04:13 +000019863#if defined(FEAT_PROFILE) || defined(PROTO)
19864/*
19865 * Called when starting to read a function line.
19866 * "sourcing_lnum" must be correct!
19867 * When skipping lines it may not actually be executed, but we won't find out
19868 * until later and we need to store the time now.
19869 */
19870 void
19871func_line_start(cookie)
19872 void *cookie;
19873{
19874 funccall_T *fcp = (funccall_T *)cookie;
19875 ufunc_T *fp = fcp->func;
19876
19877 if (fp->uf_profiling && sourcing_lnum >= 1
19878 && sourcing_lnum <= fp->uf_lines.ga_len)
19879 {
19880 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019881 /* Skip continuation lines. */
19882 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
19883 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019884 fp->uf_tml_execed = FALSE;
19885 profile_start(&fp->uf_tml_start);
19886 profile_zero(&fp->uf_tml_children);
19887 profile_get_wait(&fp->uf_tml_wait);
19888 }
19889}
19890
19891/*
19892 * Called when actually executing a function line.
19893 */
19894 void
19895func_line_exec(cookie)
19896 void *cookie;
19897{
19898 funccall_T *fcp = (funccall_T *)cookie;
19899 ufunc_T *fp = fcp->func;
19900
19901 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19902 fp->uf_tml_execed = TRUE;
19903}
19904
19905/*
19906 * Called when done with a function line.
19907 */
19908 void
19909func_line_end(cookie)
19910 void *cookie;
19911{
19912 funccall_T *fcp = (funccall_T *)cookie;
19913 ufunc_T *fp = fcp->func;
19914
19915 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19916 {
19917 if (fp->uf_tml_execed)
19918 {
19919 ++fp->uf_tml_count[fp->uf_tml_idx];
19920 profile_end(&fp->uf_tml_start);
19921 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019922 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019923 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
19924 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019925 }
19926 fp->uf_tml_idx = -1;
19927 }
19928}
19929#endif
19930
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931/*
19932 * Return TRUE if the currently active function should be ended, because a
19933 * return was encountered or an error occured. Used inside a ":while".
19934 */
19935 int
19936func_has_ended(cookie)
19937 void *cookie;
19938{
Bram Moolenaar33570922005-01-25 22:26:29 +000019939 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940
19941 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19942 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019943 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944 || fcp->returned);
19945}
19946
19947/*
19948 * return TRUE if cookie indicates a function which "abort"s on errors.
19949 */
19950 int
19951func_has_abort(cookie)
19952 void *cookie;
19953{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019954 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955}
19956
19957#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19958typedef enum
19959{
19960 VAR_FLAVOUR_DEFAULT,
19961 VAR_FLAVOUR_SESSION,
19962 VAR_FLAVOUR_VIMINFO
19963} var_flavour_T;
19964
19965static var_flavour_T var_flavour __ARGS((char_u *varname));
19966
19967 static var_flavour_T
19968var_flavour(varname)
19969 char_u *varname;
19970{
19971 char_u *p = varname;
19972
19973 if (ASCII_ISUPPER(*p))
19974 {
19975 while (*(++p))
19976 if (ASCII_ISLOWER(*p))
19977 return VAR_FLAVOUR_SESSION;
19978 return VAR_FLAVOUR_VIMINFO;
19979 }
19980 else
19981 return VAR_FLAVOUR_DEFAULT;
19982}
19983#endif
19984
19985#if defined(FEAT_VIMINFO) || defined(PROTO)
19986/*
19987 * Restore global vars that start with a capital from the viminfo file
19988 */
19989 int
19990read_viminfo_varlist(virp, writing)
19991 vir_T *virp;
19992 int writing;
19993{
19994 char_u *tab;
19995 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019996 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997
19998 if (!writing && (find_viminfo_parameter('!') != NULL))
19999 {
20000 tab = vim_strchr(virp->vir_line + 1, '\t');
20001 if (tab != NULL)
20002 {
20003 *tab++ = '\0'; /* isolate the variable name */
20004 if (*tab == 'S') /* string var */
20005 is_string = TRUE;
20006
20007 tab = vim_strchr(tab, '\t');
20008 if (tab != NULL)
20009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010 if (is_string)
20011 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020012 tv.v_type = VAR_STRING;
20013 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020015 }
20016 else
20017 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020018 tv.v_type = VAR_NUMBER;
20019 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020021 set_var(virp->vir_line + 1, &tv, FALSE);
20022 if (is_string)
20023 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 }
20025 }
20026 }
20027
20028 return viminfo_readline(virp);
20029}
20030
20031/*
20032 * Write global vars that start with a capital to the viminfo file
20033 */
20034 void
20035write_viminfo_varlist(fp)
20036 FILE *fp;
20037{
Bram Moolenaar33570922005-01-25 22:26:29 +000020038 hashitem_T *hi;
20039 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020040 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020041 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020042 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020043 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020044 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045
20046 if (find_viminfo_parameter('!') == NULL)
20047 return;
20048
20049 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020050
Bram Moolenaar33570922005-01-25 22:26:29 +000020051 todo = globvarht.ht_used;
20052 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020054 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020056 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020057 this_var = HI2DI(hi);
20058 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020059 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020060 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020061 {
20062 case VAR_STRING: s = "STR"; break;
20063 case VAR_NUMBER: s = "NUM"; break;
20064 default: continue;
20065 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020066 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020067 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020068 if (p != NULL)
20069 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020070 vim_free(tofree);
20071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 }
20073 }
20074}
20075#endif
20076
20077#if defined(FEAT_SESSION) || defined(PROTO)
20078 int
20079store_session_globals(fd)
20080 FILE *fd;
20081{
Bram Moolenaar33570922005-01-25 22:26:29 +000020082 hashitem_T *hi;
20083 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020084 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 char_u *p, *t;
20086
Bram Moolenaar33570922005-01-25 22:26:29 +000020087 todo = globvarht.ht_used;
20088 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020090 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020092 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020093 this_var = HI2DI(hi);
20094 if ((this_var->di_tv.v_type == VAR_NUMBER
20095 || this_var->di_tv.v_type == VAR_STRING)
20096 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020097 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020098 /* Escape special characters with a backslash. Turn a LF and
20099 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020100 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020101 (char_u *)"\\\"\n\r");
20102 if (p == NULL) /* out of memory */
20103 break;
20104 for (t = p; *t != NUL; ++t)
20105 if (*t == '\n')
20106 *t = 'n';
20107 else if (*t == '\r')
20108 *t = 'r';
20109 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020110 this_var->di_key,
20111 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20112 : ' ',
20113 p,
20114 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20115 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020116 || put_eol(fd) == FAIL)
20117 {
20118 vim_free(p);
20119 return FAIL;
20120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 vim_free(p);
20122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123 }
20124 }
20125 return OK;
20126}
20127#endif
20128
Bram Moolenaar661b1822005-07-28 22:36:45 +000020129/*
20130 * Display script name where an item was last set.
20131 * Should only be invoked when 'verbose' is non-zero.
20132 */
20133 void
20134last_set_msg(scriptID)
20135 scid_T scriptID;
20136{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020137 char_u *p;
20138
Bram Moolenaar661b1822005-07-28 22:36:45 +000020139 if (scriptID != 0)
20140 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020141 p = home_replace_save(NULL, get_scriptname(scriptID));
20142 if (p != NULL)
20143 {
20144 verbose_enter();
20145 MSG_PUTS(_("\n\tLast set from "));
20146 MSG_PUTS(p);
20147 vim_free(p);
20148 verbose_leave();
20149 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020150 }
20151}
20152
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153#endif /* FEAT_EVAL */
20154
20155#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20156
20157
20158#ifdef WIN3264
20159/*
20160 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20161 */
20162static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20163static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20164static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20165
20166/*
20167 * Get the short pathname of a file.
20168 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20169 */
20170 static int
20171get_short_pathname(fnamep, bufp, fnamelen)
20172 char_u **fnamep;
20173 char_u **bufp;
20174 int *fnamelen;
20175{
20176 int l,len;
20177 char_u *newbuf;
20178
20179 len = *fnamelen;
20180
20181 l = GetShortPathName(*fnamep, *fnamep, len);
20182 if (l > len - 1)
20183 {
20184 /* If that doesn't work (not enough space), then save the string
20185 * and try again with a new buffer big enough
20186 */
20187 newbuf = vim_strnsave(*fnamep, l);
20188 if (newbuf == NULL)
20189 return 0;
20190
20191 vim_free(*bufp);
20192 *fnamep = *bufp = newbuf;
20193
20194 l = GetShortPathName(*fnamep,*fnamep,l+1);
20195
20196 /* Really should always succeed, as the buffer is big enough */
20197 }
20198
20199 *fnamelen = l;
20200 return 1;
20201}
20202
20203/*
20204 * Create a short path name. Returns the length of the buffer it needs.
20205 * Doesn't copy over the end of the buffer passed in.
20206 */
20207 static int
20208shortpath_for_invalid_fname(fname, bufp, fnamelen)
20209 char_u **fname;
20210 char_u **bufp;
20211 int *fnamelen;
20212{
20213 char_u *s, *p, *pbuf2, *pbuf3;
20214 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020215 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216
20217 /* Make a copy */
20218 len2 = *fnamelen;
20219 pbuf2 = vim_strnsave(*fname, len2);
20220 pbuf3 = NULL;
20221
20222 s = pbuf2 + len2 - 1; /* Find the end */
20223 slen = 1;
20224 plen = len2;
20225
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020226 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 {
20228 --s;
20229 ++slen;
20230 --plen;
20231 }
20232
20233 do
20234 {
20235 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020236 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 {
20238 --s;
20239 ++slen;
20240 --plen;
20241 }
20242 if (s <= pbuf2)
20243 break;
20244
20245 /* Remeber the character that is about to be blatted */
20246 ch = *s;
20247 *s = 0; /* get_short_pathname requires a null-terminated string */
20248
20249 /* Try it in situ */
20250 p = pbuf2;
20251 if (!get_short_pathname(&p, &pbuf3, &plen))
20252 {
20253 vim_free(pbuf2);
20254 return -1;
20255 }
20256 *s = ch; /* Preserve the string */
20257 } while (plen == 0);
20258
20259 if (plen > 0)
20260 {
20261 /* Remeber the length of the new string. */
20262 *fnamelen = len = plen + slen;
20263 vim_free(*bufp);
20264 if (len > len2)
20265 {
20266 /* If there's not enough space in the currently allocated string,
20267 * then copy it to a buffer big enough.
20268 */
20269 *fname= *bufp = vim_strnsave(p, len);
20270 if (*fname == NULL)
20271 return -1;
20272 }
20273 else
20274 {
20275 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20276 *fname = *bufp = pbuf2;
20277 if (p != pbuf2)
20278 strncpy(*fname, p, plen);
20279 pbuf2 = NULL;
20280 }
20281 /* Concat the next bit */
20282 strncpy(*fname + plen, s, slen);
20283 (*fname)[len] = '\0';
20284 }
20285 vim_free(pbuf3);
20286 vim_free(pbuf2);
20287 return 0;
20288}
20289
20290/*
20291 * Get a pathname for a partial path.
20292 */
20293 static int
20294shortpath_for_partial(fnamep, bufp, fnamelen)
20295 char_u **fnamep;
20296 char_u **bufp;
20297 int *fnamelen;
20298{
20299 int sepcount, len, tflen;
20300 char_u *p;
20301 char_u *pbuf, *tfname;
20302 int hasTilde;
20303
20304 /* Count up the path seperators from the RHS.. so we know which part
20305 * of the path to return.
20306 */
20307 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020308 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309 if (vim_ispathsep(*p))
20310 ++sepcount;
20311
20312 /* Need full path first (use expand_env() to remove a "~/") */
20313 hasTilde = (**fnamep == '~');
20314 if (hasTilde)
20315 pbuf = tfname = expand_env_save(*fnamep);
20316 else
20317 pbuf = tfname = FullName_save(*fnamep, FALSE);
20318
20319 len = tflen = STRLEN(tfname);
20320
20321 if (!get_short_pathname(&tfname, &pbuf, &len))
20322 return -1;
20323
20324 if (len == 0)
20325 {
20326 /* Don't have a valid filename, so shorten the rest of the
20327 * path if we can. This CAN give us invalid 8.3 filenames, but
20328 * there's not a lot of point in guessing what it might be.
20329 */
20330 len = tflen;
20331 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20332 return -1;
20333 }
20334
20335 /* Count the paths backward to find the beginning of the desired string. */
20336 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020337 {
20338#ifdef FEAT_MBYTE
20339 if (has_mbyte)
20340 p -= mb_head_off(tfname, p);
20341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 if (vim_ispathsep(*p))
20343 {
20344 if (sepcount == 0 || (hasTilde && sepcount == 1))
20345 break;
20346 else
20347 sepcount --;
20348 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 if (hasTilde)
20351 {
20352 --p;
20353 if (p >= tfname)
20354 *p = '~';
20355 else
20356 return -1;
20357 }
20358 else
20359 ++p;
20360
20361 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20362 vim_free(*bufp);
20363 *fnamelen = (int)STRLEN(p);
20364 *bufp = pbuf;
20365 *fnamep = p;
20366
20367 return 0;
20368}
20369#endif /* WIN3264 */
20370
20371/*
20372 * Adjust a filename, according to a string of modifiers.
20373 * *fnamep must be NUL terminated when called. When returning, the length is
20374 * determined by *fnamelen.
20375 * Returns valid flags.
20376 * When there is an error, *fnamep is set to NULL.
20377 */
20378 int
20379modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20380 char_u *src; /* string with modifiers */
20381 int *usedlen; /* characters after src that are used */
20382 char_u **fnamep; /* file name so far */
20383 char_u **bufp; /* buffer for allocated file name or NULL */
20384 int *fnamelen; /* length of fnamep */
20385{
20386 int valid = 0;
20387 char_u *tail;
20388 char_u *s, *p, *pbuf;
20389 char_u dirname[MAXPATHL];
20390 int c;
20391 int has_fullname = 0;
20392#ifdef WIN3264
20393 int has_shortname = 0;
20394#endif
20395
20396repeat:
20397 /* ":p" - full path/file_name */
20398 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20399 {
20400 has_fullname = 1;
20401
20402 valid |= VALID_PATH;
20403 *usedlen += 2;
20404
20405 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20406 if ((*fnamep)[0] == '~'
20407#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20408 && ((*fnamep)[1] == '/'
20409# ifdef BACKSLASH_IN_FILENAME
20410 || (*fnamep)[1] == '\\'
20411# endif
20412 || (*fnamep)[1] == NUL)
20413
20414#endif
20415 )
20416 {
20417 *fnamep = expand_env_save(*fnamep);
20418 vim_free(*bufp); /* free any allocated file name */
20419 *bufp = *fnamep;
20420 if (*fnamep == NULL)
20421 return -1;
20422 }
20423
20424 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020425 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 {
20427 if (vim_ispathsep(*p)
20428 && p[1] == '.'
20429 && (p[2] == NUL
20430 || vim_ispathsep(p[2])
20431 || (p[2] == '.'
20432 && (p[3] == NUL || vim_ispathsep(p[3])))))
20433 break;
20434 }
20435
20436 /* FullName_save() is slow, don't use it when not needed. */
20437 if (*p != NUL || !vim_isAbsName(*fnamep))
20438 {
20439 *fnamep = FullName_save(*fnamep, *p != NUL);
20440 vim_free(*bufp); /* free any allocated file name */
20441 *bufp = *fnamep;
20442 if (*fnamep == NULL)
20443 return -1;
20444 }
20445
20446 /* Append a path separator to a directory. */
20447 if (mch_isdir(*fnamep))
20448 {
20449 /* Make room for one or two extra characters. */
20450 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20451 vim_free(*bufp); /* free any allocated file name */
20452 *bufp = *fnamep;
20453 if (*fnamep == NULL)
20454 return -1;
20455 add_pathsep(*fnamep);
20456 }
20457 }
20458
20459 /* ":." - path relative to the current directory */
20460 /* ":~" - path relative to the home directory */
20461 /* ":8" - shortname path - postponed till after */
20462 while (src[*usedlen] == ':'
20463 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20464 {
20465 *usedlen += 2;
20466 if (c == '8')
20467 {
20468#ifdef WIN3264
20469 has_shortname = 1; /* Postpone this. */
20470#endif
20471 continue;
20472 }
20473 pbuf = NULL;
20474 /* Need full path first (use expand_env() to remove a "~/") */
20475 if (!has_fullname)
20476 {
20477 if (c == '.' && **fnamep == '~')
20478 p = pbuf = expand_env_save(*fnamep);
20479 else
20480 p = pbuf = FullName_save(*fnamep, FALSE);
20481 }
20482 else
20483 p = *fnamep;
20484
20485 has_fullname = 0;
20486
20487 if (p != NULL)
20488 {
20489 if (c == '.')
20490 {
20491 mch_dirname(dirname, MAXPATHL);
20492 s = shorten_fname(p, dirname);
20493 if (s != NULL)
20494 {
20495 *fnamep = s;
20496 if (pbuf != NULL)
20497 {
20498 vim_free(*bufp); /* free any allocated file name */
20499 *bufp = pbuf;
20500 pbuf = NULL;
20501 }
20502 }
20503 }
20504 else
20505 {
20506 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20507 /* Only replace it when it starts with '~' */
20508 if (*dirname == '~')
20509 {
20510 s = vim_strsave(dirname);
20511 if (s != NULL)
20512 {
20513 *fnamep = s;
20514 vim_free(*bufp);
20515 *bufp = s;
20516 }
20517 }
20518 }
20519 vim_free(pbuf);
20520 }
20521 }
20522
20523 tail = gettail(*fnamep);
20524 *fnamelen = (int)STRLEN(*fnamep);
20525
20526 /* ":h" - head, remove "/file_name", can be repeated */
20527 /* Don't remove the first "/" or "c:\" */
20528 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20529 {
20530 valid |= VALID_HEAD;
20531 *usedlen += 2;
20532 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020533 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 --tail;
20535 *fnamelen = (int)(tail - *fnamep);
20536#ifdef VMS
20537 if (*fnamelen > 0)
20538 *fnamelen += 1; /* the path separator is part of the path */
20539#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020540 while (tail > s && !after_pathsep(s, tail))
20541 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542 }
20543
20544 /* ":8" - shortname */
20545 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20546 {
20547 *usedlen += 2;
20548#ifdef WIN3264
20549 has_shortname = 1;
20550#endif
20551 }
20552
20553#ifdef WIN3264
20554 /* Check shortname after we have done 'heads' and before we do 'tails'
20555 */
20556 if (has_shortname)
20557 {
20558 pbuf = NULL;
20559 /* Copy the string if it is shortened by :h */
20560 if (*fnamelen < (int)STRLEN(*fnamep))
20561 {
20562 p = vim_strnsave(*fnamep, *fnamelen);
20563 if (p == 0)
20564 return -1;
20565 vim_free(*bufp);
20566 *bufp = *fnamep = p;
20567 }
20568
20569 /* Split into two implementations - makes it easier. First is where
20570 * there isn't a full name already, second is where there is.
20571 */
20572 if (!has_fullname && !vim_isAbsName(*fnamep))
20573 {
20574 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20575 return -1;
20576 }
20577 else
20578 {
20579 int l;
20580
20581 /* Simple case, already have the full-name
20582 * Nearly always shorter, so try first time. */
20583 l = *fnamelen;
20584 if (!get_short_pathname(fnamep, bufp, &l))
20585 return -1;
20586
20587 if (l == 0)
20588 {
20589 /* Couldn't find the filename.. search the paths.
20590 */
20591 l = *fnamelen;
20592 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20593 return -1;
20594 }
20595 *fnamelen = l;
20596 }
20597 }
20598#endif /* WIN3264 */
20599
20600 /* ":t" - tail, just the basename */
20601 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20602 {
20603 *usedlen += 2;
20604 *fnamelen -= (int)(tail - *fnamep);
20605 *fnamep = tail;
20606 }
20607
20608 /* ":e" - extension, can be repeated */
20609 /* ":r" - root, without extension, can be repeated */
20610 while (src[*usedlen] == ':'
20611 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20612 {
20613 /* find a '.' in the tail:
20614 * - for second :e: before the current fname
20615 * - otherwise: The last '.'
20616 */
20617 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20618 s = *fnamep - 2;
20619 else
20620 s = *fnamep + *fnamelen - 1;
20621 for ( ; s > tail; --s)
20622 if (s[0] == '.')
20623 break;
20624 if (src[*usedlen + 1] == 'e') /* :e */
20625 {
20626 if (s > tail)
20627 {
20628 *fnamelen += (int)(*fnamep - (s + 1));
20629 *fnamep = s + 1;
20630#ifdef VMS
20631 /* cut version from the extension */
20632 s = *fnamep + *fnamelen - 1;
20633 for ( ; s > *fnamep; --s)
20634 if (s[0] == ';')
20635 break;
20636 if (s > *fnamep)
20637 *fnamelen = s - *fnamep;
20638#endif
20639 }
20640 else if (*fnamep <= tail)
20641 *fnamelen = 0;
20642 }
20643 else /* :r */
20644 {
20645 if (s > tail) /* remove one extension */
20646 *fnamelen = (int)(s - *fnamep);
20647 }
20648 *usedlen += 2;
20649 }
20650
20651 /* ":s?pat?foo?" - substitute */
20652 /* ":gs?pat?foo?" - global substitute */
20653 if (src[*usedlen] == ':'
20654 && (src[*usedlen + 1] == 's'
20655 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20656 {
20657 char_u *str;
20658 char_u *pat;
20659 char_u *sub;
20660 int sep;
20661 char_u *flags;
20662 int didit = FALSE;
20663
20664 flags = (char_u *)"";
20665 s = src + *usedlen + 2;
20666 if (src[*usedlen + 1] == 'g')
20667 {
20668 flags = (char_u *)"g";
20669 ++s;
20670 }
20671
20672 sep = *s++;
20673 if (sep)
20674 {
20675 /* find end of pattern */
20676 p = vim_strchr(s, sep);
20677 if (p != NULL)
20678 {
20679 pat = vim_strnsave(s, (int)(p - s));
20680 if (pat != NULL)
20681 {
20682 s = p + 1;
20683 /* find end of substitution */
20684 p = vim_strchr(s, sep);
20685 if (p != NULL)
20686 {
20687 sub = vim_strnsave(s, (int)(p - s));
20688 str = vim_strnsave(*fnamep, *fnamelen);
20689 if (sub != NULL && str != NULL)
20690 {
20691 *usedlen = (int)(p + 1 - src);
20692 s = do_string_sub(str, pat, sub, flags);
20693 if (s != NULL)
20694 {
20695 *fnamep = s;
20696 *fnamelen = (int)STRLEN(s);
20697 vim_free(*bufp);
20698 *bufp = s;
20699 didit = TRUE;
20700 }
20701 }
20702 vim_free(sub);
20703 vim_free(str);
20704 }
20705 vim_free(pat);
20706 }
20707 }
20708 /* after using ":s", repeat all the modifiers */
20709 if (didit)
20710 goto repeat;
20711 }
20712 }
20713
20714 return valid;
20715}
20716
20717/*
20718 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20719 * "flags" can be "g" to do a global substitute.
20720 * Returns an allocated string, NULL for error.
20721 */
20722 char_u *
20723do_string_sub(str, pat, sub, flags)
20724 char_u *str;
20725 char_u *pat;
20726 char_u *sub;
20727 char_u *flags;
20728{
20729 int sublen;
20730 regmatch_T regmatch;
20731 int i;
20732 int do_all;
20733 char_u *tail;
20734 garray_T ga;
20735 char_u *ret;
20736 char_u *save_cpo;
20737
20738 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20739 save_cpo = p_cpo;
20740 p_cpo = (char_u *)"";
20741
20742 ga_init2(&ga, 1, 200);
20743
20744 do_all = (flags[0] == 'g');
20745
20746 regmatch.rm_ic = p_ic;
20747 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20748 if (regmatch.regprog != NULL)
20749 {
20750 tail = str;
20751 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20752 {
20753 /*
20754 * Get some space for a temporary buffer to do the substitution
20755 * into. It will contain:
20756 * - The text up to where the match is.
20757 * - The substituted text.
20758 * - The text after the match.
20759 */
20760 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20761 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20762 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20763 {
20764 ga_clear(&ga);
20765 break;
20766 }
20767
20768 /* copy the text up to where the match is */
20769 i = (int)(regmatch.startp[0] - tail);
20770 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20771 /* add the substituted text */
20772 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20773 + ga.ga_len + i, TRUE, TRUE, FALSE);
20774 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775 /* avoid getting stuck on a match with an empty string */
20776 if (tail == regmatch.endp[0])
20777 {
20778 if (*tail == NUL)
20779 break;
20780 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20781 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782 }
20783 else
20784 {
20785 tail = regmatch.endp[0];
20786 if (*tail == NUL)
20787 break;
20788 }
20789 if (!do_all)
20790 break;
20791 }
20792
20793 if (ga.ga_data != NULL)
20794 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20795
20796 vim_free(regmatch.regprog);
20797 }
20798
20799 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20800 ga_clear(&ga);
20801 p_cpo = save_cpo;
20802
20803 return ret;
20804}
20805
20806#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */