blob: a622c923b475b02385a61c48eefa329e60ae096b [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;
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008145 char_u *kind = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008146 char_u *extra = NULL;
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008147 char_u *info = NULL;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008148 int icase = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008149
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008150 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8151 {
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008152 word = get_dict_string(argvars[0].vval.v_dict, (char_u *)"word", FALSE);
8153 kind = get_dict_string(argvars[0].vval.v_dict, (char_u *)"kind", FALSE);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008154 extra = get_dict_string(argvars[0].vval.v_dict,
8155 (char_u *)"menu", FALSE);
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008156 info = get_dict_string(argvars[0].vval.v_dict,
8157 (char_u *)"info", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008158 icase = get_dict_number(argvars[0].vval.v_dict, (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008159 }
8160 else
8161 word = get_tv_string_chk(&argvars[0]);
8162 if (word != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008163 rettv->vval.v_number = ins_compl_add(word, -1, icase,
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008164 NULL, kind, extra, info, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008165}
8166
8167/*
8168 * "complete_check()" function
8169 */
8170/*ARGSUSED*/
8171 static void
8172f_complete_check(argvars, rettv)
8173 typval_T *argvars;
8174 typval_T *rettv;
8175{
8176 int saved = RedrawingDisabled;
8177
8178 RedrawingDisabled = 0;
8179 ins_compl_check_keys(0);
8180 rettv->vval.v_number = compl_interrupted;
8181 RedrawingDisabled = saved;
8182}
8183#endif
8184
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185/*
8186 * "confirm(message, buttons[, default [, type]])" function
8187 */
8188/*ARGSUSED*/
8189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008190f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008191 typval_T *argvars;
8192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193{
8194#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8195 char_u *message;
8196 char_u *buttons = NULL;
8197 char_u buf[NUMBUFLEN];
8198 char_u buf2[NUMBUFLEN];
8199 int def = 1;
8200 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008201 char_u *typestr;
8202 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008204 message = get_tv_string_chk(&argvars[0]);
8205 if (message == NULL)
8206 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008207 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008209 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8210 if (buttons == NULL)
8211 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008212 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008214 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008215 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008217 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8218 if (typestr == NULL)
8219 error = TRUE;
8220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008222 switch (TOUPPER_ASC(*typestr))
8223 {
8224 case 'E': type = VIM_ERROR; break;
8225 case 'Q': type = VIM_QUESTION; break;
8226 case 'I': type = VIM_INFO; break;
8227 case 'W': type = VIM_WARNING; break;
8228 case 'G': type = VIM_GENERIC; break;
8229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 }
8231 }
8232 }
8233 }
8234
8235 if (buttons == NULL || *buttons == NUL)
8236 buttons = (char_u *)_("&Ok");
8237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008238 if (error)
8239 rettv->vval.v_number = 0;
8240 else
8241 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 def, NULL);
8243#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008244 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245#endif
8246}
8247
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008248/*
8249 * "copy()" function
8250 */
8251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008252f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008253 typval_T *argvars;
8254 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008255{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008256 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008257}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258
8259/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008260 * "count()" function
8261 */
8262 static void
8263f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008264 typval_T *argvars;
8265 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008266{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008267 long n = 0;
8268 int ic = FALSE;
8269
Bram Moolenaare9a41262005-01-15 22:18:47 +00008270 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008271 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008272 listitem_T *li;
8273 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008274 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008275
Bram Moolenaare9a41262005-01-15 22:18:47 +00008276 if ((l = argvars[0].vval.v_list) != NULL)
8277 {
8278 li = l->lv_first;
8279 if (argvars[2].v_type != VAR_UNKNOWN)
8280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008281 int error = FALSE;
8282
8283 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008284 if (argvars[3].v_type != VAR_UNKNOWN)
8285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008286 idx = get_tv_number_chk(&argvars[3], &error);
8287 if (!error)
8288 {
8289 li = list_find(l, idx);
8290 if (li == NULL)
8291 EMSGN(_(e_listidx), idx);
8292 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008294 if (error)
8295 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008296 }
8297
8298 for ( ; li != NULL; li = li->li_next)
8299 if (tv_equal(&li->li_tv, &argvars[1], ic))
8300 ++n;
8301 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008302 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008303 else if (argvars[0].v_type == VAR_DICT)
8304 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008305 int todo;
8306 dict_T *d;
8307 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008308
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008309 if ((d = argvars[0].vval.v_dict) != NULL)
8310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008311 int error = FALSE;
8312
Bram Moolenaare9a41262005-01-15 22:18:47 +00008313 if (argvars[2].v_type != VAR_UNKNOWN)
8314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008315 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008316 if (argvars[3].v_type != VAR_UNKNOWN)
8317 EMSG(_(e_invarg));
8318 }
8319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008320 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008321 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008322 {
8323 if (!HASHITEM_EMPTY(hi))
8324 {
8325 --todo;
8326 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8327 ++n;
8328 }
8329 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008330 }
8331 }
8332 else
8333 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008334 rettv->vval.v_number = n;
8335}
8336
8337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8339 *
8340 * Checks the existence of a cscope connection.
8341 */
8342/*ARGSUSED*/
8343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008344f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008345 typval_T *argvars;
8346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347{
8348#ifdef FEAT_CSCOPE
8349 int num = 0;
8350 char_u *dbpath = NULL;
8351 char_u *prepend = NULL;
8352 char_u buf[NUMBUFLEN];
8353
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008354 if (argvars[0].v_type != VAR_UNKNOWN
8355 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008357 num = (int)get_tv_number(&argvars[0]);
8358 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008359 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008360 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 }
8362
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008363 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008365 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366#endif
8367}
8368
8369/*
8370 * "cursor(lnum, col)" function
8371 *
8372 * Moves the cursor to the specified line and column
8373 */
8374/*ARGSUSED*/
8375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008376f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008377 typval_T *argvars;
8378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379{
8380 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008381#ifdef FEAT_VIRTUALEDIT
8382 long coladd = 0;
8383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384
Bram Moolenaara5525202006-03-02 22:52:09 +00008385 if (argvars[1].v_type == VAR_UNKNOWN)
8386 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008387 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008388
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008389 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008390 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008391 line = pos.lnum;
8392 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008393#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008394 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008395#endif
8396 }
8397 else
8398 {
8399 line = get_tv_lnum(argvars);
8400 col = get_tv_number_chk(&argvars[1], NULL);
8401#ifdef FEAT_VIRTUALEDIT
8402 if (argvars[2].v_type != VAR_UNKNOWN)
8403 coladd = get_tv_number_chk(&argvars[2], NULL);
8404#endif
8405 }
8406 if (line < 0 || col < 0
8407#ifdef FEAT_VIRTUALEDIT
8408 || coladd < 0
8409#endif
8410 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008411 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 if (line > 0)
8413 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 if (col > 0)
8415 curwin->w_cursor.col = col - 1;
8416#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008417 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418#endif
8419
8420 /* Make sure the cursor is in a valid position. */
8421 check_cursor();
8422#ifdef FEAT_MBYTE
8423 /* Correct cursor for multi-byte character. */
8424 if (has_mbyte)
8425 mb_adjust_cursor();
8426#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008427
8428 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429}
8430
8431/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008432 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 */
8434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008435f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008436 typval_T *argvars;
8437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008439 int noref = 0;
8440
8441 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008442 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008443 if (noref < 0 || noref > 1)
8444 EMSG(_(e_invarg));
8445 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008446 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447}
8448
8449/*
8450 * "delete()" function
8451 */
8452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008453f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008454 typval_T *argvars;
8455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456{
8457 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008458 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008460 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461}
8462
8463/*
8464 * "did_filetype()" function
8465 */
8466/*ARGSUSED*/
8467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008468f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008469 typval_T *argvars;
8470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471{
8472#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008473 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008475 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476#endif
8477}
8478
8479/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008480 * "diff_filler()" function
8481 */
8482/*ARGSUSED*/
8483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008484f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008485 typval_T *argvars;
8486 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008487{
8488#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008489 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008490#endif
8491}
8492
8493/*
8494 * "diff_hlID()" function
8495 */
8496/*ARGSUSED*/
8497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008499 typval_T *argvars;
8500 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008501{
8502#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008503 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008504 static linenr_T prev_lnum = 0;
8505 static int changedtick = 0;
8506 static int fnum = 0;
8507 static int change_start = 0;
8508 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008509 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008510 int filler_lines;
8511 int col;
8512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008513 if (lnum < 0) /* ignore type error in {lnum} arg */
8514 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008515 if (lnum != prev_lnum
8516 || changedtick != curbuf->b_changedtick
8517 || fnum != curbuf->b_fnum)
8518 {
8519 /* New line, buffer, change: need to get the values. */
8520 filler_lines = diff_check(curwin, lnum);
8521 if (filler_lines < 0)
8522 {
8523 if (filler_lines == -1)
8524 {
8525 change_start = MAXCOL;
8526 change_end = -1;
8527 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8528 hlID = HLF_ADD; /* added line */
8529 else
8530 hlID = HLF_CHD; /* changed line */
8531 }
8532 else
8533 hlID = HLF_ADD; /* added line */
8534 }
8535 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008536 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008537 prev_lnum = lnum;
8538 changedtick = curbuf->b_changedtick;
8539 fnum = curbuf->b_fnum;
8540 }
8541
8542 if (hlID == HLF_CHD || hlID == HLF_TXD)
8543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008544 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008545 if (col >= change_start && col <= change_end)
8546 hlID = HLF_TXD; /* changed text */
8547 else
8548 hlID = HLF_CHD; /* changed line */
8549 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008550 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008551#endif
8552}
8553
8554/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008555 * "empty({expr})" function
8556 */
8557 static void
8558f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008559 typval_T *argvars;
8560 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008561{
8562 int n;
8563
8564 switch (argvars[0].v_type)
8565 {
8566 case VAR_STRING:
8567 case VAR_FUNC:
8568 n = argvars[0].vval.v_string == NULL
8569 || *argvars[0].vval.v_string == NUL;
8570 break;
8571 case VAR_NUMBER:
8572 n = argvars[0].vval.v_number == 0;
8573 break;
8574 case VAR_LIST:
8575 n = argvars[0].vval.v_list == NULL
8576 || argvars[0].vval.v_list->lv_first == NULL;
8577 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008578 case VAR_DICT:
8579 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008580 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008581 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008582 default:
8583 EMSG2(_(e_intern2), "f_empty()");
8584 n = 0;
8585 }
8586
8587 rettv->vval.v_number = n;
8588}
8589
8590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 * "escape({string}, {chars})" function
8592 */
8593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008595 typval_T *argvars;
8596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
8598 char_u buf[NUMBUFLEN];
8599
Bram Moolenaar758711c2005-02-02 23:11:38 +00008600 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8601 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008602 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603}
8604
8605/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008606 * "eval()" function
8607 */
8608/*ARGSUSED*/
8609 static void
8610f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008611 typval_T *argvars;
8612 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008613{
8614 char_u *s;
8615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008616 s = get_tv_string_chk(&argvars[0]);
8617 if (s != NULL)
8618 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008620 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8621 {
8622 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008623 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008624 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008625 else if (*s != NUL)
8626 EMSG(_(e_trailing));
8627}
8628
8629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 * "eventhandler()" function
8631 */
8632/*ARGSUSED*/
8633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008634f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008635 typval_T *argvars;
8636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639}
8640
8641/*
8642 * "executable()" function
8643 */
8644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008645f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008646 typval_T *argvars;
8647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650}
8651
8652/*
8653 * "exists()" function
8654 */
8655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008656f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008657 typval_T *argvars;
8658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
8660 char_u *p;
8661 char_u *name;
8662 int n = FALSE;
8663 int len = 0;
8664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008665 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 if (*p == '$') /* environment variable */
8667 {
8668 /* first try "normal" environment variables (fast) */
8669 if (mch_getenv(p + 1) != NULL)
8670 n = TRUE;
8671 else
8672 {
8673 /* try expanding things like $VIM and ${HOME} */
8674 p = expand_env_save(p);
8675 if (p != NULL && *p != '$')
8676 n = TRUE;
8677 vim_free(p);
8678 }
8679 }
8680 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008681 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 else if (*p == '*') /* internal or user defined function */
8683 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008684 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 }
8686 else if (*p == ':')
8687 {
8688 n = cmd_exists(p + 1);
8689 }
8690 else if (*p == '#')
8691 {
8692#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008693 if (p[1] == '#')
8694 n = autocmd_supported(p + 2);
8695 else
8696 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697#endif
8698 }
8699 else /* internal variable */
8700 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008701 char_u *tofree;
8702 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008704 /* get_name_len() takes care of expanding curly braces */
8705 name = p;
8706 len = get_name_len(&p, &tofree, TRUE, FALSE);
8707 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008709 if (tofree != NULL)
8710 name = tofree;
8711 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8712 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008714 /* handle d.key, l[idx], f(expr) */
8715 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8716 if (n)
8717 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 }
8719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008721 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 }
8723
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008724 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725}
8726
8727/*
8728 * "expand()" function
8729 */
8730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008731f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008732 typval_T *argvars;
8733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734{
8735 char_u *s;
8736 int len;
8737 char_u *errormsg;
8738 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8739 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008740 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742 rettv->v_type = VAR_STRING;
8743 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 if (*s == '%' || *s == '#' || *s == '<')
8745 {
8746 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 --emsg_off;
8749 }
8750 else
8751 {
8752 /* When the optional second argument is non-zero, don't remove matches
8753 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008754 if (argvars[1].v_type != VAR_UNKNOWN
8755 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008757 if (!error)
8758 {
8759 ExpandInit(&xpc);
8760 xpc.xp_context = EXPAND_FILES;
8761 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8762 ExpandCleanup(&xpc);
8763 }
8764 else
8765 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 }
8767}
8768
8769/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008770 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008771 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008772 */
8773 static void
8774f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008775 typval_T *argvars;
8776 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008777{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008778 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008779 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008780 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008781 list_T *l1, *l2;
8782 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008783 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008784 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008785
Bram Moolenaare9a41262005-01-15 22:18:47 +00008786 l1 = argvars[0].vval.v_list;
8787 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008788 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8789 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008790 {
8791 if (argvars[2].v_type != VAR_UNKNOWN)
8792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008793 before = get_tv_number_chk(&argvars[2], &error);
8794 if (error)
8795 return; /* type error; errmsg already given */
8796
Bram Moolenaar758711c2005-02-02 23:11:38 +00008797 if (before == l1->lv_len)
8798 item = NULL;
8799 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008800 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008801 item = list_find(l1, before);
8802 if (item == NULL)
8803 {
8804 EMSGN(_(e_listidx), before);
8805 return;
8806 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008807 }
8808 }
8809 else
8810 item = NULL;
8811 list_extend(l1, l2, item);
8812
Bram Moolenaare9a41262005-01-15 22:18:47 +00008813 copy_tv(&argvars[0], rettv);
8814 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008815 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008816 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8817 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008818 dict_T *d1, *d2;
8819 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008820 char_u *action;
8821 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008822 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008823 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008824
8825 d1 = argvars[0].vval.v_dict;
8826 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008827 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8828 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008829 {
8830 /* Check the third argument. */
8831 if (argvars[2].v_type != VAR_UNKNOWN)
8832 {
8833 static char *(av[]) = {"keep", "force", "error"};
8834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008835 action = get_tv_string_chk(&argvars[2]);
8836 if (action == NULL)
8837 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008838 for (i = 0; i < 3; ++i)
8839 if (STRCMP(action, av[i]) == 0)
8840 break;
8841 if (i == 3)
8842 {
8843 EMSGN(_(e_invarg2), action);
8844 return;
8845 }
8846 }
8847 else
8848 action = (char_u *)"force";
8849
8850 /* Go over all entries in the second dict and add them to the
8851 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008852 todo = d2->dv_hashtab.ht_used;
8853 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008854 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008855 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008856 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008857 --todo;
8858 di1 = dict_find(d1, hi2->hi_key, -1);
8859 if (di1 == NULL)
8860 {
8861 di1 = dictitem_copy(HI2DI(hi2));
8862 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8863 dictitem_free(di1);
8864 }
8865 else if (*action == 'e')
8866 {
8867 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8868 break;
8869 }
8870 else if (*action == 'f')
8871 {
8872 clear_tv(&di1->di_tv);
8873 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8874 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008875 }
8876 }
8877
Bram Moolenaare9a41262005-01-15 22:18:47 +00008878 copy_tv(&argvars[0], rettv);
8879 }
8880 }
8881 else
8882 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008883}
8884
8885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 * "filereadable()" function
8887 */
8888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008889f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008890 typval_T *argvars;
8891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892{
8893 FILE *fd;
8894 char_u *p;
8895 int n;
8896
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008897 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8899 {
8900 n = TRUE;
8901 fclose(fd);
8902 }
8903 else
8904 n = FALSE;
8905
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008906 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907}
8908
8909/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008910 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 * rights to write into.
8912 */
8913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008914f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008915 typval_T *argvars;
8916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008918 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919}
8920
Bram Moolenaar33570922005-01-25 22:26:29 +00008921static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008922
8923 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008924findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008925 typval_T *argvars;
8926 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008927 int dir;
8928{
8929#ifdef FEAT_SEARCHPATH
8930 char_u *fname;
8931 char_u *fresult = NULL;
8932 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8933 char_u *p;
8934 char_u pathbuf[NUMBUFLEN];
8935 int count = 1;
8936 int first = TRUE;
8937
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008938 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008939
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008940 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008942 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8943 if (p == NULL)
8944 count = -1; /* error */
8945 else
8946 {
8947 if (*p != NUL)
8948 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008950 if (argvars[2].v_type != VAR_UNKNOWN)
8951 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8952 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008953 }
8954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008955 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008957 do
8958 {
8959 vim_free(fresult);
8960 fresult = find_file_in_path_option(first ? fname : NULL,
8961 first ? (int)STRLEN(fname) : 0,
8962 0, first, path, dir, NULL);
8963 first = FALSE;
8964 } while (--count > 0 && fresult != NULL);
8965 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008966
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008968#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008970#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008971 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008972}
8973
Bram Moolenaar33570922005-01-25 22:26:29 +00008974static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8975static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008976
8977/*
8978 * Implementation of map() and filter().
8979 */
8980 static void
8981filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008982 typval_T *argvars;
8983 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008984 int map;
8985{
8986 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008987 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008988 listitem_T *li, *nli;
8989 list_T *l = NULL;
8990 dictitem_T *di;
8991 hashtab_T *ht;
8992 hashitem_T *hi;
8993 dict_T *d = NULL;
8994 typval_T save_val;
8995 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008996 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008997 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008998 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00008999 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009000
9001 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009002 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009003 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009004 if ((l = argvars[0].vval.v_list) == NULL
9005 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009006 return;
9007 }
9008 else if (argvars[0].v_type == VAR_DICT)
9009 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009010 if ((d = argvars[0].vval.v_dict) == NULL
9011 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009012 return;
9013 }
9014 else
9015 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009016 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009017 return;
9018 }
9019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009020 expr = get_tv_string_buf_chk(&argvars[1], buf);
9021 /* On type errors, the preceding call has already displayed an error
9022 * message. Avoid a misleading error message for an empty string that
9023 * was not passed as argument. */
9024 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009026 prepare_vimvar(VV_VAL, &save_val);
9027 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009028
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009029 /* We reset "did_emsg" to be able to detect whether an error
9030 * occurred during evaluation of the expression. */
9031 save_did_emsg = did_emsg;
9032 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009034 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009035 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009036 prepare_vimvar(VV_KEY, &save_key);
9037 vimvars[VV_KEY].vv_type = VAR_STRING;
9038
9039 ht = &d->dv_hashtab;
9040 hash_lock(ht);
9041 todo = ht->ht_used;
9042 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009044 if (!HASHITEM_EMPTY(hi))
9045 {
9046 --todo;
9047 di = HI2DI(hi);
9048 if (tv_check_lock(di->di_tv.v_lock, msg))
9049 break;
9050 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009051 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009052 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009053 break;
9054 if (!map && rem)
9055 dictitem_remove(d, di);
9056 clear_tv(&vimvars[VV_KEY].vv_tv);
9057 }
9058 }
9059 hash_unlock(ht);
9060
9061 restore_vimvar(VV_KEY, &save_key);
9062 }
9063 else
9064 {
9065 for (li = l->lv_first; li != NULL; li = nli)
9066 {
9067 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009068 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009069 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009070 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009071 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009072 break;
9073 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009074 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009075 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009076 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009077
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009078 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009079
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009080 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009081 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009082
9083 copy_tv(&argvars[0], rettv);
9084}
9085
9086 static int
9087filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009088 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009089 char_u *expr;
9090 int map;
9091 int *remp;
9092{
Bram Moolenaar33570922005-01-25 22:26:29 +00009093 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009094 char_u *s;
9095
Bram Moolenaar33570922005-01-25 22:26:29 +00009096 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009097 s = expr;
9098 if (eval1(&s, &rettv, TRUE) == FAIL)
9099 return FAIL;
9100 if (*s != NUL) /* check for trailing chars after expr */
9101 {
9102 EMSG2(_(e_invexpr2), s);
9103 return FAIL;
9104 }
9105 if (map)
9106 {
9107 /* map(): replace the list item value */
9108 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009109 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009110 *tv = rettv;
9111 }
9112 else
9113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009114 int error = FALSE;
9115
Bram Moolenaare9a41262005-01-15 22:18:47 +00009116 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009117 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009118 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009119 /* On type error, nothing has been removed; return FAIL to stop the
9120 * loop. The error message was given by get_tv_number_chk(). */
9121 if (error)
9122 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009123 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009124 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009125 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009126}
9127
9128/*
9129 * "filter()" function
9130 */
9131 static void
9132f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009133 typval_T *argvars;
9134 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009135{
9136 filter_map(argvars, rettv, FALSE);
9137}
9138
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009139/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009140 * "finddir({fname}[, {path}[, {count}]])" function
9141 */
9142 static void
9143f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009144 typval_T *argvars;
9145 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009146{
9147 findfilendir(argvars, rettv, TRUE);
9148}
9149
9150/*
9151 * "findfile({fname}[, {path}[, {count}]])" function
9152 */
9153 static void
9154f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009155 typval_T *argvars;
9156 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009157{
9158 findfilendir(argvars, rettv, FALSE);
9159}
9160
9161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 * "fnamemodify({fname}, {mods})" function
9163 */
9164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009166 typval_T *argvars;
9167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168{
9169 char_u *fname;
9170 char_u *mods;
9171 int usedlen = 0;
9172 int len;
9173 char_u *fbuf = NULL;
9174 char_u buf[NUMBUFLEN];
9175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009176 fname = get_tv_string_chk(&argvars[0]);
9177 mods = get_tv_string_buf_chk(&argvars[1], buf);
9178 if (fname == NULL || mods == NULL)
9179 fname = NULL;
9180 else
9181 {
9182 len = (int)STRLEN(fname);
9183 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009190 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 vim_free(fbuf);
9192}
9193
Bram Moolenaar33570922005-01-25 22:26:29 +00009194static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195
9196/*
9197 * "foldclosed()" function
9198 */
9199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009201 typval_T *argvars;
9202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 int end;
9204{
9205#ifdef FEAT_FOLDING
9206 linenr_T lnum;
9207 linenr_T first, last;
9208
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009209 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9211 {
9212 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9213 {
9214 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009215 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009217 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 return;
9219 }
9220 }
9221#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009222 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223}
9224
9225/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009226 * "foldclosed()" function
9227 */
9228 static void
9229f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009230 typval_T *argvars;
9231 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009232{
9233 foldclosed_both(argvars, rettv, FALSE);
9234}
9235
9236/*
9237 * "foldclosedend()" function
9238 */
9239 static void
9240f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009241 typval_T *argvars;
9242 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009243{
9244 foldclosed_both(argvars, rettv, TRUE);
9245}
9246
9247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 * "foldlevel()" function
9249 */
9250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009251f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009252 typval_T *argvars;
9253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254{
9255#ifdef FEAT_FOLDING
9256 linenr_T lnum;
9257
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009258 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009260 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 else
9262#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009263 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264}
9265
9266/*
9267 * "foldtext()" function
9268 */
9269/*ARGSUSED*/
9270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009271f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009272 typval_T *argvars;
9273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274{
9275#ifdef FEAT_FOLDING
9276 linenr_T lnum;
9277 char_u *s;
9278 char_u *r;
9279 int len;
9280 char *txt;
9281#endif
9282
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009283 rettv->v_type = VAR_STRING;
9284 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009286 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9287 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9288 <= curbuf->b_ml.ml_line_count
9289 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 {
9291 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009292 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9293 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 {
9295 if (!linewhite(lnum))
9296 break;
9297 ++lnum;
9298 }
9299
9300 /* Find interesting text in this line. */
9301 s = skipwhite(ml_get(lnum));
9302 /* skip C comment-start */
9303 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009306 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009307 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009308 {
9309 s = skipwhite(ml_get(lnum + 1));
9310 if (*s == '*')
9311 s = skipwhite(s + 1);
9312 }
9313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 txt = _("+-%s%3ld lines: ");
9315 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009316 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 + 20 /* for %3ld */
9318 + STRLEN(s))); /* concatenated */
9319 if (r != NULL)
9320 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009321 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9322 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9323 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 len = (int)STRLEN(r);
9325 STRCAT(r, s);
9326 /* remove 'foldmarker' and 'commentstring' */
9327 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009328 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 }
9330 }
9331#endif
9332}
9333
9334/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009335 * "foldtextresult(lnum)" function
9336 */
9337/*ARGSUSED*/
9338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009339f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009340 typval_T *argvars;
9341 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009342{
9343#ifdef FEAT_FOLDING
9344 linenr_T lnum;
9345 char_u *text;
9346 char_u buf[51];
9347 foldinfo_T foldinfo;
9348 int fold_count;
9349#endif
9350
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351 rettv->v_type = VAR_STRING;
9352 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009353#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009354 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009355 /* treat illegal types and illegal string values for {lnum} the same */
9356 if (lnum < 0)
9357 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009358 fold_count = foldedCount(curwin, lnum, &foldinfo);
9359 if (fold_count > 0)
9360 {
9361 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9362 &foldinfo, buf);
9363 if (text == buf)
9364 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009365 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009366 }
9367#endif
9368}
9369
9370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 * "foreground()" function
9372 */
9373/*ARGSUSED*/
9374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009376 typval_T *argvars;
9377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009379 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380#ifdef FEAT_GUI
9381 if (gui.in_use)
9382 gui_mch_set_foreground();
9383#else
9384# ifdef WIN32
9385 win32_set_foreground();
9386# endif
9387#endif
9388}
9389
9390/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009391 * "function()" function
9392 */
9393/*ARGSUSED*/
9394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009395f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009396 typval_T *argvars;
9397 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009398{
9399 char_u *s;
9400
Bram Moolenaara7043832005-01-21 11:56:39 +00009401 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009402 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009403 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009404 EMSG2(_(e_invarg2), s);
9405 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009406 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009407 else
9408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009409 rettv->vval.v_string = vim_strsave(s);
9410 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009411 }
9412}
9413
9414/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009415 * "garbagecollect()" function
9416 */
9417/*ARGSUSED*/
9418 static void
9419f_garbagecollect(argvars, rettv)
9420 typval_T *argvars;
9421 typval_T *rettv;
9422{
9423 garbage_collect();
9424}
9425
9426/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009427 * "get()" function
9428 */
9429 static void
9430f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009431 typval_T *argvars;
9432 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009433{
Bram Moolenaar33570922005-01-25 22:26:29 +00009434 listitem_T *li;
9435 list_T *l;
9436 dictitem_T *di;
9437 dict_T *d;
9438 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009439
Bram Moolenaare9a41262005-01-15 22:18:47 +00009440 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009441 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009442 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009444 int error = FALSE;
9445
9446 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9447 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009448 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009449 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009450 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009451 else if (argvars[0].v_type == VAR_DICT)
9452 {
9453 if ((d = argvars[0].vval.v_dict) != NULL)
9454 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009455 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009456 if (di != NULL)
9457 tv = &di->di_tv;
9458 }
9459 }
9460 else
9461 EMSG2(_(e_listdictarg), "get()");
9462
9463 if (tv == NULL)
9464 {
9465 if (argvars[2].v_type == VAR_UNKNOWN)
9466 rettv->vval.v_number = 0;
9467 else
9468 copy_tv(&argvars[2], rettv);
9469 }
9470 else
9471 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009472}
9473
Bram Moolenaar342337a2005-07-21 21:11:17 +00009474static 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 +00009475
9476/*
9477 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009478 * Return a range (from start to end) of lines in rettv from the specified
9479 * buffer.
9480 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009481 */
9482 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009483get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009484 buf_T *buf;
9485 linenr_T start;
9486 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009487 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009488 typval_T *rettv;
9489{
9490 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009491
Bram Moolenaar342337a2005-07-21 21:11:17 +00009492 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009493 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009494 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009495 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009496 }
9497 else
9498 rettv->vval.v_number = 0;
9499
9500 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9501 return;
9502
9503 if (!retlist)
9504 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009505 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9506 p = ml_get_buf(buf, start, FALSE);
9507 else
9508 p = (char_u *)"";
9509
9510 rettv->v_type = VAR_STRING;
9511 rettv->vval.v_string = vim_strsave(p);
9512 }
9513 else
9514 {
9515 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009516 return;
9517
9518 if (start < 1)
9519 start = 1;
9520 if (end > buf->b_ml.ml_line_count)
9521 end = buf->b_ml.ml_line_count;
9522 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009523 if (list_append_string(rettv->vval.v_list,
9524 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009525 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009526 }
9527}
9528
9529/*
9530 * "getbufline()" function
9531 */
9532 static void
9533f_getbufline(argvars, rettv)
9534 typval_T *argvars;
9535 typval_T *rettv;
9536{
9537 linenr_T lnum;
9538 linenr_T end;
9539 buf_T *buf;
9540
9541 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9542 ++emsg_off;
9543 buf = get_buf_tv(&argvars[0]);
9544 --emsg_off;
9545
Bram Moolenaar661b1822005-07-28 22:36:45 +00009546 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009547 if (argvars[2].v_type == VAR_UNKNOWN)
9548 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009549 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009550 end = get_tv_lnum_buf(&argvars[2], buf);
9551
Bram Moolenaar342337a2005-07-21 21:11:17 +00009552 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009553}
9554
Bram Moolenaar0d660222005-01-07 21:51:51 +00009555/*
9556 * "getbufvar()" function
9557 */
9558 static void
9559f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009560 typval_T *argvars;
9561 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009562{
9563 buf_T *buf;
9564 buf_T *save_curbuf;
9565 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009566 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009568 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9569 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009570 ++emsg_off;
9571 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009572
9573 rettv->v_type = VAR_STRING;
9574 rettv->vval.v_string = NULL;
9575
9576 if (buf != NULL && varname != NULL)
9577 {
9578 if (*varname == '&') /* buffer-local-option */
9579 {
9580 /* set curbuf to be our buf, temporarily */
9581 save_curbuf = curbuf;
9582 curbuf = buf;
9583
9584 get_option_tv(&varname, rettv, TRUE);
9585
9586 /* restore previous notion of curbuf */
9587 curbuf = save_curbuf;
9588 }
9589 else
9590 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009591 if (*varname == NUL)
9592 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9593 * scope prefix before the NUL byte is required by
9594 * find_var_in_ht(). */
9595 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009596 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009597 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009598 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009599 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009600 }
9601 }
9602
9603 --emsg_off;
9604}
9605
9606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 * "getchar()" function
9608 */
9609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009611 typval_T *argvars;
9612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613{
9614 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009615 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616
9617 ++no_mapping;
9618 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009619 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 /* getchar(): blocking wait. */
9621 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009622 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 /* getchar(1): only check if char avail */
9624 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009625 else if (error || vpeekc() == NUL)
9626 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 n = 0;
9628 else
9629 /* getchar(0) and char avail: return char */
9630 n = safe_vgetc();
9631 --no_mapping;
9632 --allow_keys;
9633
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 if (IS_SPECIAL(n) || mod_mask != 0)
9636 {
9637 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9638 int i = 0;
9639
9640 /* Turn a special key into three bytes, plus modifier. */
9641 if (mod_mask != 0)
9642 {
9643 temp[i++] = K_SPECIAL;
9644 temp[i++] = KS_MODIFIER;
9645 temp[i++] = mod_mask;
9646 }
9647 if (IS_SPECIAL(n))
9648 {
9649 temp[i++] = K_SPECIAL;
9650 temp[i++] = K_SECOND(n);
9651 temp[i++] = K_THIRD(n);
9652 }
9653#ifdef FEAT_MBYTE
9654 else if (has_mbyte)
9655 i += (*mb_char2bytes)(n, temp + i);
9656#endif
9657 else
9658 temp[i++] = n;
9659 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 rettv->v_type = VAR_STRING;
9661 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 }
9663}
9664
9665/*
9666 * "getcharmod()" function
9667 */
9668/*ARGSUSED*/
9669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009671 typval_T *argvars;
9672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675}
9676
9677/*
9678 * "getcmdline()" function
9679 */
9680/*ARGSUSED*/
9681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009682f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009683 typval_T *argvars;
9684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686 rettv->v_type = VAR_STRING;
9687 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688}
9689
9690/*
9691 * "getcmdpos()" function
9692 */
9693/*ARGSUSED*/
9694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009696 typval_T *argvars;
9697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700}
9701
9702/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009703 * "getcmdtype()" function
9704 */
9705/*ARGSUSED*/
9706 static void
9707f_getcmdtype(argvars, rettv)
9708 typval_T *argvars;
9709 typval_T *rettv;
9710{
9711 rettv->v_type = VAR_STRING;
9712 rettv->vval.v_string = alloc(2);
9713 if (rettv->vval.v_string != NULL)
9714 {
9715 rettv->vval.v_string[0] = get_cmdline_type();
9716 rettv->vval.v_string[1] = NUL;
9717 }
9718}
9719
9720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 * "getcwd()" function
9722 */
9723/*ARGSUSED*/
9724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009725f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009726 typval_T *argvars;
9727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728{
9729 char_u cwd[MAXPATHL];
9730
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009731 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009733 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 else
9735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009738 if (rettv->vval.v_string != NULL)
9739 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740#endif
9741 }
9742}
9743
9744/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009745 * "getfontname()" function
9746 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009747/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009749f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009750 typval_T *argvars;
9751 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009752{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009753 rettv->v_type = VAR_STRING;
9754 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009755#ifdef FEAT_GUI
9756 if (gui.in_use)
9757 {
9758 GuiFont font;
9759 char_u *name = NULL;
9760
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009761 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009762 {
9763 /* Get the "Normal" font. Either the name saved by
9764 * hl_set_font_name() or from the font ID. */
9765 font = gui.norm_font;
9766 name = hl_get_font_name();
9767 }
9768 else
9769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009770 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009771 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9772 return;
9773 font = gui_mch_get_font(name, FALSE);
9774 if (font == NOFONT)
9775 return; /* Invalid font name, return empty string. */
9776 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009778 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009779 gui_mch_free_font(font);
9780 }
9781#endif
9782}
9783
9784/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009785 * "getfperm({fname})" function
9786 */
9787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009788f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009789 typval_T *argvars;
9790 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009791{
9792 char_u *fname;
9793 struct stat st;
9794 char_u *perm = NULL;
9795 char_u flags[] = "rwx";
9796 int i;
9797
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009799
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009801 if (mch_stat((char *)fname, &st) >= 0)
9802 {
9803 perm = vim_strsave((char_u *)"---------");
9804 if (perm != NULL)
9805 {
9806 for (i = 0; i < 9; i++)
9807 {
9808 if (st.st_mode & (1 << (8 - i)))
9809 perm[i] = flags[i % 3];
9810 }
9811 }
9812 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009813 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009814}
9815
9816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 * "getfsize({fname})" function
9818 */
9819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009820f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009821 typval_T *argvars;
9822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823{
9824 char_u *fname;
9825 struct stat st;
9826
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830
9831 if (mch_stat((char *)fname, &st) >= 0)
9832 {
9833 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009834 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 }
9838 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009839 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840}
9841
9842/*
9843 * "getftime({fname})" function
9844 */
9845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009846f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009847 typval_T *argvars;
9848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849{
9850 char_u *fname;
9851 struct stat st;
9852
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009853 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854
9855 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859}
9860
9861/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009862 * "getftype({fname})" function
9863 */
9864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009865f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009866 typval_T *argvars;
9867 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009868{
9869 char_u *fname;
9870 struct stat st;
9871 char_u *type = NULL;
9872 char *t;
9873
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009875
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009877 if (mch_lstat((char *)fname, &st) >= 0)
9878 {
9879#ifdef S_ISREG
9880 if (S_ISREG(st.st_mode))
9881 t = "file";
9882 else if (S_ISDIR(st.st_mode))
9883 t = "dir";
9884# ifdef S_ISLNK
9885 else if (S_ISLNK(st.st_mode))
9886 t = "link";
9887# endif
9888# ifdef S_ISBLK
9889 else if (S_ISBLK(st.st_mode))
9890 t = "bdev";
9891# endif
9892# ifdef S_ISCHR
9893 else if (S_ISCHR(st.st_mode))
9894 t = "cdev";
9895# endif
9896# ifdef S_ISFIFO
9897 else if (S_ISFIFO(st.st_mode))
9898 t = "fifo";
9899# endif
9900# ifdef S_ISSOCK
9901 else if (S_ISSOCK(st.st_mode))
9902 t = "fifo";
9903# endif
9904 else
9905 t = "other";
9906#else
9907# ifdef S_IFMT
9908 switch (st.st_mode & S_IFMT)
9909 {
9910 case S_IFREG: t = "file"; break;
9911 case S_IFDIR: t = "dir"; break;
9912# ifdef S_IFLNK
9913 case S_IFLNK: t = "link"; break;
9914# endif
9915# ifdef S_IFBLK
9916 case S_IFBLK: t = "bdev"; break;
9917# endif
9918# ifdef S_IFCHR
9919 case S_IFCHR: t = "cdev"; break;
9920# endif
9921# ifdef S_IFIFO
9922 case S_IFIFO: t = "fifo"; break;
9923# endif
9924# ifdef S_IFSOCK
9925 case S_IFSOCK: t = "socket"; break;
9926# endif
9927 default: t = "other";
9928 }
9929# else
9930 if (mch_isdir(fname))
9931 t = "dir";
9932 else
9933 t = "file";
9934# endif
9935#endif
9936 type = vim_strsave((char_u *)t);
9937 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009938 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009939}
9940
9941/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009942 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009943 */
9944 static void
9945f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009946 typval_T *argvars;
9947 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009948{
9949 linenr_T lnum;
9950 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009951 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009952
9953 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009954 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009955 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009956 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009957 retlist = FALSE;
9958 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009959 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009960 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009961 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009962 retlist = TRUE;
9963 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009964
Bram Moolenaar342337a2005-07-21 21:11:17 +00009965 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009966}
9967
9968/*
Bram Moolenaara5525202006-03-02 22:52:09 +00009969 * "getpos(string)" function
9970 */
9971 static void
9972f_getpos(argvars, rettv)
9973 typval_T *argvars;
9974 typval_T *rettv;
9975{
9976 pos_T *fp;
9977 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009978 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009979
9980 if (rettv_list_alloc(rettv) == OK)
9981 {
9982 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009983 fp = var2fpos(&argvars[0], TRUE, &fnum);
9984 if (fnum != -1)
9985 list_append_number(l, (varnumber_T)fnum);
9986 else
9987 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +00009988 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
9989 : (varnumber_T)0);
9990 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
9991 : (varnumber_T)0);
9992 list_append_number(l,
9993#ifdef FEAT_VIRTUALEDIT
9994 (fp != NULL) ? (varnumber_T)fp->coladd :
9995#endif
9996 (varnumber_T)0);
9997 }
9998 else
9999 rettv->vval.v_number = FALSE;
10000}
10001
10002/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010003 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010004 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010005/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010006 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010007f_getqflist(argvars, rettv)
10008 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010009 typval_T *rettv;
10010{
10011#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010012 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010013#endif
10014
10015 rettv->vval.v_number = FALSE;
10016#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010017 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010018 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010019 wp = NULL;
10020 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10021 {
10022 wp = find_win_by_nr(&argvars[0]);
10023 if (wp == NULL)
10024 return;
10025 }
10026
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010027 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010028 }
10029#endif
10030}
10031
10032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 * "getreg()" function
10034 */
10035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010036f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010037 typval_T *argvars;
10038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039{
10040 char_u *strregname;
10041 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010042 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010043 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010045 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010046 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010047 strregname = get_tv_string_chk(&argvars[0]);
10048 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010049 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010050 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010053 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 regname = (strregname == NULL ? '"' : *strregname);
10055 if (regname == 0)
10056 regname = '"';
10057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010059 rettv->vval.v_string = error ? NULL :
10060 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061}
10062
10063/*
10064 * "getregtype()" function
10065 */
10066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010067f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010068 typval_T *argvars;
10069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070{
10071 char_u *strregname;
10072 int regname;
10073 char_u buf[NUMBUFLEN + 2];
10074 long reglen = 0;
10075
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010076 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010077 {
10078 strregname = get_tv_string_chk(&argvars[0]);
10079 if (strregname == NULL) /* type error; errmsg already given */
10080 {
10081 rettv->v_type = VAR_STRING;
10082 rettv->vval.v_string = NULL;
10083 return;
10084 }
10085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 else
10087 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010088 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089
10090 regname = (strregname == NULL ? '"' : *strregname);
10091 if (regname == 0)
10092 regname = '"';
10093
10094 buf[0] = NUL;
10095 buf[1] = NUL;
10096 switch (get_reg_type(regname, &reglen))
10097 {
10098 case MLINE: buf[0] = 'V'; break;
10099 case MCHAR: buf[0] = 'v'; break;
10100#ifdef FEAT_VISUAL
10101 case MBLOCK:
10102 buf[0] = Ctrl_V;
10103 sprintf((char *)buf + 1, "%ld", reglen + 1);
10104 break;
10105#endif
10106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107 rettv->v_type = VAR_STRING;
10108 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109}
10110
10111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 * "getwinposx()" function
10113 */
10114/*ARGSUSED*/
10115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010116f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010117 typval_T *argvars;
10118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121#ifdef FEAT_GUI
10122 if (gui.in_use)
10123 {
10124 int x, y;
10125
10126 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 }
10129#endif
10130}
10131
10132/*
10133 * "getwinposy()" function
10134 */
10135/*ARGSUSED*/
10136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010138 typval_T *argvars;
10139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010141 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142#ifdef FEAT_GUI
10143 if (gui.in_use)
10144 {
10145 int x, y;
10146
10147 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 }
10150#endif
10151}
10152
Bram Moolenaara40058a2005-07-11 22:42:07 +000010153 static win_T *
10154find_win_by_nr(vp)
10155 typval_T *vp;
10156{
10157#ifdef FEAT_WINDOWS
10158 win_T *wp;
10159#endif
10160 int nr;
10161
10162 nr = get_tv_number_chk(vp, NULL);
10163
10164#ifdef FEAT_WINDOWS
10165 if (nr < 0)
10166 return NULL;
10167 if (nr == 0)
10168 return curwin;
10169
10170 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10171 if (--nr <= 0)
10172 break;
10173 return wp;
10174#else
10175 if (nr == 0 || nr == 1)
10176 return curwin;
10177 return NULL;
10178#endif
10179}
10180
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181/*
10182 * "getwinvar()" function
10183 */
10184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010185f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010186 typval_T *argvars;
10187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188{
10189 win_T *win, *oldcurwin;
10190 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010191 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 varname = get_tv_string_chk(&argvars[1]);
10195 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010197 rettv->v_type = VAR_STRING;
10198 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199
10200 if (win != NULL && varname != NULL)
10201 {
10202 if (*varname == '&') /* window-local-option */
10203 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010204 /* Set curwin to be our win, temporarily. Also set curbuf, so
10205 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 oldcurwin = curwin;
10207 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010208 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010210 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211
10212 /* restore previous notion of curwin */
10213 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010214 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 }
10216 else
10217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010218 if (*varname == NUL)
10219 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10220 * scope prefix before the NUL byte is required by
10221 * find_var_in_ht(). */
10222 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010224 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010226 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 }
10228 }
10229
10230 --emsg_off;
10231}
10232
10233/*
10234 * "glob()" function
10235 */
10236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010237f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010238 typval_T *argvars;
10239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240{
10241 expand_T xpc;
10242
10243 ExpandInit(&xpc);
10244 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010245 rettv->v_type = VAR_STRING;
10246 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10248 ExpandCleanup(&xpc);
10249}
10250
10251/*
10252 * "globpath()" function
10253 */
10254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010255f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010256 typval_T *argvars;
10257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258{
10259 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010260 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010262 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010263 if (file == NULL)
10264 rettv->vval.v_string = NULL;
10265 else
10266 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267}
10268
10269/*
10270 * "has()" function
10271 */
10272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010273f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010274 typval_T *argvars;
10275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276{
10277 int i;
10278 char_u *name;
10279 int n = FALSE;
10280 static char *(has_list[]) =
10281 {
10282#ifdef AMIGA
10283 "amiga",
10284# ifdef FEAT_ARP
10285 "arp",
10286# endif
10287#endif
10288#ifdef __BEOS__
10289 "beos",
10290#endif
10291#ifdef MSDOS
10292# ifdef DJGPP
10293 "dos32",
10294# else
10295 "dos16",
10296# endif
10297#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010298#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 "mac",
10300#endif
10301#if defined(MACOS_X_UNIX)
10302 "macunix",
10303#endif
10304#ifdef OS2
10305 "os2",
10306#endif
10307#ifdef __QNX__
10308 "qnx",
10309#endif
10310#ifdef RISCOS
10311 "riscos",
10312#endif
10313#ifdef UNIX
10314 "unix",
10315#endif
10316#ifdef VMS
10317 "vms",
10318#endif
10319#ifdef WIN16
10320 "win16",
10321#endif
10322#ifdef WIN32
10323 "win32",
10324#endif
10325#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10326 "win32unix",
10327#endif
10328#ifdef WIN64
10329 "win64",
10330#endif
10331#ifdef EBCDIC
10332 "ebcdic",
10333#endif
10334#ifndef CASE_INSENSITIVE_FILENAME
10335 "fname_case",
10336#endif
10337#ifdef FEAT_ARABIC
10338 "arabic",
10339#endif
10340#ifdef FEAT_AUTOCMD
10341 "autocmd",
10342#endif
10343#ifdef FEAT_BEVAL
10344 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010345# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10346 "balloon_multiline",
10347# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348#endif
10349#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10350 "builtin_terms",
10351# ifdef ALL_BUILTIN_TCAPS
10352 "all_builtin_terms",
10353# endif
10354#endif
10355#ifdef FEAT_BYTEOFF
10356 "byte_offset",
10357#endif
10358#ifdef FEAT_CINDENT
10359 "cindent",
10360#endif
10361#ifdef FEAT_CLIENTSERVER
10362 "clientserver",
10363#endif
10364#ifdef FEAT_CLIPBOARD
10365 "clipboard",
10366#endif
10367#ifdef FEAT_CMDL_COMPL
10368 "cmdline_compl",
10369#endif
10370#ifdef FEAT_CMDHIST
10371 "cmdline_hist",
10372#endif
10373#ifdef FEAT_COMMENTS
10374 "comments",
10375#endif
10376#ifdef FEAT_CRYPT
10377 "cryptv",
10378#endif
10379#ifdef FEAT_CSCOPE
10380 "cscope",
10381#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010382#ifdef CURSOR_SHAPE
10383 "cursorshape",
10384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385#ifdef DEBUG
10386 "debug",
10387#endif
10388#ifdef FEAT_CON_DIALOG
10389 "dialog_con",
10390#endif
10391#ifdef FEAT_GUI_DIALOG
10392 "dialog_gui",
10393#endif
10394#ifdef FEAT_DIFF
10395 "diff",
10396#endif
10397#ifdef FEAT_DIGRAPHS
10398 "digraphs",
10399#endif
10400#ifdef FEAT_DND
10401 "dnd",
10402#endif
10403#ifdef FEAT_EMACS_TAGS
10404 "emacs_tags",
10405#endif
10406 "eval", /* always present, of course! */
10407#ifdef FEAT_EX_EXTRA
10408 "ex_extra",
10409#endif
10410#ifdef FEAT_SEARCH_EXTRA
10411 "extra_search",
10412#endif
10413#ifdef FEAT_FKMAP
10414 "farsi",
10415#endif
10416#ifdef FEAT_SEARCHPATH
10417 "file_in_path",
10418#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010419#if defined(UNIX) && !defined(USE_SYSTEM)
10420 "filterpipe",
10421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422#ifdef FEAT_FIND_ID
10423 "find_in_path",
10424#endif
10425#ifdef FEAT_FOLDING
10426 "folding",
10427#endif
10428#ifdef FEAT_FOOTER
10429 "footer",
10430#endif
10431#if !defined(USE_SYSTEM) && defined(UNIX)
10432 "fork",
10433#endif
10434#ifdef FEAT_GETTEXT
10435 "gettext",
10436#endif
10437#ifdef FEAT_GUI
10438 "gui",
10439#endif
10440#ifdef FEAT_GUI_ATHENA
10441# ifdef FEAT_GUI_NEXTAW
10442 "gui_neXtaw",
10443# else
10444 "gui_athena",
10445# endif
10446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447#ifdef FEAT_GUI_GTK
10448 "gui_gtk",
10449# ifdef HAVE_GTK2
10450 "gui_gtk2",
10451# endif
10452#endif
10453#ifdef FEAT_GUI_MAC
10454 "gui_mac",
10455#endif
10456#ifdef FEAT_GUI_MOTIF
10457 "gui_motif",
10458#endif
10459#ifdef FEAT_GUI_PHOTON
10460 "gui_photon",
10461#endif
10462#ifdef FEAT_GUI_W16
10463 "gui_win16",
10464#endif
10465#ifdef FEAT_GUI_W32
10466 "gui_win32",
10467#endif
10468#ifdef FEAT_HANGULIN
10469 "hangul_input",
10470#endif
10471#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10472 "iconv",
10473#endif
10474#ifdef FEAT_INS_EXPAND
10475 "insert_expand",
10476#endif
10477#ifdef FEAT_JUMPLIST
10478 "jumplist",
10479#endif
10480#ifdef FEAT_KEYMAP
10481 "keymap",
10482#endif
10483#ifdef FEAT_LANGMAP
10484 "langmap",
10485#endif
10486#ifdef FEAT_LIBCALL
10487 "libcall",
10488#endif
10489#ifdef FEAT_LINEBREAK
10490 "linebreak",
10491#endif
10492#ifdef FEAT_LISP
10493 "lispindent",
10494#endif
10495#ifdef FEAT_LISTCMDS
10496 "listcmds",
10497#endif
10498#ifdef FEAT_LOCALMAP
10499 "localmap",
10500#endif
10501#ifdef FEAT_MENU
10502 "menu",
10503#endif
10504#ifdef FEAT_SESSION
10505 "mksession",
10506#endif
10507#ifdef FEAT_MODIFY_FNAME
10508 "modify_fname",
10509#endif
10510#ifdef FEAT_MOUSE
10511 "mouse",
10512#endif
10513#ifdef FEAT_MOUSESHAPE
10514 "mouseshape",
10515#endif
10516#if defined(UNIX) || defined(VMS)
10517# ifdef FEAT_MOUSE_DEC
10518 "mouse_dec",
10519# endif
10520# ifdef FEAT_MOUSE_GPM
10521 "mouse_gpm",
10522# endif
10523# ifdef FEAT_MOUSE_JSB
10524 "mouse_jsbterm",
10525# endif
10526# ifdef FEAT_MOUSE_NET
10527 "mouse_netterm",
10528# endif
10529# ifdef FEAT_MOUSE_PTERM
10530 "mouse_pterm",
10531# endif
10532# ifdef FEAT_MOUSE_XTERM
10533 "mouse_xterm",
10534# endif
10535#endif
10536#ifdef FEAT_MBYTE
10537 "multi_byte",
10538#endif
10539#ifdef FEAT_MBYTE_IME
10540 "multi_byte_ime",
10541#endif
10542#ifdef FEAT_MULTI_LANG
10543 "multi_lang",
10544#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010545#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010546#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010547 "mzscheme",
10548#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550#ifdef FEAT_OLE
10551 "ole",
10552#endif
10553#ifdef FEAT_OSFILETYPE
10554 "osfiletype",
10555#endif
10556#ifdef FEAT_PATH_EXTRA
10557 "path_extra",
10558#endif
10559#ifdef FEAT_PERL
10560#ifndef DYNAMIC_PERL
10561 "perl",
10562#endif
10563#endif
10564#ifdef FEAT_PYTHON
10565#ifndef DYNAMIC_PYTHON
10566 "python",
10567#endif
10568#endif
10569#ifdef FEAT_POSTSCRIPT
10570 "postscript",
10571#endif
10572#ifdef FEAT_PRINTER
10573 "printer",
10574#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010575#ifdef FEAT_PROFILE
10576 "profile",
10577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578#ifdef FEAT_QUICKFIX
10579 "quickfix",
10580#endif
10581#ifdef FEAT_RIGHTLEFT
10582 "rightleft",
10583#endif
10584#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10585 "ruby",
10586#endif
10587#ifdef FEAT_SCROLLBIND
10588 "scrollbind",
10589#endif
10590#ifdef FEAT_CMDL_INFO
10591 "showcmd",
10592 "cmdline_info",
10593#endif
10594#ifdef FEAT_SIGNS
10595 "signs",
10596#endif
10597#ifdef FEAT_SMARTINDENT
10598 "smartindent",
10599#endif
10600#ifdef FEAT_SNIFF
10601 "sniff",
10602#endif
10603#ifdef FEAT_STL_OPT
10604 "statusline",
10605#endif
10606#ifdef FEAT_SUN_WORKSHOP
10607 "sun_workshop",
10608#endif
10609#ifdef FEAT_NETBEANS_INTG
10610 "netbeans_intg",
10611#endif
10612#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010613 "spell",
10614#endif
10615#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 "syntax",
10617#endif
10618#if defined(USE_SYSTEM) || !defined(UNIX)
10619 "system",
10620#endif
10621#ifdef FEAT_TAG_BINS
10622 "tag_binary",
10623#endif
10624#ifdef FEAT_TAG_OLDSTATIC
10625 "tag_old_static",
10626#endif
10627#ifdef FEAT_TAG_ANYWHITE
10628 "tag_any_white",
10629#endif
10630#ifdef FEAT_TCL
10631# ifndef DYNAMIC_TCL
10632 "tcl",
10633# endif
10634#endif
10635#ifdef TERMINFO
10636 "terminfo",
10637#endif
10638#ifdef FEAT_TERMRESPONSE
10639 "termresponse",
10640#endif
10641#ifdef FEAT_TEXTOBJ
10642 "textobjects",
10643#endif
10644#ifdef HAVE_TGETENT
10645 "tgetent",
10646#endif
10647#ifdef FEAT_TITLE
10648 "title",
10649#endif
10650#ifdef FEAT_TOOLBAR
10651 "toolbar",
10652#endif
10653#ifdef FEAT_USR_CMDS
10654 "user-commands", /* was accidentally included in 5.4 */
10655 "user_commands",
10656#endif
10657#ifdef FEAT_VIMINFO
10658 "viminfo",
10659#endif
10660#ifdef FEAT_VERTSPLIT
10661 "vertsplit",
10662#endif
10663#ifdef FEAT_VIRTUALEDIT
10664 "virtualedit",
10665#endif
10666#ifdef FEAT_VISUAL
10667 "visual",
10668#endif
10669#ifdef FEAT_VISUALEXTRA
10670 "visualextra",
10671#endif
10672#ifdef FEAT_VREPLACE
10673 "vreplace",
10674#endif
10675#ifdef FEAT_WILDIGN
10676 "wildignore",
10677#endif
10678#ifdef FEAT_WILDMENU
10679 "wildmenu",
10680#endif
10681#ifdef FEAT_WINDOWS
10682 "windows",
10683#endif
10684#ifdef FEAT_WAK
10685 "winaltkeys",
10686#endif
10687#ifdef FEAT_WRITEBACKUP
10688 "writebackup",
10689#endif
10690#ifdef FEAT_XIM
10691 "xim",
10692#endif
10693#ifdef FEAT_XFONTSET
10694 "xfontset",
10695#endif
10696#ifdef USE_XSMP
10697 "xsmp",
10698#endif
10699#ifdef USE_XSMP_INTERACT
10700 "xsmp_interact",
10701#endif
10702#ifdef FEAT_XCLIPBOARD
10703 "xterm_clipboard",
10704#endif
10705#ifdef FEAT_XTERM_SAVE
10706 "xterm_save",
10707#endif
10708#if defined(UNIX) && defined(FEAT_X11)
10709 "X11",
10710#endif
10711 NULL
10712 };
10713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010714 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 for (i = 0; has_list[i] != NULL; ++i)
10716 if (STRICMP(name, has_list[i]) == 0)
10717 {
10718 n = TRUE;
10719 break;
10720 }
10721
10722 if (n == FALSE)
10723 {
10724 if (STRNICMP(name, "patch", 5) == 0)
10725 n = has_patch(atoi((char *)name + 5));
10726 else if (STRICMP(name, "vim_starting") == 0)
10727 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010728#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10729 else if (STRICMP(name, "balloon_multiline") == 0)
10730 n = multiline_balloon_available();
10731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732#ifdef DYNAMIC_TCL
10733 else if (STRICMP(name, "tcl") == 0)
10734 n = tcl_enabled(FALSE);
10735#endif
10736#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10737 else if (STRICMP(name, "iconv") == 0)
10738 n = iconv_enabled(FALSE);
10739#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010740#ifdef DYNAMIC_MZSCHEME
10741 else if (STRICMP(name, "mzscheme") == 0)
10742 n = mzscheme_enabled(FALSE);
10743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744#ifdef DYNAMIC_RUBY
10745 else if (STRICMP(name, "ruby") == 0)
10746 n = ruby_enabled(FALSE);
10747#endif
10748#ifdef DYNAMIC_PYTHON
10749 else if (STRICMP(name, "python") == 0)
10750 n = python_enabled(FALSE);
10751#endif
10752#ifdef DYNAMIC_PERL
10753 else if (STRICMP(name, "perl") == 0)
10754 n = perl_enabled(FALSE);
10755#endif
10756#ifdef FEAT_GUI
10757 else if (STRICMP(name, "gui_running") == 0)
10758 n = (gui.in_use || gui.starting);
10759# ifdef FEAT_GUI_W32
10760 else if (STRICMP(name, "gui_win32s") == 0)
10761 n = gui_is_win32s();
10762# endif
10763# ifdef FEAT_BROWSE
10764 else if (STRICMP(name, "browse") == 0)
10765 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10766# endif
10767#endif
10768#ifdef FEAT_SYN_HL
10769 else if (STRICMP(name, "syntax_items") == 0)
10770 n = syntax_present(curbuf);
10771#endif
10772#if defined(WIN3264)
10773 else if (STRICMP(name, "win95") == 0)
10774 n = mch_windows95();
10775#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010776#ifdef FEAT_NETBEANS_INTG
10777 else if (STRICMP(name, "netbeans_enabled") == 0)
10778 n = usingNetbeans;
10779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 }
10781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783}
10784
10785/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010786 * "has_key()" function
10787 */
10788 static void
10789f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010790 typval_T *argvars;
10791 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010792{
10793 rettv->vval.v_number = 0;
10794 if (argvars[0].v_type != VAR_DICT)
10795 {
10796 EMSG(_(e_dictreq));
10797 return;
10798 }
10799 if (argvars[0].vval.v_dict == NULL)
10800 return;
10801
10802 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010803 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010804}
10805
10806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 * "hasmapto()" function
10808 */
10809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010810f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010811 typval_T *argvars;
10812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813{
10814 char_u *name;
10815 char_u *mode;
10816 char_u buf[NUMBUFLEN];
10817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010818 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010819 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 mode = (char_u *)"nvo";
10821 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010822 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823
10824 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010825 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010827 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828}
10829
10830/*
10831 * "histadd()" function
10832 */
10833/*ARGSUSED*/
10834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010835f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010836 typval_T *argvars;
10837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838{
10839#ifdef FEAT_CMDHIST
10840 int histype;
10841 char_u *str;
10842 char_u buf[NUMBUFLEN];
10843#endif
10844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 if (check_restricted() || check_secure())
10847 return;
10848#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010849 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10850 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 if (histype >= 0)
10852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010853 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 if (*str != NUL)
10855 {
10856 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010857 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 return;
10859 }
10860 }
10861#endif
10862}
10863
10864/*
10865 * "histdel()" function
10866 */
10867/*ARGSUSED*/
10868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010870 typval_T *argvars;
10871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872{
10873#ifdef FEAT_CMDHIST
10874 int n;
10875 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010876 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010878 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10879 if (str == NULL)
10880 n = 0;
10881 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010883 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010884 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010886 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010887 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 else
10889 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010890 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010891 get_tv_string_buf(&argvars[1], buf));
10892 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010894 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895#endif
10896}
10897
10898/*
10899 * "histget()" function
10900 */
10901/*ARGSUSED*/
10902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010904 typval_T *argvars;
10905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906{
10907#ifdef FEAT_CMDHIST
10908 int type;
10909 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010910 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010912 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10913 if (str == NULL)
10914 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010916 {
10917 type = get_histtype(str);
10918 if (argvars[1].v_type == VAR_UNKNOWN)
10919 idx = get_history_idx(type);
10920 else
10921 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10922 /* -1 on type error */
10923 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010928 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929}
10930
10931/*
10932 * "histnr()" function
10933 */
10934/*ARGSUSED*/
10935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010936f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010937 typval_T *argvars;
10938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939{
10940 int i;
10941
10942#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010943 char_u *history = get_tv_string_chk(&argvars[0]);
10944
10945 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 if (i >= HIST_CMD && i < HIST_COUNT)
10947 i = get_history_idx(i);
10948 else
10949#endif
10950 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010951 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952}
10953
10954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 * "highlightID(name)" function
10956 */
10957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010958f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010959 typval_T *argvars;
10960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010962 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963}
10964
10965/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010966 * "highlight_exists()" function
10967 */
10968 static void
10969f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010970 typval_T *argvars;
10971 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010972{
10973 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10974}
10975
10976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 * "hostname()" function
10978 */
10979/*ARGSUSED*/
10980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010981f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010982 typval_T *argvars;
10983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984{
10985 char_u hostname[256];
10986
10987 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010988 rettv->v_type = VAR_STRING;
10989 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990}
10991
10992/*
10993 * iconv() function
10994 */
10995/*ARGSUSED*/
10996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010997f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010998 typval_T *argvars;
10999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000{
11001#ifdef FEAT_MBYTE
11002 char_u buf1[NUMBUFLEN];
11003 char_u buf2[NUMBUFLEN];
11004 char_u *from, *to, *str;
11005 vimconv_T vimconv;
11006#endif
11007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011008 rettv->v_type = VAR_STRING;
11009 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010
11011#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011012 str = get_tv_string(&argvars[0]);
11013 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11014 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 vimconv.vc_type = CONV_NONE;
11016 convert_setup(&vimconv, from, to);
11017
11018 /* If the encodings are equal, no conversion needed. */
11019 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011020 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023
11024 convert_setup(&vimconv, NULL, NULL);
11025 vim_free(from);
11026 vim_free(to);
11027#endif
11028}
11029
11030/*
11031 * "indent()" function
11032 */
11033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011034f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011035 typval_T *argvars;
11036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037{
11038 linenr_T lnum;
11039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045}
11046
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011047/*
11048 * "index()" function
11049 */
11050 static void
11051f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011052 typval_T *argvars;
11053 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011054{
Bram Moolenaar33570922005-01-25 22:26:29 +000011055 list_T *l;
11056 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011057 long idx = 0;
11058 int ic = FALSE;
11059
11060 rettv->vval.v_number = -1;
11061 if (argvars[0].v_type != VAR_LIST)
11062 {
11063 EMSG(_(e_listreq));
11064 return;
11065 }
11066 l = argvars[0].vval.v_list;
11067 if (l != NULL)
11068 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011069 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011070 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011071 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011072 int error = FALSE;
11073
Bram Moolenaar758711c2005-02-02 23:11:38 +000011074 /* Start at specified item. Use the cached index that list_find()
11075 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011077 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011078 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011079 ic = get_tv_number_chk(&argvars[3], &error);
11080 if (error)
11081 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011082 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011083
Bram Moolenaar758711c2005-02-02 23:11:38 +000011084 for ( ; item != NULL; item = item->li_next, ++idx)
11085 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011086 {
11087 rettv->vval.v_number = idx;
11088 break;
11089 }
11090 }
11091}
11092
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093static int inputsecret_flag = 0;
11094
11095/*
11096 * "input()" function
11097 * Also handles inputsecret() when inputsecret is set.
11098 */
11099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011100f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011101 typval_T *argvars;
11102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011104 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 char_u *p = NULL;
11106 int c;
11107 char_u buf[NUMBUFLEN];
11108 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011110 int xp_type = EXPAND_NOTHING;
11111 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011113 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114
11115#ifdef NO_CONSOLE_INPUT
11116 /* While starting up, there is no place to enter text. */
11117 if (no_console_input())
11118 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 return;
11121 }
11122#endif
11123
11124 cmd_silent = FALSE; /* Want to see the prompt. */
11125 if (prompt != NULL)
11126 {
11127 /* Only the part of the message after the last NL is considered as
11128 * prompt for the command line */
11129 p = vim_strrchr(prompt, '\n');
11130 if (p == NULL)
11131 p = prompt;
11132 else
11133 {
11134 ++p;
11135 c = *p;
11136 *p = NUL;
11137 msg_start();
11138 msg_clr_eos();
11139 msg_puts_attr(prompt, echo_attr);
11140 msg_didout = FALSE;
11141 msg_starthere();
11142 *p = c;
11143 }
11144 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011146 if (argvars[1].v_type != VAR_UNKNOWN)
11147 {
11148 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11149 if (defstr != NULL)
11150 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151
Bram Moolenaar4463f292005-09-25 22:20:24 +000011152 if (argvars[2].v_type != VAR_UNKNOWN)
11153 {
11154 char_u *xp_name;
11155 int xp_namelen;
11156 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011157
Bram Moolenaar4463f292005-09-25 22:20:24 +000011158 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011159
Bram Moolenaar4463f292005-09-25 22:20:24 +000011160 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11161 if (xp_name == NULL)
11162 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011163
Bram Moolenaar4463f292005-09-25 22:20:24 +000011164 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011165
Bram Moolenaar4463f292005-09-25 22:20:24 +000011166 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11167 &xp_arg) == FAIL)
11168 return;
11169 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011170 }
11171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011172 if (defstr != NULL)
11173 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011174 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11175 xp_type, xp_arg);
11176
11177 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011179 /* since the user typed this, no need to wait for return */
11180 need_wait_return = FALSE;
11181 msg_didout = FALSE;
11182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 cmd_silent = cmd_silent_save;
11184}
11185
11186/*
11187 * "inputdialog()" function
11188 */
11189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011190f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011191 typval_T *argvars;
11192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193{
11194#if defined(FEAT_GUI_TEXTDIALOG)
11195 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11196 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11197 {
11198 char_u *message;
11199 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011200 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011202 message = get_tv_string_chk(&argvars[0]);
11203 if (argvars[1].v_type != VAR_UNKNOWN
11204 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011205 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 else
11207 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011208 if (message != NULL && defstr != NULL
11209 && do_dialog(VIM_QUESTION, NULL, message,
11210 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 else
11213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011214 if (message != NULL && defstr != NULL
11215 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011216 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011217 rettv->vval.v_string = vim_strsave(
11218 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011220 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011222 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 }
11224 else
11225#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227}
11228
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011229/*
11230 * "inputlist()" function
11231 */
11232 static void
11233f_inputlist(argvars, rettv)
11234 typval_T *argvars;
11235 typval_T *rettv;
11236{
11237 listitem_T *li;
11238 int selected;
11239 int mouse_used;
11240
11241 rettv->vval.v_number = 0;
11242#ifdef NO_CONSOLE_INPUT
11243 /* While starting up, there is no place to enter text. */
11244 if (no_console_input())
11245 return;
11246#endif
11247 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11248 {
11249 EMSG2(_(e_listarg), "inputlist()");
11250 return;
11251 }
11252
11253 msg_start();
11254 lines_left = Rows; /* avoid more prompt */
11255 msg_scroll = TRUE;
11256 msg_clr_eos();
11257
11258 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11259 {
11260 msg_puts(get_tv_string(&li->li_tv));
11261 msg_putchar('\n');
11262 }
11263
11264 /* Ask for choice. */
11265 selected = prompt_for_number(&mouse_used);
11266 if (mouse_used)
11267 selected -= lines_left;
11268
11269 rettv->vval.v_number = selected;
11270}
11271
11272
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11274
11275/*
11276 * "inputrestore()" function
11277 */
11278/*ARGSUSED*/
11279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011281 typval_T *argvars;
11282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283{
11284 if (ga_userinput.ga_len > 0)
11285 {
11286 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11288 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011289 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 }
11291 else if (p_verbose > 1)
11292 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011293 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 }
11296}
11297
11298/*
11299 * "inputsave()" function
11300 */
11301/*ARGSUSED*/
11302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011303f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011304 typval_T *argvars;
11305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306{
11307 /* Add an entry to the stack of typehead storage. */
11308 if (ga_grow(&ga_userinput, 1) == OK)
11309 {
11310 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11311 + ga_userinput.ga_len);
11312 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011313 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314 }
11315 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317}
11318
11319/*
11320 * "inputsecret()" function
11321 */
11322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011324 typval_T *argvars;
11325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326{
11327 ++cmdline_star;
11328 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011329 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 --cmdline_star;
11331 --inputsecret_flag;
11332}
11333
11334/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011335 * "insert()" function
11336 */
11337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011339 typval_T *argvars;
11340 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011341{
11342 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011343 listitem_T *item;
11344 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011345 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011346
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011347 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011348 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011349 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011350 else if ((l = argvars[0].vval.v_list) != NULL
11351 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011352 {
11353 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011354 before = get_tv_number_chk(&argvars[2], &error);
11355 if (error)
11356 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011357
Bram Moolenaar758711c2005-02-02 23:11:38 +000011358 if (before == l->lv_len)
11359 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011360 else
11361 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011362 item = list_find(l, before);
11363 if (item == NULL)
11364 {
11365 EMSGN(_(e_listidx), before);
11366 l = NULL;
11367 }
11368 }
11369 if (l != NULL)
11370 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011371 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011372 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011373 }
11374 }
11375}
11376
11377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 * "isdirectory()" function
11379 */
11380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011382 typval_T *argvars;
11383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386}
11387
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011388/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011389 * "islocked()" function
11390 */
11391 static void
11392f_islocked(argvars, rettv)
11393 typval_T *argvars;
11394 typval_T *rettv;
11395{
11396 lval_T lv;
11397 char_u *end;
11398 dictitem_T *di;
11399
11400 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011401 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11402 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011403 if (end != NULL && lv.ll_name != NULL)
11404 {
11405 if (*end != NUL)
11406 EMSG(_(e_trailing));
11407 else
11408 {
11409 if (lv.ll_tv == NULL)
11410 {
11411 if (check_changedtick(lv.ll_name))
11412 rettv->vval.v_number = 1; /* always locked */
11413 else
11414 {
11415 di = find_var(lv.ll_name, NULL);
11416 if (di != NULL)
11417 {
11418 /* Consider a variable locked when:
11419 * 1. the variable itself is locked
11420 * 2. the value of the variable is locked.
11421 * 3. the List or Dict value is locked.
11422 */
11423 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11424 || tv_islocked(&di->di_tv));
11425 }
11426 }
11427 }
11428 else if (lv.ll_range)
11429 EMSG(_("E745: Range not allowed"));
11430 else if (lv.ll_newkey != NULL)
11431 EMSG2(_(e_dictkey), lv.ll_newkey);
11432 else if (lv.ll_list != NULL)
11433 /* List item. */
11434 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11435 else
11436 /* Dictionary item. */
11437 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11438 }
11439 }
11440
11441 clear_lval(&lv);
11442}
11443
Bram Moolenaar33570922005-01-25 22:26:29 +000011444static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011445
11446/*
11447 * Turn a dict into a list:
11448 * "what" == 0: list of keys
11449 * "what" == 1: list of values
11450 * "what" == 2: list of items
11451 */
11452 static void
11453dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011454 typval_T *argvars;
11455 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011456 int what;
11457{
Bram Moolenaar33570922005-01-25 22:26:29 +000011458 list_T *l2;
11459 dictitem_T *di;
11460 hashitem_T *hi;
11461 listitem_T *li;
11462 listitem_T *li2;
11463 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011464 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011465
11466 rettv->vval.v_number = 0;
11467 if (argvars[0].v_type != VAR_DICT)
11468 {
11469 EMSG(_(e_dictreq));
11470 return;
11471 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011472 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011473 return;
11474
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011475 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011476 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011477
Bram Moolenaar33570922005-01-25 22:26:29 +000011478 todo = d->dv_hashtab.ht_used;
11479 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011480 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011481 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011482 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011483 --todo;
11484 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011485
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011486 li = listitem_alloc();
11487 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011488 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011489 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011490
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011491 if (what == 0)
11492 {
11493 /* keys() */
11494 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011495 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011496 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11497 }
11498 else if (what == 1)
11499 {
11500 /* values() */
11501 copy_tv(&di->di_tv, &li->li_tv);
11502 }
11503 else
11504 {
11505 /* items() */
11506 l2 = list_alloc();
11507 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011508 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011509 li->li_tv.vval.v_list = l2;
11510 if (l2 == NULL)
11511 break;
11512 ++l2->lv_refcount;
11513
11514 li2 = listitem_alloc();
11515 if (li2 == NULL)
11516 break;
11517 list_append(l2, li2);
11518 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011519 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011520 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11521
11522 li2 = listitem_alloc();
11523 if (li2 == NULL)
11524 break;
11525 list_append(l2, li2);
11526 copy_tv(&di->di_tv, &li2->li_tv);
11527 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011528 }
11529 }
11530}
11531
11532/*
11533 * "items(dict)" function
11534 */
11535 static void
11536f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011537 typval_T *argvars;
11538 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011539{
11540 dict_list(argvars, rettv, 2);
11541}
11542
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011544 * "join()" function
11545 */
11546 static void
11547f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011548 typval_T *argvars;
11549 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011550{
11551 garray_T ga;
11552 char_u *sep;
11553
11554 rettv->vval.v_number = 0;
11555 if (argvars[0].v_type != VAR_LIST)
11556 {
11557 EMSG(_(e_listreq));
11558 return;
11559 }
11560 if (argvars[0].vval.v_list == NULL)
11561 return;
11562 if (argvars[1].v_type == VAR_UNKNOWN)
11563 sep = (char_u *)" ";
11564 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011565 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011566
11567 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011568
11569 if (sep != NULL)
11570 {
11571 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011572 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011573 ga_append(&ga, NUL);
11574 rettv->vval.v_string = (char_u *)ga.ga_data;
11575 }
11576 else
11577 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011578}
11579
11580/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011581 * "keys()" function
11582 */
11583 static void
11584f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011585 typval_T *argvars;
11586 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011587{
11588 dict_list(argvars, rettv, 0);
11589}
11590
11591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 * "last_buffer_nr()" function.
11593 */
11594/*ARGSUSED*/
11595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011597 typval_T *argvars;
11598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599{
11600 int n = 0;
11601 buf_T *buf;
11602
11603 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11604 if (n < buf->b_fnum)
11605 n = buf->b_fnum;
11606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011607 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011608}
11609
11610/*
11611 * "len()" function
11612 */
11613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011615 typval_T *argvars;
11616 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011617{
11618 switch (argvars[0].v_type)
11619 {
11620 case VAR_STRING:
11621 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 rettv->vval.v_number = (varnumber_T)STRLEN(
11623 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011624 break;
11625 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011626 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011627 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011628 case VAR_DICT:
11629 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11630 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011631 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011632 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011633 break;
11634 }
11635}
11636
Bram Moolenaar33570922005-01-25 22:26:29 +000011637static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011638
11639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011640libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011641 typval_T *argvars;
11642 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011643 int type;
11644{
11645#ifdef FEAT_LIBCALL
11646 char_u *string_in;
11647 char_u **string_result;
11648 int nr_result;
11649#endif
11650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011652 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011653 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011654 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011655 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011656
11657 if (check_restricted() || check_secure())
11658 return;
11659
11660#ifdef FEAT_LIBCALL
11661 /* The first two args must be strings, otherwise its meaningless */
11662 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11663 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011664 string_in = NULL;
11665 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011666 string_in = argvars[2].vval.v_string;
11667 if (type == VAR_NUMBER)
11668 string_result = NULL;
11669 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011671 if (mch_libcall(argvars[0].vval.v_string,
11672 argvars[1].vval.v_string,
11673 string_in,
11674 argvars[2].vval.v_number,
11675 string_result,
11676 &nr_result) == OK
11677 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011678 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011679 }
11680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681}
11682
11683/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011684 * "libcall()" function
11685 */
11686 static void
11687f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011688 typval_T *argvars;
11689 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011690{
11691 libcall_common(argvars, rettv, VAR_STRING);
11692}
11693
11694/*
11695 * "libcallnr()" function
11696 */
11697 static void
11698f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011699 typval_T *argvars;
11700 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011701{
11702 libcall_common(argvars, rettv, VAR_NUMBER);
11703}
11704
11705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 * "line(string)" function
11707 */
11708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011710 typval_T *argvars;
11711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712{
11713 linenr_T lnum = 0;
11714 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011715 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011717 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 if (fp != NULL)
11719 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011720 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721}
11722
11723/*
11724 * "line2byte(lnum)" function
11725 */
11726/*ARGSUSED*/
11727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011728f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011729 typval_T *argvars;
11730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731{
11732#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011733 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734#else
11735 linenr_T lnum;
11736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011739 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011741 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11742 if (rettv->vval.v_number >= 0)
11743 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744#endif
11745}
11746
11747/*
11748 * "lispindent(lnum)" function
11749 */
11750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011751f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011752 typval_T *argvars;
11753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754{
11755#ifdef FEAT_LISP
11756 pos_T pos;
11757 linenr_T lnum;
11758
11759 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011760 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11762 {
11763 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011764 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 curwin->w_cursor = pos;
11766 }
11767 else
11768#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770}
11771
11772/*
11773 * "localtime()" function
11774 */
11775/*ARGSUSED*/
11776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011777f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011778 typval_T *argvars;
11779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011781 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782}
11783
Bram Moolenaar33570922005-01-25 22:26:29 +000011784static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785
11786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011787get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011788 typval_T *argvars;
11789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 int exact;
11791{
11792 char_u *keys;
11793 char_u *which;
11794 char_u buf[NUMBUFLEN];
11795 char_u *keys_buf = NULL;
11796 char_u *rhs;
11797 int mode;
11798 garray_T ga;
11799
11800 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011801 rettv->v_type = VAR_STRING;
11802 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011804 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 if (*keys == NUL)
11806 return;
11807
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011808 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011809 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 else
11811 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812 if (which == NULL)
11813 return;
11814
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 mode = get_map_mode(&which, 0);
11816
11817 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011818 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819 vim_free(keys_buf);
11820 if (rhs != NULL)
11821 {
11822 ga_init(&ga);
11823 ga.ga_itemsize = 1;
11824 ga.ga_growsize = 40;
11825
11826 while (*rhs != NUL)
11827 ga_concat(&ga, str2special(&rhs, FALSE));
11828
11829 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011830 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831 }
11832}
11833
11834/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011835 * "map()" function
11836 */
11837 static void
11838f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011839 typval_T *argvars;
11840 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011841{
11842 filter_map(argvars, rettv, TRUE);
11843}
11844
11845/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011846 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 */
11848 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011849f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011850 typval_T *argvars;
11851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011853 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854}
11855
11856/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011857 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 */
11859 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011860f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011861 typval_T *argvars;
11862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011864 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865}
11866
Bram Moolenaar33570922005-01-25 22:26:29 +000011867static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868
11869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011870find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011871 typval_T *argvars;
11872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 int type;
11874{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011875 char_u *str = NULL;
11876 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 char_u *pat;
11878 regmatch_T regmatch;
11879 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011880 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881 char_u *save_cpo;
11882 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011883 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011884 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011885 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011886 list_T *l = NULL;
11887 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011888 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011889 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890
11891 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11892 save_cpo = p_cpo;
11893 p_cpo = (char_u *)"";
11894
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011895 rettv->vval.v_number = -1;
11896 if (type == 3)
11897 {
11898 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011899 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011900 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011901 }
11902 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904 rettv->v_type = VAR_STRING;
11905 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011908 if (argvars[0].v_type == VAR_LIST)
11909 {
11910 if ((l = argvars[0].vval.v_list) == NULL)
11911 goto theend;
11912 li = l->lv_first;
11913 }
11914 else
11915 expr = str = get_tv_string(&argvars[0]);
11916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011917 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11918 if (pat == NULL)
11919 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011920
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011921 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011923 int error = FALSE;
11924
11925 start = get_tv_number_chk(&argvars[2], &error);
11926 if (error)
11927 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011928 if (l != NULL)
11929 {
11930 li = list_find(l, start);
11931 if (li == NULL)
11932 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011933 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011934 }
11935 else
11936 {
11937 if (start < 0)
11938 start = 0;
11939 if (start > (long)STRLEN(str))
11940 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011941 /* When "count" argument is there ignore matches before "start",
11942 * otherwise skip part of the string. Differs when pattern is "^"
11943 * or "\<". */
11944 if (argvars[3].v_type != VAR_UNKNOWN)
11945 startcol = start;
11946 else
11947 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011948 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011949
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011950 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011951 nth = get_tv_number_chk(&argvars[3], &error);
11952 if (error)
11953 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 }
11955
11956 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11957 if (regmatch.regprog != NULL)
11958 {
11959 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011960
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011961 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011962 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011963 if (l != NULL)
11964 {
11965 if (li == NULL)
11966 {
11967 match = FALSE;
11968 break;
11969 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011970 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011971 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011972 if (str == NULL)
11973 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011974 }
11975
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011976 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011977
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011978 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011979 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011980 if (l == NULL && !match)
11981 break;
11982
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011983 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011984 if (l != NULL)
11985 {
11986 li = li->li_next;
11987 ++idx;
11988 }
11989 else
11990 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011991#ifdef FEAT_MBYTE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011992 startcol = regmatch.startp[0]
11993 + (*mb_ptr2len)(regmatch.startp[0]) - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011994#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011995 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011996#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011997 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011998 }
11999
12000 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012002 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012003 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012004 int i;
12005
12006 /* return list with matched string and submatches */
12007 for (i = 0; i < NSUBEXP; ++i)
12008 {
12009 if (regmatch.endp[i] == NULL)
12010 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012011 if (list_append_string(rettv->vval.v_list,
12012 regmatch.startp[i],
12013 (int)(regmatch.endp[i] - regmatch.startp[i]))
12014 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012015 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012016 }
12017 }
12018 else if (type == 2)
12019 {
12020 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012021 if (l != NULL)
12022 copy_tv(&li->li_tv, rettv);
12023 else
12024 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012026 }
12027 else if (l != NULL)
12028 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 else
12030 {
12031 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012032 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 (varnumber_T)(regmatch.startp[0] - str);
12034 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012035 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 }
12039 }
12040 vim_free(regmatch.regprog);
12041 }
12042
12043theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012044 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 p_cpo = save_cpo;
12046}
12047
12048/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012049 * "match()" function
12050 */
12051 static void
12052f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012053 typval_T *argvars;
12054 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012055{
12056 find_some_match(argvars, rettv, 1);
12057}
12058
12059/*
12060 * "matchend()" function
12061 */
12062 static void
12063f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012064 typval_T *argvars;
12065 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012066{
12067 find_some_match(argvars, rettv, 0);
12068}
12069
12070/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012071 * "matchlist()" function
12072 */
12073 static void
12074f_matchlist(argvars, rettv)
12075 typval_T *argvars;
12076 typval_T *rettv;
12077{
12078 find_some_match(argvars, rettv, 3);
12079}
12080
12081/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012082 * "matchstr()" function
12083 */
12084 static void
12085f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012086 typval_T *argvars;
12087 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012088{
12089 find_some_match(argvars, rettv, 2);
12090}
12091
Bram Moolenaar33570922005-01-25 22:26:29 +000012092static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012093
12094 static void
12095max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012096 typval_T *argvars;
12097 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012098 int domax;
12099{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012100 long n = 0;
12101 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012102 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012103
12104 if (argvars[0].v_type == VAR_LIST)
12105 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012106 list_T *l;
12107 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012108
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012109 l = argvars[0].vval.v_list;
12110 if (l != NULL)
12111 {
12112 li = l->lv_first;
12113 if (li != NULL)
12114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012115 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012116 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012117 {
12118 li = li->li_next;
12119 if (li == NULL)
12120 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012121 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012122 if (domax ? i > n : i < n)
12123 n = i;
12124 }
12125 }
12126 }
12127 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012128 else if (argvars[0].v_type == VAR_DICT)
12129 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012130 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012131 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012132 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012133 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012134
12135 d = argvars[0].vval.v_dict;
12136 if (d != NULL)
12137 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012138 todo = d->dv_hashtab.ht_used;
12139 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012140 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012141 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012142 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012143 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012144 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012145 if (first)
12146 {
12147 n = i;
12148 first = FALSE;
12149 }
12150 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012151 n = i;
12152 }
12153 }
12154 }
12155 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012156 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012157 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012158 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012159}
12160
12161/*
12162 * "max()" function
12163 */
12164 static void
12165f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012166 typval_T *argvars;
12167 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012168{
12169 max_min(argvars, rettv, TRUE);
12170}
12171
12172/*
12173 * "min()" function
12174 */
12175 static void
12176f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012177 typval_T *argvars;
12178 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012179{
12180 max_min(argvars, rettv, FALSE);
12181}
12182
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012183static int mkdir_recurse __ARGS((char_u *dir, int prot));
12184
12185/*
12186 * Create the directory in which "dir" is located, and higher levels when
12187 * needed.
12188 */
12189 static int
12190mkdir_recurse(dir, prot)
12191 char_u *dir;
12192 int prot;
12193{
12194 char_u *p;
12195 char_u *updir;
12196 int r = FAIL;
12197
12198 /* Get end of directory name in "dir".
12199 * We're done when it's "/" or "c:/". */
12200 p = gettail_sep(dir);
12201 if (p <= get_past_head(dir))
12202 return OK;
12203
12204 /* If the directory exists we're done. Otherwise: create it.*/
12205 updir = vim_strnsave(dir, (int)(p - dir));
12206 if (updir == NULL)
12207 return FAIL;
12208 if (mch_isdir(updir))
12209 r = OK;
12210 else if (mkdir_recurse(updir, prot) == OK)
12211 r = vim_mkdir_emsg(updir, prot);
12212 vim_free(updir);
12213 return r;
12214}
12215
12216#ifdef vim_mkdir
12217/*
12218 * "mkdir()" function
12219 */
12220 static void
12221f_mkdir(argvars, rettv)
12222 typval_T *argvars;
12223 typval_T *rettv;
12224{
12225 char_u *dir;
12226 char_u buf[NUMBUFLEN];
12227 int prot = 0755;
12228
12229 rettv->vval.v_number = FAIL;
12230 if (check_restricted() || check_secure())
12231 return;
12232
12233 dir = get_tv_string_buf(&argvars[0], buf);
12234 if (argvars[1].v_type != VAR_UNKNOWN)
12235 {
12236 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012237 prot = get_tv_number_chk(&argvars[2], NULL);
12238 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012239 mkdir_recurse(dir, prot);
12240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012241 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012242}
12243#endif
12244
Bram Moolenaar0d660222005-01-07 21:51:51 +000012245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246 * "mode()" function
12247 */
12248/*ARGSUSED*/
12249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012250f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012251 typval_T *argvars;
12252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253{
12254 char_u buf[2];
12255
12256#ifdef FEAT_VISUAL
12257 if (VIsual_active)
12258 {
12259 if (VIsual_select)
12260 buf[0] = VIsual_mode + 's' - 'v';
12261 else
12262 buf[0] = VIsual_mode;
12263 }
12264 else
12265#endif
12266 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12267 buf[0] = 'r';
12268 else if (State & INSERT)
12269 {
12270 if (State & REPLACE_FLAG)
12271 buf[0] = 'R';
12272 else
12273 buf[0] = 'i';
12274 }
12275 else if (State & CMDLINE)
12276 buf[0] = 'c';
12277 else
12278 buf[0] = 'n';
12279
12280 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012281 rettv->vval.v_string = vim_strsave(buf);
12282 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283}
12284
12285/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012286 * "nextnonblank()" function
12287 */
12288 static void
12289f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012290 typval_T *argvars;
12291 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012292{
12293 linenr_T lnum;
12294
12295 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012297 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012298 {
12299 lnum = 0;
12300 break;
12301 }
12302 if (*skipwhite(ml_get(lnum)) != NUL)
12303 break;
12304 }
12305 rettv->vval.v_number = lnum;
12306}
12307
12308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309 * "nr2char()" function
12310 */
12311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012312f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012313 typval_T *argvars;
12314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315{
12316 char_u buf[NUMBUFLEN];
12317
12318#ifdef FEAT_MBYTE
12319 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012320 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321 else
12322#endif
12323 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325 buf[1] = NUL;
12326 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012327 rettv->v_type = VAR_STRING;
12328 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012329}
12330
12331/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012332 * "prevnonblank()" function
12333 */
12334 static void
12335f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012336 typval_T *argvars;
12337 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012338{
12339 linenr_T lnum;
12340
12341 lnum = get_tv_lnum(argvars);
12342 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12343 lnum = 0;
12344 else
12345 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12346 --lnum;
12347 rettv->vval.v_number = lnum;
12348}
12349
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012350#ifdef HAVE_STDARG_H
12351/* This dummy va_list is here because:
12352 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12353 * - locally in the function results in a "used before set" warning
12354 * - using va_start() to initialize it gives "function with fixed args" error */
12355static va_list ap;
12356#endif
12357
Bram Moolenaar8c711452005-01-14 21:53:12 +000012358/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012359 * "printf()" function
12360 */
12361 static void
12362f_printf(argvars, rettv)
12363 typval_T *argvars;
12364 typval_T *rettv;
12365{
12366 rettv->v_type = VAR_STRING;
12367 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012368#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012369 {
12370 char_u buf[NUMBUFLEN];
12371 int len;
12372 char_u *s;
12373 int saved_did_emsg = did_emsg;
12374 char *fmt;
12375
12376 /* Get the required length, allocate the buffer and do it for real. */
12377 did_emsg = FALSE;
12378 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012379 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012380 if (!did_emsg)
12381 {
12382 s = alloc(len + 1);
12383 if (s != NULL)
12384 {
12385 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012386 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012387 }
12388 }
12389 did_emsg |= saved_did_emsg;
12390 }
12391#endif
12392}
12393
12394/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012395 * "pumvisible()" function
12396 */
12397/*ARGSUSED*/
12398 static void
12399f_pumvisible(argvars, rettv)
12400 typval_T *argvars;
12401 typval_T *rettv;
12402{
12403 rettv->vval.v_number = 0;
12404#ifdef FEAT_INS_EXPAND
12405 if (pum_visible())
12406 rettv->vval.v_number = 1;
12407#endif
12408}
12409
12410/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012411 * "range()" function
12412 */
12413 static void
12414f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012415 typval_T *argvars;
12416 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012417{
12418 long start;
12419 long end;
12420 long stride = 1;
12421 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012422 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012424 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012425 if (argvars[1].v_type == VAR_UNKNOWN)
12426 {
12427 end = start - 1;
12428 start = 0;
12429 }
12430 else
12431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012432 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012433 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012434 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012435 }
12436
12437 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012438 if (error)
12439 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012440 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012441 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012442 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012443 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012444 else
12445 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012446 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012447 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012448 if (list_append_number(rettv->vval.v_list,
12449 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012450 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012451 }
12452}
12453
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012454/*
12455 * "readfile()" function
12456 */
12457 static void
12458f_readfile(argvars, rettv)
12459 typval_T *argvars;
12460 typval_T *rettv;
12461{
12462 int binary = FALSE;
12463 char_u *fname;
12464 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012465 listitem_T *li;
12466#define FREAD_SIZE 200 /* optimized for text lines */
12467 char_u buf[FREAD_SIZE];
12468 int readlen; /* size of last fread() */
12469 int buflen; /* nr of valid chars in buf[] */
12470 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12471 int tolist; /* first byte in buf[] still to be put in list */
12472 int chop; /* how many CR to chop off */
12473 char_u *prev = NULL; /* previously read bytes, if any */
12474 int prevlen = 0; /* length of "prev" if not NULL */
12475 char_u *s;
12476 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012477 long maxline = MAXLNUM;
12478 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012479
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012480 if (argvars[1].v_type != VAR_UNKNOWN)
12481 {
12482 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12483 binary = TRUE;
12484 if (argvars[2].v_type != VAR_UNKNOWN)
12485 maxline = get_tv_number(&argvars[2]);
12486 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012487
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012488 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012489 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012490
12491 /* Always open the file in binary mode, library functions have a mind of
12492 * their own about CR-LF conversion. */
12493 fname = get_tv_string(&argvars[0]);
12494 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12495 {
12496 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12497 return;
12498 }
12499
12500 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012501 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012502 {
12503 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12504 buflen = filtd + readlen;
12505 tolist = 0;
12506 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12507 {
12508 if (buf[filtd] == '\n' || readlen <= 0)
12509 {
12510 /* Only when in binary mode add an empty list item when the
12511 * last line ends in a '\n'. */
12512 if (!binary && readlen == 0 && filtd == 0)
12513 break;
12514
12515 /* Found end-of-line or end-of-file: add a text line to the
12516 * list. */
12517 chop = 0;
12518 if (!binary)
12519 while (filtd - chop - 1 >= tolist
12520 && buf[filtd - chop - 1] == '\r')
12521 ++chop;
12522 len = filtd - tolist - chop;
12523 if (prev == NULL)
12524 s = vim_strnsave(buf + tolist, len);
12525 else
12526 {
12527 s = alloc((unsigned)(prevlen + len + 1));
12528 if (s != NULL)
12529 {
12530 mch_memmove(s, prev, prevlen);
12531 vim_free(prev);
12532 prev = NULL;
12533 mch_memmove(s + prevlen, buf + tolist, len);
12534 s[prevlen + len] = NUL;
12535 }
12536 }
12537 tolist = filtd + 1;
12538
12539 li = listitem_alloc();
12540 if (li == NULL)
12541 {
12542 vim_free(s);
12543 break;
12544 }
12545 li->li_tv.v_type = VAR_STRING;
12546 li->li_tv.v_lock = 0;
12547 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012548 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012549
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012550 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012551 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012552 if (readlen <= 0)
12553 break;
12554 }
12555 else if (buf[filtd] == NUL)
12556 buf[filtd] = '\n';
12557 }
12558 if (readlen <= 0)
12559 break;
12560
12561 if (tolist == 0)
12562 {
12563 /* "buf" is full, need to move text to an allocated buffer */
12564 if (prev == NULL)
12565 {
12566 prev = vim_strnsave(buf, buflen);
12567 prevlen = buflen;
12568 }
12569 else
12570 {
12571 s = alloc((unsigned)(prevlen + buflen));
12572 if (s != NULL)
12573 {
12574 mch_memmove(s, prev, prevlen);
12575 mch_memmove(s + prevlen, buf, buflen);
12576 vim_free(prev);
12577 prev = s;
12578 prevlen += buflen;
12579 }
12580 }
12581 filtd = 0;
12582 }
12583 else
12584 {
12585 mch_memmove(buf, buf + tolist, buflen - tolist);
12586 filtd -= tolist;
12587 }
12588 }
12589
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012590 /*
12591 * For a negative line count use only the lines at the end of the file,
12592 * free the rest.
12593 */
12594 if (maxline < 0)
12595 while (cnt > -maxline)
12596 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012597 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012598 --cnt;
12599 }
12600
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012601 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012602 fclose(fd);
12603}
12604
12605
Bram Moolenaar0d660222005-01-07 21:51:51 +000012606#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12607static void make_connection __ARGS((void));
12608static int check_connection __ARGS((void));
12609
12610 static void
12611make_connection()
12612{
12613 if (X_DISPLAY == NULL
12614# ifdef FEAT_GUI
12615 && !gui.in_use
12616# endif
12617 )
12618 {
12619 x_force_connect = TRUE;
12620 setup_term_clip();
12621 x_force_connect = FALSE;
12622 }
12623}
12624
12625 static int
12626check_connection()
12627{
12628 make_connection();
12629 if (X_DISPLAY == NULL)
12630 {
12631 EMSG(_("E240: No connection to Vim server"));
12632 return FAIL;
12633 }
12634 return OK;
12635}
12636#endif
12637
12638#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012639static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012640
12641 static void
12642remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012643 typval_T *argvars;
12644 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012645 int expr;
12646{
12647 char_u *server_name;
12648 char_u *keys;
12649 char_u *r = NULL;
12650 char_u buf[NUMBUFLEN];
12651# ifdef WIN32
12652 HWND w;
12653# else
12654 Window w;
12655# endif
12656
12657 if (check_restricted() || check_secure())
12658 return;
12659
12660# ifdef FEAT_X11
12661 if (check_connection() == FAIL)
12662 return;
12663# endif
12664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012665 server_name = get_tv_string_chk(&argvars[0]);
12666 if (server_name == NULL)
12667 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012668 keys = get_tv_string_buf(&argvars[1], buf);
12669# ifdef WIN32
12670 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12671# else
12672 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12673 < 0)
12674# endif
12675 {
12676 if (r != NULL)
12677 EMSG(r); /* sending worked but evaluation failed */
12678 else
12679 EMSG2(_("E241: Unable to send to %s"), server_name);
12680 return;
12681 }
12682
12683 rettv->vval.v_string = r;
12684
12685 if (argvars[2].v_type != VAR_UNKNOWN)
12686 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012687 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012688 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012690
12691 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012692 v.di_tv.v_type = VAR_STRING;
12693 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012694 idvar = get_tv_string_chk(&argvars[2]);
12695 if (idvar != NULL)
12696 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012697 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012698 }
12699}
12700#endif
12701
12702/*
12703 * "remote_expr()" function
12704 */
12705/*ARGSUSED*/
12706 static void
12707f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012708 typval_T *argvars;
12709 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012710{
12711 rettv->v_type = VAR_STRING;
12712 rettv->vval.v_string = NULL;
12713#ifdef FEAT_CLIENTSERVER
12714 remote_common(argvars, rettv, TRUE);
12715#endif
12716}
12717
12718/*
12719 * "remote_foreground()" function
12720 */
12721/*ARGSUSED*/
12722 static void
12723f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012724 typval_T *argvars;
12725 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012726{
12727 rettv->vval.v_number = 0;
12728#ifdef FEAT_CLIENTSERVER
12729# ifdef WIN32
12730 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012731 {
12732 char_u *server_name = get_tv_string_chk(&argvars[0]);
12733
12734 if (server_name != NULL)
12735 serverForeground(server_name);
12736 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012737# else
12738 /* Send a foreground() expression to the server. */
12739 argvars[1].v_type = VAR_STRING;
12740 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12741 argvars[2].v_type = VAR_UNKNOWN;
12742 remote_common(argvars, rettv, TRUE);
12743 vim_free(argvars[1].vval.v_string);
12744# endif
12745#endif
12746}
12747
12748/*ARGSUSED*/
12749 static void
12750f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012751 typval_T *argvars;
12752 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012753{
12754#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012755 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012756 char_u *s = NULL;
12757# ifdef WIN32
12758 int n = 0;
12759# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012760 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012761
12762 if (check_restricted() || check_secure())
12763 {
12764 rettv->vval.v_number = -1;
12765 return;
12766 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012767 serverid = get_tv_string_chk(&argvars[0]);
12768 if (serverid == NULL)
12769 {
12770 rettv->vval.v_number = -1;
12771 return; /* type error; errmsg already given */
12772 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012773# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012774 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012775 if (n == 0)
12776 rettv->vval.v_number = -1;
12777 else
12778 {
12779 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12780 rettv->vval.v_number = (s != NULL);
12781 }
12782# else
12783 rettv->vval.v_number = 0;
12784 if (check_connection() == FAIL)
12785 return;
12786
12787 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012788 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012789# endif
12790
12791 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012793 char_u *retvar;
12794
Bram Moolenaar33570922005-01-25 22:26:29 +000012795 v.di_tv.v_type = VAR_STRING;
12796 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012797 retvar = get_tv_string_chk(&argvars[1]);
12798 if (retvar != NULL)
12799 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012800 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012801 }
12802#else
12803 rettv->vval.v_number = -1;
12804#endif
12805}
12806
12807/*ARGSUSED*/
12808 static void
12809f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012810 typval_T *argvars;
12811 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012812{
12813 char_u *r = NULL;
12814
12815#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012816 char_u *serverid = get_tv_string_chk(&argvars[0]);
12817
12818 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012819 {
12820# ifdef WIN32
12821 /* The server's HWND is encoded in the 'id' parameter */
12822 int n = 0;
12823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012824 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012825 if (n != 0)
12826 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12827 if (r == NULL)
12828# else
12829 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012830 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012831# endif
12832 EMSG(_("E277: Unable to read a server reply"));
12833 }
12834#endif
12835 rettv->v_type = VAR_STRING;
12836 rettv->vval.v_string = r;
12837}
12838
12839/*
12840 * "remote_send()" function
12841 */
12842/*ARGSUSED*/
12843 static void
12844f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012845 typval_T *argvars;
12846 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012847{
12848 rettv->v_type = VAR_STRING;
12849 rettv->vval.v_string = NULL;
12850#ifdef FEAT_CLIENTSERVER
12851 remote_common(argvars, rettv, FALSE);
12852#endif
12853}
12854
12855/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012856 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012857 */
12858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012860 typval_T *argvars;
12861 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012862{
Bram Moolenaar33570922005-01-25 22:26:29 +000012863 list_T *l;
12864 listitem_T *item, *item2;
12865 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012866 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012867 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012868 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012869 dict_T *d;
12870 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012871
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012872 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012873 if (argvars[0].v_type == VAR_DICT)
12874 {
12875 if (argvars[2].v_type != VAR_UNKNOWN)
12876 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012877 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012878 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012879 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012880 key = get_tv_string_chk(&argvars[1]);
12881 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012883 di = dict_find(d, key, -1);
12884 if (di == NULL)
12885 EMSG2(_(e_dictkey), key);
12886 else
12887 {
12888 *rettv = di->di_tv;
12889 init_tv(&di->di_tv);
12890 dictitem_remove(d, di);
12891 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012892 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012893 }
12894 }
12895 else if (argvars[0].v_type != VAR_LIST)
12896 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012897 else if ((l = argvars[0].vval.v_list) != NULL
12898 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012899 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012900 int error = FALSE;
12901
12902 idx = get_tv_number_chk(&argvars[1], &error);
12903 if (error)
12904 ; /* type error: do nothing, errmsg already given */
12905 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012906 EMSGN(_(e_listidx), idx);
12907 else
12908 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012909 if (argvars[2].v_type == VAR_UNKNOWN)
12910 {
12911 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012912 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012913 *rettv = item->li_tv;
12914 vim_free(item);
12915 }
12916 else
12917 {
12918 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012919 end = get_tv_number_chk(&argvars[2], &error);
12920 if (error)
12921 ; /* type error: do nothing */
12922 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012923 EMSGN(_(e_listidx), end);
12924 else
12925 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012926 int cnt = 0;
12927
12928 for (li = item; li != NULL; li = li->li_next)
12929 {
12930 ++cnt;
12931 if (li == item2)
12932 break;
12933 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012934 if (li == NULL) /* didn't find "item2" after "item" */
12935 EMSG(_(e_invrange));
12936 else
12937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012938 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012939 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012940 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012941 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012942 l->lv_first = item;
12943 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012944 item->li_prev = NULL;
12945 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012946 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012947 }
12948 }
12949 }
12950 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012951 }
12952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953}
12954
12955/*
12956 * "rename({from}, {to})" function
12957 */
12958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012959f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012960 typval_T *argvars;
12961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962{
12963 char_u buf[NUMBUFLEN];
12964
12965 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012966 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012968 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12969 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970}
12971
12972/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012973 * "repeat()" function
12974 */
12975/*ARGSUSED*/
12976 static void
12977f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012978 typval_T *argvars;
12979 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012980{
12981 char_u *p;
12982 int n;
12983 int slen;
12984 int len;
12985 char_u *r;
12986 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012987
12988 n = get_tv_number(&argvars[1]);
12989 if (argvars[0].v_type == VAR_LIST)
12990 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012991 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012992 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012993 if (list_extend(rettv->vval.v_list,
12994 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012995 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012996 }
12997 else
12998 {
12999 p = get_tv_string(&argvars[0]);
13000 rettv->v_type = VAR_STRING;
13001 rettv->vval.v_string = NULL;
13002
13003 slen = (int)STRLEN(p);
13004 len = slen * n;
13005 if (len <= 0)
13006 return;
13007
13008 r = alloc(len + 1);
13009 if (r != NULL)
13010 {
13011 for (i = 0; i < n; i++)
13012 mch_memmove(r + i * slen, p, (size_t)slen);
13013 r[len] = NUL;
13014 }
13015
13016 rettv->vval.v_string = r;
13017 }
13018}
13019
13020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021 * "resolve()" function
13022 */
13023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013024f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013025 typval_T *argvars;
13026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027{
13028 char_u *p;
13029
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013030 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031#ifdef FEAT_SHORTCUT
13032 {
13033 char_u *v = NULL;
13034
13035 v = mch_resolve_shortcut(p);
13036 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013037 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013039 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040 }
13041#else
13042# ifdef HAVE_READLINK
13043 {
13044 char_u buf[MAXPATHL + 1];
13045 char_u *cpy;
13046 int len;
13047 char_u *remain = NULL;
13048 char_u *q;
13049 int is_relative_to_current = FALSE;
13050 int has_trailing_pathsep = FALSE;
13051 int limit = 100;
13052
13053 p = vim_strsave(p);
13054
13055 if (p[0] == '.' && (vim_ispathsep(p[1])
13056 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13057 is_relative_to_current = TRUE;
13058
13059 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013060 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061 has_trailing_pathsep = TRUE;
13062
13063 q = getnextcomp(p);
13064 if (*q != NUL)
13065 {
13066 /* Separate the first path component in "p", and keep the
13067 * remainder (beginning with the path separator). */
13068 remain = vim_strsave(q - 1);
13069 q[-1] = NUL;
13070 }
13071
13072 for (;;)
13073 {
13074 for (;;)
13075 {
13076 len = readlink((char *)p, (char *)buf, MAXPATHL);
13077 if (len <= 0)
13078 break;
13079 buf[len] = NUL;
13080
13081 if (limit-- == 0)
13082 {
13083 vim_free(p);
13084 vim_free(remain);
13085 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013086 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087 goto fail;
13088 }
13089
13090 /* Ensure that the result will have a trailing path separator
13091 * if the argument has one. */
13092 if (remain == NULL && has_trailing_pathsep)
13093 add_pathsep(buf);
13094
13095 /* Separate the first path component in the link value and
13096 * concatenate the remainders. */
13097 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13098 if (*q != NUL)
13099 {
13100 if (remain == NULL)
13101 remain = vim_strsave(q - 1);
13102 else
13103 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013104 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105 if (cpy != NULL)
13106 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 vim_free(remain);
13108 remain = cpy;
13109 }
13110 }
13111 q[-1] = NUL;
13112 }
13113
13114 q = gettail(p);
13115 if (q > p && *q == NUL)
13116 {
13117 /* Ignore trailing path separator. */
13118 q[-1] = NUL;
13119 q = gettail(p);
13120 }
13121 if (q > p && !mch_isFullName(buf))
13122 {
13123 /* symlink is relative to directory of argument */
13124 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13125 if (cpy != NULL)
13126 {
13127 STRCPY(cpy, p);
13128 STRCPY(gettail(cpy), buf);
13129 vim_free(p);
13130 p = cpy;
13131 }
13132 }
13133 else
13134 {
13135 vim_free(p);
13136 p = vim_strsave(buf);
13137 }
13138 }
13139
13140 if (remain == NULL)
13141 break;
13142
13143 /* Append the first path component of "remain" to "p". */
13144 q = getnextcomp(remain + 1);
13145 len = q - remain - (*q != NUL);
13146 cpy = vim_strnsave(p, STRLEN(p) + len);
13147 if (cpy != NULL)
13148 {
13149 STRNCAT(cpy, remain, len);
13150 vim_free(p);
13151 p = cpy;
13152 }
13153 /* Shorten "remain". */
13154 if (*q != NUL)
13155 STRCPY(remain, q - 1);
13156 else
13157 {
13158 vim_free(remain);
13159 remain = NULL;
13160 }
13161 }
13162
13163 /* If the result is a relative path name, make it explicitly relative to
13164 * the current directory if and only if the argument had this form. */
13165 if (!vim_ispathsep(*p))
13166 {
13167 if (is_relative_to_current
13168 && *p != NUL
13169 && !(p[0] == '.'
13170 && (p[1] == NUL
13171 || vim_ispathsep(p[1])
13172 || (p[1] == '.'
13173 && (p[2] == NUL
13174 || vim_ispathsep(p[2]))))))
13175 {
13176 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013177 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178 if (cpy != NULL)
13179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180 vim_free(p);
13181 p = cpy;
13182 }
13183 }
13184 else if (!is_relative_to_current)
13185 {
13186 /* Strip leading "./". */
13187 q = p;
13188 while (q[0] == '.' && vim_ispathsep(q[1]))
13189 q += 2;
13190 if (q > p)
13191 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13192 }
13193 }
13194
13195 /* Ensure that the result will have no trailing path separator
13196 * if the argument had none. But keep "/" or "//". */
13197 if (!has_trailing_pathsep)
13198 {
13199 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013200 if (after_pathsep(p, q))
13201 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202 }
13203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013204 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 }
13206# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208# endif
13209#endif
13210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212
13213#ifdef HAVE_READLINK
13214fail:
13215#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013216 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217}
13218
13219/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013220 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221 */
13222 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013223f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013224 typval_T *argvars;
13225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226{
Bram Moolenaar33570922005-01-25 22:26:29 +000013227 list_T *l;
13228 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229
Bram Moolenaar0d660222005-01-07 21:51:51 +000013230 rettv->vval.v_number = 0;
13231 if (argvars[0].v_type != VAR_LIST)
13232 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013233 else if ((l = argvars[0].vval.v_list) != NULL
13234 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013235 {
13236 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013237 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013238 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013239 while (li != NULL)
13240 {
13241 ni = li->li_prev;
13242 list_append(l, li);
13243 li = ni;
13244 }
13245 rettv->vval.v_list = l;
13246 rettv->v_type = VAR_LIST;
13247 ++l->lv_refcount;
13248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249}
13250
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013251#define SP_NOMOVE 0x01 /* don't move cursor */
13252#define SP_REPEAT 0x02 /* repeat to find outer pair */
13253#define SP_RETCOUNT 0x04 /* return matchcount */
13254#define SP_SETPCMARK 0x08 /* set previous context mark */
13255#define SP_START 0x10 /* accept match at start position */
13256#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13257#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013258
Bram Moolenaar33570922005-01-25 22:26:29 +000013259static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013260
13261/*
13262 * Get flags for a search function.
13263 * Possibly sets "p_ws".
13264 * Returns BACKWARD, FORWARD or zero (for an error).
13265 */
13266 static int
13267get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013268 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013269 int *flagsp;
13270{
13271 int dir = FORWARD;
13272 char_u *flags;
13273 char_u nbuf[NUMBUFLEN];
13274 int mask;
13275
13276 if (varp->v_type != VAR_UNKNOWN)
13277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013278 flags = get_tv_string_buf_chk(varp, nbuf);
13279 if (flags == NULL)
13280 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013281 while (*flags != NUL)
13282 {
13283 switch (*flags)
13284 {
13285 case 'b': dir = BACKWARD; break;
13286 case 'w': p_ws = TRUE; break;
13287 case 'W': p_ws = FALSE; break;
13288 default: mask = 0;
13289 if (flagsp != NULL)
13290 switch (*flags)
13291 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013292 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013293 case 'e': mask = SP_END; break;
13294 case 'm': mask = SP_RETCOUNT; break;
13295 case 'n': mask = SP_NOMOVE; break;
13296 case 'p': mask = SP_SUBPAT; break;
13297 case 'r': mask = SP_REPEAT; break;
13298 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013299 }
13300 if (mask == 0)
13301 {
13302 EMSG2(_(e_invarg2), flags);
13303 dir = 0;
13304 }
13305 else
13306 *flagsp |= mask;
13307 }
13308 if (dir == 0)
13309 break;
13310 ++flags;
13311 }
13312 }
13313 return dir;
13314}
13315
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013317 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013319 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013320search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013321 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013322 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013323 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013325 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326 char_u *pat;
13327 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013328 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329 int save_p_ws = p_ws;
13330 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013331 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013332 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013333 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013334 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013336 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013337 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013338 if (dir == 0)
13339 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013340 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013341 if (flags & SP_START)
13342 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013343 if (flags & SP_END)
13344 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013345
13346 /* Optional extra argument: line number to stop searching. */
13347 if (argvars[1].v_type != VAR_UNKNOWN
13348 && argvars[2].v_type != VAR_UNKNOWN)
13349 {
13350 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13351 if (lnum_stop < 0)
13352 goto theend;
13353 }
13354
Bram Moolenaar231334e2005-07-25 20:46:57 +000013355 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013356 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013357 * Check to make sure only those flags are set.
13358 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13359 * flags cannot be set. Check for that condition also.
13360 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013361 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013362 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013364 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013365 goto theend;
13366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013368 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013369 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13370 options, RE_SEARCH, (linenr_T)lnum_stop);
13371 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013373 if (flags & SP_SUBPAT)
13374 retval = subpatnum;
13375 else
13376 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013377 if (flags & SP_SETPCMARK)
13378 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013380 if (match_pos != NULL)
13381 {
13382 /* Store the match cursor position */
13383 match_pos->lnum = pos.lnum;
13384 match_pos->col = pos.col + 1;
13385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386 /* "/$" will put the cursor after the end of the line, may need to
13387 * correct that here */
13388 check_cursor();
13389 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013390
13391 /* If 'n' flag is used: restore cursor position. */
13392 if (flags & SP_NOMOVE)
13393 curwin->w_cursor = save_cursor;
13394theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013396
13397 return retval;
13398}
13399
13400/*
13401 * "search()" function
13402 */
13403 static void
13404f_search(argvars, rettv)
13405 typval_T *argvars;
13406 typval_T *rettv;
13407{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013408 int flags = 0;
13409
13410 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411}
13412
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013414 * "searchdecl()" function
13415 */
13416 static void
13417f_searchdecl(argvars, rettv)
13418 typval_T *argvars;
13419 typval_T *rettv;
13420{
13421 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013422 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013423 int error = FALSE;
13424 char_u *name;
13425
13426 rettv->vval.v_number = 1; /* default: FAIL */
13427
13428 name = get_tv_string_chk(&argvars[0]);
13429 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013430 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013431 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013432 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13433 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13434 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013435 if (!error && name != NULL)
13436 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013437 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013438}
13439
13440/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013441 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013443 static int
13444searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013445 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013446 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447{
13448 char_u *spat, *mpat, *epat;
13449 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451 int dir;
13452 int flags = 0;
13453 char_u nbuf1[NUMBUFLEN];
13454 char_u nbuf2[NUMBUFLEN];
13455 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013456 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013457 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013460 spat = get_tv_string_chk(&argvars[0]);
13461 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13462 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13463 if (spat == NULL || mpat == NULL || epat == NULL)
13464 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466 /* Handle the optional fourth argument: flags */
13467 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013468 if (dir == 0)
13469 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013470
13471 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013472 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13473 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013474 if ((flags & (SP_END | SP_SUBPAT)) != 0
13475 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013476 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013477 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013478 goto theend;
13479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013481 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013482 if (argvars[3].v_type == VAR_UNKNOWN
13483 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 skip = (char_u *)"";
13485 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013487 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013488 if (argvars[5].v_type != VAR_UNKNOWN)
13489 {
13490 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13491 if (lnum_stop < 0)
13492 goto theend;
13493 }
13494 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013495 if (skip == NULL)
13496 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013498 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13499 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013500
13501theend:
13502 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013503
13504 return retval;
13505}
13506
13507/*
13508 * "searchpair()" function
13509 */
13510 static void
13511f_searchpair(argvars, rettv)
13512 typval_T *argvars;
13513 typval_T *rettv;
13514{
13515 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13516}
13517
13518/*
13519 * "searchpairpos()" function
13520 */
13521 static void
13522f_searchpairpos(argvars, rettv)
13523 typval_T *argvars;
13524 typval_T *rettv;
13525{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013526 pos_T match_pos;
13527 int lnum = 0;
13528 int col = 0;
13529
13530 rettv->vval.v_number = 0;
13531
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013532 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013533 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013534
13535 if (searchpair_cmn(argvars, &match_pos) > 0)
13536 {
13537 lnum = match_pos.lnum;
13538 col = match_pos.col;
13539 }
13540
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013541 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13542 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013543}
13544
13545/*
13546 * Search for a start/middle/end thing.
13547 * Used by searchpair(), see its documentation for the details.
13548 * Returns 0 or -1 for no match,
13549 */
13550 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013551do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013552 char_u *spat; /* start pattern */
13553 char_u *mpat; /* middle pattern */
13554 char_u *epat; /* end pattern */
13555 int dir; /* BACKWARD or FORWARD */
13556 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013557 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013558 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013559 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013560{
13561 char_u *save_cpo;
13562 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13563 long retval = 0;
13564 pos_T pos;
13565 pos_T firstpos;
13566 pos_T foundpos;
13567 pos_T save_cursor;
13568 pos_T save_pos;
13569 int n;
13570 int r;
13571 int nest = 1;
13572 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013573 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013574
13575 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13576 save_cpo = p_cpo;
13577 p_cpo = (char_u *)"";
13578
13579 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13580 * start/middle/end (pat3, for the top pair). */
13581 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13582 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13583 if (pat2 == NULL || pat3 == NULL)
13584 goto theend;
13585 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13586 if (*mpat == NUL)
13587 STRCPY(pat3, pat2);
13588 else
13589 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13590 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013591 if (flags & SP_START)
13592 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013593
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594 save_cursor = curwin->w_cursor;
13595 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013596 clearpos(&firstpos);
13597 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 pat = pat3;
13599 for (;;)
13600 {
13601 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013602 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13604 /* didn't find it or found the first match again: FAIL */
13605 break;
13606
13607 if (firstpos.lnum == 0)
13608 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013609 if (equalpos(pos, foundpos))
13610 {
13611 /* Found the same position again. Can happen with a pattern that
13612 * has "\zs" at the end and searching backwards. Advance one
13613 * character and try again. */
13614 if (dir == BACKWARD)
13615 decl(&pos);
13616 else
13617 incl(&pos);
13618 }
13619 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620
13621 /* If the skip pattern matches, ignore this match. */
13622 if (*skip != NUL)
13623 {
13624 save_pos = curwin->w_cursor;
13625 curwin->w_cursor = pos;
13626 r = eval_to_bool(skip, &err, NULL, FALSE);
13627 curwin->w_cursor = save_pos;
13628 if (err)
13629 {
13630 /* Evaluating {skip} caused an error, break here. */
13631 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013632 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 break;
13634 }
13635 if (r)
13636 continue;
13637 }
13638
13639 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13640 {
13641 /* Found end when searching backwards or start when searching
13642 * forward: nested pair. */
13643 ++nest;
13644 pat = pat2; /* nested, don't search for middle */
13645 }
13646 else
13647 {
13648 /* Found end when searching forward or start when searching
13649 * backward: end of (nested) pair; or found middle in outer pair. */
13650 if (--nest == 1)
13651 pat = pat3; /* outer level, search for middle */
13652 }
13653
13654 if (nest == 0)
13655 {
13656 /* Found the match: return matchcount or line number. */
13657 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013658 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013660 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013661 if (flags & SP_SETPCMARK)
13662 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 curwin->w_cursor = pos;
13664 if (!(flags & SP_REPEAT))
13665 break;
13666 nest = 1; /* search for next unmatched */
13667 }
13668 }
13669
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013670 if (match_pos != NULL)
13671 {
13672 /* Store the match cursor position */
13673 match_pos->lnum = curwin->w_cursor.lnum;
13674 match_pos->col = curwin->w_cursor.col + 1;
13675 }
13676
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013678 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 curwin->w_cursor = save_cursor;
13680
13681theend:
13682 vim_free(pat2);
13683 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013685
13686 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687}
13688
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013689/*
13690 * "searchpos()" function
13691 */
13692 static void
13693f_searchpos(argvars, rettv)
13694 typval_T *argvars;
13695 typval_T *rettv;
13696{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013697 pos_T match_pos;
13698 int lnum = 0;
13699 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013700 int n;
13701 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013702
13703 rettv->vval.v_number = 0;
13704
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013705 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013706 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013707
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013708 n = search_cmn(argvars, &match_pos, &flags);
13709 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013710 {
13711 lnum = match_pos.lnum;
13712 col = match_pos.col;
13713 }
13714
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013715 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13716 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013717 if (flags & SP_SUBPAT)
13718 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013719}
13720
13721
Bram Moolenaar0d660222005-01-07 21:51:51 +000013722/*ARGSUSED*/
13723 static void
13724f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013725 typval_T *argvars;
13726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013728#ifdef FEAT_CLIENTSERVER
13729 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013730 char_u *server = get_tv_string_chk(&argvars[0]);
13731 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732
Bram Moolenaar0d660222005-01-07 21:51:51 +000013733 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013734 if (server == NULL || reply == NULL)
13735 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013736 if (check_restricted() || check_secure())
13737 return;
13738# ifdef FEAT_X11
13739 if (check_connection() == FAIL)
13740 return;
13741# endif
13742
13743 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013745 EMSG(_("E258: Unable to send to client"));
13746 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013748 rettv->vval.v_number = 0;
13749#else
13750 rettv->vval.v_number = -1;
13751#endif
13752}
13753
13754/*ARGSUSED*/
13755 static void
13756f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013757 typval_T *argvars;
13758 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013759{
13760 char_u *r = NULL;
13761
13762#ifdef FEAT_CLIENTSERVER
13763# ifdef WIN32
13764 r = serverGetVimNames();
13765# else
13766 make_connection();
13767 if (X_DISPLAY != NULL)
13768 r = serverGetVimNames(X_DISPLAY);
13769# endif
13770#endif
13771 rettv->v_type = VAR_STRING;
13772 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773}
13774
13775/*
13776 * "setbufvar()" function
13777 */
13778/*ARGSUSED*/
13779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013780f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013781 typval_T *argvars;
13782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783{
13784 buf_T *buf;
13785#ifdef FEAT_AUTOCMD
13786 aco_save_T aco;
13787#else
13788 buf_T *save_curbuf;
13789#endif
13790 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013791 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 char_u nbuf[NUMBUFLEN];
13793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013794 rettv->vval.v_number = 0;
13795
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796 if (check_restricted() || check_secure())
13797 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013798 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13799 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013800 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801 varp = &argvars[2];
13802
13803 if (buf != NULL && varname != NULL && varp != NULL)
13804 {
13805 /* set curbuf to be our buf, temporarily */
13806#ifdef FEAT_AUTOCMD
13807 aucmd_prepbuf(&aco, buf);
13808#else
13809 save_curbuf = curbuf;
13810 curbuf = buf;
13811#endif
13812
13813 if (*varname == '&')
13814 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013815 long numval;
13816 char_u *strval;
13817 int error = FALSE;
13818
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013820 numval = get_tv_number_chk(varp, &error);
13821 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 if (!error && strval != NULL)
13823 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 }
13825 else
13826 {
13827 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13828 if (bufvarname != NULL)
13829 {
13830 STRCPY(bufvarname, "b:");
13831 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013832 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833 vim_free(bufvarname);
13834 }
13835 }
13836
13837 /* reset notion of buffer */
13838#ifdef FEAT_AUTOCMD
13839 aucmd_restbuf(&aco);
13840#else
13841 curbuf = save_curbuf;
13842#endif
13843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844}
13845
13846/*
13847 * "setcmdpos()" function
13848 */
13849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013850f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013851 typval_T *argvars;
13852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013854 int pos = (int)get_tv_number(&argvars[0]) - 1;
13855
13856 if (pos >= 0)
13857 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858}
13859
13860/*
13861 * "setline()" function
13862 */
13863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013864f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013865 typval_T *argvars;
13866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867{
13868 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013869 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013870 list_T *l = NULL;
13871 listitem_T *li = NULL;
13872 long added = 0;
13873 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013875 lnum = get_tv_lnum(&argvars[0]);
13876 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013878 l = argvars[1].vval.v_list;
13879 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013881 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013882 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013883
13884 rettv->vval.v_number = 0; /* OK */
13885 for (;;)
13886 {
13887 if (l != NULL)
13888 {
13889 /* list argument, get next string */
13890 if (li == NULL)
13891 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013892 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013893 li = li->li_next;
13894 }
13895
13896 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013897 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013898 break;
13899 if (lnum <= curbuf->b_ml.ml_line_count)
13900 {
13901 /* existing line, replace it */
13902 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13903 {
13904 changed_bytes(lnum, 0);
13905 check_cursor_col();
13906 rettv->vval.v_number = 0; /* OK */
13907 }
13908 }
13909 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13910 {
13911 /* lnum is one past the last line, append the line */
13912 ++added;
13913 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13914 rettv->vval.v_number = 0; /* OK */
13915 }
13916
13917 if (l == NULL) /* only one string argument */
13918 break;
13919 ++lnum;
13920 }
13921
13922 if (added > 0)
13923 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924}
13925
13926/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013927 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013928 */
13929/*ARGSUSED*/
13930 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013931set_qf_ll_list(wp, list_arg, action_arg, rettv)
13932 win_T *wp;
13933 typval_T *list_arg;
13934 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013935 typval_T *rettv;
13936{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013937#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013938 char_u *act;
13939 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013940#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013941
Bram Moolenaar2641f772005-03-25 21:58:17 +000013942 rettv->vval.v_number = -1;
13943
13944#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013945 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013946 EMSG(_(e_listreq));
13947 else
13948 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013949 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013950
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013951 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013952 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013953 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013954 if (act == NULL)
13955 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013956 if (*act == 'a' || *act == 'r')
13957 action = *act;
13958 }
13959
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013960 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013961 rettv->vval.v_number = 0;
13962 }
13963#endif
13964}
13965
13966/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013967 * "setloclist()" function
13968 */
13969/*ARGSUSED*/
13970 static void
13971f_setloclist(argvars, rettv)
13972 typval_T *argvars;
13973 typval_T *rettv;
13974{
13975 win_T *win;
13976
13977 rettv->vval.v_number = -1;
13978
13979 win = find_win_by_nr(&argvars[0]);
13980 if (win != NULL)
13981 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13982}
13983
13984/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013985 * "setpos()" function
13986 */
13987/*ARGSUSED*/
13988 static void
13989f_setpos(argvars, rettv)
13990 typval_T *argvars;
13991 typval_T *rettv;
13992{
13993 pos_T pos;
13994 int fnum;
13995 char_u *name;
13996
13997 name = get_tv_string_chk(argvars);
13998 if (name != NULL)
13999 {
14000 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14001 {
14002 --pos.col;
14003 if (name[0] == '.') /* cursor */
14004 {
14005 if (fnum == curbuf->b_fnum)
14006 {
14007 curwin->w_cursor = pos;
14008 check_cursor();
14009 }
14010 else
14011 EMSG(_(e_invarg));
14012 }
14013 else if (name[0] == '\'') /* mark */
14014 (void)setmark_pos(name[1], &pos, fnum);
14015 else
14016 EMSG(_(e_invarg));
14017 }
14018 }
14019}
14020
14021/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014022 * "setqflist()" function
14023 */
14024/*ARGSUSED*/
14025 static void
14026f_setqflist(argvars, rettv)
14027 typval_T *argvars;
14028 typval_T *rettv;
14029{
14030 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14031}
14032
14033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034 * "setreg()" function
14035 */
14036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014037f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014038 typval_T *argvars;
14039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040{
14041 int regname;
14042 char_u *strregname;
14043 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014044 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045 int append;
14046 char_u yank_type;
14047 long block_len;
14048
14049 block_len = -1;
14050 yank_type = MAUTO;
14051 append = FALSE;
14052
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014053 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014054 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014056 if (strregname == NULL)
14057 return; /* type error; errmsg already given */
14058 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 if (regname == 0 || regname == '@')
14060 regname = '"';
14061 else if (regname == '=')
14062 return;
14063
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014064 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014066 stropt = get_tv_string_chk(&argvars[2]);
14067 if (stropt == NULL)
14068 return; /* type error */
14069 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 switch (*stropt)
14071 {
14072 case 'a': case 'A': /* append */
14073 append = TRUE;
14074 break;
14075 case 'v': case 'c': /* character-wise selection */
14076 yank_type = MCHAR;
14077 break;
14078 case 'V': case 'l': /* line-wise selection */
14079 yank_type = MLINE;
14080 break;
14081#ifdef FEAT_VISUAL
14082 case 'b': case Ctrl_V: /* block-wise selection */
14083 yank_type = MBLOCK;
14084 if (VIM_ISDIGIT(stropt[1]))
14085 {
14086 ++stropt;
14087 block_len = getdigits(&stropt) - 1;
14088 --stropt;
14089 }
14090 break;
14091#endif
14092 }
14093 }
14094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014095 strval = get_tv_string_chk(&argvars[1]);
14096 if (strval != NULL)
14097 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014099 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100}
14101
14102
14103/*
14104 * "setwinvar(expr)" function
14105 */
14106/*ARGSUSED*/
14107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014108f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014109 typval_T *argvars;
14110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111{
14112 win_T *win;
14113#ifdef FEAT_WINDOWS
14114 win_T *save_curwin;
14115#endif
14116 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014117 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118 char_u nbuf[NUMBUFLEN];
14119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014120 rettv->vval.v_number = 0;
14121
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 if (check_restricted() || check_secure())
14123 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014125 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126 varp = &argvars[2];
14127
14128 if (win != NULL && varname != NULL && varp != NULL)
14129 {
14130#ifdef FEAT_WINDOWS
14131 /* set curwin to be our win, temporarily */
14132 save_curwin = curwin;
14133 curwin = win;
14134 curbuf = curwin->w_buffer;
14135#endif
14136
14137 if (*varname == '&')
14138 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014139 long numval;
14140 char_u *strval;
14141 int error = FALSE;
14142
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014144 numval = get_tv_number_chk(varp, &error);
14145 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014146 if (!error && strval != NULL)
14147 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148 }
14149 else
14150 {
14151 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14152 if (winvarname != NULL)
14153 {
14154 STRCPY(winvarname, "w:");
14155 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014156 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 vim_free(winvarname);
14158 }
14159 }
14160
14161#ifdef FEAT_WINDOWS
14162 /* Restore current window, if it's still valid (autocomands can make
14163 * it invalid). */
14164 if (win_valid(save_curwin))
14165 {
14166 curwin = save_curwin;
14167 curbuf = curwin->w_buffer;
14168 }
14169#endif
14170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171}
14172
14173/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014174 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 */
14176 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014177f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014178 typval_T *argvars;
14179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014181 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182
Bram Moolenaar0d660222005-01-07 21:51:51 +000014183 p = get_tv_string(&argvars[0]);
14184 rettv->vval.v_string = vim_strsave(p);
14185 simplify_filename(rettv->vval.v_string); /* simplify in place */
14186 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187}
14188
Bram Moolenaar0d660222005-01-07 21:51:51 +000014189static int
14190#ifdef __BORLANDC__
14191 _RTLENTRYF
14192#endif
14193 item_compare __ARGS((const void *s1, const void *s2));
14194static int
14195#ifdef __BORLANDC__
14196 _RTLENTRYF
14197#endif
14198 item_compare2 __ARGS((const void *s1, const void *s2));
14199
14200static int item_compare_ic;
14201static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014202static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014203#define ITEM_COMPARE_FAIL 999
14204
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014206 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014208 static int
14209#ifdef __BORLANDC__
14210_RTLENTRYF
14211#endif
14212item_compare(s1, s2)
14213 const void *s1;
14214 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014216 char_u *p1, *p2;
14217 char_u *tofree1, *tofree2;
14218 int res;
14219 char_u numbuf1[NUMBUFLEN];
14220 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014222 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14223 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014224 if (item_compare_ic)
14225 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014227 res = STRCMP(p1, p2);
14228 vim_free(tofree1);
14229 vim_free(tofree2);
14230 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231}
14232
14233 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014234#ifdef __BORLANDC__
14235_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014237item_compare2(s1, s2)
14238 const void *s1;
14239 const void *s2;
14240{
14241 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014242 typval_T rettv;
14243 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014244 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014246 /* shortcut after failure in previous call; compare all items equal */
14247 if (item_compare_func_err)
14248 return 0;
14249
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014250 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14251 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014252 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14253 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014254
14255 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14256 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014257 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014258 clear_tv(&argv[0]);
14259 clear_tv(&argv[1]);
14260
14261 if (res == FAIL)
14262 res = ITEM_COMPARE_FAIL;
14263 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014264 /* return value has wrong type */
14265 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14266 if (item_compare_func_err)
14267 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014268 clear_tv(&rettv);
14269 return res;
14270}
14271
14272/*
14273 * "sort({list})" function
14274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014275 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014276f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014277 typval_T *argvars;
14278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279{
Bram Moolenaar33570922005-01-25 22:26:29 +000014280 list_T *l;
14281 listitem_T *li;
14282 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014283 long len;
14284 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285
Bram Moolenaar0d660222005-01-07 21:51:51 +000014286 rettv->vval.v_number = 0;
14287 if (argvars[0].v_type != VAR_LIST)
14288 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 else
14290 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014291 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014292 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014293 return;
14294 rettv->vval.v_list = l;
14295 rettv->v_type = VAR_LIST;
14296 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297
Bram Moolenaar0d660222005-01-07 21:51:51 +000014298 len = list_len(l);
14299 if (len <= 1)
14300 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301
Bram Moolenaar0d660222005-01-07 21:51:51 +000014302 item_compare_ic = FALSE;
14303 item_compare_func = NULL;
14304 if (argvars[1].v_type != VAR_UNKNOWN)
14305 {
14306 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014307 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014308 else
14309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014310 int error = FALSE;
14311
14312 i = get_tv_number_chk(&argvars[1], &error);
14313 if (error)
14314 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014315 if (i == 1)
14316 item_compare_ic = TRUE;
14317 else
14318 item_compare_func = get_tv_string(&argvars[1]);
14319 }
14320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321
Bram Moolenaar0d660222005-01-07 21:51:51 +000014322 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014323 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014324 if (ptrs == NULL)
14325 return;
14326 i = 0;
14327 for (li = l->lv_first; li != NULL; li = li->li_next)
14328 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014331 /* test the compare function */
14332 if (item_compare_func != NULL
14333 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14334 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014335 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014337 {
14338 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014339 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014340 item_compare_func == NULL ? item_compare : item_compare2);
14341
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342 if (!item_compare_func_err)
14343 {
14344 /* Clear the List and append the items in the sorted order. */
14345 l->lv_first = l->lv_last = NULL;
14346 l->lv_len = 0;
14347 for (i = 0; i < len; ++i)
14348 list_append(l, ptrs[i]);
14349 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014350 }
14351
14352 vim_free(ptrs);
14353 }
14354}
14355
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014356/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014357 * "soundfold({word})" function
14358 */
14359 static void
14360f_soundfold(argvars, rettv)
14361 typval_T *argvars;
14362 typval_T *rettv;
14363{
14364 char_u *s;
14365
14366 rettv->v_type = VAR_STRING;
14367 s = get_tv_string(&argvars[0]);
14368#ifdef FEAT_SYN_HL
14369 rettv->vval.v_string = eval_soundfold(s);
14370#else
14371 rettv->vval.v_string = vim_strsave(s);
14372#endif
14373}
14374
14375/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014376 * "spellbadword()" function
14377 */
14378/* ARGSUSED */
14379 static void
14380f_spellbadword(argvars, rettv)
14381 typval_T *argvars;
14382 typval_T *rettv;
14383{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014384 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014385 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014386 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014387
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014388 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014389 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014390
14391#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014392 if (argvars[0].v_type == VAR_UNKNOWN)
14393 {
14394 /* Find the start and length of the badly spelled word. */
14395 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14396 if (len != 0)
14397 word = ml_get_cursor();
14398 }
14399 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14400 {
14401 char_u *str = get_tv_string_chk(&argvars[0]);
14402 int capcol = -1;
14403
14404 if (str != NULL)
14405 {
14406 /* Check the argument for spelling. */
14407 while (*str != NUL)
14408 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014409 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014410 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014411 {
14412 word = str;
14413 break;
14414 }
14415 str += len;
14416 }
14417 }
14418 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014419#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014420
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014421 list_append_string(rettv->vval.v_list, word, len);
14422 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014423 attr == HLF_SPB ? "bad" :
14424 attr == HLF_SPR ? "rare" :
14425 attr == HLF_SPL ? "local" :
14426 attr == HLF_SPC ? "caps" :
14427 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014428}
14429
14430/*
14431 * "spellsuggest()" function
14432 */
14433 static void
14434f_spellsuggest(argvars, rettv)
14435 typval_T *argvars;
14436 typval_T *rettv;
14437{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014438#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014439 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014440 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014441 int maxcount;
14442 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014443 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014444 listitem_T *li;
14445 int need_capital = FALSE;
14446#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014447
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014448 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014449 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014450
14451#ifdef FEAT_SYN_HL
14452 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14453 {
14454 str = get_tv_string(&argvars[0]);
14455 if (argvars[1].v_type != VAR_UNKNOWN)
14456 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014457 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014458 if (maxcount <= 0)
14459 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014460 if (argvars[2].v_type != VAR_UNKNOWN)
14461 {
14462 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14463 if (typeerr)
14464 return;
14465 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014466 }
14467 else
14468 maxcount = 25;
14469
Bram Moolenaar4770d092006-01-12 23:22:24 +000014470 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014471
14472 for (i = 0; i < ga.ga_len; ++i)
14473 {
14474 str = ((char_u **)ga.ga_data)[i];
14475
14476 li = listitem_alloc();
14477 if (li == NULL)
14478 vim_free(str);
14479 else
14480 {
14481 li->li_tv.v_type = VAR_STRING;
14482 li->li_tv.v_lock = 0;
14483 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014484 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014485 }
14486 }
14487 ga_clear(&ga);
14488 }
14489#endif
14490}
14491
Bram Moolenaar0d660222005-01-07 21:51:51 +000014492 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014493f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014494 typval_T *argvars;
14495 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014496{
14497 char_u *str;
14498 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014499 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014500 regmatch_T regmatch;
14501 char_u patbuf[NUMBUFLEN];
14502 char_u *save_cpo;
14503 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014504 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014505 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014506 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014507
14508 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14509 save_cpo = p_cpo;
14510 p_cpo = (char_u *)"";
14511
14512 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014513 if (argvars[1].v_type != VAR_UNKNOWN)
14514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014515 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14516 if (pat == NULL)
14517 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014518 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014519 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014520 }
14521 if (pat == NULL || *pat == NUL)
14522 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014523
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014524 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014526 if (typeerr)
14527 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528
Bram Moolenaar0d660222005-01-07 21:51:51 +000014529 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14530 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014532 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014533 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014534 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014535 if (*str == NUL)
14536 match = FALSE; /* empty item at the end */
14537 else
14538 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014539 if (match)
14540 end = regmatch.startp[0];
14541 else
14542 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014543 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14544 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014545 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014546 if (list_append_string(rettv->vval.v_list, str,
14547 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014548 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014549 }
14550 if (!match)
14551 break;
14552 /* Advance to just after the match. */
14553 if (regmatch.endp[0] > str)
14554 col = 0;
14555 else
14556 {
14557 /* Don't get stuck at the same match. */
14558#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014559 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014560#else
14561 col = 1;
14562#endif
14563 }
14564 str = regmatch.endp[0];
14565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566
Bram Moolenaar0d660222005-01-07 21:51:51 +000014567 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569
Bram Moolenaar0d660222005-01-07 21:51:51 +000014570 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571}
14572
14573#ifdef HAVE_STRFTIME
14574/*
14575 * "strftime({format}[, {time}])" function
14576 */
14577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014578f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014579 typval_T *argvars;
14580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581{
14582 char_u result_buf[256];
14583 struct tm *curtime;
14584 time_t seconds;
14585 char_u *p;
14586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014587 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014589 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014590 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 seconds = time(NULL);
14592 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014593 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 curtime = localtime(&seconds);
14595 /* MSVC returns NULL for an invalid value of seconds. */
14596 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014597 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 else
14599 {
14600# ifdef FEAT_MBYTE
14601 vimconv_T conv;
14602 char_u *enc;
14603
14604 conv.vc_type = CONV_NONE;
14605 enc = enc_locale();
14606 convert_setup(&conv, p_enc, enc);
14607 if (conv.vc_type != CONV_NONE)
14608 p = string_convert(&conv, p, NULL);
14609# endif
14610 if (p != NULL)
14611 (void)strftime((char *)result_buf, sizeof(result_buf),
14612 (char *)p, curtime);
14613 else
14614 result_buf[0] = NUL;
14615
14616# ifdef FEAT_MBYTE
14617 if (conv.vc_type != CONV_NONE)
14618 vim_free(p);
14619 convert_setup(&conv, enc, p_enc);
14620 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014621 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 else
14623# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014624 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625
14626# ifdef FEAT_MBYTE
14627 /* Release conversion descriptors */
14628 convert_setup(&conv, NULL, NULL);
14629 vim_free(enc);
14630# endif
14631 }
14632}
14633#endif
14634
14635/*
14636 * "stridx()" function
14637 */
14638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014639f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014640 typval_T *argvars;
14641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642{
14643 char_u buf[NUMBUFLEN];
14644 char_u *needle;
14645 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014646 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014648 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014650 needle = get_tv_string_chk(&argvars[1]);
14651 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014652 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014653 if (needle == NULL || haystack == NULL)
14654 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655
Bram Moolenaar33570922005-01-25 22:26:29 +000014656 if (argvars[2].v_type != VAR_UNKNOWN)
14657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014658 int error = FALSE;
14659
14660 start_idx = get_tv_number_chk(&argvars[2], &error);
14661 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014662 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014663 if (start_idx >= 0)
14664 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014665 }
14666
14667 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14668 if (pos != NULL)
14669 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670}
14671
14672/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014673 * "string()" function
14674 */
14675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014677 typval_T *argvars;
14678 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014679{
14680 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014681 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014683 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014684 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014685 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014686 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687}
14688
14689/*
14690 * "strlen()" function
14691 */
14692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014693f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014694 typval_T *argvars;
14695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014696{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014697 rettv->vval.v_number = (varnumber_T)(STRLEN(
14698 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699}
14700
14701/*
14702 * "strpart()" function
14703 */
14704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014705f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014706 typval_T *argvars;
14707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708{
14709 char_u *p;
14710 int n;
14711 int len;
14712 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014713 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014715 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 slen = (int)STRLEN(p);
14717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014718 n = get_tv_number_chk(&argvars[1], &error);
14719 if (error)
14720 len = 0;
14721 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014722 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723 else
14724 len = slen - n; /* default len: all bytes that are available. */
14725
14726 /*
14727 * Only return the overlap between the specified part and the actual
14728 * string.
14729 */
14730 if (n < 0)
14731 {
14732 len += n;
14733 n = 0;
14734 }
14735 else if (n > slen)
14736 n = slen;
14737 if (len < 0)
14738 len = 0;
14739 else if (n + len > slen)
14740 len = slen - n;
14741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014742 rettv->v_type = VAR_STRING;
14743 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744}
14745
14746/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014747 * "strridx()" function
14748 */
14749 static void
14750f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014751 typval_T *argvars;
14752 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014753{
14754 char_u buf[NUMBUFLEN];
14755 char_u *needle;
14756 char_u *haystack;
14757 char_u *rest;
14758 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014759 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014761 needle = get_tv_string_chk(&argvars[1]);
14762 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014763 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014764
14765 rettv->vval.v_number = -1;
14766 if (needle == NULL || haystack == NULL)
14767 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014768 if (argvars[2].v_type != VAR_UNKNOWN)
14769 {
14770 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014771 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014772 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014773 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014774 }
14775 else
14776 end_idx = haystack_len;
14777
Bram Moolenaar0d660222005-01-07 21:51:51 +000014778 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014779 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014780 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014781 lastmatch = haystack + end_idx;
14782 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014783 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014784 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014785 for (rest = haystack; *rest != '\0'; ++rest)
14786 {
14787 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014788 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014789 break;
14790 lastmatch = rest;
14791 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014792 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014793
14794 if (lastmatch == NULL)
14795 rettv->vval.v_number = -1;
14796 else
14797 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14798}
14799
14800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801 * "strtrans()" function
14802 */
14803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014804f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014805 typval_T *argvars;
14806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014808 rettv->v_type = VAR_STRING;
14809 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810}
14811
14812/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014813 * "submatch()" function
14814 */
14815 static void
14816f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014817 typval_T *argvars;
14818 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014819{
14820 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014821 rettv->vval.v_string =
14822 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014823}
14824
14825/*
14826 * "substitute()" function
14827 */
14828 static void
14829f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014830 typval_T *argvars;
14831 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014832{
14833 char_u patbuf[NUMBUFLEN];
14834 char_u subbuf[NUMBUFLEN];
14835 char_u flagsbuf[NUMBUFLEN];
14836
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014837 char_u *str = get_tv_string_chk(&argvars[0]);
14838 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14839 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14840 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14841
Bram Moolenaar0d660222005-01-07 21:51:51 +000014842 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014843 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14844 rettv->vval.v_string = NULL;
14845 else
14846 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014847}
14848
14849/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014850 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851 */
14852/*ARGSUSED*/
14853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014854f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014855 typval_T *argvars;
14856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857{
14858 int id = 0;
14859#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014860 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014861 long col;
14862 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014863 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014865 lnum = get_tv_lnum(argvars); /* -1 on type error */
14866 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14867 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014869 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014870 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014871 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872#endif
14873
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014874 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875}
14876
14877/*
14878 * "synIDattr(id, what [, mode])" function
14879 */
14880/*ARGSUSED*/
14881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014882f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014883 typval_T *argvars;
14884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885{
14886 char_u *p = NULL;
14887#ifdef FEAT_SYN_HL
14888 int id;
14889 char_u *what;
14890 char_u *mode;
14891 char_u modebuf[NUMBUFLEN];
14892 int modec;
14893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014894 id = get_tv_number(&argvars[0]);
14895 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014896 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014898 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899 modec = TOLOWER_ASC(mode[0]);
14900 if (modec != 't' && modec != 'c'
14901#ifdef FEAT_GUI
14902 && modec != 'g'
14903#endif
14904 )
14905 modec = 0; /* replace invalid with current */
14906 }
14907 else
14908 {
14909#ifdef FEAT_GUI
14910 if (gui.in_use)
14911 modec = 'g';
14912 else
14913#endif
14914 if (t_colors > 1)
14915 modec = 'c';
14916 else
14917 modec = 't';
14918 }
14919
14920
14921 switch (TOLOWER_ASC(what[0]))
14922 {
14923 case 'b':
14924 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14925 p = highlight_color(id, what, modec);
14926 else /* bold */
14927 p = highlight_has_attr(id, HL_BOLD, modec);
14928 break;
14929
14930 case 'f': /* fg[#] */
14931 p = highlight_color(id, what, modec);
14932 break;
14933
14934 case 'i':
14935 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14936 p = highlight_has_attr(id, HL_INVERSE, modec);
14937 else /* italic */
14938 p = highlight_has_attr(id, HL_ITALIC, modec);
14939 break;
14940
14941 case 'n': /* name */
14942 p = get_highlight_name(NULL, id - 1);
14943 break;
14944
14945 case 'r': /* reverse */
14946 p = highlight_has_attr(id, HL_INVERSE, modec);
14947 break;
14948
14949 case 's': /* standout */
14950 p = highlight_has_attr(id, HL_STANDOUT, modec);
14951 break;
14952
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014953 case 'u':
14954 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14955 /* underline */
14956 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14957 else
14958 /* undercurl */
14959 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960 break;
14961 }
14962
14963 if (p != NULL)
14964 p = vim_strsave(p);
14965#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014966 rettv->v_type = VAR_STRING;
14967 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968}
14969
14970/*
14971 * "synIDtrans(id)" function
14972 */
14973/*ARGSUSED*/
14974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014975f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014976 typval_T *argvars;
14977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978{
14979 int id;
14980
14981#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014982 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983
14984 if (id > 0)
14985 id = syn_get_final_id(id);
14986 else
14987#endif
14988 id = 0;
14989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014990 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991}
14992
14993/*
14994 * "system()" function
14995 */
14996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014997f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014998 typval_T *argvars;
14999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015001 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015003 char_u *infile = NULL;
15004 char_u buf[NUMBUFLEN];
15005 int err = FALSE;
15006 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015008 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015009 {
15010 /*
15011 * Write the string to a temp file, to be used for input of the shell
15012 * command.
15013 */
15014 if ((infile = vim_tempname('i')) == NULL)
15015 {
15016 EMSG(_(e_notmp));
15017 return;
15018 }
15019
15020 fd = mch_fopen((char *)infile, WRITEBIN);
15021 if (fd == NULL)
15022 {
15023 EMSG2(_(e_notopen), infile);
15024 goto done;
15025 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015026 p = get_tv_string_buf_chk(&argvars[1], buf);
15027 if (p == NULL)
15028 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015029 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15030 err = TRUE;
15031 if (fclose(fd) != 0)
15032 err = TRUE;
15033 if (err)
15034 {
15035 EMSG(_("E677: Error writing temp file"));
15036 goto done;
15037 }
15038 }
15039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015040 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015041
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042#ifdef USE_CR
15043 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015044 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045 {
15046 char_u *s;
15047
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015048 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049 {
15050 if (*s == CAR)
15051 *s = NL;
15052 }
15053 }
15054#else
15055# ifdef USE_CRNL
15056 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015057 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058 {
15059 char_u *s, *d;
15060
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015061 d = res;
15062 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063 {
15064 if (s[0] == CAR && s[1] == NL)
15065 ++s;
15066 *d++ = *s;
15067 }
15068 *d = NUL;
15069 }
15070# endif
15071#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015072
15073done:
15074 if (infile != NULL)
15075 {
15076 mch_remove(infile);
15077 vim_free(infile);
15078 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015079 rettv->v_type = VAR_STRING;
15080 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081}
15082
15083/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015084 * "tabpagebuflist()" function
15085 */
15086/* ARGSUSED */
15087 static void
15088f_tabpagebuflist(argvars, rettv)
15089 typval_T *argvars;
15090 typval_T *rettv;
15091{
15092#ifndef FEAT_WINDOWS
15093 rettv->vval.v_number = 0;
15094#else
15095 tabpage_T *tp;
15096 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015097
15098 if (argvars[0].v_type == VAR_UNKNOWN)
15099 wp = firstwin;
15100 else
15101 {
15102 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15103 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015104 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015105 }
15106 if (wp == NULL)
15107 rettv->vval.v_number = 0;
15108 else
15109 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015110 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015111 rettv->vval.v_number = 0;
15112 else
15113 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015114 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015115 if (list_append_number(rettv->vval.v_list,
15116 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015117 break;
15118 }
15119 }
15120#endif
15121}
15122
15123
15124/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015125 * "tabpagenr()" function
15126 */
15127/* ARGSUSED */
15128 static void
15129f_tabpagenr(argvars, rettv)
15130 typval_T *argvars;
15131 typval_T *rettv;
15132{
15133 int nr = 1;
15134#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015135 char_u *arg;
15136
15137 if (argvars[0].v_type != VAR_UNKNOWN)
15138 {
15139 arg = get_tv_string_chk(&argvars[0]);
15140 nr = 0;
15141 if (arg != NULL)
15142 {
15143 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015144 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015145 else
15146 EMSG2(_(e_invexpr2), arg);
15147 }
15148 }
15149 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015150 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015151#endif
15152 rettv->vval.v_number = nr;
15153}
15154
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015155
15156#ifdef FEAT_WINDOWS
15157static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15158
15159/*
15160 * Common code for tabpagewinnr() and winnr().
15161 */
15162 static int
15163get_winnr(tp, argvar)
15164 tabpage_T *tp;
15165 typval_T *argvar;
15166{
15167 win_T *twin;
15168 int nr = 1;
15169 win_T *wp;
15170 char_u *arg;
15171
15172 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15173 if (argvar->v_type != VAR_UNKNOWN)
15174 {
15175 arg = get_tv_string_chk(argvar);
15176 if (arg == NULL)
15177 nr = 0; /* type error; errmsg already given */
15178 else if (STRCMP(arg, "$") == 0)
15179 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15180 else if (STRCMP(arg, "#") == 0)
15181 {
15182 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15183 if (twin == NULL)
15184 nr = 0;
15185 }
15186 else
15187 {
15188 EMSG2(_(e_invexpr2), arg);
15189 nr = 0;
15190 }
15191 }
15192
15193 if (nr > 0)
15194 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15195 wp != twin; wp = wp->w_next)
15196 ++nr;
15197 return nr;
15198}
15199#endif
15200
15201/*
15202 * "tabpagewinnr()" function
15203 */
15204/* ARGSUSED */
15205 static void
15206f_tabpagewinnr(argvars, rettv)
15207 typval_T *argvars;
15208 typval_T *rettv;
15209{
15210 int nr = 1;
15211#ifdef FEAT_WINDOWS
15212 tabpage_T *tp;
15213
15214 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15215 if (tp == NULL)
15216 nr = 0;
15217 else
15218 nr = get_winnr(tp, &argvars[1]);
15219#endif
15220 rettv->vval.v_number = nr;
15221}
15222
15223
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015224/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015225 * "tagfiles()" function
15226 */
15227/*ARGSUSED*/
15228 static void
15229f_tagfiles(argvars, rettv)
15230 typval_T *argvars;
15231 typval_T *rettv;
15232{
15233 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015234 tagname_T tn;
15235 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015236
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015237 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015238 {
15239 rettv->vval.v_number = 0;
15240 return;
15241 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015242
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015243 for (first = TRUE; ; first = FALSE)
15244 if (get_tagfname(&tn, first, fname) == FAIL
15245 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015246 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015247 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015248}
15249
15250/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015251 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015252 */
15253 static void
15254f_taglist(argvars, rettv)
15255 typval_T *argvars;
15256 typval_T *rettv;
15257{
15258 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015259
15260 tag_pattern = get_tv_string(&argvars[0]);
15261
15262 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015263 if (*tag_pattern == NUL)
15264 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015265
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015266 if (rettv_list_alloc(rettv) == OK)
15267 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015268}
15269
15270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015271 * "tempname()" function
15272 */
15273/*ARGSUSED*/
15274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015275f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015276 typval_T *argvars;
15277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278{
15279 static int x = 'A';
15280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015281 rettv->v_type = VAR_STRING;
15282 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283
15284 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15285 * names. Skip 'I' and 'O', they are used for shell redirection. */
15286 do
15287 {
15288 if (x == 'Z')
15289 x = '0';
15290 else if (x == '9')
15291 x = 'A';
15292 else
15293 {
15294#ifdef EBCDIC
15295 if (x == 'I')
15296 x = 'J';
15297 else if (x == 'R')
15298 x = 'S';
15299 else
15300#endif
15301 ++x;
15302 }
15303 } while (x == 'I' || x == 'O');
15304}
15305
15306/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015307 * "test(list)" function: Just checking the walls...
15308 */
15309/*ARGSUSED*/
15310 static void
15311f_test(argvars, rettv)
15312 typval_T *argvars;
15313 typval_T *rettv;
15314{
15315 /* Used for unit testing. Change the code below to your liking. */
15316#if 0
15317 listitem_T *li;
15318 list_T *l;
15319 char_u *bad, *good;
15320
15321 if (argvars[0].v_type != VAR_LIST)
15322 return;
15323 l = argvars[0].vval.v_list;
15324 if (l == NULL)
15325 return;
15326 li = l->lv_first;
15327 if (li == NULL)
15328 return;
15329 bad = get_tv_string(&li->li_tv);
15330 li = li->li_next;
15331 if (li == NULL)
15332 return;
15333 good = get_tv_string(&li->li_tv);
15334 rettv->vval.v_number = test_edit_score(bad, good);
15335#endif
15336}
15337
15338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339 * "tolower(string)" function
15340 */
15341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015342f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015343 typval_T *argvars;
15344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345{
15346 char_u *p;
15347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015348 p = vim_strsave(get_tv_string(&argvars[0]));
15349 rettv->v_type = VAR_STRING;
15350 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351
15352 if (p != NULL)
15353 while (*p != NUL)
15354 {
15355#ifdef FEAT_MBYTE
15356 int l;
15357
15358 if (enc_utf8)
15359 {
15360 int c, lc;
15361
15362 c = utf_ptr2char(p);
15363 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015364 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 /* TODO: reallocate string when byte count changes. */
15366 if (utf_char2len(lc) == l)
15367 utf_char2bytes(lc, p);
15368 p += l;
15369 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015370 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371 p += l; /* skip multi-byte character */
15372 else
15373#endif
15374 {
15375 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15376 ++p;
15377 }
15378 }
15379}
15380
15381/*
15382 * "toupper(string)" function
15383 */
15384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015385f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015386 typval_T *argvars;
15387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015389 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015390 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391}
15392
15393/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015394 * "tr(string, fromstr, tostr)" function
15395 */
15396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015398 typval_T *argvars;
15399 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015400{
15401 char_u *instr;
15402 char_u *fromstr;
15403 char_u *tostr;
15404 char_u *p;
15405#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015406 int inlen;
15407 int fromlen;
15408 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015409 int idx;
15410 char_u *cpstr;
15411 int cplen;
15412 int first = TRUE;
15413#endif
15414 char_u buf[NUMBUFLEN];
15415 char_u buf2[NUMBUFLEN];
15416 garray_T ga;
15417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015418 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015419 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15420 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015421
15422 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015423 rettv->v_type = VAR_STRING;
15424 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015425 if (fromstr == NULL || tostr == NULL)
15426 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015427 ga_init2(&ga, (int)sizeof(char), 80);
15428
15429#ifdef FEAT_MBYTE
15430 if (!has_mbyte)
15431#endif
15432 /* not multi-byte: fromstr and tostr must be the same length */
15433 if (STRLEN(fromstr) != STRLEN(tostr))
15434 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015435#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015436error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015437#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015438 EMSG2(_(e_invarg2), fromstr);
15439 ga_clear(&ga);
15440 return;
15441 }
15442
15443 /* fromstr and tostr have to contain the same number of chars */
15444 while (*instr != NUL)
15445 {
15446#ifdef FEAT_MBYTE
15447 if (has_mbyte)
15448 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015449 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015450 cpstr = instr;
15451 cplen = inlen;
15452 idx = 0;
15453 for (p = fromstr; *p != NUL; p += fromlen)
15454 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015455 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015456 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15457 {
15458 for (p = tostr; *p != NUL; p += tolen)
15459 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015460 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015461 if (idx-- == 0)
15462 {
15463 cplen = tolen;
15464 cpstr = p;
15465 break;
15466 }
15467 }
15468 if (*p == NUL) /* tostr is shorter than fromstr */
15469 goto error;
15470 break;
15471 }
15472 ++idx;
15473 }
15474
15475 if (first && cpstr == instr)
15476 {
15477 /* Check that fromstr and tostr have the same number of
15478 * (multi-byte) characters. Done only once when a character
15479 * of instr doesn't appear in fromstr. */
15480 first = FALSE;
15481 for (p = tostr; *p != NUL; p += tolen)
15482 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015483 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015484 --idx;
15485 }
15486 if (idx != 0)
15487 goto error;
15488 }
15489
15490 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015491 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015492 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015493
15494 instr += inlen;
15495 }
15496 else
15497#endif
15498 {
15499 /* When not using multi-byte chars we can do it faster. */
15500 p = vim_strchr(fromstr, *instr);
15501 if (p != NULL)
15502 ga_append(&ga, tostr[p - fromstr]);
15503 else
15504 ga_append(&ga, *instr);
15505 ++instr;
15506 }
15507 }
15508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015509 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015510}
15511
15512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 * "type(expr)" function
15514 */
15515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015516f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015517 typval_T *argvars;
15518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015520 int n;
15521
15522 switch (argvars[0].v_type)
15523 {
15524 case VAR_NUMBER: n = 0; break;
15525 case VAR_STRING: n = 1; break;
15526 case VAR_FUNC: n = 2; break;
15527 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015528 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015529 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15530 }
15531 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532}
15533
15534/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015535 * "values(dict)" function
15536 */
15537 static void
15538f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015539 typval_T *argvars;
15540 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015541{
15542 dict_list(argvars, rettv, 1);
15543}
15544
15545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546 * "virtcol(string)" function
15547 */
15548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015549f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015550 typval_T *argvars;
15551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552{
15553 colnr_T vcol = 0;
15554 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015555 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015557 fp = var2fpos(&argvars[0], FALSE, &fnum);
15558 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15559 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 {
15561 getvvcol(curwin, fp, NULL, NULL, &vcol);
15562 ++vcol;
15563 }
15564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015565 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566}
15567
15568/*
15569 * "visualmode()" function
15570 */
15571/*ARGSUSED*/
15572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015573f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015574 typval_T *argvars;
15575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576{
15577#ifdef FEAT_VISUAL
15578 char_u str[2];
15579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015580 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581 str[0] = curbuf->b_visual_mode_eval;
15582 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015583 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584
15585 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015586 if ((argvars[0].v_type == VAR_NUMBER
15587 && argvars[0].vval.v_number != 0)
15588 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015589 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590 curbuf->b_visual_mode_eval = NUL;
15591#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015592 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593#endif
15594}
15595
15596/*
15597 * "winbufnr(nr)" function
15598 */
15599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015600f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015601 typval_T *argvars;
15602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603{
15604 win_T *wp;
15605
15606 wp = find_win_by_nr(&argvars[0]);
15607 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015608 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015610 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611}
15612
15613/*
15614 * "wincol()" function
15615 */
15616/*ARGSUSED*/
15617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015618f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015619 typval_T *argvars;
15620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621{
15622 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015623 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624}
15625
15626/*
15627 * "winheight(nr)" function
15628 */
15629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015630f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015631 typval_T *argvars;
15632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633{
15634 win_T *wp;
15635
15636 wp = find_win_by_nr(&argvars[0]);
15637 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015638 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015640 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641}
15642
15643/*
15644 * "winline()" function
15645 */
15646/*ARGSUSED*/
15647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015648f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015649 typval_T *argvars;
15650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651{
15652 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015653 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654}
15655
15656/*
15657 * "winnr()" function
15658 */
15659/* ARGSUSED */
15660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015661f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015662 typval_T *argvars;
15663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664{
15665 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015666
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015668 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015670 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671}
15672
15673/*
15674 * "winrestcmd()" function
15675 */
15676/* ARGSUSED */
15677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015678f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015679 typval_T *argvars;
15680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681{
15682#ifdef FEAT_WINDOWS
15683 win_T *wp;
15684 int winnr = 1;
15685 garray_T ga;
15686 char_u buf[50];
15687
15688 ga_init2(&ga, (int)sizeof(char), 70);
15689 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15690 {
15691 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15692 ga_concat(&ga, buf);
15693# ifdef FEAT_VERTSPLIT
15694 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15695 ga_concat(&ga, buf);
15696# endif
15697 ++winnr;
15698 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015699 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015701 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015703 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015705 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706}
15707
15708/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015709 * "winrestview()" function
15710 */
15711/* ARGSUSED */
15712 static void
15713f_winrestview(argvars, rettv)
15714 typval_T *argvars;
15715 typval_T *rettv;
15716{
15717 dict_T *dict;
15718
15719 if (argvars[0].v_type != VAR_DICT
15720 || (dict = argvars[0].vval.v_dict) == NULL)
15721 EMSG(_(e_invarg));
15722 else
15723 {
15724 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
15725 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
15726#ifdef FEAT_VIRTUALEDIT
15727 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
15728#endif
15729 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015730 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015731
15732 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
15733#ifdef FEAT_DIFF
15734 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
15735#endif
15736 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
15737 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
15738
15739 check_cursor();
15740 changed_cline_bef_curs();
15741 invalidate_botline();
15742 redraw_later(VALID);
15743
15744 if (curwin->w_topline == 0)
15745 curwin->w_topline = 1;
15746 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15747 curwin->w_topline = curbuf->b_ml.ml_line_count;
15748#ifdef FEAT_DIFF
15749 check_topfill(curwin, TRUE);
15750#endif
15751 }
15752}
15753
15754/*
15755 * "winsaveview()" function
15756 */
15757/* ARGSUSED */
15758 static void
15759f_winsaveview(argvars, rettv)
15760 typval_T *argvars;
15761 typval_T *rettv;
15762{
15763 dict_T *dict;
15764
15765 dict = dict_alloc();
15766 if (dict == NULL)
15767 return;
15768 rettv->v_type = VAR_DICT;
15769 rettv->vval.v_dict = dict;
15770 ++dict->dv_refcount;
15771
15772 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
15773 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
15774#ifdef FEAT_VIRTUALEDIT
15775 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
15776#endif
15777 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
15778
15779 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
15780#ifdef FEAT_DIFF
15781 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
15782#endif
15783 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
15784 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
15785}
15786
15787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 * "winwidth(nr)" function
15789 */
15790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015791f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015792 typval_T *argvars;
15793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794{
15795 win_T *wp;
15796
15797 wp = find_win_by_nr(&argvars[0]);
15798 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015799 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800 else
15801#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015802 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015804 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805#endif
15806}
15807
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015809 * "writefile()" function
15810 */
15811 static void
15812f_writefile(argvars, rettv)
15813 typval_T *argvars;
15814 typval_T *rettv;
15815{
15816 int binary = FALSE;
15817 char_u *fname;
15818 FILE *fd;
15819 listitem_T *li;
15820 char_u *s;
15821 int ret = 0;
15822 int c;
15823
15824 if (argvars[0].v_type != VAR_LIST)
15825 {
15826 EMSG2(_(e_listarg), "writefile()");
15827 return;
15828 }
15829 if (argvars[0].vval.v_list == NULL)
15830 return;
15831
15832 if (argvars[2].v_type != VAR_UNKNOWN
15833 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15834 binary = TRUE;
15835
15836 /* Always open the file in binary mode, library functions have a mind of
15837 * their own about CR-LF conversion. */
15838 fname = get_tv_string(&argvars[1]);
15839 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15840 {
15841 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15842 ret = -1;
15843 }
15844 else
15845 {
15846 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15847 li = li->li_next)
15848 {
15849 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15850 {
15851 if (*s == '\n')
15852 c = putc(NUL, fd);
15853 else
15854 c = putc(*s, fd);
15855 if (c == EOF)
15856 {
15857 ret = -1;
15858 break;
15859 }
15860 }
15861 if (!binary || li->li_next != NULL)
15862 if (putc('\n', fd) == EOF)
15863 {
15864 ret = -1;
15865 break;
15866 }
15867 if (ret < 0)
15868 {
15869 EMSG(_(e_write));
15870 break;
15871 }
15872 }
15873 fclose(fd);
15874 }
15875
15876 rettv->vval.v_number = ret;
15877}
15878
15879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015881 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882 */
15883 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015884var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015885 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015887 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015889 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015891 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892
Bram Moolenaara5525202006-03-02 22:52:09 +000015893 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015894 if (varp->v_type == VAR_LIST)
15895 {
15896 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015897 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000015898 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015899
15900 l = varp->vval.v_list;
15901 if (l == NULL)
15902 return NULL;
15903
15904 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015905 pos.lnum = list_find_nr(l, 0L, &error);
15906 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015907 return NULL; /* invalid line number */
15908
15909 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015910 pos.col = list_find_nr(l, 1L, &error);
15911 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015912 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015913 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000015914 /* Accept a position up to the NUL after the line. */
15915 if (pos.col <= 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015916 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015917 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015918
Bram Moolenaara5525202006-03-02 22:52:09 +000015919#ifdef FEAT_VIRTUALEDIT
15920 /* Get the virtual offset. Defaults to zero. */
15921 pos.coladd = list_find_nr(l, 2L, &error);
15922 if (error)
15923 pos.coladd = 0;
15924#endif
15925
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015926 return &pos;
15927 }
15928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015929 name = get_tv_string_chk(varp);
15930 if (name == NULL)
15931 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932 if (name[0] == '.') /* cursor */
15933 return &curwin->w_cursor;
15934 if (name[0] == '\'') /* mark */
15935 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015936 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015937 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15938 return NULL;
15939 return pp;
15940 }
Bram Moolenaara5525202006-03-02 22:52:09 +000015941
15942#ifdef FEAT_VIRTUALEDIT
15943 pos.coladd = 0;
15944#endif
15945
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015946 if (name[0] == 'w' && lnum)
15947 {
15948 pos.col = 0;
15949 if (name[1] == '0') /* "w0": first visible line */
15950 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015951 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015952 pos.lnum = curwin->w_topline;
15953 return &pos;
15954 }
15955 else if (name[1] == '$') /* "w$": last visible line */
15956 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015957 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015958 pos.lnum = curwin->w_botline - 1;
15959 return &pos;
15960 }
15961 }
15962 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015963 {
15964 if (lnum)
15965 {
15966 pos.lnum = curbuf->b_ml.ml_line_count;
15967 pos.col = 0;
15968 }
15969 else
15970 {
15971 pos.lnum = curwin->w_cursor.lnum;
15972 pos.col = (colnr_T)STRLEN(ml_get_curline());
15973 }
15974 return &pos;
15975 }
15976 return NULL;
15977}
15978
15979/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015980 * Convert list in "arg" into a position and optional file number.
15981 * When "fnump" is NULL there is no file number, only 3 items.
15982 * Note that the column is passed on as-is, the caller may want to decrement
15983 * it to use 1 for the first column.
15984 * Return FAIL when conversion is not possible, doesn't check the position for
15985 * validity.
15986 */
15987 static int
15988list2fpos(arg, posp, fnump)
15989 typval_T *arg;
15990 pos_T *posp;
15991 int *fnump;
15992{
15993 list_T *l = arg->vval.v_list;
15994 long i = 0;
15995 long n;
15996
15997 /* List must be: [fnum, lnum, col, coladd] */
15998 if (arg->v_type != VAR_LIST || l == NULL
15999 || l->lv_len != (fnump == NULL ? 3 : 4))
16000 return FAIL;
16001
16002 if (fnump != NULL)
16003 {
16004 n = list_find_nr(l, i++, NULL); /* fnum */
16005 if (n < 0)
16006 return FAIL;
16007 if (n == 0)
16008 n = curbuf->b_fnum; /* current buffer */
16009 *fnump = n;
16010 }
16011
16012 n = list_find_nr(l, i++, NULL); /* lnum */
16013 if (n < 0)
16014 return FAIL;
16015 posp->lnum = n;
16016
16017 n = list_find_nr(l, i++, NULL); /* col */
16018 if (n < 0)
16019 return FAIL;
16020 posp->col = n;
16021
16022#ifdef FEAT_VIRTUALEDIT
16023 n = list_find_nr(l, i, NULL);
16024 if (n < 0)
16025 return FAIL;
16026 posp->coladd = n;
16027#endif
16028
16029 return OK;
16030}
16031
16032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033 * Get the length of an environment variable name.
16034 * Advance "arg" to the first character after the name.
16035 * Return 0 for error.
16036 */
16037 static int
16038get_env_len(arg)
16039 char_u **arg;
16040{
16041 char_u *p;
16042 int len;
16043
16044 for (p = *arg; vim_isIDc(*p); ++p)
16045 ;
16046 if (p == *arg) /* no name found */
16047 return 0;
16048
16049 len = (int)(p - *arg);
16050 *arg = p;
16051 return len;
16052}
16053
16054/*
16055 * Get the length of the name of a function or internal variable.
16056 * "arg" is advanced to the first non-white character after the name.
16057 * Return 0 if something is wrong.
16058 */
16059 static int
16060get_id_len(arg)
16061 char_u **arg;
16062{
16063 char_u *p;
16064 int len;
16065
16066 /* Find the end of the name. */
16067 for (p = *arg; eval_isnamec(*p); ++p)
16068 ;
16069 if (p == *arg) /* no name found */
16070 return 0;
16071
16072 len = (int)(p - *arg);
16073 *arg = skipwhite(p);
16074
16075 return len;
16076}
16077
16078/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016079 * Get the length of the name of a variable or function.
16080 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016082 * Return -1 if curly braces expansion failed.
16083 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 * If the name contains 'magic' {}'s, expand them and return the
16085 * expanded name in an allocated string via 'alias' - caller must free.
16086 */
16087 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016088get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 char_u **arg;
16090 char_u **alias;
16091 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016092 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093{
16094 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 char_u *p;
16096 char_u *expr_start;
16097 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098
16099 *alias = NULL; /* default to no alias */
16100
16101 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16102 && (*arg)[2] == (int)KE_SNR)
16103 {
16104 /* hard coded <SNR>, already translated */
16105 *arg += 3;
16106 return get_id_len(arg) + 3;
16107 }
16108 len = eval_fname_script(*arg);
16109 if (len > 0)
16110 {
16111 /* literal "<SID>", "s:" or "<SNR>" */
16112 *arg += len;
16113 }
16114
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016116 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016118 p = find_name_end(*arg, &expr_start, &expr_end,
16119 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 if (expr_start != NULL)
16121 {
16122 char_u *temp_string;
16123
16124 if (!evaluate)
16125 {
16126 len += (int)(p - *arg);
16127 *arg = skipwhite(p);
16128 return len;
16129 }
16130
16131 /*
16132 * Include any <SID> etc in the expanded string:
16133 * Thus the -len here.
16134 */
16135 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16136 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016137 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138 *alias = temp_string;
16139 *arg = skipwhite(p);
16140 return (int)STRLEN(temp_string);
16141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142
16143 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016144 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145 EMSG2(_(e_invexpr2), *arg);
16146
16147 return len;
16148}
16149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016150/*
16151 * Find the end of a variable or function name, taking care of magic braces.
16152 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16153 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016154 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016155 * Return a pointer to just after the name. Equal to "arg" if there is no
16156 * valid name.
16157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016159find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 char_u *arg;
16161 char_u **expr_start;
16162 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016163 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016165 int mb_nest = 0;
16166 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167 char_u *p;
16168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016169 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016171 *expr_start = NULL;
16172 *expr_end = NULL;
16173 }
16174
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016175 /* Quick check for valid starting character. */
16176 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16177 return arg;
16178
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016179 for (p = arg; *p != NUL
16180 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016181 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016182 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016183 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016184 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016185 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016186 if (*p == '\'')
16187 {
16188 /* skip over 'string' to avoid counting [ and ] inside it. */
16189 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16190 ;
16191 if (*p == NUL)
16192 break;
16193 }
16194 else if (*p == '"')
16195 {
16196 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16197 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16198 if (*p == '\\' && p[1] != NUL)
16199 ++p;
16200 if (*p == NUL)
16201 break;
16202 }
16203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016204 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016206 if (*p == '[')
16207 ++br_nest;
16208 else if (*p == ']')
16209 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016212 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016214 if (*p == '{')
16215 {
16216 mb_nest++;
16217 if (expr_start != NULL && *expr_start == NULL)
16218 *expr_start = p;
16219 }
16220 else if (*p == '}')
16221 {
16222 mb_nest--;
16223 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16224 *expr_end = p;
16225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227 }
16228
16229 return p;
16230}
16231
16232/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016233 * Expands out the 'magic' {}'s in a variable/function name.
16234 * Note that this can call itself recursively, to deal with
16235 * constructs like foo{bar}{baz}{bam}
16236 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16237 * "in_start" ^
16238 * "expr_start" ^
16239 * "expr_end" ^
16240 * "in_end" ^
16241 *
16242 * Returns a new allocated string, which the caller must free.
16243 * Returns NULL for failure.
16244 */
16245 static char_u *
16246make_expanded_name(in_start, expr_start, expr_end, in_end)
16247 char_u *in_start;
16248 char_u *expr_start;
16249 char_u *expr_end;
16250 char_u *in_end;
16251{
16252 char_u c1;
16253 char_u *retval = NULL;
16254 char_u *temp_result;
16255 char_u *nextcmd = NULL;
16256
16257 if (expr_end == NULL || in_end == NULL)
16258 return NULL;
16259 *expr_start = NUL;
16260 *expr_end = NUL;
16261 c1 = *in_end;
16262 *in_end = NUL;
16263
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016264 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016265 if (temp_result != NULL && nextcmd == NULL)
16266 {
16267 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16268 + (in_end - expr_end) + 1));
16269 if (retval != NULL)
16270 {
16271 STRCPY(retval, in_start);
16272 STRCAT(retval, temp_result);
16273 STRCAT(retval, expr_end + 1);
16274 }
16275 }
16276 vim_free(temp_result);
16277
16278 *in_end = c1; /* put char back for error messages */
16279 *expr_start = '{';
16280 *expr_end = '}';
16281
16282 if (retval != NULL)
16283 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016284 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016285 if (expr_start != NULL)
16286 {
16287 /* Further expansion! */
16288 temp_result = make_expanded_name(retval, expr_start,
16289 expr_end, temp_result);
16290 vim_free(retval);
16291 retval = temp_result;
16292 }
16293 }
16294
16295 return retval;
16296}
16297
16298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016300 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 */
16302 static int
16303eval_isnamec(c)
16304 int c;
16305{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016306 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16307}
16308
16309/*
16310 * Return TRUE if character "c" can be used as the first character in a
16311 * variable or function name (excluding '{' and '}').
16312 */
16313 static int
16314eval_isnamec1(c)
16315 int c;
16316{
16317 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318}
16319
16320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 * Set number v: variable to "val".
16322 */
16323 void
16324set_vim_var_nr(idx, val)
16325 int idx;
16326 long val;
16327{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016328 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329}
16330
16331/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016332 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333 */
16334 long
16335get_vim_var_nr(idx)
16336 int idx;
16337{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016338 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339}
16340
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016341#if defined(FEAT_AUTOCMD) || defined(PROTO)
16342/*
16343 * Get string v: variable value. Uses a static buffer, can only be used once.
16344 */
16345 char_u *
16346get_vim_var_str(idx)
16347 int idx;
16348{
16349 return get_tv_string(&vimvars[idx].vv_tv);
16350}
16351#endif
16352
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353/*
16354 * Set v:count, v:count1 and v:prevcount.
16355 */
16356 void
16357set_vcount(count, count1)
16358 long count;
16359 long count1;
16360{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016361 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16362 vimvars[VV_COUNT].vv_nr = count;
16363 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364}
16365
16366/*
16367 * Set string v: variable to a copy of "val".
16368 */
16369 void
16370set_vim_var_string(idx, val, len)
16371 int idx;
16372 char_u *val;
16373 int len; /* length of "val" to use or -1 (whole string) */
16374{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016375 /* Need to do this (at least) once, since we can't initialize a union.
16376 * Will always be invoked when "v:progname" is set. */
16377 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16378
Bram Moolenaare9a41262005-01-15 22:18:47 +000016379 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016381 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016383 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016385 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386}
16387
16388/*
16389 * Set v:register if needed.
16390 */
16391 void
16392set_reg_var(c)
16393 int c;
16394{
16395 char_u regname;
16396
16397 if (c == 0 || c == ' ')
16398 regname = '"';
16399 else
16400 regname = c;
16401 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016402 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403 set_vim_var_string(VV_REG, &regname, 1);
16404}
16405
16406/*
16407 * Get or set v:exception. If "oldval" == NULL, return the current value.
16408 * Otherwise, restore the value to "oldval" and return NULL.
16409 * Must always be called in pairs to save and restore v:exception! Does not
16410 * take care of memory allocations.
16411 */
16412 char_u *
16413v_exception(oldval)
16414 char_u *oldval;
16415{
16416 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016417 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418
Bram Moolenaare9a41262005-01-15 22:18:47 +000016419 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420 return NULL;
16421}
16422
16423/*
16424 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16425 * Otherwise, restore the value to "oldval" and return NULL.
16426 * Must always be called in pairs to save and restore v:throwpoint! Does not
16427 * take care of memory allocations.
16428 */
16429 char_u *
16430v_throwpoint(oldval)
16431 char_u *oldval;
16432{
16433 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016434 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016435
Bram Moolenaare9a41262005-01-15 22:18:47 +000016436 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437 return NULL;
16438}
16439
16440#if defined(FEAT_AUTOCMD) || defined(PROTO)
16441/*
16442 * Set v:cmdarg.
16443 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16444 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16445 * Must always be called in pairs!
16446 */
16447 char_u *
16448set_cmdarg(eap, oldarg)
16449 exarg_T *eap;
16450 char_u *oldarg;
16451{
16452 char_u *oldval;
16453 char_u *newval;
16454 unsigned len;
16455
Bram Moolenaare9a41262005-01-15 22:18:47 +000016456 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016457 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016459 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016460 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016461 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462 }
16463
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016464 if (eap->force_bin == FORCE_BIN)
16465 len = 6;
16466 else if (eap->force_bin == FORCE_NOBIN)
16467 len = 8;
16468 else
16469 len = 0;
16470 if (eap->force_ff != 0)
16471 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16472# ifdef FEAT_MBYTE
16473 if (eap->force_enc != 0)
16474 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016475 if (eap->bad_char != 0)
16476 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016477# endif
16478
16479 newval = alloc(len + 1);
16480 if (newval == NULL)
16481 return NULL;
16482
16483 if (eap->force_bin == FORCE_BIN)
16484 sprintf((char *)newval, " ++bin");
16485 else if (eap->force_bin == FORCE_NOBIN)
16486 sprintf((char *)newval, " ++nobin");
16487 else
16488 *newval = NUL;
16489 if (eap->force_ff != 0)
16490 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16491 eap->cmd + eap->force_ff);
16492# ifdef FEAT_MBYTE
16493 if (eap->force_enc != 0)
16494 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16495 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016496 if (eap->bad_char != 0)
16497 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16498 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016499# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016500 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016501 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502}
16503#endif
16504
16505/*
16506 * Get the value of internal variable "name".
16507 * Return OK or FAIL.
16508 */
16509 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016510get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511 char_u *name;
16512 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016513 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016514 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016515{
16516 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016517 typval_T *tv = NULL;
16518 typval_T atv;
16519 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521
16522 /* truncate the name, so that we can use strcmp() */
16523 cc = name[len];
16524 name[len] = NUL;
16525
16526 /*
16527 * Check for "b:changedtick".
16528 */
16529 if (STRCMP(name, "b:changedtick") == 0)
16530 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016531 atv.v_type = VAR_NUMBER;
16532 atv.vval.v_number = curbuf->b_changedtick;
16533 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534 }
16535
16536 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 * Check for user-defined variables.
16538 */
16539 else
16540 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016541 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016543 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544 }
16545
Bram Moolenaare9a41262005-01-15 22:18:47 +000016546 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016548 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016549 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 ret = FAIL;
16551 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016552 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016553 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554
16555 name[len] = cc;
16556
16557 return ret;
16558}
16559
16560/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016561 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16562 * Also handle function call with Funcref variable: func(expr)
16563 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16564 */
16565 static int
16566handle_subscript(arg, rettv, evaluate, verbose)
16567 char_u **arg;
16568 typval_T *rettv;
16569 int evaluate; /* do more than finding the end */
16570 int verbose; /* give error messages */
16571{
16572 int ret = OK;
16573 dict_T *selfdict = NULL;
16574 char_u *s;
16575 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016576 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016577
16578 while (ret == OK
16579 && (**arg == '['
16580 || (**arg == '.' && rettv->v_type == VAR_DICT)
16581 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16582 && !vim_iswhite(*(*arg - 1)))
16583 {
16584 if (**arg == '(')
16585 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016586 /* need to copy the funcref so that we can clear rettv */
16587 functv = *rettv;
16588 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016589
16590 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016591 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016592 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016593 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16594 &len, evaluate, selfdict);
16595
16596 /* Clear the funcref afterwards, so that deleting it while
16597 * evaluating the arguments is possible (see test55). */
16598 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016599
16600 /* Stop the expression evaluation when immediately aborting on
16601 * error, or when an interrupt occurred or an exception was thrown
16602 * but not caught. */
16603 if (aborting())
16604 {
16605 if (ret == OK)
16606 clear_tv(rettv);
16607 ret = FAIL;
16608 }
16609 dict_unref(selfdict);
16610 selfdict = NULL;
16611 }
16612 else /* **arg == '[' || **arg == '.' */
16613 {
16614 dict_unref(selfdict);
16615 if (rettv->v_type == VAR_DICT)
16616 {
16617 selfdict = rettv->vval.v_dict;
16618 if (selfdict != NULL)
16619 ++selfdict->dv_refcount;
16620 }
16621 else
16622 selfdict = NULL;
16623 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16624 {
16625 clear_tv(rettv);
16626 ret = FAIL;
16627 }
16628 }
16629 }
16630 dict_unref(selfdict);
16631 return ret;
16632}
16633
16634/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016635 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16636 * value).
16637 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016638 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016639alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016640{
Bram Moolenaar33570922005-01-25 22:26:29 +000016641 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016642}
16643
16644/*
16645 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646 * The string "s" must have been allocated, it is consumed.
16647 * Return NULL for out of memory, the variable otherwise.
16648 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016649 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016650alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016651 char_u *s;
16652{
Bram Moolenaar33570922005-01-25 22:26:29 +000016653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016655 rettv = alloc_tv();
16656 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016658 rettv->v_type = VAR_STRING;
16659 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660 }
16661 else
16662 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016663 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664}
16665
16666/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016667 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016669 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016670free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016671 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672{
16673 if (varp != NULL)
16674 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016675 switch (varp->v_type)
16676 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016677 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016678 func_unref(varp->vval.v_string);
16679 /*FALLTHROUGH*/
16680 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016681 vim_free(varp->vval.v_string);
16682 break;
16683 case VAR_LIST:
16684 list_unref(varp->vval.v_list);
16685 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016686 case VAR_DICT:
16687 dict_unref(varp->vval.v_dict);
16688 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016689 case VAR_NUMBER:
16690 case VAR_UNKNOWN:
16691 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016692 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016693 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016694 break;
16695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696 vim_free(varp);
16697 }
16698}
16699
16700/*
16701 * Free the memory for a variable value and set the value to NULL or 0.
16702 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016703 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016704clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016705 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706{
16707 if (varp != NULL)
16708 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016709 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016711 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016712 func_unref(varp->vval.v_string);
16713 /*FALLTHROUGH*/
16714 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016715 vim_free(varp->vval.v_string);
16716 varp->vval.v_string = NULL;
16717 break;
16718 case VAR_LIST:
16719 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016720 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016721 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016722 case VAR_DICT:
16723 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016724 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016725 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016726 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016727 varp->vval.v_number = 0;
16728 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016729 case VAR_UNKNOWN:
16730 break;
16731 default:
16732 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016734 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735 }
16736}
16737
16738/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016739 * Set the value of a variable to NULL without freeing items.
16740 */
16741 static void
16742init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016743 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016744{
16745 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016746 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016747}
16748
16749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750 * Get the number value of a variable.
16751 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016752 * For incompatible types, return 0.
16753 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16754 * caller of incompatible types: it sets *denote to TRUE if "denote"
16755 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 */
16757 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016758get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016759 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016761 int error = FALSE;
16762
16763 return get_tv_number_chk(varp, &error); /* return 0L on error */
16764}
16765
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016766 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016767get_tv_number_chk(varp, denote)
16768 typval_T *varp;
16769 int *denote;
16770{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016771 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016773 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016775 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016776 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016777 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016778 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016779 break;
16780 case VAR_STRING:
16781 if (varp->vval.v_string != NULL)
16782 vim_str2nr(varp->vval.v_string, NULL, NULL,
16783 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016784 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016785 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016786 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016787 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016788 case VAR_DICT:
16789 EMSG(_("E728: Using a Dictionary as a number"));
16790 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016791 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016792 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016793 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016795 if (denote == NULL) /* useful for values that must be unsigned */
16796 n = -1;
16797 else
16798 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016799 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800}
16801
16802/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016803 * Get the lnum from the first argument.
16804 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016805 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806 */
16807 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016808get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016809 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810{
Bram Moolenaar33570922005-01-25 22:26:29 +000016811 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 linenr_T lnum;
16813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016814 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 if (lnum == 0) /* no valid number, try using line() */
16816 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016817 rettv.v_type = VAR_NUMBER;
16818 f_line(argvars, &rettv);
16819 lnum = rettv.vval.v_number;
16820 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 }
16822 return lnum;
16823}
16824
16825/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016826 * Get the lnum from the first argument.
16827 * Also accepts "$", then "buf" is used.
16828 * Returns 0 on error.
16829 */
16830 static linenr_T
16831get_tv_lnum_buf(argvars, buf)
16832 typval_T *argvars;
16833 buf_T *buf;
16834{
16835 if (argvars[0].v_type == VAR_STRING
16836 && argvars[0].vval.v_string != NULL
16837 && argvars[0].vval.v_string[0] == '$'
16838 && buf != NULL)
16839 return buf->b_ml.ml_line_count;
16840 return get_tv_number_chk(&argvars[0], NULL);
16841}
16842
16843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 * Get the string value of a variable.
16845 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016846 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16847 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 * If the String variable has never been set, return an empty string.
16849 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016850 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16851 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 */
16853 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016854get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016855 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856{
16857 static char_u mybuf[NUMBUFLEN];
16858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016859 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860}
16861
16862 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016863get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016864 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016865 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016867 char_u *res = get_tv_string_buf_chk(varp, buf);
16868
16869 return res != NULL ? res : (char_u *)"";
16870}
16871
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016872 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016873get_tv_string_chk(varp)
16874 typval_T *varp;
16875{
16876 static char_u mybuf[NUMBUFLEN];
16877
16878 return get_tv_string_buf_chk(varp, mybuf);
16879}
16880
16881 static char_u *
16882get_tv_string_buf_chk(varp, buf)
16883 typval_T *varp;
16884 char_u *buf;
16885{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016886 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016888 case VAR_NUMBER:
16889 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16890 return buf;
16891 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016892 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016893 break;
16894 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016895 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016896 break;
16897 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016898 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016899 break;
16900 case VAR_STRING:
16901 if (varp->vval.v_string != NULL)
16902 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016903 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016904 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016905 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016906 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016908 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909}
16910
16911/*
16912 * Find variable "name" in the list of variables.
16913 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016914 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016915 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016916 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016918 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016919find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016921 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016924 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925
Bram Moolenaara7043832005-01-21 11:56:39 +000016926 ht = find_var_ht(name, &varname);
16927 if (htp != NULL)
16928 *htp = ht;
16929 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016931 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932}
16933
16934/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016935 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016936 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016938 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016939find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016940 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016941 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016942 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016943{
Bram Moolenaar33570922005-01-25 22:26:29 +000016944 hashitem_T *hi;
16945
16946 if (*varname == NUL)
16947 {
16948 /* Must be something like "s:", otherwise "ht" would be NULL. */
16949 switch (varname[-2])
16950 {
16951 case 's': return &SCRIPT_SV(current_SID).sv_var;
16952 case 'g': return &globvars_var;
16953 case 'v': return &vimvars_var;
16954 case 'b': return &curbuf->b_bufvar;
16955 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016956 case 'l': return current_funccal == NULL
16957 ? NULL : &current_funccal->l_vars_var;
16958 case 'a': return current_funccal == NULL
16959 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016960 }
16961 return NULL;
16962 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016963
16964 hi = hash_find(ht, varname);
16965 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016966 {
16967 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016968 * worked find the variable again. Don't auto-load a script if it was
16969 * loaded already, otherwise it would be loaded every time when
16970 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016971 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016972 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016973 hi = hash_find(ht, varname);
16974 if (HASHITEM_EMPTY(hi))
16975 return NULL;
16976 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016977 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016978}
16979
16980/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016981 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016982 * Set "varname" to the start of name without ':'.
16983 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016984 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016985find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986 char_u *name;
16987 char_u **varname;
16988{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016989 hashitem_T *hi;
16990
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 if (name[1] != ':')
16992 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016993 /* The name must not start with a colon or #. */
16994 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995 return NULL;
16996 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016997
16998 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016999 hi = hash_find(&compat_hashtab, name);
17000 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017001 return &compat_hashtab;
17002
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017004 return &globvarht; /* global variable */
17005 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 }
17007 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017008 if (*name == 'g') /* global variable */
17009 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017010 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17011 */
17012 if (vim_strchr(name + 2, ':') != NULL
17013 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017014 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017016 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017018 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000017019 if (*name == 'v') /* v: variable */
17020 return &vimvarht;
17021 if (*name == 'a' && current_funccal != NULL) /* function argument */
17022 return &current_funccal->l_avars.dv_hashtab;
17023 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17024 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 if (*name == 's' /* script variable */
17026 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17027 return &SCRIPT_VARS(current_SID);
17028 return NULL;
17029}
17030
17031/*
17032 * Get the string value of a (global/local) variable.
17033 * Returns NULL when it doesn't exist.
17034 */
17035 char_u *
17036get_var_value(name)
17037 char_u *name;
17038{
Bram Moolenaar33570922005-01-25 22:26:29 +000017039 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040
Bram Moolenaara7043832005-01-21 11:56:39 +000017041 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 if (v == NULL)
17043 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017044 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045}
17046
17047/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017048 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049 * sourcing this script and when executing functions defined in the script.
17050 */
17051 void
17052new_script_vars(id)
17053 scid_T id;
17054{
Bram Moolenaara7043832005-01-21 11:56:39 +000017055 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017056 hashtab_T *ht;
17057 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017058
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17060 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017061 /* Re-allocating ga_data means that an ht_array pointing to
17062 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017063 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017064 for (i = 1; i <= ga_scripts.ga_len; ++i)
17065 {
17066 ht = &SCRIPT_VARS(i);
17067 if (ht->ht_mask == HT_INIT_SIZE - 1)
17068 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017069 sv = &SCRIPT_SV(i);
17070 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017071 }
17072
Bram Moolenaar071d4272004-06-13 20:20:40 +000017073 while (ga_scripts.ga_len < id)
17074 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017075 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17076 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017077 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078 }
17079 }
17080}
17081
17082/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017083 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17084 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 */
17086 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017087init_var_dict(dict, dict_var)
17088 dict_T *dict;
17089 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090{
Bram Moolenaar33570922005-01-25 22:26:29 +000017091 hash_init(&dict->dv_hashtab);
17092 dict->dv_refcount = 99999;
17093 dict_var->di_tv.vval.v_dict = dict;
17094 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017095 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017096 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17097 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098}
17099
17100/*
17101 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017102 * Frees all allocated variables and the value they contain.
17103 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104 */
17105 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017106vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017107 hashtab_T *ht;
17108{
17109 vars_clear_ext(ht, TRUE);
17110}
17111
17112/*
17113 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17114 */
17115 static void
17116vars_clear_ext(ht, free_val)
17117 hashtab_T *ht;
17118 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119{
Bram Moolenaara7043832005-01-21 11:56:39 +000017120 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017121 hashitem_T *hi;
17122 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123
Bram Moolenaar33570922005-01-25 22:26:29 +000017124 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000017125 todo = ht->ht_used;
17126 for (hi = ht->ht_array; todo > 0; ++hi)
17127 {
17128 if (!HASHITEM_EMPTY(hi))
17129 {
17130 --todo;
17131
Bram Moolenaar33570922005-01-25 22:26:29 +000017132 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017133 * ht_array might change then. hash_clear() takes care of it
17134 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017135 v = HI2DI(hi);
17136 if (free_val)
17137 clear_tv(&v->di_tv);
17138 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17139 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017140 }
17141 }
17142 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017143 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144}
17145
Bram Moolenaara7043832005-01-21 11:56:39 +000017146/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017147 * Delete a variable from hashtab "ht" at item "hi".
17148 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017151delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017152 hashtab_T *ht;
17153 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154{
Bram Moolenaar33570922005-01-25 22:26:29 +000017155 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017156
17157 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017158 clear_tv(&di->di_tv);
17159 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160}
17161
17162/*
17163 * List the value of one internal variable.
17164 */
17165 static void
17166list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017167 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017168 char_u *prefix;
17169{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017170 char_u *tofree;
17171 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017172 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017173
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017174 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017175 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017176 s == NULL ? (char_u *)"" : s);
17177 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178}
17179
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180 static void
17181list_one_var_a(prefix, name, type, string)
17182 char_u *prefix;
17183 char_u *name;
17184 int type;
17185 char_u *string;
17186{
17187 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17188 if (name != NULL) /* "a:" vars don't have a name stored */
17189 msg_puts(name);
17190 msg_putchar(' ');
17191 msg_advance(22);
17192 if (type == VAR_NUMBER)
17193 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017194 else if (type == VAR_FUNC)
17195 msg_putchar('*');
17196 else if (type == VAR_LIST)
17197 {
17198 msg_putchar('[');
17199 if (*string == '[')
17200 ++string;
17201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017202 else if (type == VAR_DICT)
17203 {
17204 msg_putchar('{');
17205 if (*string == '{')
17206 ++string;
17207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 else
17209 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017210
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017212
17213 if (type == VAR_FUNC)
17214 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215}
17216
17217/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017218 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219 * If the variable already exists, the value is updated.
17220 * Otherwise the variable is created.
17221 */
17222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017223set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017225 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017226 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227{
Bram Moolenaar33570922005-01-25 22:26:29 +000017228 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017230 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017231 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017233 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017234 {
17235 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17236 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17237 ? name[2] : name[0]))
17238 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017239 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017240 return;
17241 }
17242 if (function_exists(name))
17243 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017244 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017245 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017246 return;
17247 }
17248 }
17249
Bram Moolenaara7043832005-01-21 11:56:39 +000017250 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017251 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017252 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017253 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017254 return;
17255 }
17256
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017257 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017258 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017260 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017261 if (var_check_ro(v->di_flags, name)
17262 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017263 return;
17264 if (v->di_tv.v_type != tv->v_type
17265 && !((v->di_tv.v_type == VAR_STRING
17266 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017267 && (tv->v_type == VAR_STRING
17268 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017269 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017270 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017271 return;
17272 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017273
17274 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017275 * Handle setting internal v: variables separately: we don't change
17276 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017277 */
17278 if (ht == &vimvarht)
17279 {
17280 if (v->di_tv.v_type == VAR_STRING)
17281 {
17282 vim_free(v->di_tv.vval.v_string);
17283 if (copy || tv->v_type != VAR_STRING)
17284 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17285 else
17286 {
17287 /* Take over the string to avoid an extra alloc/free. */
17288 v->di_tv.vval.v_string = tv->vval.v_string;
17289 tv->vval.v_string = NULL;
17290 }
17291 }
17292 else if (v->di_tv.v_type != VAR_NUMBER)
17293 EMSG2(_(e_intern2), "set_var()");
17294 else
17295 v->di_tv.vval.v_number = get_tv_number(tv);
17296 return;
17297 }
17298
17299 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 }
17301 else /* add a new variable */
17302 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017303 /* Make sure the variable name is valid. */
17304 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017305 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17306 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017307 {
17308 EMSG2(_(e_illvar), varname);
17309 return;
17310 }
17311
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017312 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17313 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017314 if (v == NULL)
17315 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017316 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017317 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017319 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 return;
17321 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017322 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017325 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017326 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017327 else
17328 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017329 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017330 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017331 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333}
17334
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017335/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017336 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17337 * Also give an error message.
17338 */
17339 static int
17340var_check_ro(flags, name)
17341 int flags;
17342 char_u *name;
17343{
17344 if (flags & DI_FLAGS_RO)
17345 {
17346 EMSG2(_(e_readonlyvar), name);
17347 return TRUE;
17348 }
17349 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17350 {
17351 EMSG2(_(e_readonlysbx), name);
17352 return TRUE;
17353 }
17354 return FALSE;
17355}
17356
17357/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017358 * Return TRUE if typeval "tv" is set to be locked (immutable).
17359 * Also give an error message, using "name".
17360 */
17361 static int
17362tv_check_lock(lock, name)
17363 int lock;
17364 char_u *name;
17365{
17366 if (lock & VAR_LOCKED)
17367 {
17368 EMSG2(_("E741: Value is locked: %s"),
17369 name == NULL ? (char_u *)_("Unknown") : name);
17370 return TRUE;
17371 }
17372 if (lock & VAR_FIXED)
17373 {
17374 EMSG2(_("E742: Cannot change value of %s"),
17375 name == NULL ? (char_u *)_("Unknown") : name);
17376 return TRUE;
17377 }
17378 return FALSE;
17379}
17380
17381/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017382 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017383 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017384 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017387copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017388 typval_T *from;
17389 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017391 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017392 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017393 switch (from->v_type)
17394 {
17395 case VAR_NUMBER:
17396 to->vval.v_number = from->vval.v_number;
17397 break;
17398 case VAR_STRING:
17399 case VAR_FUNC:
17400 if (from->vval.v_string == NULL)
17401 to->vval.v_string = NULL;
17402 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017403 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017404 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017405 if (from->v_type == VAR_FUNC)
17406 func_ref(to->vval.v_string);
17407 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017408 break;
17409 case VAR_LIST:
17410 if (from->vval.v_list == NULL)
17411 to->vval.v_list = NULL;
17412 else
17413 {
17414 to->vval.v_list = from->vval.v_list;
17415 ++to->vval.v_list->lv_refcount;
17416 }
17417 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017418 case VAR_DICT:
17419 if (from->vval.v_dict == NULL)
17420 to->vval.v_dict = NULL;
17421 else
17422 {
17423 to->vval.v_dict = from->vval.v_dict;
17424 ++to->vval.v_dict->dv_refcount;
17425 }
17426 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017427 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017428 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017429 break;
17430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431}
17432
17433/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017434 * Make a copy of an item.
17435 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017436 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17437 * reference to an already copied list/dict can be used.
17438 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017439 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017440 static int
17441item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017442 typval_T *from;
17443 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017444 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017445 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017446{
17447 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017448 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017449
Bram Moolenaar33570922005-01-25 22:26:29 +000017450 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017451 {
17452 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017453 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017454 }
17455 ++recurse;
17456
17457 switch (from->v_type)
17458 {
17459 case VAR_NUMBER:
17460 case VAR_STRING:
17461 case VAR_FUNC:
17462 copy_tv(from, to);
17463 break;
17464 case VAR_LIST:
17465 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017466 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017467 if (from->vval.v_list == NULL)
17468 to->vval.v_list = NULL;
17469 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17470 {
17471 /* use the copy made earlier */
17472 to->vval.v_list = from->vval.v_list->lv_copylist;
17473 ++to->vval.v_list->lv_refcount;
17474 }
17475 else
17476 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17477 if (to->vval.v_list == NULL)
17478 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017479 break;
17480 case VAR_DICT:
17481 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017482 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017483 if (from->vval.v_dict == NULL)
17484 to->vval.v_dict = NULL;
17485 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17486 {
17487 /* use the copy made earlier */
17488 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17489 ++to->vval.v_dict->dv_refcount;
17490 }
17491 else
17492 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17493 if (to->vval.v_dict == NULL)
17494 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017495 break;
17496 default:
17497 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017498 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017499 }
17500 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017501 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017502}
17503
17504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 * ":echo expr1 ..." print each argument separated with a space, add a
17506 * newline at the end.
17507 * ":echon expr1 ..." print each argument plain.
17508 */
17509 void
17510ex_echo(eap)
17511 exarg_T *eap;
17512{
17513 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017514 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017515 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 char_u *p;
17517 int needclr = TRUE;
17518 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017519 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520
17521 if (eap->skip)
17522 ++emsg_skip;
17523 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17524 {
17525 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017526 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 {
17528 /*
17529 * Report the invalid expression unless the expression evaluation
17530 * has been cancelled due to an aborting error, an interrupt, or an
17531 * exception.
17532 */
17533 if (!aborting())
17534 EMSG2(_(e_invexpr2), p);
17535 break;
17536 }
17537 if (!eap->skip)
17538 {
17539 if (atstart)
17540 {
17541 atstart = FALSE;
17542 /* Call msg_start() after eval1(), evaluating the expression
17543 * may cause a message to appear. */
17544 if (eap->cmdidx == CMD_echo)
17545 msg_start();
17546 }
17547 else if (eap->cmdidx == CMD_echo)
17548 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017549 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017550 if (p != NULL)
17551 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017553 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017555 if (*p != TAB && needclr)
17556 {
17557 /* remove any text still there from the command */
17558 msg_clr_eos();
17559 needclr = FALSE;
17560 }
17561 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 }
17563 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017564 {
17565#ifdef FEAT_MBYTE
17566 if (has_mbyte)
17567 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017568 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017569
17570 (void)msg_outtrans_len_attr(p, i, echo_attr);
17571 p += i - 1;
17572 }
17573 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017575 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017578 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017580 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581 arg = skipwhite(arg);
17582 }
17583 eap->nextcmd = check_nextcmd(arg);
17584
17585 if (eap->skip)
17586 --emsg_skip;
17587 else
17588 {
17589 /* remove text that may still be there from the command */
17590 if (needclr)
17591 msg_clr_eos();
17592 if (eap->cmdidx == CMD_echo)
17593 msg_end();
17594 }
17595}
17596
17597/*
17598 * ":echohl {name}".
17599 */
17600 void
17601ex_echohl(eap)
17602 exarg_T *eap;
17603{
17604 int id;
17605
17606 id = syn_name2id(eap->arg);
17607 if (id == 0)
17608 echo_attr = 0;
17609 else
17610 echo_attr = syn_id2attr(id);
17611}
17612
17613/*
17614 * ":execute expr1 ..." execute the result of an expression.
17615 * ":echomsg expr1 ..." Print a message
17616 * ":echoerr expr1 ..." Print an error
17617 * Each gets spaces around each argument and a newline at the end for
17618 * echo commands
17619 */
17620 void
17621ex_execute(eap)
17622 exarg_T *eap;
17623{
17624 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017625 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626 int ret = OK;
17627 char_u *p;
17628 garray_T ga;
17629 int len;
17630 int save_did_emsg;
17631
17632 ga_init2(&ga, 1, 80);
17633
17634 if (eap->skip)
17635 ++emsg_skip;
17636 while (*arg != NUL && *arg != '|' && *arg != '\n')
17637 {
17638 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017639 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 {
17641 /*
17642 * Report the invalid expression unless the expression evaluation
17643 * has been cancelled due to an aborting error, an interrupt, or an
17644 * exception.
17645 */
17646 if (!aborting())
17647 EMSG2(_(e_invexpr2), p);
17648 ret = FAIL;
17649 break;
17650 }
17651
17652 if (!eap->skip)
17653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017654 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 len = (int)STRLEN(p);
17656 if (ga_grow(&ga, len + 2) == FAIL)
17657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017658 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659 ret = FAIL;
17660 break;
17661 }
17662 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 ga.ga_len += len;
17666 }
17667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017668 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 arg = skipwhite(arg);
17670 }
17671
17672 if (ret != FAIL && ga.ga_data != NULL)
17673 {
17674 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017675 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017677 out_flush();
17678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679 else if (eap->cmdidx == CMD_echoerr)
17680 {
17681 /* We don't want to abort following commands, restore did_emsg. */
17682 save_did_emsg = did_emsg;
17683 EMSG((char_u *)ga.ga_data);
17684 if (!force_abort)
17685 did_emsg = save_did_emsg;
17686 }
17687 else if (eap->cmdidx == CMD_execute)
17688 do_cmdline((char_u *)ga.ga_data,
17689 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17690 }
17691
17692 ga_clear(&ga);
17693
17694 if (eap->skip)
17695 --emsg_skip;
17696
17697 eap->nextcmd = check_nextcmd(arg);
17698}
17699
17700/*
17701 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17702 * "arg" points to the "&" or '+' when called, to "option" when returning.
17703 * Returns NULL when no option name found. Otherwise pointer to the char
17704 * after the option name.
17705 */
17706 static char_u *
17707find_option_end(arg, opt_flags)
17708 char_u **arg;
17709 int *opt_flags;
17710{
17711 char_u *p = *arg;
17712
17713 ++p;
17714 if (*p == 'g' && p[1] == ':')
17715 {
17716 *opt_flags = OPT_GLOBAL;
17717 p += 2;
17718 }
17719 else if (*p == 'l' && p[1] == ':')
17720 {
17721 *opt_flags = OPT_LOCAL;
17722 p += 2;
17723 }
17724 else
17725 *opt_flags = 0;
17726
17727 if (!ASCII_ISALPHA(*p))
17728 return NULL;
17729 *arg = p;
17730
17731 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17732 p += 4; /* termcap option */
17733 else
17734 while (ASCII_ISALPHA(*p))
17735 ++p;
17736 return p;
17737}
17738
17739/*
17740 * ":function"
17741 */
17742 void
17743ex_function(eap)
17744 exarg_T *eap;
17745{
17746 char_u *theline;
17747 int j;
17748 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 char_u *name = NULL;
17751 char_u *p;
17752 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017753 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754 garray_T newargs;
17755 garray_T newlines;
17756 int varargs = FALSE;
17757 int mustend = FALSE;
17758 int flags = 0;
17759 ufunc_T *fp;
17760 int indent;
17761 int nesting;
17762 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017763 dictitem_T *v;
17764 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017765 static int func_nr = 0; /* number for nameless function */
17766 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017767 hashtab_T *ht;
17768 int todo;
17769 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017770 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771
17772 /*
17773 * ":function" without argument: list functions.
17774 */
17775 if (ends_excmd(*eap->arg))
17776 {
17777 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017778 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017779 todo = func_hashtab.ht_used;
17780 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017781 {
17782 if (!HASHITEM_EMPTY(hi))
17783 {
17784 --todo;
17785 fp = HI2UF(hi);
17786 if (!isdigit(*fp->uf_name))
17787 list_func_head(fp, FALSE);
17788 }
17789 }
17790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 eap->nextcmd = check_nextcmd(eap->arg);
17792 return;
17793 }
17794
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017795 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017796 * ":function /pat": list functions matching pattern.
17797 */
17798 if (*eap->arg == '/')
17799 {
17800 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17801 if (!eap->skip)
17802 {
17803 regmatch_T regmatch;
17804
17805 c = *p;
17806 *p = NUL;
17807 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17808 *p = c;
17809 if (regmatch.regprog != NULL)
17810 {
17811 regmatch.rm_ic = p_ic;
17812
17813 todo = func_hashtab.ht_used;
17814 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17815 {
17816 if (!HASHITEM_EMPTY(hi))
17817 {
17818 --todo;
17819 fp = HI2UF(hi);
17820 if (!isdigit(*fp->uf_name)
17821 && vim_regexec(&regmatch, fp->uf_name, 0))
17822 list_func_head(fp, FALSE);
17823 }
17824 }
17825 }
17826 }
17827 if (*p == '/')
17828 ++p;
17829 eap->nextcmd = check_nextcmd(p);
17830 return;
17831 }
17832
17833 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017834 * Get the function name. There are these situations:
17835 * func normal function name
17836 * "name" == func, "fudi.fd_dict" == NULL
17837 * dict.func new dictionary entry
17838 * "name" == NULL, "fudi.fd_dict" set,
17839 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17840 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017841 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017842 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17843 * dict.func existing dict entry that's not a Funcref
17844 * "name" == NULL, "fudi.fd_dict" set,
17845 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017848 name = trans_function_name(&p, eap->skip, 0, &fudi);
17849 paren = (vim_strchr(p, '(') != NULL);
17850 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851 {
17852 /*
17853 * Return on an invalid expression in braces, unless the expression
17854 * evaluation has been cancelled due to an aborting error, an
17855 * interrupt, or an exception.
17856 */
17857 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017858 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017859 if (!eap->skip && fudi.fd_newkey != NULL)
17860 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017861 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864 else
17865 eap->skip = TRUE;
17866 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000017867
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 /* An error in a function call during evaluation of an expression in magic
17869 * braces should not cause the function not to be defined. */
17870 saved_did_emsg = did_emsg;
17871 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872
17873 /*
17874 * ":function func" with only function name: list function.
17875 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017876 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877 {
17878 if (!ends_excmd(*skipwhite(p)))
17879 {
17880 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017881 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882 }
17883 eap->nextcmd = check_nextcmd(p);
17884 if (eap->nextcmd != NULL)
17885 *p = NUL;
17886 if (!eap->skip && !got_int)
17887 {
17888 fp = find_func(name);
17889 if (fp != NULL)
17890 {
17891 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017892 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017894 if (FUNCLINE(fp, j) == NULL)
17895 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 msg_putchar('\n');
17897 msg_outnum((long)(j + 1));
17898 if (j < 9)
17899 msg_putchar(' ');
17900 if (j < 99)
17901 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017902 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 out_flush(); /* show a line at a time */
17904 ui_breakcheck();
17905 }
17906 if (!got_int)
17907 {
17908 msg_putchar('\n');
17909 msg_puts((char_u *)" endfunction");
17910 }
17911 }
17912 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017913 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017915 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 }
17917
17918 /*
17919 * ":function name(arg1, arg2)" Define function.
17920 */
17921 p = skipwhite(p);
17922 if (*p != '(')
17923 {
17924 if (!eap->skip)
17925 {
17926 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017927 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928 }
17929 /* attempt to continue by skipping some text */
17930 if (vim_strchr(p, '(') != NULL)
17931 p = vim_strchr(p, '(');
17932 }
17933 p = skipwhite(p + 1);
17934
17935 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17936 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17937
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017938 if (!eap->skip)
17939 {
17940 /* Check the name of the function. */
17941 if (name != NULL)
17942 arg = name;
17943 else
17944 arg = fudi.fd_newkey;
17945 if (arg != NULL)
17946 {
17947 if (*arg == K_SPECIAL)
17948 j = 3;
17949 else
17950 j = 0;
17951 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17952 : eval_isnamec(arg[j])))
17953 ++j;
17954 if (arg[j] != NUL)
17955 emsg_funcname(_(e_invarg2), arg);
17956 }
17957 }
17958
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959 /*
17960 * Isolate the arguments: "arg1, arg2, ...)"
17961 */
17962 while (*p != ')')
17963 {
17964 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17965 {
17966 varargs = TRUE;
17967 p += 3;
17968 mustend = TRUE;
17969 }
17970 else
17971 {
17972 arg = p;
17973 while (ASCII_ISALNUM(*p) || *p == '_')
17974 ++p;
17975 if (arg == p || isdigit(*arg)
17976 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17977 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17978 {
17979 if (!eap->skip)
17980 EMSG2(_("E125: Illegal argument: %s"), arg);
17981 break;
17982 }
17983 if (ga_grow(&newargs, 1) == FAIL)
17984 goto erret;
17985 c = *p;
17986 *p = NUL;
17987 arg = vim_strsave(arg);
17988 if (arg == NULL)
17989 goto erret;
17990 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17991 *p = c;
17992 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993 if (*p == ',')
17994 ++p;
17995 else
17996 mustend = TRUE;
17997 }
17998 p = skipwhite(p);
17999 if (mustend && *p != ')')
18000 {
18001 if (!eap->skip)
18002 EMSG2(_(e_invarg2), eap->arg);
18003 break;
18004 }
18005 }
18006 ++p; /* skip the ')' */
18007
Bram Moolenaare9a41262005-01-15 22:18:47 +000018008 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 for (;;)
18010 {
18011 p = skipwhite(p);
18012 if (STRNCMP(p, "range", 5) == 0)
18013 {
18014 flags |= FC_RANGE;
18015 p += 5;
18016 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018017 else if (STRNCMP(p, "dict", 4) == 0)
18018 {
18019 flags |= FC_DICT;
18020 p += 4;
18021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 else if (STRNCMP(p, "abort", 5) == 0)
18023 {
18024 flags |= FC_ABORT;
18025 p += 5;
18026 }
18027 else
18028 break;
18029 }
18030
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018031 /* When there is a line break use what follows for the function body.
18032 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18033 if (*p == '\n')
18034 line_arg = p + 1;
18035 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 EMSG(_(e_trailing));
18037
18038 /*
18039 * Read the body of the function, until ":endfunction" is found.
18040 */
18041 if (KeyTyped)
18042 {
18043 /* Check if the function already exists, don't let the user type the
18044 * whole function before telling him it doesn't work! For a script we
18045 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018046 if (!eap->skip && !eap->forceit)
18047 {
18048 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18049 EMSG(_(e_funcdict));
18050 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018051 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018054 if (!eap->skip && did_emsg)
18055 goto erret;
18056
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057 msg_putchar('\n'); /* don't overwrite the function name */
18058 cmdline_row = msg_row;
18059 }
18060
18061 indent = 2;
18062 nesting = 0;
18063 for (;;)
18064 {
18065 msg_scroll = TRUE;
18066 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018067 sourcing_lnum_off = sourcing_lnum;
18068
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018069 if (line_arg != NULL)
18070 {
18071 /* Use eap->arg, split up in parts by line breaks. */
18072 theline = line_arg;
18073 p = vim_strchr(theline, '\n');
18074 if (p == NULL)
18075 line_arg += STRLEN(line_arg);
18076 else
18077 {
18078 *p = NUL;
18079 line_arg = p + 1;
18080 }
18081 }
18082 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083 theline = getcmdline(':', 0L, indent);
18084 else
18085 theline = eap->getline(':', eap->cookie, indent);
18086 if (KeyTyped)
18087 lines_left = Rows - 1;
18088 if (theline == NULL)
18089 {
18090 EMSG(_("E126: Missing :endfunction"));
18091 goto erret;
18092 }
18093
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018094 /* Detect line continuation: sourcing_lnum increased more than one. */
18095 if (sourcing_lnum > sourcing_lnum_off + 1)
18096 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18097 else
18098 sourcing_lnum_off = 0;
18099
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100 if (skip_until != NULL)
18101 {
18102 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18103 * don't check for ":endfunc". */
18104 if (STRCMP(theline, skip_until) == 0)
18105 {
18106 vim_free(skip_until);
18107 skip_until = NULL;
18108 }
18109 }
18110 else
18111 {
18112 /* skip ':' and blanks*/
18113 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18114 ;
18115
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018116 /* Check for "endfunction". */
18117 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018119 if (line_arg == NULL)
18120 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121 break;
18122 }
18123
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018124 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 * at "end". */
18126 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18127 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018128 else if (STRNCMP(p, "if", 2) == 0
18129 || STRNCMP(p, "wh", 2) == 0
18130 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 || STRNCMP(p, "try", 3) == 0)
18132 indent += 2;
18133
18134 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018135 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018137 if (*p == '!')
18138 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 p += eval_fname_script(p);
18140 if (ASCII_ISALPHA(*p))
18141 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018142 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143 if (*skipwhite(p) == '(')
18144 {
18145 ++nesting;
18146 indent += 2;
18147 }
18148 }
18149 }
18150
18151 /* Check for ":append" or ":insert". */
18152 p = skip_range(p, NULL);
18153 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18154 || (p[0] == 'i'
18155 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18156 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18157 skip_until = vim_strsave((char_u *)".");
18158
18159 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18160 arg = skipwhite(skiptowhite(p));
18161 if (arg[0] == '<' && arg[1] =='<'
18162 && ((p[0] == 'p' && p[1] == 'y'
18163 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18164 || (p[0] == 'p' && p[1] == 'e'
18165 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18166 || (p[0] == 't' && p[1] == 'c'
18167 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18168 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18169 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018170 || (p[0] == 'm' && p[1] == 'z'
18171 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 ))
18173 {
18174 /* ":python <<" continues until a dot, like ":append" */
18175 p = skipwhite(arg + 2);
18176 if (*p == NUL)
18177 skip_until = vim_strsave((char_u *)".");
18178 else
18179 skip_until = vim_strsave(p);
18180 }
18181 }
18182
18183 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018184 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018185 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018186 if (line_arg == NULL)
18187 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018189 }
18190
18191 /* Copy the line to newly allocated memory. get_one_sourceline()
18192 * allocates 250 bytes per line, this saves 80% on average. The cost
18193 * is an extra alloc/free. */
18194 p = vim_strsave(theline);
18195 if (p != NULL)
18196 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018197 if (line_arg == NULL)
18198 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018199 theline = p;
18200 }
18201
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018202 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18203
18204 /* Add NULL lines for continuation lines, so that the line count is
18205 * equal to the index in the growarray. */
18206 while (sourcing_lnum_off-- > 0)
18207 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018208
18209 /* Check for end of eap->arg. */
18210 if (line_arg != NULL && *line_arg == NUL)
18211 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 }
18213
18214 /* Don't define the function when skipping commands or when an error was
18215 * detected. */
18216 if (eap->skip || did_emsg)
18217 goto erret;
18218
18219 /*
18220 * If there are no errors, add the function
18221 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018222 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018223 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018224 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018225 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018226 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018227 emsg_funcname("E707: Function name conflicts with variable: %s",
18228 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018229 goto erret;
18230 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018231
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018232 fp = find_func(name);
18233 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018235 if (!eap->forceit)
18236 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018237 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018238 goto erret;
18239 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018240 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018241 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018242 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018243 name);
18244 goto erret;
18245 }
18246 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018247 ga_clear_strings(&(fp->uf_args));
18248 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018249 vim_free(name);
18250 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252 }
18253 else
18254 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018255 char numbuf[20];
18256
18257 fp = NULL;
18258 if (fudi.fd_newkey == NULL && !eap->forceit)
18259 {
18260 EMSG(_(e_funcdict));
18261 goto erret;
18262 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018263 if (fudi.fd_di == NULL)
18264 {
18265 /* Can't add a function to a locked dictionary */
18266 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18267 goto erret;
18268 }
18269 /* Can't change an existing function if it is locked */
18270 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18271 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018272
18273 /* Give the function a sequential number. Can only be used with a
18274 * Funcref! */
18275 vim_free(name);
18276 sprintf(numbuf, "%d", ++func_nr);
18277 name = vim_strsave((char_u *)numbuf);
18278 if (name == NULL)
18279 goto erret;
18280 }
18281
18282 if (fp == NULL)
18283 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018284 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018285 {
18286 int slen, plen;
18287 char_u *scriptname;
18288
18289 /* Check that the autoload name matches the script name. */
18290 j = FAIL;
18291 if (sourcing_name != NULL)
18292 {
18293 scriptname = autoload_name(name);
18294 if (scriptname != NULL)
18295 {
18296 p = vim_strchr(scriptname, '/');
18297 plen = STRLEN(p);
18298 slen = STRLEN(sourcing_name);
18299 if (slen > plen && fnamecmp(p,
18300 sourcing_name + slen - plen) == 0)
18301 j = OK;
18302 vim_free(scriptname);
18303 }
18304 }
18305 if (j == FAIL)
18306 {
18307 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18308 goto erret;
18309 }
18310 }
18311
18312 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313 if (fp == NULL)
18314 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018315
18316 if (fudi.fd_dict != NULL)
18317 {
18318 if (fudi.fd_di == NULL)
18319 {
18320 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018321 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018322 if (fudi.fd_di == NULL)
18323 {
18324 vim_free(fp);
18325 goto erret;
18326 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018327 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18328 {
18329 vim_free(fudi.fd_di);
18330 goto erret;
18331 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018332 }
18333 else
18334 /* overwrite existing dict entry */
18335 clear_tv(&fudi.fd_di->di_tv);
18336 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018337 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018338 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018339 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018340 }
18341
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018343 STRCPY(fp->uf_name, name);
18344 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018346 fp->uf_args = newargs;
18347 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018348#ifdef FEAT_PROFILE
18349 fp->uf_tml_count = NULL;
18350 fp->uf_tml_total = NULL;
18351 fp->uf_tml_self = NULL;
18352 fp->uf_profiling = FALSE;
18353 if (prof_def_func())
18354 func_do_profile(fp);
18355#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018356 fp->uf_varargs = varargs;
18357 fp->uf_flags = flags;
18358 fp->uf_calls = 0;
18359 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018360 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361
18362erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 ga_clear_strings(&newargs);
18364 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018365ret_free:
18366 vim_free(skip_until);
18367 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370}
18371
18372/*
18373 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018374 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018376 * flags:
18377 * TFN_INT: internal function name OK
18378 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 * Advances "pp" to just after the function name (if no error).
18380 */
18381 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018382trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383 char_u **pp;
18384 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018385 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018386 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018388 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389 char_u *start;
18390 char_u *end;
18391 int lead;
18392 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018394 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018395
18396 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018397 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018399
18400 /* Check for hard coded <SNR>: already translated function ID (from a user
18401 * command). */
18402 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18403 && (*pp)[2] == (int)KE_SNR)
18404 {
18405 *pp += 3;
18406 len = get_id_len(pp) + 3;
18407 return vim_strnsave(start, len);
18408 }
18409
18410 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18411 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018413 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018415
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018416 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18417 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018418 if (end == start)
18419 {
18420 if (!skip)
18421 EMSG(_("E129: Function name required"));
18422 goto theend;
18423 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018424 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018425 {
18426 /*
18427 * Report an invalid expression in braces, unless the expression
18428 * evaluation has been cancelled due to an aborting error, an
18429 * interrupt, or an exception.
18430 */
18431 if (!aborting())
18432 {
18433 if (end != NULL)
18434 EMSG2(_(e_invarg2), start);
18435 }
18436 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018437 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018438 goto theend;
18439 }
18440
18441 if (lv.ll_tv != NULL)
18442 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018443 if (fdp != NULL)
18444 {
18445 fdp->fd_dict = lv.ll_dict;
18446 fdp->fd_newkey = lv.ll_newkey;
18447 lv.ll_newkey = NULL;
18448 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018449 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018450 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18451 {
18452 name = vim_strsave(lv.ll_tv->vval.v_string);
18453 *pp = end;
18454 }
18455 else
18456 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018457 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18458 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018459 EMSG(_(e_funcref));
18460 else
18461 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018462 name = NULL;
18463 }
18464 goto theend;
18465 }
18466
18467 if (lv.ll_name == NULL)
18468 {
18469 /* Error found, but continue after the function name. */
18470 *pp = end;
18471 goto theend;
18472 }
18473
18474 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018475 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018476 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018477 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18478 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18479 {
18480 /* When there was "s:" already or the name expanded to get a
18481 * leading "s:" then remove it. */
18482 lv.ll_name += 2;
18483 len -= 2;
18484 lead = 2;
18485 }
18486 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018487 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018488 {
18489 if (lead == 2) /* skip over "s:" */
18490 lv.ll_name += 2;
18491 len = (int)(end - lv.ll_name);
18492 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018493
18494 /*
18495 * Copy the function name to allocated memory.
18496 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18497 * Accept <SNR>123_name() outside a script.
18498 */
18499 if (skip)
18500 lead = 0; /* do nothing */
18501 else if (lead > 0)
18502 {
18503 lead = 3;
18504 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18505 {
18506 if (current_SID <= 0)
18507 {
18508 EMSG(_(e_usingsid));
18509 goto theend;
18510 }
18511 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18512 lead += (int)STRLEN(sid_buf);
18513 }
18514 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018515 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018516 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018517 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018518 goto theend;
18519 }
18520 name = alloc((unsigned)(len + lead + 1));
18521 if (name != NULL)
18522 {
18523 if (lead > 0)
18524 {
18525 name[0] = K_SPECIAL;
18526 name[1] = KS_EXTRA;
18527 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018528 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018529 STRCPY(name + 3, sid_buf);
18530 }
18531 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18532 name[len + lead] = NUL;
18533 }
18534 *pp = end;
18535
18536theend:
18537 clear_lval(&lv);
18538 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539}
18540
18541/*
18542 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18543 * Return 2 if "p" starts with "s:".
18544 * Return 0 otherwise.
18545 */
18546 static int
18547eval_fname_script(p)
18548 char_u *p;
18549{
18550 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18551 || STRNICMP(p + 1, "SNR>", 4) == 0))
18552 return 5;
18553 if (p[0] == 's' && p[1] == ':')
18554 return 2;
18555 return 0;
18556}
18557
18558/*
18559 * Return TRUE if "p" starts with "<SID>" or "s:".
18560 * Only works if eval_fname_script() returned non-zero for "p"!
18561 */
18562 static int
18563eval_fname_sid(p)
18564 char_u *p;
18565{
18566 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18567}
18568
18569/*
18570 * List the head of the function: "name(arg1, arg2)".
18571 */
18572 static void
18573list_func_head(fp, indent)
18574 ufunc_T *fp;
18575 int indent;
18576{
18577 int j;
18578
18579 msg_start();
18580 if (indent)
18581 MSG_PUTS(" ");
18582 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018583 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 {
18585 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018586 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 }
18588 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018589 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018591 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 {
18593 if (j)
18594 MSG_PUTS(", ");
18595 msg_puts(FUNCARG(fp, j));
18596 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018597 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 {
18599 if (j)
18600 MSG_PUTS(", ");
18601 MSG_PUTS("...");
18602 }
18603 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018604 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018605 if (p_verbose > 0)
18606 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607}
18608
18609/*
18610 * Find a function by name, return pointer to it in ufuncs.
18611 * Return NULL for unknown function.
18612 */
18613 static ufunc_T *
18614find_func(name)
18615 char_u *name;
18616{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018617 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018619 hi = hash_find(&func_hashtab, name);
18620 if (!HASHITEM_EMPTY(hi))
18621 return HI2UF(hi);
18622 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623}
18624
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018625#if defined(EXITFREE) || defined(PROTO)
18626 void
18627free_all_functions()
18628{
18629 hashitem_T *hi;
18630
18631 /* Need to start all over every time, because func_free() may change the
18632 * hash table. */
18633 while (func_hashtab.ht_used > 0)
18634 for (hi = func_hashtab.ht_array; ; ++hi)
18635 if (!HASHITEM_EMPTY(hi))
18636 {
18637 func_free(HI2UF(hi));
18638 break;
18639 }
18640}
18641#endif
18642
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018643/*
18644 * Return TRUE if a function "name" exists.
18645 */
18646 static int
18647function_exists(name)
18648 char_u *name;
18649{
18650 char_u *p = name;
18651 int n = FALSE;
18652
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018653 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018654 if (p != NULL)
18655 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018656 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018657 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018658 else
18659 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018660 vim_free(p);
18661 }
18662 return n;
18663}
18664
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018665/*
18666 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018667 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018668 */
18669 static int
18670builtin_function(name)
18671 char_u *name;
18672{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018673 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18674 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018675}
18676
Bram Moolenaar05159a02005-02-26 23:04:13 +000018677#if defined(FEAT_PROFILE) || defined(PROTO)
18678/*
18679 * Start profiling function "fp".
18680 */
18681 static void
18682func_do_profile(fp)
18683 ufunc_T *fp;
18684{
18685 fp->uf_tm_count = 0;
18686 profile_zero(&fp->uf_tm_self);
18687 profile_zero(&fp->uf_tm_total);
18688 if (fp->uf_tml_count == NULL)
18689 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18690 (sizeof(int) * fp->uf_lines.ga_len));
18691 if (fp->uf_tml_total == NULL)
18692 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18693 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18694 if (fp->uf_tml_self == NULL)
18695 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18696 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18697 fp->uf_tml_idx = -1;
18698 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18699 || fp->uf_tml_self == NULL)
18700 return; /* out of memory */
18701
18702 fp->uf_profiling = TRUE;
18703}
18704
18705/*
18706 * Dump the profiling results for all functions in file "fd".
18707 */
18708 void
18709func_dump_profile(fd)
18710 FILE *fd;
18711{
18712 hashitem_T *hi;
18713 int todo;
18714 ufunc_T *fp;
18715 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018716 ufunc_T **sorttab;
18717 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018718
18719 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018720 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18721
Bram Moolenaar05159a02005-02-26 23:04:13 +000018722 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18723 {
18724 if (!HASHITEM_EMPTY(hi))
18725 {
18726 --todo;
18727 fp = HI2UF(hi);
18728 if (fp->uf_profiling)
18729 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018730 if (sorttab != NULL)
18731 sorttab[st_len++] = fp;
18732
Bram Moolenaar05159a02005-02-26 23:04:13 +000018733 if (fp->uf_name[0] == K_SPECIAL)
18734 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18735 else
18736 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18737 if (fp->uf_tm_count == 1)
18738 fprintf(fd, "Called 1 time\n");
18739 else
18740 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18741 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18742 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18743 fprintf(fd, "\n");
18744 fprintf(fd, "count total (s) self (s)\n");
18745
18746 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18747 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018748 if (FUNCLINE(fp, i) == NULL)
18749 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000018750 prof_func_line(fd, fp->uf_tml_count[i],
18751 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018752 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18753 }
18754 fprintf(fd, "\n");
18755 }
18756 }
18757 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018758
18759 if (sorttab != NULL && st_len > 0)
18760 {
18761 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18762 prof_total_cmp);
18763 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18764 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18765 prof_self_cmp);
18766 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18767 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018768}
Bram Moolenaar73830342005-02-28 22:48:19 +000018769
18770 static void
18771prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18772 FILE *fd;
18773 ufunc_T **sorttab;
18774 int st_len;
18775 char *title;
18776 int prefer_self; /* when equal print only self time */
18777{
18778 int i;
18779 ufunc_T *fp;
18780
18781 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18782 fprintf(fd, "count total (s) self (s) function\n");
18783 for (i = 0; i < 20 && i < st_len; ++i)
18784 {
18785 fp = sorttab[i];
18786 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18787 prefer_self);
18788 if (fp->uf_name[0] == K_SPECIAL)
18789 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18790 else
18791 fprintf(fd, " %s()\n", fp->uf_name);
18792 }
18793 fprintf(fd, "\n");
18794}
18795
18796/*
18797 * Print the count and times for one function or function line.
18798 */
18799 static void
18800prof_func_line(fd, count, total, self, prefer_self)
18801 FILE *fd;
18802 int count;
18803 proftime_T *total;
18804 proftime_T *self;
18805 int prefer_self; /* when equal print only self time */
18806{
18807 if (count > 0)
18808 {
18809 fprintf(fd, "%5d ", count);
18810 if (prefer_self && profile_equal(total, self))
18811 fprintf(fd, " ");
18812 else
18813 fprintf(fd, "%s ", profile_msg(total));
18814 if (!prefer_self && profile_equal(total, self))
18815 fprintf(fd, " ");
18816 else
18817 fprintf(fd, "%s ", profile_msg(self));
18818 }
18819 else
18820 fprintf(fd, " ");
18821}
18822
18823/*
18824 * Compare function for total time sorting.
18825 */
18826 static int
18827#ifdef __BORLANDC__
18828_RTLENTRYF
18829#endif
18830prof_total_cmp(s1, s2)
18831 const void *s1;
18832 const void *s2;
18833{
18834 ufunc_T *p1, *p2;
18835
18836 p1 = *(ufunc_T **)s1;
18837 p2 = *(ufunc_T **)s2;
18838 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18839}
18840
18841/*
18842 * Compare function for self time sorting.
18843 */
18844 static int
18845#ifdef __BORLANDC__
18846_RTLENTRYF
18847#endif
18848prof_self_cmp(s1, s2)
18849 const void *s1;
18850 const void *s2;
18851{
18852 ufunc_T *p1, *p2;
18853
18854 p1 = *(ufunc_T **)s1;
18855 p2 = *(ufunc_T **)s2;
18856 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18857}
18858
Bram Moolenaar05159a02005-02-26 23:04:13 +000018859#endif
18860
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018861/* The names of packages that once were loaded is remembered. */
18862static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18863
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018864/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018865 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018866 * Return TRUE if a package was loaded.
18867 */
18868 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018869script_autoload(name, reload)
18870 char_u *name;
18871 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018872{
18873 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018874 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018875 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018876 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018877
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018878 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018879 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018880 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018881 return FALSE;
18882
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018883 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018884
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018885 /* Find the name in the list of previously loaded package names. Skip
18886 * "autoload/", it's always the same. */
18887 for (i = 0; i < ga_loaded.ga_len; ++i)
18888 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18889 break;
18890 if (!reload && i < ga_loaded.ga_len)
18891 ret = FALSE; /* was loaded already */
18892 else
18893 {
18894 /* Remember the name if it wasn't loaded already. */
18895 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18896 {
18897 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18898 tofree = NULL;
18899 }
18900
18901 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018902 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018903 ret = TRUE;
18904 }
18905
18906 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018907 return ret;
18908}
18909
18910/*
18911 * Return the autoload script name for a function or variable name.
18912 * Returns NULL when out of memory.
18913 */
18914 static char_u *
18915autoload_name(name)
18916 char_u *name;
18917{
18918 char_u *p;
18919 char_u *scriptname;
18920
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018921 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018922 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18923 if (scriptname == NULL)
18924 return FALSE;
18925 STRCPY(scriptname, "autoload/");
18926 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018927 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018928 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018929 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018930 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018931 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018932}
18933
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18935
18936/*
18937 * Function given to ExpandGeneric() to obtain the list of user defined
18938 * function names.
18939 */
18940 char_u *
18941get_user_func_name(xp, idx)
18942 expand_T *xp;
18943 int idx;
18944{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018945 static long_u done;
18946 static hashitem_T *hi;
18947 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948
18949 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018950 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018951 done = 0;
18952 hi = func_hashtab.ht_array;
18953 }
18954 if (done < func_hashtab.ht_used)
18955 {
18956 if (done++ > 0)
18957 ++hi;
18958 while (HASHITEM_EMPTY(hi))
18959 ++hi;
18960 fp = HI2UF(hi);
18961
18962 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18963 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964
18965 cat_func_name(IObuff, fp);
18966 if (xp->xp_context != EXPAND_USER_FUNC)
18967 {
18968 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018969 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 STRCAT(IObuff, ")");
18971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 return IObuff;
18973 }
18974 return NULL;
18975}
18976
18977#endif /* FEAT_CMDL_COMPL */
18978
18979/*
18980 * Copy the function name of "fp" to buffer "buf".
18981 * "buf" must be able to hold the function name plus three bytes.
18982 * Takes care of script-local function names.
18983 */
18984 static void
18985cat_func_name(buf, fp)
18986 char_u *buf;
18987 ufunc_T *fp;
18988{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018989 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 {
18991 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018992 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993 }
18994 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018995 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996}
18997
18998/*
18999 * ":delfunction {name}"
19000 */
19001 void
19002ex_delfunction(eap)
19003 exarg_T *eap;
19004{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019005 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006 char_u *p;
19007 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019008 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009
19010 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019011 name = trans_function_name(&p, eap->skip, 0, &fudi);
19012 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019014 {
19015 if (fudi.fd_dict != NULL && !eap->skip)
19016 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019 if (!ends_excmd(*skipwhite(p)))
19020 {
19021 vim_free(name);
19022 EMSG(_(e_trailing));
19023 return;
19024 }
19025 eap->nextcmd = check_nextcmd(p);
19026 if (eap->nextcmd != NULL)
19027 *p = NUL;
19028
19029 if (!eap->skip)
19030 fp = find_func(name);
19031 vim_free(name);
19032
19033 if (!eap->skip)
19034 {
19035 if (fp == NULL)
19036 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019037 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038 return;
19039 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019040 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 {
19042 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19043 return;
19044 }
19045
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019046 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019048 /* Delete the dict item that refers to the function, it will
19049 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019050 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019052 else
19053 func_free(fp);
19054 }
19055}
19056
19057/*
19058 * Free a function and remove it from the list of functions.
19059 */
19060 static void
19061func_free(fp)
19062 ufunc_T *fp;
19063{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019064 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019065
19066 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019067 ga_clear_strings(&(fp->uf_args));
19068 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019069#ifdef FEAT_PROFILE
19070 vim_free(fp->uf_tml_count);
19071 vim_free(fp->uf_tml_total);
19072 vim_free(fp->uf_tml_self);
19073#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019074
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019075 /* remove the function from the function hashtable */
19076 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19077 if (HASHITEM_EMPTY(hi))
19078 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019079 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019080 hash_remove(&func_hashtab, hi);
19081
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019082 vim_free(fp);
19083}
19084
19085/*
19086 * Unreference a Function: decrement the reference count and free it when it
19087 * becomes zero. Only for numbered functions.
19088 */
19089 static void
19090func_unref(name)
19091 char_u *name;
19092{
19093 ufunc_T *fp;
19094
19095 if (name != NULL && isdigit(*name))
19096 {
19097 fp = find_func(name);
19098 if (fp == NULL)
19099 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019100 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019101 {
19102 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019103 * when "uf_calls" becomes zero. */
19104 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019105 func_free(fp);
19106 }
19107 }
19108}
19109
19110/*
19111 * Count a reference to a Function.
19112 */
19113 static void
19114func_ref(name)
19115 char_u *name;
19116{
19117 ufunc_T *fp;
19118
19119 if (name != NULL && isdigit(*name))
19120 {
19121 fp = find_func(name);
19122 if (fp == NULL)
19123 EMSG2(_(e_intern2), "func_ref()");
19124 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019125 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126 }
19127}
19128
19129/*
19130 * Call a user function.
19131 */
19132 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019133call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 ufunc_T *fp; /* pointer to function */
19135 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019136 typval_T *argvars; /* arguments */
19137 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 linenr_T firstline; /* first line of range */
19139 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019140 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141{
Bram Moolenaar33570922005-01-25 22:26:29 +000019142 char_u *save_sourcing_name;
19143 linenr_T save_sourcing_lnum;
19144 scid_T save_current_SID;
19145 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019146 int save_did_emsg;
19147 static int depth = 0;
19148 dictitem_T *v;
19149 int fixvar_idx = 0; /* index in fixvar[] */
19150 int i;
19151 int ai;
19152 char_u numbuf[NUMBUFLEN];
19153 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019154#ifdef FEAT_PROFILE
19155 proftime_T wait_start;
19156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157
19158 /* If depth of calling is getting too high, don't execute the function */
19159 if (depth >= p_mfd)
19160 {
19161 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019162 rettv->v_type = VAR_NUMBER;
19163 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 return;
19165 }
19166 ++depth;
19167
19168 line_breakcheck(); /* check for CTRL-C hit */
19169
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019170 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019171 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019173 fc.rettv = rettv;
19174 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175 fc.linenr = 0;
19176 fc.returned = FALSE;
19177 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019179 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180 fc.dbg_tick = debug_tick;
19181
Bram Moolenaar33570922005-01-25 22:26:29 +000019182 /*
19183 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19184 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19185 * each argument variable and saves a lot of time.
19186 */
19187 /*
19188 * Init l: variables.
19189 */
19190 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019191 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019192 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019193 /* Set l:self to "selfdict". */
19194 v = &fc.fixvar[fixvar_idx++].var;
19195 STRCPY(v->di_key, "self");
19196 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19197 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19198 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019199 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019200 v->di_tv.vval.v_dict = selfdict;
19201 ++selfdict->dv_refcount;
19202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019203
Bram Moolenaar33570922005-01-25 22:26:29 +000019204 /*
19205 * Init a: variables.
19206 * Set a:0 to "argcount".
19207 * Set a:000 to a list with room for the "..." arguments.
19208 */
19209 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19210 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019211 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019212 v = &fc.fixvar[fixvar_idx++].var;
19213 STRCPY(v->di_key, "000");
19214 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19215 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19216 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019217 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019218 v->di_tv.vval.v_list = &fc.l_varlist;
19219 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19220 fc.l_varlist.lv_refcount = 99999;
19221
19222 /*
19223 * Set a:firstline to "firstline" and a:lastline to "lastline".
19224 * Set a:name to named arguments.
19225 * Set a:N to the "..." arguments.
19226 */
19227 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19228 (varnumber_T)firstline);
19229 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19230 (varnumber_T)lastline);
19231 for (i = 0; i < argcount; ++i)
19232 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019233 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019234 if (ai < 0)
19235 /* named argument a:name */
19236 name = FUNCARG(fp, i);
19237 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019238 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019239 /* "..." argument a:1, a:2, etc. */
19240 sprintf((char *)numbuf, "%d", ai + 1);
19241 name = numbuf;
19242 }
19243 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19244 {
19245 v = &fc.fixvar[fixvar_idx++].var;
19246 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19247 }
19248 else
19249 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019250 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19251 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019252 if (v == NULL)
19253 break;
19254 v->di_flags = DI_FLAGS_RO;
19255 }
19256 STRCPY(v->di_key, name);
19257 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19258
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019259 /* Note: the values are copied directly to avoid alloc/free.
19260 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019261 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019262 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019263
19264 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19265 {
19266 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19267 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019268 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019269 }
19270 }
19271
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272 /* Don't redraw while executing the function. */
19273 ++RedrawingDisabled;
19274 save_sourcing_name = sourcing_name;
19275 save_sourcing_lnum = sourcing_lnum;
19276 sourcing_lnum = 1;
19277 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019278 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279 if (sourcing_name != NULL)
19280 {
19281 if (save_sourcing_name != NULL
19282 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19283 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19284 else
19285 STRCPY(sourcing_name, "function ");
19286 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19287
19288 if (p_verbose >= 12)
19289 {
19290 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019291 verbose_enter_scroll();
19292
Bram Moolenaar555b2802005-05-19 21:08:39 +000019293 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294 if (p_verbose >= 14)
19295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019297 char_u numbuf[NUMBUFLEN];
19298 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299
19300 msg_puts((char_u *)"(");
19301 for (i = 0; i < argcount; ++i)
19302 {
19303 if (i > 0)
19304 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019305 if (argvars[i].v_type == VAR_NUMBER)
19306 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307 else
19308 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019309 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019310 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019312 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 }
19314 }
19315 msg_puts((char_u *)")");
19316 }
19317 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019318
19319 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320 --no_wait_return;
19321 }
19322 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019323#ifdef FEAT_PROFILE
19324 if (do_profiling)
19325 {
19326 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19327 func_do_profile(fp);
19328 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019329 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019330 {
19331 ++fp->uf_tm_count;
19332 profile_start(&fp->uf_tm_start);
19333 profile_zero(&fp->uf_tm_children);
19334 }
19335 script_prof_save(&wait_start);
19336 }
19337#endif
19338
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019340 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 save_did_emsg = did_emsg;
19342 did_emsg = FALSE;
19343
19344 /* call do_cmdline() to execute the lines */
19345 do_cmdline(NULL, get_func_line, (void *)&fc,
19346 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19347
19348 --RedrawingDisabled;
19349
19350 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019351 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019353 clear_tv(rettv);
19354 rettv->v_type = VAR_NUMBER;
19355 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019356 }
19357
Bram Moolenaar05159a02005-02-26 23:04:13 +000019358#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019359 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019360 {
19361 profile_end(&fp->uf_tm_start);
19362 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19363 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019364 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019365 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019366 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019367 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19368 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019369 }
19370 }
19371#endif
19372
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373 /* when being verbose, mention the return value */
19374 if (p_verbose >= 12)
19375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019377 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019380 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019381 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019382 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19383 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019384 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019386 char_u buf[MSG_BUF_LEN];
19387 char_u numbuf[NUMBUFLEN];
19388 char_u *tofree;
19389
Bram Moolenaar555b2802005-05-19 21:08:39 +000019390 /* The value may be very long. Skip the middle part, so that we
19391 * have some idea how it starts and ends. smsg() would always
19392 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019393 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019394 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019395 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019396 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397 }
19398 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019399
19400 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 --no_wait_return;
19402 }
19403
19404 vim_free(sourcing_name);
19405 sourcing_name = save_sourcing_name;
19406 sourcing_lnum = save_sourcing_lnum;
19407 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019408#ifdef FEAT_PROFILE
19409 if (do_profiling)
19410 script_prof_restore(&wait_start);
19411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412
19413 if (p_verbose >= 12 && sourcing_name != NULL)
19414 {
19415 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019416 verbose_enter_scroll();
19417
Bram Moolenaar555b2802005-05-19 21:08:39 +000019418 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019420
19421 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 --no_wait_return;
19423 }
19424
19425 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019426 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427
Bram Moolenaar33570922005-01-25 22:26:29 +000019428 /* The a: variables typevals were not alloced, only free the allocated
19429 * variables. */
19430 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19431
19432 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 --depth;
19434}
19435
19436/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019437 * Add a number variable "name" to dict "dp" with value "nr".
19438 */
19439 static void
19440add_nr_var(dp, v, name, nr)
19441 dict_T *dp;
19442 dictitem_T *v;
19443 char *name;
19444 varnumber_T nr;
19445{
19446 STRCPY(v->di_key, name);
19447 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19448 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19449 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019450 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019451 v->di_tv.vval.v_number = nr;
19452}
19453
19454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 * ":return [expr]"
19456 */
19457 void
19458ex_return(eap)
19459 exarg_T *eap;
19460{
19461 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019462 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 int returning = FALSE;
19464
19465 if (current_funccal == NULL)
19466 {
19467 EMSG(_("E133: :return not inside a function"));
19468 return;
19469 }
19470
19471 if (eap->skip)
19472 ++emsg_skip;
19473
19474 eap->nextcmd = NULL;
19475 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019476 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 {
19478 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019479 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019481 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 }
19483 /* It's safer to return also on error. */
19484 else if (!eap->skip)
19485 {
19486 /*
19487 * Return unless the expression evaluation has been cancelled due to an
19488 * aborting error, an interrupt, or an exception.
19489 */
19490 if (!aborting())
19491 returning = do_return(eap, FALSE, TRUE, NULL);
19492 }
19493
19494 /* When skipping or the return gets pending, advance to the next command
19495 * in this line (!returning). Otherwise, ignore the rest of the line.
19496 * Following lines will be ignored by get_func_line(). */
19497 if (returning)
19498 eap->nextcmd = NULL;
19499 else if (eap->nextcmd == NULL) /* no argument */
19500 eap->nextcmd = check_nextcmd(arg);
19501
19502 if (eap->skip)
19503 --emsg_skip;
19504}
19505
19506/*
19507 * Return from a function. Possibly makes the return pending. Also called
19508 * for a pending return at the ":endtry" or after returning from an extra
19509 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019510 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019511 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512 * FALSE when the return gets pending.
19513 */
19514 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019515do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 exarg_T *eap;
19517 int reanimate;
19518 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019519 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520{
19521 int idx;
19522 struct condstack *cstack = eap->cstack;
19523
19524 if (reanimate)
19525 /* Undo the return. */
19526 current_funccal->returned = FALSE;
19527
19528 /*
19529 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19530 * not in its finally clause (which then is to be executed next) is found.
19531 * In this case, make the ":return" pending for execution at the ":endtry".
19532 * Otherwise, return normally.
19533 */
19534 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19535 if (idx >= 0)
19536 {
19537 cstack->cs_pending[idx] = CSTP_RETURN;
19538
19539 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019540 /* A pending return again gets pending. "rettv" points to an
19541 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019543 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 else
19545 {
19546 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019547 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019549 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019551 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 {
19553 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019554 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019555 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556 else
19557 EMSG(_(e_outofmem));
19558 }
19559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019560 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561
19562 if (reanimate)
19563 {
19564 /* The pending return value could be overwritten by a ":return"
19565 * without argument in a finally clause; reset the default
19566 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019567 current_funccal->rettv->v_type = VAR_NUMBER;
19568 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569 }
19570 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019571 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 }
19573 else
19574 {
19575 current_funccal->returned = TRUE;
19576
19577 /* If the return is carried out now, store the return value. For
19578 * a return immediately after reanimation, the value is already
19579 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019580 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019582 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019583 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019585 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586 }
19587 }
19588
19589 return idx < 0;
19590}
19591
19592/*
19593 * Free the variable with a pending return value.
19594 */
19595 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019596discard_pending_return(rettv)
19597 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598{
Bram Moolenaar33570922005-01-25 22:26:29 +000019599 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600}
19601
19602/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019603 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604 * is an allocated string. Used by report_pending() for verbose messages.
19605 */
19606 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019607get_return_cmd(rettv)
19608 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019610 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019611 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019612 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019614 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019615 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019616 if (s == NULL)
19617 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019618
19619 STRCPY(IObuff, ":return ");
19620 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19621 if (STRLEN(s) + 8 >= IOSIZE)
19622 STRCPY(IObuff + IOSIZE - 4, "...");
19623 vim_free(tofree);
19624 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625}
19626
19627/*
19628 * Get next function line.
19629 * Called by do_cmdline() to get the next line.
19630 * Returns allocated string, or NULL for end of function.
19631 */
19632/* ARGSUSED */
19633 char_u *
19634get_func_line(c, cookie, indent)
19635 int c; /* not used */
19636 void *cookie;
19637 int indent; /* not used */
19638{
Bram Moolenaar33570922005-01-25 22:26:29 +000019639 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019640 ufunc_T *fp = fcp->func;
19641 char_u *retval;
19642 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643
19644 /* If breakpoints have been added/deleted need to check for it. */
19645 if (fcp->dbg_tick != debug_tick)
19646 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019647 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648 sourcing_lnum);
19649 fcp->dbg_tick = debug_tick;
19650 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019651#ifdef FEAT_PROFILE
19652 if (do_profiling)
19653 func_line_end(cookie);
19654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655
Bram Moolenaar05159a02005-02-26 23:04:13 +000019656 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019657 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
19658 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659 retval = NULL;
19660 else
19661 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019662 /* Skip NULL lines (continuation lines). */
19663 while (fcp->linenr < gap->ga_len
19664 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
19665 ++fcp->linenr;
19666 if (fcp->linenr >= gap->ga_len)
19667 retval = NULL;
19668 else
19669 {
19670 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19671 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019672#ifdef FEAT_PROFILE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019673 if (do_profiling)
19674 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019675#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677 }
19678
19679 /* Did we encounter a breakpoint? */
19680 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19681 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019682 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019684 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 sourcing_lnum);
19686 fcp->dbg_tick = debug_tick;
19687 }
19688
19689 return retval;
19690}
19691
Bram Moolenaar05159a02005-02-26 23:04:13 +000019692#if defined(FEAT_PROFILE) || defined(PROTO)
19693/*
19694 * Called when starting to read a function line.
19695 * "sourcing_lnum" must be correct!
19696 * When skipping lines it may not actually be executed, but we won't find out
19697 * until later and we need to store the time now.
19698 */
19699 void
19700func_line_start(cookie)
19701 void *cookie;
19702{
19703 funccall_T *fcp = (funccall_T *)cookie;
19704 ufunc_T *fp = fcp->func;
19705
19706 if (fp->uf_profiling && sourcing_lnum >= 1
19707 && sourcing_lnum <= fp->uf_lines.ga_len)
19708 {
19709 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019710 /* Skip continuation lines. */
19711 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
19712 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019713 fp->uf_tml_execed = FALSE;
19714 profile_start(&fp->uf_tml_start);
19715 profile_zero(&fp->uf_tml_children);
19716 profile_get_wait(&fp->uf_tml_wait);
19717 }
19718}
19719
19720/*
19721 * Called when actually executing a function line.
19722 */
19723 void
19724func_line_exec(cookie)
19725 void *cookie;
19726{
19727 funccall_T *fcp = (funccall_T *)cookie;
19728 ufunc_T *fp = fcp->func;
19729
19730 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19731 fp->uf_tml_execed = TRUE;
19732}
19733
19734/*
19735 * Called when done with a function line.
19736 */
19737 void
19738func_line_end(cookie)
19739 void *cookie;
19740{
19741 funccall_T *fcp = (funccall_T *)cookie;
19742 ufunc_T *fp = fcp->func;
19743
19744 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19745 {
19746 if (fp->uf_tml_execed)
19747 {
19748 ++fp->uf_tml_count[fp->uf_tml_idx];
19749 profile_end(&fp->uf_tml_start);
19750 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019751 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019752 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
19753 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019754 }
19755 fp->uf_tml_idx = -1;
19756 }
19757}
19758#endif
19759
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760/*
19761 * Return TRUE if the currently active function should be ended, because a
19762 * return was encountered or an error occured. Used inside a ":while".
19763 */
19764 int
19765func_has_ended(cookie)
19766 void *cookie;
19767{
Bram Moolenaar33570922005-01-25 22:26:29 +000019768 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769
19770 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19771 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019772 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 || fcp->returned);
19774}
19775
19776/*
19777 * return TRUE if cookie indicates a function which "abort"s on errors.
19778 */
19779 int
19780func_has_abort(cookie)
19781 void *cookie;
19782{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019783 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784}
19785
19786#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19787typedef enum
19788{
19789 VAR_FLAVOUR_DEFAULT,
19790 VAR_FLAVOUR_SESSION,
19791 VAR_FLAVOUR_VIMINFO
19792} var_flavour_T;
19793
19794static var_flavour_T var_flavour __ARGS((char_u *varname));
19795
19796 static var_flavour_T
19797var_flavour(varname)
19798 char_u *varname;
19799{
19800 char_u *p = varname;
19801
19802 if (ASCII_ISUPPER(*p))
19803 {
19804 while (*(++p))
19805 if (ASCII_ISLOWER(*p))
19806 return VAR_FLAVOUR_SESSION;
19807 return VAR_FLAVOUR_VIMINFO;
19808 }
19809 else
19810 return VAR_FLAVOUR_DEFAULT;
19811}
19812#endif
19813
19814#if defined(FEAT_VIMINFO) || defined(PROTO)
19815/*
19816 * Restore global vars that start with a capital from the viminfo file
19817 */
19818 int
19819read_viminfo_varlist(virp, writing)
19820 vir_T *virp;
19821 int writing;
19822{
19823 char_u *tab;
19824 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019825 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826
19827 if (!writing && (find_viminfo_parameter('!') != NULL))
19828 {
19829 tab = vim_strchr(virp->vir_line + 1, '\t');
19830 if (tab != NULL)
19831 {
19832 *tab++ = '\0'; /* isolate the variable name */
19833 if (*tab == 'S') /* string var */
19834 is_string = TRUE;
19835
19836 tab = vim_strchr(tab, '\t');
19837 if (tab != NULL)
19838 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 if (is_string)
19840 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019841 tv.v_type = VAR_STRING;
19842 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844 }
19845 else
19846 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019847 tv.v_type = VAR_NUMBER;
19848 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019850 set_var(virp->vir_line + 1, &tv, FALSE);
19851 if (is_string)
19852 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853 }
19854 }
19855 }
19856
19857 return viminfo_readline(virp);
19858}
19859
19860/*
19861 * Write global vars that start with a capital to the viminfo file
19862 */
19863 void
19864write_viminfo_varlist(fp)
19865 FILE *fp;
19866{
Bram Moolenaar33570922005-01-25 22:26:29 +000019867 hashitem_T *hi;
19868 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019869 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019870 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019871 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019872 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019873 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874
19875 if (find_viminfo_parameter('!') == NULL)
19876 return;
19877
19878 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019879
Bram Moolenaar33570922005-01-25 22:26:29 +000019880 todo = globvarht.ht_used;
19881 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019882 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019883 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019885 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019886 this_var = HI2DI(hi);
19887 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019888 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019889 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019890 {
19891 case VAR_STRING: s = "STR"; break;
19892 case VAR_NUMBER: s = "NUM"; break;
19893 default: continue;
19894 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019895 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019896 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019897 if (p != NULL)
19898 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019899 vim_free(tofree);
19900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 }
19902 }
19903}
19904#endif
19905
19906#if defined(FEAT_SESSION) || defined(PROTO)
19907 int
19908store_session_globals(fd)
19909 FILE *fd;
19910{
Bram Moolenaar33570922005-01-25 22:26:29 +000019911 hashitem_T *hi;
19912 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019913 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 char_u *p, *t;
19915
Bram Moolenaar33570922005-01-25 22:26:29 +000019916 todo = globvarht.ht_used;
19917 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019919 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019921 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019922 this_var = HI2DI(hi);
19923 if ((this_var->di_tv.v_type == VAR_NUMBER
19924 || this_var->di_tv.v_type == VAR_STRING)
19925 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019926 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019927 /* Escape special characters with a backslash. Turn a LF and
19928 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019929 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019930 (char_u *)"\\\"\n\r");
19931 if (p == NULL) /* out of memory */
19932 break;
19933 for (t = p; *t != NUL; ++t)
19934 if (*t == '\n')
19935 *t = 'n';
19936 else if (*t == '\r')
19937 *t = 'r';
19938 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019939 this_var->di_key,
19940 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19941 : ' ',
19942 p,
19943 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19944 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019945 || put_eol(fd) == FAIL)
19946 {
19947 vim_free(p);
19948 return FAIL;
19949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 vim_free(p);
19951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 }
19953 }
19954 return OK;
19955}
19956#endif
19957
Bram Moolenaar661b1822005-07-28 22:36:45 +000019958/*
19959 * Display script name where an item was last set.
19960 * Should only be invoked when 'verbose' is non-zero.
19961 */
19962 void
19963last_set_msg(scriptID)
19964 scid_T scriptID;
19965{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019966 char_u *p;
19967
Bram Moolenaar661b1822005-07-28 22:36:45 +000019968 if (scriptID != 0)
19969 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019970 p = home_replace_save(NULL, get_scriptname(scriptID));
19971 if (p != NULL)
19972 {
19973 verbose_enter();
19974 MSG_PUTS(_("\n\tLast set from "));
19975 MSG_PUTS(p);
19976 vim_free(p);
19977 verbose_leave();
19978 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019979 }
19980}
19981
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982#endif /* FEAT_EVAL */
19983
19984#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19985
19986
19987#ifdef WIN3264
19988/*
19989 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19990 */
19991static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19992static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19993static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19994
19995/*
19996 * Get the short pathname of a file.
19997 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19998 */
19999 static int
20000get_short_pathname(fnamep, bufp, fnamelen)
20001 char_u **fnamep;
20002 char_u **bufp;
20003 int *fnamelen;
20004{
20005 int l,len;
20006 char_u *newbuf;
20007
20008 len = *fnamelen;
20009
20010 l = GetShortPathName(*fnamep, *fnamep, len);
20011 if (l > len - 1)
20012 {
20013 /* If that doesn't work (not enough space), then save the string
20014 * and try again with a new buffer big enough
20015 */
20016 newbuf = vim_strnsave(*fnamep, l);
20017 if (newbuf == NULL)
20018 return 0;
20019
20020 vim_free(*bufp);
20021 *fnamep = *bufp = newbuf;
20022
20023 l = GetShortPathName(*fnamep,*fnamep,l+1);
20024
20025 /* Really should always succeed, as the buffer is big enough */
20026 }
20027
20028 *fnamelen = l;
20029 return 1;
20030}
20031
20032/*
20033 * Create a short path name. Returns the length of the buffer it needs.
20034 * Doesn't copy over the end of the buffer passed in.
20035 */
20036 static int
20037shortpath_for_invalid_fname(fname, bufp, fnamelen)
20038 char_u **fname;
20039 char_u **bufp;
20040 int *fnamelen;
20041{
20042 char_u *s, *p, *pbuf2, *pbuf3;
20043 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020044 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045
20046 /* Make a copy */
20047 len2 = *fnamelen;
20048 pbuf2 = vim_strnsave(*fname, len2);
20049 pbuf3 = NULL;
20050
20051 s = pbuf2 + len2 - 1; /* Find the end */
20052 slen = 1;
20053 plen = len2;
20054
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020055 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 {
20057 --s;
20058 ++slen;
20059 --plen;
20060 }
20061
20062 do
20063 {
20064 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020065 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066 {
20067 --s;
20068 ++slen;
20069 --plen;
20070 }
20071 if (s <= pbuf2)
20072 break;
20073
20074 /* Remeber the character that is about to be blatted */
20075 ch = *s;
20076 *s = 0; /* get_short_pathname requires a null-terminated string */
20077
20078 /* Try it in situ */
20079 p = pbuf2;
20080 if (!get_short_pathname(&p, &pbuf3, &plen))
20081 {
20082 vim_free(pbuf2);
20083 return -1;
20084 }
20085 *s = ch; /* Preserve the string */
20086 } while (plen == 0);
20087
20088 if (plen > 0)
20089 {
20090 /* Remeber the length of the new string. */
20091 *fnamelen = len = plen + slen;
20092 vim_free(*bufp);
20093 if (len > len2)
20094 {
20095 /* If there's not enough space in the currently allocated string,
20096 * then copy it to a buffer big enough.
20097 */
20098 *fname= *bufp = vim_strnsave(p, len);
20099 if (*fname == NULL)
20100 return -1;
20101 }
20102 else
20103 {
20104 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20105 *fname = *bufp = pbuf2;
20106 if (p != pbuf2)
20107 strncpy(*fname, p, plen);
20108 pbuf2 = NULL;
20109 }
20110 /* Concat the next bit */
20111 strncpy(*fname + plen, s, slen);
20112 (*fname)[len] = '\0';
20113 }
20114 vim_free(pbuf3);
20115 vim_free(pbuf2);
20116 return 0;
20117}
20118
20119/*
20120 * Get a pathname for a partial path.
20121 */
20122 static int
20123shortpath_for_partial(fnamep, bufp, fnamelen)
20124 char_u **fnamep;
20125 char_u **bufp;
20126 int *fnamelen;
20127{
20128 int sepcount, len, tflen;
20129 char_u *p;
20130 char_u *pbuf, *tfname;
20131 int hasTilde;
20132
20133 /* Count up the path seperators from the RHS.. so we know which part
20134 * of the path to return.
20135 */
20136 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020137 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 if (vim_ispathsep(*p))
20139 ++sepcount;
20140
20141 /* Need full path first (use expand_env() to remove a "~/") */
20142 hasTilde = (**fnamep == '~');
20143 if (hasTilde)
20144 pbuf = tfname = expand_env_save(*fnamep);
20145 else
20146 pbuf = tfname = FullName_save(*fnamep, FALSE);
20147
20148 len = tflen = STRLEN(tfname);
20149
20150 if (!get_short_pathname(&tfname, &pbuf, &len))
20151 return -1;
20152
20153 if (len == 0)
20154 {
20155 /* Don't have a valid filename, so shorten the rest of the
20156 * path if we can. This CAN give us invalid 8.3 filenames, but
20157 * there's not a lot of point in guessing what it might be.
20158 */
20159 len = tflen;
20160 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20161 return -1;
20162 }
20163
20164 /* Count the paths backward to find the beginning of the desired string. */
20165 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020166 {
20167#ifdef FEAT_MBYTE
20168 if (has_mbyte)
20169 p -= mb_head_off(tfname, p);
20170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 if (vim_ispathsep(*p))
20172 {
20173 if (sepcount == 0 || (hasTilde && sepcount == 1))
20174 break;
20175 else
20176 sepcount --;
20177 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179 if (hasTilde)
20180 {
20181 --p;
20182 if (p >= tfname)
20183 *p = '~';
20184 else
20185 return -1;
20186 }
20187 else
20188 ++p;
20189
20190 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20191 vim_free(*bufp);
20192 *fnamelen = (int)STRLEN(p);
20193 *bufp = pbuf;
20194 *fnamep = p;
20195
20196 return 0;
20197}
20198#endif /* WIN3264 */
20199
20200/*
20201 * Adjust a filename, according to a string of modifiers.
20202 * *fnamep must be NUL terminated when called. When returning, the length is
20203 * determined by *fnamelen.
20204 * Returns valid flags.
20205 * When there is an error, *fnamep is set to NULL.
20206 */
20207 int
20208modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20209 char_u *src; /* string with modifiers */
20210 int *usedlen; /* characters after src that are used */
20211 char_u **fnamep; /* file name so far */
20212 char_u **bufp; /* buffer for allocated file name or NULL */
20213 int *fnamelen; /* length of fnamep */
20214{
20215 int valid = 0;
20216 char_u *tail;
20217 char_u *s, *p, *pbuf;
20218 char_u dirname[MAXPATHL];
20219 int c;
20220 int has_fullname = 0;
20221#ifdef WIN3264
20222 int has_shortname = 0;
20223#endif
20224
20225repeat:
20226 /* ":p" - full path/file_name */
20227 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20228 {
20229 has_fullname = 1;
20230
20231 valid |= VALID_PATH;
20232 *usedlen += 2;
20233
20234 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20235 if ((*fnamep)[0] == '~'
20236#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20237 && ((*fnamep)[1] == '/'
20238# ifdef BACKSLASH_IN_FILENAME
20239 || (*fnamep)[1] == '\\'
20240# endif
20241 || (*fnamep)[1] == NUL)
20242
20243#endif
20244 )
20245 {
20246 *fnamep = expand_env_save(*fnamep);
20247 vim_free(*bufp); /* free any allocated file name */
20248 *bufp = *fnamep;
20249 if (*fnamep == NULL)
20250 return -1;
20251 }
20252
20253 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020254 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255 {
20256 if (vim_ispathsep(*p)
20257 && p[1] == '.'
20258 && (p[2] == NUL
20259 || vim_ispathsep(p[2])
20260 || (p[2] == '.'
20261 && (p[3] == NUL || vim_ispathsep(p[3])))))
20262 break;
20263 }
20264
20265 /* FullName_save() is slow, don't use it when not needed. */
20266 if (*p != NUL || !vim_isAbsName(*fnamep))
20267 {
20268 *fnamep = FullName_save(*fnamep, *p != NUL);
20269 vim_free(*bufp); /* free any allocated file name */
20270 *bufp = *fnamep;
20271 if (*fnamep == NULL)
20272 return -1;
20273 }
20274
20275 /* Append a path separator to a directory. */
20276 if (mch_isdir(*fnamep))
20277 {
20278 /* Make room for one or two extra characters. */
20279 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20280 vim_free(*bufp); /* free any allocated file name */
20281 *bufp = *fnamep;
20282 if (*fnamep == NULL)
20283 return -1;
20284 add_pathsep(*fnamep);
20285 }
20286 }
20287
20288 /* ":." - path relative to the current directory */
20289 /* ":~" - path relative to the home directory */
20290 /* ":8" - shortname path - postponed till after */
20291 while (src[*usedlen] == ':'
20292 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20293 {
20294 *usedlen += 2;
20295 if (c == '8')
20296 {
20297#ifdef WIN3264
20298 has_shortname = 1; /* Postpone this. */
20299#endif
20300 continue;
20301 }
20302 pbuf = NULL;
20303 /* Need full path first (use expand_env() to remove a "~/") */
20304 if (!has_fullname)
20305 {
20306 if (c == '.' && **fnamep == '~')
20307 p = pbuf = expand_env_save(*fnamep);
20308 else
20309 p = pbuf = FullName_save(*fnamep, FALSE);
20310 }
20311 else
20312 p = *fnamep;
20313
20314 has_fullname = 0;
20315
20316 if (p != NULL)
20317 {
20318 if (c == '.')
20319 {
20320 mch_dirname(dirname, MAXPATHL);
20321 s = shorten_fname(p, dirname);
20322 if (s != NULL)
20323 {
20324 *fnamep = s;
20325 if (pbuf != NULL)
20326 {
20327 vim_free(*bufp); /* free any allocated file name */
20328 *bufp = pbuf;
20329 pbuf = NULL;
20330 }
20331 }
20332 }
20333 else
20334 {
20335 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20336 /* Only replace it when it starts with '~' */
20337 if (*dirname == '~')
20338 {
20339 s = vim_strsave(dirname);
20340 if (s != NULL)
20341 {
20342 *fnamep = s;
20343 vim_free(*bufp);
20344 *bufp = s;
20345 }
20346 }
20347 }
20348 vim_free(pbuf);
20349 }
20350 }
20351
20352 tail = gettail(*fnamep);
20353 *fnamelen = (int)STRLEN(*fnamep);
20354
20355 /* ":h" - head, remove "/file_name", can be repeated */
20356 /* Don't remove the first "/" or "c:\" */
20357 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20358 {
20359 valid |= VALID_HEAD;
20360 *usedlen += 2;
20361 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020362 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 --tail;
20364 *fnamelen = (int)(tail - *fnamep);
20365#ifdef VMS
20366 if (*fnamelen > 0)
20367 *fnamelen += 1; /* the path separator is part of the path */
20368#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020369 while (tail > s && !after_pathsep(s, tail))
20370 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371 }
20372
20373 /* ":8" - shortname */
20374 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20375 {
20376 *usedlen += 2;
20377#ifdef WIN3264
20378 has_shortname = 1;
20379#endif
20380 }
20381
20382#ifdef WIN3264
20383 /* Check shortname after we have done 'heads' and before we do 'tails'
20384 */
20385 if (has_shortname)
20386 {
20387 pbuf = NULL;
20388 /* Copy the string if it is shortened by :h */
20389 if (*fnamelen < (int)STRLEN(*fnamep))
20390 {
20391 p = vim_strnsave(*fnamep, *fnamelen);
20392 if (p == 0)
20393 return -1;
20394 vim_free(*bufp);
20395 *bufp = *fnamep = p;
20396 }
20397
20398 /* Split into two implementations - makes it easier. First is where
20399 * there isn't a full name already, second is where there is.
20400 */
20401 if (!has_fullname && !vim_isAbsName(*fnamep))
20402 {
20403 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20404 return -1;
20405 }
20406 else
20407 {
20408 int l;
20409
20410 /* Simple case, already have the full-name
20411 * Nearly always shorter, so try first time. */
20412 l = *fnamelen;
20413 if (!get_short_pathname(fnamep, bufp, &l))
20414 return -1;
20415
20416 if (l == 0)
20417 {
20418 /* Couldn't find the filename.. search the paths.
20419 */
20420 l = *fnamelen;
20421 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20422 return -1;
20423 }
20424 *fnamelen = l;
20425 }
20426 }
20427#endif /* WIN3264 */
20428
20429 /* ":t" - tail, just the basename */
20430 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20431 {
20432 *usedlen += 2;
20433 *fnamelen -= (int)(tail - *fnamep);
20434 *fnamep = tail;
20435 }
20436
20437 /* ":e" - extension, can be repeated */
20438 /* ":r" - root, without extension, can be repeated */
20439 while (src[*usedlen] == ':'
20440 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20441 {
20442 /* find a '.' in the tail:
20443 * - for second :e: before the current fname
20444 * - otherwise: The last '.'
20445 */
20446 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20447 s = *fnamep - 2;
20448 else
20449 s = *fnamep + *fnamelen - 1;
20450 for ( ; s > tail; --s)
20451 if (s[0] == '.')
20452 break;
20453 if (src[*usedlen + 1] == 'e') /* :e */
20454 {
20455 if (s > tail)
20456 {
20457 *fnamelen += (int)(*fnamep - (s + 1));
20458 *fnamep = s + 1;
20459#ifdef VMS
20460 /* cut version from the extension */
20461 s = *fnamep + *fnamelen - 1;
20462 for ( ; s > *fnamep; --s)
20463 if (s[0] == ';')
20464 break;
20465 if (s > *fnamep)
20466 *fnamelen = s - *fnamep;
20467#endif
20468 }
20469 else if (*fnamep <= tail)
20470 *fnamelen = 0;
20471 }
20472 else /* :r */
20473 {
20474 if (s > tail) /* remove one extension */
20475 *fnamelen = (int)(s - *fnamep);
20476 }
20477 *usedlen += 2;
20478 }
20479
20480 /* ":s?pat?foo?" - substitute */
20481 /* ":gs?pat?foo?" - global substitute */
20482 if (src[*usedlen] == ':'
20483 && (src[*usedlen + 1] == 's'
20484 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20485 {
20486 char_u *str;
20487 char_u *pat;
20488 char_u *sub;
20489 int sep;
20490 char_u *flags;
20491 int didit = FALSE;
20492
20493 flags = (char_u *)"";
20494 s = src + *usedlen + 2;
20495 if (src[*usedlen + 1] == 'g')
20496 {
20497 flags = (char_u *)"g";
20498 ++s;
20499 }
20500
20501 sep = *s++;
20502 if (sep)
20503 {
20504 /* find end of pattern */
20505 p = vim_strchr(s, sep);
20506 if (p != NULL)
20507 {
20508 pat = vim_strnsave(s, (int)(p - s));
20509 if (pat != NULL)
20510 {
20511 s = p + 1;
20512 /* find end of substitution */
20513 p = vim_strchr(s, sep);
20514 if (p != NULL)
20515 {
20516 sub = vim_strnsave(s, (int)(p - s));
20517 str = vim_strnsave(*fnamep, *fnamelen);
20518 if (sub != NULL && str != NULL)
20519 {
20520 *usedlen = (int)(p + 1 - src);
20521 s = do_string_sub(str, pat, sub, flags);
20522 if (s != NULL)
20523 {
20524 *fnamep = s;
20525 *fnamelen = (int)STRLEN(s);
20526 vim_free(*bufp);
20527 *bufp = s;
20528 didit = TRUE;
20529 }
20530 }
20531 vim_free(sub);
20532 vim_free(str);
20533 }
20534 vim_free(pat);
20535 }
20536 }
20537 /* after using ":s", repeat all the modifiers */
20538 if (didit)
20539 goto repeat;
20540 }
20541 }
20542
20543 return valid;
20544}
20545
20546/*
20547 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20548 * "flags" can be "g" to do a global substitute.
20549 * Returns an allocated string, NULL for error.
20550 */
20551 char_u *
20552do_string_sub(str, pat, sub, flags)
20553 char_u *str;
20554 char_u *pat;
20555 char_u *sub;
20556 char_u *flags;
20557{
20558 int sublen;
20559 regmatch_T regmatch;
20560 int i;
20561 int do_all;
20562 char_u *tail;
20563 garray_T ga;
20564 char_u *ret;
20565 char_u *save_cpo;
20566
20567 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20568 save_cpo = p_cpo;
20569 p_cpo = (char_u *)"";
20570
20571 ga_init2(&ga, 1, 200);
20572
20573 do_all = (flags[0] == 'g');
20574
20575 regmatch.rm_ic = p_ic;
20576 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20577 if (regmatch.regprog != NULL)
20578 {
20579 tail = str;
20580 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20581 {
20582 /*
20583 * Get some space for a temporary buffer to do the substitution
20584 * into. It will contain:
20585 * - The text up to where the match is.
20586 * - The substituted text.
20587 * - The text after the match.
20588 */
20589 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20590 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20591 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20592 {
20593 ga_clear(&ga);
20594 break;
20595 }
20596
20597 /* copy the text up to where the match is */
20598 i = (int)(regmatch.startp[0] - tail);
20599 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20600 /* add the substituted text */
20601 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20602 + ga.ga_len + i, TRUE, TRUE, FALSE);
20603 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604 /* avoid getting stuck on a match with an empty string */
20605 if (tail == regmatch.endp[0])
20606 {
20607 if (*tail == NUL)
20608 break;
20609 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20610 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 }
20612 else
20613 {
20614 tail = regmatch.endp[0];
20615 if (*tail == NUL)
20616 break;
20617 }
20618 if (!do_all)
20619 break;
20620 }
20621
20622 if (ga.ga_data != NULL)
20623 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20624
20625 vim_free(regmatch.regprog);
20626 }
20627
20628 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20629 ga_clear(&ga);
20630 p_cpo = save_cpo;
20631
20632 return ret;
20633}
20634
20635#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */