blob: fdb7e81a82b088037b360c4704f986406550e559 [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));
373static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
374static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
375static int check_changedtick __ARGS((char_u *arg));
376static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
377static void clear_lval __ARGS((lval_T *lp));
378static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
379static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
380static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
381static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
382static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
383static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
384static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
385static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
386static void item_lock __ARGS((typval_T *tv, int deep, int lock));
387static int tv_islocked __ARGS((typval_T *tv));
388
Bram Moolenaar33570922005-01-25 22:26:29 +0000389static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
390static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000398static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000403static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000404static listitem_T *listitem_alloc __ARGS((void));
405static void listitem_free __ARGS((listitem_T *item));
406static void listitem_remove __ARGS((list_T *l, listitem_T *item));
407static long list_len __ARGS((list_T *l));
408static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
409static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
410static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000411static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000412static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static void list_append __ARGS((list_T *l, listitem_T *item));
415static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000416static int list_append_string __ARGS((list_T *l, char_u *str, int len));
417static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
419static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
420static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000421static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000423static char_u *list2string __ARGS((typval_T *tv, int copyID));
424static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000425static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
426static void set_ref_in_list __ARGS((list_T *l, int copyID));
427static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static void dict_unref __ARGS((dict_T *d));
429static void dict_free __ARGS((dict_T *d));
430static dictitem_T *dictitem_alloc __ARGS((char_u *key));
431static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
432static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
433static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int dict_add __ARGS((dict_T *d, dictitem_T *item));
436static long dict_len __ARGS((dict_T *d));
437static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
441static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static char_u *string_quote __ARGS((char_u *str, int function));
443static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
444static int find_internal_func __ARGS((char_u *name));
445static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
446static 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));
447static 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 +0000448static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449
450static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000469#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000470static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000471static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
473#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000474static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
479static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000505static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000507static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000513static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000514static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000521static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000522static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000523static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000545static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000551static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000552static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000568static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000572#ifdef vim_mkdir
573static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
574#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000575static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000579static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000580static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000582static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000594static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000596static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000598static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000603static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000604static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000605static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000610static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000611static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
614#ifdef HAVE_STRFTIME
615static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
616#endif
617static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000629static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000630static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000631static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000632static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000633static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000635static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000649static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000652static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000654static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
655static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static int get_env_len __ARGS((char_u **arg));
657static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000659static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
660#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
661#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
662 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000663static 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 +0000664static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000665static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000666static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
667static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static typval_T *alloc_tv __ARGS((void));
669static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static void init_tv __ARGS((typval_T *varp));
671static long get_tv_number __ARGS((typval_T *varp));
672static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000673static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static char_u *get_tv_string __ARGS((typval_T *varp));
675static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000676static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000677static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000678static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
680static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
681static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
682static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
683static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
684static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
685static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000686static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000688static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
690static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
691static int eval_fname_script __ARGS((char_u *p));
692static int eval_fname_sid __ARGS((char_u *p));
693static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static ufunc_T *find_func __ARGS((char_u *name));
695static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000696static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697#ifdef FEAT_PROFILE
698static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000699static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
700static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
701static int
702# ifdef __BORLANDC__
703 _RTLENTRYF
704# endif
705 prof_total_cmp __ARGS((const void *s1, const void *s2));
706static int
707# ifdef __BORLANDC__
708 _RTLENTRYF
709# endif
710 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000711#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000712static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000713static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000714static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static void func_free __ARGS((ufunc_T *fp));
716static void func_unref __ARGS((char_u *name));
717static void func_ref __ARGS((char_u *name));
718static 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));
719static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000720static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000721static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000722static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000723
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000724/* Character used as separated in autoload function/variable names. */
725#define AUTOLOAD_CHAR '#'
726
Bram Moolenaar33570922005-01-25 22:26:29 +0000727/*
728 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000729 */
730 void
731eval_init()
732{
Bram Moolenaar33570922005-01-25 22:26:29 +0000733 int i;
734 struct vimvar *p;
735
736 init_var_dict(&globvardict, &globvars_var);
737 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000738 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000739 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000740
741 for (i = 0; i < VV_LEN; ++i)
742 {
743 p = &vimvars[i];
744 STRCPY(p->vv_di.di_key, p->vv_name);
745 if (p->vv_flags & VV_RO)
746 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
747 else if (p->vv_flags & VV_RO_SBX)
748 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
749 else
750 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000751
752 /* add to v: scope dict, unless the value is not always available */
753 if (p->vv_type != VAR_UNKNOWN)
754 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000755 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000756 /* add to compat scope dict */
757 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000758 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000759}
760
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000761#if defined(EXITFREE) || defined(PROTO)
762 void
763eval_clear()
764{
765 int i;
766 struct vimvar *p;
767
768 for (i = 0; i < VV_LEN; ++i)
769 {
770 p = &vimvars[i];
771 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000772 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000773 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000774 p->vv_di.di_tv.vval.v_string = NULL;
775 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000776 }
777 hash_clear(&vimvarht);
778 hash_clear(&compat_hashtab);
779
780 /* script-local variables */
781 for (i = 1; i <= ga_scripts.ga_len; ++i)
782 vars_clear(&SCRIPT_VARS(i));
783 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000784 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000785
786 /* global variables */
787 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000788
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000789 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000790 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000791 hash_clear(&func_hashtab);
792
793 /* unreferenced lists and dicts */
794 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000795}
796#endif
797
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 * Return the name of the executed function.
800 */
801 char_u *
802func_name(cookie)
803 void *cookie;
804{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000805 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806}
807
808/*
809 * Return the address holding the next breakpoint line for a funccall cookie.
810 */
811 linenr_T *
812func_breakpoint(cookie)
813 void *cookie;
814{
Bram Moolenaar33570922005-01-25 22:26:29 +0000815 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816}
817
818/*
819 * Return the address holding the debug tick for a funccall cookie.
820 */
821 int *
822func_dbg_tick(cookie)
823 void *cookie;
824{
Bram Moolenaar33570922005-01-25 22:26:29 +0000825 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826}
827
828/*
829 * Return the nesting level for a funccall cookie.
830 */
831 int
832func_level(cookie)
833 void *cookie;
834{
Bram Moolenaar33570922005-01-25 22:26:29 +0000835 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836}
837
838/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000839funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
841/*
842 * Return TRUE when a function was ended by a ":return" command.
843 */
844 int
845current_func_returned()
846{
847 return current_funccal->returned;
848}
849
850
851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 * Set an internal variable to a string value. Creates the variable if it does
853 * not already exist.
854 */
855 void
856set_internal_string_var(name, value)
857 char_u *name;
858 char_u *value;
859{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000860 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000861 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
863 val = vim_strsave(value);
864 if (val != NULL)
865 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000866 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000867 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000869 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000870 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
872 }
873}
874
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000875static lval_T *redir_lval = NULL;
876static char_u *redir_endp = NULL;
877static char_u *redir_varname = NULL;
878
879/*
880 * Start recording command output to a variable
881 * Returns OK if successfully completed the setup. FAIL otherwise.
882 */
883 int
884var_redir_start(name, append)
885 char_u *name;
886 int append; /* append to an existing variable */
887{
888 int save_emsg;
889 int err;
890 typval_T tv;
891
892 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000893 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000894 {
895 EMSG(_(e_invarg));
896 return FAIL;
897 }
898
899 redir_varname = vim_strsave(name);
900 if (redir_varname == NULL)
901 return FAIL;
902
903 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
904 if (redir_lval == NULL)
905 {
906 var_redir_stop();
907 return FAIL;
908 }
909
910 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000911 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
912 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000913 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
914 {
915 if (redir_endp != NULL && *redir_endp != NUL)
916 /* Trailing characters are present after the variable name */
917 EMSG(_(e_trailing));
918 else
919 EMSG(_(e_invarg));
920 var_redir_stop();
921 return FAIL;
922 }
923
924 /* check if we can write to the variable: set it to or append an empty
925 * string */
926 save_emsg = did_emsg;
927 did_emsg = FALSE;
928 tv.v_type = VAR_STRING;
929 tv.vval.v_string = (char_u *)"";
930 if (append)
931 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
932 else
933 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
934 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000935 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000936 if (err)
937 {
938 var_redir_stop();
939 return FAIL;
940 }
941 if (redir_lval->ll_newkey != NULL)
942 {
943 /* Dictionary item was created, don't do it again. */
944 vim_free(redir_lval->ll_newkey);
945 redir_lval->ll_newkey = NULL;
946 }
947
948 return OK;
949}
950
951/*
952 * Append "value[len]" to the variable set by var_redir_start().
953 */
954 void
955var_redir_str(value, len)
956 char_u *value;
957 int len;
958{
959 char_u *val;
960 typval_T tv;
961 int save_emsg;
962 int err;
963
964 if (redir_lval == NULL)
965 return;
966
967 if (len == -1)
968 /* Append the entire string */
969 val = vim_strsave(value);
970 else
971 /* Append only the specified number of characters */
972 val = vim_strnsave(value, len);
973 if (val == NULL)
974 return;
975
976 tv.v_type = VAR_STRING;
977 tv.vval.v_string = val;
978
979 save_emsg = did_emsg;
980 did_emsg = FALSE;
981 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
982 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000983 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000984 if (err)
985 var_redir_stop();
986
987 vim_free(tv.vval.v_string);
988}
989
990/*
991 * Stop redirecting command output to a variable.
992 */
993 void
994var_redir_stop()
995{
996 if (redir_lval != NULL)
997 {
998 clear_lval(redir_lval);
999 vim_free(redir_lval);
1000 redir_lval = NULL;
1001 }
1002 vim_free(redir_varname);
1003 redir_varname = NULL;
1004}
1005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006# if defined(FEAT_MBYTE) || defined(PROTO)
1007 int
1008eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1009 char_u *enc_from;
1010 char_u *enc_to;
1011 char_u *fname_from;
1012 char_u *fname_to;
1013{
1014 int err = FALSE;
1015
1016 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1017 set_vim_var_string(VV_CC_TO, enc_to, -1);
1018 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1019 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1020 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1021 err = TRUE;
1022 set_vim_var_string(VV_CC_FROM, NULL, -1);
1023 set_vim_var_string(VV_CC_TO, NULL, -1);
1024 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1025 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1026
1027 if (err)
1028 return FAIL;
1029 return OK;
1030}
1031# endif
1032
1033# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1034 int
1035eval_printexpr(fname, args)
1036 char_u *fname;
1037 char_u *args;
1038{
1039 int err = FALSE;
1040
1041 set_vim_var_string(VV_FNAME_IN, fname, -1);
1042 set_vim_var_string(VV_CMDARG, args, -1);
1043 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1044 err = TRUE;
1045 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1046 set_vim_var_string(VV_CMDARG, NULL, -1);
1047
1048 if (err)
1049 {
1050 mch_remove(fname);
1051 return FAIL;
1052 }
1053 return OK;
1054}
1055# endif
1056
1057# if defined(FEAT_DIFF) || defined(PROTO)
1058 void
1059eval_diff(origfile, newfile, outfile)
1060 char_u *origfile;
1061 char_u *newfile;
1062 char_u *outfile;
1063{
1064 int err = FALSE;
1065
1066 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1067 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1068 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1069 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1070 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1071 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1072 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1073}
1074
1075 void
1076eval_patch(origfile, difffile, outfile)
1077 char_u *origfile;
1078 char_u *difffile;
1079 char_u *outfile;
1080{
1081 int err;
1082
1083 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1084 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1085 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1086 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1087 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1088 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1089 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1090}
1091# endif
1092
1093/*
1094 * Top level evaluation function, returning a boolean.
1095 * Sets "error" to TRUE if there was an error.
1096 * Return TRUE or FALSE.
1097 */
1098 int
1099eval_to_bool(arg, error, nextcmd, skip)
1100 char_u *arg;
1101 int *error;
1102 char_u **nextcmd;
1103 int skip; /* only parse, don't execute */
1104{
Bram Moolenaar33570922005-01-25 22:26:29 +00001105 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 int retval = FALSE;
1107
1108 if (skip)
1109 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001110 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 else
1113 {
1114 *error = FALSE;
1115 if (!skip)
1116 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001117 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001118 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120 }
1121 if (skip)
1122 --emsg_skip;
1123
1124 return retval;
1125}
1126
1127/*
1128 * Top level evaluation function, returning a string. If "skip" is TRUE,
1129 * only parsing to "nextcmd" is done, without reporting errors. Return
1130 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1131 */
1132 char_u *
1133eval_to_string_skip(arg, nextcmd, skip)
1134 char_u *arg;
1135 char_u **nextcmd;
1136 int skip; /* only parse, don't execute */
1137{
Bram Moolenaar33570922005-01-25 22:26:29 +00001138 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 char_u *retval;
1140
1141 if (skip)
1142 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001143 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 retval = NULL;
1145 else
1146 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001147 retval = vim_strsave(get_tv_string(&tv));
1148 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 }
1150 if (skip)
1151 --emsg_skip;
1152
1153 return retval;
1154}
1155
1156/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001157 * Skip over an expression at "*pp".
1158 * Return FAIL for an error, OK otherwise.
1159 */
1160 int
1161skip_expr(pp)
1162 char_u **pp;
1163{
Bram Moolenaar33570922005-01-25 22:26:29 +00001164 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001165
1166 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001167 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001168}
1169
1170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 * Top level evaluation function, returning a string.
1172 * Return pointer to allocated memory, or NULL for failure.
1173 */
1174 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001175eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 char_u *arg;
1177 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001178 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179{
Bram Moolenaar33570922005-01-25 22:26:29 +00001180 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001182 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001184 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 retval = NULL;
1186 else
1187 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001188 if (dolist && tv.v_type == VAR_LIST)
1189 {
1190 ga_init2(&ga, (int)sizeof(char), 80);
1191 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1192 ga_append(&ga, NUL);
1193 retval = (char_u *)ga.ga_data;
1194 }
1195 else
1196 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001197 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 }
1199
1200 return retval;
1201}
1202
1203/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001204 * Call eval_to_string() without using current local variables and using
1205 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 */
1207 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001208eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 char_u *arg;
1210 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001211 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212{
1213 char_u *retval;
1214 void *save_funccalp;
1215
1216 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001217 if (use_sandbox)
1218 ++sandbox;
1219 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001220 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001221 if (use_sandbox)
1222 --sandbox;
1223 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 restore_funccal(save_funccalp);
1225 return retval;
1226}
1227
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228/*
1229 * Top level evaluation function, returning a number.
1230 * Evaluates "expr" silently.
1231 * Returns -1 for an error.
1232 */
1233 int
1234eval_to_number(expr)
1235 char_u *expr;
1236{
Bram Moolenaar33570922005-01-25 22:26:29 +00001237 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001239 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
1241 ++emsg_off;
1242
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001243 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 retval = -1;
1245 else
1246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001247 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001248 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 }
1250 --emsg_off;
1251
1252 return retval;
1253}
1254
Bram Moolenaara40058a2005-07-11 22:42:07 +00001255/*
1256 * Prepare v: variable "idx" to be used.
1257 * Save the current typeval in "save_tv".
1258 * When not used yet add the variable to the v: hashtable.
1259 */
1260 static void
1261prepare_vimvar(idx, save_tv)
1262 int idx;
1263 typval_T *save_tv;
1264{
1265 *save_tv = vimvars[idx].vv_tv;
1266 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1267 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1268}
1269
1270/*
1271 * Restore v: variable "idx" to typeval "save_tv".
1272 * When no longer defined, remove the variable from the v: hashtable.
1273 */
1274 static void
1275restore_vimvar(idx, save_tv)
1276 int idx;
1277 typval_T *save_tv;
1278{
1279 hashitem_T *hi;
1280
1281 clear_tv(&vimvars[idx].vv_tv);
1282 vimvars[idx].vv_tv = *save_tv;
1283 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1284 {
1285 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1286 if (HASHITEM_EMPTY(hi))
1287 EMSG2(_(e_intern2), "restore_vimvar()");
1288 else
1289 hash_remove(&vimvarht, hi);
1290 }
1291}
1292
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001293#if defined(FEAT_SYN_HL) || defined(PROTO)
1294/*
1295 * Evaluate an expression to a list with suggestions.
1296 * For the "expr:" part of 'spellsuggest'.
1297 */
1298 list_T *
1299eval_spell_expr(badword, expr)
1300 char_u *badword;
1301 char_u *expr;
1302{
1303 typval_T save_val;
1304 typval_T rettv;
1305 list_T *list = NULL;
1306 char_u *p = skipwhite(expr);
1307
1308 /* Set "v:val" to the bad word. */
1309 prepare_vimvar(VV_VAL, &save_val);
1310 vimvars[VV_VAL].vv_type = VAR_STRING;
1311 vimvars[VV_VAL].vv_str = badword;
1312 if (p_verbose == 0)
1313 ++emsg_off;
1314
1315 if (eval1(&p, &rettv, TRUE) == OK)
1316 {
1317 if (rettv.v_type != VAR_LIST)
1318 clear_tv(&rettv);
1319 else
1320 list = rettv.vval.v_list;
1321 }
1322
1323 if (p_verbose == 0)
1324 --emsg_off;
1325 vimvars[VV_VAL].vv_str = NULL;
1326 restore_vimvar(VV_VAL, &save_val);
1327
1328 return list;
1329}
1330
1331/*
1332 * "list" is supposed to contain two items: a word and a number. Return the
1333 * word in "pp" and the number as the return value.
1334 * Return -1 if anything isn't right.
1335 * Used to get the good word and score from the eval_spell_expr() result.
1336 */
1337 int
1338get_spellword(list, pp)
1339 list_T *list;
1340 char_u **pp;
1341{
1342 listitem_T *li;
1343
1344 li = list->lv_first;
1345 if (li == NULL)
1346 return -1;
1347 *pp = get_tv_string(&li->li_tv);
1348
1349 li = li->li_next;
1350 if (li == NULL)
1351 return -1;
1352 return get_tv_number(&li->li_tv);
1353}
1354#endif
1355
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001356/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001357 * Top level evaluation function.
1358 * Returns an allocated typval_T with the result.
1359 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001360 */
1361 typval_T *
1362eval_expr(arg, nextcmd)
1363 char_u *arg;
1364 char_u **nextcmd;
1365{
1366 typval_T *tv;
1367
1368 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001369 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001370 {
1371 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001372 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001373 }
1374
1375 return tv;
1376}
1377
1378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1380/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001381 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001383 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001385 static int
1386call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 char_u *func;
1388 int argc;
1389 char_u **argv;
1390 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
Bram Moolenaar33570922005-01-25 22:26:29 +00001393 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 long n;
1395 int len;
1396 int i;
1397 int doesrange;
1398 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001399 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400
Bram Moolenaar33570922005-01-25 22:26:29 +00001401 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001403 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
1405 for (i = 0; i < argc; i++)
1406 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001407 /* Pass a NULL or empty argument as an empty string */
1408 if (argv[i] == NULL || *argv[i] == NUL)
1409 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001410 argvars[i].v_type = VAR_STRING;
1411 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001412 continue;
1413 }
1414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 /* Recognize a number argument, the others must be strings. */
1416 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1417 if (len != 0 && len == (int)STRLEN(argv[i]))
1418 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001419 argvars[i].v_type = VAR_NUMBER;
1420 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 }
1422 else
1423 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001424 argvars[i].v_type = VAR_STRING;
1425 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 }
1427 }
1428
1429 if (safe)
1430 {
1431 save_funccalp = save_funccal();
1432 ++sandbox;
1433 }
1434
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001435 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1436 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001438 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 if (safe)
1440 {
1441 --sandbox;
1442 restore_funccal(save_funccalp);
1443 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001444 vim_free(argvars);
1445
1446 if (ret == FAIL)
1447 clear_tv(rettv);
1448
1449 return ret;
1450}
1451
1452/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001453 * Call vimL function "func" and return the result as a string.
1454 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001455 * Uses argv[argc] for the function arguments.
1456 */
1457 void *
1458call_func_retstr(func, argc, argv, safe)
1459 char_u *func;
1460 int argc;
1461 char_u **argv;
1462 int safe; /* use the sandbox */
1463{
1464 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001465 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001466
1467 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1468 return NULL;
1469
1470 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001471 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 return retval;
1473}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001474
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001475#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001476/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001477 * Call vimL function "func" and return the result as a number.
1478 * Returns -1 when calling the function fails.
1479 * Uses argv[argc] for the function arguments.
1480 */
1481 long
1482call_func_retnr(func, argc, argv, safe)
1483 char_u *func;
1484 int argc;
1485 char_u **argv;
1486 int safe; /* use the sandbox */
1487{
1488 typval_T rettv;
1489 long retval;
1490
1491 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1492 return -1;
1493
1494 retval = get_tv_number_chk(&rettv, NULL);
1495 clear_tv(&rettv);
1496 return retval;
1497}
1498#endif
1499
1500/*
1501 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001502 * Uses argv[argc] for the function arguments.
1503 */
1504 void *
1505call_func_retlist(func, argc, argv, safe)
1506 char_u *func;
1507 int argc;
1508 char_u **argv;
1509 int safe; /* use the sandbox */
1510{
1511 typval_T rettv;
1512
1513 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1514 return NULL;
1515
1516 if (rettv.v_type != VAR_LIST)
1517 {
1518 clear_tv(&rettv);
1519 return NULL;
1520 }
1521
1522 return rettv.vval.v_list;
1523}
1524
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525#endif
1526
1527/*
1528 * Save the current function call pointer, and set it to NULL.
1529 * Used when executing autocommands and for ":source".
1530 */
1531 void *
1532save_funccal()
1533{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001534 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 current_funccal = NULL;
1537 return (void *)fc;
1538}
1539
1540 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001541restore_funccal(vfc)
1542 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001544 funccall_T *fc = (funccall_T *)vfc;
1545
1546 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547}
1548
Bram Moolenaar05159a02005-02-26 23:04:13 +00001549#if defined(FEAT_PROFILE) || defined(PROTO)
1550/*
1551 * Prepare profiling for entering a child or something else that is not
1552 * counted for the script/function itself.
1553 * Should always be called in pair with prof_child_exit().
1554 */
1555 void
1556prof_child_enter(tm)
1557 proftime_T *tm; /* place to store waittime */
1558{
1559 funccall_T *fc = current_funccal;
1560
1561 if (fc != NULL && fc->func->uf_profiling)
1562 profile_start(&fc->prof_child);
1563 script_prof_save(tm);
1564}
1565
1566/*
1567 * Take care of time spent in a child.
1568 * Should always be called after prof_child_enter().
1569 */
1570 void
1571prof_child_exit(tm)
1572 proftime_T *tm; /* where waittime was stored */
1573{
1574 funccall_T *fc = current_funccal;
1575
1576 if (fc != NULL && fc->func->uf_profiling)
1577 {
1578 profile_end(&fc->prof_child);
1579 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1580 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1581 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1582 }
1583 script_prof_restore(tm);
1584}
1585#endif
1586
1587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588#ifdef FEAT_FOLDING
1589/*
1590 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1591 * it in "*cp". Doesn't give error messages.
1592 */
1593 int
1594eval_foldexpr(arg, cp)
1595 char_u *arg;
1596 int *cp;
1597{
Bram Moolenaar33570922005-01-25 22:26:29 +00001598 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 int retval;
1600 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001601 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1602 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
1604 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001605 if (use_sandbox)
1606 ++sandbox;
1607 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 retval = 0;
1611 else
1612 {
1613 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001614 if (tv.v_type == VAR_NUMBER)
1615 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001616 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 retval = 0;
1618 else
1619 {
1620 /* If the result is a string, check if there is a non-digit before
1621 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001622 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (!VIM_ISDIGIT(*s) && *s != '-')
1624 *cp = *s++;
1625 retval = atol((char *)s);
1626 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001627 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 }
1629 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001630 if (use_sandbox)
1631 --sandbox;
1632 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
1634 return retval;
1635}
1636#endif
1637
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639 * ":let" list all variable values
1640 * ":let var1 var2" list variable values
1641 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001642 * ":let var += expr" assignment command.
1643 * ":let var -= expr" assignment command.
1644 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001645 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 */
1647 void
1648ex_let(eap)
1649 exarg_T *eap;
1650{
1651 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001653 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655 int var_count = 0;
1656 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001657 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659 expr = skip_var_list(arg, &var_count, &semicolon);
1660 if (expr == NULL)
1661 return;
1662 expr = vim_strchr(expr, '=');
1663 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001665 /*
1666 * ":let" without "=": list variables
1667 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668 if (*arg == '[')
1669 EMSG(_(e_invarg));
1670 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671 /* ":let var1 var2" */
1672 arg = list_arg_vars(eap, arg);
1673 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001676 list_glob_vars();
1677 list_buf_vars();
1678 list_win_vars();
1679 list_vim_vars();
1680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 eap->nextcmd = check_nextcmd(arg);
1682 }
1683 else
1684 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001685 op[0] = '=';
1686 op[1] = NUL;
1687 if (expr > arg)
1688 {
1689 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1690 op[0] = expr[-1]; /* +=, -= or .= */
1691 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001693
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 if (eap->skip)
1695 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 if (eap->skip)
1698 {
1699 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001700 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 --emsg_skip;
1702 }
1703 else if (i != FAIL)
1704 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001706 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001707 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 }
1709 }
1710}
1711
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712/*
1713 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1714 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001715 * When "nextchars" is not NULL it points to a string with characters that
1716 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1717 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001718 * Returns OK or FAIL;
1719 */
1720 static int
1721ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1722 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001723 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 int copy; /* copy values from "tv", don't move */
1725 int semicolon; /* from skip_var_list() */
1726 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001727 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001728{
1729 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001730 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001731 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001732 listitem_T *item;
1733 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001734
1735 if (*arg != '[')
1736 {
1737 /*
1738 * ":let var = expr" or ":for var in list"
1739 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001740 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741 return FAIL;
1742 return OK;
1743 }
1744
1745 /*
1746 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1747 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001748 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749 {
1750 EMSG(_(e_listreq));
1751 return FAIL;
1752 }
1753
1754 i = list_len(l);
1755 if (semicolon == 0 && var_count < i)
1756 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001757 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001758 return FAIL;
1759 }
1760 if (var_count - semicolon > i)
1761 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001762 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001763 return FAIL;
1764 }
1765
1766 item = l->lv_first;
1767 while (*arg != ']')
1768 {
1769 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001770 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 item = item->li_next;
1772 if (arg == NULL)
1773 return FAIL;
1774
1775 arg = skipwhite(arg);
1776 if (*arg == ';')
1777 {
1778 /* Put the rest of the list (may be empty) in the var after ';'.
1779 * Create a new list for this. */
1780 l = list_alloc();
1781 if (l == NULL)
1782 return FAIL;
1783 while (item != NULL)
1784 {
1785 list_append_tv(l, &item->li_tv);
1786 item = item->li_next;
1787 }
1788
1789 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001790 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 ltv.vval.v_list = l;
1792 l->lv_refcount = 1;
1793
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001794 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1795 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001796 clear_tv(&ltv);
1797 if (arg == NULL)
1798 return FAIL;
1799 break;
1800 }
1801 else if (*arg != ',' && *arg != ']')
1802 {
1803 EMSG2(_(e_intern2), "ex_let_vars()");
1804 return FAIL;
1805 }
1806 }
1807
1808 return OK;
1809}
1810
1811/*
1812 * Skip over assignable variable "var" or list of variables "[var, var]".
1813 * Used for ":let varvar = expr" and ":for varvar in expr".
1814 * For "[var, var]" increment "*var_count" for each variable.
1815 * for "[var, var; var]" set "semicolon".
1816 * Return NULL for an error.
1817 */
1818 static char_u *
1819skip_var_list(arg, var_count, semicolon)
1820 char_u *arg;
1821 int *var_count;
1822 int *semicolon;
1823{
1824 char_u *p, *s;
1825
1826 if (*arg == '[')
1827 {
1828 /* "[var, var]": find the matching ']'. */
1829 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001830 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001831 {
1832 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1833 s = skip_var_one(p);
1834 if (s == p)
1835 {
1836 EMSG2(_(e_invarg2), p);
1837 return NULL;
1838 }
1839 ++*var_count;
1840
1841 p = skipwhite(s);
1842 if (*p == ']')
1843 break;
1844 else if (*p == ';')
1845 {
1846 if (*semicolon == 1)
1847 {
1848 EMSG(_("Double ; in list of variables"));
1849 return NULL;
1850 }
1851 *semicolon = 1;
1852 }
1853 else if (*p != ',')
1854 {
1855 EMSG2(_(e_invarg2), p);
1856 return NULL;
1857 }
1858 }
1859 return p + 1;
1860 }
1861 else
1862 return skip_var_one(arg);
1863}
1864
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001865/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001866 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1867 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001868 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869 static char_u *
1870skip_var_one(arg)
1871 char_u *arg;
1872{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001873 if (*arg == '@' && arg[1] != NUL)
1874 return arg + 2;
1875 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1876 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001877}
1878
Bram Moolenaara7043832005-01-21 11:56:39 +00001879/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001880 * List variables for hashtab "ht" with prefix "prefix".
1881 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001882 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001884list_hashtable_vars(ht, prefix, empty)
1885 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001886 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001887 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001888{
Bram Moolenaar33570922005-01-25 22:26:29 +00001889 hashitem_T *hi;
1890 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001891 int todo;
1892
1893 todo = ht->ht_used;
1894 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1895 {
1896 if (!HASHITEM_EMPTY(hi))
1897 {
1898 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001899 di = HI2DI(hi);
1900 if (empty || di->di_tv.v_type != VAR_STRING
1901 || di->di_tv.vval.v_string != NULL)
1902 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001903 }
1904 }
1905}
1906
1907/*
1908 * List global variables.
1909 */
1910 static void
1911list_glob_vars()
1912{
Bram Moolenaar33570922005-01-25 22:26:29 +00001913 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001914}
1915
1916/*
1917 * List buffer variables.
1918 */
1919 static void
1920list_buf_vars()
1921{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001922 char_u numbuf[NUMBUFLEN];
1923
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001925
1926 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1927 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001928}
1929
1930/*
1931 * List window variables.
1932 */
1933 static void
1934list_win_vars()
1935{
Bram Moolenaar33570922005-01-25 22:26:29 +00001936 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001937}
1938
1939/*
1940 * List Vim variables.
1941 */
1942 static void
1943list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001944{
Bram Moolenaar33570922005-01-25 22:26:29 +00001945 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946}
1947
1948/*
1949 * List variables in "arg".
1950 */
1951 static char_u *
1952list_arg_vars(eap, arg)
1953 exarg_T *eap;
1954 char_u *arg;
1955{
1956 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001957 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001959 char_u *name_start;
1960 char_u *arg_subsc;
1961 char_u *tofree;
1962 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963
1964 while (!ends_excmd(*arg) && !got_int)
1965 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001966 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001967 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001968 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001969 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1970 {
1971 emsg_severe = TRUE;
1972 EMSG(_(e_trailing));
1973 break;
1974 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001975 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001976 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001977 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001978 /* get_name_len() takes care of expanding curly braces */
1979 name_start = name = arg;
1980 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1981 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001982 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001983 /* This is mainly to keep test 49 working: when expanding
1984 * curly braces fails overrule the exception error message. */
1985 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001987 emsg_severe = TRUE;
1988 EMSG2(_(e_invarg2), arg);
1989 break;
1990 }
1991 error = TRUE;
1992 }
1993 else
1994 {
1995 if (tofree != NULL)
1996 name = tofree;
1997 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001999 else
2000 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002001 /* handle d.key, l[idx], f(expr) */
2002 arg_subsc = arg;
2003 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002004 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002005 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002006 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002007 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002008 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002009 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002010 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002011 case 'g': list_glob_vars(); break;
2012 case 'b': list_buf_vars(); break;
2013 case 'w': list_win_vars(); break;
2014 case 'v': list_vim_vars(); break;
2015 default:
2016 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002017 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002018 }
2019 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002020 {
2021 char_u numbuf[NUMBUFLEN];
2022 char_u *tf;
2023 int c;
2024 char_u *s;
2025
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002026 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002027 c = *arg;
2028 *arg = NUL;
2029 list_one_var_a((char_u *)"",
2030 arg == arg_subsc ? name : name_start,
2031 tv.v_type, s == NULL ? (char_u *)"" : s);
2032 *arg = c;
2033 vim_free(tf);
2034 }
2035 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002036 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002037 }
2038 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002039
2040 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002041 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002042
2043 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044 }
2045
2046 return arg;
2047}
2048
2049/*
2050 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2051 * Returns a pointer to the char just after the var name.
2052 * Returns NULL if there is an error.
2053 */
2054 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002055ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002057 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002058 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002060 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061{
2062 int c1;
2063 char_u *name;
2064 char_u *p;
2065 char_u *arg_end = NULL;
2066 int len;
2067 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002068 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002069
2070 /*
2071 * ":let $VAR = expr": Set environment variable.
2072 */
2073 if (*arg == '$')
2074 {
2075 /* Find the end of the name. */
2076 ++arg;
2077 name = arg;
2078 len = get_env_len(&arg);
2079 if (len == 0)
2080 EMSG2(_(e_invarg2), name - 1);
2081 else
2082 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002083 if (op != NULL && (*op == '+' || *op == '-'))
2084 EMSG2(_(e_letwrong), op);
2085 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002087 EMSG(_(e_letunexp));
2088 else
2089 {
2090 c1 = name[len];
2091 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002092 p = get_tv_string_chk(tv);
2093 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002094 {
2095 int mustfree = FALSE;
2096 char_u *s = vim_getenv(name, &mustfree);
2097
2098 if (s != NULL)
2099 {
2100 p = tofree = concat_str(s, p);
2101 if (mustfree)
2102 vim_free(s);
2103 }
2104 }
2105 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002106 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002107 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002108 if (STRICMP(name, "HOME") == 0)
2109 init_homedir();
2110 else if (didset_vim && STRICMP(name, "VIM") == 0)
2111 didset_vim = FALSE;
2112 else if (didset_vimruntime
2113 && STRICMP(name, "VIMRUNTIME") == 0)
2114 didset_vimruntime = FALSE;
2115 arg_end = arg;
2116 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002117 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002118 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002119 }
2120 }
2121 }
2122
2123 /*
2124 * ":let &option = expr": Set option value.
2125 * ":let &l:option = expr": Set local option value.
2126 * ":let &g:option = expr": Set global option value.
2127 */
2128 else if (*arg == '&')
2129 {
2130 /* Find the end of the name. */
2131 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002132 if (p == NULL || (endchars != NULL
2133 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002134 EMSG(_(e_letunexp));
2135 else
2136 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002137 long n;
2138 int opt_type;
2139 long numval;
2140 char_u *stringval = NULL;
2141 char_u *s;
2142
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002143 c1 = *p;
2144 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002145
2146 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002147 s = get_tv_string_chk(tv); /* != NULL if number or string */
2148 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002149 {
2150 opt_type = get_option_value(arg, &numval,
2151 &stringval, opt_flags);
2152 if ((opt_type == 1 && *op == '.')
2153 || (opt_type == 0 && *op != '.'))
2154 EMSG2(_(e_letwrong), op);
2155 else
2156 {
2157 if (opt_type == 1) /* number */
2158 {
2159 if (*op == '+')
2160 n = numval + n;
2161 else
2162 n = numval - n;
2163 }
2164 else if (opt_type == 0 && stringval != NULL) /* string */
2165 {
2166 s = concat_str(stringval, s);
2167 vim_free(stringval);
2168 stringval = s;
2169 }
2170 }
2171 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002172 if (s != NULL)
2173 {
2174 set_option_value(arg, n, s, opt_flags);
2175 arg_end = p;
2176 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002178 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179 }
2180 }
2181
2182 /*
2183 * ":let @r = expr": Set register contents.
2184 */
2185 else if (*arg == '@')
2186 {
2187 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002188 if (op != NULL && (*op == '+' || *op == '-'))
2189 EMSG2(_(e_letwrong), op);
2190 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002191 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 EMSG(_(e_letunexp));
2193 else
2194 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002195 char_u *tofree = NULL;
2196 char_u *s;
2197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002198 p = get_tv_string_chk(tv);
2199 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002200 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002201 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002202 if (s != NULL)
2203 {
2204 p = tofree = concat_str(s, p);
2205 vim_free(s);
2206 }
2207 }
2208 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002209 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002210 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002211 arg_end = arg + 1;
2212 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002213 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 }
2215 }
2216
2217 /*
2218 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002221 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002223 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002225 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002226 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002228 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2229 EMSG(_(e_letunexp));
2230 else
2231 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002232 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002233 arg_end = p;
2234 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 }
2238
2239 else
2240 EMSG2(_(e_invarg2), arg);
2241
2242 return arg_end;
2243}
2244
2245/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002246 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2247 */
2248 static int
2249check_changedtick(arg)
2250 char_u *arg;
2251{
2252 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2253 {
2254 EMSG2(_(e_readonlyvar), arg);
2255 return TRUE;
2256 }
2257 return FALSE;
2258}
2259
2260/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 * Get an lval: variable, Dict item or List item that can be assigned a value
2262 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2263 * "name.key", "name.key[expr]" etc.
2264 * Indexing only works if "name" is an existing List or Dictionary.
2265 * "name" points to the start of the name.
2266 * If "rettv" is not NULL it points to the value to be assigned.
2267 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2268 * wrong; must end in space or cmd separator.
2269 *
2270 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002272 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 */
2274 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002275get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002277 typval_T *rettv;
2278 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 int unlet;
2280 int skip;
2281 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002282 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 char_u *expr_start, *expr_end;
2286 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002287 dictitem_T *v;
2288 typval_T var1;
2289 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002291 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002292 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002293 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002294 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002297 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002298
2299 if (skip)
2300 {
2301 /* When skipping just find the end of the name. */
2302 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002303 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 }
2305
2306 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002307 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 if (expr_start != NULL)
2309 {
2310 /* Don't expand the name when we already know there is an error. */
2311 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2312 && *p != '[' && *p != '.')
2313 {
2314 EMSG(_(e_trailing));
2315 return NULL;
2316 }
2317
2318 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2319 if (lp->ll_exp_name == NULL)
2320 {
2321 /* Report an invalid expression in braces, unless the
2322 * expression evaluation has been cancelled due to an
2323 * aborting error, an interrupt, or an exception. */
2324 if (!aborting() && !quiet)
2325 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002326 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002327 EMSG2(_(e_invarg2), name);
2328 return NULL;
2329 }
2330 }
2331 lp->ll_name = lp->ll_exp_name;
2332 }
2333 else
2334 lp->ll_name = name;
2335
2336 /* Without [idx] or .key we are done. */
2337 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2338 return p;
2339
2340 cc = *p;
2341 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002342 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002343 if (v == NULL && !quiet)
2344 EMSG2(_(e_undefvar), lp->ll_name);
2345 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 if (v == NULL)
2347 return NULL;
2348
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 /*
2350 * Loop until no more [idx] or .key is following.
2351 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002352 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002354 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2356 && !(lp->ll_tv->v_type == VAR_DICT
2357 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 if (!quiet)
2360 EMSG(_("E689: Can only index a List or Dictionary"));
2361 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002363 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 if (!quiet)
2366 EMSG(_("E708: [:] must come last"));
2367 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002368 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002369
Bram Moolenaar8c711452005-01-14 21:53:12 +00002370 len = -1;
2371 if (*p == '.')
2372 {
2373 key = p + 1;
2374 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2375 ;
2376 if (len == 0)
2377 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 if (!quiet)
2379 EMSG(_(e_emptykey));
2380 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002381 }
2382 p = key + len;
2383 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002384 else
2385 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002386 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002387 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002388 if (*p == ':')
2389 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002390 else
2391 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002392 empty1 = FALSE;
2393 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 if (get_tv_string_chk(&var1) == NULL)
2396 {
2397 /* not a number or string */
2398 clear_tv(&var1);
2399 return NULL;
2400 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002401 }
2402
2403 /* Optionally get the second index [ :expr]. */
2404 if (*p == ':')
2405 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002409 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002410 if (!empty1)
2411 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002413 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002414 if (rettv != NULL && (rettv->v_type != VAR_LIST
2415 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 if (!quiet)
2418 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 if (!empty1)
2420 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002422 }
2423 p = skipwhite(p + 1);
2424 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002426 else
2427 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002429 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2430 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 if (!empty1)
2432 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002435 if (get_tv_string_chk(&var2) == NULL)
2436 {
2437 /* not a number or string */
2438 if (!empty1)
2439 clear_tv(&var1);
2440 clear_tv(&var2);
2441 return NULL;
2442 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002445 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002446 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002448
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002450 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 if (!quiet)
2452 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 if (!empty1)
2454 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 }
2459
2460 /* Skip to past ']'. */
2461 ++p;
2462 }
2463
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002465 {
2466 if (len == -1)
2467 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002470 if (*key == NUL)
2471 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 if (!quiet)
2473 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002476 }
2477 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 lp->ll_list = NULL;
2479 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002480 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002482 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002483 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002485 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002487 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 if (len == -1)
2489 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 }
2492 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 if (len == -1)
2497 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 p = NULL;
2500 break;
2501 }
2502 if (len == -1)
2503 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 }
2506 else
2507 {
2508 /*
2509 * Get the number and item for the only or first index of the List.
2510 */
2511 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513 else
2514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002515 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 clear_tv(&var1);
2517 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002518 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 lp->ll_list = lp->ll_tv->vval.v_list;
2520 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2521 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (!quiet)
2524 EMSGN(_(e_listidx), lp->ll_n1);
2525 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 }
2529
2530 /*
2531 * May need to find the item or absolute index for the second
2532 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 * When no index given: "lp->ll_empty2" is TRUE.
2534 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002538 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002541 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543 if (ni == NULL)
2544 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 if (!quiet)
2546 EMSGN(_(e_listidx), lp->ll_n2);
2547 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002548 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 }
2551
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2553 if (lp->ll_n1 < 0)
2554 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2555 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002556 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 if (!quiet)
2558 EMSGN(_(e_listidx), lp->ll_n2);
2559 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002560 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002561 }
2562
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002565 }
2566
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 return p;
2568}
2569
2570/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 */
2573 static void
2574clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002575 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576{
2577 vim_free(lp->ll_exp_name);
2578 vim_free(lp->ll_newkey);
2579}
2580
2581/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002582 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002584 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 */
2586 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002587set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002590 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002592 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593{
2594 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002595 listitem_T *ri;
2596 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597
2598 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002601 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 cc = *endp;
2603 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002604 if (op != NULL && *op != '=')
2605 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002606 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002607
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002608 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002609 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2610 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002611 {
2612 if (tv_op(&tv, rettv, op) == OK)
2613 set_var(lp->ll_name, &tv, FALSE);
2614 clear_tv(&tv);
2615 }
2616 }
2617 else
2618 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002622 else if (tv_check_lock(lp->ll_newkey == NULL
2623 ? lp->ll_tv->v_lock
2624 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2625 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 else if (lp->ll_range)
2627 {
2628 /*
2629 * Assign the List values to the list items.
2630 */
2631 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002633 if (op != NULL && *op != '=')
2634 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2635 else
2636 {
2637 clear_tv(&lp->ll_li->li_tv);
2638 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2639 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 ri = ri->li_next;
2641 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2642 break;
2643 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002646 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 ri = NULL;
2649 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002651 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 lp->ll_li = lp->ll_li->li_next;
2653 ++lp->ll_n1;
2654 }
2655 if (ri != NULL)
2656 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002657 else if (lp->ll_empty2
2658 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 : lp->ll_n1 != lp->ll_n2)
2660 EMSG(_("E711: List value has not enough items"));
2661 }
2662 else
2663 {
2664 /*
2665 * Assign to a List or Dictionary item.
2666 */
2667 if (lp->ll_newkey != NULL)
2668 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002669 if (op != NULL && *op != '=')
2670 {
2671 EMSG2(_(e_letwrong), op);
2672 return;
2673 }
2674
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002676 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (di == NULL)
2678 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002679 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2680 {
2681 vim_free(di);
2682 return;
2683 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002686 else if (op != NULL && *op != '=')
2687 {
2688 tv_op(lp->ll_tv, rettv, op);
2689 return;
2690 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002691 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 /*
2695 * Assign the value to the variable or list item.
2696 */
2697 if (copy)
2698 copy_tv(rettv, lp->ll_tv);
2699 else
2700 {
2701 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002702 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002704 }
2705 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002706}
2707
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002708/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002709 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2710 * Returns OK or FAIL.
2711 */
2712 static int
2713tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002714 typval_T *tv1;
2715 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002716 char_u *op;
2717{
2718 long n;
2719 char_u numbuf[NUMBUFLEN];
2720 char_u *s;
2721
2722 /* Can't do anything with a Funcref or a Dict on the right. */
2723 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2724 {
2725 switch (tv1->v_type)
2726 {
2727 case VAR_DICT:
2728 case VAR_FUNC:
2729 break;
2730
2731 case VAR_LIST:
2732 if (*op != '+' || tv2->v_type != VAR_LIST)
2733 break;
2734 /* List += List */
2735 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2736 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2737 return OK;
2738
2739 case VAR_NUMBER:
2740 case VAR_STRING:
2741 if (tv2->v_type == VAR_LIST)
2742 break;
2743 if (*op == '+' || *op == '-')
2744 {
2745 /* nr += nr or nr -= nr*/
2746 n = get_tv_number(tv1);
2747 if (*op == '+')
2748 n += get_tv_number(tv2);
2749 else
2750 n -= get_tv_number(tv2);
2751 clear_tv(tv1);
2752 tv1->v_type = VAR_NUMBER;
2753 tv1->vval.v_number = n;
2754 }
2755 else
2756 {
2757 /* str .= str */
2758 s = get_tv_string(tv1);
2759 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2760 clear_tv(tv1);
2761 tv1->v_type = VAR_STRING;
2762 tv1->vval.v_string = s;
2763 }
2764 return OK;
2765 }
2766 }
2767
2768 EMSG2(_(e_letwrong), op);
2769 return FAIL;
2770}
2771
2772/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002773 * Add a watcher to a list.
2774 */
2775 static void
2776list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002777 list_T *l;
2778 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002779{
2780 lw->lw_next = l->lv_watch;
2781 l->lv_watch = lw;
2782}
2783
2784/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002785 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002786 * No warning when it isn't found...
2787 */
2788 static void
2789list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002790 list_T *l;
2791 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002792{
Bram Moolenaar33570922005-01-25 22:26:29 +00002793 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002794
2795 lwp = &l->lv_watch;
2796 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2797 {
2798 if (lw == lwrem)
2799 {
2800 *lwp = lw->lw_next;
2801 break;
2802 }
2803 lwp = &lw->lw_next;
2804 }
2805}
2806
2807/*
2808 * Just before removing an item from a list: advance watchers to the next
2809 * item.
2810 */
2811 static void
2812list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002813 list_T *l;
2814 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002815{
Bram Moolenaar33570922005-01-25 22:26:29 +00002816 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002817
2818 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2819 if (lw->lw_item == item)
2820 lw->lw_item = item->li_next;
2821}
2822
2823/*
2824 * Evaluate the expression used in a ":for var in expr" command.
2825 * "arg" points to "var".
2826 * Set "*errp" to TRUE for an error, FALSE otherwise;
2827 * Return a pointer that holds the info. Null when there is an error.
2828 */
2829 void *
2830eval_for_line(arg, errp, nextcmdp, skip)
2831 char_u *arg;
2832 int *errp;
2833 char_u **nextcmdp;
2834 int skip;
2835{
Bram Moolenaar33570922005-01-25 22:26:29 +00002836 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002837 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002838 typval_T tv;
2839 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002840
2841 *errp = TRUE; /* default: there is an error */
2842
Bram Moolenaar33570922005-01-25 22:26:29 +00002843 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002844 if (fi == NULL)
2845 return NULL;
2846
2847 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2848 if (expr == NULL)
2849 return fi;
2850
2851 expr = skipwhite(expr);
2852 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2853 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002854 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002855 return fi;
2856 }
2857
2858 if (skip)
2859 ++emsg_skip;
2860 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2861 {
2862 *errp = FALSE;
2863 if (!skip)
2864 {
2865 l = tv.vval.v_list;
2866 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002867 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002868 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002869 clear_tv(&tv);
2870 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002871 else
2872 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002873 /* No need to increment the refcount, it's already set for the
2874 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002875 fi->fi_list = l;
2876 list_add_watch(l, &fi->fi_lw);
2877 fi->fi_lw.lw_item = l->lv_first;
2878 }
2879 }
2880 }
2881 if (skip)
2882 --emsg_skip;
2883
2884 return fi;
2885}
2886
2887/*
2888 * Use the first item in a ":for" list. Advance to the next.
2889 * Assign the values to the variable (list). "arg" points to the first one.
2890 * Return TRUE when a valid item was found, FALSE when at end of list or
2891 * something wrong.
2892 */
2893 int
2894next_for_item(fi_void, arg)
2895 void *fi_void;
2896 char_u *arg;
2897{
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002899 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002900 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002901
2902 item = fi->fi_lw.lw_item;
2903 if (item == NULL)
2904 result = FALSE;
2905 else
2906 {
2907 fi->fi_lw.lw_item = item->li_next;
2908 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2909 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2910 }
2911 return result;
2912}
2913
2914/*
2915 * Free the structure used to store info used by ":for".
2916 */
2917 void
2918free_for_info(fi_void)
2919 void *fi_void;
2920{
Bram Moolenaar33570922005-01-25 22:26:29 +00002921 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002922
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002923 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002924 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002925 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002926 list_unref(fi->fi_list);
2927 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002928 vim_free(fi);
2929}
2930
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2932
2933 void
2934set_context_for_expression(xp, arg, cmdidx)
2935 expand_T *xp;
2936 char_u *arg;
2937 cmdidx_T cmdidx;
2938{
2939 int got_eq = FALSE;
2940 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002941 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002943 if (cmdidx == CMD_let)
2944 {
2945 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002946 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002947 {
2948 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002949 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002950 {
2951 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002952 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002953 if (vim_iswhite(*p))
2954 break;
2955 }
2956 return;
2957 }
2958 }
2959 else
2960 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2961 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 while ((xp->xp_pattern = vim_strpbrk(arg,
2963 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2964 {
2965 c = *xp->xp_pattern;
2966 if (c == '&')
2967 {
2968 c = xp->xp_pattern[1];
2969 if (c == '&')
2970 {
2971 ++xp->xp_pattern;
2972 xp->xp_context = cmdidx != CMD_let || got_eq
2973 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2974 }
2975 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002976 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002978 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2979 xp->xp_pattern += 2;
2980
2981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
2983 else if (c == '$')
2984 {
2985 /* environment variable */
2986 xp->xp_context = EXPAND_ENV_VARS;
2987 }
2988 else if (c == '=')
2989 {
2990 got_eq = TRUE;
2991 xp->xp_context = EXPAND_EXPRESSION;
2992 }
2993 else if (c == '<'
2994 && xp->xp_context == EXPAND_FUNCTIONS
2995 && vim_strchr(xp->xp_pattern, '(') == NULL)
2996 {
2997 /* Function name can start with "<SNR>" */
2998 break;
2999 }
3000 else if (cmdidx != CMD_let || got_eq)
3001 {
3002 if (c == '"') /* string */
3003 {
3004 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3005 if (c == '\\' && xp->xp_pattern[1] != NUL)
3006 ++xp->xp_pattern;
3007 xp->xp_context = EXPAND_NOTHING;
3008 }
3009 else if (c == '\'') /* literal string */
3010 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003011 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3013 /* skip */ ;
3014 xp->xp_context = EXPAND_NOTHING;
3015 }
3016 else if (c == '|')
3017 {
3018 if (xp->xp_pattern[1] == '|')
3019 {
3020 ++xp->xp_pattern;
3021 xp->xp_context = EXPAND_EXPRESSION;
3022 }
3023 else
3024 xp->xp_context = EXPAND_COMMANDS;
3025 }
3026 else
3027 xp->xp_context = EXPAND_EXPRESSION;
3028 }
3029 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003030 /* Doesn't look like something valid, expand as an expression
3031 * anyway. */
3032 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 arg = xp->xp_pattern;
3034 if (*arg != NUL)
3035 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3036 /* skip */ ;
3037 }
3038 xp->xp_pattern = arg;
3039}
3040
3041#endif /* FEAT_CMDL_COMPL */
3042
3043/*
3044 * ":1,25call func(arg1, arg2)" function call.
3045 */
3046 void
3047ex_call(eap)
3048 exarg_T *eap;
3049{
3050 char_u *arg = eap->arg;
3051 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003055 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 linenr_T lnum;
3057 int doesrange;
3058 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003059 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003061 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3062 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003063 if (tofree == NULL)
3064 return;
3065
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003066 /* Increase refcount on dictionary, it could get deleted when evaluating
3067 * the arguments. */
3068 if (fudi.fd_dict != NULL)
3069 ++fudi.fd_dict->dv_refcount;
3070
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3072 len = STRLEN(tofree);
3073 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074
Bram Moolenaar532c7802005-01-27 14:44:31 +00003075 /* Skip white space to allow ":call func ()". Not good, but required for
3076 * backward compatibility. */
3077 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003078 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
3080 if (*startarg != '(')
3081 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003082 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 goto end;
3084 }
3085
3086 /*
3087 * When skipping, evaluate the function once, to find the end of the
3088 * arguments.
3089 * When the function takes a range, this is discovered after the first
3090 * call, and the loop is broken.
3091 */
3092 if (eap->skip)
3093 {
3094 ++emsg_skip;
3095 lnum = eap->line2; /* do it once, also with an invalid range */
3096 }
3097 else
3098 lnum = eap->line1;
3099 for ( ; lnum <= eap->line2; ++lnum)
3100 {
3101 if (!eap->skip && eap->addr_count > 0)
3102 {
3103 curwin->w_cursor.lnum = lnum;
3104 curwin->w_cursor.col = 0;
3105 }
3106 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003107 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003108 eap->line1, eap->line2, &doesrange,
3109 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 {
3111 failed = TRUE;
3112 break;
3113 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003114 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 if (doesrange || eap->skip)
3116 break;
3117 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003118 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003119 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003120 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 if (aborting())
3122 break;
3123 }
3124 if (eap->skip)
3125 --emsg_skip;
3126
3127 if (!failed)
3128 {
3129 /* Check for trailing illegal characters and a following command. */
3130 if (!ends_excmd(*arg))
3131 {
3132 emsg_severe = TRUE;
3133 EMSG(_(e_trailing));
3134 }
3135 else
3136 eap->nextcmd = check_nextcmd(arg);
3137 }
3138
3139end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003140 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142}
3143
3144/*
3145 * ":unlet[!] var1 ... " command.
3146 */
3147 void
3148ex_unlet(eap)
3149 exarg_T *eap;
3150{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003151 ex_unletlock(eap, eap->arg, 0);
3152}
3153
3154/*
3155 * ":lockvar" and ":unlockvar" commands
3156 */
3157 void
3158ex_lockvar(eap)
3159 exarg_T *eap;
3160{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003162 int deep = 2;
3163
3164 if (eap->forceit)
3165 deep = -1;
3166 else if (vim_isdigit(*arg))
3167 {
3168 deep = getdigits(&arg);
3169 arg = skipwhite(arg);
3170 }
3171
3172 ex_unletlock(eap, arg, deep);
3173}
3174
3175/*
3176 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3177 */
3178 static void
3179ex_unletlock(eap, argstart, deep)
3180 exarg_T *eap;
3181 char_u *argstart;
3182 int deep;
3183{
3184 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003187 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 do
3190 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003191 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003192 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3193 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003194 if (lv.ll_name == NULL)
3195 error = TRUE; /* error but continue parsing */
3196 if (name_end == NULL || (!vim_iswhite(*name_end)
3197 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003199 if (name_end != NULL)
3200 {
3201 emsg_severe = TRUE;
3202 EMSG(_(e_trailing));
3203 }
3204 if (!(eap->skip || error))
3205 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 break;
3207 }
3208
3209 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003210 {
3211 if (eap->cmdidx == CMD_unlet)
3212 {
3213 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3214 error = TRUE;
3215 }
3216 else
3217 {
3218 if (do_lock_var(&lv, name_end, deep,
3219 eap->cmdidx == CMD_lockvar) == FAIL)
3220 error = TRUE;
3221 }
3222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003224 if (!eap->skip)
3225 clear_lval(&lv);
3226
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 arg = skipwhite(name_end);
3228 } while (!ends_excmd(*arg));
3229
3230 eap->nextcmd = check_nextcmd(arg);
3231}
3232
Bram Moolenaar8c711452005-01-14 21:53:12 +00003233 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003234do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003236 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003237 int forceit;
3238{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003239 int ret = OK;
3240 int cc;
3241
3242 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003243 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003244 cc = *name_end;
3245 *name_end = NUL;
3246
3247 /* Normal name or expanded name. */
3248 if (check_changedtick(lp->ll_name))
3249 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003250 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003251 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003252 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003253 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003254 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3255 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003256 else if (lp->ll_range)
3257 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003258 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003259
3260 /* Delete a range of List items. */
3261 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3262 {
3263 li = lp->ll_li->li_next;
3264 listitem_remove(lp->ll_list, lp->ll_li);
3265 lp->ll_li = li;
3266 ++lp->ll_n1;
3267 }
3268 }
3269 else
3270 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003271 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003272 /* unlet a List item. */
3273 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003274 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003275 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003276 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003277 }
3278
3279 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003280}
3281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282/*
3283 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003284 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 */
3286 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003287do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003289 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
Bram Moolenaar33570922005-01-25 22:26:29 +00003291 hashtab_T *ht;
3292 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003293 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 ht = find_var_ht(name, &varname);
3296 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003298 hi = hash_find(ht, varname);
3299 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003300 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003301 if (var_check_ro(HI2DI(hi)->di_flags, name))
3302 return FAIL;
3303 delete_var(ht, hi);
3304 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003307 if (forceit)
3308 return OK;
3309 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 return FAIL;
3311}
3312
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003313/*
3314 * Lock or unlock variable indicated by "lp".
3315 * "deep" is the levels to go (-1 for unlimited);
3316 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3317 */
3318 static int
3319do_lock_var(lp, name_end, deep, lock)
3320 lval_T *lp;
3321 char_u *name_end;
3322 int deep;
3323 int lock;
3324{
3325 int ret = OK;
3326 int cc;
3327 dictitem_T *di;
3328
3329 if (deep == 0) /* nothing to do */
3330 return OK;
3331
3332 if (lp->ll_tv == NULL)
3333 {
3334 cc = *name_end;
3335 *name_end = NUL;
3336
3337 /* Normal name or expanded name. */
3338 if (check_changedtick(lp->ll_name))
3339 ret = FAIL;
3340 else
3341 {
3342 di = find_var(lp->ll_name, NULL);
3343 if (di == NULL)
3344 ret = FAIL;
3345 else
3346 {
3347 if (lock)
3348 di->di_flags |= DI_FLAGS_LOCK;
3349 else
3350 di->di_flags &= ~DI_FLAGS_LOCK;
3351 item_lock(&di->di_tv, deep, lock);
3352 }
3353 }
3354 *name_end = cc;
3355 }
3356 else if (lp->ll_range)
3357 {
3358 listitem_T *li = lp->ll_li;
3359
3360 /* (un)lock a range of List items. */
3361 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3362 {
3363 item_lock(&li->li_tv, deep, lock);
3364 li = li->li_next;
3365 ++lp->ll_n1;
3366 }
3367 }
3368 else if (lp->ll_list != NULL)
3369 /* (un)lock a List item. */
3370 item_lock(&lp->ll_li->li_tv, deep, lock);
3371 else
3372 /* un(lock) a Dictionary item. */
3373 item_lock(&lp->ll_di->di_tv, deep, lock);
3374
3375 return ret;
3376}
3377
3378/*
3379 * Lock or unlock an item. "deep" is nr of levels to go.
3380 */
3381 static void
3382item_lock(tv, deep, lock)
3383 typval_T *tv;
3384 int deep;
3385 int lock;
3386{
3387 static int recurse = 0;
3388 list_T *l;
3389 listitem_T *li;
3390 dict_T *d;
3391 hashitem_T *hi;
3392 int todo;
3393
3394 if (recurse >= DICT_MAXNEST)
3395 {
3396 EMSG(_("E743: variable nested too deep for (un)lock"));
3397 return;
3398 }
3399 if (deep == 0)
3400 return;
3401 ++recurse;
3402
3403 /* lock/unlock the item itself */
3404 if (lock)
3405 tv->v_lock |= VAR_LOCKED;
3406 else
3407 tv->v_lock &= ~VAR_LOCKED;
3408
3409 switch (tv->v_type)
3410 {
3411 case VAR_LIST:
3412 if ((l = tv->vval.v_list) != NULL)
3413 {
3414 if (lock)
3415 l->lv_lock |= VAR_LOCKED;
3416 else
3417 l->lv_lock &= ~VAR_LOCKED;
3418 if (deep < 0 || deep > 1)
3419 /* recursive: lock/unlock the items the List contains */
3420 for (li = l->lv_first; li != NULL; li = li->li_next)
3421 item_lock(&li->li_tv, deep - 1, lock);
3422 }
3423 break;
3424 case VAR_DICT:
3425 if ((d = tv->vval.v_dict) != NULL)
3426 {
3427 if (lock)
3428 d->dv_lock |= VAR_LOCKED;
3429 else
3430 d->dv_lock &= ~VAR_LOCKED;
3431 if (deep < 0 || deep > 1)
3432 {
3433 /* recursive: lock/unlock the items the List contains */
3434 todo = d->dv_hashtab.ht_used;
3435 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3436 {
3437 if (!HASHITEM_EMPTY(hi))
3438 {
3439 --todo;
3440 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3441 }
3442 }
3443 }
3444 }
3445 }
3446 --recurse;
3447}
3448
Bram Moolenaara40058a2005-07-11 22:42:07 +00003449/*
3450 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3451 * it refers to a List or Dictionary that is locked.
3452 */
3453 static int
3454tv_islocked(tv)
3455 typval_T *tv;
3456{
3457 return (tv->v_lock & VAR_LOCKED)
3458 || (tv->v_type == VAR_LIST
3459 && tv->vval.v_list != NULL
3460 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3461 || (tv->v_type == VAR_DICT
3462 && tv->vval.v_dict != NULL
3463 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3464}
3465
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3467/*
3468 * Delete all "menutrans_" variables.
3469 */
3470 void
3471del_menutrans_vars()
3472{
Bram Moolenaar33570922005-01-25 22:26:29 +00003473 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003474 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
Bram Moolenaar33570922005-01-25 22:26:29 +00003476 hash_lock(&globvarht);
3477 todo = globvarht.ht_used;
3478 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003479 {
3480 if (!HASHITEM_EMPTY(hi))
3481 {
3482 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003483 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3484 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003485 }
3486 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003487 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488}
3489#endif
3490
3491#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3492
3493/*
3494 * Local string buffer for the next two functions to store a variable name
3495 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3496 * get_user_var_name().
3497 */
3498
3499static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3500
3501static char_u *varnamebuf = NULL;
3502static int varnamebuflen = 0;
3503
3504/*
3505 * Function to concatenate a prefix and a variable name.
3506 */
3507 static char_u *
3508cat_prefix_varname(prefix, name)
3509 int prefix;
3510 char_u *name;
3511{
3512 int len;
3513
3514 len = (int)STRLEN(name) + 3;
3515 if (len > varnamebuflen)
3516 {
3517 vim_free(varnamebuf);
3518 len += 10; /* some additional space */
3519 varnamebuf = alloc(len);
3520 if (varnamebuf == NULL)
3521 {
3522 varnamebuflen = 0;
3523 return NULL;
3524 }
3525 varnamebuflen = len;
3526 }
3527 *varnamebuf = prefix;
3528 varnamebuf[1] = ':';
3529 STRCPY(varnamebuf + 2, name);
3530 return varnamebuf;
3531}
3532
3533/*
3534 * Function given to ExpandGeneric() to obtain the list of user defined
3535 * (global/buffer/window/built-in) variable names.
3536 */
3537/*ARGSUSED*/
3538 char_u *
3539get_user_var_name(xp, idx)
3540 expand_T *xp;
3541 int idx;
3542{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003543 static long_u gdone;
3544 static long_u bdone;
3545 static long_u wdone;
3546 static int vidx;
3547 static hashitem_T *hi;
3548 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
3550 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003551 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003552
3553 /* Global variables */
3554 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003556 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003557 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003558 else
3559 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003560 while (HASHITEM_EMPTY(hi))
3561 ++hi;
3562 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3563 return cat_prefix_varname('g', hi->hi_key);
3564 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003566
3567 /* b: variables */
3568 ht = &curbuf->b_vars.dv_hashtab;
3569 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003571 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003572 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003573 else
3574 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003575 while (HASHITEM_EMPTY(hi))
3576 ++hi;
3577 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003579 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003581 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 return (char_u *)"b:changedtick";
3583 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003584
3585 /* w: variables */
3586 ht = &curwin->w_vars.dv_hashtab;
3587 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003589 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003590 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003591 else
3592 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003593 while (HASHITEM_EMPTY(hi))
3594 ++hi;
3595 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003597
3598 /* v: variables */
3599 if (vidx < VV_LEN)
3600 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601
3602 vim_free(varnamebuf);
3603 varnamebuf = NULL;
3604 varnamebuflen = 0;
3605 return NULL;
3606}
3607
3608#endif /* FEAT_CMDL_COMPL */
3609
3610/*
3611 * types for expressions.
3612 */
3613typedef enum
3614{
3615 TYPE_UNKNOWN = 0
3616 , TYPE_EQUAL /* == */
3617 , TYPE_NEQUAL /* != */
3618 , TYPE_GREATER /* > */
3619 , TYPE_GEQUAL /* >= */
3620 , TYPE_SMALLER /* < */
3621 , TYPE_SEQUAL /* <= */
3622 , TYPE_MATCH /* =~ */
3623 , TYPE_NOMATCH /* !~ */
3624} exptype_T;
3625
3626/*
3627 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003628 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3630 */
3631
3632/*
3633 * Handle zero level expression.
3634 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003635 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003636 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 * Return OK or FAIL.
3638 */
3639 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003640eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 char_u **nextcmd;
3644 int evaluate;
3645{
3646 int ret;
3647 char_u *p;
3648
3649 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003650 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 if (ret == FAIL || !ends_excmd(*p))
3652 {
3653 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003654 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 /*
3656 * Report the invalid expression unless the expression evaluation has
3657 * been cancelled due to an aborting error, an interrupt, or an
3658 * exception.
3659 */
3660 if (!aborting())
3661 EMSG2(_(e_invexpr2), arg);
3662 ret = FAIL;
3663 }
3664 if (nextcmd != NULL)
3665 *nextcmd = check_nextcmd(p);
3666
3667 return ret;
3668}
3669
3670/*
3671 * Handle top level expression:
3672 * expr1 ? expr0 : expr0
3673 *
3674 * "arg" must point to the first non-white of the expression.
3675 * "arg" is advanced to the next non-white after the recognized expression.
3676 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003677 * Note: "rettv.v_lock" is not set.
3678 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 * Return OK or FAIL.
3680 */
3681 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003682eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 int evaluate;
3686{
3687 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003688 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689
3690 /*
3691 * Get the first variable.
3692 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003693 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 return FAIL;
3695
3696 if ((*arg)[0] == '?')
3697 {
3698 result = FALSE;
3699 if (evaluate)
3700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003701 int error = FALSE;
3702
3703 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003705 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003706 if (error)
3707 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709
3710 /*
3711 * Get the second variable.
3712 */
3713 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003714 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 return FAIL;
3716
3717 /*
3718 * Check for the ":".
3719 */
3720 if ((*arg)[0] != ':')
3721 {
3722 EMSG(_("E109: Missing ':' after '?'"));
3723 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003724 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 return FAIL;
3726 }
3727
3728 /*
3729 * Get the third variable.
3730 */
3731 *arg = skipwhite(*arg + 1);
3732 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3733 {
3734 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003735 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 return FAIL;
3737 }
3738 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003739 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
3741
3742 return OK;
3743}
3744
3745/*
3746 * Handle first level expression:
3747 * expr2 || expr2 || expr2 logical OR
3748 *
3749 * "arg" must point to the first non-white of the expression.
3750 * "arg" is advanced to the next non-white after the recognized expression.
3751 *
3752 * Return OK or FAIL.
3753 */
3754 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003755eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 int evaluate;
3759{
Bram Moolenaar33570922005-01-25 22:26:29 +00003760 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 long result;
3762 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003763 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
3765 /*
3766 * Get the first variable.
3767 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003768 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 return FAIL;
3770
3771 /*
3772 * Repeat until there is no following "||".
3773 */
3774 first = TRUE;
3775 result = FALSE;
3776 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3777 {
3778 if (evaluate && first)
3779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003780 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003782 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003783 if (error)
3784 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 first = FALSE;
3786 }
3787
3788 /*
3789 * Get the second variable.
3790 */
3791 *arg = skipwhite(*arg + 2);
3792 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3793 return FAIL;
3794
3795 /*
3796 * Compute the result.
3797 */
3798 if (evaluate && !result)
3799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003800 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003802 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003803 if (error)
3804 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 }
3806 if (evaluate)
3807 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003808 rettv->v_type = VAR_NUMBER;
3809 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811 }
3812
3813 return OK;
3814}
3815
3816/*
3817 * Handle second level expression:
3818 * expr3 && expr3 && expr3 logical AND
3819 *
3820 * "arg" must point to the first non-white of the expression.
3821 * "arg" is advanced to the next non-white after the recognized expression.
3822 *
3823 * Return OK or FAIL.
3824 */
3825 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003826eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 int evaluate;
3830{
Bram Moolenaar33570922005-01-25 22:26:29 +00003831 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 long result;
3833 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003834 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835
3836 /*
3837 * Get the first variable.
3838 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003839 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 return FAIL;
3841
3842 /*
3843 * Repeat until there is no following "&&".
3844 */
3845 first = TRUE;
3846 result = TRUE;
3847 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3848 {
3849 if (evaluate && first)
3850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003851 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003853 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003854 if (error)
3855 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 first = FALSE;
3857 }
3858
3859 /*
3860 * Get the second variable.
3861 */
3862 *arg = skipwhite(*arg + 2);
3863 if (eval4(arg, &var2, evaluate && result) == FAIL)
3864 return FAIL;
3865
3866 /*
3867 * Compute the result.
3868 */
3869 if (evaluate && result)
3870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003871 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003873 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003874 if (error)
3875 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 }
3877 if (evaluate)
3878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003879 rettv->v_type = VAR_NUMBER;
3880 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
3882 }
3883
3884 return OK;
3885}
3886
3887/*
3888 * Handle third level expression:
3889 * var1 == var2
3890 * var1 =~ var2
3891 * var1 != var2
3892 * var1 !~ var2
3893 * var1 > var2
3894 * var1 >= var2
3895 * var1 < var2
3896 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003897 * var1 is var2
3898 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 *
3900 * "arg" must point to the first non-white of the expression.
3901 * "arg" is advanced to the next non-white after the recognized expression.
3902 *
3903 * Return OK or FAIL.
3904 */
3905 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003906eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 int evaluate;
3910{
Bram Moolenaar33570922005-01-25 22:26:29 +00003911 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 char_u *p;
3913 int i;
3914 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003915 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 int len = 2;
3917 long n1, n2;
3918 char_u *s1, *s2;
3919 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3920 regmatch_T regmatch;
3921 int ic;
3922 char_u *save_cpo;
3923
3924 /*
3925 * Get the first variable.
3926 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003927 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 return FAIL;
3929
3930 p = *arg;
3931 switch (p[0])
3932 {
3933 case '=': if (p[1] == '=')
3934 type = TYPE_EQUAL;
3935 else if (p[1] == '~')
3936 type = TYPE_MATCH;
3937 break;
3938 case '!': if (p[1] == '=')
3939 type = TYPE_NEQUAL;
3940 else if (p[1] == '~')
3941 type = TYPE_NOMATCH;
3942 break;
3943 case '>': if (p[1] != '=')
3944 {
3945 type = TYPE_GREATER;
3946 len = 1;
3947 }
3948 else
3949 type = TYPE_GEQUAL;
3950 break;
3951 case '<': if (p[1] != '=')
3952 {
3953 type = TYPE_SMALLER;
3954 len = 1;
3955 }
3956 else
3957 type = TYPE_SEQUAL;
3958 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003959 case 'i': if (p[1] == 's')
3960 {
3961 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3962 len = 5;
3963 if (!vim_isIDc(p[len]))
3964 {
3965 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3966 type_is = TRUE;
3967 }
3968 }
3969 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971
3972 /*
3973 * If there is a comparitive operator, use it.
3974 */
3975 if (type != TYPE_UNKNOWN)
3976 {
3977 /* extra question mark appended: ignore case */
3978 if (p[len] == '?')
3979 {
3980 ic = TRUE;
3981 ++len;
3982 }
3983 /* extra '#' appended: match case */
3984 else if (p[len] == '#')
3985 {
3986 ic = FALSE;
3987 ++len;
3988 }
3989 /* nothing appened: use 'ignorecase' */
3990 else
3991 ic = p_ic;
3992
3993 /*
3994 * Get the second variable.
3995 */
3996 *arg = skipwhite(p + len);
3997 if (eval5(arg, &var2, evaluate) == FAIL)
3998 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003999 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 return FAIL;
4001 }
4002
4003 if (evaluate)
4004 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004005 if (type_is && rettv->v_type != var2.v_type)
4006 {
4007 /* For "is" a different type always means FALSE, for "notis"
4008 * it means TRUE. */
4009 n1 = (type == TYPE_NEQUAL);
4010 }
4011 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4012 {
4013 if (type_is)
4014 {
4015 n1 = (rettv->v_type == var2.v_type
4016 && rettv->vval.v_list == var2.vval.v_list);
4017 if (type == TYPE_NEQUAL)
4018 n1 = !n1;
4019 }
4020 else if (rettv->v_type != var2.v_type
4021 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4022 {
4023 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004024 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004025 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004026 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004027 clear_tv(rettv);
4028 clear_tv(&var2);
4029 return FAIL;
4030 }
4031 else
4032 {
4033 /* Compare two Lists for being equal or unequal. */
4034 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4035 if (type == TYPE_NEQUAL)
4036 n1 = !n1;
4037 }
4038 }
4039
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004040 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4041 {
4042 if (type_is)
4043 {
4044 n1 = (rettv->v_type == var2.v_type
4045 && rettv->vval.v_dict == var2.vval.v_dict);
4046 if (type == TYPE_NEQUAL)
4047 n1 = !n1;
4048 }
4049 else if (rettv->v_type != var2.v_type
4050 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4051 {
4052 if (rettv->v_type != var2.v_type)
4053 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4054 else
4055 EMSG(_("E736: Invalid operation for Dictionary"));
4056 clear_tv(rettv);
4057 clear_tv(&var2);
4058 return FAIL;
4059 }
4060 else
4061 {
4062 /* Compare two Dictionaries for being equal or unequal. */
4063 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4064 if (type == TYPE_NEQUAL)
4065 n1 = !n1;
4066 }
4067 }
4068
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004069 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4070 {
4071 if (rettv->v_type != var2.v_type
4072 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4073 {
4074 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004075 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004076 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004077 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004078 clear_tv(rettv);
4079 clear_tv(&var2);
4080 return FAIL;
4081 }
4082 else
4083 {
4084 /* Compare two Funcrefs for being equal or unequal. */
4085 if (rettv->vval.v_string == NULL
4086 || var2.vval.v_string == NULL)
4087 n1 = FALSE;
4088 else
4089 n1 = STRCMP(rettv->vval.v_string,
4090 var2.vval.v_string) == 0;
4091 if (type == TYPE_NEQUAL)
4092 n1 = !n1;
4093 }
4094 }
4095
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 /*
4097 * If one of the two variables is a number, compare as a number.
4098 * When using "=~" or "!~", always compare as string.
4099 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004100 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4102 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 n1 = get_tv_number(rettv);
4104 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 switch (type)
4106 {
4107 case TYPE_EQUAL: n1 = (n1 == n2); break;
4108 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4109 case TYPE_GREATER: n1 = (n1 > n2); break;
4110 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4111 case TYPE_SMALLER: n1 = (n1 < n2); break;
4112 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4113 case TYPE_UNKNOWN:
4114 case TYPE_MATCH:
4115 case TYPE_NOMATCH: break; /* avoid gcc warning */
4116 }
4117 }
4118 else
4119 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 s1 = get_tv_string_buf(rettv, buf1);
4121 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4123 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4124 else
4125 i = 0;
4126 n1 = FALSE;
4127 switch (type)
4128 {
4129 case TYPE_EQUAL: n1 = (i == 0); break;
4130 case TYPE_NEQUAL: n1 = (i != 0); break;
4131 case TYPE_GREATER: n1 = (i > 0); break;
4132 case TYPE_GEQUAL: n1 = (i >= 0); break;
4133 case TYPE_SMALLER: n1 = (i < 0); break;
4134 case TYPE_SEQUAL: n1 = (i <= 0); break;
4135
4136 case TYPE_MATCH:
4137 case TYPE_NOMATCH:
4138 /* avoid 'l' flag in 'cpoptions' */
4139 save_cpo = p_cpo;
4140 p_cpo = (char_u *)"";
4141 regmatch.regprog = vim_regcomp(s2,
4142 RE_MAGIC + RE_STRING);
4143 regmatch.rm_ic = ic;
4144 if (regmatch.regprog != NULL)
4145 {
4146 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4147 vim_free(regmatch.regprog);
4148 if (type == TYPE_NOMATCH)
4149 n1 = !n1;
4150 }
4151 p_cpo = save_cpo;
4152 break;
4153
4154 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4155 }
4156 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157 clear_tv(rettv);
4158 clear_tv(&var2);
4159 rettv->v_type = VAR_NUMBER;
4160 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 }
4162 }
4163
4164 return OK;
4165}
4166
4167/*
4168 * Handle fourth level expression:
4169 * + number addition
4170 * - number subtraction
4171 * . string concatenation
4172 *
4173 * "arg" must point to the first non-white of the expression.
4174 * "arg" is advanced to the next non-white after the recognized expression.
4175 *
4176 * Return OK or FAIL.
4177 */
4178 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004179eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 int evaluate;
4183{
Bram Moolenaar33570922005-01-25 22:26:29 +00004184 typval_T var2;
4185 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 int op;
4187 long n1, n2;
4188 char_u *s1, *s2;
4189 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4190 char_u *p;
4191
4192 /*
4193 * Get the first variable.
4194 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 return FAIL;
4197
4198 /*
4199 * Repeat computing, until no '+', '-' or '.' is following.
4200 */
4201 for (;;)
4202 {
4203 op = **arg;
4204 if (op != '+' && op != '-' && op != '.')
4205 break;
4206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 if (op != '+' || rettv->v_type != VAR_LIST)
4208 {
4209 /* For "list + ...", an illegal use of the first operand as
4210 * a number cannot be determined before evaluating the 2nd
4211 * operand: if this is also a list, all is ok.
4212 * For "something . ...", "something - ..." or "non-list + ...",
4213 * we know that the first operand needs to be a string or number
4214 * without evaluating the 2nd operand. So check before to avoid
4215 * side effects after an error. */
4216 if (evaluate && get_tv_string_chk(rettv) == NULL)
4217 {
4218 clear_tv(rettv);
4219 return FAIL;
4220 }
4221 }
4222
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 /*
4224 * Get the second variable.
4225 */
4226 *arg = skipwhite(*arg + 1);
4227 if (eval6(arg, &var2, evaluate) == FAIL)
4228 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 return FAIL;
4231 }
4232
4233 if (evaluate)
4234 {
4235 /*
4236 * Compute the result.
4237 */
4238 if (op == '.')
4239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004240 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4241 s2 = get_tv_string_buf_chk(&var2, buf2);
4242 if (s2 == NULL) /* type error ? */
4243 {
4244 clear_tv(rettv);
4245 clear_tv(&var2);
4246 return FAIL;
4247 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004248 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 clear_tv(rettv);
4250 rettv->v_type = VAR_STRING;
4251 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004253 else if (op == '+' && rettv->v_type == VAR_LIST
4254 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004255 {
4256 /* concatenate Lists */
4257 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4258 &var3) == FAIL)
4259 {
4260 clear_tv(rettv);
4261 clear_tv(&var2);
4262 return FAIL;
4263 }
4264 clear_tv(rettv);
4265 *rettv = var3;
4266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 else
4268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 int error = FALSE;
4270
4271 n1 = get_tv_number_chk(rettv, &error);
4272 if (error)
4273 {
4274 /* This can only happen for "list + non-list".
4275 * For "non-list + ..." or "something - ...", we returned
4276 * before evaluating the 2nd operand. */
4277 clear_tv(rettv);
4278 return FAIL;
4279 }
4280 n2 = get_tv_number_chk(&var2, &error);
4281 if (error)
4282 {
4283 clear_tv(rettv);
4284 clear_tv(&var2);
4285 return FAIL;
4286 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 if (op == '+')
4289 n1 = n1 + n2;
4290 else
4291 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004292 rettv->v_type = VAR_NUMBER;
4293 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 }
4298 return OK;
4299}
4300
4301/*
4302 * Handle fifth level expression:
4303 * * number multiplication
4304 * / number division
4305 * % number modulo
4306 *
4307 * "arg" must point to the first non-white of the expression.
4308 * "arg" is advanced to the next non-white after the recognized expression.
4309 *
4310 * Return OK or FAIL.
4311 */
4312 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 int evaluate;
4317{
Bram Moolenaar33570922005-01-25 22:26:29 +00004318 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 int op;
4320 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 /*
4324 * Get the first variable.
4325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return FAIL;
4328
4329 /*
4330 * Repeat computing, until no '*', '/' or '%' is following.
4331 */
4332 for (;;)
4333 {
4334 op = **arg;
4335 if (op != '*' && op != '/' && op != '%')
4336 break;
4337
4338 if (evaluate)
4339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (error)
4343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 else
4346 n1 = 0;
4347
4348 /*
4349 * Get the second variable.
4350 */
4351 *arg = skipwhite(*arg + 1);
4352 if (eval7(arg, &var2, evaluate) == FAIL)
4353 return FAIL;
4354
4355 if (evaluate)
4356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004358 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004359 if (error)
4360 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
4362 /*
4363 * Compute the result.
4364 */
4365 if (op == '*')
4366 n1 = n1 * n2;
4367 else if (op == '/')
4368 {
4369 if (n2 == 0) /* give an error message? */
4370 n1 = 0x7fffffffL;
4371 else
4372 n1 = n1 / n2;
4373 }
4374 else
4375 {
4376 if (n2 == 0) /* give an error message? */
4377 n1 = 0;
4378 else
4379 n1 = n1 % n2;
4380 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004381 rettv->v_type = VAR_NUMBER;
4382 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
4384 }
4385
4386 return OK;
4387}
4388
4389/*
4390 * Handle sixth level expression:
4391 * number number constant
4392 * "string" string contstant
4393 * 'string' literal string contstant
4394 * &option-name option value
4395 * @r register contents
4396 * identifier variable value
4397 * function() function call
4398 * $VAR environment variable
4399 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004400 * [expr, expr] List
4401 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 *
4403 * Also handle:
4404 * ! in front logical NOT
4405 * - in front unary minus
4406 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004407 * trailing [] subscript in String or List
4408 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 *
4410 * "arg" must point to the first non-white of the expression.
4411 * "arg" is advanced to the next non-white after the recognized expression.
4412 *
4413 * Return OK or FAIL.
4414 */
4415 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004416eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004418 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 int evaluate;
4420{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 long n;
4422 int len;
4423 char_u *s;
4424 int val;
4425 char_u *start_leader, *end_leader;
4426 int ret = OK;
4427 char_u *alias;
4428
4429 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004430 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004431 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004433 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
4435 /*
4436 * Skip '!' and '-' characters. They are handled later.
4437 */
4438 start_leader = *arg;
4439 while (**arg == '!' || **arg == '-' || **arg == '+')
4440 *arg = skipwhite(*arg + 1);
4441 end_leader = *arg;
4442
4443 switch (**arg)
4444 {
4445 /*
4446 * Number constant.
4447 */
4448 case '0':
4449 case '1':
4450 case '2':
4451 case '3':
4452 case '4':
4453 case '5':
4454 case '6':
4455 case '7':
4456 case '8':
4457 case '9':
4458 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4459 *arg += len;
4460 if (evaluate)
4461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004462 rettv->v_type = VAR_NUMBER;
4463 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465 break;
4466
4467 /*
4468 * String constant: "string".
4469 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 break;
4472
4473 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004474 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004476 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004477 break;
4478
4479 /*
4480 * List: [expr, expr]
4481 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004482 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 break;
4484
4485 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004486 * Dictionary: {key: val, key: val}
4487 */
4488 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4489 break;
4490
4491 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004492 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004494 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 break;
4496
4497 /*
4498 * Environment variable: $VAR.
4499 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004500 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 break;
4502
4503 /*
4504 * Register contents: @r.
4505 */
4506 case '@': ++*arg;
4507 if (evaluate)
4508 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004509 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004510 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 }
4512 if (**arg != NUL)
4513 ++*arg;
4514 break;
4515
4516 /*
4517 * nested expression: (expression).
4518 */
4519 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004520 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 if (**arg == ')')
4522 ++*arg;
4523 else if (ret == OK)
4524 {
4525 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004526 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 ret = FAIL;
4528 }
4529 break;
4530
Bram Moolenaar8c711452005-01-14 21:53:12 +00004531 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 break;
4533 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004534
4535 if (ret == NOTDONE)
4536 {
4537 /*
4538 * Must be a variable or function name.
4539 * Can also be a curly-braces kind of name: {expr}.
4540 */
4541 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004542 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004543 if (alias != NULL)
4544 s = alias;
4545
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004546 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004547 ret = FAIL;
4548 else
4549 {
4550 if (**arg == '(') /* recursive! */
4551 {
4552 /* If "s" is the name of a variable of type VAR_FUNC
4553 * use its contents. */
4554 s = deref_func_name(s, &len);
4555
4556 /* Invoke the function. */
4557 ret = get_func_tv(s, len, rettv, arg,
4558 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004559 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004560 /* Stop the expression evaluation when immediately
4561 * aborting on error, or when an interrupt occurred or
4562 * an exception was thrown but not caught. */
4563 if (aborting())
4564 {
4565 if (ret == OK)
4566 clear_tv(rettv);
4567 ret = FAIL;
4568 }
4569 }
4570 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004571 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004572 else
4573 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004574 }
4575
4576 if (alias != NULL)
4577 vim_free(alias);
4578 }
4579
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 *arg = skipwhite(*arg);
4581
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004582 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4583 * expr(expr). */
4584 if (ret == OK)
4585 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
4587 /*
4588 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4589 */
4590 if (ret == OK && evaluate && end_leader > start_leader)
4591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004592 int error = FALSE;
4593
4594 val = get_tv_number_chk(rettv, &error);
4595 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004597 clear_tv(rettv);
4598 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004600 else
4601 {
4602 while (end_leader > start_leader)
4603 {
4604 --end_leader;
4605 if (*end_leader == '!')
4606 val = !val;
4607 else if (*end_leader == '-')
4608 val = -val;
4609 }
4610 clear_tv(rettv);
4611 rettv->v_type = VAR_NUMBER;
4612 rettv->vval.v_number = val;
4613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
4615
4616 return ret;
4617}
4618
4619/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620 * Evaluate an "[expr]" or "[expr:expr]" index.
4621 * "*arg" points to the '['.
4622 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4623 */
4624 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004625eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004626 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004627 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004628 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004629 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004630{
4631 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004632 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004633 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004634 long len = -1;
4635 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004636 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004637 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004638
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004639 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004640 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004641 if (verbose)
4642 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004643 return FAIL;
4644 }
4645
Bram Moolenaar8c711452005-01-14 21:53:12 +00004646 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004647 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004648 /*
4649 * dict.name
4650 */
4651 key = *arg + 1;
4652 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4653 ;
4654 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004656 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004657 }
4658 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004660 /*
4661 * something[idx]
4662 *
4663 * Get the (first) variable from inside the [].
4664 */
4665 *arg = skipwhite(*arg + 1);
4666 if (**arg == ':')
4667 empty1 = TRUE;
4668 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4669 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004670 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4671 {
4672 /* not a number or string */
4673 clear_tv(&var1);
4674 return FAIL;
4675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004676
4677 /*
4678 * Get the second variable from inside the [:].
4679 */
4680 if (**arg == ':')
4681 {
4682 range = TRUE;
4683 *arg = skipwhite(*arg + 1);
4684 if (**arg == ']')
4685 empty2 = TRUE;
4686 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004688 if (!empty1)
4689 clear_tv(&var1);
4690 return FAIL;
4691 }
4692 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4693 {
4694 /* not a number or string */
4695 if (!empty1)
4696 clear_tv(&var1);
4697 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004698 return FAIL;
4699 }
4700 }
4701
4702 /* Check for the ']'. */
4703 if (**arg != ']')
4704 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004705 if (verbose)
4706 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004707 clear_tv(&var1);
4708 if (range)
4709 clear_tv(&var2);
4710 return FAIL;
4711 }
4712 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004713 }
4714
4715 if (evaluate)
4716 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004717 n1 = 0;
4718 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004720 n1 = get_tv_number(&var1);
4721 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 }
4723 if (range)
4724 {
4725 if (empty2)
4726 n2 = -1;
4727 else
4728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004729 n2 = get_tv_number(&var2);
4730 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004731 }
4732 }
4733
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004734 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004735 {
4736 case VAR_NUMBER:
4737 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004738 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004739 len = (long)STRLEN(s);
4740 if (range)
4741 {
4742 /* The resulting variable is a substring. If the indexes
4743 * are out of range the result is empty. */
4744 if (n1 < 0)
4745 {
4746 n1 = len + n1;
4747 if (n1 < 0)
4748 n1 = 0;
4749 }
4750 if (n2 < 0)
4751 n2 = len + n2;
4752 else if (n2 >= len)
4753 n2 = len;
4754 if (n1 >= len || n2 < 0 || n1 > n2)
4755 s = NULL;
4756 else
4757 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4758 }
4759 else
4760 {
4761 /* The resulting variable is a string of a single
4762 * character. If the index is too big or negative the
4763 * result is empty. */
4764 if (n1 >= len || n1 < 0)
4765 s = NULL;
4766 else
4767 s = vim_strnsave(s + n1, 1);
4768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 clear_tv(rettv);
4770 rettv->v_type = VAR_STRING;
4771 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772 break;
4773
4774 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004775 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 if (n1 < 0)
4777 n1 = len + n1;
4778 if (!empty1 && (n1 < 0 || n1 >= len))
4779 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004780 if (verbose)
4781 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 return FAIL;
4783 }
4784 if (range)
4785 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004786 list_T *l;
4787 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004788
4789 if (n2 < 0)
4790 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004791 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004792 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004793 if (verbose)
4794 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004795 return FAIL;
4796 }
4797 l = list_alloc();
4798 if (l == NULL)
4799 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004800 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 n1 <= n2; ++n1)
4802 {
4803 if (list_append_tv(l, &item->li_tv) == FAIL)
4804 {
4805 list_free(l);
4806 return FAIL;
4807 }
4808 item = item->li_next;
4809 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004810 clear_tv(rettv);
4811 rettv->v_type = VAR_LIST;
4812 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004813 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004814 }
4815 else
4816 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004817 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004818 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004819 clear_tv(rettv);
4820 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004821 }
4822 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004823
4824 case VAR_DICT:
4825 if (range)
4826 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004827 if (verbose)
4828 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004829 if (len == -1)
4830 clear_tv(&var1);
4831 return FAIL;
4832 }
4833 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004834 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004835
4836 if (len == -1)
4837 {
4838 key = get_tv_string(&var1);
4839 if (*key == NUL)
4840 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004841 if (verbose)
4842 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004843 clear_tv(&var1);
4844 return FAIL;
4845 }
4846 }
4847
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004848 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004849
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004850 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004851 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004852 if (len == -1)
4853 clear_tv(&var1);
4854 if (item == NULL)
4855 return FAIL;
4856
4857 copy_tv(&item->di_tv, &var1);
4858 clear_tv(rettv);
4859 *rettv = var1;
4860 }
4861 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004862 }
4863 }
4864
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004865 return OK;
4866}
4867
4868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 * Get an option value.
4870 * "arg" points to the '&' or '+' before the option name.
4871 * "arg" is advanced to character after the option name.
4872 * Return OK or FAIL.
4873 */
4874 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004875get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004877 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 int evaluate;
4879{
4880 char_u *option_end;
4881 long numval;
4882 char_u *stringval;
4883 int opt_type;
4884 int c;
4885 int working = (**arg == '+'); /* has("+option") */
4886 int ret = OK;
4887 int opt_flags;
4888
4889 /*
4890 * Isolate the option name and find its value.
4891 */
4892 option_end = find_option_end(arg, &opt_flags);
4893 if (option_end == NULL)
4894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 EMSG2(_("E112: Option name missing: %s"), *arg);
4897 return FAIL;
4898 }
4899
4900 if (!evaluate)
4901 {
4902 *arg = option_end;
4903 return OK;
4904 }
4905
4906 c = *option_end;
4907 *option_end = NUL;
4908 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910
4911 if (opt_type == -3) /* invalid name */
4912 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 EMSG2(_("E113: Unknown option: %s"), *arg);
4915 ret = FAIL;
4916 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004917 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 {
4919 if (opt_type == -2) /* hidden string option */
4920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 rettv->v_type = VAR_STRING;
4922 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 }
4924 else if (opt_type == -1) /* hidden number option */
4925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004926 rettv->v_type = VAR_NUMBER;
4927 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
4929 else if (opt_type == 1) /* number option */
4930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004931 rettv->v_type = VAR_NUMBER;
4932 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 else /* string option */
4935 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004936 rettv->v_type = VAR_STRING;
4937 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
4939 }
4940 else if (working && (opt_type == -2 || opt_type == -1))
4941 ret = FAIL;
4942
4943 *option_end = c; /* put back for error messages */
4944 *arg = option_end;
4945
4946 return ret;
4947}
4948
4949/*
4950 * Allocate a variable for a string constant.
4951 * Return OK or FAIL.
4952 */
4953 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004954get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 int evaluate;
4958{
4959 char_u *p;
4960 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 int extra = 0;
4962
4963 /*
4964 * Find the end of the string, skipping backslashed characters.
4965 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004966 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 {
4968 if (*p == '\\' && p[1] != NUL)
4969 {
4970 ++p;
4971 /* A "\<x>" form occupies at least 4 characters, and produces up
4972 * to 6 characters: reserve space for 2 extra */
4973 if (*p == '<')
4974 extra += 2;
4975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 }
4977
4978 if (*p != '"')
4979 {
4980 EMSG2(_("E114: Missing quote: %s"), *arg);
4981 return FAIL;
4982 }
4983
4984 /* If only parsing, set *arg and return here */
4985 if (!evaluate)
4986 {
4987 *arg = p + 1;
4988 return OK;
4989 }
4990
4991 /*
4992 * Copy the string into allocated memory, handling backslashed
4993 * characters.
4994 */
4995 name = alloc((unsigned)(p - *arg + extra));
4996 if (name == NULL)
4997 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 rettv->v_type = VAR_STRING;
4999 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000
Bram Moolenaar8c711452005-01-14 21:53:12 +00005001 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
5003 if (*p == '\\')
5004 {
5005 switch (*++p)
5006 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005007 case 'b': *name++ = BS; ++p; break;
5008 case 'e': *name++ = ESC; ++p; break;
5009 case 'f': *name++ = FF; ++p; break;
5010 case 'n': *name++ = NL; ++p; break;
5011 case 'r': *name++ = CAR; ++p; break;
5012 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
5014 case 'X': /* hex: "\x1", "\x12" */
5015 case 'x':
5016 case 'u': /* Unicode: "\u0023" */
5017 case 'U':
5018 if (vim_isxdigit(p[1]))
5019 {
5020 int n, nr;
5021 int c = toupper(*p);
5022
5023 if (c == 'X')
5024 n = 2;
5025 else
5026 n = 4;
5027 nr = 0;
5028 while (--n >= 0 && vim_isxdigit(p[1]))
5029 {
5030 ++p;
5031 nr = (nr << 4) + hex2nr(*p);
5032 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005033 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034#ifdef FEAT_MBYTE
5035 /* For "\u" store the number according to
5036 * 'encoding'. */
5037 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 else
5040#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 break;
5044
5045 /* octal: "\1", "\12", "\123" */
5046 case '0':
5047 case '1':
5048 case '2':
5049 case '3':
5050 case '4':
5051 case '5':
5052 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 case '7': *name = *p++ - '0';
5054 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 *name = (*name << 3) + *p++ - '0';
5057 if (*p >= '0' && *p <= '7')
5058 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 break;
5062
5063 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 if (extra != 0)
5066 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 break;
5069 }
5070 /* FALLTHROUGH */
5071
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 break;
5074 }
5075 }
5076 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 *arg = p + 1;
5082
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 return OK;
5084}
5085
5086/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 * Return OK or FAIL.
5089 */
5090 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005091get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 int evaluate;
5095{
5096 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005097 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005098 int reduce = 0;
5099
5100 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 */
5103 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5104 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005106 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005107 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005108 break;
5109 ++reduce;
5110 ++p;
5111 }
5112 }
5113
Bram Moolenaar8c711452005-01-14 21:53:12 +00005114 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005115 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005117 return FAIL;
5118 }
5119
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005121 if (!evaluate)
5122 {
5123 *arg = p + 1;
5124 return OK;
5125 }
5126
5127 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005128 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005129 */
5130 str = alloc((unsigned)((p - *arg) - reduce));
5131 if (str == NULL)
5132 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133 rettv->v_type = VAR_STRING;
5134 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005135
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005137 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005139 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005141 break;
5142 ++p;
5143 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005145 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005147 *arg = p + 1;
5148
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005149 return OK;
5150}
5151
5152/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005153 * Allocate a variable for a List and fill it from "*arg".
5154 * Return OK or FAIL.
5155 */
5156 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005159 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005160 int evaluate;
5161{
Bram Moolenaar33570922005-01-25 22:26:29 +00005162 list_T *l = NULL;
5163 typval_T tv;
5164 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165
5166 if (evaluate)
5167 {
5168 l = list_alloc();
5169 if (l == NULL)
5170 return FAIL;
5171 }
5172
5173 *arg = skipwhite(*arg + 1);
5174 while (**arg != ']' && **arg != NUL)
5175 {
5176 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5177 goto failret;
5178 if (evaluate)
5179 {
5180 item = listitem_alloc();
5181 if (item != NULL)
5182 {
5183 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005184 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185 list_append(l, item);
5186 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005187 else
5188 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005189 }
5190
5191 if (**arg == ']')
5192 break;
5193 if (**arg != ',')
5194 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005195 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005196 goto failret;
5197 }
5198 *arg = skipwhite(*arg + 1);
5199 }
5200
5201 if (**arg != ']')
5202 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005203 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005204failret:
5205 if (evaluate)
5206 list_free(l);
5207 return FAIL;
5208 }
5209
5210 *arg = skipwhite(*arg + 1);
5211 if (evaluate)
5212 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005213 rettv->v_type = VAR_LIST;
5214 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005215 ++l->lv_refcount;
5216 }
5217
5218 return OK;
5219}
5220
5221/*
5222 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005223 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005225 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226list_alloc()
5227{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005228 list_T *l;
5229
5230 l = (list_T *)alloc_clear(sizeof(list_T));
5231 if (l != NULL)
5232 {
5233 /* Prepend the list to the list of lists for garbage collection. */
5234 if (first_list != NULL)
5235 first_list->lv_used_prev = l;
5236 l->lv_used_prev = NULL;
5237 l->lv_used_next = first_list;
5238 first_list = l;
5239 }
5240 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241}
5242
5243/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005244 * Allocate an empty list for a return value.
5245 * Returns OK or FAIL.
5246 */
5247 static int
5248rettv_list_alloc(rettv)
5249 typval_T *rettv;
5250{
5251 list_T *l = list_alloc();
5252
5253 if (l == NULL)
5254 return FAIL;
5255
5256 rettv->vval.v_list = l;
5257 rettv->v_type = VAR_LIST;
5258 ++l->lv_refcount;
5259 return OK;
5260}
5261
5262/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 * Unreference a list: decrement the reference count and free it when it
5264 * becomes zero.
5265 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005266 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005268 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005270 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5271 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272}
5273
5274/*
5275 * Free a list, including all items it points to.
5276 * Ignores the reference count.
5277 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005278 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005280 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281{
Bram Moolenaar33570922005-01-25 22:26:29 +00005282 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283
Bram Moolenaard9fba312005-06-26 22:34:35 +00005284 /* Avoid that recursive reference to the list frees us again. */
5285 l->lv_refcount = DEL_REFCOUNT;
5286
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005287 /* Remove the list from the list of lists for garbage collection. */
5288 if (l->lv_used_prev == NULL)
5289 first_list = l->lv_used_next;
5290 else
5291 l->lv_used_prev->lv_used_next = l->lv_used_next;
5292 if (l->lv_used_next != NULL)
5293 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5294
Bram Moolenaard9fba312005-06-26 22:34:35 +00005295 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005297 /* Remove the item before deleting it. */
5298 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 listitem_free(item);
5300 }
5301 vim_free(l);
5302}
5303
5304/*
5305 * Allocate a list item.
5306 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005307 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308listitem_alloc()
5309{
Bram Moolenaar33570922005-01-25 22:26:29 +00005310 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311}
5312
5313/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005314 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315 */
5316 static void
5317listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005318 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005320 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005321 vim_free(item);
5322}
5323
5324/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005325 * Remove a list item from a List and free it. Also clears the value.
5326 */
5327 static void
5328listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005329 list_T *l;
5330 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005331{
5332 list_remove(l, item, item);
5333 listitem_free(item);
5334}
5335
5336/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337 * Get the number of items in a list.
5338 */
5339 static long
5340list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005341 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 if (l == NULL)
5344 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005345 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346}
5347
5348/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005349 * Return TRUE when two lists have exactly the same values.
5350 */
5351 static int
5352list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005353 list_T *l1;
5354 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005355 int ic; /* ignore case for strings */
5356{
Bram Moolenaar33570922005-01-25 22:26:29 +00005357 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005358
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005359 if (list_len(l1) != list_len(l2))
5360 return FALSE;
5361
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005362 for (item1 = l1->lv_first, item2 = l2->lv_first;
5363 item1 != NULL && item2 != NULL;
5364 item1 = item1->li_next, item2 = item2->li_next)
5365 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5366 return FALSE;
5367 return item1 == NULL && item2 == NULL;
5368}
5369
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005370#if defined(FEAT_PYTHON) || defined(PROTO)
5371/*
5372 * Return the dictitem that an entry in a hashtable points to.
5373 */
5374 dictitem_T *
5375dict_lookup(hi)
5376 hashitem_T *hi;
5377{
5378 return HI2DI(hi);
5379}
5380#endif
5381
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005382/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005383 * Return TRUE when two dictionaries have exactly the same key/values.
5384 */
5385 static int
5386dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005387 dict_T *d1;
5388 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005389 int ic; /* ignore case for strings */
5390{
Bram Moolenaar33570922005-01-25 22:26:29 +00005391 hashitem_T *hi;
5392 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005393 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005394
5395 if (dict_len(d1) != dict_len(d2))
5396 return FALSE;
5397
Bram Moolenaar33570922005-01-25 22:26:29 +00005398 todo = d1->dv_hashtab.ht_used;
5399 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005401 if (!HASHITEM_EMPTY(hi))
5402 {
5403 item2 = dict_find(d2, hi->hi_key, -1);
5404 if (item2 == NULL)
5405 return FALSE;
5406 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5407 return FALSE;
5408 --todo;
5409 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005410 }
5411 return TRUE;
5412}
5413
5414/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005415 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005416 * Compares the items just like "==" would compare them, but strings and
5417 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005418 */
5419 static int
5420tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005421 typval_T *tv1;
5422 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005423 int ic; /* ignore case */
5424{
5425 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005426 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005427
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005428 if (tv1->v_type != tv2->v_type)
5429 return FALSE;
5430
5431 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005432 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005433 case VAR_LIST:
5434 /* recursive! */
5435 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5436
5437 case VAR_DICT:
5438 /* recursive! */
5439 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5440
5441 case VAR_FUNC:
5442 return (tv1->vval.v_string != NULL
5443 && tv2->vval.v_string != NULL
5444 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5445
5446 case VAR_NUMBER:
5447 return tv1->vval.v_number == tv2->vval.v_number;
5448
5449 case VAR_STRING:
5450 s1 = get_tv_string_buf(tv1, buf1);
5451 s2 = get_tv_string_buf(tv2, buf2);
5452 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005453 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005454
5455 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005456 return TRUE;
5457}
5458
5459/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 * Locate item with index "n" in list "l" and return it.
5461 * A negative index is counted from the end; -1 is the last item.
5462 * Returns NULL when "n" is out of range.
5463 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005464 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005466 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 long n;
5468{
Bram Moolenaar33570922005-01-25 22:26:29 +00005469 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 long idx;
5471
5472 if (l == NULL)
5473 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005474
5475 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005477 n = l->lv_len + n;
5478
5479 /* Check for index out of range. */
5480 if (n < 0 || n >= l->lv_len)
5481 return NULL;
5482
5483 /* When there is a cached index may start search from there. */
5484 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005486 if (n < l->lv_idx / 2)
5487 {
5488 /* closest to the start of the list */
5489 item = l->lv_first;
5490 idx = 0;
5491 }
5492 else if (n > (l->lv_idx + l->lv_len) / 2)
5493 {
5494 /* closest to the end of the list */
5495 item = l->lv_last;
5496 idx = l->lv_len - 1;
5497 }
5498 else
5499 {
5500 /* closest to the cached index */
5501 item = l->lv_idx_item;
5502 idx = l->lv_idx;
5503 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 }
5505 else
5506 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005507 if (n < l->lv_len / 2)
5508 {
5509 /* closest to the start of the list */
5510 item = l->lv_first;
5511 idx = 0;
5512 }
5513 else
5514 {
5515 /* closest to the end of the list */
5516 item = l->lv_last;
5517 idx = l->lv_len - 1;
5518 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005520
5521 while (n > idx)
5522 {
5523 /* search forward */
5524 item = item->li_next;
5525 ++idx;
5526 }
5527 while (n < idx)
5528 {
5529 /* search backward */
5530 item = item->li_prev;
5531 --idx;
5532 }
5533
5534 /* cache the used index */
5535 l->lv_idx = idx;
5536 l->lv_idx_item = item;
5537
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 return item;
5539}
5540
5541/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005542 * Get list item "l[idx]" as a number.
5543 */
5544 static long
5545list_find_nr(l, idx, errorp)
5546 list_T *l;
5547 long idx;
5548 int *errorp; /* set to TRUE when something wrong */
5549{
5550 listitem_T *li;
5551
5552 li = list_find(l, idx);
5553 if (li == NULL)
5554 {
5555 if (errorp != NULL)
5556 *errorp = TRUE;
5557 return -1L;
5558 }
5559 return get_tv_number_chk(&li->li_tv, errorp);
5560}
5561
5562/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005563 * Locate "item" list "l" and return its index.
5564 * Returns -1 when "item" is not in the list.
5565 */
5566 static long
5567list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005568 list_T *l;
5569 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005570{
5571 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005572 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005573
5574 if (l == NULL)
5575 return -1;
5576 idx = 0;
5577 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5578 ++idx;
5579 if (li == NULL)
5580 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005581 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005582}
5583
5584/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 * Append item "item" to the end of list "l".
5586 */
5587 static void
5588list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005589 list_T *l;
5590 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005591{
5592 if (l->lv_last == NULL)
5593 {
5594 /* empty list */
5595 l->lv_first = item;
5596 l->lv_last = item;
5597 item->li_prev = NULL;
5598 }
5599 else
5600 {
5601 l->lv_last->li_next = item;
5602 item->li_prev = l->lv_last;
5603 l->lv_last = item;
5604 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005605 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005606 item->li_next = NULL;
5607}
5608
5609/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005610 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005611 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 */
5613 static int
5614list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005615 list_T *l;
5616 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005617{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005618 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619
Bram Moolenaar05159a02005-02-26 23:04:13 +00005620 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005621 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005622 copy_tv(tv, &li->li_tv);
5623 list_append(l, li);
5624 return OK;
5625}
5626
5627/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005628 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005629 * Return FAIL when out of memory.
5630 */
5631 int
5632list_append_dict(list, dict)
5633 list_T *list;
5634 dict_T *dict;
5635{
5636 listitem_T *li = listitem_alloc();
5637
5638 if (li == NULL)
5639 return FAIL;
5640 li->li_tv.v_type = VAR_DICT;
5641 li->li_tv.v_lock = 0;
5642 li->li_tv.vval.v_dict = dict;
5643 list_append(list, li);
5644 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645 return OK;
5646}
5647
5648/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005649 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005650 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005651 * Returns FAIL when out of memory.
5652 */
5653 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005654list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005655 list_T *l;
5656 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005657 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005658{
5659 listitem_T *li = listitem_alloc();
5660
5661 if (li == NULL)
5662 return FAIL;
5663 list_append(l, li);
5664 li->li_tv.v_type = VAR_STRING;
5665 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005666 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5667 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005668 return FAIL;
5669 return OK;
5670}
5671
5672/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005673 * Append "n" to list "l".
5674 * Returns FAIL when out of memory.
5675 */
5676 static int
5677list_append_number(l, n)
5678 list_T *l;
5679 varnumber_T n;
5680{
5681 listitem_T *li;
5682
5683 li = listitem_alloc();
5684 if (li == NULL)
5685 return FAIL;
5686 li->li_tv.v_type = VAR_NUMBER;
5687 li->li_tv.v_lock = 0;
5688 li->li_tv.vval.v_number = n;
5689 list_append(l, li);
5690 return OK;
5691}
5692
5693/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005694 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005695 * If "item" is NULL append at the end.
5696 * Return FAIL when out of memory.
5697 */
5698 static int
5699list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005700 list_T *l;
5701 typval_T *tv;
5702 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005703{
Bram Moolenaar33570922005-01-25 22:26:29 +00005704 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005705
5706 if (ni == NULL)
5707 return FAIL;
5708 copy_tv(tv, &ni->li_tv);
5709 if (item == NULL)
5710 /* Append new item at end of list. */
5711 list_append(l, ni);
5712 else
5713 {
5714 /* Insert new item before existing item. */
5715 ni->li_prev = item->li_prev;
5716 ni->li_next = item;
5717 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005718 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005719 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005720 ++l->lv_idx;
5721 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005722 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005723 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005724 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005725 l->lv_idx_item = NULL;
5726 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005727 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005728 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005729 }
5730 return OK;
5731}
5732
5733/*
5734 * Extend "l1" with "l2".
5735 * If "bef" is NULL append at the end, otherwise insert before this item.
5736 * Returns FAIL when out of memory.
5737 */
5738 static int
5739list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005740 list_T *l1;
5741 list_T *l2;
5742 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005743{
Bram Moolenaar33570922005-01-25 22:26:29 +00005744 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005745
5746 for (item = l2->lv_first; item != NULL; item = item->li_next)
5747 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5748 return FAIL;
5749 return OK;
5750}
5751
5752/*
5753 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5754 * Return FAIL when out of memory.
5755 */
5756 static int
5757list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005758 list_T *l1;
5759 list_T *l2;
5760 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005761{
Bram Moolenaar33570922005-01-25 22:26:29 +00005762 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005763
5764 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005765 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005766 if (l == NULL)
5767 return FAIL;
5768 tv->v_type = VAR_LIST;
5769 tv->vval.v_list = l;
5770
5771 /* append all items from the second list */
5772 return list_extend(l, l2, NULL);
5773}
5774
5775/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005776 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005778 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 * Returns NULL when out of memory.
5780 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005781 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005782list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005783 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005784 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005785 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005786{
Bram Moolenaar33570922005-01-25 22:26:29 +00005787 list_T *copy;
5788 listitem_T *item;
5789 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005790
5791 if (orig == NULL)
5792 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005793
5794 copy = list_alloc();
5795 if (copy != NULL)
5796 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005797 if (copyID != 0)
5798 {
5799 /* Do this before adding the items, because one of the items may
5800 * refer back to this list. */
5801 orig->lv_copyID = copyID;
5802 orig->lv_copylist = copy;
5803 }
5804 for (item = orig->lv_first; item != NULL && !got_int;
5805 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 {
5807 ni = listitem_alloc();
5808 if (ni == NULL)
5809 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005810 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005811 {
5812 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5813 {
5814 vim_free(ni);
5815 break;
5816 }
5817 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005819 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005820 list_append(copy, ni);
5821 }
5822 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005823 if (item != NULL)
5824 {
5825 list_unref(copy);
5826 copy = NULL;
5827 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 }
5829
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830 return copy;
5831}
5832
5833/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005834 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005835 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005837 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005838list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005839 list_T *l;
5840 listitem_T *item;
5841 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005842{
Bram Moolenaar33570922005-01-25 22:26:29 +00005843 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005845 /* notify watchers */
5846 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005848 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005849 list_fix_watch(l, ip);
5850 if (ip == item2)
5851 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005853
5854 if (item2->li_next == NULL)
5855 l->lv_last = item->li_prev;
5856 else
5857 item2->li_next->li_prev = item->li_prev;
5858 if (item->li_prev == NULL)
5859 l->lv_first = item2->li_next;
5860 else
5861 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005862 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863}
5864
5865/*
5866 * Return an allocated string with the string representation of a list.
5867 * May return NULL.
5868 */
5869 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005870list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005871 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005872 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873{
5874 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875
5876 if (tv->vval.v_list == NULL)
5877 return NULL;
5878 ga_init2(&ga, (int)sizeof(char), 80);
5879 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005880 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005881 {
5882 vim_free(ga.ga_data);
5883 return NULL;
5884 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885 ga_append(&ga, ']');
5886 ga_append(&ga, NUL);
5887 return (char_u *)ga.ga_data;
5888}
5889
5890/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 * Join list "l" into a string in "*gap", using separator "sep".
5892 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005893 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005895 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005896list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005898 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 char_u *sep;
5900 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005901 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902{
5903 int first = TRUE;
5904 char_u *tofree;
5905 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005906 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907 char_u *s;
5908
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005909 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005910 {
5911 if (first)
5912 first = FALSE;
5913 else
5914 ga_concat(gap, sep);
5915
5916 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005917 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005918 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005919 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 if (s != NULL)
5921 ga_concat(gap, s);
5922 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005923 if (s == NULL)
5924 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005926 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005927}
5928
5929/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005930 * Garbage collection for lists and dictionaries.
5931 *
5932 * We use reference counts to be able to free most items right away when they
5933 * are no longer used. But for composite items it's possible that it becomes
5934 * unused while the reference count is > 0: When there is a recursive
5935 * reference. Example:
5936 * :let l = [1, 2, 3]
5937 * :let d = {9: l}
5938 * :let l[1] = d
5939 *
5940 * Since this is quite unusual we handle this with garbage collection: every
5941 * once in a while find out which lists and dicts are not referenced from any
5942 * variable.
5943 *
5944 * Here is a good reference text about garbage collection (refers to Python
5945 * but it applies to all reference-counting mechanisms):
5946 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005947 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005948
5949/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005950 * Do garbage collection for lists and dicts.
5951 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005952 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005953 int
5954garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005955{
5956 dict_T *dd;
5957 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005958 int copyID = ++current_copyID;
5959 buf_T *buf;
5960 win_T *wp;
5961 int i;
5962 funccall_T *fc;
5963 int did_free = FALSE;
5964
5965 /*
5966 * 1. Go through all accessible variables and mark all lists and dicts
5967 * with copyID.
5968 */
5969 /* script-local variables */
5970 for (i = 1; i <= ga_scripts.ga_len; ++i)
5971 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5972
5973 /* buffer-local variables */
5974 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5975 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5976
5977 /* window-local variables */
5978 FOR_ALL_WINDOWS(wp)
5979 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5980
5981 /* global variables */
5982 set_ref_in_ht(&globvarht, copyID);
5983
5984 /* function-local variables */
5985 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5986 {
5987 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5988 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5989 }
5990
5991 /*
5992 * 2. Go through the list of dicts and free items without the copyID.
5993 */
5994 for (dd = first_dict; dd != NULL; )
5995 if (dd->dv_copyID != copyID)
5996 {
5997 dict_free(dd);
5998 did_free = TRUE;
5999
6000 /* restart, next dict may also have been freed */
6001 dd = first_dict;
6002 }
6003 else
6004 dd = dd->dv_used_next;
6005
6006 /*
6007 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006008 * But don't free a list that has a watcher (used in a for loop), these
6009 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006010 */
6011 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006012 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006013 {
6014 list_free(ll);
6015 did_free = TRUE;
6016
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006017 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006018 ll = first_list;
6019 }
6020 else
6021 ll = ll->lv_used_next;
6022
6023 return did_free;
6024}
6025
6026/*
6027 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6028 */
6029 static void
6030set_ref_in_ht(ht, copyID)
6031 hashtab_T *ht;
6032 int copyID;
6033{
6034 int todo;
6035 hashitem_T *hi;
6036
6037 todo = ht->ht_used;
6038 for (hi = ht->ht_array; todo > 0; ++hi)
6039 if (!HASHITEM_EMPTY(hi))
6040 {
6041 --todo;
6042 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6043 }
6044}
6045
6046/*
6047 * Mark all lists and dicts referenced through list "l" with "copyID".
6048 */
6049 static void
6050set_ref_in_list(l, copyID)
6051 list_T *l;
6052 int copyID;
6053{
6054 listitem_T *li;
6055
6056 for (li = l->lv_first; li != NULL; li = li->li_next)
6057 set_ref_in_item(&li->li_tv, copyID);
6058}
6059
6060/*
6061 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6062 */
6063 static void
6064set_ref_in_item(tv, copyID)
6065 typval_T *tv;
6066 int copyID;
6067{
6068 dict_T *dd;
6069 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006070
6071 switch (tv->v_type)
6072 {
6073 case VAR_DICT:
6074 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006075 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006076 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006077 /* Didn't see this dict yet. */
6078 dd->dv_copyID = copyID;
6079 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006080 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006081 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006082
6083 case VAR_LIST:
6084 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006085 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006086 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006087 /* Didn't see this list yet. */
6088 ll->lv_copyID = copyID;
6089 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006090 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006091 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006092 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006093 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006094}
6095
6096/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006097 * Allocate an empty header for a dictionary.
6098 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006099 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006100dict_alloc()
6101{
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006103
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006105 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006106 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006107 /* Add the list to the hashtable for garbage collection. */
6108 if (first_dict != NULL)
6109 first_dict->dv_used_prev = d;
6110 d->dv_used_next = first_dict;
6111 d->dv_used_prev = NULL;
6112
Bram Moolenaar33570922005-01-25 22:26:29 +00006113 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006114 d->dv_lock = 0;
6115 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006116 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006117 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006118 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006119}
6120
6121/*
6122 * Unreference a Dictionary: decrement the reference count and free it when it
6123 * becomes zero.
6124 */
6125 static void
6126dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006127 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006128{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006129 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6130 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006131}
6132
6133/*
6134 * Free a Dictionary, including all items it contains.
6135 * Ignores the reference count.
6136 */
6137 static void
6138dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006139 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006140{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006141 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006142 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006143 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006144
Bram Moolenaard9fba312005-06-26 22:34:35 +00006145 /* Avoid that recursive reference to the dict frees us again. */
6146 d->dv_refcount = DEL_REFCOUNT;
6147
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006148 /* Remove the dict from the list of dicts for garbage collection. */
6149 if (d->dv_used_prev == NULL)
6150 first_dict = d->dv_used_next;
6151 else
6152 d->dv_used_prev->dv_used_next = d->dv_used_next;
6153 if (d->dv_used_next != NULL)
6154 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6155
6156 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006157 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 todo = d->dv_hashtab.ht_used;
6159 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006160 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006161 if (!HASHITEM_EMPTY(hi))
6162 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006163 /* Remove the item before deleting it, just in case there is
6164 * something recursive causing trouble. */
6165 di = HI2DI(hi);
6166 hash_remove(&d->dv_hashtab, hi);
6167 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006168 --todo;
6169 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006170 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006171 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006172 vim_free(d);
6173}
6174
6175/*
6176 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006177 * The "key" is copied to the new item.
6178 * Note that the value of the item "di_tv" still needs to be initialized!
6179 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006180 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006181 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006182dictitem_alloc(key)
6183 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006184{
Bram Moolenaar33570922005-01-25 22:26:29 +00006185 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006186
Bram Moolenaar33570922005-01-25 22:26:29 +00006187 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006188 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006189 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006190 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006191 di->di_flags = 0;
6192 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006193 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006194}
6195
6196/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006197 * Make a copy of a Dictionary item.
6198 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006199 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006200dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006201 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006202{
Bram Moolenaar33570922005-01-25 22:26:29 +00006203 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006204
Bram Moolenaar33570922005-01-25 22:26:29 +00006205 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006206 if (di != NULL)
6207 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006208 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006209 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006210 copy_tv(&org->di_tv, &di->di_tv);
6211 }
6212 return di;
6213}
6214
6215/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006216 * Remove item "item" from Dictionary "dict" and free it.
6217 */
6218 static void
6219dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006220 dict_T *dict;
6221 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006222{
Bram Moolenaar33570922005-01-25 22:26:29 +00006223 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006224
Bram Moolenaar33570922005-01-25 22:26:29 +00006225 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006226 if (HASHITEM_EMPTY(hi))
6227 EMSG2(_(e_intern2), "dictitem_remove()");
6228 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006229 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006230 dictitem_free(item);
6231}
6232
6233/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006234 * Free a dict item. Also clears the value.
6235 */
6236 static void
6237dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006238 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006239{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006240 clear_tv(&item->di_tv);
6241 vim_free(item);
6242}
6243
6244/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006245 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6246 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006247 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006248 * Returns NULL when out of memory.
6249 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006250 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006251dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006253 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006254 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006255{
Bram Moolenaar33570922005-01-25 22:26:29 +00006256 dict_T *copy;
6257 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006258 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006260
6261 if (orig == NULL)
6262 return NULL;
6263
6264 copy = dict_alloc();
6265 if (copy != NULL)
6266 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006267 if (copyID != 0)
6268 {
6269 orig->dv_copyID = copyID;
6270 orig->dv_copydict = copy;
6271 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006272 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006273 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006274 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006275 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006276 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006277 --todo;
6278
6279 di = dictitem_alloc(hi->hi_key);
6280 if (di == NULL)
6281 break;
6282 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006283 {
6284 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6285 copyID) == FAIL)
6286 {
6287 vim_free(di);
6288 break;
6289 }
6290 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006291 else
6292 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6293 if (dict_add(copy, di) == FAIL)
6294 {
6295 dictitem_free(di);
6296 break;
6297 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006298 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006299 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006300
Bram Moolenaare9a41262005-01-15 22:18:47 +00006301 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006302 if (todo > 0)
6303 {
6304 dict_unref(copy);
6305 copy = NULL;
6306 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006307 }
6308
6309 return copy;
6310}
6311
6312/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006313 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006314 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006315 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006316 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006317dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006318 dict_T *d;
6319 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006320{
Bram Moolenaar33570922005-01-25 22:26:29 +00006321 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006322}
6323
Bram Moolenaar8c711452005-01-14 21:53:12 +00006324/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006325 * Add a number or string entry to dictionary "d".
6326 * When "str" is NULL use number "nr", otherwise use "str".
6327 * Returns FAIL when out of memory and when key already exists.
6328 */
6329 int
6330dict_add_nr_str(d, key, nr, str)
6331 dict_T *d;
6332 char *key;
6333 long nr;
6334 char_u *str;
6335{
6336 dictitem_T *item;
6337
6338 item = dictitem_alloc((char_u *)key);
6339 if (item == NULL)
6340 return FAIL;
6341 item->di_tv.v_lock = 0;
6342 if (str == NULL)
6343 {
6344 item->di_tv.v_type = VAR_NUMBER;
6345 item->di_tv.vval.v_number = nr;
6346 }
6347 else
6348 {
6349 item->di_tv.v_type = VAR_STRING;
6350 item->di_tv.vval.v_string = vim_strsave(str);
6351 }
6352 if (dict_add(d, item) == FAIL)
6353 {
6354 dictitem_free(item);
6355 return FAIL;
6356 }
6357 return OK;
6358}
6359
6360/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006361 * Get the number of items in a Dictionary.
6362 */
6363 static long
6364dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006365 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006366{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006367 if (d == NULL)
6368 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006369 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006370}
6371
6372/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006373 * Find item "key[len]" in Dictionary "d".
6374 * If "len" is negative use strlen(key).
6375 * Returns NULL when not found.
6376 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006378dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006380 char_u *key;
6381 int len;
6382{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006383#define AKEYLEN 200
6384 char_u buf[AKEYLEN];
6385 char_u *akey;
6386 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006387 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006388
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006389 if (len < 0)
6390 akey = key;
6391 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006392 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006393 tofree = akey = vim_strnsave(key, len);
6394 if (akey == NULL)
6395 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006396 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006397 else
6398 {
6399 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006400 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006401 akey = buf;
6402 }
6403
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006405 vim_free(tofree);
6406 if (HASHITEM_EMPTY(hi))
6407 return NULL;
6408 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006409}
6410
6411/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006412 * Get a string item from a dictionary.
6413 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006414 * Returns NULL if the entry doesn't exist or out of memory.
6415 */
6416 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006417get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006418 dict_T *d;
6419 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006420 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006421{
6422 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006423 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006424
6425 di = dict_find(d, key, -1);
6426 if (di == NULL)
6427 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006428 s = get_tv_string(&di->di_tv);
6429 if (save && s != NULL)
6430 s = vim_strsave(s);
6431 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006432}
6433
6434/*
6435 * Get a number item from a dictionary.
6436 * Returns 0 if the entry doesn't exist or out of memory.
6437 */
6438 long
6439get_dict_number(d, key)
6440 dict_T *d;
6441 char_u *key;
6442{
6443 dictitem_T *di;
6444
6445 di = dict_find(d, key, -1);
6446 if (di == NULL)
6447 return 0;
6448 return get_tv_number(&di->di_tv);
6449}
6450
6451/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006452 * Return an allocated string with the string representation of a Dictionary.
6453 * May return NULL.
6454 */
6455 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006456dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006457 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006458 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006459{
6460 garray_T ga;
6461 int first = TRUE;
6462 char_u *tofree;
6463 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006464 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006465 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006466 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006467 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006468
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006469 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006470 return NULL;
6471 ga_init2(&ga, (int)sizeof(char), 80);
6472 ga_append(&ga, '{');
6473
Bram Moolenaar33570922005-01-25 22:26:29 +00006474 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006475 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006476 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006477 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006478 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006479 --todo;
6480
6481 if (first)
6482 first = FALSE;
6483 else
6484 ga_concat(&ga, (char_u *)", ");
6485
6486 tofree = string_quote(hi->hi_key, FALSE);
6487 if (tofree != NULL)
6488 {
6489 ga_concat(&ga, tofree);
6490 vim_free(tofree);
6491 }
6492 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006493 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006494 if (s != NULL)
6495 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006496 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006497 if (s == NULL)
6498 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006500 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006501 if (todo > 0)
6502 {
6503 vim_free(ga.ga_data);
6504 return NULL;
6505 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506
6507 ga_append(&ga, '}');
6508 ga_append(&ga, NUL);
6509 return (char_u *)ga.ga_data;
6510}
6511
6512/*
6513 * Allocate a variable for a Dictionary and fill it from "*arg".
6514 * Return OK or FAIL. Returns NOTDONE for {expr}.
6515 */
6516 static int
6517get_dict_tv(arg, rettv, evaluate)
6518 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006519 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006520 int evaluate;
6521{
Bram Moolenaar33570922005-01-25 22:26:29 +00006522 dict_T *d = NULL;
6523 typval_T tvkey;
6524 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006525 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006526 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006527 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006528 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006529
6530 /*
6531 * First check if it's not a curly-braces thing: {expr}.
6532 * Must do this without evaluating, otherwise a function may be called
6533 * twice. Unfortunately this means we need to call eval1() twice for the
6534 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006535 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006536 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006537 if (*start != '}')
6538 {
6539 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6540 return FAIL;
6541 if (*start == '}')
6542 return NOTDONE;
6543 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006544
6545 if (evaluate)
6546 {
6547 d = dict_alloc();
6548 if (d == NULL)
6549 return FAIL;
6550 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006551 tvkey.v_type = VAR_UNKNOWN;
6552 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006553
6554 *arg = skipwhite(*arg + 1);
6555 while (**arg != '}' && **arg != NUL)
6556 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006557 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006558 goto failret;
6559 if (**arg != ':')
6560 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006561 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006562 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006563 goto failret;
6564 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006565 key = get_tv_string_buf_chk(&tvkey, buf);
6566 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006567 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006568 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6569 if (key != NULL)
6570 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006571 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006572 goto failret;
6573 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006574
6575 *arg = skipwhite(*arg + 1);
6576 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6577 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006578 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006579 goto failret;
6580 }
6581 if (evaluate)
6582 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006583 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006584 if (item != NULL)
6585 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006586 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006587 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006588 clear_tv(&tv);
6589 goto failret;
6590 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006591 item = dictitem_alloc(key);
6592 clear_tv(&tvkey);
6593 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006594 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006595 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006596 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006597 if (dict_add(d, item) == FAIL)
6598 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006599 }
6600 }
6601
6602 if (**arg == '}')
6603 break;
6604 if (**arg != ',')
6605 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006606 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006607 goto failret;
6608 }
6609 *arg = skipwhite(*arg + 1);
6610 }
6611
6612 if (**arg != '}')
6613 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006614 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006615failret:
6616 if (evaluate)
6617 dict_free(d);
6618 return FAIL;
6619 }
6620
6621 *arg = skipwhite(*arg + 1);
6622 if (evaluate)
6623 {
6624 rettv->v_type = VAR_DICT;
6625 rettv->vval.v_dict = d;
6626 ++d->dv_refcount;
6627 }
6628
6629 return OK;
6630}
6631
Bram Moolenaar8c711452005-01-14 21:53:12 +00006632/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633 * Return a string with the string representation of a variable.
6634 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006635 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006636 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006637 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 * May return NULL;
6639 */
6640 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006641echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006642 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006645 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006647 static int recurse = 0;
6648 char_u *r = NULL;
6649
Bram Moolenaar33570922005-01-25 22:26:29 +00006650 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006651 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006652 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006653 *tofree = NULL;
6654 return NULL;
6655 }
6656 ++recurse;
6657
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006658 switch (tv->v_type)
6659 {
6660 case VAR_FUNC:
6661 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006662 r = tv->vval.v_string;
6663 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006664
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006665 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006666 if (tv->vval.v_list == NULL)
6667 {
6668 *tofree = NULL;
6669 r = NULL;
6670 }
6671 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6672 {
6673 *tofree = NULL;
6674 r = (char_u *)"[...]";
6675 }
6676 else
6677 {
6678 tv->vval.v_list->lv_copyID = copyID;
6679 *tofree = list2string(tv, copyID);
6680 r = *tofree;
6681 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006682 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006683
Bram Moolenaar8c711452005-01-14 21:53:12 +00006684 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006685 if (tv->vval.v_dict == NULL)
6686 {
6687 *tofree = NULL;
6688 r = NULL;
6689 }
6690 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6691 {
6692 *tofree = NULL;
6693 r = (char_u *)"{...}";
6694 }
6695 else
6696 {
6697 tv->vval.v_dict->dv_copyID = copyID;
6698 *tofree = dict2string(tv, copyID);
6699 r = *tofree;
6700 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006701 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006702
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006703 case VAR_STRING:
6704 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006705 *tofree = NULL;
6706 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006707 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006708
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006709 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006710 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006711 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006712 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006713
6714 --recurse;
6715 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006716}
6717
6718/*
6719 * Return a string with the string representation of a variable.
6720 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6721 * "numbuf" is used for a number.
6722 * Puts quotes around strings, so that they can be parsed back by eval().
6723 * May return NULL;
6724 */
6725 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006726tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006727 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006728 char_u **tofree;
6729 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006730 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006731{
6732 switch (tv->v_type)
6733 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006734 case VAR_FUNC:
6735 *tofree = string_quote(tv->vval.v_string, TRUE);
6736 return *tofree;
6737 case VAR_STRING:
6738 *tofree = string_quote(tv->vval.v_string, FALSE);
6739 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006740 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006741 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006742 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006743 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006744 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006745 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006746 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006747 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006748}
6749
6750/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006751 * Return string "str" in ' quotes, doubling ' characters.
6752 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006753 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006754 */
6755 static char_u *
6756string_quote(str, function)
6757 char_u *str;
6758 int function;
6759{
Bram Moolenaar33570922005-01-25 22:26:29 +00006760 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006761 char_u *p, *r, *s;
6762
Bram Moolenaar33570922005-01-25 22:26:29 +00006763 len = (function ? 13 : 3);
6764 if (str != NULL)
6765 {
6766 len += STRLEN(str);
6767 for (p = str; *p != NUL; mb_ptr_adv(p))
6768 if (*p == '\'')
6769 ++len;
6770 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006771 s = r = alloc(len);
6772 if (r != NULL)
6773 {
6774 if (function)
6775 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006776 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006777 r += 10;
6778 }
6779 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006780 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006781 if (str != NULL)
6782 for (p = str; *p != NUL; )
6783 {
6784 if (*p == '\'')
6785 *r++ = '\'';
6786 MB_COPY_CHAR(p, r);
6787 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006788 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006789 if (function)
6790 *r++ = ')';
6791 *r++ = NUL;
6792 }
6793 return s;
6794}
6795
6796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 * Get the value of an environment variable.
6798 * "arg" is pointing to the '$'. It is advanced to after the name.
6799 * If the environment variable was not set, silently assume it is empty.
6800 * Always return OK.
6801 */
6802 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006803get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 int evaluate;
6807{
6808 char_u *string = NULL;
6809 int len;
6810 int cc;
6811 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006812 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813
6814 ++*arg;
6815 name = *arg;
6816 len = get_env_len(arg);
6817 if (evaluate)
6818 {
6819 if (len != 0)
6820 {
6821 cc = name[len];
6822 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006823 /* first try vim_getenv(), fast for normal environment vars */
6824 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006826 {
6827 if (!mustfree)
6828 string = vim_strsave(string);
6829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 else
6831 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006832 if (mustfree)
6833 vim_free(string);
6834
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 /* next try expanding things like $VIM and ${HOME} */
6836 string = expand_env_save(name - 1);
6837 if (string != NULL && *string == '$')
6838 {
6839 vim_free(string);
6840 string = NULL;
6841 }
6842 }
6843 name[len] = cc;
6844 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006845 rettv->v_type = VAR_STRING;
6846 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 }
6848
6849 return OK;
6850}
6851
6852/*
6853 * Array with names and number of arguments of all internal functions
6854 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6855 */
6856static struct fst
6857{
6858 char *f_name; /* function name */
6859 char f_min_argc; /* minimal number of arguments */
6860 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006861 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006862 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863} functions[] =
6864{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006865 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {"append", 2, 2, f_append},
6867 {"argc", 0, 0, f_argc},
6868 {"argidx", 0, 0, f_argidx},
6869 {"argv", 1, 1, f_argv},
6870 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006871 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {"bufexists", 1, 1, f_bufexists},
6873 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6874 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6875 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6876 {"buflisted", 1, 1, f_buflisted},
6877 {"bufloaded", 1, 1, f_bufloaded},
6878 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006879 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {"bufwinnr", 1, 1, f_bufwinnr},
6881 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006882 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006883 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {"char2nr", 1, 1, f_char2nr},
6885 {"cindent", 1, 1, f_cindent},
6886 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006887#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00006888 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006889 {"complete_add", 1, 1, f_complete_add},
6890 {"complete_check", 0, 0, f_complete_check},
6891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006893 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006894 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00006896 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006897 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 {"delete", 1, 1, f_delete},
6899 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006900 {"diff_filler", 1, 1, f_diff_filler},
6901 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006902 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006904 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 {"eventhandler", 0, 0, f_eventhandler},
6906 {"executable", 1, 1, f_executable},
6907 {"exists", 1, 1, f_exists},
6908 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006909 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6911 {"filereadable", 1, 1, f_filereadable},
6912 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006913 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006914 {"finddir", 1, 3, f_finddir},
6915 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 {"fnamemodify", 2, 2, f_fnamemodify},
6917 {"foldclosed", 1, 1, f_foldclosed},
6918 {"foldclosedend", 1, 1, f_foldclosedend},
6919 {"foldlevel", 1, 1, f_foldlevel},
6920 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006921 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006923 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006925 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006926 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 {"getbufvar", 2, 2, f_getbufvar},
6928 {"getchar", 0, 1, f_getchar},
6929 {"getcharmod", 0, 0, f_getcharmod},
6930 {"getcmdline", 0, 0, f_getcmdline},
6931 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006932 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006934 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006935 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {"getfsize", 1, 1, f_getfsize},
6937 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006938 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006939 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006940 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00006941 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006942 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006943 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 {"getregtype", 0, 1, f_getregtype},
6945 {"getwinposx", 0, 0, f_getwinposx},
6946 {"getwinposy", 0, 0, f_getwinposy},
6947 {"getwinvar", 2, 2, f_getwinvar},
6948 {"glob", 1, 1, f_glob},
6949 {"globpath", 2, 2, f_globpath},
6950 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006951 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 {"hasmapto", 1, 2, f_hasmapto},
6953 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6954 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6955 {"histadd", 2, 2, f_histadd},
6956 {"histdel", 1, 2, f_histdel},
6957 {"histget", 1, 2, f_histget},
6958 {"histnr", 1, 1, f_histnr},
6959 {"hlID", 1, 1, f_hlID},
6960 {"hlexists", 1, 1, f_hlexists},
6961 {"hostname", 0, 0, f_hostname},
6962 {"iconv", 3, 3, f_iconv},
6963 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006964 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006965 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006967 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 {"inputrestore", 0, 0, f_inputrestore},
6969 {"inputsave", 0, 0, f_inputsave},
6970 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006971 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006973 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006974 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006975 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006976 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006978 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {"libcall", 3, 3, f_libcall},
6980 {"libcallnr", 3, 3, f_libcallnr},
6981 {"line", 1, 1, f_line},
6982 {"line2byte", 1, 1, f_line2byte},
6983 {"lispindent", 1, 1, f_lispindent},
6984 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006985 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 {"maparg", 1, 2, f_maparg},
6987 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006988 {"match", 2, 4, f_match},
6989 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006990 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006991 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006992 {"max", 1, 1, f_max},
6993 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006994#ifdef vim_mkdir
6995 {"mkdir", 1, 3, f_mkdir},
6996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {"mode", 0, 0, f_mode},
6998 {"nextnonblank", 1, 1, f_nextnonblank},
6999 {"nr2char", 1, 1, f_nr2char},
7000 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007001 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007002 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007003 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007004 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 {"remote_expr", 2, 3, f_remote_expr},
7006 {"remote_foreground", 1, 1, f_remote_foreground},
7007 {"remote_peek", 1, 2, f_remote_peek},
7008 {"remote_read", 1, 1, f_remote_read},
7009 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007010 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007012 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007014 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007015 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007016 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007017 {"searchpair", 3, 6, f_searchpair},
7018 {"searchpairpos", 3, 6, f_searchpairpos},
7019 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 {"server2client", 2, 2, f_server2client},
7021 {"serverlist", 0, 0, f_serverlist},
7022 {"setbufvar", 3, 3, f_setbufvar},
7023 {"setcmdpos", 1, 1, f_setcmdpos},
7024 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007025 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007026 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007027 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 {"setreg", 2, 3, f_setreg},
7029 {"setwinvar", 3, 3, f_setwinvar},
7030 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007031 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007032 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007033 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007034 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007035 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036#ifdef HAVE_STRFTIME
7037 {"strftime", 1, 2, f_strftime},
7038#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007039 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007040 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 {"strlen", 1, 1, f_strlen},
7042 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007043 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {"strtrans", 1, 1, f_strtrans},
7045 {"submatch", 1, 1, f_submatch},
7046 {"substitute", 4, 4, f_substitute},
7047 {"synID", 3, 3, f_synID},
7048 {"synIDattr", 2, 3, f_synIDattr},
7049 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007050 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007051 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007052 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007053 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007054 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007055 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007057 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {"tolower", 1, 1, f_tolower},
7059 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007060 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007062 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {"virtcol", 1, 1, f_virtcol},
7064 {"visualmode", 0, 1, f_visualmode},
7065 {"winbufnr", 1, 1, f_winbufnr},
7066 {"wincol", 0, 0, f_wincol},
7067 {"winheight", 1, 1, f_winheight},
7068 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007069 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007071 {"winrestview", 1, 1, f_winrestview},
7072 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007074 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075};
7076
7077#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7078
7079/*
7080 * Function given to ExpandGeneric() to obtain the list of internal
7081 * or user defined function names.
7082 */
7083 char_u *
7084get_function_name(xp, idx)
7085 expand_T *xp;
7086 int idx;
7087{
7088 static int intidx = -1;
7089 char_u *name;
7090
7091 if (idx == 0)
7092 intidx = -1;
7093 if (intidx < 0)
7094 {
7095 name = get_user_func_name(xp, idx);
7096 if (name != NULL)
7097 return name;
7098 }
7099 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7100 {
7101 STRCPY(IObuff, functions[intidx].f_name);
7102 STRCAT(IObuff, "(");
7103 if (functions[intidx].f_max_argc == 0)
7104 STRCAT(IObuff, ")");
7105 return IObuff;
7106 }
7107
7108 return NULL;
7109}
7110
7111/*
7112 * Function given to ExpandGeneric() to obtain the list of internal or
7113 * user defined variable or function names.
7114 */
7115/*ARGSUSED*/
7116 char_u *
7117get_expr_name(xp, idx)
7118 expand_T *xp;
7119 int idx;
7120{
7121 static int intidx = -1;
7122 char_u *name;
7123
7124 if (idx == 0)
7125 intidx = -1;
7126 if (intidx < 0)
7127 {
7128 name = get_function_name(xp, idx);
7129 if (name != NULL)
7130 return name;
7131 }
7132 return get_user_var_name(xp, ++intidx);
7133}
7134
7135#endif /* FEAT_CMDL_COMPL */
7136
7137/*
7138 * Find internal function in table above.
7139 * Return index, or -1 if not found
7140 */
7141 static int
7142find_internal_func(name)
7143 char_u *name; /* name of the function */
7144{
7145 int first = 0;
7146 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7147 int cmp;
7148 int x;
7149
7150 /*
7151 * Find the function name in the table. Binary search.
7152 */
7153 while (first <= last)
7154 {
7155 x = first + ((unsigned)(last - first) >> 1);
7156 cmp = STRCMP(name, functions[x].f_name);
7157 if (cmp < 0)
7158 last = x - 1;
7159 else if (cmp > 0)
7160 first = x + 1;
7161 else
7162 return x;
7163 }
7164 return -1;
7165}
7166
7167/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007168 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7169 * name it contains, otherwise return "name".
7170 */
7171 static char_u *
7172deref_func_name(name, lenp)
7173 char_u *name;
7174 int *lenp;
7175{
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007177 int cc;
7178
7179 cc = name[*lenp];
7180 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007181 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007182 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007184 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007185 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007186 {
7187 *lenp = 0;
7188 return (char_u *)""; /* just in case */
7189 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 *lenp = STRLEN(v->di_tv.vval.v_string);
7191 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007192 }
7193
7194 return name;
7195}
7196
7197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 * Allocate a variable for the result of a function.
7199 * Return OK or FAIL.
7200 */
7201 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007202get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7203 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 char_u *name; /* name of the function */
7205 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 char_u **arg; /* argument, pointing to the '(' */
7208 linenr_T firstline; /* first line of range */
7209 linenr_T lastline; /* last line of range */
7210 int *doesrange; /* return: function handled range */
7211 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007212 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213{
7214 char_u *argp;
7215 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007216 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 int argcount = 0; /* number of arguments found */
7218
7219 /*
7220 * Get the arguments.
7221 */
7222 argp = *arg;
7223 while (argcount < MAX_FUNC_ARGS)
7224 {
7225 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7226 if (*argp == ')' || *argp == ',' || *argp == NUL)
7227 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7229 {
7230 ret = FAIL;
7231 break;
7232 }
7233 ++argcount;
7234 if (*argp != ',')
7235 break;
7236 }
7237 if (*argp == ')')
7238 ++argp;
7239 else
7240 ret = FAIL;
7241
7242 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007243 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007244 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007246 {
7247 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007248 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007250 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252
7253 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007254 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255
7256 *arg = skipwhite(argp);
7257 return ret;
7258}
7259
7260
7261/*
7262 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007263 * Return OK when the function can't be called, FAIL otherwise.
7264 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 */
7266 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007267call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007268 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 char_u *name; /* name of the function */
7270 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007271 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007273 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 linenr_T firstline; /* first line of range */
7275 linenr_T lastline; /* last line of range */
7276 int *doesrange; /* return: function handled range */
7277 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007278 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279{
7280 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281#define ERROR_UNKNOWN 0
7282#define ERROR_TOOMANY 1
7283#define ERROR_TOOFEW 2
7284#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007285#define ERROR_DICT 4
7286#define ERROR_NONE 5
7287#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 int error = ERROR_NONE;
7289 int i;
7290 int llen;
7291 ufunc_T *fp;
7292 int cc;
7293#define FLEN_FIXED 40
7294 char_u fname_buf[FLEN_FIXED + 1];
7295 char_u *fname;
7296
7297 /*
7298 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7299 * Change <SNR>123_name() to K_SNR 123_name().
7300 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7301 */
7302 cc = name[len];
7303 name[len] = NUL;
7304 llen = eval_fname_script(name);
7305 if (llen > 0)
7306 {
7307 fname_buf[0] = K_SPECIAL;
7308 fname_buf[1] = KS_EXTRA;
7309 fname_buf[2] = (int)KE_SNR;
7310 i = 3;
7311 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7312 {
7313 if (current_SID <= 0)
7314 error = ERROR_SCRIPT;
7315 else
7316 {
7317 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7318 i = (int)STRLEN(fname_buf);
7319 }
7320 }
7321 if (i + STRLEN(name + llen) < FLEN_FIXED)
7322 {
7323 STRCPY(fname_buf + i, name + llen);
7324 fname = fname_buf;
7325 }
7326 else
7327 {
7328 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7329 if (fname == NULL)
7330 error = ERROR_OTHER;
7331 else
7332 {
7333 mch_memmove(fname, fname_buf, (size_t)i);
7334 STRCPY(fname + i, name + llen);
7335 }
7336 }
7337 }
7338 else
7339 fname = name;
7340
7341 *doesrange = FALSE;
7342
7343
7344 /* execute the function if no errors detected and executing */
7345 if (evaluate && error == ERROR_NONE)
7346 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007347 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 error = ERROR_UNKNOWN;
7349
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007350 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 {
7352 /*
7353 * User defined function.
7354 */
7355 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007356
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007358 /* Trigger FuncUndefined event, may load the function. */
7359 if (fp == NULL
7360 && apply_autocmds(EVENT_FUNCUNDEFINED,
7361 fname, fname, TRUE, NULL)
7362 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007364 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 fp = find_func(fname);
7366 }
7367#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007368 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007369 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007370 {
7371 /* loaded a package, search for the function again */
7372 fp = find_func(fname);
7373 }
7374
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 if (fp != NULL)
7376 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007377 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007379 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007381 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007383 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007384 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 else
7386 {
7387 /*
7388 * Call the user function.
7389 * Save and restore search patterns, script variables and
7390 * redo buffer.
7391 */
7392 save_search_patterns();
7393 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007394 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007395 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007397 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7398 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7399 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007400 /* Function was unreferenced while being used, free it
7401 * now. */
7402 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 restoreRedobuff();
7404 restore_search_patterns();
7405 error = ERROR_NONE;
7406 }
7407 }
7408 }
7409 else
7410 {
7411 /*
7412 * Find the function name in the table, call its implementation.
7413 */
7414 i = find_internal_func(fname);
7415 if (i >= 0)
7416 {
7417 if (argcount < functions[i].f_min_argc)
7418 error = ERROR_TOOFEW;
7419 else if (argcount > functions[i].f_max_argc)
7420 error = ERROR_TOOMANY;
7421 else
7422 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007423 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007424 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 error = ERROR_NONE;
7426 }
7427 }
7428 }
7429 /*
7430 * The function call (or "FuncUndefined" autocommand sequence) might
7431 * have been aborted by an error, an interrupt, or an explicitly thrown
7432 * exception that has not been caught so far. This situation can be
7433 * tested for by calling aborting(). For an error in an internal
7434 * function or for the "E132" error in call_user_func(), however, the
7435 * throw point at which the "force_abort" flag (temporarily reset by
7436 * emsg()) is normally updated has not been reached yet. We need to
7437 * update that flag first to make aborting() reliable.
7438 */
7439 update_force_abort();
7440 }
7441 if (error == ERROR_NONE)
7442 ret = OK;
7443
7444 /*
7445 * Report an error unless the argument evaluation or function call has been
7446 * cancelled due to an aborting error, an interrupt, or an exception.
7447 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448 if (!aborting())
7449 {
7450 switch (error)
7451 {
7452 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007453 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454 break;
7455 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007456 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007457 break;
7458 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007459 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460 name);
7461 break;
7462 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007463 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464 name);
7465 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007466 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007467 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 name);
7469 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470 }
7471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472
7473 name[len] = cc;
7474 if (fname != name && fname != fname_buf)
7475 vim_free(fname);
7476
7477 return ret;
7478}
7479
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007480/*
7481 * Give an error message with a function name. Handle <SNR> things.
7482 */
7483 static void
7484emsg_funcname(msg, name)
7485 char *msg;
7486 char_u *name;
7487{
7488 char_u *p;
7489
7490 if (*name == K_SPECIAL)
7491 p = concat_str((char_u *)"<SNR>", name + 3);
7492 else
7493 p = name;
7494 EMSG2(_(msg), p);
7495 if (p != name)
7496 vim_free(p);
7497}
7498
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499/*********************************************
7500 * Implementation of the built-in functions
7501 */
7502
7503/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007504 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 */
7506 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007507f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007508 typval_T *argvars;
7509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510{
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007513 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007514 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007516 if ((l = argvars[0].vval.v_list) != NULL
7517 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7518 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007519 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007520 }
7521 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007522 EMSG(_(e_listreq));
7523}
7524
7525/*
7526 * "append(lnum, string/list)" function
7527 */
7528 static void
7529f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007530 typval_T *argvars;
7531 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007532{
7533 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007534 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007535 list_T *l = NULL;
7536 listitem_T *li = NULL;
7537 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007538 long added = 0;
7539
Bram Moolenaar0d660222005-01-07 21:51:51 +00007540 lnum = get_tv_lnum(argvars);
7541 if (lnum >= 0
7542 && lnum <= curbuf->b_ml.ml_line_count
7543 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007544 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007545 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007546 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007547 l = argvars[1].vval.v_list;
7548 if (l == NULL)
7549 return;
7550 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007551 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007552 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007553 for (;;)
7554 {
7555 if (l == NULL)
7556 tv = &argvars[1]; /* append a string */
7557 else if (li == NULL)
7558 break; /* end of list */
7559 else
7560 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007561 line = get_tv_string_chk(tv);
7562 if (line == NULL) /* type error */
7563 {
7564 rettv->vval.v_number = 1; /* Failed */
7565 break;
7566 }
7567 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007568 ++added;
7569 if (l == NULL)
7570 break;
7571 li = li->li_next;
7572 }
7573
7574 appended_lines_mark(lnum, added);
7575 if (curwin->w_cursor.lnum > lnum)
7576 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007578 else
7579 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580}
7581
7582/*
7583 * "argc()" function
7584 */
7585/* ARGSUSED */
7586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007587f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 typval_T *argvars;
7589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007591 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592}
7593
7594/*
7595 * "argidx()" function
7596 */
7597/* ARGSUSED */
7598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007599f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007600 typval_T *argvars;
7601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007603 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604}
7605
7606/*
7607 * "argv(nr)" function
7608 */
7609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007610f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007611 typval_T *argvars;
7612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613{
7614 int idx;
7615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007616 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620 rettv->vval.v_string = NULL;
7621 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622}
7623
7624/*
7625 * "browse(save, title, initdir, default)" function
7626 */
7627/* ARGSUSED */
7628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007629f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 typval_T *argvars;
7631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632{
7633#ifdef FEAT_BROWSE
7634 int save;
7635 char_u *title;
7636 char_u *initdir;
7637 char_u *defname;
7638 char_u buf[NUMBUFLEN];
7639 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007640 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007642 save = get_tv_number_chk(&argvars[0], &error);
7643 title = get_tv_string_chk(&argvars[1]);
7644 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7645 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007647 if (error || title == NULL || initdir == NULL || defname == NULL)
7648 rettv->vval.v_string = NULL;
7649 else
7650 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007651 do_browse(save ? BROWSE_SAVE : 0,
7652 title, defname, NULL, initdir, NULL, curbuf);
7653#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007654 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007655#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007656 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007657}
7658
7659/*
7660 * "browsedir(title, initdir)" function
7661 */
7662/* ARGSUSED */
7663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007664f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007665 typval_T *argvars;
7666 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007667{
7668#ifdef FEAT_BROWSE
7669 char_u *title;
7670 char_u *initdir;
7671 char_u buf[NUMBUFLEN];
7672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007673 title = get_tv_string_chk(&argvars[0]);
7674 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007676 if (title == NULL || initdir == NULL)
7677 rettv->vval.v_string = NULL;
7678 else
7679 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007680 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007682 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007684 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685}
7686
Bram Moolenaar33570922005-01-25 22:26:29 +00007687static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007688
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689/*
7690 * Find a buffer by number or exact name.
7691 */
7692 static buf_T *
7693find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
7696 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007698 if (avar->v_type == VAR_NUMBER)
7699 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007700 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007702 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007703 if (buf == NULL)
7704 {
7705 /* No full path name match, try a match with a URL or a "nofile"
7706 * buffer, these don't use the full path. */
7707 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7708 if (buf->b_fname != NULL
7709 && (path_with_url(buf->b_fname)
7710#ifdef FEAT_QUICKFIX
7711 || bt_nofile(buf)
7712#endif
7713 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007714 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007715 break;
7716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 }
7718 return buf;
7719}
7720
7721/*
7722 * "bufexists(expr)" function
7723 */
7724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007725f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007726 typval_T *argvars;
7727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007729 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730}
7731
7732/*
7733 * "buflisted(expr)" function
7734 */
7735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007736f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 typval_T *argvars;
7738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
7740 buf_T *buf;
7741
7742 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007743 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744}
7745
7746/*
7747 * "bufloaded(expr)" function
7748 */
7749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007750f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 typval_T *argvars;
7752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
7754 buf_T *buf;
7755
7756 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007757 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758}
7759
Bram Moolenaar33570922005-01-25 22:26:29 +00007760static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007761
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762/*
7763 * Get buffer by number or pattern.
7764 */
7765 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007766get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007767 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007769 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 int save_magic;
7771 char_u *save_cpo;
7772 buf_T *buf;
7773
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774 if (tv->v_type == VAR_NUMBER)
7775 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007776 if (tv->v_type != VAR_STRING)
7777 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 if (name == NULL || *name == NUL)
7779 return curbuf;
7780 if (name[0] == '$' && name[1] == NUL)
7781 return lastbuf;
7782
7783 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7784 save_magic = p_magic;
7785 p_magic = TRUE;
7786 save_cpo = p_cpo;
7787 p_cpo = (char_u *)"";
7788
7789 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7790 TRUE, FALSE));
7791
7792 p_magic = save_magic;
7793 p_cpo = save_cpo;
7794
7795 /* If not found, try expanding the name, like done for bufexists(). */
7796 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007797 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798
7799 return buf;
7800}
7801
7802/*
7803 * "bufname(expr)" function
7804 */
7805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007806f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007807 typval_T *argvars;
7808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809{
7810 buf_T *buf;
7811
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007812 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007814 buf = get_buf_tv(&argvars[0]);
7815 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007817 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007819 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 --emsg_off;
7821}
7822
7823/*
7824 * "bufnr(expr)" function
7825 */
7826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007827f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007828 typval_T *argvars;
7829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830{
7831 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007832 int error = FALSE;
7833 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007835 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007837 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007838 --emsg_off;
7839
7840 /* If the buffer isn't found and the second argument is not zero create a
7841 * new buffer. */
7842 if (buf == NULL
7843 && argvars[1].v_type != VAR_UNKNOWN
7844 && get_tv_number_chk(&argvars[1], &error) != 0
7845 && !error
7846 && (name = get_tv_string_chk(&argvars[0])) != NULL
7847 && !error)
7848 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7849
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007851 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007853 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854}
7855
7856/*
7857 * "bufwinnr(nr)" function
7858 */
7859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007860f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007861 typval_T *argvars;
7862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863{
7864#ifdef FEAT_WINDOWS
7865 win_T *wp;
7866 int winnr = 0;
7867#endif
7868 buf_T *buf;
7869
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007870 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007872 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873#ifdef FEAT_WINDOWS
7874 for (wp = firstwin; wp; wp = wp->w_next)
7875 {
7876 ++winnr;
7877 if (wp->w_buffer == buf)
7878 break;
7879 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007880 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007882 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883#endif
7884 --emsg_off;
7885}
7886
7887/*
7888 * "byte2line(byte)" function
7889 */
7890/*ARGSUSED*/
7891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007892f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007893 typval_T *argvars;
7894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
7896#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007897 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898#else
7899 long boff = 0;
7900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007901 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007903 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007905 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 (linenr_T)0, &boff);
7907#endif
7908}
7909
7910/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007911 * "byteidx()" function
7912 */
7913/*ARGSUSED*/
7914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007915f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007916 typval_T *argvars;
7917 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007918{
7919#ifdef FEAT_MBYTE
7920 char_u *t;
7921#endif
7922 char_u *str;
7923 long idx;
7924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007925 str = get_tv_string_chk(&argvars[0]);
7926 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007928 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007929 return;
7930
7931#ifdef FEAT_MBYTE
7932 t = str;
7933 for ( ; idx > 0; idx--)
7934 {
7935 if (*t == NUL) /* EOL reached */
7936 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007937 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007939 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007940#else
7941 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007942 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007943#endif
7944}
7945
7946/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007947 * "call(func, arglist)" function
7948 */
7949 static void
7950f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007951 typval_T *argvars;
7952 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007953{
7954 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007955 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007956 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007957 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007958 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007959 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007960
7961 rettv->vval.v_number = 0;
7962 if (argvars[1].v_type != VAR_LIST)
7963 {
7964 EMSG(_(e_listreq));
7965 return;
7966 }
7967 if (argvars[1].vval.v_list == NULL)
7968 return;
7969
7970 if (argvars[0].v_type == VAR_FUNC)
7971 func = argvars[0].vval.v_string;
7972 else
7973 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007974 if (*func == NUL)
7975 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007976
Bram Moolenaare9a41262005-01-15 22:18:47 +00007977 if (argvars[2].v_type != VAR_UNKNOWN)
7978 {
7979 if (argvars[2].v_type != VAR_DICT)
7980 {
7981 EMSG(_(e_dictreq));
7982 return;
7983 }
7984 selfdict = argvars[2].vval.v_dict;
7985 }
7986
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007987 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7988 item = item->li_next)
7989 {
7990 if (argc == MAX_FUNC_ARGS)
7991 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007992 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007993 break;
7994 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007995 /* Make a copy of each argument. This is needed to be able to set
7996 * v_lock to VAR_FIXED in the copy without changing the original list.
7997 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007998 copy_tv(&item->li_tv, &argv[argc++]);
7999 }
8000
8001 if (item == NULL)
8002 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008003 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8004 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008005
8006 /* Free the arguments. */
8007 while (argc > 0)
8008 clear_tv(&argv[--argc]);
8009}
8010
8011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 * "char2nr(string)" function
8013 */
8014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008015f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008016 typval_T *argvars;
8017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018{
8019#ifdef FEAT_MBYTE
8020 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008021 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 else
8023#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008024 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025}
8026
8027/*
8028 * "cindent(lnum)" function
8029 */
8030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008031f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008032 typval_T *argvars;
8033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034{
8035#ifdef FEAT_CINDENT
8036 pos_T pos;
8037 linenr_T lnum;
8038
8039 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008040 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8042 {
8043 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008044 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 curwin->w_cursor = pos;
8046 }
8047 else
8048#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008049 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050}
8051
8052/*
8053 * "col(string)" function
8054 */
8055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008056f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008057 typval_T *argvars;
8058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
8060 colnr_T col = 0;
8061 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008062 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008064 fp = var2fpos(&argvars[0], FALSE, &fnum);
8065 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {
8067 if (fp->col == MAXCOL)
8068 {
8069 /* '> can be MAXCOL, get the length of the line then */
8070 if (fp->lnum <= curbuf->b_ml.ml_line_count)
8071 col = STRLEN(ml_get(fp->lnum)) + 1;
8072 else
8073 col = MAXCOL;
8074 }
8075 else
8076 {
8077 col = fp->col + 1;
8078#ifdef FEAT_VIRTUALEDIT
8079 /* col(".") when the cursor is on the NUL at the end of the line
8080 * because of "coladd" can be seen as an extra column. */
8081 if (virtual_active() && fp == &curwin->w_cursor)
8082 {
8083 char_u *p = ml_get_cursor();
8084
8085 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8086 curwin->w_virtcol - curwin->w_cursor.coladd))
8087 {
8088# ifdef FEAT_MBYTE
8089 int l;
8090
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008091 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 col += l;
8093# else
8094 if (*p != NUL && p[1] == NUL)
8095 ++col;
8096# endif
8097 }
8098 }
8099#endif
8100 }
8101 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008102 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103}
8104
Bram Moolenaar572cb562005-08-05 21:35:02 +00008105#if defined(FEAT_INS_EXPAND)
8106/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008107 * "complete()" function
8108 */
8109/*ARGSUSED*/
8110 static void
8111f_complete(argvars, rettv)
8112 typval_T *argvars;
8113 typval_T *rettv;
8114{
8115 int startcol;
8116
8117 if ((State & INSERT) == 0)
8118 {
8119 EMSG(_("E785: complete() can only be used in Insert mode"));
8120 return;
8121 }
8122 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8123 {
8124 EMSG(_(e_invarg));
8125 return;
8126 }
8127
8128 startcol = get_tv_number_chk(&argvars[0], NULL);
8129 if (startcol <= 0)
8130 return;
8131
8132 set_completion(startcol - 1, argvars[1].vval.v_list);
8133}
8134
8135/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008136 * "complete_add()" function
8137 */
8138/*ARGSUSED*/
8139 static void
8140f_complete_add(argvars, rettv)
8141 typval_T *argvars;
8142 typval_T *rettv;
8143{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008144 char_u *word;
8145 char_u *extra = NULL;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008146 int icase = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008147
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008148 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8149 {
8150 word = get_dict_string(argvars[0].vval.v_dict,
8151 (char_u *)"word", FALSE);
8152 extra = get_dict_string(argvars[0].vval.v_dict,
8153 (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008154 icase = get_dict_number(argvars[0].vval.v_dict, (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008155 }
8156 else
8157 word = get_tv_string_chk(&argvars[0]);
8158 if (word != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008159 rettv->vval.v_number = ins_compl_add(word, -1, icase,
8160 NULL, extra, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008161}
8162
8163/*
8164 * "complete_check()" function
8165 */
8166/*ARGSUSED*/
8167 static void
8168f_complete_check(argvars, rettv)
8169 typval_T *argvars;
8170 typval_T *rettv;
8171{
8172 int saved = RedrawingDisabled;
8173
8174 RedrawingDisabled = 0;
8175 ins_compl_check_keys(0);
8176 rettv->vval.v_number = compl_interrupted;
8177 RedrawingDisabled = saved;
8178}
8179#endif
8180
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181/*
8182 * "confirm(message, buttons[, default [, type]])" function
8183 */
8184/*ARGSUSED*/
8185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008186f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008187 typval_T *argvars;
8188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189{
8190#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8191 char_u *message;
8192 char_u *buttons = NULL;
8193 char_u buf[NUMBUFLEN];
8194 char_u buf2[NUMBUFLEN];
8195 int def = 1;
8196 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008197 char_u *typestr;
8198 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008200 message = get_tv_string_chk(&argvars[0]);
8201 if (message == NULL)
8202 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008203 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008205 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8206 if (buttons == NULL)
8207 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008208 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008210 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008211 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008213 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8214 if (typestr == NULL)
8215 error = TRUE;
8216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008218 switch (TOUPPER_ASC(*typestr))
8219 {
8220 case 'E': type = VIM_ERROR; break;
8221 case 'Q': type = VIM_QUESTION; break;
8222 case 'I': type = VIM_INFO; break;
8223 case 'W': type = VIM_WARNING; break;
8224 case 'G': type = VIM_GENERIC; break;
8225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 }
8227 }
8228 }
8229 }
8230
8231 if (buttons == NULL || *buttons == NUL)
8232 buttons = (char_u *)_("&Ok");
8233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008234 if (error)
8235 rettv->vval.v_number = 0;
8236 else
8237 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 def, NULL);
8239#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008240 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241#endif
8242}
8243
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008244/*
8245 * "copy()" function
8246 */
8247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008248f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008249 typval_T *argvars;
8250 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008251{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008252 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008253}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254
8255/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008256 * "count()" function
8257 */
8258 static void
8259f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008260 typval_T *argvars;
8261 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008262{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008263 long n = 0;
8264 int ic = FALSE;
8265
Bram Moolenaare9a41262005-01-15 22:18:47 +00008266 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008267 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008268 listitem_T *li;
8269 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008270 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008271
Bram Moolenaare9a41262005-01-15 22:18:47 +00008272 if ((l = argvars[0].vval.v_list) != NULL)
8273 {
8274 li = l->lv_first;
8275 if (argvars[2].v_type != VAR_UNKNOWN)
8276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008277 int error = FALSE;
8278
8279 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008280 if (argvars[3].v_type != VAR_UNKNOWN)
8281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008282 idx = get_tv_number_chk(&argvars[3], &error);
8283 if (!error)
8284 {
8285 li = list_find(l, idx);
8286 if (li == NULL)
8287 EMSGN(_(e_listidx), idx);
8288 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008289 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008290 if (error)
8291 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008292 }
8293
8294 for ( ; li != NULL; li = li->li_next)
8295 if (tv_equal(&li->li_tv, &argvars[1], ic))
8296 ++n;
8297 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008298 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008299 else if (argvars[0].v_type == VAR_DICT)
8300 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008301 int todo;
8302 dict_T *d;
8303 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008304
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008305 if ((d = argvars[0].vval.v_dict) != NULL)
8306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008307 int error = FALSE;
8308
Bram Moolenaare9a41262005-01-15 22:18:47 +00008309 if (argvars[2].v_type != VAR_UNKNOWN)
8310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008311 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008312 if (argvars[3].v_type != VAR_UNKNOWN)
8313 EMSG(_(e_invarg));
8314 }
8315
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008316 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008317 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008318 {
8319 if (!HASHITEM_EMPTY(hi))
8320 {
8321 --todo;
8322 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8323 ++n;
8324 }
8325 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008326 }
8327 }
8328 else
8329 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008330 rettv->vval.v_number = n;
8331}
8332
8333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8335 *
8336 * Checks the existence of a cscope connection.
8337 */
8338/*ARGSUSED*/
8339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008340f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008341 typval_T *argvars;
8342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343{
8344#ifdef FEAT_CSCOPE
8345 int num = 0;
8346 char_u *dbpath = NULL;
8347 char_u *prepend = NULL;
8348 char_u buf[NUMBUFLEN];
8349
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008350 if (argvars[0].v_type != VAR_UNKNOWN
8351 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008353 num = (int)get_tv_number(&argvars[0]);
8354 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008355 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008356 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 }
8358
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008359 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008361 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362#endif
8363}
8364
8365/*
8366 * "cursor(lnum, col)" function
8367 *
8368 * Moves the cursor to the specified line and column
8369 */
8370/*ARGSUSED*/
8371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008372f_cursor(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 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008377#ifdef FEAT_VIRTUALEDIT
8378 long coladd = 0;
8379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380
Bram Moolenaara5525202006-03-02 22:52:09 +00008381 if (argvars[1].v_type == VAR_UNKNOWN)
8382 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008383 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008384
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008385 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008386 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008387 line = pos.lnum;
8388 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008389#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008390 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008391#endif
8392 }
8393 else
8394 {
8395 line = get_tv_lnum(argvars);
8396 col = get_tv_number_chk(&argvars[1], NULL);
8397#ifdef FEAT_VIRTUALEDIT
8398 if (argvars[2].v_type != VAR_UNKNOWN)
8399 coladd = get_tv_number_chk(&argvars[2], NULL);
8400#endif
8401 }
8402 if (line < 0 || col < 0
8403#ifdef FEAT_VIRTUALEDIT
8404 || coladd < 0
8405#endif
8406 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008407 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 if (line > 0)
8409 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 if (col > 0)
8411 curwin->w_cursor.col = col - 1;
8412#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008413 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414#endif
8415
8416 /* Make sure the cursor is in a valid position. */
8417 check_cursor();
8418#ifdef FEAT_MBYTE
8419 /* Correct cursor for multi-byte character. */
8420 if (has_mbyte)
8421 mb_adjust_cursor();
8422#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008423
8424 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425}
8426
8427/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008428 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 */
8430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008431f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008432 typval_T *argvars;
8433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008435 int noref = 0;
8436
8437 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008438 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008439 if (noref < 0 || noref > 1)
8440 EMSG(_(e_invarg));
8441 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008442 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443}
8444
8445/*
8446 * "delete()" function
8447 */
8448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008449f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008450 typval_T *argvars;
8451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
8453 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008454 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008456 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457}
8458
8459/*
8460 * "did_filetype()" function
8461 */
8462/*ARGSUSED*/
8463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008464f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008465 typval_T *argvars;
8466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467{
8468#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008469 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008471 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472#endif
8473}
8474
8475/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008476 * "diff_filler()" function
8477 */
8478/*ARGSUSED*/
8479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008480f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008481 typval_T *argvars;
8482 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008483{
8484#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008485 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008486#endif
8487}
8488
8489/*
8490 * "diff_hlID()" function
8491 */
8492/*ARGSUSED*/
8493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008494f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008495 typval_T *argvars;
8496 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008497{
8498#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008499 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008500 static linenr_T prev_lnum = 0;
8501 static int changedtick = 0;
8502 static int fnum = 0;
8503 static int change_start = 0;
8504 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008505 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008506 int filler_lines;
8507 int col;
8508
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008509 if (lnum < 0) /* ignore type error in {lnum} arg */
8510 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008511 if (lnum != prev_lnum
8512 || changedtick != curbuf->b_changedtick
8513 || fnum != curbuf->b_fnum)
8514 {
8515 /* New line, buffer, change: need to get the values. */
8516 filler_lines = diff_check(curwin, lnum);
8517 if (filler_lines < 0)
8518 {
8519 if (filler_lines == -1)
8520 {
8521 change_start = MAXCOL;
8522 change_end = -1;
8523 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8524 hlID = HLF_ADD; /* added line */
8525 else
8526 hlID = HLF_CHD; /* changed line */
8527 }
8528 else
8529 hlID = HLF_ADD; /* added line */
8530 }
8531 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008532 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008533 prev_lnum = lnum;
8534 changedtick = curbuf->b_changedtick;
8535 fnum = curbuf->b_fnum;
8536 }
8537
8538 if (hlID == HLF_CHD || hlID == HLF_TXD)
8539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008540 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008541 if (col >= change_start && col <= change_end)
8542 hlID = HLF_TXD; /* changed text */
8543 else
8544 hlID = HLF_CHD; /* changed line */
8545 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008546 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008547#endif
8548}
8549
8550/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008551 * "empty({expr})" function
8552 */
8553 static void
8554f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008555 typval_T *argvars;
8556 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008557{
8558 int n;
8559
8560 switch (argvars[0].v_type)
8561 {
8562 case VAR_STRING:
8563 case VAR_FUNC:
8564 n = argvars[0].vval.v_string == NULL
8565 || *argvars[0].vval.v_string == NUL;
8566 break;
8567 case VAR_NUMBER:
8568 n = argvars[0].vval.v_number == 0;
8569 break;
8570 case VAR_LIST:
8571 n = argvars[0].vval.v_list == NULL
8572 || argvars[0].vval.v_list->lv_first == NULL;
8573 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008574 case VAR_DICT:
8575 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008576 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008577 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008578 default:
8579 EMSG2(_(e_intern2), "f_empty()");
8580 n = 0;
8581 }
8582
8583 rettv->vval.v_number = n;
8584}
8585
8586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 * "escape({string}, {chars})" function
8588 */
8589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008591 typval_T *argvars;
8592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594 char_u buf[NUMBUFLEN];
8595
Bram Moolenaar758711c2005-02-02 23:11:38 +00008596 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8597 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599}
8600
8601/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008602 * "eval()" function
8603 */
8604/*ARGSUSED*/
8605 static void
8606f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008607 typval_T *argvars;
8608 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008609{
8610 char_u *s;
8611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008612 s = get_tv_string_chk(&argvars[0]);
8613 if (s != NULL)
8614 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008616 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8617 {
8618 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008619 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008620 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008621 else if (*s != NUL)
8622 EMSG(_(e_trailing));
8623}
8624
8625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 * "eventhandler()" function
8627 */
8628/*ARGSUSED*/
8629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008630f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008631 typval_T *argvars;
8632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008634 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635}
8636
8637/*
8638 * "executable()" function
8639 */
8640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008641f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008642 typval_T *argvars;
8643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008645 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646}
8647
8648/*
8649 * "exists()" function
8650 */
8651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008652f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008653 typval_T *argvars;
8654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655{
8656 char_u *p;
8657 char_u *name;
8658 int n = FALSE;
8659 int len = 0;
8660
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008661 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 if (*p == '$') /* environment variable */
8663 {
8664 /* first try "normal" environment variables (fast) */
8665 if (mch_getenv(p + 1) != NULL)
8666 n = TRUE;
8667 else
8668 {
8669 /* try expanding things like $VIM and ${HOME} */
8670 p = expand_env_save(p);
8671 if (p != NULL && *p != '$')
8672 n = TRUE;
8673 vim_free(p);
8674 }
8675 }
8676 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008677 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 else if (*p == '*') /* internal or user defined function */
8679 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008680 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 }
8682 else if (*p == ':')
8683 {
8684 n = cmd_exists(p + 1);
8685 }
8686 else if (*p == '#')
8687 {
8688#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008689 if (p[1] == '#')
8690 n = autocmd_supported(p + 2);
8691 else
8692 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693#endif
8694 }
8695 else /* internal variable */
8696 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008697 char_u *tofree;
8698 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008700 /* get_name_len() takes care of expanding curly braces */
8701 name = p;
8702 len = get_name_len(&p, &tofree, TRUE, FALSE);
8703 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008705 if (tofree != NULL)
8706 name = tofree;
8707 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8708 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008710 /* handle d.key, l[idx], f(expr) */
8711 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8712 if (n)
8713 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 }
8715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008717 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 }
8719
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008720 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721}
8722
8723/*
8724 * "expand()" function
8725 */
8726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008727f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008728 typval_T *argvars;
8729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
8731 char_u *s;
8732 int len;
8733 char_u *errormsg;
8734 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8735 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008736 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008738 rettv->v_type = VAR_STRING;
8739 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 if (*s == '%' || *s == '#' || *s == '<')
8741 {
8742 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008743 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 --emsg_off;
8745 }
8746 else
8747 {
8748 /* When the optional second argument is non-zero, don't remove matches
8749 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008750 if (argvars[1].v_type != VAR_UNKNOWN
8751 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008753 if (!error)
8754 {
8755 ExpandInit(&xpc);
8756 xpc.xp_context = EXPAND_FILES;
8757 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8758 ExpandCleanup(&xpc);
8759 }
8760 else
8761 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 }
8763}
8764
8765/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008766 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008767 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008768 */
8769 static void
8770f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *argvars;
8772 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008773{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008774 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008775 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008776 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008777 list_T *l1, *l2;
8778 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008779 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008780 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008781
Bram Moolenaare9a41262005-01-15 22:18:47 +00008782 l1 = argvars[0].vval.v_list;
8783 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008784 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8785 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008786 {
8787 if (argvars[2].v_type != VAR_UNKNOWN)
8788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008789 before = get_tv_number_chk(&argvars[2], &error);
8790 if (error)
8791 return; /* type error; errmsg already given */
8792
Bram Moolenaar758711c2005-02-02 23:11:38 +00008793 if (before == l1->lv_len)
8794 item = NULL;
8795 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008796 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008797 item = list_find(l1, before);
8798 if (item == NULL)
8799 {
8800 EMSGN(_(e_listidx), before);
8801 return;
8802 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008803 }
8804 }
8805 else
8806 item = NULL;
8807 list_extend(l1, l2, item);
8808
Bram Moolenaare9a41262005-01-15 22:18:47 +00008809 copy_tv(&argvars[0], rettv);
8810 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008811 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008812 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8813 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008814 dict_T *d1, *d2;
8815 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008816 char_u *action;
8817 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008818 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008819 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008820
8821 d1 = argvars[0].vval.v_dict;
8822 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008823 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8824 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008825 {
8826 /* Check the third argument. */
8827 if (argvars[2].v_type != VAR_UNKNOWN)
8828 {
8829 static char *(av[]) = {"keep", "force", "error"};
8830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008831 action = get_tv_string_chk(&argvars[2]);
8832 if (action == NULL)
8833 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008834 for (i = 0; i < 3; ++i)
8835 if (STRCMP(action, av[i]) == 0)
8836 break;
8837 if (i == 3)
8838 {
8839 EMSGN(_(e_invarg2), action);
8840 return;
8841 }
8842 }
8843 else
8844 action = (char_u *)"force";
8845
8846 /* Go over all entries in the second dict and add them to the
8847 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008848 todo = d2->dv_hashtab.ht_used;
8849 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008850 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008851 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008852 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008853 --todo;
8854 di1 = dict_find(d1, hi2->hi_key, -1);
8855 if (di1 == NULL)
8856 {
8857 di1 = dictitem_copy(HI2DI(hi2));
8858 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8859 dictitem_free(di1);
8860 }
8861 else if (*action == 'e')
8862 {
8863 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8864 break;
8865 }
8866 else if (*action == 'f')
8867 {
8868 clear_tv(&di1->di_tv);
8869 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8870 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008871 }
8872 }
8873
Bram Moolenaare9a41262005-01-15 22:18:47 +00008874 copy_tv(&argvars[0], rettv);
8875 }
8876 }
8877 else
8878 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008879}
8880
8881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 * "filereadable()" function
8883 */
8884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008885f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008886 typval_T *argvars;
8887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 FILE *fd;
8890 char_u *p;
8891 int n;
8892
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008893 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8895 {
8896 n = TRUE;
8897 fclose(fd);
8898 }
8899 else
8900 n = FALSE;
8901
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903}
8904
8905/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008906 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 * rights to write into.
8908 */
8909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008910f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008911 typval_T *argvars;
8912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008914 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915}
8916
Bram Moolenaar33570922005-01-25 22:26:29 +00008917static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008918
8919 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008920findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008921 typval_T *argvars;
8922 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008923 int dir;
8924{
8925#ifdef FEAT_SEARCHPATH
8926 char_u *fname;
8927 char_u *fresult = NULL;
8928 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8929 char_u *p;
8930 char_u pathbuf[NUMBUFLEN];
8931 int count = 1;
8932 int first = TRUE;
8933
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008935
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008936 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008937 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008938 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8939 if (p == NULL)
8940 count = -1; /* error */
8941 else
8942 {
8943 if (*p != NUL)
8944 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008946 if (argvars[2].v_type != VAR_UNKNOWN)
8947 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8948 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008949 }
8950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008951 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008952 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008953 do
8954 {
8955 vim_free(fresult);
8956 fresult = find_file_in_path_option(first ? fname : NULL,
8957 first ? (int)STRLEN(fname) : 0,
8958 0, first, path, dir, NULL);
8959 first = FALSE;
8960 } while (--count > 0 && fresult != NULL);
8961 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008962
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008963 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008964#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008965 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008966#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008968}
8969
Bram Moolenaar33570922005-01-25 22:26:29 +00008970static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8971static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008972
8973/*
8974 * Implementation of map() and filter().
8975 */
8976 static void
8977filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 typval_T *argvars;
8979 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008980 int map;
8981{
8982 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008983 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008984 listitem_T *li, *nli;
8985 list_T *l = NULL;
8986 dictitem_T *di;
8987 hashtab_T *ht;
8988 hashitem_T *hi;
8989 dict_T *d = NULL;
8990 typval_T save_val;
8991 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008992 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008993 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008994 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00008995 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008996
8997 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008998 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008999 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009000 if ((l = argvars[0].vval.v_list) == NULL
9001 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009002 return;
9003 }
9004 else if (argvars[0].v_type == VAR_DICT)
9005 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009006 if ((d = argvars[0].vval.v_dict) == NULL
9007 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009008 return;
9009 }
9010 else
9011 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009012 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009013 return;
9014 }
9015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009016 expr = get_tv_string_buf_chk(&argvars[1], buf);
9017 /* On type errors, the preceding call has already displayed an error
9018 * message. Avoid a misleading error message for an empty string that
9019 * was not passed as argument. */
9020 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009021 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009022 prepare_vimvar(VV_VAL, &save_val);
9023 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009024
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009025 /* We reset "did_emsg" to be able to detect whether an error
9026 * occurred during evaluation of the expression. */
9027 save_did_emsg = did_emsg;
9028 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009030 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009032 prepare_vimvar(VV_KEY, &save_key);
9033 vimvars[VV_KEY].vv_type = VAR_STRING;
9034
9035 ht = &d->dv_hashtab;
9036 hash_lock(ht);
9037 todo = ht->ht_used;
9038 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009039 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009040 if (!HASHITEM_EMPTY(hi))
9041 {
9042 --todo;
9043 di = HI2DI(hi);
9044 if (tv_check_lock(di->di_tv.v_lock, msg))
9045 break;
9046 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009047 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009048 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009049 break;
9050 if (!map && rem)
9051 dictitem_remove(d, di);
9052 clear_tv(&vimvars[VV_KEY].vv_tv);
9053 }
9054 }
9055 hash_unlock(ht);
9056
9057 restore_vimvar(VV_KEY, &save_key);
9058 }
9059 else
9060 {
9061 for (li = l->lv_first; li != NULL; li = nli)
9062 {
9063 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009064 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009065 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009066 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009067 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009068 break;
9069 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009070 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009071 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009072 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009074 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009075
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009076 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009077 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009078
9079 copy_tv(&argvars[0], rettv);
9080}
9081
9082 static int
9083filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009084 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009085 char_u *expr;
9086 int map;
9087 int *remp;
9088{
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009090 char_u *s;
9091
Bram Moolenaar33570922005-01-25 22:26:29 +00009092 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009093 s = expr;
9094 if (eval1(&s, &rettv, TRUE) == FAIL)
9095 return FAIL;
9096 if (*s != NUL) /* check for trailing chars after expr */
9097 {
9098 EMSG2(_(e_invexpr2), s);
9099 return FAIL;
9100 }
9101 if (map)
9102 {
9103 /* map(): replace the list item value */
9104 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009105 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009106 *tv = rettv;
9107 }
9108 else
9109 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009110 int error = FALSE;
9111
Bram Moolenaare9a41262005-01-15 22:18:47 +00009112 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009113 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009114 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009115 /* On type error, nothing has been removed; return FAIL to stop the
9116 * loop. The error message was given by get_tv_number_chk(). */
9117 if (error)
9118 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009119 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009120 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009121 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009122}
9123
9124/*
9125 * "filter()" function
9126 */
9127 static void
9128f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009129 typval_T *argvars;
9130 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009131{
9132 filter_map(argvars, rettv, FALSE);
9133}
9134
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009135/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009136 * "finddir({fname}[, {path}[, {count}]])" function
9137 */
9138 static void
9139f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009140 typval_T *argvars;
9141 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009142{
9143 findfilendir(argvars, rettv, TRUE);
9144}
9145
9146/*
9147 * "findfile({fname}[, {path}[, {count}]])" function
9148 */
9149 static void
9150f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009151 typval_T *argvars;
9152 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009153{
9154 findfilendir(argvars, rettv, FALSE);
9155}
9156
9157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 * "fnamemodify({fname}, {mods})" function
9159 */
9160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009162 typval_T *argvars;
9163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
9165 char_u *fname;
9166 char_u *mods;
9167 int usedlen = 0;
9168 int len;
9169 char_u *fbuf = NULL;
9170 char_u buf[NUMBUFLEN];
9171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009172 fname = get_tv_string_chk(&argvars[0]);
9173 mods = get_tv_string_buf_chk(&argvars[1], buf);
9174 if (fname == NULL || mods == NULL)
9175 fname = NULL;
9176 else
9177 {
9178 len = (int)STRLEN(fname);
9179 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 vim_free(fbuf);
9188}
9189
Bram Moolenaar33570922005-01-25 22:26:29 +00009190static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191
9192/*
9193 * "foldclosed()" function
9194 */
9195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009197 typval_T *argvars;
9198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 int end;
9200{
9201#ifdef FEAT_FOLDING
9202 linenr_T lnum;
9203 linenr_T first, last;
9204
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009205 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9207 {
9208 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9209 {
9210 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009211 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009213 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 return;
9215 }
9216 }
9217#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009218 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219}
9220
9221/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009222 * "foldclosed()" function
9223 */
9224 static void
9225f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009226 typval_T *argvars;
9227 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009228{
9229 foldclosed_both(argvars, rettv, FALSE);
9230}
9231
9232/*
9233 * "foldclosedend()" function
9234 */
9235 static void
9236f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009237 typval_T *argvars;
9238 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009239{
9240 foldclosed_both(argvars, rettv, TRUE);
9241}
9242
9243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 * "foldlevel()" function
9245 */
9246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009247f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009248 typval_T *argvars;
9249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250{
9251#ifdef FEAT_FOLDING
9252 linenr_T lnum;
9253
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009254 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009256 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 else
9258#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009259 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260}
9261
9262/*
9263 * "foldtext()" function
9264 */
9265/*ARGSUSED*/
9266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009267f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009268 typval_T *argvars;
9269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270{
9271#ifdef FEAT_FOLDING
9272 linenr_T lnum;
9273 char_u *s;
9274 char_u *r;
9275 int len;
9276 char *txt;
9277#endif
9278
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009279 rettv->v_type = VAR_STRING;
9280 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009282 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9283 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9284 <= curbuf->b_ml.ml_line_count
9285 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 {
9287 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009288 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9289 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 {
9291 if (!linewhite(lnum))
9292 break;
9293 ++lnum;
9294 }
9295
9296 /* Find interesting text in this line. */
9297 s = skipwhite(ml_get(lnum));
9298 /* skip C comment-start */
9299 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009300 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009302 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009303 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009304 {
9305 s = skipwhite(ml_get(lnum + 1));
9306 if (*s == '*')
9307 s = skipwhite(s + 1);
9308 }
9309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 txt = _("+-%s%3ld lines: ");
9311 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009312 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 + 20 /* for %3ld */
9314 + STRLEN(s))); /* concatenated */
9315 if (r != NULL)
9316 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009317 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9318 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9319 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 len = (int)STRLEN(r);
9321 STRCAT(r, s);
9322 /* remove 'foldmarker' and 'commentstring' */
9323 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009324 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 }
9326 }
9327#endif
9328}
9329
9330/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009331 * "foldtextresult(lnum)" function
9332 */
9333/*ARGSUSED*/
9334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009335f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009336 typval_T *argvars;
9337 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009338{
9339#ifdef FEAT_FOLDING
9340 linenr_T lnum;
9341 char_u *text;
9342 char_u buf[51];
9343 foldinfo_T foldinfo;
9344 int fold_count;
9345#endif
9346
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009347 rettv->v_type = VAR_STRING;
9348 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009349#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009351 /* treat illegal types and illegal string values for {lnum} the same */
9352 if (lnum < 0)
9353 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009354 fold_count = foldedCount(curwin, lnum, &foldinfo);
9355 if (fold_count > 0)
9356 {
9357 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9358 &foldinfo, buf);
9359 if (text == buf)
9360 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009362 }
9363#endif
9364}
9365
9366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 * "foreground()" function
9368 */
9369/*ARGSUSED*/
9370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009372 typval_T *argvars;
9373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376#ifdef FEAT_GUI
9377 if (gui.in_use)
9378 gui_mch_set_foreground();
9379#else
9380# ifdef WIN32
9381 win32_set_foreground();
9382# endif
9383#endif
9384}
9385
9386/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009387 * "function()" function
9388 */
9389/*ARGSUSED*/
9390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009391f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009392 typval_T *argvars;
9393 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009394{
9395 char_u *s;
9396
Bram Moolenaara7043832005-01-21 11:56:39 +00009397 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009399 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009400 EMSG2(_(e_invarg2), s);
9401 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009402 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009403 else
9404 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009405 rettv->vval.v_string = vim_strsave(s);
9406 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009407 }
9408}
9409
9410/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009411 * "garbagecollect()" function
9412 */
9413/*ARGSUSED*/
9414 static void
9415f_garbagecollect(argvars, rettv)
9416 typval_T *argvars;
9417 typval_T *rettv;
9418{
9419 garbage_collect();
9420}
9421
9422/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009423 * "get()" function
9424 */
9425 static void
9426f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009427 typval_T *argvars;
9428 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009429{
Bram Moolenaar33570922005-01-25 22:26:29 +00009430 listitem_T *li;
9431 list_T *l;
9432 dictitem_T *di;
9433 dict_T *d;
9434 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009435
Bram Moolenaare9a41262005-01-15 22:18:47 +00009436 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009437 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009438 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009440 int error = FALSE;
9441
9442 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9443 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009444 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009445 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009446 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009447 else if (argvars[0].v_type == VAR_DICT)
9448 {
9449 if ((d = argvars[0].vval.v_dict) != NULL)
9450 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009451 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009452 if (di != NULL)
9453 tv = &di->di_tv;
9454 }
9455 }
9456 else
9457 EMSG2(_(e_listdictarg), "get()");
9458
9459 if (tv == NULL)
9460 {
9461 if (argvars[2].v_type == VAR_UNKNOWN)
9462 rettv->vval.v_number = 0;
9463 else
9464 copy_tv(&argvars[2], rettv);
9465 }
9466 else
9467 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009468}
9469
Bram Moolenaar342337a2005-07-21 21:11:17 +00009470static 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 +00009471
9472/*
9473 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009474 * Return a range (from start to end) of lines in rettv from the specified
9475 * buffer.
9476 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009477 */
9478 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009479get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009480 buf_T *buf;
9481 linenr_T start;
9482 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009483 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009484 typval_T *rettv;
9485{
9486 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009487
Bram Moolenaar342337a2005-07-21 21:11:17 +00009488 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009489 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009490 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009491 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009492 }
9493 else
9494 rettv->vval.v_number = 0;
9495
9496 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9497 return;
9498
9499 if (!retlist)
9500 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009501 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9502 p = ml_get_buf(buf, start, FALSE);
9503 else
9504 p = (char_u *)"";
9505
9506 rettv->v_type = VAR_STRING;
9507 rettv->vval.v_string = vim_strsave(p);
9508 }
9509 else
9510 {
9511 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009512 return;
9513
9514 if (start < 1)
9515 start = 1;
9516 if (end > buf->b_ml.ml_line_count)
9517 end = buf->b_ml.ml_line_count;
9518 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009519 if (list_append_string(rettv->vval.v_list,
9520 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009521 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009522 }
9523}
9524
9525/*
9526 * "getbufline()" function
9527 */
9528 static void
9529f_getbufline(argvars, rettv)
9530 typval_T *argvars;
9531 typval_T *rettv;
9532{
9533 linenr_T lnum;
9534 linenr_T end;
9535 buf_T *buf;
9536
9537 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9538 ++emsg_off;
9539 buf = get_buf_tv(&argvars[0]);
9540 --emsg_off;
9541
Bram Moolenaar661b1822005-07-28 22:36:45 +00009542 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009543 if (argvars[2].v_type == VAR_UNKNOWN)
9544 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009545 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009546 end = get_tv_lnum_buf(&argvars[2], buf);
9547
Bram Moolenaar342337a2005-07-21 21:11:17 +00009548 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009549}
9550
Bram Moolenaar0d660222005-01-07 21:51:51 +00009551/*
9552 * "getbufvar()" function
9553 */
9554 static void
9555f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009556 typval_T *argvars;
9557 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009558{
9559 buf_T *buf;
9560 buf_T *save_curbuf;
9561 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009562 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009564 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9565 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009566 ++emsg_off;
9567 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009568
9569 rettv->v_type = VAR_STRING;
9570 rettv->vval.v_string = NULL;
9571
9572 if (buf != NULL && varname != NULL)
9573 {
9574 if (*varname == '&') /* buffer-local-option */
9575 {
9576 /* set curbuf to be our buf, temporarily */
9577 save_curbuf = curbuf;
9578 curbuf = buf;
9579
9580 get_option_tv(&varname, rettv, TRUE);
9581
9582 /* restore previous notion of curbuf */
9583 curbuf = save_curbuf;
9584 }
9585 else
9586 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009587 if (*varname == NUL)
9588 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9589 * scope prefix before the NUL byte is required by
9590 * find_var_in_ht(). */
9591 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009592 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009593 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009594 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009595 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009596 }
9597 }
9598
9599 --emsg_off;
9600}
9601
9602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 * "getchar()" function
9604 */
9605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009606f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009607 typval_T *argvars;
9608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609{
9610 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009611 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612
9613 ++no_mapping;
9614 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009615 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 /* getchar(): blocking wait. */
9617 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009618 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 /* getchar(1): only check if char avail */
9620 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009621 else if (error || vpeekc() == NUL)
9622 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 n = 0;
9624 else
9625 /* getchar(0) and char avail: return char */
9626 n = safe_vgetc();
9627 --no_mapping;
9628 --allow_keys;
9629
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 if (IS_SPECIAL(n) || mod_mask != 0)
9632 {
9633 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9634 int i = 0;
9635
9636 /* Turn a special key into three bytes, plus modifier. */
9637 if (mod_mask != 0)
9638 {
9639 temp[i++] = K_SPECIAL;
9640 temp[i++] = KS_MODIFIER;
9641 temp[i++] = mod_mask;
9642 }
9643 if (IS_SPECIAL(n))
9644 {
9645 temp[i++] = K_SPECIAL;
9646 temp[i++] = K_SECOND(n);
9647 temp[i++] = K_THIRD(n);
9648 }
9649#ifdef FEAT_MBYTE
9650 else if (has_mbyte)
9651 i += (*mb_char2bytes)(n, temp + i);
9652#endif
9653 else
9654 temp[i++] = n;
9655 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009656 rettv->v_type = VAR_STRING;
9657 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 }
9659}
9660
9661/*
9662 * "getcharmod()" function
9663 */
9664/*ARGSUSED*/
9665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009666f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009667 typval_T *argvars;
9668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671}
9672
9673/*
9674 * "getcmdline()" function
9675 */
9676/*ARGSUSED*/
9677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009679 typval_T *argvars;
9680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009682 rettv->v_type = VAR_STRING;
9683 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684}
9685
9686/*
9687 * "getcmdpos()" function
9688 */
9689/*ARGSUSED*/
9690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009692 typval_T *argvars;
9693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696}
9697
9698/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009699 * "getcmdtype()" function
9700 */
9701/*ARGSUSED*/
9702 static void
9703f_getcmdtype(argvars, rettv)
9704 typval_T *argvars;
9705 typval_T *rettv;
9706{
9707 rettv->v_type = VAR_STRING;
9708 rettv->vval.v_string = alloc(2);
9709 if (rettv->vval.v_string != NULL)
9710 {
9711 rettv->vval.v_string[0] = get_cmdline_type();
9712 rettv->vval.v_string[1] = NUL;
9713 }
9714}
9715
9716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 * "getcwd()" function
9718 */
9719/*ARGSUSED*/
9720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009722 typval_T *argvars;
9723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724{
9725 char_u cwd[MAXPATHL];
9726
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 else
9731 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009732 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009734 if (rettv->vval.v_string != NULL)
9735 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736#endif
9737 }
9738}
9739
9740/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009741 * "getfontname()" function
9742 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009743/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009746 typval_T *argvars;
9747 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009748{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009749 rettv->v_type = VAR_STRING;
9750 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009751#ifdef FEAT_GUI
9752 if (gui.in_use)
9753 {
9754 GuiFont font;
9755 char_u *name = NULL;
9756
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009757 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009758 {
9759 /* Get the "Normal" font. Either the name saved by
9760 * hl_set_font_name() or from the font ID. */
9761 font = gui.norm_font;
9762 name = hl_get_font_name();
9763 }
9764 else
9765 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009767 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9768 return;
9769 font = gui_mch_get_font(name, FALSE);
9770 if (font == NOFONT)
9771 return; /* Invalid font name, return empty string. */
9772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009773 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009774 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009775 gui_mch_free_font(font);
9776 }
9777#endif
9778}
9779
9780/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009781 * "getfperm({fname})" function
9782 */
9783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009785 typval_T *argvars;
9786 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009787{
9788 char_u *fname;
9789 struct stat st;
9790 char_u *perm = NULL;
9791 char_u flags[] = "rwx";
9792 int i;
9793
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009795
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009797 if (mch_stat((char *)fname, &st) >= 0)
9798 {
9799 perm = vim_strsave((char_u *)"---------");
9800 if (perm != NULL)
9801 {
9802 for (i = 0; i < 9; i++)
9803 {
9804 if (st.st_mode & (1 << (8 - i)))
9805 perm[i] = flags[i % 3];
9806 }
9807 }
9808 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009810}
9811
9812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 * "getfsize({fname})" function
9814 */
9815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009816f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009817 typval_T *argvars;
9818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819{
9820 char_u *fname;
9821 struct stat st;
9822
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009823 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826
9827 if (mch_stat((char *)fname, &st) >= 0)
9828 {
9829 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 }
9834 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836}
9837
9838/*
9839 * "getftime({fname})" function
9840 */
9841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009843 typval_T *argvars;
9844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845{
9846 char_u *fname;
9847 struct stat st;
9848
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850
9851 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009852 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009854 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855}
9856
9857/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009858 * "getftype({fname})" function
9859 */
9860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009861f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009862 typval_T *argvars;
9863 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009864{
9865 char_u *fname;
9866 struct stat st;
9867 char_u *type = NULL;
9868 char *t;
9869
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009871
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009872 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009873 if (mch_lstat((char *)fname, &st) >= 0)
9874 {
9875#ifdef S_ISREG
9876 if (S_ISREG(st.st_mode))
9877 t = "file";
9878 else if (S_ISDIR(st.st_mode))
9879 t = "dir";
9880# ifdef S_ISLNK
9881 else if (S_ISLNK(st.st_mode))
9882 t = "link";
9883# endif
9884# ifdef S_ISBLK
9885 else if (S_ISBLK(st.st_mode))
9886 t = "bdev";
9887# endif
9888# ifdef S_ISCHR
9889 else if (S_ISCHR(st.st_mode))
9890 t = "cdev";
9891# endif
9892# ifdef S_ISFIFO
9893 else if (S_ISFIFO(st.st_mode))
9894 t = "fifo";
9895# endif
9896# ifdef S_ISSOCK
9897 else if (S_ISSOCK(st.st_mode))
9898 t = "fifo";
9899# endif
9900 else
9901 t = "other";
9902#else
9903# ifdef S_IFMT
9904 switch (st.st_mode & S_IFMT)
9905 {
9906 case S_IFREG: t = "file"; break;
9907 case S_IFDIR: t = "dir"; break;
9908# ifdef S_IFLNK
9909 case S_IFLNK: t = "link"; break;
9910# endif
9911# ifdef S_IFBLK
9912 case S_IFBLK: t = "bdev"; break;
9913# endif
9914# ifdef S_IFCHR
9915 case S_IFCHR: t = "cdev"; break;
9916# endif
9917# ifdef S_IFIFO
9918 case S_IFIFO: t = "fifo"; break;
9919# endif
9920# ifdef S_IFSOCK
9921 case S_IFSOCK: t = "socket"; break;
9922# endif
9923 default: t = "other";
9924 }
9925# else
9926 if (mch_isdir(fname))
9927 t = "dir";
9928 else
9929 t = "file";
9930# endif
9931#endif
9932 type = vim_strsave((char_u *)t);
9933 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009934 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009935}
9936
9937/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009938 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009939 */
9940 static void
9941f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009942 typval_T *argvars;
9943 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009944{
9945 linenr_T lnum;
9946 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009947 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009948
9949 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009950 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009951 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009952 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009953 retlist = FALSE;
9954 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009955 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009956 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009957 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009958 retlist = TRUE;
9959 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009960
Bram Moolenaar342337a2005-07-21 21:11:17 +00009961 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009962}
9963
9964/*
Bram Moolenaara5525202006-03-02 22:52:09 +00009965 * "getpos(string)" function
9966 */
9967 static void
9968f_getpos(argvars, rettv)
9969 typval_T *argvars;
9970 typval_T *rettv;
9971{
9972 pos_T *fp;
9973 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009974 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009975
9976 if (rettv_list_alloc(rettv) == OK)
9977 {
9978 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009979 fp = var2fpos(&argvars[0], TRUE, &fnum);
9980 if (fnum != -1)
9981 list_append_number(l, (varnumber_T)fnum);
9982 else
9983 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +00009984 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
9985 : (varnumber_T)0);
9986 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
9987 : (varnumber_T)0);
9988 list_append_number(l,
9989#ifdef FEAT_VIRTUALEDIT
9990 (fp != NULL) ? (varnumber_T)fp->coladd :
9991#endif
9992 (varnumber_T)0);
9993 }
9994 else
9995 rettv->vval.v_number = FALSE;
9996}
9997
9998/*
Bram Moolenaar280f1262006-01-30 00:14:18 +00009999 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010000 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010001/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010002 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010003f_getqflist(argvars, rettv)
10004 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010005 typval_T *rettv;
10006{
10007#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010008 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010009#endif
10010
10011 rettv->vval.v_number = FALSE;
10012#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010013 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010014 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010015 wp = NULL;
10016 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10017 {
10018 wp = find_win_by_nr(&argvars[0]);
10019 if (wp == NULL)
10020 return;
10021 }
10022
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010023 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010024 }
10025#endif
10026}
10027
10028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 * "getreg()" function
10030 */
10031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010033 typval_T *argvars;
10034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035{
10036 char_u *strregname;
10037 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010038 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010039 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010041 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010043 strregname = get_tv_string_chk(&argvars[0]);
10044 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010045 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010046 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010049 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 regname = (strregname == NULL ? '"' : *strregname);
10051 if (regname == 0)
10052 regname = '"';
10053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010055 rettv->vval.v_string = error ? NULL :
10056 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057}
10058
10059/*
10060 * "getregtype()" function
10061 */
10062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010063f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010064 typval_T *argvars;
10065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066{
10067 char_u *strregname;
10068 int regname;
10069 char_u buf[NUMBUFLEN + 2];
10070 long reglen = 0;
10071
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010072 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010073 {
10074 strregname = get_tv_string_chk(&argvars[0]);
10075 if (strregname == NULL) /* type error; errmsg already given */
10076 {
10077 rettv->v_type = VAR_STRING;
10078 rettv->vval.v_string = NULL;
10079 return;
10080 }
10081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 else
10083 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010084 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085
10086 regname = (strregname == NULL ? '"' : *strregname);
10087 if (regname == 0)
10088 regname = '"';
10089
10090 buf[0] = NUL;
10091 buf[1] = NUL;
10092 switch (get_reg_type(regname, &reglen))
10093 {
10094 case MLINE: buf[0] = 'V'; break;
10095 case MCHAR: buf[0] = 'v'; break;
10096#ifdef FEAT_VISUAL
10097 case MBLOCK:
10098 buf[0] = Ctrl_V;
10099 sprintf((char *)buf + 1, "%ld", reglen + 1);
10100 break;
10101#endif
10102 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010103 rettv->v_type = VAR_STRING;
10104 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105}
10106
10107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 * "getwinposx()" function
10109 */
10110/*ARGSUSED*/
10111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010112f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010113 typval_T *argvars;
10114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010116 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117#ifdef FEAT_GUI
10118 if (gui.in_use)
10119 {
10120 int x, y;
10121
10122 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 }
10125#endif
10126}
10127
10128/*
10129 * "getwinposy()" function
10130 */
10131/*ARGSUSED*/
10132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010133f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010134 typval_T *argvars;
10135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138#ifdef FEAT_GUI
10139 if (gui.in_use)
10140 {
10141 int x, y;
10142
10143 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010144 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 }
10146#endif
10147}
10148
Bram Moolenaara40058a2005-07-11 22:42:07 +000010149 static win_T *
10150find_win_by_nr(vp)
10151 typval_T *vp;
10152{
10153#ifdef FEAT_WINDOWS
10154 win_T *wp;
10155#endif
10156 int nr;
10157
10158 nr = get_tv_number_chk(vp, NULL);
10159
10160#ifdef FEAT_WINDOWS
10161 if (nr < 0)
10162 return NULL;
10163 if (nr == 0)
10164 return curwin;
10165
10166 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10167 if (--nr <= 0)
10168 break;
10169 return wp;
10170#else
10171 if (nr == 0 || nr == 1)
10172 return curwin;
10173 return NULL;
10174#endif
10175}
10176
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177/*
10178 * "getwinvar()" function
10179 */
10180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010181f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010182 typval_T *argvars;
10183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184{
10185 win_T *win, *oldcurwin;
10186 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010187 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010190 varname = get_tv_string_chk(&argvars[1]);
10191 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010193 rettv->v_type = VAR_STRING;
10194 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195
10196 if (win != NULL && varname != NULL)
10197 {
10198 if (*varname == '&') /* window-local-option */
10199 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010200 /* Set curwin to be our win, temporarily. Also set curbuf, so
10201 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 oldcurwin = curwin;
10203 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010204 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010206 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207
10208 /* restore previous notion of curwin */
10209 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010210 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 }
10212 else
10213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010214 if (*varname == NUL)
10215 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10216 * scope prefix before the NUL byte is required by
10217 * find_var_in_ht(). */
10218 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010220 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010222 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 }
10224 }
10225
10226 --emsg_off;
10227}
10228
10229/*
10230 * "glob()" function
10231 */
10232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010233f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010234 typval_T *argvars;
10235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236{
10237 expand_T xpc;
10238
10239 ExpandInit(&xpc);
10240 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010241 rettv->v_type = VAR_STRING;
10242 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10244 ExpandCleanup(&xpc);
10245}
10246
10247/*
10248 * "globpath()" function
10249 */
10250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010251f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010252 typval_T *argvars;
10253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254{
10255 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010256 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010258 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010259 if (file == NULL)
10260 rettv->vval.v_string = NULL;
10261 else
10262 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263}
10264
10265/*
10266 * "has()" function
10267 */
10268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010269f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010270 typval_T *argvars;
10271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272{
10273 int i;
10274 char_u *name;
10275 int n = FALSE;
10276 static char *(has_list[]) =
10277 {
10278#ifdef AMIGA
10279 "amiga",
10280# ifdef FEAT_ARP
10281 "arp",
10282# endif
10283#endif
10284#ifdef __BEOS__
10285 "beos",
10286#endif
10287#ifdef MSDOS
10288# ifdef DJGPP
10289 "dos32",
10290# else
10291 "dos16",
10292# endif
10293#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010294#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 "mac",
10296#endif
10297#if defined(MACOS_X_UNIX)
10298 "macunix",
10299#endif
10300#ifdef OS2
10301 "os2",
10302#endif
10303#ifdef __QNX__
10304 "qnx",
10305#endif
10306#ifdef RISCOS
10307 "riscos",
10308#endif
10309#ifdef UNIX
10310 "unix",
10311#endif
10312#ifdef VMS
10313 "vms",
10314#endif
10315#ifdef WIN16
10316 "win16",
10317#endif
10318#ifdef WIN32
10319 "win32",
10320#endif
10321#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10322 "win32unix",
10323#endif
10324#ifdef WIN64
10325 "win64",
10326#endif
10327#ifdef EBCDIC
10328 "ebcdic",
10329#endif
10330#ifndef CASE_INSENSITIVE_FILENAME
10331 "fname_case",
10332#endif
10333#ifdef FEAT_ARABIC
10334 "arabic",
10335#endif
10336#ifdef FEAT_AUTOCMD
10337 "autocmd",
10338#endif
10339#ifdef FEAT_BEVAL
10340 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010341# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10342 "balloon_multiline",
10343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344#endif
10345#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10346 "builtin_terms",
10347# ifdef ALL_BUILTIN_TCAPS
10348 "all_builtin_terms",
10349# endif
10350#endif
10351#ifdef FEAT_BYTEOFF
10352 "byte_offset",
10353#endif
10354#ifdef FEAT_CINDENT
10355 "cindent",
10356#endif
10357#ifdef FEAT_CLIENTSERVER
10358 "clientserver",
10359#endif
10360#ifdef FEAT_CLIPBOARD
10361 "clipboard",
10362#endif
10363#ifdef FEAT_CMDL_COMPL
10364 "cmdline_compl",
10365#endif
10366#ifdef FEAT_CMDHIST
10367 "cmdline_hist",
10368#endif
10369#ifdef FEAT_COMMENTS
10370 "comments",
10371#endif
10372#ifdef FEAT_CRYPT
10373 "cryptv",
10374#endif
10375#ifdef FEAT_CSCOPE
10376 "cscope",
10377#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010378#ifdef CURSOR_SHAPE
10379 "cursorshape",
10380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381#ifdef DEBUG
10382 "debug",
10383#endif
10384#ifdef FEAT_CON_DIALOG
10385 "dialog_con",
10386#endif
10387#ifdef FEAT_GUI_DIALOG
10388 "dialog_gui",
10389#endif
10390#ifdef FEAT_DIFF
10391 "diff",
10392#endif
10393#ifdef FEAT_DIGRAPHS
10394 "digraphs",
10395#endif
10396#ifdef FEAT_DND
10397 "dnd",
10398#endif
10399#ifdef FEAT_EMACS_TAGS
10400 "emacs_tags",
10401#endif
10402 "eval", /* always present, of course! */
10403#ifdef FEAT_EX_EXTRA
10404 "ex_extra",
10405#endif
10406#ifdef FEAT_SEARCH_EXTRA
10407 "extra_search",
10408#endif
10409#ifdef FEAT_FKMAP
10410 "farsi",
10411#endif
10412#ifdef FEAT_SEARCHPATH
10413 "file_in_path",
10414#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010415#if defined(UNIX) && !defined(USE_SYSTEM)
10416 "filterpipe",
10417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418#ifdef FEAT_FIND_ID
10419 "find_in_path",
10420#endif
10421#ifdef FEAT_FOLDING
10422 "folding",
10423#endif
10424#ifdef FEAT_FOOTER
10425 "footer",
10426#endif
10427#if !defined(USE_SYSTEM) && defined(UNIX)
10428 "fork",
10429#endif
10430#ifdef FEAT_GETTEXT
10431 "gettext",
10432#endif
10433#ifdef FEAT_GUI
10434 "gui",
10435#endif
10436#ifdef FEAT_GUI_ATHENA
10437# ifdef FEAT_GUI_NEXTAW
10438 "gui_neXtaw",
10439# else
10440 "gui_athena",
10441# endif
10442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443#ifdef FEAT_GUI_GTK
10444 "gui_gtk",
10445# ifdef HAVE_GTK2
10446 "gui_gtk2",
10447# endif
10448#endif
10449#ifdef FEAT_GUI_MAC
10450 "gui_mac",
10451#endif
10452#ifdef FEAT_GUI_MOTIF
10453 "gui_motif",
10454#endif
10455#ifdef FEAT_GUI_PHOTON
10456 "gui_photon",
10457#endif
10458#ifdef FEAT_GUI_W16
10459 "gui_win16",
10460#endif
10461#ifdef FEAT_GUI_W32
10462 "gui_win32",
10463#endif
10464#ifdef FEAT_HANGULIN
10465 "hangul_input",
10466#endif
10467#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10468 "iconv",
10469#endif
10470#ifdef FEAT_INS_EXPAND
10471 "insert_expand",
10472#endif
10473#ifdef FEAT_JUMPLIST
10474 "jumplist",
10475#endif
10476#ifdef FEAT_KEYMAP
10477 "keymap",
10478#endif
10479#ifdef FEAT_LANGMAP
10480 "langmap",
10481#endif
10482#ifdef FEAT_LIBCALL
10483 "libcall",
10484#endif
10485#ifdef FEAT_LINEBREAK
10486 "linebreak",
10487#endif
10488#ifdef FEAT_LISP
10489 "lispindent",
10490#endif
10491#ifdef FEAT_LISTCMDS
10492 "listcmds",
10493#endif
10494#ifdef FEAT_LOCALMAP
10495 "localmap",
10496#endif
10497#ifdef FEAT_MENU
10498 "menu",
10499#endif
10500#ifdef FEAT_SESSION
10501 "mksession",
10502#endif
10503#ifdef FEAT_MODIFY_FNAME
10504 "modify_fname",
10505#endif
10506#ifdef FEAT_MOUSE
10507 "mouse",
10508#endif
10509#ifdef FEAT_MOUSESHAPE
10510 "mouseshape",
10511#endif
10512#if defined(UNIX) || defined(VMS)
10513# ifdef FEAT_MOUSE_DEC
10514 "mouse_dec",
10515# endif
10516# ifdef FEAT_MOUSE_GPM
10517 "mouse_gpm",
10518# endif
10519# ifdef FEAT_MOUSE_JSB
10520 "mouse_jsbterm",
10521# endif
10522# ifdef FEAT_MOUSE_NET
10523 "mouse_netterm",
10524# endif
10525# ifdef FEAT_MOUSE_PTERM
10526 "mouse_pterm",
10527# endif
10528# ifdef FEAT_MOUSE_XTERM
10529 "mouse_xterm",
10530# endif
10531#endif
10532#ifdef FEAT_MBYTE
10533 "multi_byte",
10534#endif
10535#ifdef FEAT_MBYTE_IME
10536 "multi_byte_ime",
10537#endif
10538#ifdef FEAT_MULTI_LANG
10539 "multi_lang",
10540#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010541#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010542#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010543 "mzscheme",
10544#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546#ifdef FEAT_OLE
10547 "ole",
10548#endif
10549#ifdef FEAT_OSFILETYPE
10550 "osfiletype",
10551#endif
10552#ifdef FEAT_PATH_EXTRA
10553 "path_extra",
10554#endif
10555#ifdef FEAT_PERL
10556#ifndef DYNAMIC_PERL
10557 "perl",
10558#endif
10559#endif
10560#ifdef FEAT_PYTHON
10561#ifndef DYNAMIC_PYTHON
10562 "python",
10563#endif
10564#endif
10565#ifdef FEAT_POSTSCRIPT
10566 "postscript",
10567#endif
10568#ifdef FEAT_PRINTER
10569 "printer",
10570#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010571#ifdef FEAT_PROFILE
10572 "profile",
10573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574#ifdef FEAT_QUICKFIX
10575 "quickfix",
10576#endif
10577#ifdef FEAT_RIGHTLEFT
10578 "rightleft",
10579#endif
10580#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10581 "ruby",
10582#endif
10583#ifdef FEAT_SCROLLBIND
10584 "scrollbind",
10585#endif
10586#ifdef FEAT_CMDL_INFO
10587 "showcmd",
10588 "cmdline_info",
10589#endif
10590#ifdef FEAT_SIGNS
10591 "signs",
10592#endif
10593#ifdef FEAT_SMARTINDENT
10594 "smartindent",
10595#endif
10596#ifdef FEAT_SNIFF
10597 "sniff",
10598#endif
10599#ifdef FEAT_STL_OPT
10600 "statusline",
10601#endif
10602#ifdef FEAT_SUN_WORKSHOP
10603 "sun_workshop",
10604#endif
10605#ifdef FEAT_NETBEANS_INTG
10606 "netbeans_intg",
10607#endif
10608#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010609 "spell",
10610#endif
10611#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 "syntax",
10613#endif
10614#if defined(USE_SYSTEM) || !defined(UNIX)
10615 "system",
10616#endif
10617#ifdef FEAT_TAG_BINS
10618 "tag_binary",
10619#endif
10620#ifdef FEAT_TAG_OLDSTATIC
10621 "tag_old_static",
10622#endif
10623#ifdef FEAT_TAG_ANYWHITE
10624 "tag_any_white",
10625#endif
10626#ifdef FEAT_TCL
10627# ifndef DYNAMIC_TCL
10628 "tcl",
10629# endif
10630#endif
10631#ifdef TERMINFO
10632 "terminfo",
10633#endif
10634#ifdef FEAT_TERMRESPONSE
10635 "termresponse",
10636#endif
10637#ifdef FEAT_TEXTOBJ
10638 "textobjects",
10639#endif
10640#ifdef HAVE_TGETENT
10641 "tgetent",
10642#endif
10643#ifdef FEAT_TITLE
10644 "title",
10645#endif
10646#ifdef FEAT_TOOLBAR
10647 "toolbar",
10648#endif
10649#ifdef FEAT_USR_CMDS
10650 "user-commands", /* was accidentally included in 5.4 */
10651 "user_commands",
10652#endif
10653#ifdef FEAT_VIMINFO
10654 "viminfo",
10655#endif
10656#ifdef FEAT_VERTSPLIT
10657 "vertsplit",
10658#endif
10659#ifdef FEAT_VIRTUALEDIT
10660 "virtualedit",
10661#endif
10662#ifdef FEAT_VISUAL
10663 "visual",
10664#endif
10665#ifdef FEAT_VISUALEXTRA
10666 "visualextra",
10667#endif
10668#ifdef FEAT_VREPLACE
10669 "vreplace",
10670#endif
10671#ifdef FEAT_WILDIGN
10672 "wildignore",
10673#endif
10674#ifdef FEAT_WILDMENU
10675 "wildmenu",
10676#endif
10677#ifdef FEAT_WINDOWS
10678 "windows",
10679#endif
10680#ifdef FEAT_WAK
10681 "winaltkeys",
10682#endif
10683#ifdef FEAT_WRITEBACKUP
10684 "writebackup",
10685#endif
10686#ifdef FEAT_XIM
10687 "xim",
10688#endif
10689#ifdef FEAT_XFONTSET
10690 "xfontset",
10691#endif
10692#ifdef USE_XSMP
10693 "xsmp",
10694#endif
10695#ifdef USE_XSMP_INTERACT
10696 "xsmp_interact",
10697#endif
10698#ifdef FEAT_XCLIPBOARD
10699 "xterm_clipboard",
10700#endif
10701#ifdef FEAT_XTERM_SAVE
10702 "xterm_save",
10703#endif
10704#if defined(UNIX) && defined(FEAT_X11)
10705 "X11",
10706#endif
10707 NULL
10708 };
10709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010710 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 for (i = 0; has_list[i] != NULL; ++i)
10712 if (STRICMP(name, has_list[i]) == 0)
10713 {
10714 n = TRUE;
10715 break;
10716 }
10717
10718 if (n == FALSE)
10719 {
10720 if (STRNICMP(name, "patch", 5) == 0)
10721 n = has_patch(atoi((char *)name + 5));
10722 else if (STRICMP(name, "vim_starting") == 0)
10723 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010724#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10725 else if (STRICMP(name, "balloon_multiline") == 0)
10726 n = multiline_balloon_available();
10727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728#ifdef DYNAMIC_TCL
10729 else if (STRICMP(name, "tcl") == 0)
10730 n = tcl_enabled(FALSE);
10731#endif
10732#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10733 else if (STRICMP(name, "iconv") == 0)
10734 n = iconv_enabled(FALSE);
10735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010736#ifdef DYNAMIC_MZSCHEME
10737 else if (STRICMP(name, "mzscheme") == 0)
10738 n = mzscheme_enabled(FALSE);
10739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740#ifdef DYNAMIC_RUBY
10741 else if (STRICMP(name, "ruby") == 0)
10742 n = ruby_enabled(FALSE);
10743#endif
10744#ifdef DYNAMIC_PYTHON
10745 else if (STRICMP(name, "python") == 0)
10746 n = python_enabled(FALSE);
10747#endif
10748#ifdef DYNAMIC_PERL
10749 else if (STRICMP(name, "perl") == 0)
10750 n = perl_enabled(FALSE);
10751#endif
10752#ifdef FEAT_GUI
10753 else if (STRICMP(name, "gui_running") == 0)
10754 n = (gui.in_use || gui.starting);
10755# ifdef FEAT_GUI_W32
10756 else if (STRICMP(name, "gui_win32s") == 0)
10757 n = gui_is_win32s();
10758# endif
10759# ifdef FEAT_BROWSE
10760 else if (STRICMP(name, "browse") == 0)
10761 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10762# endif
10763#endif
10764#ifdef FEAT_SYN_HL
10765 else if (STRICMP(name, "syntax_items") == 0)
10766 n = syntax_present(curbuf);
10767#endif
10768#if defined(WIN3264)
10769 else if (STRICMP(name, "win95") == 0)
10770 n = mch_windows95();
10771#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010772#ifdef FEAT_NETBEANS_INTG
10773 else if (STRICMP(name, "netbeans_enabled") == 0)
10774 n = usingNetbeans;
10775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 }
10777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010778 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779}
10780
10781/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010782 * "has_key()" function
10783 */
10784 static void
10785f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010786 typval_T *argvars;
10787 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010788{
10789 rettv->vval.v_number = 0;
10790 if (argvars[0].v_type != VAR_DICT)
10791 {
10792 EMSG(_(e_dictreq));
10793 return;
10794 }
10795 if (argvars[0].vval.v_dict == NULL)
10796 return;
10797
10798 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010799 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010800}
10801
10802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 * "hasmapto()" function
10804 */
10805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010806f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010807 typval_T *argvars;
10808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809{
10810 char_u *name;
10811 char_u *mode;
10812 char_u buf[NUMBUFLEN];
10813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010814 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010815 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 mode = (char_u *)"nvo";
10817 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010818 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819
10820 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010823 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824}
10825
10826/*
10827 * "histadd()" function
10828 */
10829/*ARGSUSED*/
10830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010832 typval_T *argvars;
10833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834{
10835#ifdef FEAT_CMDHIST
10836 int histype;
10837 char_u *str;
10838 char_u buf[NUMBUFLEN];
10839#endif
10840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010841 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 if (check_restricted() || check_secure())
10843 return;
10844#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010845 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10846 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 if (histype >= 0)
10848 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010849 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 if (*str != NUL)
10851 {
10852 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010853 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 return;
10855 }
10856 }
10857#endif
10858}
10859
10860/*
10861 * "histdel()" function
10862 */
10863/*ARGSUSED*/
10864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010865f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010866 typval_T *argvars;
10867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868{
10869#ifdef FEAT_CMDHIST
10870 int n;
10871 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010872 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10875 if (str == NULL)
10876 n = 0;
10877 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010879 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010880 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010882 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 else
10885 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010886 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010887 get_tv_string_buf(&argvars[1], buf));
10888 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010890 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891#endif
10892}
10893
10894/*
10895 * "histget()" function
10896 */
10897/*ARGSUSED*/
10898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010899f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010900 typval_T *argvars;
10901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902{
10903#ifdef FEAT_CMDHIST
10904 int type;
10905 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010906 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010908 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10909 if (str == NULL)
10910 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010912 {
10913 type = get_histtype(str);
10914 if (argvars[1].v_type == VAR_UNKNOWN)
10915 idx = get_history_idx(type);
10916 else
10917 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10918 /* -1 on type error */
10919 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010924 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925}
10926
10927/*
10928 * "histnr()" function
10929 */
10930/*ARGSUSED*/
10931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010932f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010933 typval_T *argvars;
10934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935{
10936 int i;
10937
10938#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010939 char_u *history = get_tv_string_chk(&argvars[0]);
10940
10941 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 if (i >= HIST_CMD && i < HIST_COUNT)
10943 i = get_history_idx(i);
10944 else
10945#endif
10946 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010947 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948}
10949
10950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 * "highlightID(name)" function
10952 */
10953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010955 typval_T *argvars;
10956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010958 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959}
10960
10961/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010962 * "highlight_exists()" function
10963 */
10964 static void
10965f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010966 typval_T *argvars;
10967 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010968{
10969 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10970}
10971
10972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 * "hostname()" function
10974 */
10975/*ARGSUSED*/
10976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010978 typval_T *argvars;
10979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980{
10981 char_u hostname[256];
10982
10983 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010984 rettv->v_type = VAR_STRING;
10985 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986}
10987
10988/*
10989 * iconv() function
10990 */
10991/*ARGSUSED*/
10992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010993f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010994 typval_T *argvars;
10995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996{
10997#ifdef FEAT_MBYTE
10998 char_u buf1[NUMBUFLEN];
10999 char_u buf2[NUMBUFLEN];
11000 char_u *from, *to, *str;
11001 vimconv_T vimconv;
11002#endif
11003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004 rettv->v_type = VAR_STRING;
11005 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006
11007#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011008 str = get_tv_string(&argvars[0]);
11009 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11010 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 vimconv.vc_type = CONV_NONE;
11012 convert_setup(&vimconv, from, to);
11013
11014 /* If the encodings are equal, no conversion needed. */
11015 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011018 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019
11020 convert_setup(&vimconv, NULL, NULL);
11021 vim_free(from);
11022 vim_free(to);
11023#endif
11024}
11025
11026/*
11027 * "indent()" function
11028 */
11029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011030f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011031 typval_T *argvars;
11032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033{
11034 linenr_T lnum;
11035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011038 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041}
11042
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011043/*
11044 * "index()" function
11045 */
11046 static void
11047f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011048 typval_T *argvars;
11049 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011050{
Bram Moolenaar33570922005-01-25 22:26:29 +000011051 list_T *l;
11052 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011053 long idx = 0;
11054 int ic = FALSE;
11055
11056 rettv->vval.v_number = -1;
11057 if (argvars[0].v_type != VAR_LIST)
11058 {
11059 EMSG(_(e_listreq));
11060 return;
11061 }
11062 l = argvars[0].vval.v_list;
11063 if (l != NULL)
11064 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011065 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011066 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011068 int error = FALSE;
11069
Bram Moolenaar758711c2005-02-02 23:11:38 +000011070 /* Start at specified item. Use the cached index that list_find()
11071 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011072 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011073 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011074 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075 ic = get_tv_number_chk(&argvars[3], &error);
11076 if (error)
11077 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011078 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011079
Bram Moolenaar758711c2005-02-02 23:11:38 +000011080 for ( ; item != NULL; item = item->li_next, ++idx)
11081 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011082 {
11083 rettv->vval.v_number = idx;
11084 break;
11085 }
11086 }
11087}
11088
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089static int inputsecret_flag = 0;
11090
11091/*
11092 * "input()" function
11093 * Also handles inputsecret() when inputsecret is set.
11094 */
11095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011096f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011097 typval_T *argvars;
11098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011100 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 char_u *p = NULL;
11102 int c;
11103 char_u buf[NUMBUFLEN];
11104 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011105 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011106 int xp_type = EXPAND_NOTHING;
11107 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110
11111#ifdef NO_CONSOLE_INPUT
11112 /* While starting up, there is no place to enter text. */
11113 if (no_console_input())
11114 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 return;
11117 }
11118#endif
11119
11120 cmd_silent = FALSE; /* Want to see the prompt. */
11121 if (prompt != NULL)
11122 {
11123 /* Only the part of the message after the last NL is considered as
11124 * prompt for the command line */
11125 p = vim_strrchr(prompt, '\n');
11126 if (p == NULL)
11127 p = prompt;
11128 else
11129 {
11130 ++p;
11131 c = *p;
11132 *p = NUL;
11133 msg_start();
11134 msg_clr_eos();
11135 msg_puts_attr(prompt, echo_attr);
11136 msg_didout = FALSE;
11137 msg_starthere();
11138 *p = c;
11139 }
11140 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011142 if (argvars[1].v_type != VAR_UNKNOWN)
11143 {
11144 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11145 if (defstr != NULL)
11146 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147
Bram Moolenaar4463f292005-09-25 22:20:24 +000011148 if (argvars[2].v_type != VAR_UNKNOWN)
11149 {
11150 char_u *xp_name;
11151 int xp_namelen;
11152 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011153
Bram Moolenaar4463f292005-09-25 22:20:24 +000011154 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011155
Bram Moolenaar4463f292005-09-25 22:20:24 +000011156 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11157 if (xp_name == NULL)
11158 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011159
Bram Moolenaar4463f292005-09-25 22:20:24 +000011160 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011161
Bram Moolenaar4463f292005-09-25 22:20:24 +000011162 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11163 &xp_arg) == FAIL)
11164 return;
11165 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011166 }
11167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011168 if (defstr != NULL)
11169 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011170 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11171 xp_type, xp_arg);
11172
11173 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011175 /* since the user typed this, no need to wait for return */
11176 need_wait_return = FALSE;
11177 msg_didout = FALSE;
11178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 cmd_silent = cmd_silent_save;
11180}
11181
11182/*
11183 * "inputdialog()" function
11184 */
11185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011186f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011187 typval_T *argvars;
11188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189{
11190#if defined(FEAT_GUI_TEXTDIALOG)
11191 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11192 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11193 {
11194 char_u *message;
11195 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011196 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011198 message = get_tv_string_chk(&argvars[0]);
11199 if (argvars[1].v_type != VAR_UNKNOWN
11200 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011201 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 else
11203 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011204 if (message != NULL && defstr != NULL
11205 && do_dialog(VIM_QUESTION, NULL, message,
11206 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011207 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 else
11209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011210 if (message != NULL && defstr != NULL
11211 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011212 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213 rettv->vval.v_string = vim_strsave(
11214 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011216 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 }
11220 else
11221#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011222 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223}
11224
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011225/*
11226 * "inputlist()" function
11227 */
11228 static void
11229f_inputlist(argvars, rettv)
11230 typval_T *argvars;
11231 typval_T *rettv;
11232{
11233 listitem_T *li;
11234 int selected;
11235 int mouse_used;
11236
11237 rettv->vval.v_number = 0;
11238#ifdef NO_CONSOLE_INPUT
11239 /* While starting up, there is no place to enter text. */
11240 if (no_console_input())
11241 return;
11242#endif
11243 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11244 {
11245 EMSG2(_(e_listarg), "inputlist()");
11246 return;
11247 }
11248
11249 msg_start();
11250 lines_left = Rows; /* avoid more prompt */
11251 msg_scroll = TRUE;
11252 msg_clr_eos();
11253
11254 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11255 {
11256 msg_puts(get_tv_string(&li->li_tv));
11257 msg_putchar('\n');
11258 }
11259
11260 /* Ask for choice. */
11261 selected = prompt_for_number(&mouse_used);
11262 if (mouse_used)
11263 selected -= lines_left;
11264
11265 rettv->vval.v_number = selected;
11266}
11267
11268
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11270
11271/*
11272 * "inputrestore()" function
11273 */
11274/*ARGSUSED*/
11275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011276f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011277 typval_T *argvars;
11278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279{
11280 if (ga_userinput.ga_len > 0)
11281 {
11282 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11284 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 }
11287 else if (p_verbose > 1)
11288 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011289 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 }
11292}
11293
11294/*
11295 * "inputsave()" function
11296 */
11297/*ARGSUSED*/
11298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011299f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011300 typval_T *argvars;
11301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302{
11303 /* Add an entry to the stack of typehead storage. */
11304 if (ga_grow(&ga_userinput, 1) == OK)
11305 {
11306 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11307 + ga_userinput.ga_len);
11308 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 }
11311 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313}
11314
11315/*
11316 * "inputsecret()" function
11317 */
11318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011319f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011320 typval_T *argvars;
11321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322{
11323 ++cmdline_star;
11324 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011325 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 --cmdline_star;
11327 --inputsecret_flag;
11328}
11329
11330/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011331 * "insert()" function
11332 */
11333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011335 typval_T *argvars;
11336 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011337{
11338 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011339 listitem_T *item;
11340 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011341 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011342
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011343 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011344 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011345 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011346 else if ((l = argvars[0].vval.v_list) != NULL
11347 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011348 {
11349 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011350 before = get_tv_number_chk(&argvars[2], &error);
11351 if (error)
11352 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011353
Bram Moolenaar758711c2005-02-02 23:11:38 +000011354 if (before == l->lv_len)
11355 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011356 else
11357 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011358 item = list_find(l, before);
11359 if (item == NULL)
11360 {
11361 EMSGN(_(e_listidx), before);
11362 l = NULL;
11363 }
11364 }
11365 if (l != NULL)
11366 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011367 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011368 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011369 }
11370 }
11371}
11372
11373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 * "isdirectory()" function
11375 */
11376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011378 typval_T *argvars;
11379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382}
11383
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011384/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011385 * "islocked()" function
11386 */
11387 static void
11388f_islocked(argvars, rettv)
11389 typval_T *argvars;
11390 typval_T *rettv;
11391{
11392 lval_T lv;
11393 char_u *end;
11394 dictitem_T *di;
11395
11396 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011397 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11398 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011399 if (end != NULL && lv.ll_name != NULL)
11400 {
11401 if (*end != NUL)
11402 EMSG(_(e_trailing));
11403 else
11404 {
11405 if (lv.ll_tv == NULL)
11406 {
11407 if (check_changedtick(lv.ll_name))
11408 rettv->vval.v_number = 1; /* always locked */
11409 else
11410 {
11411 di = find_var(lv.ll_name, NULL);
11412 if (di != NULL)
11413 {
11414 /* Consider a variable locked when:
11415 * 1. the variable itself is locked
11416 * 2. the value of the variable is locked.
11417 * 3. the List or Dict value is locked.
11418 */
11419 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11420 || tv_islocked(&di->di_tv));
11421 }
11422 }
11423 }
11424 else if (lv.ll_range)
11425 EMSG(_("E745: Range not allowed"));
11426 else if (lv.ll_newkey != NULL)
11427 EMSG2(_(e_dictkey), lv.ll_newkey);
11428 else if (lv.ll_list != NULL)
11429 /* List item. */
11430 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11431 else
11432 /* Dictionary item. */
11433 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11434 }
11435 }
11436
11437 clear_lval(&lv);
11438}
11439
Bram Moolenaar33570922005-01-25 22:26:29 +000011440static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011441
11442/*
11443 * Turn a dict into a list:
11444 * "what" == 0: list of keys
11445 * "what" == 1: list of values
11446 * "what" == 2: list of items
11447 */
11448 static void
11449dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011450 typval_T *argvars;
11451 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011452 int what;
11453{
Bram Moolenaar33570922005-01-25 22:26:29 +000011454 list_T *l2;
11455 dictitem_T *di;
11456 hashitem_T *hi;
11457 listitem_T *li;
11458 listitem_T *li2;
11459 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011460 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011461
11462 rettv->vval.v_number = 0;
11463 if (argvars[0].v_type != VAR_DICT)
11464 {
11465 EMSG(_(e_dictreq));
11466 return;
11467 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011468 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011469 return;
11470
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011471 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011472 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011473
Bram Moolenaar33570922005-01-25 22:26:29 +000011474 todo = d->dv_hashtab.ht_used;
11475 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011476 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011477 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011478 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011479 --todo;
11480 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011481
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011482 li = listitem_alloc();
11483 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011484 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011485 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011486
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011487 if (what == 0)
11488 {
11489 /* keys() */
11490 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011491 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011492 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11493 }
11494 else if (what == 1)
11495 {
11496 /* values() */
11497 copy_tv(&di->di_tv, &li->li_tv);
11498 }
11499 else
11500 {
11501 /* items() */
11502 l2 = list_alloc();
11503 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011504 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011505 li->li_tv.vval.v_list = l2;
11506 if (l2 == NULL)
11507 break;
11508 ++l2->lv_refcount;
11509
11510 li2 = listitem_alloc();
11511 if (li2 == NULL)
11512 break;
11513 list_append(l2, li2);
11514 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011515 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011516 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11517
11518 li2 = listitem_alloc();
11519 if (li2 == NULL)
11520 break;
11521 list_append(l2, li2);
11522 copy_tv(&di->di_tv, &li2->li_tv);
11523 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011524 }
11525 }
11526}
11527
11528/*
11529 * "items(dict)" function
11530 */
11531 static void
11532f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011533 typval_T *argvars;
11534 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011535{
11536 dict_list(argvars, rettv, 2);
11537}
11538
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011540 * "join()" function
11541 */
11542 static void
11543f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011544 typval_T *argvars;
11545 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011546{
11547 garray_T ga;
11548 char_u *sep;
11549
11550 rettv->vval.v_number = 0;
11551 if (argvars[0].v_type != VAR_LIST)
11552 {
11553 EMSG(_(e_listreq));
11554 return;
11555 }
11556 if (argvars[0].vval.v_list == NULL)
11557 return;
11558 if (argvars[1].v_type == VAR_UNKNOWN)
11559 sep = (char_u *)" ";
11560 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011561 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011562
11563 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011564
11565 if (sep != NULL)
11566 {
11567 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011568 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011569 ga_append(&ga, NUL);
11570 rettv->vval.v_string = (char_u *)ga.ga_data;
11571 }
11572 else
11573 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011574}
11575
11576/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011577 * "keys()" function
11578 */
11579 static void
11580f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011581 typval_T *argvars;
11582 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011583{
11584 dict_list(argvars, rettv, 0);
11585}
11586
11587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 * "last_buffer_nr()" function.
11589 */
11590/*ARGSUSED*/
11591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011593 typval_T *argvars;
11594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595{
11596 int n = 0;
11597 buf_T *buf;
11598
11599 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11600 if (n < buf->b_fnum)
11601 n = buf->b_fnum;
11602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011603 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011604}
11605
11606/*
11607 * "len()" function
11608 */
11609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011610f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011611 typval_T *argvars;
11612 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011613{
11614 switch (argvars[0].v_type)
11615 {
11616 case VAR_STRING:
11617 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011618 rettv->vval.v_number = (varnumber_T)STRLEN(
11619 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011620 break;
11621 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011623 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011624 case VAR_DICT:
11625 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11626 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011627 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011628 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011629 break;
11630 }
11631}
11632
Bram Moolenaar33570922005-01-25 22:26:29 +000011633static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011634
11635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011636libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011637 typval_T *argvars;
11638 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011639 int type;
11640{
11641#ifdef FEAT_LIBCALL
11642 char_u *string_in;
11643 char_u **string_result;
11644 int nr_result;
11645#endif
11646
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011648 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011649 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011652
11653 if (check_restricted() || check_secure())
11654 return;
11655
11656#ifdef FEAT_LIBCALL
11657 /* The first two args must be strings, otherwise its meaningless */
11658 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11659 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011660 string_in = NULL;
11661 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011662 string_in = argvars[2].vval.v_string;
11663 if (type == VAR_NUMBER)
11664 string_result = NULL;
11665 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011667 if (mch_libcall(argvars[0].vval.v_string,
11668 argvars[1].vval.v_string,
11669 string_in,
11670 argvars[2].vval.v_number,
11671 string_result,
11672 &nr_result) == OK
11673 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011674 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011675 }
11676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677}
11678
11679/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011680 * "libcall()" function
11681 */
11682 static void
11683f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011684 typval_T *argvars;
11685 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011686{
11687 libcall_common(argvars, rettv, VAR_STRING);
11688}
11689
11690/*
11691 * "libcallnr()" function
11692 */
11693 static void
11694f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011695 typval_T *argvars;
11696 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011697{
11698 libcall_common(argvars, rettv, VAR_NUMBER);
11699}
11700
11701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 * "line(string)" function
11703 */
11704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011705f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011706 typval_T *argvars;
11707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708{
11709 linenr_T lnum = 0;
11710 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011711 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011713 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 if (fp != NULL)
11715 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011716 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717}
11718
11719/*
11720 * "line2byte(lnum)" function
11721 */
11722/*ARGSUSED*/
11723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011724f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011725 typval_T *argvars;
11726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727{
11728#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011729 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730#else
11731 linenr_T lnum;
11732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011733 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011735 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11738 if (rettv->vval.v_number >= 0)
11739 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740#endif
11741}
11742
11743/*
11744 * "lispindent(lnum)" function
11745 */
11746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011747f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011748 typval_T *argvars;
11749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750{
11751#ifdef FEAT_LISP
11752 pos_T pos;
11753 linenr_T lnum;
11754
11755 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011756 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11758 {
11759 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011760 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761 curwin->w_cursor = pos;
11762 }
11763 else
11764#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011765 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766}
11767
11768/*
11769 * "localtime()" function
11770 */
11771/*ARGSUSED*/
11772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011773f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011774 typval_T *argvars;
11775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011777 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778}
11779
Bram Moolenaar33570922005-01-25 22:26:29 +000011780static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781
11782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011783get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011784 typval_T *argvars;
11785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786 int exact;
11787{
11788 char_u *keys;
11789 char_u *which;
11790 char_u buf[NUMBUFLEN];
11791 char_u *keys_buf = NULL;
11792 char_u *rhs;
11793 int mode;
11794 garray_T ga;
11795
11796 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011797 rettv->v_type = VAR_STRING;
11798 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 if (*keys == NUL)
11802 return;
11803
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011804 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011805 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 else
11807 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 if (which == NULL)
11809 return;
11810
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 mode = get_map_mode(&which, 0);
11812
11813 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011814 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 vim_free(keys_buf);
11816 if (rhs != NULL)
11817 {
11818 ga_init(&ga);
11819 ga.ga_itemsize = 1;
11820 ga.ga_growsize = 40;
11821
11822 while (*rhs != NUL)
11823 ga_concat(&ga, str2special(&rhs, FALSE));
11824
11825 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011826 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827 }
11828}
11829
11830/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011831 * "map()" function
11832 */
11833 static void
11834f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011835 typval_T *argvars;
11836 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011837{
11838 filter_map(argvars, rettv, TRUE);
11839}
11840
11841/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011842 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 */
11844 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011845f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011846 typval_T *argvars;
11847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011849 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850}
11851
11852/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011853 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 */
11855 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011856f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011857 typval_T *argvars;
11858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011860 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861}
11862
Bram Moolenaar33570922005-01-25 22:26:29 +000011863static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864
11865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011866find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011867 typval_T *argvars;
11868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869 int type;
11870{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011871 char_u *str = NULL;
11872 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 char_u *pat;
11874 regmatch_T regmatch;
11875 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011876 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 char_u *save_cpo;
11878 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011879 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011880 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011881 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011882 list_T *l = NULL;
11883 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011884 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011885 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886
11887 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11888 save_cpo = p_cpo;
11889 p_cpo = (char_u *)"";
11890
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011891 rettv->vval.v_number = -1;
11892 if (type == 3)
11893 {
11894 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011895 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011896 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011897 }
11898 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011900 rettv->v_type = VAR_STRING;
11901 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011904 if (argvars[0].v_type == VAR_LIST)
11905 {
11906 if ((l = argvars[0].vval.v_list) == NULL)
11907 goto theend;
11908 li = l->lv_first;
11909 }
11910 else
11911 expr = str = get_tv_string(&argvars[0]);
11912
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011913 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11914 if (pat == NULL)
11915 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011916
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011917 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011919 int error = FALSE;
11920
11921 start = get_tv_number_chk(&argvars[2], &error);
11922 if (error)
11923 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011924 if (l != NULL)
11925 {
11926 li = list_find(l, start);
11927 if (li == NULL)
11928 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011929 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011930 }
11931 else
11932 {
11933 if (start < 0)
11934 start = 0;
11935 if (start > (long)STRLEN(str))
11936 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011937 /* When "count" argument is there ignore matches before "start",
11938 * otherwise skip part of the string. Differs when pattern is "^"
11939 * or "\<". */
11940 if (argvars[3].v_type != VAR_UNKNOWN)
11941 startcol = start;
11942 else
11943 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011944 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011945
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011946 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011947 nth = get_tv_number_chk(&argvars[3], &error);
11948 if (error)
11949 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 }
11951
11952 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11953 if (regmatch.regprog != NULL)
11954 {
11955 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011956
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011957 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011958 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011959 if (l != NULL)
11960 {
11961 if (li == NULL)
11962 {
11963 match = FALSE;
11964 break;
11965 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011966 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011967 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011968 if (str == NULL)
11969 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011970 }
11971
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011972 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011973
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011974 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011975 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011976 if (l == NULL && !match)
11977 break;
11978
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011979 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011980 if (l != NULL)
11981 {
11982 li = li->li_next;
11983 ++idx;
11984 }
11985 else
11986 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011987#ifdef FEAT_MBYTE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011988 startcol = regmatch.startp[0]
11989 + (*mb_ptr2len)(regmatch.startp[0]) - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011990#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011991 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011992#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011993 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011994 }
11995
11996 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011998 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011999 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012000 int i;
12001
12002 /* return list with matched string and submatches */
12003 for (i = 0; i < NSUBEXP; ++i)
12004 {
12005 if (regmatch.endp[i] == NULL)
12006 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012007 if (list_append_string(rettv->vval.v_list,
12008 regmatch.startp[i],
12009 (int)(regmatch.endp[i] - regmatch.startp[i]))
12010 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012011 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012012 }
12013 }
12014 else if (type == 2)
12015 {
12016 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012017 if (l != NULL)
12018 copy_tv(&li->li_tv, rettv);
12019 else
12020 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012022 }
12023 else if (l != NULL)
12024 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025 else
12026 {
12027 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 (varnumber_T)(regmatch.startp[0] - str);
12030 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 }
12035 }
12036 vim_free(regmatch.regprog);
12037 }
12038
12039theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012040 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041 p_cpo = save_cpo;
12042}
12043
12044/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012045 * "match()" function
12046 */
12047 static void
12048f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012049 typval_T *argvars;
12050 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012051{
12052 find_some_match(argvars, rettv, 1);
12053}
12054
12055/*
12056 * "matchend()" function
12057 */
12058 static void
12059f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012060 typval_T *argvars;
12061 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012062{
12063 find_some_match(argvars, rettv, 0);
12064}
12065
12066/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012067 * "matchlist()" function
12068 */
12069 static void
12070f_matchlist(argvars, rettv)
12071 typval_T *argvars;
12072 typval_T *rettv;
12073{
12074 find_some_match(argvars, rettv, 3);
12075}
12076
12077/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012078 * "matchstr()" function
12079 */
12080 static void
12081f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012082 typval_T *argvars;
12083 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012084{
12085 find_some_match(argvars, rettv, 2);
12086}
12087
Bram Moolenaar33570922005-01-25 22:26:29 +000012088static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012089
12090 static void
12091max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012092 typval_T *argvars;
12093 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012094 int domax;
12095{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012096 long n = 0;
12097 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012098 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012099
12100 if (argvars[0].v_type == VAR_LIST)
12101 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012102 list_T *l;
12103 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012104
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012105 l = argvars[0].vval.v_list;
12106 if (l != NULL)
12107 {
12108 li = l->lv_first;
12109 if (li != NULL)
12110 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012111 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012112 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012113 {
12114 li = li->li_next;
12115 if (li == NULL)
12116 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012117 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012118 if (domax ? i > n : i < n)
12119 n = i;
12120 }
12121 }
12122 }
12123 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012124 else if (argvars[0].v_type == VAR_DICT)
12125 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012126 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012127 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012128 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012129 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012130
12131 d = argvars[0].vval.v_dict;
12132 if (d != NULL)
12133 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012134 todo = d->dv_hashtab.ht_used;
12135 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012136 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012137 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012138 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012139 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012140 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012141 if (first)
12142 {
12143 n = i;
12144 first = FALSE;
12145 }
12146 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012147 n = i;
12148 }
12149 }
12150 }
12151 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012152 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012153 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012154 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012155}
12156
12157/*
12158 * "max()" function
12159 */
12160 static void
12161f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012162 typval_T *argvars;
12163 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012164{
12165 max_min(argvars, rettv, TRUE);
12166}
12167
12168/*
12169 * "min()" function
12170 */
12171 static void
12172f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012173 typval_T *argvars;
12174 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012175{
12176 max_min(argvars, rettv, FALSE);
12177}
12178
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012179static int mkdir_recurse __ARGS((char_u *dir, int prot));
12180
12181/*
12182 * Create the directory in which "dir" is located, and higher levels when
12183 * needed.
12184 */
12185 static int
12186mkdir_recurse(dir, prot)
12187 char_u *dir;
12188 int prot;
12189{
12190 char_u *p;
12191 char_u *updir;
12192 int r = FAIL;
12193
12194 /* Get end of directory name in "dir".
12195 * We're done when it's "/" or "c:/". */
12196 p = gettail_sep(dir);
12197 if (p <= get_past_head(dir))
12198 return OK;
12199
12200 /* If the directory exists we're done. Otherwise: create it.*/
12201 updir = vim_strnsave(dir, (int)(p - dir));
12202 if (updir == NULL)
12203 return FAIL;
12204 if (mch_isdir(updir))
12205 r = OK;
12206 else if (mkdir_recurse(updir, prot) == OK)
12207 r = vim_mkdir_emsg(updir, prot);
12208 vim_free(updir);
12209 return r;
12210}
12211
12212#ifdef vim_mkdir
12213/*
12214 * "mkdir()" function
12215 */
12216 static void
12217f_mkdir(argvars, rettv)
12218 typval_T *argvars;
12219 typval_T *rettv;
12220{
12221 char_u *dir;
12222 char_u buf[NUMBUFLEN];
12223 int prot = 0755;
12224
12225 rettv->vval.v_number = FAIL;
12226 if (check_restricted() || check_secure())
12227 return;
12228
12229 dir = get_tv_string_buf(&argvars[0], buf);
12230 if (argvars[1].v_type != VAR_UNKNOWN)
12231 {
12232 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012233 prot = get_tv_number_chk(&argvars[2], NULL);
12234 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012235 mkdir_recurse(dir, prot);
12236 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012237 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012238}
12239#endif
12240
Bram Moolenaar0d660222005-01-07 21:51:51 +000012241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242 * "mode()" function
12243 */
12244/*ARGSUSED*/
12245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012246f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012247 typval_T *argvars;
12248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249{
12250 char_u buf[2];
12251
12252#ifdef FEAT_VISUAL
12253 if (VIsual_active)
12254 {
12255 if (VIsual_select)
12256 buf[0] = VIsual_mode + 's' - 'v';
12257 else
12258 buf[0] = VIsual_mode;
12259 }
12260 else
12261#endif
12262 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12263 buf[0] = 'r';
12264 else if (State & INSERT)
12265 {
12266 if (State & REPLACE_FLAG)
12267 buf[0] = 'R';
12268 else
12269 buf[0] = 'i';
12270 }
12271 else if (State & CMDLINE)
12272 buf[0] = 'c';
12273 else
12274 buf[0] = 'n';
12275
12276 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012277 rettv->vval.v_string = vim_strsave(buf);
12278 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279}
12280
12281/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012282 * "nextnonblank()" function
12283 */
12284 static void
12285f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012286 typval_T *argvars;
12287 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012288{
12289 linenr_T lnum;
12290
12291 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012293 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012294 {
12295 lnum = 0;
12296 break;
12297 }
12298 if (*skipwhite(ml_get(lnum)) != NUL)
12299 break;
12300 }
12301 rettv->vval.v_number = lnum;
12302}
12303
12304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305 * "nr2char()" function
12306 */
12307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012308f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012309 typval_T *argvars;
12310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311{
12312 char_u buf[NUMBUFLEN];
12313
12314#ifdef FEAT_MBYTE
12315 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012316 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317 else
12318#endif
12319 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012320 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321 buf[1] = NUL;
12322 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012323 rettv->v_type = VAR_STRING;
12324 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012325}
12326
12327/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012328 * "prevnonblank()" function
12329 */
12330 static void
12331f_prevnonblank(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 lnum = get_tv_lnum(argvars);
12338 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12339 lnum = 0;
12340 else
12341 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12342 --lnum;
12343 rettv->vval.v_number = lnum;
12344}
12345
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012346#ifdef HAVE_STDARG_H
12347/* This dummy va_list is here because:
12348 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12349 * - locally in the function results in a "used before set" warning
12350 * - using va_start() to initialize it gives "function with fixed args" error */
12351static va_list ap;
12352#endif
12353
Bram Moolenaar8c711452005-01-14 21:53:12 +000012354/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012355 * "printf()" function
12356 */
12357 static void
12358f_printf(argvars, rettv)
12359 typval_T *argvars;
12360 typval_T *rettv;
12361{
12362 rettv->v_type = VAR_STRING;
12363 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012364#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012365 {
12366 char_u buf[NUMBUFLEN];
12367 int len;
12368 char_u *s;
12369 int saved_did_emsg = did_emsg;
12370 char *fmt;
12371
12372 /* Get the required length, allocate the buffer and do it for real. */
12373 did_emsg = FALSE;
12374 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012375 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012376 if (!did_emsg)
12377 {
12378 s = alloc(len + 1);
12379 if (s != NULL)
12380 {
12381 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012382 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012383 }
12384 }
12385 did_emsg |= saved_did_emsg;
12386 }
12387#endif
12388}
12389
12390/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012391 * "pumvisible()" function
12392 */
12393/*ARGSUSED*/
12394 static void
12395f_pumvisible(argvars, rettv)
12396 typval_T *argvars;
12397 typval_T *rettv;
12398{
12399 rettv->vval.v_number = 0;
12400#ifdef FEAT_INS_EXPAND
12401 if (pum_visible())
12402 rettv->vval.v_number = 1;
12403#endif
12404}
12405
12406/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012407 * "range()" function
12408 */
12409 static void
12410f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012411 typval_T *argvars;
12412 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012413{
12414 long start;
12415 long end;
12416 long stride = 1;
12417 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012418 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012420 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012421 if (argvars[1].v_type == VAR_UNKNOWN)
12422 {
12423 end = start - 1;
12424 start = 0;
12425 }
12426 else
12427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012428 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012429 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012430 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012431 }
12432
12433 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012434 if (error)
12435 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012436 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012437 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012438 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012439 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012440 else
12441 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012442 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012443 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012444 if (list_append_number(rettv->vval.v_list,
12445 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012446 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012447 }
12448}
12449
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012450/*
12451 * "readfile()" function
12452 */
12453 static void
12454f_readfile(argvars, rettv)
12455 typval_T *argvars;
12456 typval_T *rettv;
12457{
12458 int binary = FALSE;
12459 char_u *fname;
12460 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012461 listitem_T *li;
12462#define FREAD_SIZE 200 /* optimized for text lines */
12463 char_u buf[FREAD_SIZE];
12464 int readlen; /* size of last fread() */
12465 int buflen; /* nr of valid chars in buf[] */
12466 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12467 int tolist; /* first byte in buf[] still to be put in list */
12468 int chop; /* how many CR to chop off */
12469 char_u *prev = NULL; /* previously read bytes, if any */
12470 int prevlen = 0; /* length of "prev" if not NULL */
12471 char_u *s;
12472 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012473 long maxline = MAXLNUM;
12474 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012475
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012476 if (argvars[1].v_type != VAR_UNKNOWN)
12477 {
12478 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12479 binary = TRUE;
12480 if (argvars[2].v_type != VAR_UNKNOWN)
12481 maxline = get_tv_number(&argvars[2]);
12482 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012483
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012484 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012485 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012486
12487 /* Always open the file in binary mode, library functions have a mind of
12488 * their own about CR-LF conversion. */
12489 fname = get_tv_string(&argvars[0]);
12490 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12491 {
12492 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12493 return;
12494 }
12495
12496 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012497 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012498 {
12499 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12500 buflen = filtd + readlen;
12501 tolist = 0;
12502 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12503 {
12504 if (buf[filtd] == '\n' || readlen <= 0)
12505 {
12506 /* Only when in binary mode add an empty list item when the
12507 * last line ends in a '\n'. */
12508 if (!binary && readlen == 0 && filtd == 0)
12509 break;
12510
12511 /* Found end-of-line or end-of-file: add a text line to the
12512 * list. */
12513 chop = 0;
12514 if (!binary)
12515 while (filtd - chop - 1 >= tolist
12516 && buf[filtd - chop - 1] == '\r')
12517 ++chop;
12518 len = filtd - tolist - chop;
12519 if (prev == NULL)
12520 s = vim_strnsave(buf + tolist, len);
12521 else
12522 {
12523 s = alloc((unsigned)(prevlen + len + 1));
12524 if (s != NULL)
12525 {
12526 mch_memmove(s, prev, prevlen);
12527 vim_free(prev);
12528 prev = NULL;
12529 mch_memmove(s + prevlen, buf + tolist, len);
12530 s[prevlen + len] = NUL;
12531 }
12532 }
12533 tolist = filtd + 1;
12534
12535 li = listitem_alloc();
12536 if (li == NULL)
12537 {
12538 vim_free(s);
12539 break;
12540 }
12541 li->li_tv.v_type = VAR_STRING;
12542 li->li_tv.v_lock = 0;
12543 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012544 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012545
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012546 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012547 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012548 if (readlen <= 0)
12549 break;
12550 }
12551 else if (buf[filtd] == NUL)
12552 buf[filtd] = '\n';
12553 }
12554 if (readlen <= 0)
12555 break;
12556
12557 if (tolist == 0)
12558 {
12559 /* "buf" is full, need to move text to an allocated buffer */
12560 if (prev == NULL)
12561 {
12562 prev = vim_strnsave(buf, buflen);
12563 prevlen = buflen;
12564 }
12565 else
12566 {
12567 s = alloc((unsigned)(prevlen + buflen));
12568 if (s != NULL)
12569 {
12570 mch_memmove(s, prev, prevlen);
12571 mch_memmove(s + prevlen, buf, buflen);
12572 vim_free(prev);
12573 prev = s;
12574 prevlen += buflen;
12575 }
12576 }
12577 filtd = 0;
12578 }
12579 else
12580 {
12581 mch_memmove(buf, buf + tolist, buflen - tolist);
12582 filtd -= tolist;
12583 }
12584 }
12585
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012586 /*
12587 * For a negative line count use only the lines at the end of the file,
12588 * free the rest.
12589 */
12590 if (maxline < 0)
12591 while (cnt > -maxline)
12592 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012593 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012594 --cnt;
12595 }
12596
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012597 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012598 fclose(fd);
12599}
12600
12601
Bram Moolenaar0d660222005-01-07 21:51:51 +000012602#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12603static void make_connection __ARGS((void));
12604static int check_connection __ARGS((void));
12605
12606 static void
12607make_connection()
12608{
12609 if (X_DISPLAY == NULL
12610# ifdef FEAT_GUI
12611 && !gui.in_use
12612# endif
12613 )
12614 {
12615 x_force_connect = TRUE;
12616 setup_term_clip();
12617 x_force_connect = FALSE;
12618 }
12619}
12620
12621 static int
12622check_connection()
12623{
12624 make_connection();
12625 if (X_DISPLAY == NULL)
12626 {
12627 EMSG(_("E240: No connection to Vim server"));
12628 return FAIL;
12629 }
12630 return OK;
12631}
12632#endif
12633
12634#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012635static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012636
12637 static void
12638remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012639 typval_T *argvars;
12640 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012641 int expr;
12642{
12643 char_u *server_name;
12644 char_u *keys;
12645 char_u *r = NULL;
12646 char_u buf[NUMBUFLEN];
12647# ifdef WIN32
12648 HWND w;
12649# else
12650 Window w;
12651# endif
12652
12653 if (check_restricted() || check_secure())
12654 return;
12655
12656# ifdef FEAT_X11
12657 if (check_connection() == FAIL)
12658 return;
12659# endif
12660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012661 server_name = get_tv_string_chk(&argvars[0]);
12662 if (server_name == NULL)
12663 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012664 keys = get_tv_string_buf(&argvars[1], buf);
12665# ifdef WIN32
12666 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12667# else
12668 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12669 < 0)
12670# endif
12671 {
12672 if (r != NULL)
12673 EMSG(r); /* sending worked but evaluation failed */
12674 else
12675 EMSG2(_("E241: Unable to send to %s"), server_name);
12676 return;
12677 }
12678
12679 rettv->vval.v_string = r;
12680
12681 if (argvars[2].v_type != VAR_UNKNOWN)
12682 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012683 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012684 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012685 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012686
12687 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012688 v.di_tv.v_type = VAR_STRING;
12689 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012690 idvar = get_tv_string_chk(&argvars[2]);
12691 if (idvar != NULL)
12692 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012693 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012694 }
12695}
12696#endif
12697
12698/*
12699 * "remote_expr()" function
12700 */
12701/*ARGSUSED*/
12702 static void
12703f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012704 typval_T *argvars;
12705 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012706{
12707 rettv->v_type = VAR_STRING;
12708 rettv->vval.v_string = NULL;
12709#ifdef FEAT_CLIENTSERVER
12710 remote_common(argvars, rettv, TRUE);
12711#endif
12712}
12713
12714/*
12715 * "remote_foreground()" function
12716 */
12717/*ARGSUSED*/
12718 static void
12719f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012720 typval_T *argvars;
12721 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012722{
12723 rettv->vval.v_number = 0;
12724#ifdef FEAT_CLIENTSERVER
12725# ifdef WIN32
12726 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012727 {
12728 char_u *server_name = get_tv_string_chk(&argvars[0]);
12729
12730 if (server_name != NULL)
12731 serverForeground(server_name);
12732 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012733# else
12734 /* Send a foreground() expression to the server. */
12735 argvars[1].v_type = VAR_STRING;
12736 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12737 argvars[2].v_type = VAR_UNKNOWN;
12738 remote_common(argvars, rettv, TRUE);
12739 vim_free(argvars[1].vval.v_string);
12740# endif
12741#endif
12742}
12743
12744/*ARGSUSED*/
12745 static void
12746f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012747 typval_T *argvars;
12748 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012749{
12750#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012751 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012752 char_u *s = NULL;
12753# ifdef WIN32
12754 int n = 0;
12755# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012756 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012757
12758 if (check_restricted() || check_secure())
12759 {
12760 rettv->vval.v_number = -1;
12761 return;
12762 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012763 serverid = get_tv_string_chk(&argvars[0]);
12764 if (serverid == NULL)
12765 {
12766 rettv->vval.v_number = -1;
12767 return; /* type error; errmsg already given */
12768 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012769# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012770 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012771 if (n == 0)
12772 rettv->vval.v_number = -1;
12773 else
12774 {
12775 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12776 rettv->vval.v_number = (s != NULL);
12777 }
12778# else
12779 rettv->vval.v_number = 0;
12780 if (check_connection() == FAIL)
12781 return;
12782
12783 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012784 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012785# endif
12786
12787 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012789 char_u *retvar;
12790
Bram Moolenaar33570922005-01-25 22:26:29 +000012791 v.di_tv.v_type = VAR_STRING;
12792 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012793 retvar = get_tv_string_chk(&argvars[1]);
12794 if (retvar != NULL)
12795 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012796 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012797 }
12798#else
12799 rettv->vval.v_number = -1;
12800#endif
12801}
12802
12803/*ARGSUSED*/
12804 static void
12805f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012806 typval_T *argvars;
12807 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012808{
12809 char_u *r = NULL;
12810
12811#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012812 char_u *serverid = get_tv_string_chk(&argvars[0]);
12813
12814 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012815 {
12816# ifdef WIN32
12817 /* The server's HWND is encoded in the 'id' parameter */
12818 int n = 0;
12819
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012820 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012821 if (n != 0)
12822 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12823 if (r == NULL)
12824# else
12825 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012826 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012827# endif
12828 EMSG(_("E277: Unable to read a server reply"));
12829 }
12830#endif
12831 rettv->v_type = VAR_STRING;
12832 rettv->vval.v_string = r;
12833}
12834
12835/*
12836 * "remote_send()" function
12837 */
12838/*ARGSUSED*/
12839 static void
12840f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012841 typval_T *argvars;
12842 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012843{
12844 rettv->v_type = VAR_STRING;
12845 rettv->vval.v_string = NULL;
12846#ifdef FEAT_CLIENTSERVER
12847 remote_common(argvars, rettv, FALSE);
12848#endif
12849}
12850
12851/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012852 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012853 */
12854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012856 typval_T *argvars;
12857 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012858{
Bram Moolenaar33570922005-01-25 22:26:29 +000012859 list_T *l;
12860 listitem_T *item, *item2;
12861 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012862 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012863 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012864 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012865 dict_T *d;
12866 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012867
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012868 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012869 if (argvars[0].v_type == VAR_DICT)
12870 {
12871 if (argvars[2].v_type != VAR_UNKNOWN)
12872 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012873 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012874 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012876 key = get_tv_string_chk(&argvars[1]);
12877 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012879 di = dict_find(d, key, -1);
12880 if (di == NULL)
12881 EMSG2(_(e_dictkey), key);
12882 else
12883 {
12884 *rettv = di->di_tv;
12885 init_tv(&di->di_tv);
12886 dictitem_remove(d, di);
12887 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012888 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012889 }
12890 }
12891 else if (argvars[0].v_type != VAR_LIST)
12892 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012893 else if ((l = argvars[0].vval.v_list) != NULL
12894 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012895 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012896 int error = FALSE;
12897
12898 idx = get_tv_number_chk(&argvars[1], &error);
12899 if (error)
12900 ; /* type error: do nothing, errmsg already given */
12901 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012902 EMSGN(_(e_listidx), idx);
12903 else
12904 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012905 if (argvars[2].v_type == VAR_UNKNOWN)
12906 {
12907 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012908 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012909 *rettv = item->li_tv;
12910 vim_free(item);
12911 }
12912 else
12913 {
12914 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012915 end = get_tv_number_chk(&argvars[2], &error);
12916 if (error)
12917 ; /* type error: do nothing */
12918 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012919 EMSGN(_(e_listidx), end);
12920 else
12921 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012922 int cnt = 0;
12923
12924 for (li = item; li != NULL; li = li->li_next)
12925 {
12926 ++cnt;
12927 if (li == item2)
12928 break;
12929 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012930 if (li == NULL) /* didn't find "item2" after "item" */
12931 EMSG(_(e_invrange));
12932 else
12933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012934 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012935 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012936 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012937 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012938 l->lv_first = item;
12939 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012940 item->li_prev = NULL;
12941 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012942 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012943 }
12944 }
12945 }
12946 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012947 }
12948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949}
12950
12951/*
12952 * "rename({from}, {to})" function
12953 */
12954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012955f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012956 typval_T *argvars;
12957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958{
12959 char_u buf[NUMBUFLEN];
12960
12961 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012962 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012964 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12965 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966}
12967
12968/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012969 * "repeat()" function
12970 */
12971/*ARGSUSED*/
12972 static void
12973f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012974 typval_T *argvars;
12975 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012976{
12977 char_u *p;
12978 int n;
12979 int slen;
12980 int len;
12981 char_u *r;
12982 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012983
12984 n = get_tv_number(&argvars[1]);
12985 if (argvars[0].v_type == VAR_LIST)
12986 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012987 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012988 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012989 if (list_extend(rettv->vval.v_list,
12990 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012991 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012992 }
12993 else
12994 {
12995 p = get_tv_string(&argvars[0]);
12996 rettv->v_type = VAR_STRING;
12997 rettv->vval.v_string = NULL;
12998
12999 slen = (int)STRLEN(p);
13000 len = slen * n;
13001 if (len <= 0)
13002 return;
13003
13004 r = alloc(len + 1);
13005 if (r != NULL)
13006 {
13007 for (i = 0; i < n; i++)
13008 mch_memmove(r + i * slen, p, (size_t)slen);
13009 r[len] = NUL;
13010 }
13011
13012 rettv->vval.v_string = r;
13013 }
13014}
13015
13016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017 * "resolve()" function
13018 */
13019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013020f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013021 typval_T *argvars;
13022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023{
13024 char_u *p;
13025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013026 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027#ifdef FEAT_SHORTCUT
13028 {
13029 char_u *v = NULL;
13030
13031 v = mch_resolve_shortcut(p);
13032 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013033 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013035 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036 }
13037#else
13038# ifdef HAVE_READLINK
13039 {
13040 char_u buf[MAXPATHL + 1];
13041 char_u *cpy;
13042 int len;
13043 char_u *remain = NULL;
13044 char_u *q;
13045 int is_relative_to_current = FALSE;
13046 int has_trailing_pathsep = FALSE;
13047 int limit = 100;
13048
13049 p = vim_strsave(p);
13050
13051 if (p[0] == '.' && (vim_ispathsep(p[1])
13052 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13053 is_relative_to_current = TRUE;
13054
13055 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013056 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057 has_trailing_pathsep = TRUE;
13058
13059 q = getnextcomp(p);
13060 if (*q != NUL)
13061 {
13062 /* Separate the first path component in "p", and keep the
13063 * remainder (beginning with the path separator). */
13064 remain = vim_strsave(q - 1);
13065 q[-1] = NUL;
13066 }
13067
13068 for (;;)
13069 {
13070 for (;;)
13071 {
13072 len = readlink((char *)p, (char *)buf, MAXPATHL);
13073 if (len <= 0)
13074 break;
13075 buf[len] = NUL;
13076
13077 if (limit-- == 0)
13078 {
13079 vim_free(p);
13080 vim_free(remain);
13081 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013082 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 goto fail;
13084 }
13085
13086 /* Ensure that the result will have a trailing path separator
13087 * if the argument has one. */
13088 if (remain == NULL && has_trailing_pathsep)
13089 add_pathsep(buf);
13090
13091 /* Separate the first path component in the link value and
13092 * concatenate the remainders. */
13093 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13094 if (*q != NUL)
13095 {
13096 if (remain == NULL)
13097 remain = vim_strsave(q - 1);
13098 else
13099 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013100 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013101 if (cpy != NULL)
13102 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103 vim_free(remain);
13104 remain = cpy;
13105 }
13106 }
13107 q[-1] = NUL;
13108 }
13109
13110 q = gettail(p);
13111 if (q > p && *q == NUL)
13112 {
13113 /* Ignore trailing path separator. */
13114 q[-1] = NUL;
13115 q = gettail(p);
13116 }
13117 if (q > p && !mch_isFullName(buf))
13118 {
13119 /* symlink is relative to directory of argument */
13120 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13121 if (cpy != NULL)
13122 {
13123 STRCPY(cpy, p);
13124 STRCPY(gettail(cpy), buf);
13125 vim_free(p);
13126 p = cpy;
13127 }
13128 }
13129 else
13130 {
13131 vim_free(p);
13132 p = vim_strsave(buf);
13133 }
13134 }
13135
13136 if (remain == NULL)
13137 break;
13138
13139 /* Append the first path component of "remain" to "p". */
13140 q = getnextcomp(remain + 1);
13141 len = q - remain - (*q != NUL);
13142 cpy = vim_strnsave(p, STRLEN(p) + len);
13143 if (cpy != NULL)
13144 {
13145 STRNCAT(cpy, remain, len);
13146 vim_free(p);
13147 p = cpy;
13148 }
13149 /* Shorten "remain". */
13150 if (*q != NUL)
13151 STRCPY(remain, q - 1);
13152 else
13153 {
13154 vim_free(remain);
13155 remain = NULL;
13156 }
13157 }
13158
13159 /* If the result is a relative path name, make it explicitly relative to
13160 * the current directory if and only if the argument had this form. */
13161 if (!vim_ispathsep(*p))
13162 {
13163 if (is_relative_to_current
13164 && *p != NUL
13165 && !(p[0] == '.'
13166 && (p[1] == NUL
13167 || vim_ispathsep(p[1])
13168 || (p[1] == '.'
13169 && (p[2] == NUL
13170 || vim_ispathsep(p[2]))))))
13171 {
13172 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013173 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 if (cpy != NULL)
13175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 vim_free(p);
13177 p = cpy;
13178 }
13179 }
13180 else if (!is_relative_to_current)
13181 {
13182 /* Strip leading "./". */
13183 q = p;
13184 while (q[0] == '.' && vim_ispathsep(q[1]))
13185 q += 2;
13186 if (q > p)
13187 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13188 }
13189 }
13190
13191 /* Ensure that the result will have no trailing path separator
13192 * if the argument had none. But keep "/" or "//". */
13193 if (!has_trailing_pathsep)
13194 {
13195 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013196 if (after_pathsep(p, q))
13197 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198 }
13199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013200 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201 }
13202# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204# endif
13205#endif
13206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208
13209#ifdef HAVE_READLINK
13210fail:
13211#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013212 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213}
13214
13215/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013216 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 */
13218 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013219f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013220 typval_T *argvars;
13221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222{
Bram Moolenaar33570922005-01-25 22:26:29 +000013223 list_T *l;
13224 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225
Bram Moolenaar0d660222005-01-07 21:51:51 +000013226 rettv->vval.v_number = 0;
13227 if (argvars[0].v_type != VAR_LIST)
13228 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013229 else if ((l = argvars[0].vval.v_list) != NULL
13230 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013231 {
13232 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013233 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013234 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013235 while (li != NULL)
13236 {
13237 ni = li->li_prev;
13238 list_append(l, li);
13239 li = ni;
13240 }
13241 rettv->vval.v_list = l;
13242 rettv->v_type = VAR_LIST;
13243 ++l->lv_refcount;
13244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245}
13246
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013247#define SP_NOMOVE 0x01 /* don't move cursor */
13248#define SP_REPEAT 0x02 /* repeat to find outer pair */
13249#define SP_RETCOUNT 0x04 /* return matchcount */
13250#define SP_SETPCMARK 0x08 /* set previous context mark */
13251#define SP_START 0x10 /* accept match at start position */
13252#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13253#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013254
Bram Moolenaar33570922005-01-25 22:26:29 +000013255static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013256
13257/*
13258 * Get flags for a search function.
13259 * Possibly sets "p_ws".
13260 * Returns BACKWARD, FORWARD or zero (for an error).
13261 */
13262 static int
13263get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013264 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013265 int *flagsp;
13266{
13267 int dir = FORWARD;
13268 char_u *flags;
13269 char_u nbuf[NUMBUFLEN];
13270 int mask;
13271
13272 if (varp->v_type != VAR_UNKNOWN)
13273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013274 flags = get_tv_string_buf_chk(varp, nbuf);
13275 if (flags == NULL)
13276 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013277 while (*flags != NUL)
13278 {
13279 switch (*flags)
13280 {
13281 case 'b': dir = BACKWARD; break;
13282 case 'w': p_ws = TRUE; break;
13283 case 'W': p_ws = FALSE; break;
13284 default: mask = 0;
13285 if (flagsp != NULL)
13286 switch (*flags)
13287 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013288 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013289 case 'e': mask = SP_END; break;
13290 case 'm': mask = SP_RETCOUNT; break;
13291 case 'n': mask = SP_NOMOVE; break;
13292 case 'p': mask = SP_SUBPAT; break;
13293 case 'r': mask = SP_REPEAT; break;
13294 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013295 }
13296 if (mask == 0)
13297 {
13298 EMSG2(_(e_invarg2), flags);
13299 dir = 0;
13300 }
13301 else
13302 *flagsp |= mask;
13303 }
13304 if (dir == 0)
13305 break;
13306 ++flags;
13307 }
13308 }
13309 return dir;
13310}
13311
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013313 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013315 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013316search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013317 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013318 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013319 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013321 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322 char_u *pat;
13323 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013324 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325 int save_p_ws = p_ws;
13326 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013327 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013328 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013329 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013330 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013332 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013333 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013334 if (dir == 0)
13335 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013336 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013337 if (flags & SP_START)
13338 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013339 if (flags & SP_END)
13340 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013341
13342 /* Optional extra argument: line number to stop searching. */
13343 if (argvars[1].v_type != VAR_UNKNOWN
13344 && argvars[2].v_type != VAR_UNKNOWN)
13345 {
13346 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13347 if (lnum_stop < 0)
13348 goto theend;
13349 }
13350
Bram Moolenaar231334e2005-07-25 20:46:57 +000013351 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013352 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013353 * Check to make sure only those flags are set.
13354 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13355 * flags cannot be set. Check for that condition also.
13356 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013357 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013358 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013360 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013361 goto theend;
13362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013364 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013365 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13366 options, RE_SEARCH, (linenr_T)lnum_stop);
13367 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013369 if (flags & SP_SUBPAT)
13370 retval = subpatnum;
13371 else
13372 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013373 if (flags & SP_SETPCMARK)
13374 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013376 if (match_pos != NULL)
13377 {
13378 /* Store the match cursor position */
13379 match_pos->lnum = pos.lnum;
13380 match_pos->col = pos.col + 1;
13381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382 /* "/$" will put the cursor after the end of the line, may need to
13383 * correct that here */
13384 check_cursor();
13385 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013386
13387 /* If 'n' flag is used: restore cursor position. */
13388 if (flags & SP_NOMOVE)
13389 curwin->w_cursor = save_cursor;
13390theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013392
13393 return retval;
13394}
13395
13396/*
13397 * "search()" function
13398 */
13399 static void
13400f_search(argvars, rettv)
13401 typval_T *argvars;
13402 typval_T *rettv;
13403{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013404 int flags = 0;
13405
13406 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407}
13408
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013410 * "searchdecl()" function
13411 */
13412 static void
13413f_searchdecl(argvars, rettv)
13414 typval_T *argvars;
13415 typval_T *rettv;
13416{
13417 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013418 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013419 int error = FALSE;
13420 char_u *name;
13421
13422 rettv->vval.v_number = 1; /* default: FAIL */
13423
13424 name = get_tv_string_chk(&argvars[0]);
13425 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013426 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013427 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013428 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13429 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13430 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013431 if (!error && name != NULL)
13432 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013433 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013434}
13435
13436/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013437 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013439 static int
13440searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013441 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013442 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443{
13444 char_u *spat, *mpat, *epat;
13445 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447 int dir;
13448 int flags = 0;
13449 char_u nbuf1[NUMBUFLEN];
13450 char_u nbuf2[NUMBUFLEN];
13451 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013452 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013453 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013456 spat = get_tv_string_chk(&argvars[0]);
13457 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13458 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13459 if (spat == NULL || mpat == NULL || epat == NULL)
13460 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 /* Handle the optional fourth argument: flags */
13463 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013464 if (dir == 0)
13465 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013466
13467 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013468 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13469 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013470 if ((flags & (SP_END | SP_SUBPAT)) != 0
13471 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013472 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013473 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013474 goto theend;
13475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013477 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013478 if (argvars[3].v_type == VAR_UNKNOWN
13479 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 skip = (char_u *)"";
13481 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013483 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013484 if (argvars[5].v_type != VAR_UNKNOWN)
13485 {
13486 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13487 if (lnum_stop < 0)
13488 goto theend;
13489 }
13490 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013491 if (skip == NULL)
13492 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013494 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13495 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013496
13497theend:
13498 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013499
13500 return retval;
13501}
13502
13503/*
13504 * "searchpair()" function
13505 */
13506 static void
13507f_searchpair(argvars, rettv)
13508 typval_T *argvars;
13509 typval_T *rettv;
13510{
13511 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13512}
13513
13514/*
13515 * "searchpairpos()" function
13516 */
13517 static void
13518f_searchpairpos(argvars, rettv)
13519 typval_T *argvars;
13520 typval_T *rettv;
13521{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013522 pos_T match_pos;
13523 int lnum = 0;
13524 int col = 0;
13525
13526 rettv->vval.v_number = 0;
13527
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013528 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013529 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013530
13531 if (searchpair_cmn(argvars, &match_pos) > 0)
13532 {
13533 lnum = match_pos.lnum;
13534 col = match_pos.col;
13535 }
13536
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013537 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13538 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013539}
13540
13541/*
13542 * Search for a start/middle/end thing.
13543 * Used by searchpair(), see its documentation for the details.
13544 * Returns 0 or -1 for no match,
13545 */
13546 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013547do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013548 char_u *spat; /* start pattern */
13549 char_u *mpat; /* middle pattern */
13550 char_u *epat; /* end pattern */
13551 int dir; /* BACKWARD or FORWARD */
13552 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013553 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013554 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013555 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013556{
13557 char_u *save_cpo;
13558 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13559 long retval = 0;
13560 pos_T pos;
13561 pos_T firstpos;
13562 pos_T foundpos;
13563 pos_T save_cursor;
13564 pos_T save_pos;
13565 int n;
13566 int r;
13567 int nest = 1;
13568 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013569 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013570
13571 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13572 save_cpo = p_cpo;
13573 p_cpo = (char_u *)"";
13574
13575 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13576 * start/middle/end (pat3, for the top pair). */
13577 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13578 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13579 if (pat2 == NULL || pat3 == NULL)
13580 goto theend;
13581 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13582 if (*mpat == NUL)
13583 STRCPY(pat3, pat2);
13584 else
13585 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13586 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013587 if (flags & SP_START)
13588 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013589
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590 save_cursor = curwin->w_cursor;
13591 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013592 clearpos(&firstpos);
13593 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594 pat = pat3;
13595 for (;;)
13596 {
13597 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013598 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13600 /* didn't find it or found the first match again: FAIL */
13601 break;
13602
13603 if (firstpos.lnum == 0)
13604 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013605 if (equalpos(pos, foundpos))
13606 {
13607 /* Found the same position again. Can happen with a pattern that
13608 * has "\zs" at the end and searching backwards. Advance one
13609 * character and try again. */
13610 if (dir == BACKWARD)
13611 decl(&pos);
13612 else
13613 incl(&pos);
13614 }
13615 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616
13617 /* If the skip pattern matches, ignore this match. */
13618 if (*skip != NUL)
13619 {
13620 save_pos = curwin->w_cursor;
13621 curwin->w_cursor = pos;
13622 r = eval_to_bool(skip, &err, NULL, FALSE);
13623 curwin->w_cursor = save_pos;
13624 if (err)
13625 {
13626 /* Evaluating {skip} caused an error, break here. */
13627 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013628 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 break;
13630 }
13631 if (r)
13632 continue;
13633 }
13634
13635 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13636 {
13637 /* Found end when searching backwards or start when searching
13638 * forward: nested pair. */
13639 ++nest;
13640 pat = pat2; /* nested, don't search for middle */
13641 }
13642 else
13643 {
13644 /* Found end when searching forward or start when searching
13645 * backward: end of (nested) pair; or found middle in outer pair. */
13646 if (--nest == 1)
13647 pat = pat3; /* outer level, search for middle */
13648 }
13649
13650 if (nest == 0)
13651 {
13652 /* Found the match: return matchcount or line number. */
13653 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013654 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013656 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013657 if (flags & SP_SETPCMARK)
13658 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 curwin->w_cursor = pos;
13660 if (!(flags & SP_REPEAT))
13661 break;
13662 nest = 1; /* search for next unmatched */
13663 }
13664 }
13665
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013666 if (match_pos != NULL)
13667 {
13668 /* Store the match cursor position */
13669 match_pos->lnum = curwin->w_cursor.lnum;
13670 match_pos->col = curwin->w_cursor.col + 1;
13671 }
13672
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013674 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 curwin->w_cursor = save_cursor;
13676
13677theend:
13678 vim_free(pat2);
13679 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013681
13682 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683}
13684
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013685/*
13686 * "searchpos()" function
13687 */
13688 static void
13689f_searchpos(argvars, rettv)
13690 typval_T *argvars;
13691 typval_T *rettv;
13692{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013693 pos_T match_pos;
13694 int lnum = 0;
13695 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013696 int n;
13697 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013698
13699 rettv->vval.v_number = 0;
13700
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013701 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013702 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013703
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013704 n = search_cmn(argvars, &match_pos, &flags);
13705 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013706 {
13707 lnum = match_pos.lnum;
13708 col = match_pos.col;
13709 }
13710
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013711 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13712 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013713 if (flags & SP_SUBPAT)
13714 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013715}
13716
13717
Bram Moolenaar0d660222005-01-07 21:51:51 +000013718/*ARGSUSED*/
13719 static void
13720f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013721 typval_T *argvars;
13722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013724#ifdef FEAT_CLIENTSERVER
13725 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013726 char_u *server = get_tv_string_chk(&argvars[0]);
13727 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728
Bram Moolenaar0d660222005-01-07 21:51:51 +000013729 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013730 if (server == NULL || reply == NULL)
13731 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013732 if (check_restricted() || check_secure())
13733 return;
13734# ifdef FEAT_X11
13735 if (check_connection() == FAIL)
13736 return;
13737# endif
13738
13739 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013741 EMSG(_("E258: Unable to send to client"));
13742 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013744 rettv->vval.v_number = 0;
13745#else
13746 rettv->vval.v_number = -1;
13747#endif
13748}
13749
13750/*ARGSUSED*/
13751 static void
13752f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013753 typval_T *argvars;
13754 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013755{
13756 char_u *r = NULL;
13757
13758#ifdef FEAT_CLIENTSERVER
13759# ifdef WIN32
13760 r = serverGetVimNames();
13761# else
13762 make_connection();
13763 if (X_DISPLAY != NULL)
13764 r = serverGetVimNames(X_DISPLAY);
13765# endif
13766#endif
13767 rettv->v_type = VAR_STRING;
13768 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769}
13770
13771/*
13772 * "setbufvar()" function
13773 */
13774/*ARGSUSED*/
13775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013776f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013777 typval_T *argvars;
13778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779{
13780 buf_T *buf;
13781#ifdef FEAT_AUTOCMD
13782 aco_save_T aco;
13783#else
13784 buf_T *save_curbuf;
13785#endif
13786 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013787 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 char_u nbuf[NUMBUFLEN];
13789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013790 rettv->vval.v_number = 0;
13791
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 if (check_restricted() || check_secure())
13793 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013794 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13795 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013796 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797 varp = &argvars[2];
13798
13799 if (buf != NULL && varname != NULL && varp != NULL)
13800 {
13801 /* set curbuf to be our buf, temporarily */
13802#ifdef FEAT_AUTOCMD
13803 aucmd_prepbuf(&aco, buf);
13804#else
13805 save_curbuf = curbuf;
13806 curbuf = buf;
13807#endif
13808
13809 if (*varname == '&')
13810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013811 long numval;
13812 char_u *strval;
13813 int error = FALSE;
13814
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013816 numval = get_tv_number_chk(varp, &error);
13817 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013818 if (!error && strval != NULL)
13819 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 }
13821 else
13822 {
13823 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13824 if (bufvarname != NULL)
13825 {
13826 STRCPY(bufvarname, "b:");
13827 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013828 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 vim_free(bufvarname);
13830 }
13831 }
13832
13833 /* reset notion of buffer */
13834#ifdef FEAT_AUTOCMD
13835 aucmd_restbuf(&aco);
13836#else
13837 curbuf = save_curbuf;
13838#endif
13839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840}
13841
13842/*
13843 * "setcmdpos()" function
13844 */
13845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013846f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013847 typval_T *argvars;
13848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013850 int pos = (int)get_tv_number(&argvars[0]) - 1;
13851
13852 if (pos >= 0)
13853 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854}
13855
13856/*
13857 * "setline()" function
13858 */
13859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013860f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013861 typval_T *argvars;
13862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863{
13864 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013865 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013866 list_T *l = NULL;
13867 listitem_T *li = NULL;
13868 long added = 0;
13869 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013871 lnum = get_tv_lnum(&argvars[0]);
13872 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013874 l = argvars[1].vval.v_list;
13875 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013877 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013878 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013879
13880 rettv->vval.v_number = 0; /* OK */
13881 for (;;)
13882 {
13883 if (l != NULL)
13884 {
13885 /* list argument, get next string */
13886 if (li == NULL)
13887 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013888 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013889 li = li->li_next;
13890 }
13891
13892 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013893 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013894 break;
13895 if (lnum <= curbuf->b_ml.ml_line_count)
13896 {
13897 /* existing line, replace it */
13898 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13899 {
13900 changed_bytes(lnum, 0);
13901 check_cursor_col();
13902 rettv->vval.v_number = 0; /* OK */
13903 }
13904 }
13905 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13906 {
13907 /* lnum is one past the last line, append the line */
13908 ++added;
13909 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13910 rettv->vval.v_number = 0; /* OK */
13911 }
13912
13913 if (l == NULL) /* only one string argument */
13914 break;
13915 ++lnum;
13916 }
13917
13918 if (added > 0)
13919 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920}
13921
13922/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013923 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013924 */
13925/*ARGSUSED*/
13926 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013927set_qf_ll_list(wp, list_arg, action_arg, rettv)
13928 win_T *wp;
13929 typval_T *list_arg;
13930 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013931 typval_T *rettv;
13932{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013933#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013934 char_u *act;
13935 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013936#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013937
Bram Moolenaar2641f772005-03-25 21:58:17 +000013938 rettv->vval.v_number = -1;
13939
13940#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013941 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013942 EMSG(_(e_listreq));
13943 else
13944 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013945 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013946
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013947 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013948 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013949 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013950 if (act == NULL)
13951 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013952 if (*act == 'a' || *act == 'r')
13953 action = *act;
13954 }
13955
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013956 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013957 rettv->vval.v_number = 0;
13958 }
13959#endif
13960}
13961
13962/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013963 * "setloclist()" function
13964 */
13965/*ARGSUSED*/
13966 static void
13967f_setloclist(argvars, rettv)
13968 typval_T *argvars;
13969 typval_T *rettv;
13970{
13971 win_T *win;
13972
13973 rettv->vval.v_number = -1;
13974
13975 win = find_win_by_nr(&argvars[0]);
13976 if (win != NULL)
13977 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13978}
13979
13980/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013981 * "setpos()" function
13982 */
13983/*ARGSUSED*/
13984 static void
13985f_setpos(argvars, rettv)
13986 typval_T *argvars;
13987 typval_T *rettv;
13988{
13989 pos_T pos;
13990 int fnum;
13991 char_u *name;
13992
13993 name = get_tv_string_chk(argvars);
13994 if (name != NULL)
13995 {
13996 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
13997 {
13998 --pos.col;
13999 if (name[0] == '.') /* cursor */
14000 {
14001 if (fnum == curbuf->b_fnum)
14002 {
14003 curwin->w_cursor = pos;
14004 check_cursor();
14005 }
14006 else
14007 EMSG(_(e_invarg));
14008 }
14009 else if (name[0] == '\'') /* mark */
14010 (void)setmark_pos(name[1], &pos, fnum);
14011 else
14012 EMSG(_(e_invarg));
14013 }
14014 }
14015}
14016
14017/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014018 * "setqflist()" function
14019 */
14020/*ARGSUSED*/
14021 static void
14022f_setqflist(argvars, rettv)
14023 typval_T *argvars;
14024 typval_T *rettv;
14025{
14026 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14027}
14028
14029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030 * "setreg()" function
14031 */
14032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014033f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014034 typval_T *argvars;
14035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036{
14037 int regname;
14038 char_u *strregname;
14039 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014040 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041 int append;
14042 char_u yank_type;
14043 long block_len;
14044
14045 block_len = -1;
14046 yank_type = MAUTO;
14047 append = FALSE;
14048
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014049 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014050 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014052 if (strregname == NULL)
14053 return; /* type error; errmsg already given */
14054 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 if (regname == 0 || regname == '@')
14056 regname = '"';
14057 else if (regname == '=')
14058 return;
14059
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014060 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014062 stropt = get_tv_string_chk(&argvars[2]);
14063 if (stropt == NULL)
14064 return; /* type error */
14065 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066 switch (*stropt)
14067 {
14068 case 'a': case 'A': /* append */
14069 append = TRUE;
14070 break;
14071 case 'v': case 'c': /* character-wise selection */
14072 yank_type = MCHAR;
14073 break;
14074 case 'V': case 'l': /* line-wise selection */
14075 yank_type = MLINE;
14076 break;
14077#ifdef FEAT_VISUAL
14078 case 'b': case Ctrl_V: /* block-wise selection */
14079 yank_type = MBLOCK;
14080 if (VIM_ISDIGIT(stropt[1]))
14081 {
14082 ++stropt;
14083 block_len = getdigits(&stropt) - 1;
14084 --stropt;
14085 }
14086 break;
14087#endif
14088 }
14089 }
14090
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014091 strval = get_tv_string_chk(&argvars[1]);
14092 if (strval != NULL)
14093 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014095 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096}
14097
14098
14099/*
14100 * "setwinvar(expr)" function
14101 */
14102/*ARGSUSED*/
14103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014104f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014105 typval_T *argvars;
14106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107{
14108 win_T *win;
14109#ifdef FEAT_WINDOWS
14110 win_T *save_curwin;
14111#endif
14112 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014113 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114 char_u nbuf[NUMBUFLEN];
14115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014116 rettv->vval.v_number = 0;
14117
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118 if (check_restricted() || check_secure())
14119 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014121 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 varp = &argvars[2];
14123
14124 if (win != NULL && varname != NULL && varp != NULL)
14125 {
14126#ifdef FEAT_WINDOWS
14127 /* set curwin to be our win, temporarily */
14128 save_curwin = curwin;
14129 curwin = win;
14130 curbuf = curwin->w_buffer;
14131#endif
14132
14133 if (*varname == '&')
14134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014135 long numval;
14136 char_u *strval;
14137 int error = FALSE;
14138
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014140 numval = get_tv_number_chk(varp, &error);
14141 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014142 if (!error && strval != NULL)
14143 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 }
14145 else
14146 {
14147 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14148 if (winvarname != NULL)
14149 {
14150 STRCPY(winvarname, "w:");
14151 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014152 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 vim_free(winvarname);
14154 }
14155 }
14156
14157#ifdef FEAT_WINDOWS
14158 /* Restore current window, if it's still valid (autocomands can make
14159 * it invalid). */
14160 if (win_valid(save_curwin))
14161 {
14162 curwin = save_curwin;
14163 curbuf = curwin->w_buffer;
14164 }
14165#endif
14166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167}
14168
14169/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014170 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171 */
14172 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014173f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014174 typval_T *argvars;
14175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014177 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178
Bram Moolenaar0d660222005-01-07 21:51:51 +000014179 p = get_tv_string(&argvars[0]);
14180 rettv->vval.v_string = vim_strsave(p);
14181 simplify_filename(rettv->vval.v_string); /* simplify in place */
14182 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183}
14184
Bram Moolenaar0d660222005-01-07 21:51:51 +000014185static int
14186#ifdef __BORLANDC__
14187 _RTLENTRYF
14188#endif
14189 item_compare __ARGS((const void *s1, const void *s2));
14190static int
14191#ifdef __BORLANDC__
14192 _RTLENTRYF
14193#endif
14194 item_compare2 __ARGS((const void *s1, const void *s2));
14195
14196static int item_compare_ic;
14197static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014198static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014199#define ITEM_COMPARE_FAIL 999
14200
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014202 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014204 static int
14205#ifdef __BORLANDC__
14206_RTLENTRYF
14207#endif
14208item_compare(s1, s2)
14209 const void *s1;
14210 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014212 char_u *p1, *p2;
14213 char_u *tofree1, *tofree2;
14214 int res;
14215 char_u numbuf1[NUMBUFLEN];
14216 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014218 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14219 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014220 if (item_compare_ic)
14221 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014223 res = STRCMP(p1, p2);
14224 vim_free(tofree1);
14225 vim_free(tofree2);
14226 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227}
14228
14229 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014230#ifdef __BORLANDC__
14231_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014233item_compare2(s1, s2)
14234 const void *s1;
14235 const void *s2;
14236{
14237 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014238 typval_T rettv;
14239 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014240 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014242 /* shortcut after failure in previous call; compare all items equal */
14243 if (item_compare_func_err)
14244 return 0;
14245
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014246 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14247 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014248 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14249 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014250
14251 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14252 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014253 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014254 clear_tv(&argv[0]);
14255 clear_tv(&argv[1]);
14256
14257 if (res == FAIL)
14258 res = ITEM_COMPARE_FAIL;
14259 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014260 /* return value has wrong type */
14261 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14262 if (item_compare_func_err)
14263 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014264 clear_tv(&rettv);
14265 return res;
14266}
14267
14268/*
14269 * "sort({list})" function
14270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014272f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014273 typval_T *argvars;
14274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014275{
Bram Moolenaar33570922005-01-25 22:26:29 +000014276 list_T *l;
14277 listitem_T *li;
14278 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014279 long len;
14280 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281
Bram Moolenaar0d660222005-01-07 21:51:51 +000014282 rettv->vval.v_number = 0;
14283 if (argvars[0].v_type != VAR_LIST)
14284 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 else
14286 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014287 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014288 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014289 return;
14290 rettv->vval.v_list = l;
14291 rettv->v_type = VAR_LIST;
14292 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293
Bram Moolenaar0d660222005-01-07 21:51:51 +000014294 len = list_len(l);
14295 if (len <= 1)
14296 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297
Bram Moolenaar0d660222005-01-07 21:51:51 +000014298 item_compare_ic = FALSE;
14299 item_compare_func = NULL;
14300 if (argvars[1].v_type != VAR_UNKNOWN)
14301 {
14302 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014303 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014304 else
14305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014306 int error = FALSE;
14307
14308 i = get_tv_number_chk(&argvars[1], &error);
14309 if (error)
14310 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014311 if (i == 1)
14312 item_compare_ic = TRUE;
14313 else
14314 item_compare_func = get_tv_string(&argvars[1]);
14315 }
14316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317
Bram Moolenaar0d660222005-01-07 21:51:51 +000014318 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014319 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014320 if (ptrs == NULL)
14321 return;
14322 i = 0;
14323 for (li = l->lv_first; li != NULL; li = li->li_next)
14324 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014326 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014327 /* test the compare function */
14328 if (item_compare_func != NULL
14329 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14330 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014331 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014333 {
14334 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014335 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014336 item_compare_func == NULL ? item_compare : item_compare2);
14337
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014338 if (!item_compare_func_err)
14339 {
14340 /* Clear the List and append the items in the sorted order. */
14341 l->lv_first = l->lv_last = NULL;
14342 l->lv_len = 0;
14343 for (i = 0; i < len; ++i)
14344 list_append(l, ptrs[i]);
14345 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014346 }
14347
14348 vim_free(ptrs);
14349 }
14350}
14351
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014352/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014353 * "soundfold({word})" function
14354 */
14355 static void
14356f_soundfold(argvars, rettv)
14357 typval_T *argvars;
14358 typval_T *rettv;
14359{
14360 char_u *s;
14361
14362 rettv->v_type = VAR_STRING;
14363 s = get_tv_string(&argvars[0]);
14364#ifdef FEAT_SYN_HL
14365 rettv->vval.v_string = eval_soundfold(s);
14366#else
14367 rettv->vval.v_string = vim_strsave(s);
14368#endif
14369}
14370
14371/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014372 * "spellbadword()" function
14373 */
14374/* ARGSUSED */
14375 static void
14376f_spellbadword(argvars, rettv)
14377 typval_T *argvars;
14378 typval_T *rettv;
14379{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014380 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014381 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014382 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014383
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014384 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014385 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014386
14387#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014388 if (argvars[0].v_type == VAR_UNKNOWN)
14389 {
14390 /* Find the start and length of the badly spelled word. */
14391 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14392 if (len != 0)
14393 word = ml_get_cursor();
14394 }
14395 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14396 {
14397 char_u *str = get_tv_string_chk(&argvars[0]);
14398 int capcol = -1;
14399
14400 if (str != NULL)
14401 {
14402 /* Check the argument for spelling. */
14403 while (*str != NUL)
14404 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014405 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014406 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014407 {
14408 word = str;
14409 break;
14410 }
14411 str += len;
14412 }
14413 }
14414 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014415#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014416
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014417 list_append_string(rettv->vval.v_list, word, len);
14418 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014419 attr == HLF_SPB ? "bad" :
14420 attr == HLF_SPR ? "rare" :
14421 attr == HLF_SPL ? "local" :
14422 attr == HLF_SPC ? "caps" :
14423 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014424}
14425
14426/*
14427 * "spellsuggest()" function
14428 */
14429 static void
14430f_spellsuggest(argvars, rettv)
14431 typval_T *argvars;
14432 typval_T *rettv;
14433{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014434#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014435 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014436 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014437 int maxcount;
14438 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014439 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014440 listitem_T *li;
14441 int need_capital = FALSE;
14442#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014443
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014444 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014445 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014446
14447#ifdef FEAT_SYN_HL
14448 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14449 {
14450 str = get_tv_string(&argvars[0]);
14451 if (argvars[1].v_type != VAR_UNKNOWN)
14452 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014453 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014454 if (maxcount <= 0)
14455 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014456 if (argvars[2].v_type != VAR_UNKNOWN)
14457 {
14458 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14459 if (typeerr)
14460 return;
14461 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014462 }
14463 else
14464 maxcount = 25;
14465
Bram Moolenaar4770d092006-01-12 23:22:24 +000014466 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014467
14468 for (i = 0; i < ga.ga_len; ++i)
14469 {
14470 str = ((char_u **)ga.ga_data)[i];
14471
14472 li = listitem_alloc();
14473 if (li == NULL)
14474 vim_free(str);
14475 else
14476 {
14477 li->li_tv.v_type = VAR_STRING;
14478 li->li_tv.v_lock = 0;
14479 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014480 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014481 }
14482 }
14483 ga_clear(&ga);
14484 }
14485#endif
14486}
14487
Bram Moolenaar0d660222005-01-07 21:51:51 +000014488 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014489f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014490 typval_T *argvars;
14491 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014492{
14493 char_u *str;
14494 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014495 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014496 regmatch_T regmatch;
14497 char_u patbuf[NUMBUFLEN];
14498 char_u *save_cpo;
14499 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014500 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014501 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014502 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014503
14504 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14505 save_cpo = p_cpo;
14506 p_cpo = (char_u *)"";
14507
14508 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014509 if (argvars[1].v_type != VAR_UNKNOWN)
14510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014511 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14512 if (pat == NULL)
14513 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014514 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014515 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014516 }
14517 if (pat == NULL || *pat == NUL)
14518 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014519
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014520 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014522 if (typeerr)
14523 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524
Bram Moolenaar0d660222005-01-07 21:51:51 +000014525 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14526 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014528 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014529 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014530 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014531 if (*str == NUL)
14532 match = FALSE; /* empty item at the end */
14533 else
14534 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014535 if (match)
14536 end = regmatch.startp[0];
14537 else
14538 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014539 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14540 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014541 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014542 if (list_append_string(rettv->vval.v_list, str,
14543 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014544 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014545 }
14546 if (!match)
14547 break;
14548 /* Advance to just after the match. */
14549 if (regmatch.endp[0] > str)
14550 col = 0;
14551 else
14552 {
14553 /* Don't get stuck at the same match. */
14554#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014555 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014556#else
14557 col = 1;
14558#endif
14559 }
14560 str = regmatch.endp[0];
14561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562
Bram Moolenaar0d660222005-01-07 21:51:51 +000014563 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565
Bram Moolenaar0d660222005-01-07 21:51:51 +000014566 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567}
14568
14569#ifdef HAVE_STRFTIME
14570/*
14571 * "strftime({format}[, {time}])" function
14572 */
14573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014574f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014575 typval_T *argvars;
14576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577{
14578 char_u result_buf[256];
14579 struct tm *curtime;
14580 time_t seconds;
14581 char_u *p;
14582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014583 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014585 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014586 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587 seconds = time(NULL);
14588 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014589 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590 curtime = localtime(&seconds);
14591 /* MSVC returns NULL for an invalid value of seconds. */
14592 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014593 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 else
14595 {
14596# ifdef FEAT_MBYTE
14597 vimconv_T conv;
14598 char_u *enc;
14599
14600 conv.vc_type = CONV_NONE;
14601 enc = enc_locale();
14602 convert_setup(&conv, p_enc, enc);
14603 if (conv.vc_type != CONV_NONE)
14604 p = string_convert(&conv, p, NULL);
14605# endif
14606 if (p != NULL)
14607 (void)strftime((char *)result_buf, sizeof(result_buf),
14608 (char *)p, curtime);
14609 else
14610 result_buf[0] = NUL;
14611
14612# ifdef FEAT_MBYTE
14613 if (conv.vc_type != CONV_NONE)
14614 vim_free(p);
14615 convert_setup(&conv, enc, p_enc);
14616 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014617 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618 else
14619# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014620 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621
14622# ifdef FEAT_MBYTE
14623 /* Release conversion descriptors */
14624 convert_setup(&conv, NULL, NULL);
14625 vim_free(enc);
14626# endif
14627 }
14628}
14629#endif
14630
14631/*
14632 * "stridx()" function
14633 */
14634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014635f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014636 typval_T *argvars;
14637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014638{
14639 char_u buf[NUMBUFLEN];
14640 char_u *needle;
14641 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014642 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014644 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014646 needle = get_tv_string_chk(&argvars[1]);
14647 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014648 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014649 if (needle == NULL || haystack == NULL)
14650 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014651
Bram Moolenaar33570922005-01-25 22:26:29 +000014652 if (argvars[2].v_type != VAR_UNKNOWN)
14653 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014654 int error = FALSE;
14655
14656 start_idx = get_tv_number_chk(&argvars[2], &error);
14657 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014658 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014659 if (start_idx >= 0)
14660 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014661 }
14662
14663 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14664 if (pos != NULL)
14665 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666}
14667
14668/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014669 * "string()" function
14670 */
14671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014672f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014673 typval_T *argvars;
14674 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014675{
14676 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014677 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014679 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014680 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014681 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014682 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683}
14684
14685/*
14686 * "strlen()" function
14687 */
14688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014689f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014690 typval_T *argvars;
14691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014693 rettv->vval.v_number = (varnumber_T)(STRLEN(
14694 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695}
14696
14697/*
14698 * "strpart()" function
14699 */
14700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014701f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014702 typval_T *argvars;
14703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704{
14705 char_u *p;
14706 int n;
14707 int len;
14708 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014709 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014711 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712 slen = (int)STRLEN(p);
14713
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014714 n = get_tv_number_chk(&argvars[1], &error);
14715 if (error)
14716 len = 0;
14717 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014718 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719 else
14720 len = slen - n; /* default len: all bytes that are available. */
14721
14722 /*
14723 * Only return the overlap between the specified part and the actual
14724 * string.
14725 */
14726 if (n < 0)
14727 {
14728 len += n;
14729 n = 0;
14730 }
14731 else if (n > slen)
14732 n = slen;
14733 if (len < 0)
14734 len = 0;
14735 else if (n + len > slen)
14736 len = slen - n;
14737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014738 rettv->v_type = VAR_STRING;
14739 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740}
14741
14742/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014743 * "strridx()" function
14744 */
14745 static void
14746f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014747 typval_T *argvars;
14748 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014749{
14750 char_u buf[NUMBUFLEN];
14751 char_u *needle;
14752 char_u *haystack;
14753 char_u *rest;
14754 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014755 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014756
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014757 needle = get_tv_string_chk(&argvars[1]);
14758 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014759 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014760
14761 rettv->vval.v_number = -1;
14762 if (needle == NULL || haystack == NULL)
14763 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014764 if (argvars[2].v_type != VAR_UNKNOWN)
14765 {
14766 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014767 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014768 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014769 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014770 }
14771 else
14772 end_idx = haystack_len;
14773
Bram Moolenaar0d660222005-01-07 21:51:51 +000014774 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014775 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014776 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014777 lastmatch = haystack + end_idx;
14778 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014779 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014780 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014781 for (rest = haystack; *rest != '\0'; ++rest)
14782 {
14783 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014784 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014785 break;
14786 lastmatch = rest;
14787 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014788 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014789
14790 if (lastmatch == NULL)
14791 rettv->vval.v_number = -1;
14792 else
14793 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14794}
14795
14796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797 * "strtrans()" function
14798 */
14799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014800f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014801 typval_T *argvars;
14802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014804 rettv->v_type = VAR_STRING;
14805 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806}
14807
14808/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014809 * "submatch()" function
14810 */
14811 static void
14812f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014813 typval_T *argvars;
14814 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014815{
14816 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014817 rettv->vval.v_string =
14818 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014819}
14820
14821/*
14822 * "substitute()" function
14823 */
14824 static void
14825f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014826 typval_T *argvars;
14827 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014828{
14829 char_u patbuf[NUMBUFLEN];
14830 char_u subbuf[NUMBUFLEN];
14831 char_u flagsbuf[NUMBUFLEN];
14832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014833 char_u *str = get_tv_string_chk(&argvars[0]);
14834 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14835 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14836 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14837
Bram Moolenaar0d660222005-01-07 21:51:51 +000014838 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014839 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14840 rettv->vval.v_string = NULL;
14841 else
14842 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014843}
14844
14845/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014846 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847 */
14848/*ARGSUSED*/
14849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014850f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014851 typval_T *argvars;
14852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853{
14854 int id = 0;
14855#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014856 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857 long col;
14858 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014859 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014861 lnum = get_tv_lnum(argvars); /* -1 on type error */
14862 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14863 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014865 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014866 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014867 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868#endif
14869
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014870 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871}
14872
14873/*
14874 * "synIDattr(id, what [, mode])" function
14875 */
14876/*ARGSUSED*/
14877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014878f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014879 typval_T *argvars;
14880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881{
14882 char_u *p = NULL;
14883#ifdef FEAT_SYN_HL
14884 int id;
14885 char_u *what;
14886 char_u *mode;
14887 char_u modebuf[NUMBUFLEN];
14888 int modec;
14889
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014890 id = get_tv_number(&argvars[0]);
14891 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014892 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014894 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895 modec = TOLOWER_ASC(mode[0]);
14896 if (modec != 't' && modec != 'c'
14897#ifdef FEAT_GUI
14898 && modec != 'g'
14899#endif
14900 )
14901 modec = 0; /* replace invalid with current */
14902 }
14903 else
14904 {
14905#ifdef FEAT_GUI
14906 if (gui.in_use)
14907 modec = 'g';
14908 else
14909#endif
14910 if (t_colors > 1)
14911 modec = 'c';
14912 else
14913 modec = 't';
14914 }
14915
14916
14917 switch (TOLOWER_ASC(what[0]))
14918 {
14919 case 'b':
14920 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14921 p = highlight_color(id, what, modec);
14922 else /* bold */
14923 p = highlight_has_attr(id, HL_BOLD, modec);
14924 break;
14925
14926 case 'f': /* fg[#] */
14927 p = highlight_color(id, what, modec);
14928 break;
14929
14930 case 'i':
14931 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14932 p = highlight_has_attr(id, HL_INVERSE, modec);
14933 else /* italic */
14934 p = highlight_has_attr(id, HL_ITALIC, modec);
14935 break;
14936
14937 case 'n': /* name */
14938 p = get_highlight_name(NULL, id - 1);
14939 break;
14940
14941 case 'r': /* reverse */
14942 p = highlight_has_attr(id, HL_INVERSE, modec);
14943 break;
14944
14945 case 's': /* standout */
14946 p = highlight_has_attr(id, HL_STANDOUT, modec);
14947 break;
14948
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014949 case 'u':
14950 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14951 /* underline */
14952 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14953 else
14954 /* undercurl */
14955 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956 break;
14957 }
14958
14959 if (p != NULL)
14960 p = vim_strsave(p);
14961#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014962 rettv->v_type = VAR_STRING;
14963 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964}
14965
14966/*
14967 * "synIDtrans(id)" function
14968 */
14969/*ARGSUSED*/
14970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014971f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014972 typval_T *argvars;
14973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974{
14975 int id;
14976
14977#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014978 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979
14980 if (id > 0)
14981 id = syn_get_final_id(id);
14982 else
14983#endif
14984 id = 0;
14985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014986 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987}
14988
14989/*
14990 * "system()" function
14991 */
14992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014993f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014994 typval_T *argvars;
14995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014997 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014998 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014999 char_u *infile = NULL;
15000 char_u buf[NUMBUFLEN];
15001 int err = FALSE;
15002 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015003
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015004 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015005 {
15006 /*
15007 * Write the string to a temp file, to be used for input of the shell
15008 * command.
15009 */
15010 if ((infile = vim_tempname('i')) == NULL)
15011 {
15012 EMSG(_(e_notmp));
15013 return;
15014 }
15015
15016 fd = mch_fopen((char *)infile, WRITEBIN);
15017 if (fd == NULL)
15018 {
15019 EMSG2(_(e_notopen), infile);
15020 goto done;
15021 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015022 p = get_tv_string_buf_chk(&argvars[1], buf);
15023 if (p == NULL)
15024 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015025 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15026 err = TRUE;
15027 if (fclose(fd) != 0)
15028 err = TRUE;
15029 if (err)
15030 {
15031 EMSG(_("E677: Error writing temp file"));
15032 goto done;
15033 }
15034 }
15035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015036 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015037
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038#ifdef USE_CR
15039 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015040 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 {
15042 char_u *s;
15043
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015044 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045 {
15046 if (*s == CAR)
15047 *s = NL;
15048 }
15049 }
15050#else
15051# ifdef USE_CRNL
15052 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015053 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 {
15055 char_u *s, *d;
15056
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015057 d = res;
15058 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059 {
15060 if (s[0] == CAR && s[1] == NL)
15061 ++s;
15062 *d++ = *s;
15063 }
15064 *d = NUL;
15065 }
15066# endif
15067#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015068
15069done:
15070 if (infile != NULL)
15071 {
15072 mch_remove(infile);
15073 vim_free(infile);
15074 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015075 rettv->v_type = VAR_STRING;
15076 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077}
15078
15079/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015080 * "tabpagebuflist()" function
15081 */
15082/* ARGSUSED */
15083 static void
15084f_tabpagebuflist(argvars, rettv)
15085 typval_T *argvars;
15086 typval_T *rettv;
15087{
15088#ifndef FEAT_WINDOWS
15089 rettv->vval.v_number = 0;
15090#else
15091 tabpage_T *tp;
15092 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015093
15094 if (argvars[0].v_type == VAR_UNKNOWN)
15095 wp = firstwin;
15096 else
15097 {
15098 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15099 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015100 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015101 }
15102 if (wp == NULL)
15103 rettv->vval.v_number = 0;
15104 else
15105 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015106 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015107 rettv->vval.v_number = 0;
15108 else
15109 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015110 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015111 if (list_append_number(rettv->vval.v_list,
15112 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015113 break;
15114 }
15115 }
15116#endif
15117}
15118
15119
15120/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015121 * "tabpagenr()" function
15122 */
15123/* ARGSUSED */
15124 static void
15125f_tabpagenr(argvars, rettv)
15126 typval_T *argvars;
15127 typval_T *rettv;
15128{
15129 int nr = 1;
15130#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015131 char_u *arg;
15132
15133 if (argvars[0].v_type != VAR_UNKNOWN)
15134 {
15135 arg = get_tv_string_chk(&argvars[0]);
15136 nr = 0;
15137 if (arg != NULL)
15138 {
15139 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015140 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015141 else
15142 EMSG2(_(e_invexpr2), arg);
15143 }
15144 }
15145 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015146 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015147#endif
15148 rettv->vval.v_number = nr;
15149}
15150
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015151
15152#ifdef FEAT_WINDOWS
15153static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15154
15155/*
15156 * Common code for tabpagewinnr() and winnr().
15157 */
15158 static int
15159get_winnr(tp, argvar)
15160 tabpage_T *tp;
15161 typval_T *argvar;
15162{
15163 win_T *twin;
15164 int nr = 1;
15165 win_T *wp;
15166 char_u *arg;
15167
15168 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15169 if (argvar->v_type != VAR_UNKNOWN)
15170 {
15171 arg = get_tv_string_chk(argvar);
15172 if (arg == NULL)
15173 nr = 0; /* type error; errmsg already given */
15174 else if (STRCMP(arg, "$") == 0)
15175 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15176 else if (STRCMP(arg, "#") == 0)
15177 {
15178 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15179 if (twin == NULL)
15180 nr = 0;
15181 }
15182 else
15183 {
15184 EMSG2(_(e_invexpr2), arg);
15185 nr = 0;
15186 }
15187 }
15188
15189 if (nr > 0)
15190 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15191 wp != twin; wp = wp->w_next)
15192 ++nr;
15193 return nr;
15194}
15195#endif
15196
15197/*
15198 * "tabpagewinnr()" function
15199 */
15200/* ARGSUSED */
15201 static void
15202f_tabpagewinnr(argvars, rettv)
15203 typval_T *argvars;
15204 typval_T *rettv;
15205{
15206 int nr = 1;
15207#ifdef FEAT_WINDOWS
15208 tabpage_T *tp;
15209
15210 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15211 if (tp == NULL)
15212 nr = 0;
15213 else
15214 nr = get_winnr(tp, &argvars[1]);
15215#endif
15216 rettv->vval.v_number = nr;
15217}
15218
15219
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015220/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015221 * "tagfiles()" function
15222 */
15223/*ARGSUSED*/
15224 static void
15225f_tagfiles(argvars, rettv)
15226 typval_T *argvars;
15227 typval_T *rettv;
15228{
15229 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015230 tagname_T tn;
15231 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015232
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015233 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015234 {
15235 rettv->vval.v_number = 0;
15236 return;
15237 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015238
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015239 for (first = TRUE; ; first = FALSE)
15240 if (get_tagfname(&tn, first, fname) == FAIL
15241 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015242 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015243 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015244}
15245
15246/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015247 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015248 */
15249 static void
15250f_taglist(argvars, rettv)
15251 typval_T *argvars;
15252 typval_T *rettv;
15253{
15254 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015255
15256 tag_pattern = get_tv_string(&argvars[0]);
15257
15258 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015259 if (*tag_pattern == NUL)
15260 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015261
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015262 if (rettv_list_alloc(rettv) == OK)
15263 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015264}
15265
15266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267 * "tempname()" function
15268 */
15269/*ARGSUSED*/
15270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015271f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015272 typval_T *argvars;
15273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274{
15275 static int x = 'A';
15276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015277 rettv->v_type = VAR_STRING;
15278 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279
15280 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15281 * names. Skip 'I' and 'O', they are used for shell redirection. */
15282 do
15283 {
15284 if (x == 'Z')
15285 x = '0';
15286 else if (x == '9')
15287 x = 'A';
15288 else
15289 {
15290#ifdef EBCDIC
15291 if (x == 'I')
15292 x = 'J';
15293 else if (x == 'R')
15294 x = 'S';
15295 else
15296#endif
15297 ++x;
15298 }
15299 } while (x == 'I' || x == 'O');
15300}
15301
15302/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015303 * "test(list)" function: Just checking the walls...
15304 */
15305/*ARGSUSED*/
15306 static void
15307f_test(argvars, rettv)
15308 typval_T *argvars;
15309 typval_T *rettv;
15310{
15311 /* Used for unit testing. Change the code below to your liking. */
15312#if 0
15313 listitem_T *li;
15314 list_T *l;
15315 char_u *bad, *good;
15316
15317 if (argvars[0].v_type != VAR_LIST)
15318 return;
15319 l = argvars[0].vval.v_list;
15320 if (l == NULL)
15321 return;
15322 li = l->lv_first;
15323 if (li == NULL)
15324 return;
15325 bad = get_tv_string(&li->li_tv);
15326 li = li->li_next;
15327 if (li == NULL)
15328 return;
15329 good = get_tv_string(&li->li_tv);
15330 rettv->vval.v_number = test_edit_score(bad, good);
15331#endif
15332}
15333
15334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 * "tolower(string)" function
15336 */
15337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015338f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015339 typval_T *argvars;
15340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341{
15342 char_u *p;
15343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015344 p = vim_strsave(get_tv_string(&argvars[0]));
15345 rettv->v_type = VAR_STRING;
15346 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347
15348 if (p != NULL)
15349 while (*p != NUL)
15350 {
15351#ifdef FEAT_MBYTE
15352 int l;
15353
15354 if (enc_utf8)
15355 {
15356 int c, lc;
15357
15358 c = utf_ptr2char(p);
15359 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015360 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361 /* TODO: reallocate string when byte count changes. */
15362 if (utf_char2len(lc) == l)
15363 utf_char2bytes(lc, p);
15364 p += l;
15365 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015366 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367 p += l; /* skip multi-byte character */
15368 else
15369#endif
15370 {
15371 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15372 ++p;
15373 }
15374 }
15375}
15376
15377/*
15378 * "toupper(string)" function
15379 */
15380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015381f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015382 typval_T *argvars;
15383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015385 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015386 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387}
15388
15389/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015390 * "tr(string, fromstr, tostr)" function
15391 */
15392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015393f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015394 typval_T *argvars;
15395 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015396{
15397 char_u *instr;
15398 char_u *fromstr;
15399 char_u *tostr;
15400 char_u *p;
15401#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015402 int inlen;
15403 int fromlen;
15404 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015405 int idx;
15406 char_u *cpstr;
15407 int cplen;
15408 int first = TRUE;
15409#endif
15410 char_u buf[NUMBUFLEN];
15411 char_u buf2[NUMBUFLEN];
15412 garray_T ga;
15413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015414 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015415 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15416 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015417
15418 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015419 rettv->v_type = VAR_STRING;
15420 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015421 if (fromstr == NULL || tostr == NULL)
15422 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015423 ga_init2(&ga, (int)sizeof(char), 80);
15424
15425#ifdef FEAT_MBYTE
15426 if (!has_mbyte)
15427#endif
15428 /* not multi-byte: fromstr and tostr must be the same length */
15429 if (STRLEN(fromstr) != STRLEN(tostr))
15430 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015431#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015432error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015433#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015434 EMSG2(_(e_invarg2), fromstr);
15435 ga_clear(&ga);
15436 return;
15437 }
15438
15439 /* fromstr and tostr have to contain the same number of chars */
15440 while (*instr != NUL)
15441 {
15442#ifdef FEAT_MBYTE
15443 if (has_mbyte)
15444 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015445 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015446 cpstr = instr;
15447 cplen = inlen;
15448 idx = 0;
15449 for (p = fromstr; *p != NUL; p += fromlen)
15450 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015451 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015452 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15453 {
15454 for (p = tostr; *p != NUL; p += tolen)
15455 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015456 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015457 if (idx-- == 0)
15458 {
15459 cplen = tolen;
15460 cpstr = p;
15461 break;
15462 }
15463 }
15464 if (*p == NUL) /* tostr is shorter than fromstr */
15465 goto error;
15466 break;
15467 }
15468 ++idx;
15469 }
15470
15471 if (first && cpstr == instr)
15472 {
15473 /* Check that fromstr and tostr have the same number of
15474 * (multi-byte) characters. Done only once when a character
15475 * of instr doesn't appear in fromstr. */
15476 first = FALSE;
15477 for (p = tostr; *p != NUL; p += tolen)
15478 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015479 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015480 --idx;
15481 }
15482 if (idx != 0)
15483 goto error;
15484 }
15485
15486 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015487 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015488 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015489
15490 instr += inlen;
15491 }
15492 else
15493#endif
15494 {
15495 /* When not using multi-byte chars we can do it faster. */
15496 p = vim_strchr(fromstr, *instr);
15497 if (p != NULL)
15498 ga_append(&ga, tostr[p - fromstr]);
15499 else
15500 ga_append(&ga, *instr);
15501 ++instr;
15502 }
15503 }
15504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015505 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015506}
15507
15508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 * "type(expr)" function
15510 */
15511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015512f_type(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{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015516 int n;
15517
15518 switch (argvars[0].v_type)
15519 {
15520 case VAR_NUMBER: n = 0; break;
15521 case VAR_STRING: n = 1; break;
15522 case VAR_FUNC: n = 2; break;
15523 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015524 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015525 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15526 }
15527 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528}
15529
15530/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015531 * "values(dict)" function
15532 */
15533 static void
15534f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015535 typval_T *argvars;
15536 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015537{
15538 dict_list(argvars, rettv, 1);
15539}
15540
15541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542 * "virtcol(string)" function
15543 */
15544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015545f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015546 typval_T *argvars;
15547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548{
15549 colnr_T vcol = 0;
15550 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015551 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015553 fp = var2fpos(&argvars[0], FALSE, &fnum);
15554 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15555 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556 {
15557 getvvcol(curwin, fp, NULL, NULL, &vcol);
15558 ++vcol;
15559 }
15560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015561 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562}
15563
15564/*
15565 * "visualmode()" function
15566 */
15567/*ARGSUSED*/
15568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015569f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015570 typval_T *argvars;
15571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572{
15573#ifdef FEAT_VISUAL
15574 char_u str[2];
15575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015576 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577 str[0] = curbuf->b_visual_mode_eval;
15578 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015579 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580
15581 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015582 if ((argvars[0].v_type == VAR_NUMBER
15583 && argvars[0].vval.v_number != 0)
15584 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015585 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 curbuf->b_visual_mode_eval = NUL;
15587#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015588 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589#endif
15590}
15591
15592/*
15593 * "winbufnr(nr)" function
15594 */
15595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015596f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015597 typval_T *argvars;
15598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599{
15600 win_T *wp;
15601
15602 wp = find_win_by_nr(&argvars[0]);
15603 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015604 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015606 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607}
15608
15609/*
15610 * "wincol()" function
15611 */
15612/*ARGSUSED*/
15613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015614f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015615 typval_T *argvars;
15616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617{
15618 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015619 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620}
15621
15622/*
15623 * "winheight(nr)" function
15624 */
15625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015626f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015627 typval_T *argvars;
15628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629{
15630 win_T *wp;
15631
15632 wp = find_win_by_nr(&argvars[0]);
15633 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015634 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015636 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637}
15638
15639/*
15640 * "winline()" function
15641 */
15642/*ARGSUSED*/
15643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015644f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015645 typval_T *argvars;
15646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647{
15648 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015649 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650}
15651
15652/*
15653 * "winnr()" function
15654 */
15655/* ARGSUSED */
15656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015657f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015658 typval_T *argvars;
15659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660{
15661 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015662
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015664 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015666 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667}
15668
15669/*
15670 * "winrestcmd()" function
15671 */
15672/* ARGSUSED */
15673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015674f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015675 typval_T *argvars;
15676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677{
15678#ifdef FEAT_WINDOWS
15679 win_T *wp;
15680 int winnr = 1;
15681 garray_T ga;
15682 char_u buf[50];
15683
15684 ga_init2(&ga, (int)sizeof(char), 70);
15685 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15686 {
15687 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15688 ga_concat(&ga, buf);
15689# ifdef FEAT_VERTSPLIT
15690 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15691 ga_concat(&ga, buf);
15692# endif
15693 ++winnr;
15694 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015695 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015697 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015699 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015701 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702}
15703
15704/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015705 * "winrestview()" function
15706 */
15707/* ARGSUSED */
15708 static void
15709f_winrestview(argvars, rettv)
15710 typval_T *argvars;
15711 typval_T *rettv;
15712{
15713 dict_T *dict;
15714
15715 if (argvars[0].v_type != VAR_DICT
15716 || (dict = argvars[0].vval.v_dict) == NULL)
15717 EMSG(_(e_invarg));
15718 else
15719 {
15720 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
15721 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
15722#ifdef FEAT_VIRTUALEDIT
15723 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
15724#endif
15725 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015726 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015727
15728 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
15729#ifdef FEAT_DIFF
15730 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
15731#endif
15732 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
15733 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
15734
15735 check_cursor();
15736 changed_cline_bef_curs();
15737 invalidate_botline();
15738 redraw_later(VALID);
15739
15740 if (curwin->w_topline == 0)
15741 curwin->w_topline = 1;
15742 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15743 curwin->w_topline = curbuf->b_ml.ml_line_count;
15744#ifdef FEAT_DIFF
15745 check_topfill(curwin, TRUE);
15746#endif
15747 }
15748}
15749
15750/*
15751 * "winsaveview()" function
15752 */
15753/* ARGSUSED */
15754 static void
15755f_winsaveview(argvars, rettv)
15756 typval_T *argvars;
15757 typval_T *rettv;
15758{
15759 dict_T *dict;
15760
15761 dict = dict_alloc();
15762 if (dict == NULL)
15763 return;
15764 rettv->v_type = VAR_DICT;
15765 rettv->vval.v_dict = dict;
15766 ++dict->dv_refcount;
15767
15768 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
15769 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
15770#ifdef FEAT_VIRTUALEDIT
15771 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
15772#endif
15773 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
15774
15775 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
15776#ifdef FEAT_DIFF
15777 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
15778#endif
15779 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
15780 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
15781}
15782
15783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784 * "winwidth(nr)" function
15785 */
15786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015787f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015788 typval_T *argvars;
15789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790{
15791 win_T *wp;
15792
15793 wp = find_win_by_nr(&argvars[0]);
15794 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015795 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796 else
15797#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015798 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015800 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801#endif
15802}
15803
Bram Moolenaar071d4272004-06-13 20:20:40 +000015804/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015805 * "writefile()" function
15806 */
15807 static void
15808f_writefile(argvars, rettv)
15809 typval_T *argvars;
15810 typval_T *rettv;
15811{
15812 int binary = FALSE;
15813 char_u *fname;
15814 FILE *fd;
15815 listitem_T *li;
15816 char_u *s;
15817 int ret = 0;
15818 int c;
15819
15820 if (argvars[0].v_type != VAR_LIST)
15821 {
15822 EMSG2(_(e_listarg), "writefile()");
15823 return;
15824 }
15825 if (argvars[0].vval.v_list == NULL)
15826 return;
15827
15828 if (argvars[2].v_type != VAR_UNKNOWN
15829 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15830 binary = TRUE;
15831
15832 /* Always open the file in binary mode, library functions have a mind of
15833 * their own about CR-LF conversion. */
15834 fname = get_tv_string(&argvars[1]);
15835 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15836 {
15837 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15838 ret = -1;
15839 }
15840 else
15841 {
15842 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15843 li = li->li_next)
15844 {
15845 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15846 {
15847 if (*s == '\n')
15848 c = putc(NUL, fd);
15849 else
15850 c = putc(*s, fd);
15851 if (c == EOF)
15852 {
15853 ret = -1;
15854 break;
15855 }
15856 }
15857 if (!binary || li->li_next != NULL)
15858 if (putc('\n', fd) == EOF)
15859 {
15860 ret = -1;
15861 break;
15862 }
15863 if (ret < 0)
15864 {
15865 EMSG(_(e_write));
15866 break;
15867 }
15868 }
15869 fclose(fd);
15870 }
15871
15872 rettv->vval.v_number = ret;
15873}
15874
15875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015877 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878 */
15879 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015880var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015881 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015883 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015885 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015887 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888
Bram Moolenaara5525202006-03-02 22:52:09 +000015889 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015890 if (varp->v_type == VAR_LIST)
15891 {
15892 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015893 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000015894 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015895
15896 l = varp->vval.v_list;
15897 if (l == NULL)
15898 return NULL;
15899
15900 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015901 pos.lnum = list_find_nr(l, 0L, &error);
15902 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015903 return NULL; /* invalid line number */
15904
15905 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015906 pos.col = list_find_nr(l, 1L, &error);
15907 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015908 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015909 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000015910 /* Accept a position up to the NUL after the line. */
15911 if (pos.col <= 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015912 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015913 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015914
Bram Moolenaara5525202006-03-02 22:52:09 +000015915#ifdef FEAT_VIRTUALEDIT
15916 /* Get the virtual offset. Defaults to zero. */
15917 pos.coladd = list_find_nr(l, 2L, &error);
15918 if (error)
15919 pos.coladd = 0;
15920#endif
15921
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015922 return &pos;
15923 }
15924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015925 name = get_tv_string_chk(varp);
15926 if (name == NULL)
15927 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928 if (name[0] == '.') /* cursor */
15929 return &curwin->w_cursor;
15930 if (name[0] == '\'') /* mark */
15931 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015932 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15934 return NULL;
15935 return pp;
15936 }
Bram Moolenaara5525202006-03-02 22:52:09 +000015937
15938#ifdef FEAT_VIRTUALEDIT
15939 pos.coladd = 0;
15940#endif
15941
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015942 if (name[0] == 'w' && lnum)
15943 {
15944 pos.col = 0;
15945 if (name[1] == '0') /* "w0": first visible line */
15946 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015947 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015948 pos.lnum = curwin->w_topline;
15949 return &pos;
15950 }
15951 else if (name[1] == '$') /* "w$": last visible line */
15952 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015953 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015954 pos.lnum = curwin->w_botline - 1;
15955 return &pos;
15956 }
15957 }
15958 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015959 {
15960 if (lnum)
15961 {
15962 pos.lnum = curbuf->b_ml.ml_line_count;
15963 pos.col = 0;
15964 }
15965 else
15966 {
15967 pos.lnum = curwin->w_cursor.lnum;
15968 pos.col = (colnr_T)STRLEN(ml_get_curline());
15969 }
15970 return &pos;
15971 }
15972 return NULL;
15973}
15974
15975/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015976 * Convert list in "arg" into a position and optional file number.
15977 * When "fnump" is NULL there is no file number, only 3 items.
15978 * Note that the column is passed on as-is, the caller may want to decrement
15979 * it to use 1 for the first column.
15980 * Return FAIL when conversion is not possible, doesn't check the position for
15981 * validity.
15982 */
15983 static int
15984list2fpos(arg, posp, fnump)
15985 typval_T *arg;
15986 pos_T *posp;
15987 int *fnump;
15988{
15989 list_T *l = arg->vval.v_list;
15990 long i = 0;
15991 long n;
15992
15993 /* List must be: [fnum, lnum, col, coladd] */
15994 if (arg->v_type != VAR_LIST || l == NULL
15995 || l->lv_len != (fnump == NULL ? 3 : 4))
15996 return FAIL;
15997
15998 if (fnump != NULL)
15999 {
16000 n = list_find_nr(l, i++, NULL); /* fnum */
16001 if (n < 0)
16002 return FAIL;
16003 if (n == 0)
16004 n = curbuf->b_fnum; /* current buffer */
16005 *fnump = n;
16006 }
16007
16008 n = list_find_nr(l, i++, NULL); /* lnum */
16009 if (n < 0)
16010 return FAIL;
16011 posp->lnum = n;
16012
16013 n = list_find_nr(l, i++, NULL); /* col */
16014 if (n < 0)
16015 return FAIL;
16016 posp->col = n;
16017
16018#ifdef FEAT_VIRTUALEDIT
16019 n = list_find_nr(l, i, NULL);
16020 if (n < 0)
16021 return FAIL;
16022 posp->coladd = n;
16023#endif
16024
16025 return OK;
16026}
16027
16028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 * Get the length of an environment variable name.
16030 * Advance "arg" to the first character after the name.
16031 * Return 0 for error.
16032 */
16033 static int
16034get_env_len(arg)
16035 char_u **arg;
16036{
16037 char_u *p;
16038 int len;
16039
16040 for (p = *arg; vim_isIDc(*p); ++p)
16041 ;
16042 if (p == *arg) /* no name found */
16043 return 0;
16044
16045 len = (int)(p - *arg);
16046 *arg = p;
16047 return len;
16048}
16049
16050/*
16051 * Get the length of the name of a function or internal variable.
16052 * "arg" is advanced to the first non-white character after the name.
16053 * Return 0 if something is wrong.
16054 */
16055 static int
16056get_id_len(arg)
16057 char_u **arg;
16058{
16059 char_u *p;
16060 int len;
16061
16062 /* Find the end of the name. */
16063 for (p = *arg; eval_isnamec(*p); ++p)
16064 ;
16065 if (p == *arg) /* no name found */
16066 return 0;
16067
16068 len = (int)(p - *arg);
16069 *arg = skipwhite(p);
16070
16071 return len;
16072}
16073
16074/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016075 * Get the length of the name of a variable or function.
16076 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016078 * Return -1 if curly braces expansion failed.
16079 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080 * If the name contains 'magic' {}'s, expand them and return the
16081 * expanded name in an allocated string via 'alias' - caller must free.
16082 */
16083 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016084get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085 char_u **arg;
16086 char_u **alias;
16087 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016088 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089{
16090 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 char_u *p;
16092 char_u *expr_start;
16093 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094
16095 *alias = NULL; /* default to no alias */
16096
16097 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16098 && (*arg)[2] == (int)KE_SNR)
16099 {
16100 /* hard coded <SNR>, already translated */
16101 *arg += 3;
16102 return get_id_len(arg) + 3;
16103 }
16104 len = eval_fname_script(*arg);
16105 if (len > 0)
16106 {
16107 /* literal "<SID>", "s:" or "<SNR>" */
16108 *arg += len;
16109 }
16110
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016112 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016114 p = find_name_end(*arg, &expr_start, &expr_end,
16115 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116 if (expr_start != NULL)
16117 {
16118 char_u *temp_string;
16119
16120 if (!evaluate)
16121 {
16122 len += (int)(p - *arg);
16123 *arg = skipwhite(p);
16124 return len;
16125 }
16126
16127 /*
16128 * Include any <SID> etc in the expanded string:
16129 * Thus the -len here.
16130 */
16131 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16132 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016133 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134 *alias = temp_string;
16135 *arg = skipwhite(p);
16136 return (int)STRLEN(temp_string);
16137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138
16139 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016140 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141 EMSG2(_(e_invexpr2), *arg);
16142
16143 return len;
16144}
16145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016146/*
16147 * Find the end of a variable or function name, taking care of magic braces.
16148 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16149 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016150 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016151 * Return a pointer to just after the name. Equal to "arg" if there is no
16152 * valid name.
16153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016155find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156 char_u *arg;
16157 char_u **expr_start;
16158 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016159 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016161 int mb_nest = 0;
16162 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163 char_u *p;
16164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016165 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016167 *expr_start = NULL;
16168 *expr_end = NULL;
16169 }
16170
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016171 /* Quick check for valid starting character. */
16172 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16173 return arg;
16174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016175 for (p = arg; *p != NUL
16176 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016177 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016178 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016179 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016180 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016181 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016182 if (*p == '\'')
16183 {
16184 /* skip over 'string' to avoid counting [ and ] inside it. */
16185 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16186 ;
16187 if (*p == NUL)
16188 break;
16189 }
16190 else if (*p == '"')
16191 {
16192 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16193 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16194 if (*p == '\\' && p[1] != NUL)
16195 ++p;
16196 if (*p == NUL)
16197 break;
16198 }
16199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016200 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016202 if (*p == '[')
16203 ++br_nest;
16204 else if (*p == ']')
16205 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016208 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016209 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016210 if (*p == '{')
16211 {
16212 mb_nest++;
16213 if (expr_start != NULL && *expr_start == NULL)
16214 *expr_start = p;
16215 }
16216 else if (*p == '}')
16217 {
16218 mb_nest--;
16219 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16220 *expr_end = p;
16221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223 }
16224
16225 return p;
16226}
16227
16228/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016229 * Expands out the 'magic' {}'s in a variable/function name.
16230 * Note that this can call itself recursively, to deal with
16231 * constructs like foo{bar}{baz}{bam}
16232 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16233 * "in_start" ^
16234 * "expr_start" ^
16235 * "expr_end" ^
16236 * "in_end" ^
16237 *
16238 * Returns a new allocated string, which the caller must free.
16239 * Returns NULL for failure.
16240 */
16241 static char_u *
16242make_expanded_name(in_start, expr_start, expr_end, in_end)
16243 char_u *in_start;
16244 char_u *expr_start;
16245 char_u *expr_end;
16246 char_u *in_end;
16247{
16248 char_u c1;
16249 char_u *retval = NULL;
16250 char_u *temp_result;
16251 char_u *nextcmd = NULL;
16252
16253 if (expr_end == NULL || in_end == NULL)
16254 return NULL;
16255 *expr_start = NUL;
16256 *expr_end = NUL;
16257 c1 = *in_end;
16258 *in_end = NUL;
16259
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016260 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016261 if (temp_result != NULL && nextcmd == NULL)
16262 {
16263 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16264 + (in_end - expr_end) + 1));
16265 if (retval != NULL)
16266 {
16267 STRCPY(retval, in_start);
16268 STRCAT(retval, temp_result);
16269 STRCAT(retval, expr_end + 1);
16270 }
16271 }
16272 vim_free(temp_result);
16273
16274 *in_end = c1; /* put char back for error messages */
16275 *expr_start = '{';
16276 *expr_end = '}';
16277
16278 if (retval != NULL)
16279 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016280 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016281 if (expr_start != NULL)
16282 {
16283 /* Further expansion! */
16284 temp_result = make_expanded_name(retval, expr_start,
16285 expr_end, temp_result);
16286 vim_free(retval);
16287 retval = temp_result;
16288 }
16289 }
16290
16291 return retval;
16292}
16293
16294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016296 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297 */
16298 static int
16299eval_isnamec(c)
16300 int c;
16301{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016302 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16303}
16304
16305/*
16306 * Return TRUE if character "c" can be used as the first character in a
16307 * variable or function name (excluding '{' and '}').
16308 */
16309 static int
16310eval_isnamec1(c)
16311 int c;
16312{
16313 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314}
16315
16316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317 * Set number v: variable to "val".
16318 */
16319 void
16320set_vim_var_nr(idx, val)
16321 int idx;
16322 long val;
16323{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016324 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325}
16326
16327/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016328 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329 */
16330 long
16331get_vim_var_nr(idx)
16332 int idx;
16333{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016334 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335}
16336
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016337#if defined(FEAT_AUTOCMD) || defined(PROTO)
16338/*
16339 * Get string v: variable value. Uses a static buffer, can only be used once.
16340 */
16341 char_u *
16342get_vim_var_str(idx)
16343 int idx;
16344{
16345 return get_tv_string(&vimvars[idx].vv_tv);
16346}
16347#endif
16348
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349/*
16350 * Set v:count, v:count1 and v:prevcount.
16351 */
16352 void
16353set_vcount(count, count1)
16354 long count;
16355 long count1;
16356{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016357 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16358 vimvars[VV_COUNT].vv_nr = count;
16359 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360}
16361
16362/*
16363 * Set string v: variable to a copy of "val".
16364 */
16365 void
16366set_vim_var_string(idx, val, len)
16367 int idx;
16368 char_u *val;
16369 int len; /* length of "val" to use or -1 (whole string) */
16370{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016371 /* Need to do this (at least) once, since we can't initialize a union.
16372 * Will always be invoked when "v:progname" is set. */
16373 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16374
Bram Moolenaare9a41262005-01-15 22:18:47 +000016375 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016377 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016379 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016381 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382}
16383
16384/*
16385 * Set v:register if needed.
16386 */
16387 void
16388set_reg_var(c)
16389 int c;
16390{
16391 char_u regname;
16392
16393 if (c == 0 || c == ' ')
16394 regname = '"';
16395 else
16396 regname = c;
16397 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016398 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399 set_vim_var_string(VV_REG, &regname, 1);
16400}
16401
16402/*
16403 * Get or set v:exception. If "oldval" == NULL, return the current value.
16404 * Otherwise, restore the value to "oldval" and return NULL.
16405 * Must always be called in pairs to save and restore v:exception! Does not
16406 * take care of memory allocations.
16407 */
16408 char_u *
16409v_exception(oldval)
16410 char_u *oldval;
16411{
16412 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016413 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414
Bram Moolenaare9a41262005-01-15 22:18:47 +000016415 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 return NULL;
16417}
16418
16419/*
16420 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16421 * Otherwise, restore the value to "oldval" and return NULL.
16422 * Must always be called in pairs to save and restore v:throwpoint! Does not
16423 * take care of memory allocations.
16424 */
16425 char_u *
16426v_throwpoint(oldval)
16427 char_u *oldval;
16428{
16429 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016430 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431
Bram Moolenaare9a41262005-01-15 22:18:47 +000016432 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433 return NULL;
16434}
16435
16436#if defined(FEAT_AUTOCMD) || defined(PROTO)
16437/*
16438 * Set v:cmdarg.
16439 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16440 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16441 * Must always be called in pairs!
16442 */
16443 char_u *
16444set_cmdarg(eap, oldarg)
16445 exarg_T *eap;
16446 char_u *oldarg;
16447{
16448 char_u *oldval;
16449 char_u *newval;
16450 unsigned len;
16451
Bram Moolenaare9a41262005-01-15 22:18:47 +000016452 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016453 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016455 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016456 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016457 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458 }
16459
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016460 if (eap->force_bin == FORCE_BIN)
16461 len = 6;
16462 else if (eap->force_bin == FORCE_NOBIN)
16463 len = 8;
16464 else
16465 len = 0;
16466 if (eap->force_ff != 0)
16467 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16468# ifdef FEAT_MBYTE
16469 if (eap->force_enc != 0)
16470 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016471 if (eap->bad_char != 0)
16472 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016473# endif
16474
16475 newval = alloc(len + 1);
16476 if (newval == NULL)
16477 return NULL;
16478
16479 if (eap->force_bin == FORCE_BIN)
16480 sprintf((char *)newval, " ++bin");
16481 else if (eap->force_bin == FORCE_NOBIN)
16482 sprintf((char *)newval, " ++nobin");
16483 else
16484 *newval = NUL;
16485 if (eap->force_ff != 0)
16486 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16487 eap->cmd + eap->force_ff);
16488# ifdef FEAT_MBYTE
16489 if (eap->force_enc != 0)
16490 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16491 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016492 if (eap->bad_char != 0)
16493 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16494 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016495# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016496 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016497 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498}
16499#endif
16500
16501/*
16502 * Get the value of internal variable "name".
16503 * Return OK or FAIL.
16504 */
16505 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016506get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507 char_u *name;
16508 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016509 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016510 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511{
16512 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016513 typval_T *tv = NULL;
16514 typval_T atv;
16515 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517
16518 /* truncate the name, so that we can use strcmp() */
16519 cc = name[len];
16520 name[len] = NUL;
16521
16522 /*
16523 * Check for "b:changedtick".
16524 */
16525 if (STRCMP(name, "b:changedtick") == 0)
16526 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016527 atv.v_type = VAR_NUMBER;
16528 atv.vval.v_number = curbuf->b_changedtick;
16529 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530 }
16531
16532 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533 * Check for user-defined variables.
16534 */
16535 else
16536 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016537 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016539 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 }
16541
Bram Moolenaare9a41262005-01-15 22:18:47 +000016542 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016544 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016545 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 ret = FAIL;
16547 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016548 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016549 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550
16551 name[len] = cc;
16552
16553 return ret;
16554}
16555
16556/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016557 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16558 * Also handle function call with Funcref variable: func(expr)
16559 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16560 */
16561 static int
16562handle_subscript(arg, rettv, evaluate, verbose)
16563 char_u **arg;
16564 typval_T *rettv;
16565 int evaluate; /* do more than finding the end */
16566 int verbose; /* give error messages */
16567{
16568 int ret = OK;
16569 dict_T *selfdict = NULL;
16570 char_u *s;
16571 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016572 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016573
16574 while (ret == OK
16575 && (**arg == '['
16576 || (**arg == '.' && rettv->v_type == VAR_DICT)
16577 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16578 && !vim_iswhite(*(*arg - 1)))
16579 {
16580 if (**arg == '(')
16581 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016582 /* need to copy the funcref so that we can clear rettv */
16583 functv = *rettv;
16584 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016585
16586 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016587 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016588 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016589 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16590 &len, evaluate, selfdict);
16591
16592 /* Clear the funcref afterwards, so that deleting it while
16593 * evaluating the arguments is possible (see test55). */
16594 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016595
16596 /* Stop the expression evaluation when immediately aborting on
16597 * error, or when an interrupt occurred or an exception was thrown
16598 * but not caught. */
16599 if (aborting())
16600 {
16601 if (ret == OK)
16602 clear_tv(rettv);
16603 ret = FAIL;
16604 }
16605 dict_unref(selfdict);
16606 selfdict = NULL;
16607 }
16608 else /* **arg == '[' || **arg == '.' */
16609 {
16610 dict_unref(selfdict);
16611 if (rettv->v_type == VAR_DICT)
16612 {
16613 selfdict = rettv->vval.v_dict;
16614 if (selfdict != NULL)
16615 ++selfdict->dv_refcount;
16616 }
16617 else
16618 selfdict = NULL;
16619 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16620 {
16621 clear_tv(rettv);
16622 ret = FAIL;
16623 }
16624 }
16625 }
16626 dict_unref(selfdict);
16627 return ret;
16628}
16629
16630/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016631 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16632 * value).
16633 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016634 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016635alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016636{
Bram Moolenaar33570922005-01-25 22:26:29 +000016637 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016638}
16639
16640/*
16641 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642 * The string "s" must have been allocated, it is consumed.
16643 * Return NULL for out of memory, the variable otherwise.
16644 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016645 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016646alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647 char_u *s;
16648{
Bram Moolenaar33570922005-01-25 22:26:29 +000016649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016651 rettv = alloc_tv();
16652 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016654 rettv->v_type = VAR_STRING;
16655 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656 }
16657 else
16658 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016659 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660}
16661
16662/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016663 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016665 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016666free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016667 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668{
16669 if (varp != NULL)
16670 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016671 switch (varp->v_type)
16672 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016673 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016674 func_unref(varp->vval.v_string);
16675 /*FALLTHROUGH*/
16676 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016677 vim_free(varp->vval.v_string);
16678 break;
16679 case VAR_LIST:
16680 list_unref(varp->vval.v_list);
16681 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016682 case VAR_DICT:
16683 dict_unref(varp->vval.v_dict);
16684 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016685 case VAR_NUMBER:
16686 case VAR_UNKNOWN:
16687 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016688 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016689 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016690 break;
16691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692 vim_free(varp);
16693 }
16694}
16695
16696/*
16697 * Free the memory for a variable value and set the value to NULL or 0.
16698 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016699 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016700clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016701 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702{
16703 if (varp != NULL)
16704 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016705 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016707 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016708 func_unref(varp->vval.v_string);
16709 /*FALLTHROUGH*/
16710 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016711 vim_free(varp->vval.v_string);
16712 varp->vval.v_string = NULL;
16713 break;
16714 case VAR_LIST:
16715 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016716 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016717 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016718 case VAR_DICT:
16719 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016720 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016721 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016722 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016723 varp->vval.v_number = 0;
16724 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016725 case VAR_UNKNOWN:
16726 break;
16727 default:
16728 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016730 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731 }
16732}
16733
16734/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016735 * Set the value of a variable to NULL without freeing items.
16736 */
16737 static void
16738init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016739 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016740{
16741 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016742 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016743}
16744
16745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746 * Get the number value of a variable.
16747 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016748 * For incompatible types, return 0.
16749 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16750 * caller of incompatible types: it sets *denote to TRUE if "denote"
16751 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752 */
16753 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016754get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016755 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016757 int error = FALSE;
16758
16759 return get_tv_number_chk(varp, &error); /* return 0L on error */
16760}
16761
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016762 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016763get_tv_number_chk(varp, denote)
16764 typval_T *varp;
16765 int *denote;
16766{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016767 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016769 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016771 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016772 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016773 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016774 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016775 break;
16776 case VAR_STRING:
16777 if (varp->vval.v_string != NULL)
16778 vim_str2nr(varp->vval.v_string, NULL, NULL,
16779 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016780 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016781 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016782 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016783 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016784 case VAR_DICT:
16785 EMSG(_("E728: Using a Dictionary as a number"));
16786 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016787 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016788 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016789 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016791 if (denote == NULL) /* useful for values that must be unsigned */
16792 n = -1;
16793 else
16794 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016795 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796}
16797
16798/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016799 * Get the lnum from the first argument.
16800 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016801 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 */
16803 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016804get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016805 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806{
Bram Moolenaar33570922005-01-25 22:26:29 +000016807 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808 linenr_T lnum;
16809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016810 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811 if (lnum == 0) /* no valid number, try using line() */
16812 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016813 rettv.v_type = VAR_NUMBER;
16814 f_line(argvars, &rettv);
16815 lnum = rettv.vval.v_number;
16816 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 }
16818 return lnum;
16819}
16820
16821/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016822 * Get the lnum from the first argument.
16823 * Also accepts "$", then "buf" is used.
16824 * Returns 0 on error.
16825 */
16826 static linenr_T
16827get_tv_lnum_buf(argvars, buf)
16828 typval_T *argvars;
16829 buf_T *buf;
16830{
16831 if (argvars[0].v_type == VAR_STRING
16832 && argvars[0].vval.v_string != NULL
16833 && argvars[0].vval.v_string[0] == '$'
16834 && buf != NULL)
16835 return buf->b_ml.ml_line_count;
16836 return get_tv_number_chk(&argvars[0], NULL);
16837}
16838
16839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840 * Get the string value of a variable.
16841 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016842 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16843 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 * If the String variable has never been set, return an empty string.
16845 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016846 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16847 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 */
16849 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016850get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016851 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852{
16853 static char_u mybuf[NUMBUFLEN];
16854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016855 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856}
16857
16858 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016859get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016860 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016861 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016863 char_u *res = get_tv_string_buf_chk(varp, buf);
16864
16865 return res != NULL ? res : (char_u *)"";
16866}
16867
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016868 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016869get_tv_string_chk(varp)
16870 typval_T *varp;
16871{
16872 static char_u mybuf[NUMBUFLEN];
16873
16874 return get_tv_string_buf_chk(varp, mybuf);
16875}
16876
16877 static char_u *
16878get_tv_string_buf_chk(varp, buf)
16879 typval_T *varp;
16880 char_u *buf;
16881{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016882 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016884 case VAR_NUMBER:
16885 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16886 return buf;
16887 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016888 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016889 break;
16890 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016891 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016892 break;
16893 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016894 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016895 break;
16896 case VAR_STRING:
16897 if (varp->vval.v_string != NULL)
16898 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016899 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016900 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016901 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016902 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016904 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905}
16906
16907/*
16908 * Find variable "name" in the list of variables.
16909 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016910 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016911 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016912 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016914 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016915find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016917 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016920 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921
Bram Moolenaara7043832005-01-21 11:56:39 +000016922 ht = find_var_ht(name, &varname);
16923 if (htp != NULL)
16924 *htp = ht;
16925 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016927 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928}
16929
16930/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016931 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016932 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016934 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016935find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016936 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016937 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016938 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016939{
Bram Moolenaar33570922005-01-25 22:26:29 +000016940 hashitem_T *hi;
16941
16942 if (*varname == NUL)
16943 {
16944 /* Must be something like "s:", otherwise "ht" would be NULL. */
16945 switch (varname[-2])
16946 {
16947 case 's': return &SCRIPT_SV(current_SID).sv_var;
16948 case 'g': return &globvars_var;
16949 case 'v': return &vimvars_var;
16950 case 'b': return &curbuf->b_bufvar;
16951 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016952 case 'l': return current_funccal == NULL
16953 ? NULL : &current_funccal->l_vars_var;
16954 case 'a': return current_funccal == NULL
16955 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016956 }
16957 return NULL;
16958 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016959
16960 hi = hash_find(ht, varname);
16961 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016962 {
16963 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016964 * worked find the variable again. Don't auto-load a script if it was
16965 * loaded already, otherwise it would be loaded every time when
16966 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016967 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016968 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016969 hi = hash_find(ht, varname);
16970 if (HASHITEM_EMPTY(hi))
16971 return NULL;
16972 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016973 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016974}
16975
16976/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016977 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016978 * Set "varname" to the start of name without ':'.
16979 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016980 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016981find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982 char_u *name;
16983 char_u **varname;
16984{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016985 hashitem_T *hi;
16986
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987 if (name[1] != ':')
16988 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016989 /* The name must not start with a colon or #. */
16990 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 return NULL;
16992 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016993
16994 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016995 hi = hash_find(&compat_hashtab, name);
16996 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016997 return &compat_hashtab;
16998
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017000 return &globvarht; /* global variable */
17001 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 }
17003 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017004 if (*name == 'g') /* global variable */
17005 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017006 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17007 */
17008 if (vim_strchr(name + 2, ':') != NULL
17009 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017010 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017012 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017014 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000017015 if (*name == 'v') /* v: variable */
17016 return &vimvarht;
17017 if (*name == 'a' && current_funccal != NULL) /* function argument */
17018 return &current_funccal->l_avars.dv_hashtab;
17019 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17020 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 if (*name == 's' /* script variable */
17022 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17023 return &SCRIPT_VARS(current_SID);
17024 return NULL;
17025}
17026
17027/*
17028 * Get the string value of a (global/local) variable.
17029 * Returns NULL when it doesn't exist.
17030 */
17031 char_u *
17032get_var_value(name)
17033 char_u *name;
17034{
Bram Moolenaar33570922005-01-25 22:26:29 +000017035 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036
Bram Moolenaara7043832005-01-21 11:56:39 +000017037 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 if (v == NULL)
17039 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017040 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041}
17042
17043/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017044 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 * sourcing this script and when executing functions defined in the script.
17046 */
17047 void
17048new_script_vars(id)
17049 scid_T id;
17050{
Bram Moolenaara7043832005-01-21 11:56:39 +000017051 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017052 hashtab_T *ht;
17053 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017054
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17056 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017057 /* Re-allocating ga_data means that an ht_array pointing to
17058 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017059 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017060 for (i = 1; i <= ga_scripts.ga_len; ++i)
17061 {
17062 ht = &SCRIPT_VARS(i);
17063 if (ht->ht_mask == HT_INIT_SIZE - 1)
17064 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017065 sv = &SCRIPT_SV(i);
17066 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017067 }
17068
Bram Moolenaar071d4272004-06-13 20:20:40 +000017069 while (ga_scripts.ga_len < id)
17070 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017071 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17072 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017073 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074 }
17075 }
17076}
17077
17078/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017079 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17080 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081 */
17082 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017083init_var_dict(dict, dict_var)
17084 dict_T *dict;
17085 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086{
Bram Moolenaar33570922005-01-25 22:26:29 +000017087 hash_init(&dict->dv_hashtab);
17088 dict->dv_refcount = 99999;
17089 dict_var->di_tv.vval.v_dict = dict;
17090 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017091 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017092 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17093 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094}
17095
17096/*
17097 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017098 * Frees all allocated variables and the value they contain.
17099 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100 */
17101 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017102vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017103 hashtab_T *ht;
17104{
17105 vars_clear_ext(ht, TRUE);
17106}
17107
17108/*
17109 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17110 */
17111 static void
17112vars_clear_ext(ht, free_val)
17113 hashtab_T *ht;
17114 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115{
Bram Moolenaara7043832005-01-21 11:56:39 +000017116 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017117 hashitem_T *hi;
17118 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119
Bram Moolenaar33570922005-01-25 22:26:29 +000017120 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000017121 todo = ht->ht_used;
17122 for (hi = ht->ht_array; todo > 0; ++hi)
17123 {
17124 if (!HASHITEM_EMPTY(hi))
17125 {
17126 --todo;
17127
Bram Moolenaar33570922005-01-25 22:26:29 +000017128 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017129 * ht_array might change then. hash_clear() takes care of it
17130 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017131 v = HI2DI(hi);
17132 if (free_val)
17133 clear_tv(&v->di_tv);
17134 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17135 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017136 }
17137 }
17138 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017139 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140}
17141
Bram Moolenaara7043832005-01-21 11:56:39 +000017142/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017143 * Delete a variable from hashtab "ht" at item "hi".
17144 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017147delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017148 hashtab_T *ht;
17149 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150{
Bram Moolenaar33570922005-01-25 22:26:29 +000017151 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017152
17153 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017154 clear_tv(&di->di_tv);
17155 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156}
17157
17158/*
17159 * List the value of one internal variable.
17160 */
17161 static void
17162list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017163 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 char_u *prefix;
17165{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017166 char_u *tofree;
17167 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017168 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017169
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017170 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017171 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017172 s == NULL ? (char_u *)"" : s);
17173 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174}
17175
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 static void
17177list_one_var_a(prefix, name, type, string)
17178 char_u *prefix;
17179 char_u *name;
17180 int type;
17181 char_u *string;
17182{
17183 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17184 if (name != NULL) /* "a:" vars don't have a name stored */
17185 msg_puts(name);
17186 msg_putchar(' ');
17187 msg_advance(22);
17188 if (type == VAR_NUMBER)
17189 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017190 else if (type == VAR_FUNC)
17191 msg_putchar('*');
17192 else if (type == VAR_LIST)
17193 {
17194 msg_putchar('[');
17195 if (*string == '[')
17196 ++string;
17197 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017198 else if (type == VAR_DICT)
17199 {
17200 msg_putchar('{');
17201 if (*string == '{')
17202 ++string;
17203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204 else
17205 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017206
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017208
17209 if (type == VAR_FUNC)
17210 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211}
17212
17213/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017214 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 * If the variable already exists, the value is updated.
17216 * Otherwise the variable is created.
17217 */
17218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017219set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017221 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017222 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223{
Bram Moolenaar33570922005-01-25 22:26:29 +000017224 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017226 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017227 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017229 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017230 {
17231 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17232 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17233 ? name[2] : name[0]))
17234 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017235 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017236 return;
17237 }
17238 if (function_exists(name))
17239 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017240 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017241 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017242 return;
17243 }
17244 }
17245
Bram Moolenaara7043832005-01-21 11:56:39 +000017246 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017247 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017248 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017249 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017250 return;
17251 }
17252
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017253 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017254 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017255 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017256 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017257 if (var_check_ro(v->di_flags, name)
17258 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017259 return;
17260 if (v->di_tv.v_type != tv->v_type
17261 && !((v->di_tv.v_type == VAR_STRING
17262 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017263 && (tv->v_type == VAR_STRING
17264 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017265 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017266 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017267 return;
17268 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017269
17270 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017271 * Handle setting internal v: variables separately: we don't change
17272 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017273 */
17274 if (ht == &vimvarht)
17275 {
17276 if (v->di_tv.v_type == VAR_STRING)
17277 {
17278 vim_free(v->di_tv.vval.v_string);
17279 if (copy || tv->v_type != VAR_STRING)
17280 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17281 else
17282 {
17283 /* Take over the string to avoid an extra alloc/free. */
17284 v->di_tv.vval.v_string = tv->vval.v_string;
17285 tv->vval.v_string = NULL;
17286 }
17287 }
17288 else if (v->di_tv.v_type != VAR_NUMBER)
17289 EMSG2(_(e_intern2), "set_var()");
17290 else
17291 v->di_tv.vval.v_number = get_tv_number(tv);
17292 return;
17293 }
17294
17295 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296 }
17297 else /* add a new variable */
17298 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017299 /* Make sure the variable name is valid. */
17300 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017301 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17302 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017303 {
17304 EMSG2(_(e_illvar), varname);
17305 return;
17306 }
17307
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017308 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17309 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017310 if (v == NULL)
17311 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017312 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017313 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017315 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316 return;
17317 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017318 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017321 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017322 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017323 else
17324 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017325 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017326 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017327 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329}
17330
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017331/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017332 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17333 * Also give an error message.
17334 */
17335 static int
17336var_check_ro(flags, name)
17337 int flags;
17338 char_u *name;
17339{
17340 if (flags & DI_FLAGS_RO)
17341 {
17342 EMSG2(_(e_readonlyvar), name);
17343 return TRUE;
17344 }
17345 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17346 {
17347 EMSG2(_(e_readonlysbx), name);
17348 return TRUE;
17349 }
17350 return FALSE;
17351}
17352
17353/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017354 * Return TRUE if typeval "tv" is set to be locked (immutable).
17355 * Also give an error message, using "name".
17356 */
17357 static int
17358tv_check_lock(lock, name)
17359 int lock;
17360 char_u *name;
17361{
17362 if (lock & VAR_LOCKED)
17363 {
17364 EMSG2(_("E741: Value is locked: %s"),
17365 name == NULL ? (char_u *)_("Unknown") : name);
17366 return TRUE;
17367 }
17368 if (lock & VAR_FIXED)
17369 {
17370 EMSG2(_("E742: Cannot change value of %s"),
17371 name == NULL ? (char_u *)_("Unknown") : name);
17372 return TRUE;
17373 }
17374 return FALSE;
17375}
17376
17377/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017378 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017379 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017380 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017383copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017384 typval_T *from;
17385 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017387 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017388 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017389 switch (from->v_type)
17390 {
17391 case VAR_NUMBER:
17392 to->vval.v_number = from->vval.v_number;
17393 break;
17394 case VAR_STRING:
17395 case VAR_FUNC:
17396 if (from->vval.v_string == NULL)
17397 to->vval.v_string = NULL;
17398 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017399 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017400 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017401 if (from->v_type == VAR_FUNC)
17402 func_ref(to->vval.v_string);
17403 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017404 break;
17405 case VAR_LIST:
17406 if (from->vval.v_list == NULL)
17407 to->vval.v_list = NULL;
17408 else
17409 {
17410 to->vval.v_list = from->vval.v_list;
17411 ++to->vval.v_list->lv_refcount;
17412 }
17413 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017414 case VAR_DICT:
17415 if (from->vval.v_dict == NULL)
17416 to->vval.v_dict = NULL;
17417 else
17418 {
17419 to->vval.v_dict = from->vval.v_dict;
17420 ++to->vval.v_dict->dv_refcount;
17421 }
17422 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017423 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017424 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017425 break;
17426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427}
17428
17429/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017430 * Make a copy of an item.
17431 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017432 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17433 * reference to an already copied list/dict can be used.
17434 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017435 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017436 static int
17437item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017438 typval_T *from;
17439 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017440 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017441 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017442{
17443 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017444 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017445
Bram Moolenaar33570922005-01-25 22:26:29 +000017446 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017447 {
17448 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017449 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017450 }
17451 ++recurse;
17452
17453 switch (from->v_type)
17454 {
17455 case VAR_NUMBER:
17456 case VAR_STRING:
17457 case VAR_FUNC:
17458 copy_tv(from, to);
17459 break;
17460 case VAR_LIST:
17461 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017462 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017463 if (from->vval.v_list == NULL)
17464 to->vval.v_list = NULL;
17465 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17466 {
17467 /* use the copy made earlier */
17468 to->vval.v_list = from->vval.v_list->lv_copylist;
17469 ++to->vval.v_list->lv_refcount;
17470 }
17471 else
17472 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17473 if (to->vval.v_list == NULL)
17474 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017475 break;
17476 case VAR_DICT:
17477 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017478 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017479 if (from->vval.v_dict == NULL)
17480 to->vval.v_dict = NULL;
17481 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17482 {
17483 /* use the copy made earlier */
17484 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17485 ++to->vval.v_dict->dv_refcount;
17486 }
17487 else
17488 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17489 if (to->vval.v_dict == NULL)
17490 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017491 break;
17492 default:
17493 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017494 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017495 }
17496 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017497 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017498}
17499
17500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 * ":echo expr1 ..." print each argument separated with a space, add a
17502 * newline at the end.
17503 * ":echon expr1 ..." print each argument plain.
17504 */
17505 void
17506ex_echo(eap)
17507 exarg_T *eap;
17508{
17509 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017510 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017511 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 char_u *p;
17513 int needclr = TRUE;
17514 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017515 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516
17517 if (eap->skip)
17518 ++emsg_skip;
17519 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17520 {
17521 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017522 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 {
17524 /*
17525 * Report the invalid expression unless the expression evaluation
17526 * has been cancelled due to an aborting error, an interrupt, or an
17527 * exception.
17528 */
17529 if (!aborting())
17530 EMSG2(_(e_invexpr2), p);
17531 break;
17532 }
17533 if (!eap->skip)
17534 {
17535 if (atstart)
17536 {
17537 atstart = FALSE;
17538 /* Call msg_start() after eval1(), evaluating the expression
17539 * may cause a message to appear. */
17540 if (eap->cmdidx == CMD_echo)
17541 msg_start();
17542 }
17543 else if (eap->cmdidx == CMD_echo)
17544 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017545 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017546 if (p != NULL)
17547 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017549 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017551 if (*p != TAB && needclr)
17552 {
17553 /* remove any text still there from the command */
17554 msg_clr_eos();
17555 needclr = FALSE;
17556 }
17557 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 }
17559 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017560 {
17561#ifdef FEAT_MBYTE
17562 if (has_mbyte)
17563 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017564 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017565
17566 (void)msg_outtrans_len_attr(p, i, echo_attr);
17567 p += i - 1;
17568 }
17569 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017571 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017574 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017576 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 arg = skipwhite(arg);
17578 }
17579 eap->nextcmd = check_nextcmd(arg);
17580
17581 if (eap->skip)
17582 --emsg_skip;
17583 else
17584 {
17585 /* remove text that may still be there from the command */
17586 if (needclr)
17587 msg_clr_eos();
17588 if (eap->cmdidx == CMD_echo)
17589 msg_end();
17590 }
17591}
17592
17593/*
17594 * ":echohl {name}".
17595 */
17596 void
17597ex_echohl(eap)
17598 exarg_T *eap;
17599{
17600 int id;
17601
17602 id = syn_name2id(eap->arg);
17603 if (id == 0)
17604 echo_attr = 0;
17605 else
17606 echo_attr = syn_id2attr(id);
17607}
17608
17609/*
17610 * ":execute expr1 ..." execute the result of an expression.
17611 * ":echomsg expr1 ..." Print a message
17612 * ":echoerr expr1 ..." Print an error
17613 * Each gets spaces around each argument and a newline at the end for
17614 * echo commands
17615 */
17616 void
17617ex_execute(eap)
17618 exarg_T *eap;
17619{
17620 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017621 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 int ret = OK;
17623 char_u *p;
17624 garray_T ga;
17625 int len;
17626 int save_did_emsg;
17627
17628 ga_init2(&ga, 1, 80);
17629
17630 if (eap->skip)
17631 ++emsg_skip;
17632 while (*arg != NUL && *arg != '|' && *arg != '\n')
17633 {
17634 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017635 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 {
17637 /*
17638 * Report the invalid expression unless the expression evaluation
17639 * has been cancelled due to an aborting error, an interrupt, or an
17640 * exception.
17641 */
17642 if (!aborting())
17643 EMSG2(_(e_invexpr2), p);
17644 ret = FAIL;
17645 break;
17646 }
17647
17648 if (!eap->skip)
17649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017650 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 len = (int)STRLEN(p);
17652 if (ga_grow(&ga, len + 2) == FAIL)
17653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017654 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 ret = FAIL;
17656 break;
17657 }
17658 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 ga.ga_len += len;
17662 }
17663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017664 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 arg = skipwhite(arg);
17666 }
17667
17668 if (ret != FAIL && ga.ga_data != NULL)
17669 {
17670 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017671 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017673 out_flush();
17674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 else if (eap->cmdidx == CMD_echoerr)
17676 {
17677 /* We don't want to abort following commands, restore did_emsg. */
17678 save_did_emsg = did_emsg;
17679 EMSG((char_u *)ga.ga_data);
17680 if (!force_abort)
17681 did_emsg = save_did_emsg;
17682 }
17683 else if (eap->cmdidx == CMD_execute)
17684 do_cmdline((char_u *)ga.ga_data,
17685 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17686 }
17687
17688 ga_clear(&ga);
17689
17690 if (eap->skip)
17691 --emsg_skip;
17692
17693 eap->nextcmd = check_nextcmd(arg);
17694}
17695
17696/*
17697 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17698 * "arg" points to the "&" or '+' when called, to "option" when returning.
17699 * Returns NULL when no option name found. Otherwise pointer to the char
17700 * after the option name.
17701 */
17702 static char_u *
17703find_option_end(arg, opt_flags)
17704 char_u **arg;
17705 int *opt_flags;
17706{
17707 char_u *p = *arg;
17708
17709 ++p;
17710 if (*p == 'g' && p[1] == ':')
17711 {
17712 *opt_flags = OPT_GLOBAL;
17713 p += 2;
17714 }
17715 else if (*p == 'l' && p[1] == ':')
17716 {
17717 *opt_flags = OPT_LOCAL;
17718 p += 2;
17719 }
17720 else
17721 *opt_flags = 0;
17722
17723 if (!ASCII_ISALPHA(*p))
17724 return NULL;
17725 *arg = p;
17726
17727 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17728 p += 4; /* termcap option */
17729 else
17730 while (ASCII_ISALPHA(*p))
17731 ++p;
17732 return p;
17733}
17734
17735/*
17736 * ":function"
17737 */
17738 void
17739ex_function(eap)
17740 exarg_T *eap;
17741{
17742 char_u *theline;
17743 int j;
17744 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 char_u *name = NULL;
17747 char_u *p;
17748 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017749 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 garray_T newargs;
17751 garray_T newlines;
17752 int varargs = FALSE;
17753 int mustend = FALSE;
17754 int flags = 0;
17755 ufunc_T *fp;
17756 int indent;
17757 int nesting;
17758 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017759 dictitem_T *v;
17760 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017761 static int func_nr = 0; /* number for nameless function */
17762 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017763 hashtab_T *ht;
17764 int todo;
17765 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017766 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767
17768 /*
17769 * ":function" without argument: list functions.
17770 */
17771 if (ends_excmd(*eap->arg))
17772 {
17773 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017774 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017775 todo = func_hashtab.ht_used;
17776 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017777 {
17778 if (!HASHITEM_EMPTY(hi))
17779 {
17780 --todo;
17781 fp = HI2UF(hi);
17782 if (!isdigit(*fp->uf_name))
17783 list_func_head(fp, FALSE);
17784 }
17785 }
17786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 eap->nextcmd = check_nextcmd(eap->arg);
17788 return;
17789 }
17790
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017791 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017792 * ":function /pat": list functions matching pattern.
17793 */
17794 if (*eap->arg == '/')
17795 {
17796 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17797 if (!eap->skip)
17798 {
17799 regmatch_T regmatch;
17800
17801 c = *p;
17802 *p = NUL;
17803 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17804 *p = c;
17805 if (regmatch.regprog != NULL)
17806 {
17807 regmatch.rm_ic = p_ic;
17808
17809 todo = func_hashtab.ht_used;
17810 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17811 {
17812 if (!HASHITEM_EMPTY(hi))
17813 {
17814 --todo;
17815 fp = HI2UF(hi);
17816 if (!isdigit(*fp->uf_name)
17817 && vim_regexec(&regmatch, fp->uf_name, 0))
17818 list_func_head(fp, FALSE);
17819 }
17820 }
17821 }
17822 }
17823 if (*p == '/')
17824 ++p;
17825 eap->nextcmd = check_nextcmd(p);
17826 return;
17827 }
17828
17829 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017830 * Get the function name. There are these situations:
17831 * func normal function name
17832 * "name" == func, "fudi.fd_dict" == NULL
17833 * dict.func new dictionary entry
17834 * "name" == NULL, "fudi.fd_dict" set,
17835 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17836 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017837 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017838 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17839 * dict.func existing dict entry that's not a Funcref
17840 * "name" == NULL, "fudi.fd_dict" set,
17841 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017844 name = trans_function_name(&p, eap->skip, 0, &fudi);
17845 paren = (vim_strchr(p, '(') != NULL);
17846 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 {
17848 /*
17849 * Return on an invalid expression in braces, unless the expression
17850 * evaluation has been cancelled due to an aborting error, an
17851 * interrupt, or an exception.
17852 */
17853 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017854 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017855 if (!eap->skip && fudi.fd_newkey != NULL)
17856 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017857 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 else
17861 eap->skip = TRUE;
17862 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000017863
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864 /* An error in a function call during evaluation of an expression in magic
17865 * braces should not cause the function not to be defined. */
17866 saved_did_emsg = did_emsg;
17867 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868
17869 /*
17870 * ":function func" with only function name: list function.
17871 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017872 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 {
17874 if (!ends_excmd(*skipwhite(p)))
17875 {
17876 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017877 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878 }
17879 eap->nextcmd = check_nextcmd(p);
17880 if (eap->nextcmd != NULL)
17881 *p = NUL;
17882 if (!eap->skip && !got_int)
17883 {
17884 fp = find_func(name);
17885 if (fp != NULL)
17886 {
17887 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017888 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017890 if (FUNCLINE(fp, j) == NULL)
17891 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 msg_putchar('\n');
17893 msg_outnum((long)(j + 1));
17894 if (j < 9)
17895 msg_putchar(' ');
17896 if (j < 99)
17897 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017898 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899 out_flush(); /* show a line at a time */
17900 ui_breakcheck();
17901 }
17902 if (!got_int)
17903 {
17904 msg_putchar('\n');
17905 msg_puts((char_u *)" endfunction");
17906 }
17907 }
17908 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017909 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017911 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 }
17913
17914 /*
17915 * ":function name(arg1, arg2)" Define function.
17916 */
17917 p = skipwhite(p);
17918 if (*p != '(')
17919 {
17920 if (!eap->skip)
17921 {
17922 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017923 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924 }
17925 /* attempt to continue by skipping some text */
17926 if (vim_strchr(p, '(') != NULL)
17927 p = vim_strchr(p, '(');
17928 }
17929 p = skipwhite(p + 1);
17930
17931 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17932 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17933
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017934 if (!eap->skip)
17935 {
17936 /* Check the name of the function. */
17937 if (name != NULL)
17938 arg = name;
17939 else
17940 arg = fudi.fd_newkey;
17941 if (arg != NULL)
17942 {
17943 if (*arg == K_SPECIAL)
17944 j = 3;
17945 else
17946 j = 0;
17947 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17948 : eval_isnamec(arg[j])))
17949 ++j;
17950 if (arg[j] != NUL)
17951 emsg_funcname(_(e_invarg2), arg);
17952 }
17953 }
17954
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955 /*
17956 * Isolate the arguments: "arg1, arg2, ...)"
17957 */
17958 while (*p != ')')
17959 {
17960 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17961 {
17962 varargs = TRUE;
17963 p += 3;
17964 mustend = TRUE;
17965 }
17966 else
17967 {
17968 arg = p;
17969 while (ASCII_ISALNUM(*p) || *p == '_')
17970 ++p;
17971 if (arg == p || isdigit(*arg)
17972 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17973 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17974 {
17975 if (!eap->skip)
17976 EMSG2(_("E125: Illegal argument: %s"), arg);
17977 break;
17978 }
17979 if (ga_grow(&newargs, 1) == FAIL)
17980 goto erret;
17981 c = *p;
17982 *p = NUL;
17983 arg = vim_strsave(arg);
17984 if (arg == NULL)
17985 goto erret;
17986 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17987 *p = c;
17988 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 if (*p == ',')
17990 ++p;
17991 else
17992 mustend = TRUE;
17993 }
17994 p = skipwhite(p);
17995 if (mustend && *p != ')')
17996 {
17997 if (!eap->skip)
17998 EMSG2(_(e_invarg2), eap->arg);
17999 break;
18000 }
18001 }
18002 ++p; /* skip the ')' */
18003
Bram Moolenaare9a41262005-01-15 22:18:47 +000018004 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005 for (;;)
18006 {
18007 p = skipwhite(p);
18008 if (STRNCMP(p, "range", 5) == 0)
18009 {
18010 flags |= FC_RANGE;
18011 p += 5;
18012 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018013 else if (STRNCMP(p, "dict", 4) == 0)
18014 {
18015 flags |= FC_DICT;
18016 p += 4;
18017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018 else if (STRNCMP(p, "abort", 5) == 0)
18019 {
18020 flags |= FC_ABORT;
18021 p += 5;
18022 }
18023 else
18024 break;
18025 }
18026
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018027 /* When there is a line break use what follows for the function body.
18028 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18029 if (*p == '\n')
18030 line_arg = p + 1;
18031 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 EMSG(_(e_trailing));
18033
18034 /*
18035 * Read the body of the function, until ":endfunction" is found.
18036 */
18037 if (KeyTyped)
18038 {
18039 /* Check if the function already exists, don't let the user type the
18040 * whole function before telling him it doesn't work! For a script we
18041 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018042 if (!eap->skip && !eap->forceit)
18043 {
18044 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18045 EMSG(_(e_funcdict));
18046 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018047 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018050 if (!eap->skip && did_emsg)
18051 goto erret;
18052
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 msg_putchar('\n'); /* don't overwrite the function name */
18054 cmdline_row = msg_row;
18055 }
18056
18057 indent = 2;
18058 nesting = 0;
18059 for (;;)
18060 {
18061 msg_scroll = TRUE;
18062 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018063 sourcing_lnum_off = sourcing_lnum;
18064
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018065 if (line_arg != NULL)
18066 {
18067 /* Use eap->arg, split up in parts by line breaks. */
18068 theline = line_arg;
18069 p = vim_strchr(theline, '\n');
18070 if (p == NULL)
18071 line_arg += STRLEN(line_arg);
18072 else
18073 {
18074 *p = NUL;
18075 line_arg = p + 1;
18076 }
18077 }
18078 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 theline = getcmdline(':', 0L, indent);
18080 else
18081 theline = eap->getline(':', eap->cookie, indent);
18082 if (KeyTyped)
18083 lines_left = Rows - 1;
18084 if (theline == NULL)
18085 {
18086 EMSG(_("E126: Missing :endfunction"));
18087 goto erret;
18088 }
18089
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018090 /* Detect line continuation: sourcing_lnum increased more than one. */
18091 if (sourcing_lnum > sourcing_lnum_off + 1)
18092 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18093 else
18094 sourcing_lnum_off = 0;
18095
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 if (skip_until != NULL)
18097 {
18098 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18099 * don't check for ":endfunc". */
18100 if (STRCMP(theline, skip_until) == 0)
18101 {
18102 vim_free(skip_until);
18103 skip_until = NULL;
18104 }
18105 }
18106 else
18107 {
18108 /* skip ':' and blanks*/
18109 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18110 ;
18111
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018112 /* Check for "endfunction". */
18113 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018114 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018115 if (line_arg == NULL)
18116 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 break;
18118 }
18119
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018120 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121 * at "end". */
18122 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18123 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018124 else if (STRNCMP(p, "if", 2) == 0
18125 || STRNCMP(p, "wh", 2) == 0
18126 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127 || STRNCMP(p, "try", 3) == 0)
18128 indent += 2;
18129
18130 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018131 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018133 if (*p == '!')
18134 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135 p += eval_fname_script(p);
18136 if (ASCII_ISALPHA(*p))
18137 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018138 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 if (*skipwhite(p) == '(')
18140 {
18141 ++nesting;
18142 indent += 2;
18143 }
18144 }
18145 }
18146
18147 /* Check for ":append" or ":insert". */
18148 p = skip_range(p, NULL);
18149 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18150 || (p[0] == 'i'
18151 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18152 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18153 skip_until = vim_strsave((char_u *)".");
18154
18155 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18156 arg = skipwhite(skiptowhite(p));
18157 if (arg[0] == '<' && arg[1] =='<'
18158 && ((p[0] == 'p' && p[1] == 'y'
18159 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18160 || (p[0] == 'p' && p[1] == 'e'
18161 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18162 || (p[0] == 't' && p[1] == 'c'
18163 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18164 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18165 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018166 || (p[0] == 'm' && p[1] == 'z'
18167 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 ))
18169 {
18170 /* ":python <<" continues until a dot, like ":append" */
18171 p = skipwhite(arg + 2);
18172 if (*p == NUL)
18173 skip_until = vim_strsave((char_u *)".");
18174 else
18175 skip_until = vim_strsave(p);
18176 }
18177 }
18178
18179 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018180 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018181 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018182 if (line_arg == NULL)
18183 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018185 }
18186
18187 /* Copy the line to newly allocated memory. get_one_sourceline()
18188 * allocates 250 bytes per line, this saves 80% on average. The cost
18189 * is an extra alloc/free. */
18190 p = vim_strsave(theline);
18191 if (p != NULL)
18192 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018193 if (line_arg == NULL)
18194 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018195 theline = p;
18196 }
18197
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018198 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18199
18200 /* Add NULL lines for continuation lines, so that the line count is
18201 * equal to the index in the growarray. */
18202 while (sourcing_lnum_off-- > 0)
18203 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018204
18205 /* Check for end of eap->arg. */
18206 if (line_arg != NULL && *line_arg == NUL)
18207 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208 }
18209
18210 /* Don't define the function when skipping commands or when an error was
18211 * detected. */
18212 if (eap->skip || did_emsg)
18213 goto erret;
18214
18215 /*
18216 * If there are no errors, add the function
18217 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018218 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018219 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018220 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018221 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018222 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018223 emsg_funcname("E707: Function name conflicts with variable: %s",
18224 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018225 goto erret;
18226 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018227
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018228 fp = find_func(name);
18229 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018231 if (!eap->forceit)
18232 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018233 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018234 goto erret;
18235 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018236 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018237 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018238 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018239 name);
18240 goto erret;
18241 }
18242 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018243 ga_clear_strings(&(fp->uf_args));
18244 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018245 vim_free(name);
18246 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248 }
18249 else
18250 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018251 char numbuf[20];
18252
18253 fp = NULL;
18254 if (fudi.fd_newkey == NULL && !eap->forceit)
18255 {
18256 EMSG(_(e_funcdict));
18257 goto erret;
18258 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018259 if (fudi.fd_di == NULL)
18260 {
18261 /* Can't add a function to a locked dictionary */
18262 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18263 goto erret;
18264 }
18265 /* Can't change an existing function if it is locked */
18266 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18267 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018268
18269 /* Give the function a sequential number. Can only be used with a
18270 * Funcref! */
18271 vim_free(name);
18272 sprintf(numbuf, "%d", ++func_nr);
18273 name = vim_strsave((char_u *)numbuf);
18274 if (name == NULL)
18275 goto erret;
18276 }
18277
18278 if (fp == NULL)
18279 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018280 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018281 {
18282 int slen, plen;
18283 char_u *scriptname;
18284
18285 /* Check that the autoload name matches the script name. */
18286 j = FAIL;
18287 if (sourcing_name != NULL)
18288 {
18289 scriptname = autoload_name(name);
18290 if (scriptname != NULL)
18291 {
18292 p = vim_strchr(scriptname, '/');
18293 plen = STRLEN(p);
18294 slen = STRLEN(sourcing_name);
18295 if (slen > plen && fnamecmp(p,
18296 sourcing_name + slen - plen) == 0)
18297 j = OK;
18298 vim_free(scriptname);
18299 }
18300 }
18301 if (j == FAIL)
18302 {
18303 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18304 goto erret;
18305 }
18306 }
18307
18308 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309 if (fp == NULL)
18310 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018311
18312 if (fudi.fd_dict != NULL)
18313 {
18314 if (fudi.fd_di == NULL)
18315 {
18316 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018317 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018318 if (fudi.fd_di == NULL)
18319 {
18320 vim_free(fp);
18321 goto erret;
18322 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018323 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18324 {
18325 vim_free(fudi.fd_di);
18326 goto erret;
18327 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018328 }
18329 else
18330 /* overwrite existing dict entry */
18331 clear_tv(&fudi.fd_di->di_tv);
18332 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018333 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018334 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018335 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018336 }
18337
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018339 STRCPY(fp->uf_name, name);
18340 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018342 fp->uf_args = newargs;
18343 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018344#ifdef FEAT_PROFILE
18345 fp->uf_tml_count = NULL;
18346 fp->uf_tml_total = NULL;
18347 fp->uf_tml_self = NULL;
18348 fp->uf_profiling = FALSE;
18349 if (prof_def_func())
18350 func_do_profile(fp);
18351#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018352 fp->uf_varargs = varargs;
18353 fp->uf_flags = flags;
18354 fp->uf_calls = 0;
18355 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018356 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357
18358erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 ga_clear_strings(&newargs);
18360 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018361ret_free:
18362 vim_free(skip_until);
18363 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366}
18367
18368/*
18369 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018370 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018371 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018372 * flags:
18373 * TFN_INT: internal function name OK
18374 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 * Advances "pp" to just after the function name (if no error).
18376 */
18377 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018378trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 char_u **pp;
18380 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018381 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018382 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018384 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385 char_u *start;
18386 char_u *end;
18387 int lead;
18388 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018390 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018391
18392 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018393 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018395
18396 /* Check for hard coded <SNR>: already translated function ID (from a user
18397 * command). */
18398 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18399 && (*pp)[2] == (int)KE_SNR)
18400 {
18401 *pp += 3;
18402 len = get_id_len(pp) + 3;
18403 return vim_strnsave(start, len);
18404 }
18405
18406 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18407 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018409 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018410 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018411
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018412 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18413 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018414 if (end == start)
18415 {
18416 if (!skip)
18417 EMSG(_("E129: Function name required"));
18418 goto theend;
18419 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018420 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018421 {
18422 /*
18423 * Report an invalid expression in braces, unless the expression
18424 * evaluation has been cancelled due to an aborting error, an
18425 * interrupt, or an exception.
18426 */
18427 if (!aborting())
18428 {
18429 if (end != NULL)
18430 EMSG2(_(e_invarg2), start);
18431 }
18432 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018433 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018434 goto theend;
18435 }
18436
18437 if (lv.ll_tv != NULL)
18438 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018439 if (fdp != NULL)
18440 {
18441 fdp->fd_dict = lv.ll_dict;
18442 fdp->fd_newkey = lv.ll_newkey;
18443 lv.ll_newkey = NULL;
18444 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018445 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018446 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18447 {
18448 name = vim_strsave(lv.ll_tv->vval.v_string);
18449 *pp = end;
18450 }
18451 else
18452 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018453 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18454 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018455 EMSG(_(e_funcref));
18456 else
18457 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018458 name = NULL;
18459 }
18460 goto theend;
18461 }
18462
18463 if (lv.ll_name == NULL)
18464 {
18465 /* Error found, but continue after the function name. */
18466 *pp = end;
18467 goto theend;
18468 }
18469
18470 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018471 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018472 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018473 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18474 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18475 {
18476 /* When there was "s:" already or the name expanded to get a
18477 * leading "s:" then remove it. */
18478 lv.ll_name += 2;
18479 len -= 2;
18480 lead = 2;
18481 }
18482 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018483 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018484 {
18485 if (lead == 2) /* skip over "s:" */
18486 lv.ll_name += 2;
18487 len = (int)(end - lv.ll_name);
18488 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018489
18490 /*
18491 * Copy the function name to allocated memory.
18492 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18493 * Accept <SNR>123_name() outside a script.
18494 */
18495 if (skip)
18496 lead = 0; /* do nothing */
18497 else if (lead > 0)
18498 {
18499 lead = 3;
18500 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18501 {
18502 if (current_SID <= 0)
18503 {
18504 EMSG(_(e_usingsid));
18505 goto theend;
18506 }
18507 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18508 lead += (int)STRLEN(sid_buf);
18509 }
18510 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018511 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018512 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018513 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018514 goto theend;
18515 }
18516 name = alloc((unsigned)(len + lead + 1));
18517 if (name != NULL)
18518 {
18519 if (lead > 0)
18520 {
18521 name[0] = K_SPECIAL;
18522 name[1] = KS_EXTRA;
18523 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018524 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018525 STRCPY(name + 3, sid_buf);
18526 }
18527 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18528 name[len + lead] = NUL;
18529 }
18530 *pp = end;
18531
18532theend:
18533 clear_lval(&lv);
18534 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535}
18536
18537/*
18538 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18539 * Return 2 if "p" starts with "s:".
18540 * Return 0 otherwise.
18541 */
18542 static int
18543eval_fname_script(p)
18544 char_u *p;
18545{
18546 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18547 || STRNICMP(p + 1, "SNR>", 4) == 0))
18548 return 5;
18549 if (p[0] == 's' && p[1] == ':')
18550 return 2;
18551 return 0;
18552}
18553
18554/*
18555 * Return TRUE if "p" starts with "<SID>" or "s:".
18556 * Only works if eval_fname_script() returned non-zero for "p"!
18557 */
18558 static int
18559eval_fname_sid(p)
18560 char_u *p;
18561{
18562 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18563}
18564
18565/*
18566 * List the head of the function: "name(arg1, arg2)".
18567 */
18568 static void
18569list_func_head(fp, indent)
18570 ufunc_T *fp;
18571 int indent;
18572{
18573 int j;
18574
18575 msg_start();
18576 if (indent)
18577 MSG_PUTS(" ");
18578 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018579 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 {
18581 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018582 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583 }
18584 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018585 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018587 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588 {
18589 if (j)
18590 MSG_PUTS(", ");
18591 msg_puts(FUNCARG(fp, j));
18592 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018593 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594 {
18595 if (j)
18596 MSG_PUTS(", ");
18597 MSG_PUTS("...");
18598 }
18599 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018600 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018601 if (p_verbose > 0)
18602 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603}
18604
18605/*
18606 * Find a function by name, return pointer to it in ufuncs.
18607 * Return NULL for unknown function.
18608 */
18609 static ufunc_T *
18610find_func(name)
18611 char_u *name;
18612{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018613 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018615 hi = hash_find(&func_hashtab, name);
18616 if (!HASHITEM_EMPTY(hi))
18617 return HI2UF(hi);
18618 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619}
18620
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018621#if defined(EXITFREE) || defined(PROTO)
18622 void
18623free_all_functions()
18624{
18625 hashitem_T *hi;
18626
18627 /* Need to start all over every time, because func_free() may change the
18628 * hash table. */
18629 while (func_hashtab.ht_used > 0)
18630 for (hi = func_hashtab.ht_array; ; ++hi)
18631 if (!HASHITEM_EMPTY(hi))
18632 {
18633 func_free(HI2UF(hi));
18634 break;
18635 }
18636}
18637#endif
18638
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018639/*
18640 * Return TRUE if a function "name" exists.
18641 */
18642 static int
18643function_exists(name)
18644 char_u *name;
18645{
18646 char_u *p = name;
18647 int n = FALSE;
18648
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018649 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018650 if (p != NULL)
18651 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018652 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018653 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018654 else
18655 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018656 vim_free(p);
18657 }
18658 return n;
18659}
18660
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018661/*
18662 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018663 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018664 */
18665 static int
18666builtin_function(name)
18667 char_u *name;
18668{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018669 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18670 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018671}
18672
Bram Moolenaar05159a02005-02-26 23:04:13 +000018673#if defined(FEAT_PROFILE) || defined(PROTO)
18674/*
18675 * Start profiling function "fp".
18676 */
18677 static void
18678func_do_profile(fp)
18679 ufunc_T *fp;
18680{
18681 fp->uf_tm_count = 0;
18682 profile_zero(&fp->uf_tm_self);
18683 profile_zero(&fp->uf_tm_total);
18684 if (fp->uf_tml_count == NULL)
18685 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18686 (sizeof(int) * fp->uf_lines.ga_len));
18687 if (fp->uf_tml_total == NULL)
18688 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18689 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18690 if (fp->uf_tml_self == NULL)
18691 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18692 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18693 fp->uf_tml_idx = -1;
18694 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18695 || fp->uf_tml_self == NULL)
18696 return; /* out of memory */
18697
18698 fp->uf_profiling = TRUE;
18699}
18700
18701/*
18702 * Dump the profiling results for all functions in file "fd".
18703 */
18704 void
18705func_dump_profile(fd)
18706 FILE *fd;
18707{
18708 hashitem_T *hi;
18709 int todo;
18710 ufunc_T *fp;
18711 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018712 ufunc_T **sorttab;
18713 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018714
18715 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018716 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18717
Bram Moolenaar05159a02005-02-26 23:04:13 +000018718 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18719 {
18720 if (!HASHITEM_EMPTY(hi))
18721 {
18722 --todo;
18723 fp = HI2UF(hi);
18724 if (fp->uf_profiling)
18725 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018726 if (sorttab != NULL)
18727 sorttab[st_len++] = fp;
18728
Bram Moolenaar05159a02005-02-26 23:04:13 +000018729 if (fp->uf_name[0] == K_SPECIAL)
18730 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18731 else
18732 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18733 if (fp->uf_tm_count == 1)
18734 fprintf(fd, "Called 1 time\n");
18735 else
18736 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18737 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18738 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18739 fprintf(fd, "\n");
18740 fprintf(fd, "count total (s) self (s)\n");
18741
18742 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18743 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018744 if (FUNCLINE(fp, i) == NULL)
18745 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000018746 prof_func_line(fd, fp->uf_tml_count[i],
18747 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018748 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18749 }
18750 fprintf(fd, "\n");
18751 }
18752 }
18753 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018754
18755 if (sorttab != NULL && st_len > 0)
18756 {
18757 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18758 prof_total_cmp);
18759 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18760 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18761 prof_self_cmp);
18762 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18763 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018764}
Bram Moolenaar73830342005-02-28 22:48:19 +000018765
18766 static void
18767prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18768 FILE *fd;
18769 ufunc_T **sorttab;
18770 int st_len;
18771 char *title;
18772 int prefer_self; /* when equal print only self time */
18773{
18774 int i;
18775 ufunc_T *fp;
18776
18777 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18778 fprintf(fd, "count total (s) self (s) function\n");
18779 for (i = 0; i < 20 && i < st_len; ++i)
18780 {
18781 fp = sorttab[i];
18782 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18783 prefer_self);
18784 if (fp->uf_name[0] == K_SPECIAL)
18785 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18786 else
18787 fprintf(fd, " %s()\n", fp->uf_name);
18788 }
18789 fprintf(fd, "\n");
18790}
18791
18792/*
18793 * Print the count and times for one function or function line.
18794 */
18795 static void
18796prof_func_line(fd, count, total, self, prefer_self)
18797 FILE *fd;
18798 int count;
18799 proftime_T *total;
18800 proftime_T *self;
18801 int prefer_self; /* when equal print only self time */
18802{
18803 if (count > 0)
18804 {
18805 fprintf(fd, "%5d ", count);
18806 if (prefer_self && profile_equal(total, self))
18807 fprintf(fd, " ");
18808 else
18809 fprintf(fd, "%s ", profile_msg(total));
18810 if (!prefer_self && profile_equal(total, self))
18811 fprintf(fd, " ");
18812 else
18813 fprintf(fd, "%s ", profile_msg(self));
18814 }
18815 else
18816 fprintf(fd, " ");
18817}
18818
18819/*
18820 * Compare function for total time sorting.
18821 */
18822 static int
18823#ifdef __BORLANDC__
18824_RTLENTRYF
18825#endif
18826prof_total_cmp(s1, s2)
18827 const void *s1;
18828 const void *s2;
18829{
18830 ufunc_T *p1, *p2;
18831
18832 p1 = *(ufunc_T **)s1;
18833 p2 = *(ufunc_T **)s2;
18834 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18835}
18836
18837/*
18838 * Compare function for self time sorting.
18839 */
18840 static int
18841#ifdef __BORLANDC__
18842_RTLENTRYF
18843#endif
18844prof_self_cmp(s1, s2)
18845 const void *s1;
18846 const void *s2;
18847{
18848 ufunc_T *p1, *p2;
18849
18850 p1 = *(ufunc_T **)s1;
18851 p2 = *(ufunc_T **)s2;
18852 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18853}
18854
Bram Moolenaar05159a02005-02-26 23:04:13 +000018855#endif
18856
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018857/* The names of packages that once were loaded is remembered. */
18858static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18859
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018860/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018861 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018862 * Return TRUE if a package was loaded.
18863 */
18864 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018865script_autoload(name, reload)
18866 char_u *name;
18867 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018868{
18869 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018870 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018871 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018872 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018873
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018874 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018875 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018876 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018877 return FALSE;
18878
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018879 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018880
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018881 /* Find the name in the list of previously loaded package names. Skip
18882 * "autoload/", it's always the same. */
18883 for (i = 0; i < ga_loaded.ga_len; ++i)
18884 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18885 break;
18886 if (!reload && i < ga_loaded.ga_len)
18887 ret = FALSE; /* was loaded already */
18888 else
18889 {
18890 /* Remember the name if it wasn't loaded already. */
18891 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18892 {
18893 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18894 tofree = NULL;
18895 }
18896
18897 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018898 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018899 ret = TRUE;
18900 }
18901
18902 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018903 return ret;
18904}
18905
18906/*
18907 * Return the autoload script name for a function or variable name.
18908 * Returns NULL when out of memory.
18909 */
18910 static char_u *
18911autoload_name(name)
18912 char_u *name;
18913{
18914 char_u *p;
18915 char_u *scriptname;
18916
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018917 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018918 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18919 if (scriptname == NULL)
18920 return FALSE;
18921 STRCPY(scriptname, "autoload/");
18922 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018923 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018924 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018925 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018926 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018927 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018928}
18929
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18931
18932/*
18933 * Function given to ExpandGeneric() to obtain the list of user defined
18934 * function names.
18935 */
18936 char_u *
18937get_user_func_name(xp, idx)
18938 expand_T *xp;
18939 int idx;
18940{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018941 static long_u done;
18942 static hashitem_T *hi;
18943 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944
18945 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018947 done = 0;
18948 hi = func_hashtab.ht_array;
18949 }
18950 if (done < func_hashtab.ht_used)
18951 {
18952 if (done++ > 0)
18953 ++hi;
18954 while (HASHITEM_EMPTY(hi))
18955 ++hi;
18956 fp = HI2UF(hi);
18957
18958 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18959 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960
18961 cat_func_name(IObuff, fp);
18962 if (xp->xp_context != EXPAND_USER_FUNC)
18963 {
18964 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018965 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966 STRCAT(IObuff, ")");
18967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 return IObuff;
18969 }
18970 return NULL;
18971}
18972
18973#endif /* FEAT_CMDL_COMPL */
18974
18975/*
18976 * Copy the function name of "fp" to buffer "buf".
18977 * "buf" must be able to hold the function name plus three bytes.
18978 * Takes care of script-local function names.
18979 */
18980 static void
18981cat_func_name(buf, fp)
18982 char_u *buf;
18983 ufunc_T *fp;
18984{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018985 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 {
18987 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018988 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 }
18990 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018991 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992}
18993
18994/*
18995 * ":delfunction {name}"
18996 */
18997 void
18998ex_delfunction(eap)
18999 exarg_T *eap;
19000{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019001 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002 char_u *p;
19003 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019004 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005
19006 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019007 name = trans_function_name(&p, eap->skip, 0, &fudi);
19008 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019010 {
19011 if (fudi.fd_dict != NULL && !eap->skip)
19012 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 if (!ends_excmd(*skipwhite(p)))
19016 {
19017 vim_free(name);
19018 EMSG(_(e_trailing));
19019 return;
19020 }
19021 eap->nextcmd = check_nextcmd(p);
19022 if (eap->nextcmd != NULL)
19023 *p = NUL;
19024
19025 if (!eap->skip)
19026 fp = find_func(name);
19027 vim_free(name);
19028
19029 if (!eap->skip)
19030 {
19031 if (fp == NULL)
19032 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019033 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034 return;
19035 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019036 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037 {
19038 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19039 return;
19040 }
19041
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019042 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019044 /* Delete the dict item that refers to the function, it will
19045 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019046 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019048 else
19049 func_free(fp);
19050 }
19051}
19052
19053/*
19054 * Free a function and remove it from the list of functions.
19055 */
19056 static void
19057func_free(fp)
19058 ufunc_T *fp;
19059{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019060 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019061
19062 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019063 ga_clear_strings(&(fp->uf_args));
19064 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019065#ifdef FEAT_PROFILE
19066 vim_free(fp->uf_tml_count);
19067 vim_free(fp->uf_tml_total);
19068 vim_free(fp->uf_tml_self);
19069#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019070
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019071 /* remove the function from the function hashtable */
19072 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19073 if (HASHITEM_EMPTY(hi))
19074 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019075 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019076 hash_remove(&func_hashtab, hi);
19077
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019078 vim_free(fp);
19079}
19080
19081/*
19082 * Unreference a Function: decrement the reference count and free it when it
19083 * becomes zero. Only for numbered functions.
19084 */
19085 static void
19086func_unref(name)
19087 char_u *name;
19088{
19089 ufunc_T *fp;
19090
19091 if (name != NULL && isdigit(*name))
19092 {
19093 fp = find_func(name);
19094 if (fp == NULL)
19095 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019096 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019097 {
19098 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019099 * when "uf_calls" becomes zero. */
19100 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019101 func_free(fp);
19102 }
19103 }
19104}
19105
19106/*
19107 * Count a reference to a Function.
19108 */
19109 static void
19110func_ref(name)
19111 char_u *name;
19112{
19113 ufunc_T *fp;
19114
19115 if (name != NULL && isdigit(*name))
19116 {
19117 fp = find_func(name);
19118 if (fp == NULL)
19119 EMSG2(_(e_intern2), "func_ref()");
19120 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019121 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 }
19123}
19124
19125/*
19126 * Call a user function.
19127 */
19128 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019129call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130 ufunc_T *fp; /* pointer to function */
19131 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019132 typval_T *argvars; /* arguments */
19133 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 linenr_T firstline; /* first line of range */
19135 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019136 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137{
Bram Moolenaar33570922005-01-25 22:26:29 +000019138 char_u *save_sourcing_name;
19139 linenr_T save_sourcing_lnum;
19140 scid_T save_current_SID;
19141 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019142 int save_did_emsg;
19143 static int depth = 0;
19144 dictitem_T *v;
19145 int fixvar_idx = 0; /* index in fixvar[] */
19146 int i;
19147 int ai;
19148 char_u numbuf[NUMBUFLEN];
19149 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019150#ifdef FEAT_PROFILE
19151 proftime_T wait_start;
19152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153
19154 /* If depth of calling is getting too high, don't execute the function */
19155 if (depth >= p_mfd)
19156 {
19157 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019158 rettv->v_type = VAR_NUMBER;
19159 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 return;
19161 }
19162 ++depth;
19163
19164 line_breakcheck(); /* check for CTRL-C hit */
19165
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019166 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019167 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019169 fc.rettv = rettv;
19170 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171 fc.linenr = 0;
19172 fc.returned = FALSE;
19173 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019175 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176 fc.dbg_tick = debug_tick;
19177
Bram Moolenaar33570922005-01-25 22:26:29 +000019178 /*
19179 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19180 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19181 * each argument variable and saves a lot of time.
19182 */
19183 /*
19184 * Init l: variables.
19185 */
19186 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019187 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019188 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019189 /* Set l:self to "selfdict". */
19190 v = &fc.fixvar[fixvar_idx++].var;
19191 STRCPY(v->di_key, "self");
19192 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19193 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19194 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019195 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019196 v->di_tv.vval.v_dict = selfdict;
19197 ++selfdict->dv_refcount;
19198 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019199
Bram Moolenaar33570922005-01-25 22:26:29 +000019200 /*
19201 * Init a: variables.
19202 * Set a:0 to "argcount".
19203 * Set a:000 to a list with room for the "..." arguments.
19204 */
19205 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19206 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019207 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019208 v = &fc.fixvar[fixvar_idx++].var;
19209 STRCPY(v->di_key, "000");
19210 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19211 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19212 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019213 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019214 v->di_tv.vval.v_list = &fc.l_varlist;
19215 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19216 fc.l_varlist.lv_refcount = 99999;
19217
19218 /*
19219 * Set a:firstline to "firstline" and a:lastline to "lastline".
19220 * Set a:name to named arguments.
19221 * Set a:N to the "..." arguments.
19222 */
19223 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19224 (varnumber_T)firstline);
19225 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19226 (varnumber_T)lastline);
19227 for (i = 0; i < argcount; ++i)
19228 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019229 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019230 if (ai < 0)
19231 /* named argument a:name */
19232 name = FUNCARG(fp, i);
19233 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019234 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019235 /* "..." argument a:1, a:2, etc. */
19236 sprintf((char *)numbuf, "%d", ai + 1);
19237 name = numbuf;
19238 }
19239 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19240 {
19241 v = &fc.fixvar[fixvar_idx++].var;
19242 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19243 }
19244 else
19245 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019246 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19247 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019248 if (v == NULL)
19249 break;
19250 v->di_flags = DI_FLAGS_RO;
19251 }
19252 STRCPY(v->di_key, name);
19253 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19254
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019255 /* Note: the values are copied directly to avoid alloc/free.
19256 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019257 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019258 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019259
19260 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19261 {
19262 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19263 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019264 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019265 }
19266 }
19267
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268 /* Don't redraw while executing the function. */
19269 ++RedrawingDisabled;
19270 save_sourcing_name = sourcing_name;
19271 save_sourcing_lnum = sourcing_lnum;
19272 sourcing_lnum = 1;
19273 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019274 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275 if (sourcing_name != NULL)
19276 {
19277 if (save_sourcing_name != NULL
19278 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19279 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19280 else
19281 STRCPY(sourcing_name, "function ");
19282 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19283
19284 if (p_verbose >= 12)
19285 {
19286 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019287 verbose_enter_scroll();
19288
Bram Moolenaar555b2802005-05-19 21:08:39 +000019289 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290 if (p_verbose >= 14)
19291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019293 char_u numbuf[NUMBUFLEN];
19294 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295
19296 msg_puts((char_u *)"(");
19297 for (i = 0; i < argcount; ++i)
19298 {
19299 if (i > 0)
19300 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019301 if (argvars[i].v_type == VAR_NUMBER)
19302 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303 else
19304 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019305 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019306 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019308 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309 }
19310 }
19311 msg_puts((char_u *)")");
19312 }
19313 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019314
19315 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316 --no_wait_return;
19317 }
19318 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019319#ifdef FEAT_PROFILE
19320 if (do_profiling)
19321 {
19322 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19323 func_do_profile(fp);
19324 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019325 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019326 {
19327 ++fp->uf_tm_count;
19328 profile_start(&fp->uf_tm_start);
19329 profile_zero(&fp->uf_tm_children);
19330 }
19331 script_prof_save(&wait_start);
19332 }
19333#endif
19334
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019336 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337 save_did_emsg = did_emsg;
19338 did_emsg = FALSE;
19339
19340 /* call do_cmdline() to execute the lines */
19341 do_cmdline(NULL, get_func_line, (void *)&fc,
19342 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19343
19344 --RedrawingDisabled;
19345
19346 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019347 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019349 clear_tv(rettv);
19350 rettv->v_type = VAR_NUMBER;
19351 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019352 }
19353
Bram Moolenaar05159a02005-02-26 23:04:13 +000019354#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019355 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019356 {
19357 profile_end(&fp->uf_tm_start);
19358 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19359 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019360 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019361 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019362 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019363 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19364 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019365 }
19366 }
19367#endif
19368
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369 /* when being verbose, mention the return value */
19370 if (p_verbose >= 12)
19371 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019373 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019376 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019377 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019378 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19379 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019380 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019382 char_u buf[MSG_BUF_LEN];
19383 char_u numbuf[NUMBUFLEN];
19384 char_u *tofree;
19385
Bram Moolenaar555b2802005-05-19 21:08:39 +000019386 /* The value may be very long. Skip the middle part, so that we
19387 * have some idea how it starts and ends. smsg() would always
19388 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019389 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019390 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019391 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019392 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393 }
19394 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019395
19396 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397 --no_wait_return;
19398 }
19399
19400 vim_free(sourcing_name);
19401 sourcing_name = save_sourcing_name;
19402 sourcing_lnum = save_sourcing_lnum;
19403 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019404#ifdef FEAT_PROFILE
19405 if (do_profiling)
19406 script_prof_restore(&wait_start);
19407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408
19409 if (p_verbose >= 12 && sourcing_name != NULL)
19410 {
19411 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019412 verbose_enter_scroll();
19413
Bram Moolenaar555b2802005-05-19 21:08:39 +000019414 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019416
19417 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019418 --no_wait_return;
19419 }
19420
19421 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019422 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423
Bram Moolenaar33570922005-01-25 22:26:29 +000019424 /* The a: variables typevals were not alloced, only free the allocated
19425 * variables. */
19426 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19427
19428 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 --depth;
19430}
19431
19432/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019433 * Add a number variable "name" to dict "dp" with value "nr".
19434 */
19435 static void
19436add_nr_var(dp, v, name, nr)
19437 dict_T *dp;
19438 dictitem_T *v;
19439 char *name;
19440 varnumber_T nr;
19441{
19442 STRCPY(v->di_key, name);
19443 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19444 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19445 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019446 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019447 v->di_tv.vval.v_number = nr;
19448}
19449
19450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 * ":return [expr]"
19452 */
19453 void
19454ex_return(eap)
19455 exarg_T *eap;
19456{
19457 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019458 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 int returning = FALSE;
19460
19461 if (current_funccal == NULL)
19462 {
19463 EMSG(_("E133: :return not inside a function"));
19464 return;
19465 }
19466
19467 if (eap->skip)
19468 ++emsg_skip;
19469
19470 eap->nextcmd = NULL;
19471 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019472 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473 {
19474 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019475 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019477 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 }
19479 /* It's safer to return also on error. */
19480 else if (!eap->skip)
19481 {
19482 /*
19483 * Return unless the expression evaluation has been cancelled due to an
19484 * aborting error, an interrupt, or an exception.
19485 */
19486 if (!aborting())
19487 returning = do_return(eap, FALSE, TRUE, NULL);
19488 }
19489
19490 /* When skipping or the return gets pending, advance to the next command
19491 * in this line (!returning). Otherwise, ignore the rest of the line.
19492 * Following lines will be ignored by get_func_line(). */
19493 if (returning)
19494 eap->nextcmd = NULL;
19495 else if (eap->nextcmd == NULL) /* no argument */
19496 eap->nextcmd = check_nextcmd(arg);
19497
19498 if (eap->skip)
19499 --emsg_skip;
19500}
19501
19502/*
19503 * Return from a function. Possibly makes the return pending. Also called
19504 * for a pending return at the ":endtry" or after returning from an extra
19505 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019506 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019507 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508 * FALSE when the return gets pending.
19509 */
19510 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019511do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512 exarg_T *eap;
19513 int reanimate;
19514 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019515 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516{
19517 int idx;
19518 struct condstack *cstack = eap->cstack;
19519
19520 if (reanimate)
19521 /* Undo the return. */
19522 current_funccal->returned = FALSE;
19523
19524 /*
19525 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19526 * not in its finally clause (which then is to be executed next) is found.
19527 * In this case, make the ":return" pending for execution at the ":endtry".
19528 * Otherwise, return normally.
19529 */
19530 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19531 if (idx >= 0)
19532 {
19533 cstack->cs_pending[idx] = CSTP_RETURN;
19534
19535 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019536 /* A pending return again gets pending. "rettv" points to an
19537 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019539 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540 else
19541 {
19542 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019543 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019545 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019547 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548 {
19549 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019550 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019551 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 else
19553 EMSG(_(e_outofmem));
19554 }
19555 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019556 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557
19558 if (reanimate)
19559 {
19560 /* The pending return value could be overwritten by a ":return"
19561 * without argument in a finally clause; reset the default
19562 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019563 current_funccal->rettv->v_type = VAR_NUMBER;
19564 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019565 }
19566 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019567 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568 }
19569 else
19570 {
19571 current_funccal->returned = TRUE;
19572
19573 /* If the return is carried out now, store the return value. For
19574 * a return immediately after reanimation, the value is already
19575 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019576 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019578 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019579 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019580 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019581 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582 }
19583 }
19584
19585 return idx < 0;
19586}
19587
19588/*
19589 * Free the variable with a pending return value.
19590 */
19591 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019592discard_pending_return(rettv)
19593 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594{
Bram Moolenaar33570922005-01-25 22:26:29 +000019595 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596}
19597
19598/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019599 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600 * is an allocated string. Used by report_pending() for verbose messages.
19601 */
19602 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019603get_return_cmd(rettv)
19604 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019606 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019607 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019608 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019610 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019611 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019612 if (s == NULL)
19613 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019614
19615 STRCPY(IObuff, ":return ");
19616 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19617 if (STRLEN(s) + 8 >= IOSIZE)
19618 STRCPY(IObuff + IOSIZE - 4, "...");
19619 vim_free(tofree);
19620 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621}
19622
19623/*
19624 * Get next function line.
19625 * Called by do_cmdline() to get the next line.
19626 * Returns allocated string, or NULL for end of function.
19627 */
19628/* ARGSUSED */
19629 char_u *
19630get_func_line(c, cookie, indent)
19631 int c; /* not used */
19632 void *cookie;
19633 int indent; /* not used */
19634{
Bram Moolenaar33570922005-01-25 22:26:29 +000019635 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019636 ufunc_T *fp = fcp->func;
19637 char_u *retval;
19638 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639
19640 /* If breakpoints have been added/deleted need to check for it. */
19641 if (fcp->dbg_tick != debug_tick)
19642 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019643 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 sourcing_lnum);
19645 fcp->dbg_tick = debug_tick;
19646 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019647#ifdef FEAT_PROFILE
19648 if (do_profiling)
19649 func_line_end(cookie);
19650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651
Bram Moolenaar05159a02005-02-26 23:04:13 +000019652 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019653 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
19654 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655 retval = NULL;
19656 else
19657 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019658 /* Skip NULL lines (continuation lines). */
19659 while (fcp->linenr < gap->ga_len
19660 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
19661 ++fcp->linenr;
19662 if (fcp->linenr >= gap->ga_len)
19663 retval = NULL;
19664 else
19665 {
19666 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19667 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019668#ifdef FEAT_PROFILE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019669 if (do_profiling)
19670 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019671#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673 }
19674
19675 /* Did we encounter a breakpoint? */
19676 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19677 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019678 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019680 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681 sourcing_lnum);
19682 fcp->dbg_tick = debug_tick;
19683 }
19684
19685 return retval;
19686}
19687
Bram Moolenaar05159a02005-02-26 23:04:13 +000019688#if defined(FEAT_PROFILE) || defined(PROTO)
19689/*
19690 * Called when starting to read a function line.
19691 * "sourcing_lnum" must be correct!
19692 * When skipping lines it may not actually be executed, but we won't find out
19693 * until later and we need to store the time now.
19694 */
19695 void
19696func_line_start(cookie)
19697 void *cookie;
19698{
19699 funccall_T *fcp = (funccall_T *)cookie;
19700 ufunc_T *fp = fcp->func;
19701
19702 if (fp->uf_profiling && sourcing_lnum >= 1
19703 && sourcing_lnum <= fp->uf_lines.ga_len)
19704 {
19705 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019706 /* Skip continuation lines. */
19707 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
19708 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019709 fp->uf_tml_execed = FALSE;
19710 profile_start(&fp->uf_tml_start);
19711 profile_zero(&fp->uf_tml_children);
19712 profile_get_wait(&fp->uf_tml_wait);
19713 }
19714}
19715
19716/*
19717 * Called when actually executing a function line.
19718 */
19719 void
19720func_line_exec(cookie)
19721 void *cookie;
19722{
19723 funccall_T *fcp = (funccall_T *)cookie;
19724 ufunc_T *fp = fcp->func;
19725
19726 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19727 fp->uf_tml_execed = TRUE;
19728}
19729
19730/*
19731 * Called when done with a function line.
19732 */
19733 void
19734func_line_end(cookie)
19735 void *cookie;
19736{
19737 funccall_T *fcp = (funccall_T *)cookie;
19738 ufunc_T *fp = fcp->func;
19739
19740 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19741 {
19742 if (fp->uf_tml_execed)
19743 {
19744 ++fp->uf_tml_count[fp->uf_tml_idx];
19745 profile_end(&fp->uf_tml_start);
19746 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019747 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019748 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
19749 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019750 }
19751 fp->uf_tml_idx = -1;
19752 }
19753}
19754#endif
19755
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756/*
19757 * Return TRUE if the currently active function should be ended, because a
19758 * return was encountered or an error occured. Used inside a ":while".
19759 */
19760 int
19761func_has_ended(cookie)
19762 void *cookie;
19763{
Bram Moolenaar33570922005-01-25 22:26:29 +000019764 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765
19766 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19767 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019768 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 || fcp->returned);
19770}
19771
19772/*
19773 * return TRUE if cookie indicates a function which "abort"s on errors.
19774 */
19775 int
19776func_has_abort(cookie)
19777 void *cookie;
19778{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019779 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780}
19781
19782#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19783typedef enum
19784{
19785 VAR_FLAVOUR_DEFAULT,
19786 VAR_FLAVOUR_SESSION,
19787 VAR_FLAVOUR_VIMINFO
19788} var_flavour_T;
19789
19790static var_flavour_T var_flavour __ARGS((char_u *varname));
19791
19792 static var_flavour_T
19793var_flavour(varname)
19794 char_u *varname;
19795{
19796 char_u *p = varname;
19797
19798 if (ASCII_ISUPPER(*p))
19799 {
19800 while (*(++p))
19801 if (ASCII_ISLOWER(*p))
19802 return VAR_FLAVOUR_SESSION;
19803 return VAR_FLAVOUR_VIMINFO;
19804 }
19805 else
19806 return VAR_FLAVOUR_DEFAULT;
19807}
19808#endif
19809
19810#if defined(FEAT_VIMINFO) || defined(PROTO)
19811/*
19812 * Restore global vars that start with a capital from the viminfo file
19813 */
19814 int
19815read_viminfo_varlist(virp, writing)
19816 vir_T *virp;
19817 int writing;
19818{
19819 char_u *tab;
19820 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019821 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822
19823 if (!writing && (find_viminfo_parameter('!') != NULL))
19824 {
19825 tab = vim_strchr(virp->vir_line + 1, '\t');
19826 if (tab != NULL)
19827 {
19828 *tab++ = '\0'; /* isolate the variable name */
19829 if (*tab == 'S') /* string var */
19830 is_string = TRUE;
19831
19832 tab = vim_strchr(tab, '\t');
19833 if (tab != NULL)
19834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 if (is_string)
19836 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019837 tv.v_type = VAR_STRING;
19838 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840 }
19841 else
19842 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019843 tv.v_type = VAR_NUMBER;
19844 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019846 set_var(virp->vir_line + 1, &tv, FALSE);
19847 if (is_string)
19848 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 }
19850 }
19851 }
19852
19853 return viminfo_readline(virp);
19854}
19855
19856/*
19857 * Write global vars that start with a capital to the viminfo file
19858 */
19859 void
19860write_viminfo_varlist(fp)
19861 FILE *fp;
19862{
Bram Moolenaar33570922005-01-25 22:26:29 +000019863 hashitem_T *hi;
19864 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019865 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019866 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019867 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019868 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019869 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870
19871 if (find_viminfo_parameter('!') == NULL)
19872 return;
19873
19874 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019875
Bram Moolenaar33570922005-01-25 22:26:29 +000019876 todo = globvarht.ht_used;
19877 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019879 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019881 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019882 this_var = HI2DI(hi);
19883 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019884 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019885 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019886 {
19887 case VAR_STRING: s = "STR"; break;
19888 case VAR_NUMBER: s = "NUM"; break;
19889 default: continue;
19890 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019891 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019892 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019893 if (p != NULL)
19894 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019895 vim_free(tofree);
19896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897 }
19898 }
19899}
19900#endif
19901
19902#if defined(FEAT_SESSION) || defined(PROTO)
19903 int
19904store_session_globals(fd)
19905 FILE *fd;
19906{
Bram Moolenaar33570922005-01-25 22:26:29 +000019907 hashitem_T *hi;
19908 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019909 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 char_u *p, *t;
19911
Bram Moolenaar33570922005-01-25 22:26:29 +000019912 todo = globvarht.ht_used;
19913 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019915 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019917 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019918 this_var = HI2DI(hi);
19919 if ((this_var->di_tv.v_type == VAR_NUMBER
19920 || this_var->di_tv.v_type == VAR_STRING)
19921 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019922 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019923 /* Escape special characters with a backslash. Turn a LF and
19924 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019925 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019926 (char_u *)"\\\"\n\r");
19927 if (p == NULL) /* out of memory */
19928 break;
19929 for (t = p; *t != NUL; ++t)
19930 if (*t == '\n')
19931 *t = 'n';
19932 else if (*t == '\r')
19933 *t = 'r';
19934 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019935 this_var->di_key,
19936 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19937 : ' ',
19938 p,
19939 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19940 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019941 || put_eol(fd) == FAIL)
19942 {
19943 vim_free(p);
19944 return FAIL;
19945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 vim_free(p);
19947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 }
19949 }
19950 return OK;
19951}
19952#endif
19953
Bram Moolenaar661b1822005-07-28 22:36:45 +000019954/*
19955 * Display script name where an item was last set.
19956 * Should only be invoked when 'verbose' is non-zero.
19957 */
19958 void
19959last_set_msg(scriptID)
19960 scid_T scriptID;
19961{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019962 char_u *p;
19963
Bram Moolenaar661b1822005-07-28 22:36:45 +000019964 if (scriptID != 0)
19965 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019966 p = home_replace_save(NULL, get_scriptname(scriptID));
19967 if (p != NULL)
19968 {
19969 verbose_enter();
19970 MSG_PUTS(_("\n\tLast set from "));
19971 MSG_PUTS(p);
19972 vim_free(p);
19973 verbose_leave();
19974 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019975 }
19976}
19977
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978#endif /* FEAT_EVAL */
19979
19980#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19981
19982
19983#ifdef WIN3264
19984/*
19985 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19986 */
19987static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19988static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19989static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19990
19991/*
19992 * Get the short pathname of a file.
19993 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19994 */
19995 static int
19996get_short_pathname(fnamep, bufp, fnamelen)
19997 char_u **fnamep;
19998 char_u **bufp;
19999 int *fnamelen;
20000{
20001 int l,len;
20002 char_u *newbuf;
20003
20004 len = *fnamelen;
20005
20006 l = GetShortPathName(*fnamep, *fnamep, len);
20007 if (l > len - 1)
20008 {
20009 /* If that doesn't work (not enough space), then save the string
20010 * and try again with a new buffer big enough
20011 */
20012 newbuf = vim_strnsave(*fnamep, l);
20013 if (newbuf == NULL)
20014 return 0;
20015
20016 vim_free(*bufp);
20017 *fnamep = *bufp = newbuf;
20018
20019 l = GetShortPathName(*fnamep,*fnamep,l+1);
20020
20021 /* Really should always succeed, as the buffer is big enough */
20022 }
20023
20024 *fnamelen = l;
20025 return 1;
20026}
20027
20028/*
20029 * Create a short path name. Returns the length of the buffer it needs.
20030 * Doesn't copy over the end of the buffer passed in.
20031 */
20032 static int
20033shortpath_for_invalid_fname(fname, bufp, fnamelen)
20034 char_u **fname;
20035 char_u **bufp;
20036 int *fnamelen;
20037{
20038 char_u *s, *p, *pbuf2, *pbuf3;
20039 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020040 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041
20042 /* Make a copy */
20043 len2 = *fnamelen;
20044 pbuf2 = vim_strnsave(*fname, len2);
20045 pbuf3 = NULL;
20046
20047 s = pbuf2 + len2 - 1; /* Find the end */
20048 slen = 1;
20049 plen = len2;
20050
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020051 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052 {
20053 --s;
20054 ++slen;
20055 --plen;
20056 }
20057
20058 do
20059 {
20060 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020061 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 {
20063 --s;
20064 ++slen;
20065 --plen;
20066 }
20067 if (s <= pbuf2)
20068 break;
20069
20070 /* Remeber the character that is about to be blatted */
20071 ch = *s;
20072 *s = 0; /* get_short_pathname requires a null-terminated string */
20073
20074 /* Try it in situ */
20075 p = pbuf2;
20076 if (!get_short_pathname(&p, &pbuf3, &plen))
20077 {
20078 vim_free(pbuf2);
20079 return -1;
20080 }
20081 *s = ch; /* Preserve the string */
20082 } while (plen == 0);
20083
20084 if (plen > 0)
20085 {
20086 /* Remeber the length of the new string. */
20087 *fnamelen = len = plen + slen;
20088 vim_free(*bufp);
20089 if (len > len2)
20090 {
20091 /* If there's not enough space in the currently allocated string,
20092 * then copy it to a buffer big enough.
20093 */
20094 *fname= *bufp = vim_strnsave(p, len);
20095 if (*fname == NULL)
20096 return -1;
20097 }
20098 else
20099 {
20100 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20101 *fname = *bufp = pbuf2;
20102 if (p != pbuf2)
20103 strncpy(*fname, p, plen);
20104 pbuf2 = NULL;
20105 }
20106 /* Concat the next bit */
20107 strncpy(*fname + plen, s, slen);
20108 (*fname)[len] = '\0';
20109 }
20110 vim_free(pbuf3);
20111 vim_free(pbuf2);
20112 return 0;
20113}
20114
20115/*
20116 * Get a pathname for a partial path.
20117 */
20118 static int
20119shortpath_for_partial(fnamep, bufp, fnamelen)
20120 char_u **fnamep;
20121 char_u **bufp;
20122 int *fnamelen;
20123{
20124 int sepcount, len, tflen;
20125 char_u *p;
20126 char_u *pbuf, *tfname;
20127 int hasTilde;
20128
20129 /* Count up the path seperators from the RHS.. so we know which part
20130 * of the path to return.
20131 */
20132 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020133 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 if (vim_ispathsep(*p))
20135 ++sepcount;
20136
20137 /* Need full path first (use expand_env() to remove a "~/") */
20138 hasTilde = (**fnamep == '~');
20139 if (hasTilde)
20140 pbuf = tfname = expand_env_save(*fnamep);
20141 else
20142 pbuf = tfname = FullName_save(*fnamep, FALSE);
20143
20144 len = tflen = STRLEN(tfname);
20145
20146 if (!get_short_pathname(&tfname, &pbuf, &len))
20147 return -1;
20148
20149 if (len == 0)
20150 {
20151 /* Don't have a valid filename, so shorten the rest of the
20152 * path if we can. This CAN give us invalid 8.3 filenames, but
20153 * there's not a lot of point in guessing what it might be.
20154 */
20155 len = tflen;
20156 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20157 return -1;
20158 }
20159
20160 /* Count the paths backward to find the beginning of the desired string. */
20161 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020162 {
20163#ifdef FEAT_MBYTE
20164 if (has_mbyte)
20165 p -= mb_head_off(tfname, p);
20166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 if (vim_ispathsep(*p))
20168 {
20169 if (sepcount == 0 || (hasTilde && sepcount == 1))
20170 break;
20171 else
20172 sepcount --;
20173 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175 if (hasTilde)
20176 {
20177 --p;
20178 if (p >= tfname)
20179 *p = '~';
20180 else
20181 return -1;
20182 }
20183 else
20184 ++p;
20185
20186 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20187 vim_free(*bufp);
20188 *fnamelen = (int)STRLEN(p);
20189 *bufp = pbuf;
20190 *fnamep = p;
20191
20192 return 0;
20193}
20194#endif /* WIN3264 */
20195
20196/*
20197 * Adjust a filename, according to a string of modifiers.
20198 * *fnamep must be NUL terminated when called. When returning, the length is
20199 * determined by *fnamelen.
20200 * Returns valid flags.
20201 * When there is an error, *fnamep is set to NULL.
20202 */
20203 int
20204modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20205 char_u *src; /* string with modifiers */
20206 int *usedlen; /* characters after src that are used */
20207 char_u **fnamep; /* file name so far */
20208 char_u **bufp; /* buffer for allocated file name or NULL */
20209 int *fnamelen; /* length of fnamep */
20210{
20211 int valid = 0;
20212 char_u *tail;
20213 char_u *s, *p, *pbuf;
20214 char_u dirname[MAXPATHL];
20215 int c;
20216 int has_fullname = 0;
20217#ifdef WIN3264
20218 int has_shortname = 0;
20219#endif
20220
20221repeat:
20222 /* ":p" - full path/file_name */
20223 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20224 {
20225 has_fullname = 1;
20226
20227 valid |= VALID_PATH;
20228 *usedlen += 2;
20229
20230 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20231 if ((*fnamep)[0] == '~'
20232#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20233 && ((*fnamep)[1] == '/'
20234# ifdef BACKSLASH_IN_FILENAME
20235 || (*fnamep)[1] == '\\'
20236# endif
20237 || (*fnamep)[1] == NUL)
20238
20239#endif
20240 )
20241 {
20242 *fnamep = expand_env_save(*fnamep);
20243 vim_free(*bufp); /* free any allocated file name */
20244 *bufp = *fnamep;
20245 if (*fnamep == NULL)
20246 return -1;
20247 }
20248
20249 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020250 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 {
20252 if (vim_ispathsep(*p)
20253 && p[1] == '.'
20254 && (p[2] == NUL
20255 || vim_ispathsep(p[2])
20256 || (p[2] == '.'
20257 && (p[3] == NUL || vim_ispathsep(p[3])))))
20258 break;
20259 }
20260
20261 /* FullName_save() is slow, don't use it when not needed. */
20262 if (*p != NUL || !vim_isAbsName(*fnamep))
20263 {
20264 *fnamep = FullName_save(*fnamep, *p != NUL);
20265 vim_free(*bufp); /* free any allocated file name */
20266 *bufp = *fnamep;
20267 if (*fnamep == NULL)
20268 return -1;
20269 }
20270
20271 /* Append a path separator to a directory. */
20272 if (mch_isdir(*fnamep))
20273 {
20274 /* Make room for one or two extra characters. */
20275 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20276 vim_free(*bufp); /* free any allocated file name */
20277 *bufp = *fnamep;
20278 if (*fnamep == NULL)
20279 return -1;
20280 add_pathsep(*fnamep);
20281 }
20282 }
20283
20284 /* ":." - path relative to the current directory */
20285 /* ":~" - path relative to the home directory */
20286 /* ":8" - shortname path - postponed till after */
20287 while (src[*usedlen] == ':'
20288 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20289 {
20290 *usedlen += 2;
20291 if (c == '8')
20292 {
20293#ifdef WIN3264
20294 has_shortname = 1; /* Postpone this. */
20295#endif
20296 continue;
20297 }
20298 pbuf = NULL;
20299 /* Need full path first (use expand_env() to remove a "~/") */
20300 if (!has_fullname)
20301 {
20302 if (c == '.' && **fnamep == '~')
20303 p = pbuf = expand_env_save(*fnamep);
20304 else
20305 p = pbuf = FullName_save(*fnamep, FALSE);
20306 }
20307 else
20308 p = *fnamep;
20309
20310 has_fullname = 0;
20311
20312 if (p != NULL)
20313 {
20314 if (c == '.')
20315 {
20316 mch_dirname(dirname, MAXPATHL);
20317 s = shorten_fname(p, dirname);
20318 if (s != NULL)
20319 {
20320 *fnamep = s;
20321 if (pbuf != NULL)
20322 {
20323 vim_free(*bufp); /* free any allocated file name */
20324 *bufp = pbuf;
20325 pbuf = NULL;
20326 }
20327 }
20328 }
20329 else
20330 {
20331 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20332 /* Only replace it when it starts with '~' */
20333 if (*dirname == '~')
20334 {
20335 s = vim_strsave(dirname);
20336 if (s != NULL)
20337 {
20338 *fnamep = s;
20339 vim_free(*bufp);
20340 *bufp = s;
20341 }
20342 }
20343 }
20344 vim_free(pbuf);
20345 }
20346 }
20347
20348 tail = gettail(*fnamep);
20349 *fnamelen = (int)STRLEN(*fnamep);
20350
20351 /* ":h" - head, remove "/file_name", can be repeated */
20352 /* Don't remove the first "/" or "c:\" */
20353 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20354 {
20355 valid |= VALID_HEAD;
20356 *usedlen += 2;
20357 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020358 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 --tail;
20360 *fnamelen = (int)(tail - *fnamep);
20361#ifdef VMS
20362 if (*fnamelen > 0)
20363 *fnamelen += 1; /* the path separator is part of the path */
20364#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020365 while (tail > s && !after_pathsep(s, tail))
20366 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 }
20368
20369 /* ":8" - shortname */
20370 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20371 {
20372 *usedlen += 2;
20373#ifdef WIN3264
20374 has_shortname = 1;
20375#endif
20376 }
20377
20378#ifdef WIN3264
20379 /* Check shortname after we have done 'heads' and before we do 'tails'
20380 */
20381 if (has_shortname)
20382 {
20383 pbuf = NULL;
20384 /* Copy the string if it is shortened by :h */
20385 if (*fnamelen < (int)STRLEN(*fnamep))
20386 {
20387 p = vim_strnsave(*fnamep, *fnamelen);
20388 if (p == 0)
20389 return -1;
20390 vim_free(*bufp);
20391 *bufp = *fnamep = p;
20392 }
20393
20394 /* Split into two implementations - makes it easier. First is where
20395 * there isn't a full name already, second is where there is.
20396 */
20397 if (!has_fullname && !vim_isAbsName(*fnamep))
20398 {
20399 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20400 return -1;
20401 }
20402 else
20403 {
20404 int l;
20405
20406 /* Simple case, already have the full-name
20407 * Nearly always shorter, so try first time. */
20408 l = *fnamelen;
20409 if (!get_short_pathname(fnamep, bufp, &l))
20410 return -1;
20411
20412 if (l == 0)
20413 {
20414 /* Couldn't find the filename.. search the paths.
20415 */
20416 l = *fnamelen;
20417 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20418 return -1;
20419 }
20420 *fnamelen = l;
20421 }
20422 }
20423#endif /* WIN3264 */
20424
20425 /* ":t" - tail, just the basename */
20426 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20427 {
20428 *usedlen += 2;
20429 *fnamelen -= (int)(tail - *fnamep);
20430 *fnamep = tail;
20431 }
20432
20433 /* ":e" - extension, can be repeated */
20434 /* ":r" - root, without extension, can be repeated */
20435 while (src[*usedlen] == ':'
20436 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20437 {
20438 /* find a '.' in the tail:
20439 * - for second :e: before the current fname
20440 * - otherwise: The last '.'
20441 */
20442 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20443 s = *fnamep - 2;
20444 else
20445 s = *fnamep + *fnamelen - 1;
20446 for ( ; s > tail; --s)
20447 if (s[0] == '.')
20448 break;
20449 if (src[*usedlen + 1] == 'e') /* :e */
20450 {
20451 if (s > tail)
20452 {
20453 *fnamelen += (int)(*fnamep - (s + 1));
20454 *fnamep = s + 1;
20455#ifdef VMS
20456 /* cut version from the extension */
20457 s = *fnamep + *fnamelen - 1;
20458 for ( ; s > *fnamep; --s)
20459 if (s[0] == ';')
20460 break;
20461 if (s > *fnamep)
20462 *fnamelen = s - *fnamep;
20463#endif
20464 }
20465 else if (*fnamep <= tail)
20466 *fnamelen = 0;
20467 }
20468 else /* :r */
20469 {
20470 if (s > tail) /* remove one extension */
20471 *fnamelen = (int)(s - *fnamep);
20472 }
20473 *usedlen += 2;
20474 }
20475
20476 /* ":s?pat?foo?" - substitute */
20477 /* ":gs?pat?foo?" - global substitute */
20478 if (src[*usedlen] == ':'
20479 && (src[*usedlen + 1] == 's'
20480 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20481 {
20482 char_u *str;
20483 char_u *pat;
20484 char_u *sub;
20485 int sep;
20486 char_u *flags;
20487 int didit = FALSE;
20488
20489 flags = (char_u *)"";
20490 s = src + *usedlen + 2;
20491 if (src[*usedlen + 1] == 'g')
20492 {
20493 flags = (char_u *)"g";
20494 ++s;
20495 }
20496
20497 sep = *s++;
20498 if (sep)
20499 {
20500 /* find end of pattern */
20501 p = vim_strchr(s, sep);
20502 if (p != NULL)
20503 {
20504 pat = vim_strnsave(s, (int)(p - s));
20505 if (pat != NULL)
20506 {
20507 s = p + 1;
20508 /* find end of substitution */
20509 p = vim_strchr(s, sep);
20510 if (p != NULL)
20511 {
20512 sub = vim_strnsave(s, (int)(p - s));
20513 str = vim_strnsave(*fnamep, *fnamelen);
20514 if (sub != NULL && str != NULL)
20515 {
20516 *usedlen = (int)(p + 1 - src);
20517 s = do_string_sub(str, pat, sub, flags);
20518 if (s != NULL)
20519 {
20520 *fnamep = s;
20521 *fnamelen = (int)STRLEN(s);
20522 vim_free(*bufp);
20523 *bufp = s;
20524 didit = TRUE;
20525 }
20526 }
20527 vim_free(sub);
20528 vim_free(str);
20529 }
20530 vim_free(pat);
20531 }
20532 }
20533 /* after using ":s", repeat all the modifiers */
20534 if (didit)
20535 goto repeat;
20536 }
20537 }
20538
20539 return valid;
20540}
20541
20542/*
20543 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20544 * "flags" can be "g" to do a global substitute.
20545 * Returns an allocated string, NULL for error.
20546 */
20547 char_u *
20548do_string_sub(str, pat, sub, flags)
20549 char_u *str;
20550 char_u *pat;
20551 char_u *sub;
20552 char_u *flags;
20553{
20554 int sublen;
20555 regmatch_T regmatch;
20556 int i;
20557 int do_all;
20558 char_u *tail;
20559 garray_T ga;
20560 char_u *ret;
20561 char_u *save_cpo;
20562
20563 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20564 save_cpo = p_cpo;
20565 p_cpo = (char_u *)"";
20566
20567 ga_init2(&ga, 1, 200);
20568
20569 do_all = (flags[0] == 'g');
20570
20571 regmatch.rm_ic = p_ic;
20572 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20573 if (regmatch.regprog != NULL)
20574 {
20575 tail = str;
20576 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20577 {
20578 /*
20579 * Get some space for a temporary buffer to do the substitution
20580 * into. It will contain:
20581 * - The text up to where the match is.
20582 * - The substituted text.
20583 * - The text after the match.
20584 */
20585 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20586 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20587 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20588 {
20589 ga_clear(&ga);
20590 break;
20591 }
20592
20593 /* copy the text up to where the match is */
20594 i = (int)(regmatch.startp[0] - tail);
20595 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20596 /* add the substituted text */
20597 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20598 + ga.ga_len + i, TRUE, TRUE, FALSE);
20599 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600 /* avoid getting stuck on a match with an empty string */
20601 if (tail == regmatch.endp[0])
20602 {
20603 if (*tail == NUL)
20604 break;
20605 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20606 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607 }
20608 else
20609 {
20610 tail = regmatch.endp[0];
20611 if (*tail == NUL)
20612 break;
20613 }
20614 if (!do_all)
20615 break;
20616 }
20617
20618 if (ga.ga_data != NULL)
20619 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20620
20621 vim_free(regmatch.regprog);
20622 }
20623
20624 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20625 ga_clear(&ga);
20626 p_cpo = save_cpo;
20627
20628 return ret;
20629}
20630
20631#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */