blob: b0202cdb32addd8336d6d242e29a752be5b6a812 [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)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#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 Moolenaar33570922005-01-25 22:26:29 +0000403static listitem_T *listitem_alloc __ARGS((void));
404static void listitem_free __ARGS((listitem_T *item));
405static void listitem_remove __ARGS((list_T *l, listitem_T *item));
406static long list_len __ARGS((list_T *l));
407static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
408static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
409static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static listitem_T *list_find __ARGS((list_T *l, long n));
411static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static void list_append __ARGS((list_T *l, listitem_T *item));
413static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000414static int list_append_string __ARGS((list_T *l, char_u *str, int len));
415static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
417static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
418static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000419static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000421static char_u *list2string __ARGS((typval_T *tv, int copyID));
422static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000423static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
424static void set_ref_in_list __ARGS((list_T *l, int copyID));
425static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static void dict_unref __ARGS((dict_T *d));
427static void dict_free __ARGS((dict_T *d));
428static dictitem_T *dictitem_alloc __ARGS((char_u *key));
429static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
430static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
431static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000432static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static int dict_add __ARGS((dict_T *d, dictitem_T *item));
434static long dict_len __ARGS((dict_T *d));
435static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
439static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static char_u *string_quote __ARGS((char_u *str, int function));
441static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
442static int find_internal_func __ARGS((char_u *name));
443static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
444static 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));
445static 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 +0000446static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447
448static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000467#if defined(FEAT_INS_EXPAND)
468static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
476static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000502static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000504static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000510static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000518static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000541static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000547static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000564static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000568#ifdef vim_mkdir
569static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
570#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000571static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000575static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000576static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000578static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000590static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000592static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000599static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000600static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000605static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000606static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
609#ifdef HAVE_STRFTIME
610static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
611#endif
612static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000624static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000625static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000626static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000628static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000643static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
646static int get_env_len __ARGS((char_u **arg));
647static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000648static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000649static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
650#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
651#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
652 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000653static 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 +0000654static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000655static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000656static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
657static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static typval_T *alloc_tv __ARGS((void));
659static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static void init_tv __ARGS((typval_T *varp));
661static long get_tv_number __ARGS((typval_T *varp));
662static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000663static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static char_u *get_tv_string __ARGS((typval_T *varp));
665static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000666static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000668static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
670static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
671static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
672static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
673static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
674static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
675static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000676static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000677static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000678static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
680static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
681static int eval_fname_script __ARGS((char_u *p));
682static int eval_fname_sid __ARGS((char_u *p));
683static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static ufunc_T *find_func __ARGS((char_u *name));
685static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000686static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000687#ifdef FEAT_PROFILE
688static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000689static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
690static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
691static int
692# ifdef __BORLANDC__
693 _RTLENTRYF
694# endif
695 prof_total_cmp __ARGS((const void *s1, const void *s2));
696static int
697# ifdef __BORLANDC__
698 _RTLENTRYF
699# endif
700 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000702static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000703static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000704static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void func_free __ARGS((ufunc_T *fp));
706static void func_unref __ARGS((char_u *name));
707static void func_ref __ARGS((char_u *name));
708static 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));
709static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000710static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000711static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
712static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar33570922005-01-25 22:26:29 +0000713
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000714/* Character used as separated in autoload function/variable names. */
715#define AUTOLOAD_CHAR '#'
716
Bram Moolenaar33570922005-01-25 22:26:29 +0000717/*
718 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000719 */
720 void
721eval_init()
722{
Bram Moolenaar33570922005-01-25 22:26:29 +0000723 int i;
724 struct vimvar *p;
725
726 init_var_dict(&globvardict, &globvars_var);
727 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000728 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000729 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000730
731 for (i = 0; i < VV_LEN; ++i)
732 {
733 p = &vimvars[i];
734 STRCPY(p->vv_di.di_key, p->vv_name);
735 if (p->vv_flags & VV_RO)
736 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
737 else if (p->vv_flags & VV_RO_SBX)
738 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
739 else
740 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000741
742 /* add to v: scope dict, unless the value is not always available */
743 if (p->vv_type != VAR_UNKNOWN)
744 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000745 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000746 /* add to compat scope dict */
747 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000748 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000749}
750
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000751#if defined(EXITFREE) || defined(PROTO)
752 void
753eval_clear()
754{
755 int i;
756 struct vimvar *p;
757
758 for (i = 0; i < VV_LEN; ++i)
759 {
760 p = &vimvars[i];
761 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000762 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000763 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000764 p->vv_di.di_tv.vval.v_string = NULL;
765 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000766 }
767 hash_clear(&vimvarht);
768 hash_clear(&compat_hashtab);
769
770 /* script-local variables */
771 for (i = 1; i <= ga_scripts.ga_len; ++i)
772 vars_clear(&SCRIPT_VARS(i));
773 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000774 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000775
776 /* global variables */
777 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000778
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000779 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000780 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000781 hash_clear(&func_hashtab);
782
783 /* unreferenced lists and dicts */
784 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000785}
786#endif
787
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 * Return the name of the executed function.
790 */
791 char_u *
792func_name(cookie)
793 void *cookie;
794{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000795 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
798/*
799 * Return the address holding the next breakpoint line for a funccall cookie.
800 */
801 linenr_T *
802func_breakpoint(cookie)
803 void *cookie;
804{
Bram Moolenaar33570922005-01-25 22:26:29 +0000805 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806}
807
808/*
809 * Return the address holding the debug tick for a funccall cookie.
810 */
811 int *
812func_dbg_tick(cookie)
813 void *cookie;
814{
Bram Moolenaar33570922005-01-25 22:26:29 +0000815 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816}
817
818/*
819 * Return the nesting level for a funccall cookie.
820 */
821 int
822func_level(cookie)
823 void *cookie;
824{
Bram Moolenaar33570922005-01-25 22:26:29 +0000825 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826}
827
828/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000829funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831/*
832 * Return TRUE when a function was ended by a ":return" command.
833 */
834 int
835current_func_returned()
836{
837 return current_funccal->returned;
838}
839
840
841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 * Set an internal variable to a string value. Creates the variable if it does
843 * not already exist.
844 */
845 void
846set_internal_string_var(name, value)
847 char_u *name;
848 char_u *value;
849{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000850 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
853 val = vim_strsave(value);
854 if (val != NULL)
855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000856 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000857 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000859 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000860 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 }
862 }
863}
864
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000865static lval_T *redir_lval = NULL;
866static char_u *redir_endp = NULL;
867static char_u *redir_varname = NULL;
868
869/*
870 * Start recording command output to a variable
871 * Returns OK if successfully completed the setup. FAIL otherwise.
872 */
873 int
874var_redir_start(name, append)
875 char_u *name;
876 int append; /* append to an existing variable */
877{
878 int save_emsg;
879 int err;
880 typval_T tv;
881
882 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000883 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000884 {
885 EMSG(_(e_invarg));
886 return FAIL;
887 }
888
889 redir_varname = vim_strsave(name);
890 if (redir_varname == NULL)
891 return FAIL;
892
893 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
894 if (redir_lval == NULL)
895 {
896 var_redir_stop();
897 return FAIL;
898 }
899
900 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000901 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
902 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000903 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
904 {
905 if (redir_endp != NULL && *redir_endp != NUL)
906 /* Trailing characters are present after the variable name */
907 EMSG(_(e_trailing));
908 else
909 EMSG(_(e_invarg));
910 var_redir_stop();
911 return FAIL;
912 }
913
914 /* check if we can write to the variable: set it to or append an empty
915 * string */
916 save_emsg = did_emsg;
917 did_emsg = FALSE;
918 tv.v_type = VAR_STRING;
919 tv.vval.v_string = (char_u *)"";
920 if (append)
921 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
922 else
923 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
924 err = did_emsg;
925 did_emsg += save_emsg;
926 if (err)
927 {
928 var_redir_stop();
929 return FAIL;
930 }
931 if (redir_lval->ll_newkey != NULL)
932 {
933 /* Dictionary item was created, don't do it again. */
934 vim_free(redir_lval->ll_newkey);
935 redir_lval->ll_newkey = NULL;
936 }
937
938 return OK;
939}
940
941/*
942 * Append "value[len]" to the variable set by var_redir_start().
943 */
944 void
945var_redir_str(value, len)
946 char_u *value;
947 int len;
948{
949 char_u *val;
950 typval_T tv;
951 int save_emsg;
952 int err;
953
954 if (redir_lval == NULL)
955 return;
956
957 if (len == -1)
958 /* Append the entire string */
959 val = vim_strsave(value);
960 else
961 /* Append only the specified number of characters */
962 val = vim_strnsave(value, len);
963 if (val == NULL)
964 return;
965
966 tv.v_type = VAR_STRING;
967 tv.vval.v_string = val;
968
969 save_emsg = did_emsg;
970 did_emsg = FALSE;
971 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
972 err = did_emsg;
973 did_emsg += save_emsg;
974 if (err)
975 var_redir_stop();
976
977 vim_free(tv.vval.v_string);
978}
979
980/*
981 * Stop redirecting command output to a variable.
982 */
983 void
984var_redir_stop()
985{
986 if (redir_lval != NULL)
987 {
988 clear_lval(redir_lval);
989 vim_free(redir_lval);
990 redir_lval = NULL;
991 }
992 vim_free(redir_varname);
993 redir_varname = NULL;
994}
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996# if defined(FEAT_MBYTE) || defined(PROTO)
997 int
998eval_charconvert(enc_from, enc_to, fname_from, fname_to)
999 char_u *enc_from;
1000 char_u *enc_to;
1001 char_u *fname_from;
1002 char_u *fname_to;
1003{
1004 int err = FALSE;
1005
1006 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1007 set_vim_var_string(VV_CC_TO, enc_to, -1);
1008 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1009 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1010 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1011 err = TRUE;
1012 set_vim_var_string(VV_CC_FROM, NULL, -1);
1013 set_vim_var_string(VV_CC_TO, NULL, -1);
1014 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1015 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1016
1017 if (err)
1018 return FAIL;
1019 return OK;
1020}
1021# endif
1022
1023# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1024 int
1025eval_printexpr(fname, args)
1026 char_u *fname;
1027 char_u *args;
1028{
1029 int err = FALSE;
1030
1031 set_vim_var_string(VV_FNAME_IN, fname, -1);
1032 set_vim_var_string(VV_CMDARG, args, -1);
1033 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1034 err = TRUE;
1035 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1036 set_vim_var_string(VV_CMDARG, NULL, -1);
1037
1038 if (err)
1039 {
1040 mch_remove(fname);
1041 return FAIL;
1042 }
1043 return OK;
1044}
1045# endif
1046
1047# if defined(FEAT_DIFF) || defined(PROTO)
1048 void
1049eval_diff(origfile, newfile, outfile)
1050 char_u *origfile;
1051 char_u *newfile;
1052 char_u *outfile;
1053{
1054 int err = FALSE;
1055
1056 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1057 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1058 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1059 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1060 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1061 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1062 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1063}
1064
1065 void
1066eval_patch(origfile, difffile, outfile)
1067 char_u *origfile;
1068 char_u *difffile;
1069 char_u *outfile;
1070{
1071 int err;
1072
1073 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1074 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1075 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1076 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1077 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1078 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1079 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1080}
1081# endif
1082
1083/*
1084 * Top level evaluation function, returning a boolean.
1085 * Sets "error" to TRUE if there was an error.
1086 * Return TRUE or FALSE.
1087 */
1088 int
1089eval_to_bool(arg, error, nextcmd, skip)
1090 char_u *arg;
1091 int *error;
1092 char_u **nextcmd;
1093 int skip; /* only parse, don't execute */
1094{
Bram Moolenaar33570922005-01-25 22:26:29 +00001095 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 int retval = FALSE;
1097
1098 if (skip)
1099 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001100 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 else
1103 {
1104 *error = FALSE;
1105 if (!skip)
1106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001107 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001108 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 }
1110 }
1111 if (skip)
1112 --emsg_skip;
1113
1114 return retval;
1115}
1116
1117/*
1118 * Top level evaluation function, returning a string. If "skip" is TRUE,
1119 * only parsing to "nextcmd" is done, without reporting errors. Return
1120 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1121 */
1122 char_u *
1123eval_to_string_skip(arg, nextcmd, skip)
1124 char_u *arg;
1125 char_u **nextcmd;
1126 int skip; /* only parse, don't execute */
1127{
Bram Moolenaar33570922005-01-25 22:26:29 +00001128 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 char_u *retval;
1130
1131 if (skip)
1132 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001133 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 retval = NULL;
1135 else
1136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001137 retval = vim_strsave(get_tv_string(&tv));
1138 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
1140 if (skip)
1141 --emsg_skip;
1142
1143 return retval;
1144}
1145
1146/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001147 * Skip over an expression at "*pp".
1148 * Return FAIL for an error, OK otherwise.
1149 */
1150 int
1151skip_expr(pp)
1152 char_u **pp;
1153{
Bram Moolenaar33570922005-01-25 22:26:29 +00001154 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001155
1156 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001157 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001158}
1159
1160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 * Top level evaluation function, returning a string.
1162 * Return pointer to allocated memory, or NULL for failure.
1163 */
1164 char_u *
1165eval_to_string(arg, nextcmd)
1166 char_u *arg;
1167 char_u **nextcmd;
1168{
Bram Moolenaar33570922005-01-25 22:26:29 +00001169 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 char_u *retval;
1171
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001172 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 retval = NULL;
1174 else
1175 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001176 retval = vim_strsave(get_tv_string(&tv));
1177 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 }
1179
1180 return retval;
1181}
1182
1183/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001184 * Call eval_to_string() without using current local variables and using
1185 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 */
1187 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001188eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 char_u *arg;
1190 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001191 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192{
1193 char_u *retval;
1194 void *save_funccalp;
1195
1196 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001197 if (use_sandbox)
1198 ++sandbox;
1199 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 retval = eval_to_string(arg, nextcmd);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001201 if (use_sandbox)
1202 --sandbox;
1203 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 restore_funccal(save_funccalp);
1205 return retval;
1206}
1207
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208/*
1209 * Top level evaluation function, returning a number.
1210 * Evaluates "expr" silently.
1211 * Returns -1 for an error.
1212 */
1213 int
1214eval_to_number(expr)
1215 char_u *expr;
1216{
Bram Moolenaar33570922005-01-25 22:26:29 +00001217 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001219 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220
1221 ++emsg_off;
1222
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001223 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 retval = -1;
1225 else
1226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001227 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001228 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230 --emsg_off;
1231
1232 return retval;
1233}
1234
Bram Moolenaara40058a2005-07-11 22:42:07 +00001235/*
1236 * Prepare v: variable "idx" to be used.
1237 * Save the current typeval in "save_tv".
1238 * When not used yet add the variable to the v: hashtable.
1239 */
1240 static void
1241prepare_vimvar(idx, save_tv)
1242 int idx;
1243 typval_T *save_tv;
1244{
1245 *save_tv = vimvars[idx].vv_tv;
1246 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1247 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1248}
1249
1250/*
1251 * Restore v: variable "idx" to typeval "save_tv".
1252 * When no longer defined, remove the variable from the v: hashtable.
1253 */
1254 static void
1255restore_vimvar(idx, save_tv)
1256 int idx;
1257 typval_T *save_tv;
1258{
1259 hashitem_T *hi;
1260
1261 clear_tv(&vimvars[idx].vv_tv);
1262 vimvars[idx].vv_tv = *save_tv;
1263 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1264 {
1265 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1266 if (HASHITEM_EMPTY(hi))
1267 EMSG2(_(e_intern2), "restore_vimvar()");
1268 else
1269 hash_remove(&vimvarht, hi);
1270 }
1271}
1272
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001273#if defined(FEAT_SYN_HL) || defined(PROTO)
1274/*
1275 * Evaluate an expression to a list with suggestions.
1276 * For the "expr:" part of 'spellsuggest'.
1277 */
1278 list_T *
1279eval_spell_expr(badword, expr)
1280 char_u *badword;
1281 char_u *expr;
1282{
1283 typval_T save_val;
1284 typval_T rettv;
1285 list_T *list = NULL;
1286 char_u *p = skipwhite(expr);
1287
1288 /* Set "v:val" to the bad word. */
1289 prepare_vimvar(VV_VAL, &save_val);
1290 vimvars[VV_VAL].vv_type = VAR_STRING;
1291 vimvars[VV_VAL].vv_str = badword;
1292 if (p_verbose == 0)
1293 ++emsg_off;
1294
1295 if (eval1(&p, &rettv, TRUE) == OK)
1296 {
1297 if (rettv.v_type != VAR_LIST)
1298 clear_tv(&rettv);
1299 else
1300 list = rettv.vval.v_list;
1301 }
1302
1303 if (p_verbose == 0)
1304 --emsg_off;
1305 vimvars[VV_VAL].vv_str = NULL;
1306 restore_vimvar(VV_VAL, &save_val);
1307
1308 return list;
1309}
1310
1311/*
1312 * "list" is supposed to contain two items: a word and a number. Return the
1313 * word in "pp" and the number as the return value.
1314 * Return -1 if anything isn't right.
1315 * Used to get the good word and score from the eval_spell_expr() result.
1316 */
1317 int
1318get_spellword(list, pp)
1319 list_T *list;
1320 char_u **pp;
1321{
1322 listitem_T *li;
1323
1324 li = list->lv_first;
1325 if (li == NULL)
1326 return -1;
1327 *pp = get_tv_string(&li->li_tv);
1328
1329 li = li->li_next;
1330 if (li == NULL)
1331 return -1;
1332 return get_tv_number(&li->li_tv);
1333}
1334#endif
1335
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001336/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001337 * Top level evaluation function.
1338 * Returns an allocated typval_T with the result.
1339 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001340 */
1341 typval_T *
1342eval_expr(arg, nextcmd)
1343 char_u *arg;
1344 char_u **nextcmd;
1345{
1346 typval_T *tv;
1347
1348 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001349 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001350 {
1351 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001352 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001353 }
1354
1355 return tv;
1356}
1357
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1360/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001361 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001363 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001365 static int
1366call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 char_u *func;
1368 int argc;
1369 char_u **argv;
1370 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
Bram Moolenaar33570922005-01-25 22:26:29 +00001373 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 long n;
1375 int len;
1376 int i;
1377 int doesrange;
1378 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001379 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
Bram Moolenaar33570922005-01-25 22:26:29 +00001381 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001383 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
1385 for (i = 0; i < argc; i++)
1386 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001387 /* Pass a NULL or empty argument as an empty string */
1388 if (argv[i] == NULL || *argv[i] == NUL)
1389 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001390 argvars[i].v_type = VAR_STRING;
1391 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001392 continue;
1393 }
1394
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 /* Recognize a number argument, the others must be strings. */
1396 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1397 if (len != 0 && len == (int)STRLEN(argv[i]))
1398 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001399 argvars[i].v_type = VAR_NUMBER;
1400 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402 else
1403 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001404 argvars[i].v_type = VAR_STRING;
1405 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 }
1407 }
1408
1409 if (safe)
1410 {
1411 save_funccalp = save_funccal();
1412 ++sandbox;
1413 }
1414
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001415 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1416 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001418 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if (safe)
1420 {
1421 --sandbox;
1422 restore_funccal(save_funccalp);
1423 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001424 vim_free(argvars);
1425
1426 if (ret == FAIL)
1427 clear_tv(rettv);
1428
1429 return ret;
1430}
1431
1432/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001433 * Call vimL function "func" and return the result as a string.
1434 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001435 * Uses argv[argc] for the function arguments.
1436 */
1437 void *
1438call_func_retstr(func, argc, argv, safe)
1439 char_u *func;
1440 int argc;
1441 char_u **argv;
1442 int safe; /* use the sandbox */
1443{
1444 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001445 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001446
1447 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1448 return NULL;
1449
1450 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001451 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 return retval;
1453}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001454
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001455#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001456/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001457 * Call vimL function "func" and return the result as a number.
1458 * Returns -1 when calling the function fails.
1459 * Uses argv[argc] for the function arguments.
1460 */
1461 long
1462call_func_retnr(func, argc, argv, safe)
1463 char_u *func;
1464 int argc;
1465 char_u **argv;
1466 int safe; /* use the sandbox */
1467{
1468 typval_T rettv;
1469 long retval;
1470
1471 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1472 return -1;
1473
1474 retval = get_tv_number_chk(&rettv, NULL);
1475 clear_tv(&rettv);
1476 return retval;
1477}
1478#endif
1479
1480/*
1481 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001482 * Uses argv[argc] for the function arguments.
1483 */
1484 void *
1485call_func_retlist(func, argc, argv, safe)
1486 char_u *func;
1487 int argc;
1488 char_u **argv;
1489 int safe; /* use the sandbox */
1490{
1491 typval_T rettv;
1492
1493 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1494 return NULL;
1495
1496 if (rettv.v_type != VAR_LIST)
1497 {
1498 clear_tv(&rettv);
1499 return NULL;
1500 }
1501
1502 return rettv.vval.v_list;
1503}
1504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505#endif
1506
1507/*
1508 * Save the current function call pointer, and set it to NULL.
1509 * Used when executing autocommands and for ":source".
1510 */
1511 void *
1512save_funccal()
1513{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001514 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 current_funccal = NULL;
1517 return (void *)fc;
1518}
1519
1520 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001521restore_funccal(vfc)
1522 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001524 funccall_T *fc = (funccall_T *)vfc;
1525
1526 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527}
1528
Bram Moolenaar05159a02005-02-26 23:04:13 +00001529#if defined(FEAT_PROFILE) || defined(PROTO)
1530/*
1531 * Prepare profiling for entering a child or something else that is not
1532 * counted for the script/function itself.
1533 * Should always be called in pair with prof_child_exit().
1534 */
1535 void
1536prof_child_enter(tm)
1537 proftime_T *tm; /* place to store waittime */
1538{
1539 funccall_T *fc = current_funccal;
1540
1541 if (fc != NULL && fc->func->uf_profiling)
1542 profile_start(&fc->prof_child);
1543 script_prof_save(tm);
1544}
1545
1546/*
1547 * Take care of time spent in a child.
1548 * Should always be called after prof_child_enter().
1549 */
1550 void
1551prof_child_exit(tm)
1552 proftime_T *tm; /* where waittime was stored */
1553{
1554 funccall_T *fc = current_funccal;
1555
1556 if (fc != NULL && fc->func->uf_profiling)
1557 {
1558 profile_end(&fc->prof_child);
1559 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1560 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1561 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1562 }
1563 script_prof_restore(tm);
1564}
1565#endif
1566
1567
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568#ifdef FEAT_FOLDING
1569/*
1570 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1571 * it in "*cp". Doesn't give error messages.
1572 */
1573 int
1574eval_foldexpr(arg, cp)
1575 char_u *arg;
1576 int *cp;
1577{
Bram Moolenaar33570922005-01-25 22:26:29 +00001578 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 int retval;
1580 char_u *s;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001581 int use_sandbox = was_set_insecurely((char_u *)"foldexpr");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001584 if (use_sandbox)
1585 ++sandbox;
1586 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 retval = 0;
1590 else
1591 {
1592 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001593 if (tv.v_type == VAR_NUMBER)
1594 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001595 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 retval = 0;
1597 else
1598 {
1599 /* If the result is a string, check if there is a non-digit before
1600 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 if (!VIM_ISDIGIT(*s) && *s != '-')
1603 *cp = *s++;
1604 retval = atol((char *)s);
1605 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001606 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001609 if (use_sandbox)
1610 --sandbox;
1611 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
1613 return retval;
1614}
1615#endif
1616
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618 * ":let" list all variable values
1619 * ":let var1 var2" list variable values
1620 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001621 * ":let var += expr" assignment command.
1622 * ":let var -= expr" assignment command.
1623 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001624 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 */
1626 void
1627ex_let(eap)
1628 exarg_T *eap;
1629{
1630 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001631 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001632 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001634 int var_count = 0;
1635 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001636 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001638 expr = skip_var_list(arg, &var_count, &semicolon);
1639 if (expr == NULL)
1640 return;
1641 expr = vim_strchr(expr, '=');
1642 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001644 /*
1645 * ":let" without "=": list variables
1646 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001647 if (*arg == '[')
1648 EMSG(_(e_invarg));
1649 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001650 /* ":let var1 var2" */
1651 arg = list_arg_vars(eap, arg);
1652 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001655 list_glob_vars();
1656 list_buf_vars();
1657 list_win_vars();
1658 list_vim_vars();
1659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 eap->nextcmd = check_nextcmd(arg);
1661 }
1662 else
1663 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001664 op[0] = '=';
1665 op[1] = NUL;
1666 if (expr > arg)
1667 {
1668 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1669 op[0] = expr[-1]; /* +=, -= or .= */
1670 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001672
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (eap->skip)
1674 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (eap->skip)
1677 {
1678 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001679 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 --emsg_skip;
1681 }
1682 else if (i != FAIL)
1683 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001685 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001686 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
1688 }
1689}
1690
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691/*
1692 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1693 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001694 * When "nextchars" is not NULL it points to a string with characters that
1695 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1696 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 * Returns OK or FAIL;
1698 */
1699 static int
1700ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1701 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001702 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703 int copy; /* copy values from "tv", don't move */
1704 int semicolon; /* from skip_var_list() */
1705 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001706 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707{
1708 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001709 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001711 listitem_T *item;
1712 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713
1714 if (*arg != '[')
1715 {
1716 /*
1717 * ":let var = expr" or ":for var in list"
1718 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 return FAIL;
1721 return OK;
1722 }
1723
1724 /*
1725 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1726 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001727 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001728 {
1729 EMSG(_(e_listreq));
1730 return FAIL;
1731 }
1732
1733 i = list_len(l);
1734 if (semicolon == 0 && var_count < i)
1735 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001736 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001737 return FAIL;
1738 }
1739 if (var_count - semicolon > i)
1740 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001741 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001742 return FAIL;
1743 }
1744
1745 item = l->lv_first;
1746 while (*arg != ']')
1747 {
1748 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001749 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 item = item->li_next;
1751 if (arg == NULL)
1752 return FAIL;
1753
1754 arg = skipwhite(arg);
1755 if (*arg == ';')
1756 {
1757 /* Put the rest of the list (may be empty) in the var after ';'.
1758 * Create a new list for this. */
1759 l = list_alloc();
1760 if (l == NULL)
1761 return FAIL;
1762 while (item != NULL)
1763 {
1764 list_append_tv(l, &item->li_tv);
1765 item = item->li_next;
1766 }
1767
1768 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001769 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770 ltv.vval.v_list = l;
1771 l->lv_refcount = 1;
1772
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1774 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775 clear_tv(&ltv);
1776 if (arg == NULL)
1777 return FAIL;
1778 break;
1779 }
1780 else if (*arg != ',' && *arg != ']')
1781 {
1782 EMSG2(_(e_intern2), "ex_let_vars()");
1783 return FAIL;
1784 }
1785 }
1786
1787 return OK;
1788}
1789
1790/*
1791 * Skip over assignable variable "var" or list of variables "[var, var]".
1792 * Used for ":let varvar = expr" and ":for varvar in expr".
1793 * For "[var, var]" increment "*var_count" for each variable.
1794 * for "[var, var; var]" set "semicolon".
1795 * Return NULL for an error.
1796 */
1797 static char_u *
1798skip_var_list(arg, var_count, semicolon)
1799 char_u *arg;
1800 int *var_count;
1801 int *semicolon;
1802{
1803 char_u *p, *s;
1804
1805 if (*arg == '[')
1806 {
1807 /* "[var, var]": find the matching ']'. */
1808 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001809 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 {
1811 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1812 s = skip_var_one(p);
1813 if (s == p)
1814 {
1815 EMSG2(_(e_invarg2), p);
1816 return NULL;
1817 }
1818 ++*var_count;
1819
1820 p = skipwhite(s);
1821 if (*p == ']')
1822 break;
1823 else if (*p == ';')
1824 {
1825 if (*semicolon == 1)
1826 {
1827 EMSG(_("Double ; in list of variables"));
1828 return NULL;
1829 }
1830 *semicolon = 1;
1831 }
1832 else if (*p != ',')
1833 {
1834 EMSG2(_(e_invarg2), p);
1835 return NULL;
1836 }
1837 }
1838 return p + 1;
1839 }
1840 else
1841 return skip_var_one(arg);
1842}
1843
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001844/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001845 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1846 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001847 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 static char_u *
1849skip_var_one(arg)
1850 char_u *arg;
1851{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001852 if (*arg == '@' && arg[1] != NUL)
1853 return arg + 2;
1854 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1855 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856}
1857
Bram Moolenaara7043832005-01-21 11:56:39 +00001858/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001859 * List variables for hashtab "ht" with prefix "prefix".
1860 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001861 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001863list_hashtable_vars(ht, prefix, empty)
1864 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001865 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001866 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001867{
Bram Moolenaar33570922005-01-25 22:26:29 +00001868 hashitem_T *hi;
1869 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001870 int todo;
1871
1872 todo = ht->ht_used;
1873 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1874 {
1875 if (!HASHITEM_EMPTY(hi))
1876 {
1877 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001878 di = HI2DI(hi);
1879 if (empty || di->di_tv.v_type != VAR_STRING
1880 || di->di_tv.vval.v_string != NULL)
1881 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001882 }
1883 }
1884}
1885
1886/*
1887 * List global variables.
1888 */
1889 static void
1890list_glob_vars()
1891{
Bram Moolenaar33570922005-01-25 22:26:29 +00001892 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001893}
1894
1895/*
1896 * List buffer variables.
1897 */
1898 static void
1899list_buf_vars()
1900{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001901 char_u numbuf[NUMBUFLEN];
1902
Bram Moolenaar33570922005-01-25 22:26:29 +00001903 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001904
1905 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1906 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001907}
1908
1909/*
1910 * List window variables.
1911 */
1912 static void
1913list_win_vars()
1914{
Bram Moolenaar33570922005-01-25 22:26:29 +00001915 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001916}
1917
1918/*
1919 * List Vim variables.
1920 */
1921 static void
1922list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923{
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925}
1926
1927/*
1928 * List variables in "arg".
1929 */
1930 static char_u *
1931list_arg_vars(eap, arg)
1932 exarg_T *eap;
1933 char_u *arg;
1934{
1935 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001936 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001938 char_u *name_start;
1939 char_u *arg_subsc;
1940 char_u *tofree;
1941 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942
1943 while (!ends_excmd(*arg) && !got_int)
1944 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001945 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001947 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001948 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1949 {
1950 emsg_severe = TRUE;
1951 EMSG(_(e_trailing));
1952 break;
1953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001955 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001957 /* get_name_len() takes care of expanding curly braces */
1958 name_start = name = arg;
1959 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1960 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001962 /* This is mainly to keep test 49 working: when expanding
1963 * curly braces fails overrule the exception error message. */
1964 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001965 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001966 emsg_severe = TRUE;
1967 EMSG2(_(e_invarg2), arg);
1968 break;
1969 }
1970 error = TRUE;
1971 }
1972 else
1973 {
1974 if (tofree != NULL)
1975 name = tofree;
1976 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001977 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001978 else
1979 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001980 /* handle d.key, l[idx], f(expr) */
1981 arg_subsc = arg;
1982 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001983 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001984 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001985 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001986 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001987 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001988 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001989 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001990 case 'g': list_glob_vars(); break;
1991 case 'b': list_buf_vars(); break;
1992 case 'w': list_win_vars(); break;
1993 case 'v': list_vim_vars(); break;
1994 default:
1995 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001996 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001997 }
1998 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001999 {
2000 char_u numbuf[NUMBUFLEN];
2001 char_u *tf;
2002 int c;
2003 char_u *s;
2004
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002005 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002006 c = *arg;
2007 *arg = NUL;
2008 list_one_var_a((char_u *)"",
2009 arg == arg_subsc ? name : name_start,
2010 tv.v_type, s == NULL ? (char_u *)"" : s);
2011 *arg = c;
2012 vim_free(tf);
2013 }
2014 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002015 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002016 }
2017 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002018
2019 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002021
2022 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002023 }
2024
2025 return arg;
2026}
2027
2028/*
2029 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2030 * Returns a pointer to the char just after the var name.
2031 * Returns NULL if there is an error.
2032 */
2033 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002034ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002035 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002036 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002037 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002039 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040{
2041 int c1;
2042 char_u *name;
2043 char_u *p;
2044 char_u *arg_end = NULL;
2045 int len;
2046 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002047 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002048
2049 /*
2050 * ":let $VAR = expr": Set environment variable.
2051 */
2052 if (*arg == '$')
2053 {
2054 /* Find the end of the name. */
2055 ++arg;
2056 name = arg;
2057 len = get_env_len(&arg);
2058 if (len == 0)
2059 EMSG2(_(e_invarg2), name - 1);
2060 else
2061 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002062 if (op != NULL && (*op == '+' || *op == '-'))
2063 EMSG2(_(e_letwrong), op);
2064 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002065 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002066 EMSG(_(e_letunexp));
2067 else
2068 {
2069 c1 = name[len];
2070 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002071 p = get_tv_string_chk(tv);
2072 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002073 {
2074 int mustfree = FALSE;
2075 char_u *s = vim_getenv(name, &mustfree);
2076
2077 if (s != NULL)
2078 {
2079 p = tofree = concat_str(s, p);
2080 if (mustfree)
2081 vim_free(s);
2082 }
2083 }
2084 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002085 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002086 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002087 if (STRICMP(name, "HOME") == 0)
2088 init_homedir();
2089 else if (didset_vim && STRICMP(name, "VIM") == 0)
2090 didset_vim = FALSE;
2091 else if (didset_vimruntime
2092 && STRICMP(name, "VIMRUNTIME") == 0)
2093 didset_vimruntime = FALSE;
2094 arg_end = arg;
2095 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002097 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098 }
2099 }
2100 }
2101
2102 /*
2103 * ":let &option = expr": Set option value.
2104 * ":let &l:option = expr": Set local option value.
2105 * ":let &g:option = expr": Set global option value.
2106 */
2107 else if (*arg == '&')
2108 {
2109 /* Find the end of the name. */
2110 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002111 if (p == NULL || (endchars != NULL
2112 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002113 EMSG(_(e_letunexp));
2114 else
2115 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002116 long n;
2117 int opt_type;
2118 long numval;
2119 char_u *stringval = NULL;
2120 char_u *s;
2121
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002122 c1 = *p;
2123 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002124
2125 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002126 s = get_tv_string_chk(tv); /* != NULL if number or string */
2127 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002128 {
2129 opt_type = get_option_value(arg, &numval,
2130 &stringval, opt_flags);
2131 if ((opt_type == 1 && *op == '.')
2132 || (opt_type == 0 && *op != '.'))
2133 EMSG2(_(e_letwrong), op);
2134 else
2135 {
2136 if (opt_type == 1) /* number */
2137 {
2138 if (*op == '+')
2139 n = numval + n;
2140 else
2141 n = numval - n;
2142 }
2143 else if (opt_type == 0 && stringval != NULL) /* string */
2144 {
2145 s = concat_str(stringval, s);
2146 vim_free(stringval);
2147 stringval = s;
2148 }
2149 }
2150 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002151 if (s != NULL)
2152 {
2153 set_option_value(arg, n, s, opt_flags);
2154 arg_end = p;
2155 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002157 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158 }
2159 }
2160
2161 /*
2162 * ":let @r = expr": Set register contents.
2163 */
2164 else if (*arg == '@')
2165 {
2166 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002167 if (op != NULL && (*op == '+' || *op == '-'))
2168 EMSG2(_(e_letwrong), op);
2169 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002170 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002171 EMSG(_(e_letunexp));
2172 else
2173 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002174 char_u *tofree = NULL;
2175 char_u *s;
2176
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002177 p = get_tv_string_chk(tv);
2178 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002179 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002180 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002181 if (s != NULL)
2182 {
2183 p = tofree = concat_str(s, p);
2184 vim_free(s);
2185 }
2186 }
2187 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002188 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002189 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002190 arg_end = arg + 1;
2191 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002192 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 }
2194 }
2195
2196 /*
2197 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002198 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002200 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002202 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002204 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002205 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002207 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2208 EMSG(_(e_letunexp));
2209 else
2210 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002211 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002212 arg_end = p;
2213 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 }
2217
2218 else
2219 EMSG2(_(e_invarg2), arg);
2220
2221 return arg_end;
2222}
2223
2224/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002225 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2226 */
2227 static int
2228check_changedtick(arg)
2229 char_u *arg;
2230{
2231 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2232 {
2233 EMSG2(_(e_readonlyvar), arg);
2234 return TRUE;
2235 }
2236 return FALSE;
2237}
2238
2239/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002240 * Get an lval: variable, Dict item or List item that can be assigned a value
2241 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2242 * "name.key", "name.key[expr]" etc.
2243 * Indexing only works if "name" is an existing List or Dictionary.
2244 * "name" points to the start of the name.
2245 * If "rettv" is not NULL it points to the value to be assigned.
2246 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2247 * wrong; must end in space or cmd separator.
2248 *
2249 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002250 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 */
2253 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002254get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002256 typval_T *rettv;
2257 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 int unlet;
2259 int skip;
2260 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002261 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002264 char_u *expr_start, *expr_end;
2265 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002266 dictitem_T *v;
2267 typval_T var1;
2268 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002270 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002271 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002272 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002273 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002275 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002276 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002277
2278 if (skip)
2279 {
2280 /* When skipping just find the end of the name. */
2281 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002282 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002283 }
2284
2285 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002286 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002287 if (expr_start != NULL)
2288 {
2289 /* Don't expand the name when we already know there is an error. */
2290 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2291 && *p != '[' && *p != '.')
2292 {
2293 EMSG(_(e_trailing));
2294 return NULL;
2295 }
2296
2297 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2298 if (lp->ll_exp_name == NULL)
2299 {
2300 /* Report an invalid expression in braces, unless the
2301 * expression evaluation has been cancelled due to an
2302 * aborting error, an interrupt, or an exception. */
2303 if (!aborting() && !quiet)
2304 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002305 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 EMSG2(_(e_invarg2), name);
2307 return NULL;
2308 }
2309 }
2310 lp->ll_name = lp->ll_exp_name;
2311 }
2312 else
2313 lp->ll_name = name;
2314
2315 /* Without [idx] or .key we are done. */
2316 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2317 return p;
2318
2319 cc = *p;
2320 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002321 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 if (v == NULL && !quiet)
2323 EMSG2(_(e_undefvar), lp->ll_name);
2324 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 if (v == NULL)
2326 return NULL;
2327
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002328 /*
2329 * Loop until no more [idx] or .key is following.
2330 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002331 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002334 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2335 && !(lp->ll_tv->v_type == VAR_DICT
2336 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 if (!quiet)
2339 EMSG(_("E689: Can only index a List or Dictionary"));
2340 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002344 if (!quiet)
2345 EMSG(_("E708: [:] must come last"));
2346 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002348
Bram Moolenaar8c711452005-01-14 21:53:12 +00002349 len = -1;
2350 if (*p == '.')
2351 {
2352 key = p + 1;
2353 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2354 ;
2355 if (len == 0)
2356 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002357 if (!quiet)
2358 EMSG(_(e_emptykey));
2359 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002360 }
2361 p = key + len;
2362 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002363 else
2364 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002365 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002366 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002367 if (*p == ':')
2368 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002369 else
2370 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002371 empty1 = FALSE;
2372 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002374 if (get_tv_string_chk(&var1) == NULL)
2375 {
2376 /* not a number or string */
2377 clear_tv(&var1);
2378 return NULL;
2379 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002380 }
2381
2382 /* Optionally get the second index [ :expr]. */
2383 if (*p == ':')
2384 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002385 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002386 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002389 if (!empty1)
2390 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002392 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 if (rettv != NULL && (rettv->v_type != VAR_LIST
2394 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 if (!quiet)
2397 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002398 if (!empty1)
2399 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002400 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002401 }
2402 p = skipwhite(p + 1);
2403 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002404 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002405 else
2406 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002408 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2409 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002410 if (!empty1)
2411 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002413 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002414 if (get_tv_string_chk(&var2) == NULL)
2415 {
2416 /* not a number or string */
2417 if (!empty1)
2418 clear_tv(&var1);
2419 clear_tv(&var2);
2420 return NULL;
2421 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002422 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002424 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002425 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002426 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002427
Bram Moolenaar8c711452005-01-14 21:53:12 +00002428 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002429 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 if (!quiet)
2431 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002432 if (!empty1)
2433 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002435 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002437 }
2438
2439 /* Skip to past ']'. */
2440 ++p;
2441 }
2442
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002444 {
2445 if (len == -1)
2446 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002448 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 if (*key == NUL)
2450 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 if (!quiet)
2452 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 }
2456 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 lp->ll_list = NULL;
2458 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002459 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002462 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002464 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002467 if (len == -1)
2468 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002470 }
2471 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 if (len == -1)
2476 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002478 p = NULL;
2479 break;
2480 }
2481 if (len == -1)
2482 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 }
2485 else
2486 {
2487 /*
2488 * Get the number and item for the only or first index of the List.
2489 */
2490 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 else
2493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002494 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 clear_tv(&var1);
2496 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 lp->ll_list = lp->ll_tv->vval.v_list;
2499 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2500 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 if (!quiet)
2503 EMSGN(_(e_listidx), lp->ll_n1);
2504 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002507 }
2508
2509 /*
2510 * May need to find the item or absolute index for the second
2511 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 * When no index given: "lp->ll_empty2" is TRUE.
2513 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002517 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002518 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 if (ni == NULL)
2523 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 if (!quiet)
2525 EMSGN(_(e_listidx), lp->ll_n2);
2526 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002527 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 }
2530
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2532 if (lp->ll_n1 < 0)
2533 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2534 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002535 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 if (!quiet)
2537 EMSGN(_(e_listidx), lp->ll_n2);
2538 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002539 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002540 }
2541
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002543 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544 }
2545
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 return p;
2547}
2548
2549/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002550 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 */
2552 static void
2553clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555{
2556 vim_free(lp->ll_exp_name);
2557 vim_free(lp->ll_newkey);
2558}
2559
2560/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002561 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002563 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 */
2565 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002566set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002569 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002571 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572{
2573 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002574 listitem_T *ri;
2575 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576
2577 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002578 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 cc = *endp;
2582 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002583 if (op != NULL && *op != '=')
2584 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002585 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002586
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002587 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002588 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2589 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002590 {
2591 if (tv_op(&tv, rettv, op) == OK)
2592 set_var(lp->ll_name, &tv, FALSE);
2593 clear_tv(&tv);
2594 }
2595 }
2596 else
2597 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002601 else if (tv_check_lock(lp->ll_newkey == NULL
2602 ? lp->ll_tv->v_lock
2603 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2604 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 else if (lp->ll_range)
2606 {
2607 /*
2608 * Assign the List values to the list items.
2609 */
2610 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002612 if (op != NULL && *op != '=')
2613 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2614 else
2615 {
2616 clear_tv(&lp->ll_li->li_tv);
2617 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2618 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 ri = ri->li_next;
2620 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2621 break;
2622 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002625 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 ri = NULL;
2628 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 lp->ll_li = lp->ll_li->li_next;
2632 ++lp->ll_n1;
2633 }
2634 if (ri != NULL)
2635 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002636 else if (lp->ll_empty2
2637 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 : lp->ll_n1 != lp->ll_n2)
2639 EMSG(_("E711: List value has not enough items"));
2640 }
2641 else
2642 {
2643 /*
2644 * Assign to a List or Dictionary item.
2645 */
2646 if (lp->ll_newkey != NULL)
2647 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002648 if (op != NULL && *op != '=')
2649 {
2650 EMSG2(_(e_letwrong), op);
2651 return;
2652 }
2653
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002655 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (di == NULL)
2657 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002658 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2659 {
2660 vim_free(di);
2661 return;
2662 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002665 else if (op != NULL && *op != '=')
2666 {
2667 tv_op(lp->ll_tv, rettv, op);
2668 return;
2669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 /*
2674 * Assign the value to the variable or list item.
2675 */
2676 if (copy)
2677 copy_tv(rettv, lp->ll_tv);
2678 else
2679 {
2680 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002681 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002683 }
2684 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002685}
2686
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002687/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002688 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2689 * Returns OK or FAIL.
2690 */
2691 static int
2692tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002693 typval_T *tv1;
2694 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002695 char_u *op;
2696{
2697 long n;
2698 char_u numbuf[NUMBUFLEN];
2699 char_u *s;
2700
2701 /* Can't do anything with a Funcref or a Dict on the right. */
2702 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2703 {
2704 switch (tv1->v_type)
2705 {
2706 case VAR_DICT:
2707 case VAR_FUNC:
2708 break;
2709
2710 case VAR_LIST:
2711 if (*op != '+' || tv2->v_type != VAR_LIST)
2712 break;
2713 /* List += List */
2714 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2715 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2716 return OK;
2717
2718 case VAR_NUMBER:
2719 case VAR_STRING:
2720 if (tv2->v_type == VAR_LIST)
2721 break;
2722 if (*op == '+' || *op == '-')
2723 {
2724 /* nr += nr or nr -= nr*/
2725 n = get_tv_number(tv1);
2726 if (*op == '+')
2727 n += get_tv_number(tv2);
2728 else
2729 n -= get_tv_number(tv2);
2730 clear_tv(tv1);
2731 tv1->v_type = VAR_NUMBER;
2732 tv1->vval.v_number = n;
2733 }
2734 else
2735 {
2736 /* str .= str */
2737 s = get_tv_string(tv1);
2738 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2739 clear_tv(tv1);
2740 tv1->v_type = VAR_STRING;
2741 tv1->vval.v_string = s;
2742 }
2743 return OK;
2744 }
2745 }
2746
2747 EMSG2(_(e_letwrong), op);
2748 return FAIL;
2749}
2750
2751/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002752 * Add a watcher to a list.
2753 */
2754 static void
2755list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002756 list_T *l;
2757 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002758{
2759 lw->lw_next = l->lv_watch;
2760 l->lv_watch = lw;
2761}
2762
2763/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002764 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002765 * No warning when it isn't found...
2766 */
2767 static void
2768list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002769 list_T *l;
2770 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002771{
Bram Moolenaar33570922005-01-25 22:26:29 +00002772 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002773
2774 lwp = &l->lv_watch;
2775 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2776 {
2777 if (lw == lwrem)
2778 {
2779 *lwp = lw->lw_next;
2780 break;
2781 }
2782 lwp = &lw->lw_next;
2783 }
2784}
2785
2786/*
2787 * Just before removing an item from a list: advance watchers to the next
2788 * item.
2789 */
2790 static void
2791list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002792 list_T *l;
2793 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002794{
Bram Moolenaar33570922005-01-25 22:26:29 +00002795 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002796
2797 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2798 if (lw->lw_item == item)
2799 lw->lw_item = item->li_next;
2800}
2801
2802/*
2803 * Evaluate the expression used in a ":for var in expr" command.
2804 * "arg" points to "var".
2805 * Set "*errp" to TRUE for an error, FALSE otherwise;
2806 * Return a pointer that holds the info. Null when there is an error.
2807 */
2808 void *
2809eval_for_line(arg, errp, nextcmdp, skip)
2810 char_u *arg;
2811 int *errp;
2812 char_u **nextcmdp;
2813 int skip;
2814{
Bram Moolenaar33570922005-01-25 22:26:29 +00002815 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002816 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002817 typval_T tv;
2818 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002819
2820 *errp = TRUE; /* default: there is an error */
2821
Bram Moolenaar33570922005-01-25 22:26:29 +00002822 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002823 if (fi == NULL)
2824 return NULL;
2825
2826 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2827 if (expr == NULL)
2828 return fi;
2829
2830 expr = skipwhite(expr);
2831 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2832 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002833 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002834 return fi;
2835 }
2836
2837 if (skip)
2838 ++emsg_skip;
2839 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2840 {
2841 *errp = FALSE;
2842 if (!skip)
2843 {
2844 l = tv.vval.v_list;
2845 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002846 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002847 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002848 clear_tv(&tv);
2849 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002850 else
2851 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002852 /* No need to increment the refcount, it's already set for the
2853 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002854 fi->fi_list = l;
2855 list_add_watch(l, &fi->fi_lw);
2856 fi->fi_lw.lw_item = l->lv_first;
2857 }
2858 }
2859 }
2860 if (skip)
2861 --emsg_skip;
2862
2863 return fi;
2864}
2865
2866/*
2867 * Use the first item in a ":for" list. Advance to the next.
2868 * Assign the values to the variable (list). "arg" points to the first one.
2869 * Return TRUE when a valid item was found, FALSE when at end of list or
2870 * something wrong.
2871 */
2872 int
2873next_for_item(fi_void, arg)
2874 void *fi_void;
2875 char_u *arg;
2876{
Bram Moolenaar33570922005-01-25 22:26:29 +00002877 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002878 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002880
2881 item = fi->fi_lw.lw_item;
2882 if (item == NULL)
2883 result = FALSE;
2884 else
2885 {
2886 fi->fi_lw.lw_item = item->li_next;
2887 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2888 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2889 }
2890 return result;
2891}
2892
2893/*
2894 * Free the structure used to store info used by ":for".
2895 */
2896 void
2897free_for_info(fi_void)
2898 void *fi_void;
2899{
Bram Moolenaar33570922005-01-25 22:26:29 +00002900 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002901
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002902 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002903 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002904 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002905 list_unref(fi->fi_list);
2906 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002907 vim_free(fi);
2908}
2909
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2911
2912 void
2913set_context_for_expression(xp, arg, cmdidx)
2914 expand_T *xp;
2915 char_u *arg;
2916 cmdidx_T cmdidx;
2917{
2918 int got_eq = FALSE;
2919 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002920 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002922 if (cmdidx == CMD_let)
2923 {
2924 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002925 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002926 {
2927 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002928 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002929 {
2930 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002931 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002932 if (vim_iswhite(*p))
2933 break;
2934 }
2935 return;
2936 }
2937 }
2938 else
2939 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2940 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 while ((xp->xp_pattern = vim_strpbrk(arg,
2942 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2943 {
2944 c = *xp->xp_pattern;
2945 if (c == '&')
2946 {
2947 c = xp->xp_pattern[1];
2948 if (c == '&')
2949 {
2950 ++xp->xp_pattern;
2951 xp->xp_context = cmdidx != CMD_let || got_eq
2952 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2953 }
2954 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002957 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2958 xp->xp_pattern += 2;
2959
2960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 }
2962 else if (c == '$')
2963 {
2964 /* environment variable */
2965 xp->xp_context = EXPAND_ENV_VARS;
2966 }
2967 else if (c == '=')
2968 {
2969 got_eq = TRUE;
2970 xp->xp_context = EXPAND_EXPRESSION;
2971 }
2972 else if (c == '<'
2973 && xp->xp_context == EXPAND_FUNCTIONS
2974 && vim_strchr(xp->xp_pattern, '(') == NULL)
2975 {
2976 /* Function name can start with "<SNR>" */
2977 break;
2978 }
2979 else if (cmdidx != CMD_let || got_eq)
2980 {
2981 if (c == '"') /* string */
2982 {
2983 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2984 if (c == '\\' && xp->xp_pattern[1] != NUL)
2985 ++xp->xp_pattern;
2986 xp->xp_context = EXPAND_NOTHING;
2987 }
2988 else if (c == '\'') /* literal string */
2989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002990 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2992 /* skip */ ;
2993 xp->xp_context = EXPAND_NOTHING;
2994 }
2995 else if (c == '|')
2996 {
2997 if (xp->xp_pattern[1] == '|')
2998 {
2999 ++xp->xp_pattern;
3000 xp->xp_context = EXPAND_EXPRESSION;
3001 }
3002 else
3003 xp->xp_context = EXPAND_COMMANDS;
3004 }
3005 else
3006 xp->xp_context = EXPAND_EXPRESSION;
3007 }
3008 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003009 /* Doesn't look like something valid, expand as an expression
3010 * anyway. */
3011 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 arg = xp->xp_pattern;
3013 if (*arg != NUL)
3014 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3015 /* skip */ ;
3016 }
3017 xp->xp_pattern = arg;
3018}
3019
3020#endif /* FEAT_CMDL_COMPL */
3021
3022/*
3023 * ":1,25call func(arg1, arg2)" function call.
3024 */
3025 void
3026ex_call(eap)
3027 exarg_T *eap;
3028{
3029 char_u *arg = eap->arg;
3030 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003032 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003034 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 linenr_T lnum;
3036 int doesrange;
3037 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003038 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003040 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3041 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003042 if (tofree == NULL)
3043 return;
3044
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003045 /* Increase refcount on dictionary, it could get deleted when evaluating
3046 * the arguments. */
3047 if (fudi.fd_dict != NULL)
3048 ++fudi.fd_dict->dv_refcount;
3049
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003050 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3051 len = STRLEN(tofree);
3052 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
Bram Moolenaar532c7802005-01-27 14:44:31 +00003054 /* Skip white space to allow ":call func ()". Not good, but required for
3055 * backward compatibility. */
3056 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003057 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058
3059 if (*startarg != '(')
3060 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003061 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 goto end;
3063 }
3064
3065 /*
3066 * When skipping, evaluate the function once, to find the end of the
3067 * arguments.
3068 * When the function takes a range, this is discovered after the first
3069 * call, and the loop is broken.
3070 */
3071 if (eap->skip)
3072 {
3073 ++emsg_skip;
3074 lnum = eap->line2; /* do it once, also with an invalid range */
3075 }
3076 else
3077 lnum = eap->line1;
3078 for ( ; lnum <= eap->line2; ++lnum)
3079 {
3080 if (!eap->skip && eap->addr_count > 0)
3081 {
3082 curwin->w_cursor.lnum = lnum;
3083 curwin->w_cursor.col = 0;
3084 }
3085 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003086 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003087 eap->line1, eap->line2, &doesrange,
3088 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 {
3090 failed = TRUE;
3091 break;
3092 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003093 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (doesrange || eap->skip)
3095 break;
3096 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003097 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003098 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003099 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (aborting())
3101 break;
3102 }
3103 if (eap->skip)
3104 --emsg_skip;
3105
3106 if (!failed)
3107 {
3108 /* Check for trailing illegal characters and a following command. */
3109 if (!ends_excmd(*arg))
3110 {
3111 emsg_severe = TRUE;
3112 EMSG(_(e_trailing));
3113 }
3114 else
3115 eap->nextcmd = check_nextcmd(arg);
3116 }
3117
3118end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003119 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003120 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121}
3122
3123/*
3124 * ":unlet[!] var1 ... " command.
3125 */
3126 void
3127ex_unlet(eap)
3128 exarg_T *eap;
3129{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003130 ex_unletlock(eap, eap->arg, 0);
3131}
3132
3133/*
3134 * ":lockvar" and ":unlockvar" commands
3135 */
3136 void
3137ex_lockvar(eap)
3138 exarg_T *eap;
3139{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003141 int deep = 2;
3142
3143 if (eap->forceit)
3144 deep = -1;
3145 else if (vim_isdigit(*arg))
3146 {
3147 deep = getdigits(&arg);
3148 arg = skipwhite(arg);
3149 }
3150
3151 ex_unletlock(eap, arg, deep);
3152}
3153
3154/*
3155 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3156 */
3157 static void
3158ex_unletlock(eap, argstart, deep)
3159 exarg_T *eap;
3160 char_u *argstart;
3161 int deep;
3162{
3163 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003166 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
3168 do
3169 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003170 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003171 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3172 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003173 if (lv.ll_name == NULL)
3174 error = TRUE; /* error but continue parsing */
3175 if (name_end == NULL || (!vim_iswhite(*name_end)
3176 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003178 if (name_end != NULL)
3179 {
3180 emsg_severe = TRUE;
3181 EMSG(_(e_trailing));
3182 }
3183 if (!(eap->skip || error))
3184 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 break;
3186 }
3187
3188 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003189 {
3190 if (eap->cmdidx == CMD_unlet)
3191 {
3192 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3193 error = TRUE;
3194 }
3195 else
3196 {
3197 if (do_lock_var(&lv, name_end, deep,
3198 eap->cmdidx == CMD_lockvar) == FAIL)
3199 error = TRUE;
3200 }
3201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003203 if (!eap->skip)
3204 clear_lval(&lv);
3205
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 arg = skipwhite(name_end);
3207 } while (!ends_excmd(*arg));
3208
3209 eap->nextcmd = check_nextcmd(arg);
3210}
3211
Bram Moolenaar8c711452005-01-14 21:53:12 +00003212 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003213do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003215 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003216 int forceit;
3217{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003218 int ret = OK;
3219 int cc;
3220
3221 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003222 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003223 cc = *name_end;
3224 *name_end = NUL;
3225
3226 /* Normal name or expanded name. */
3227 if (check_changedtick(lp->ll_name))
3228 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003229 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003230 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003231 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003232 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003233 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3234 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003235 else if (lp->ll_range)
3236 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003238
3239 /* Delete a range of List items. */
3240 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3241 {
3242 li = lp->ll_li->li_next;
3243 listitem_remove(lp->ll_list, lp->ll_li);
3244 lp->ll_li = li;
3245 ++lp->ll_n1;
3246 }
3247 }
3248 else
3249 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003250 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003251 /* unlet a List item. */
3252 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003253 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003254 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003255 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003256 }
3257
3258 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003259}
3260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261/*
3262 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003263 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 */
3265 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003266do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003268 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
Bram Moolenaar33570922005-01-25 22:26:29 +00003270 hashtab_T *ht;
3271 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003272 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 ht = find_var_ht(name, &varname);
3275 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 hi = hash_find(ht, varname);
3278 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003279 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003280 if (var_check_ro(HI2DI(hi)->di_flags, name))
3281 return FAIL;
3282 delete_var(ht, hi);
3283 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003286 if (forceit)
3287 return OK;
3288 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 return FAIL;
3290}
3291
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003292/*
3293 * Lock or unlock variable indicated by "lp".
3294 * "deep" is the levels to go (-1 for unlimited);
3295 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3296 */
3297 static int
3298do_lock_var(lp, name_end, deep, lock)
3299 lval_T *lp;
3300 char_u *name_end;
3301 int deep;
3302 int lock;
3303{
3304 int ret = OK;
3305 int cc;
3306 dictitem_T *di;
3307
3308 if (deep == 0) /* nothing to do */
3309 return OK;
3310
3311 if (lp->ll_tv == NULL)
3312 {
3313 cc = *name_end;
3314 *name_end = NUL;
3315
3316 /* Normal name or expanded name. */
3317 if (check_changedtick(lp->ll_name))
3318 ret = FAIL;
3319 else
3320 {
3321 di = find_var(lp->ll_name, NULL);
3322 if (di == NULL)
3323 ret = FAIL;
3324 else
3325 {
3326 if (lock)
3327 di->di_flags |= DI_FLAGS_LOCK;
3328 else
3329 di->di_flags &= ~DI_FLAGS_LOCK;
3330 item_lock(&di->di_tv, deep, lock);
3331 }
3332 }
3333 *name_end = cc;
3334 }
3335 else if (lp->ll_range)
3336 {
3337 listitem_T *li = lp->ll_li;
3338
3339 /* (un)lock a range of List items. */
3340 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3341 {
3342 item_lock(&li->li_tv, deep, lock);
3343 li = li->li_next;
3344 ++lp->ll_n1;
3345 }
3346 }
3347 else if (lp->ll_list != NULL)
3348 /* (un)lock a List item. */
3349 item_lock(&lp->ll_li->li_tv, deep, lock);
3350 else
3351 /* un(lock) a Dictionary item. */
3352 item_lock(&lp->ll_di->di_tv, deep, lock);
3353
3354 return ret;
3355}
3356
3357/*
3358 * Lock or unlock an item. "deep" is nr of levels to go.
3359 */
3360 static void
3361item_lock(tv, deep, lock)
3362 typval_T *tv;
3363 int deep;
3364 int lock;
3365{
3366 static int recurse = 0;
3367 list_T *l;
3368 listitem_T *li;
3369 dict_T *d;
3370 hashitem_T *hi;
3371 int todo;
3372
3373 if (recurse >= DICT_MAXNEST)
3374 {
3375 EMSG(_("E743: variable nested too deep for (un)lock"));
3376 return;
3377 }
3378 if (deep == 0)
3379 return;
3380 ++recurse;
3381
3382 /* lock/unlock the item itself */
3383 if (lock)
3384 tv->v_lock |= VAR_LOCKED;
3385 else
3386 tv->v_lock &= ~VAR_LOCKED;
3387
3388 switch (tv->v_type)
3389 {
3390 case VAR_LIST:
3391 if ((l = tv->vval.v_list) != NULL)
3392 {
3393 if (lock)
3394 l->lv_lock |= VAR_LOCKED;
3395 else
3396 l->lv_lock &= ~VAR_LOCKED;
3397 if (deep < 0 || deep > 1)
3398 /* recursive: lock/unlock the items the List contains */
3399 for (li = l->lv_first; li != NULL; li = li->li_next)
3400 item_lock(&li->li_tv, deep - 1, lock);
3401 }
3402 break;
3403 case VAR_DICT:
3404 if ((d = tv->vval.v_dict) != NULL)
3405 {
3406 if (lock)
3407 d->dv_lock |= VAR_LOCKED;
3408 else
3409 d->dv_lock &= ~VAR_LOCKED;
3410 if (deep < 0 || deep > 1)
3411 {
3412 /* recursive: lock/unlock the items the List contains */
3413 todo = d->dv_hashtab.ht_used;
3414 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3415 {
3416 if (!HASHITEM_EMPTY(hi))
3417 {
3418 --todo;
3419 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3420 }
3421 }
3422 }
3423 }
3424 }
3425 --recurse;
3426}
3427
Bram Moolenaara40058a2005-07-11 22:42:07 +00003428/*
3429 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3430 * it refers to a List or Dictionary that is locked.
3431 */
3432 static int
3433tv_islocked(tv)
3434 typval_T *tv;
3435{
3436 return (tv->v_lock & VAR_LOCKED)
3437 || (tv->v_type == VAR_LIST
3438 && tv->vval.v_list != NULL
3439 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3440 || (tv->v_type == VAR_DICT
3441 && tv->vval.v_dict != NULL
3442 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3443}
3444
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3446/*
3447 * Delete all "menutrans_" variables.
3448 */
3449 void
3450del_menutrans_vars()
3451{
Bram Moolenaar33570922005-01-25 22:26:29 +00003452 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003453 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
Bram Moolenaar33570922005-01-25 22:26:29 +00003455 hash_lock(&globvarht);
3456 todo = globvarht.ht_used;
3457 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003458 {
3459 if (!HASHITEM_EMPTY(hi))
3460 {
3461 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003462 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3463 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003464 }
3465 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003466 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467}
3468#endif
3469
3470#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3471
3472/*
3473 * Local string buffer for the next two functions to store a variable name
3474 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3475 * get_user_var_name().
3476 */
3477
3478static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3479
3480static char_u *varnamebuf = NULL;
3481static int varnamebuflen = 0;
3482
3483/*
3484 * Function to concatenate a prefix and a variable name.
3485 */
3486 static char_u *
3487cat_prefix_varname(prefix, name)
3488 int prefix;
3489 char_u *name;
3490{
3491 int len;
3492
3493 len = (int)STRLEN(name) + 3;
3494 if (len > varnamebuflen)
3495 {
3496 vim_free(varnamebuf);
3497 len += 10; /* some additional space */
3498 varnamebuf = alloc(len);
3499 if (varnamebuf == NULL)
3500 {
3501 varnamebuflen = 0;
3502 return NULL;
3503 }
3504 varnamebuflen = len;
3505 }
3506 *varnamebuf = prefix;
3507 varnamebuf[1] = ':';
3508 STRCPY(varnamebuf + 2, name);
3509 return varnamebuf;
3510}
3511
3512/*
3513 * Function given to ExpandGeneric() to obtain the list of user defined
3514 * (global/buffer/window/built-in) variable names.
3515 */
3516/*ARGSUSED*/
3517 char_u *
3518get_user_var_name(xp, idx)
3519 expand_T *xp;
3520 int idx;
3521{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003522 static long_u gdone;
3523 static long_u bdone;
3524 static long_u wdone;
3525 static int vidx;
3526 static hashitem_T *hi;
3527 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
3529 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003530 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003531
3532 /* Global variables */
3533 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003535 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003536 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003537 else
3538 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003539 while (HASHITEM_EMPTY(hi))
3540 ++hi;
3541 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3542 return cat_prefix_varname('g', hi->hi_key);
3543 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003545
3546 /* b: variables */
3547 ht = &curbuf->b_vars.dv_hashtab;
3548 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003550 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003551 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003552 else
3553 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003554 while (HASHITEM_EMPTY(hi))
3555 ++hi;
3556 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003558 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003560 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 return (char_u *)"b:changedtick";
3562 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003563
3564 /* w: variables */
3565 ht = &curwin->w_vars.dv_hashtab;
3566 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003568 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003569 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003570 else
3571 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003572 while (HASHITEM_EMPTY(hi))
3573 ++hi;
3574 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003576
3577 /* v: variables */
3578 if (vidx < VV_LEN)
3579 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580
3581 vim_free(varnamebuf);
3582 varnamebuf = NULL;
3583 varnamebuflen = 0;
3584 return NULL;
3585}
3586
3587#endif /* FEAT_CMDL_COMPL */
3588
3589/*
3590 * types for expressions.
3591 */
3592typedef enum
3593{
3594 TYPE_UNKNOWN = 0
3595 , TYPE_EQUAL /* == */
3596 , TYPE_NEQUAL /* != */
3597 , TYPE_GREATER /* > */
3598 , TYPE_GEQUAL /* >= */
3599 , TYPE_SMALLER /* < */
3600 , TYPE_SEQUAL /* <= */
3601 , TYPE_MATCH /* =~ */
3602 , TYPE_NOMATCH /* !~ */
3603} exptype_T;
3604
3605/*
3606 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003607 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3609 */
3610
3611/*
3612 * Handle zero level expression.
3613 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003614 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003615 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 * Return OK or FAIL.
3617 */
3618 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003619eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 char_u **nextcmd;
3623 int evaluate;
3624{
3625 int ret;
3626 char_u *p;
3627
3628 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003629 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 if (ret == FAIL || !ends_excmd(*p))
3631 {
3632 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003633 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 /*
3635 * Report the invalid expression unless the expression evaluation has
3636 * been cancelled due to an aborting error, an interrupt, or an
3637 * exception.
3638 */
3639 if (!aborting())
3640 EMSG2(_(e_invexpr2), arg);
3641 ret = FAIL;
3642 }
3643 if (nextcmd != NULL)
3644 *nextcmd = check_nextcmd(p);
3645
3646 return ret;
3647}
3648
3649/*
3650 * Handle top level expression:
3651 * expr1 ? expr0 : expr0
3652 *
3653 * "arg" must point to the first non-white of the expression.
3654 * "arg" is advanced to the next non-white after the recognized expression.
3655 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003656 * Note: "rettv.v_lock" is not set.
3657 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 * Return OK or FAIL.
3659 */
3660 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003661eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 int evaluate;
3665{
3666 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003667 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
3669 /*
3670 * Get the first variable.
3671 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003672 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 return FAIL;
3674
3675 if ((*arg)[0] == '?')
3676 {
3677 result = FALSE;
3678 if (evaluate)
3679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003680 int error = FALSE;
3681
3682 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003684 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003685 if (error)
3686 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 }
3688
3689 /*
3690 * Get the second variable.
3691 */
3692 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003693 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 return FAIL;
3695
3696 /*
3697 * Check for the ":".
3698 */
3699 if ((*arg)[0] != ':')
3700 {
3701 EMSG(_("E109: Missing ':' after '?'"));
3702 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003703 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 return FAIL;
3705 }
3706
3707 /*
3708 * Get the third variable.
3709 */
3710 *arg = skipwhite(*arg + 1);
3711 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3712 {
3713 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003714 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 return FAIL;
3716 }
3717 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003718 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720
3721 return OK;
3722}
3723
3724/*
3725 * Handle first level expression:
3726 * expr2 || expr2 || expr2 logical OR
3727 *
3728 * "arg" must point to the first non-white of the expression.
3729 * "arg" is advanced to the next non-white after the recognized expression.
3730 *
3731 * Return OK or FAIL.
3732 */
3733 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 int evaluate;
3738{
Bram Moolenaar33570922005-01-25 22:26:29 +00003739 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 long result;
3741 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003742 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
3744 /*
3745 * Get the first variable.
3746 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003747 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 return FAIL;
3749
3750 /*
3751 * Repeat until there is no following "||".
3752 */
3753 first = TRUE;
3754 result = FALSE;
3755 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3756 {
3757 if (evaluate && first)
3758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003759 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003761 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003762 if (error)
3763 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 first = FALSE;
3765 }
3766
3767 /*
3768 * Get the second variable.
3769 */
3770 *arg = skipwhite(*arg + 2);
3771 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3772 return FAIL;
3773
3774 /*
3775 * Compute the result.
3776 */
3777 if (evaluate && !result)
3778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003779 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003782 if (error)
3783 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 }
3785 if (evaluate)
3786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003787 rettv->v_type = VAR_NUMBER;
3788 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 }
3791
3792 return OK;
3793}
3794
3795/*
3796 * Handle second level expression:
3797 * expr3 && expr3 && expr3 logical AND
3798 *
3799 * "arg" must point to the first non-white of the expression.
3800 * "arg" is advanced to the next non-white after the recognized expression.
3801 *
3802 * Return OK or FAIL.
3803 */
3804 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003805eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 int evaluate;
3809{
Bram Moolenaar33570922005-01-25 22:26:29 +00003810 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 long result;
3812 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003813 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
3815 /*
3816 * Get the first variable.
3817 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003818 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 return FAIL;
3820
3821 /*
3822 * Repeat until there is no following "&&".
3823 */
3824 first = TRUE;
3825 result = TRUE;
3826 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3827 {
3828 if (evaluate && first)
3829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003830 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003832 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003833 if (error)
3834 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 first = FALSE;
3836 }
3837
3838 /*
3839 * Get the second variable.
3840 */
3841 *arg = skipwhite(*arg + 2);
3842 if (eval4(arg, &var2, evaluate && result) == FAIL)
3843 return FAIL;
3844
3845 /*
3846 * Compute the result.
3847 */
3848 if (evaluate && result)
3849 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003850 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003852 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003853 if (error)
3854 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
3856 if (evaluate)
3857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003858 rettv->v_type = VAR_NUMBER;
3859 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 }
3861 }
3862
3863 return OK;
3864}
3865
3866/*
3867 * Handle third level expression:
3868 * var1 == var2
3869 * var1 =~ var2
3870 * var1 != var2
3871 * var1 !~ var2
3872 * var1 > var2
3873 * var1 >= var2
3874 * var1 < var2
3875 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003876 * var1 is var2
3877 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 *
3879 * "arg" must point to the first non-white of the expression.
3880 * "arg" is advanced to the next non-white after the recognized expression.
3881 *
3882 * Return OK or FAIL.
3883 */
3884 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003885eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 int evaluate;
3889{
Bram Moolenaar33570922005-01-25 22:26:29 +00003890 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 char_u *p;
3892 int i;
3893 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003894 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 int len = 2;
3896 long n1, n2;
3897 char_u *s1, *s2;
3898 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3899 regmatch_T regmatch;
3900 int ic;
3901 char_u *save_cpo;
3902
3903 /*
3904 * Get the first variable.
3905 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003906 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 return FAIL;
3908
3909 p = *arg;
3910 switch (p[0])
3911 {
3912 case '=': if (p[1] == '=')
3913 type = TYPE_EQUAL;
3914 else if (p[1] == '~')
3915 type = TYPE_MATCH;
3916 break;
3917 case '!': if (p[1] == '=')
3918 type = TYPE_NEQUAL;
3919 else if (p[1] == '~')
3920 type = TYPE_NOMATCH;
3921 break;
3922 case '>': if (p[1] != '=')
3923 {
3924 type = TYPE_GREATER;
3925 len = 1;
3926 }
3927 else
3928 type = TYPE_GEQUAL;
3929 break;
3930 case '<': if (p[1] != '=')
3931 {
3932 type = TYPE_SMALLER;
3933 len = 1;
3934 }
3935 else
3936 type = TYPE_SEQUAL;
3937 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003938 case 'i': if (p[1] == 's')
3939 {
3940 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3941 len = 5;
3942 if (!vim_isIDc(p[len]))
3943 {
3944 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3945 type_is = TRUE;
3946 }
3947 }
3948 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950
3951 /*
3952 * If there is a comparitive operator, use it.
3953 */
3954 if (type != TYPE_UNKNOWN)
3955 {
3956 /* extra question mark appended: ignore case */
3957 if (p[len] == '?')
3958 {
3959 ic = TRUE;
3960 ++len;
3961 }
3962 /* extra '#' appended: match case */
3963 else if (p[len] == '#')
3964 {
3965 ic = FALSE;
3966 ++len;
3967 }
3968 /* nothing appened: use 'ignorecase' */
3969 else
3970 ic = p_ic;
3971
3972 /*
3973 * Get the second variable.
3974 */
3975 *arg = skipwhite(p + len);
3976 if (eval5(arg, &var2, evaluate) == FAIL)
3977 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003978 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 return FAIL;
3980 }
3981
3982 if (evaluate)
3983 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003984 if (type_is && rettv->v_type != var2.v_type)
3985 {
3986 /* For "is" a different type always means FALSE, for "notis"
3987 * it means TRUE. */
3988 n1 = (type == TYPE_NEQUAL);
3989 }
3990 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3991 {
3992 if (type_is)
3993 {
3994 n1 = (rettv->v_type == var2.v_type
3995 && rettv->vval.v_list == var2.vval.v_list);
3996 if (type == TYPE_NEQUAL)
3997 n1 = !n1;
3998 }
3999 else if (rettv->v_type != var2.v_type
4000 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4001 {
4002 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004003 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004004 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004005 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004006 clear_tv(rettv);
4007 clear_tv(&var2);
4008 return FAIL;
4009 }
4010 else
4011 {
4012 /* Compare two Lists for being equal or unequal. */
4013 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4014 if (type == TYPE_NEQUAL)
4015 n1 = !n1;
4016 }
4017 }
4018
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004019 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4020 {
4021 if (type_is)
4022 {
4023 n1 = (rettv->v_type == var2.v_type
4024 && rettv->vval.v_dict == var2.vval.v_dict);
4025 if (type == TYPE_NEQUAL)
4026 n1 = !n1;
4027 }
4028 else if (rettv->v_type != var2.v_type
4029 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4030 {
4031 if (rettv->v_type != var2.v_type)
4032 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4033 else
4034 EMSG(_("E736: Invalid operation for Dictionary"));
4035 clear_tv(rettv);
4036 clear_tv(&var2);
4037 return FAIL;
4038 }
4039 else
4040 {
4041 /* Compare two Dictionaries for being equal or unequal. */
4042 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4043 if (type == TYPE_NEQUAL)
4044 n1 = !n1;
4045 }
4046 }
4047
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004048 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4049 {
4050 if (rettv->v_type != var2.v_type
4051 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4052 {
4053 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004054 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004055 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004056 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004057 clear_tv(rettv);
4058 clear_tv(&var2);
4059 return FAIL;
4060 }
4061 else
4062 {
4063 /* Compare two Funcrefs for being equal or unequal. */
4064 if (rettv->vval.v_string == NULL
4065 || var2.vval.v_string == NULL)
4066 n1 = FALSE;
4067 else
4068 n1 = STRCMP(rettv->vval.v_string,
4069 var2.vval.v_string) == 0;
4070 if (type == TYPE_NEQUAL)
4071 n1 = !n1;
4072 }
4073 }
4074
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 /*
4076 * If one of the two variables is a number, compare as a number.
4077 * When using "=~" or "!~", always compare as string.
4078 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004079 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082 n1 = get_tv_number(rettv);
4083 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 switch (type)
4085 {
4086 case TYPE_EQUAL: n1 = (n1 == n2); break;
4087 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4088 case TYPE_GREATER: n1 = (n1 > n2); break;
4089 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4090 case TYPE_SMALLER: n1 = (n1 < n2); break;
4091 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4092 case TYPE_UNKNOWN:
4093 case TYPE_MATCH:
4094 case TYPE_NOMATCH: break; /* avoid gcc warning */
4095 }
4096 }
4097 else
4098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 s1 = get_tv_string_buf(rettv, buf1);
4100 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4102 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4103 else
4104 i = 0;
4105 n1 = FALSE;
4106 switch (type)
4107 {
4108 case TYPE_EQUAL: n1 = (i == 0); break;
4109 case TYPE_NEQUAL: n1 = (i != 0); break;
4110 case TYPE_GREATER: n1 = (i > 0); break;
4111 case TYPE_GEQUAL: n1 = (i >= 0); break;
4112 case TYPE_SMALLER: n1 = (i < 0); break;
4113 case TYPE_SEQUAL: n1 = (i <= 0); break;
4114
4115 case TYPE_MATCH:
4116 case TYPE_NOMATCH:
4117 /* avoid 'l' flag in 'cpoptions' */
4118 save_cpo = p_cpo;
4119 p_cpo = (char_u *)"";
4120 regmatch.regprog = vim_regcomp(s2,
4121 RE_MAGIC + RE_STRING);
4122 regmatch.rm_ic = ic;
4123 if (regmatch.regprog != NULL)
4124 {
4125 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4126 vim_free(regmatch.regprog);
4127 if (type == TYPE_NOMATCH)
4128 n1 = !n1;
4129 }
4130 p_cpo = save_cpo;
4131 break;
4132
4133 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4134 }
4135 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 clear_tv(rettv);
4137 clear_tv(&var2);
4138 rettv->v_type = VAR_NUMBER;
4139 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141 }
4142
4143 return OK;
4144}
4145
4146/*
4147 * Handle fourth level expression:
4148 * + number addition
4149 * - number subtraction
4150 * . string concatenation
4151 *
4152 * "arg" must point to the first non-white of the expression.
4153 * "arg" is advanced to the next non-white after the recognized expression.
4154 *
4155 * Return OK or FAIL.
4156 */
4157 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004158eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 int evaluate;
4162{
Bram Moolenaar33570922005-01-25 22:26:29 +00004163 typval_T var2;
4164 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 int op;
4166 long n1, n2;
4167 char_u *s1, *s2;
4168 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4169 char_u *p;
4170
4171 /*
4172 * Get the first variable.
4173 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004174 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 return FAIL;
4176
4177 /*
4178 * Repeat computing, until no '+', '-' or '.' is following.
4179 */
4180 for (;;)
4181 {
4182 op = **arg;
4183 if (op != '+' && op != '-' && op != '.')
4184 break;
4185
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 if (op != '+' || rettv->v_type != VAR_LIST)
4187 {
4188 /* For "list + ...", an illegal use of the first operand as
4189 * a number cannot be determined before evaluating the 2nd
4190 * operand: if this is also a list, all is ok.
4191 * For "something . ...", "something - ..." or "non-list + ...",
4192 * we know that the first operand needs to be a string or number
4193 * without evaluating the 2nd operand. So check before to avoid
4194 * side effects after an error. */
4195 if (evaluate && get_tv_string_chk(rettv) == NULL)
4196 {
4197 clear_tv(rettv);
4198 return FAIL;
4199 }
4200 }
4201
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 /*
4203 * Get the second variable.
4204 */
4205 *arg = skipwhite(*arg + 1);
4206 if (eval6(arg, &var2, evaluate) == FAIL)
4207 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return FAIL;
4210 }
4211
4212 if (evaluate)
4213 {
4214 /*
4215 * Compute the result.
4216 */
4217 if (op == '.')
4218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004219 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4220 s2 = get_tv_string_buf_chk(&var2, buf2);
4221 if (s2 == NULL) /* type error ? */
4222 {
4223 clear_tv(rettv);
4224 clear_tv(&var2);
4225 return FAIL;
4226 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004227 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004228 clear_tv(rettv);
4229 rettv->v_type = VAR_STRING;
4230 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004232 else if (op == '+' && rettv->v_type == VAR_LIST
4233 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004234 {
4235 /* concatenate Lists */
4236 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4237 &var3) == FAIL)
4238 {
4239 clear_tv(rettv);
4240 clear_tv(&var2);
4241 return FAIL;
4242 }
4243 clear_tv(rettv);
4244 *rettv = var3;
4245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 else
4247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004248 int error = FALSE;
4249
4250 n1 = get_tv_number_chk(rettv, &error);
4251 if (error)
4252 {
4253 /* This can only happen for "list + non-list".
4254 * For "non-list + ..." or "something - ...", we returned
4255 * before evaluating the 2nd operand. */
4256 clear_tv(rettv);
4257 return FAIL;
4258 }
4259 n2 = get_tv_number_chk(&var2, &error);
4260 if (error)
4261 {
4262 clear_tv(rettv);
4263 clear_tv(&var2);
4264 return FAIL;
4265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 if (op == '+')
4268 n1 = n1 + n2;
4269 else
4270 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 rettv->v_type = VAR_NUMBER;
4272 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 }
4277 return OK;
4278}
4279
4280/*
4281 * Handle fifth level expression:
4282 * * number multiplication
4283 * / number division
4284 * % number modulo
4285 *
4286 * "arg" must point to the first non-white of the expression.
4287 * "arg" is advanced to the next non-white after the recognized expression.
4288 *
4289 * Return OK or FAIL.
4290 */
4291 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004292eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 int evaluate;
4296{
Bram Moolenaar33570922005-01-25 22:26:29 +00004297 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 int op;
4299 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004300 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301
4302 /*
4303 * Get the first variable.
4304 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004305 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 return FAIL;
4307
4308 /*
4309 * Repeat computing, until no '*', '/' or '%' is following.
4310 */
4311 for (;;)
4312 {
4313 op = **arg;
4314 if (op != '*' && op != '/' && op != '%')
4315 break;
4316
4317 if (evaluate)
4318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004320 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 if (error)
4322 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324 else
4325 n1 = 0;
4326
4327 /*
4328 * Get the second variable.
4329 */
4330 *arg = skipwhite(*arg + 1);
4331 if (eval7(arg, &var2, evaluate) == FAIL)
4332 return FAIL;
4333
4334 if (evaluate)
4335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004336 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (error)
4339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340
4341 /*
4342 * Compute the result.
4343 */
4344 if (op == '*')
4345 n1 = n1 * n2;
4346 else if (op == '/')
4347 {
4348 if (n2 == 0) /* give an error message? */
4349 n1 = 0x7fffffffL;
4350 else
4351 n1 = n1 / n2;
4352 }
4353 else
4354 {
4355 if (n2 == 0) /* give an error message? */
4356 n1 = 0;
4357 else
4358 n1 = n1 % n2;
4359 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 rettv->v_type = VAR_NUMBER;
4361 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363 }
4364
4365 return OK;
4366}
4367
4368/*
4369 * Handle sixth level expression:
4370 * number number constant
4371 * "string" string contstant
4372 * 'string' literal string contstant
4373 * &option-name option value
4374 * @r register contents
4375 * identifier variable value
4376 * function() function call
4377 * $VAR environment variable
4378 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004379 * [expr, expr] List
4380 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 *
4382 * Also handle:
4383 * ! in front logical NOT
4384 * - in front unary minus
4385 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004386 * trailing [] subscript in String or List
4387 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 *
4389 * "arg" must point to the first non-white of the expression.
4390 * "arg" is advanced to the next non-white after the recognized expression.
4391 *
4392 * Return OK or FAIL.
4393 */
4394 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004395eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 int evaluate;
4399{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 long n;
4401 int len;
4402 char_u *s;
4403 int val;
4404 char_u *start_leader, *end_leader;
4405 int ret = OK;
4406 char_u *alias;
4407
4408 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004409 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004410 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
4414 /*
4415 * Skip '!' and '-' characters. They are handled later.
4416 */
4417 start_leader = *arg;
4418 while (**arg == '!' || **arg == '-' || **arg == '+')
4419 *arg = skipwhite(*arg + 1);
4420 end_leader = *arg;
4421
4422 switch (**arg)
4423 {
4424 /*
4425 * Number constant.
4426 */
4427 case '0':
4428 case '1':
4429 case '2':
4430 case '3':
4431 case '4':
4432 case '5':
4433 case '6':
4434 case '7':
4435 case '8':
4436 case '9':
4437 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4438 *arg += len;
4439 if (evaluate)
4440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004441 rettv->v_type = VAR_NUMBER;
4442 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444 break;
4445
4446 /*
4447 * String constant: "string".
4448 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004449 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 break;
4451
4452 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004453 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004456 break;
4457
4458 /*
4459 * List: [expr, expr]
4460 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004461 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 break;
4463
4464 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004465 * Dictionary: {key: val, key: val}
4466 */
4467 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4468 break;
4469
4470 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004471 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004473 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 break;
4475
4476 /*
4477 * Environment variable: $VAR.
4478 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004479 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 break;
4481
4482 /*
4483 * Register contents: @r.
4484 */
4485 case '@': ++*arg;
4486 if (evaluate)
4487 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004488 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004489 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491 if (**arg != NUL)
4492 ++*arg;
4493 break;
4494
4495 /*
4496 * nested expression: (expression).
4497 */
4498 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004499 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 if (**arg == ')')
4501 ++*arg;
4502 else if (ret == OK)
4503 {
4504 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004505 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 ret = FAIL;
4507 }
4508 break;
4509
Bram Moolenaar8c711452005-01-14 21:53:12 +00004510 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 break;
4512 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004513
4514 if (ret == NOTDONE)
4515 {
4516 /*
4517 * Must be a variable or function name.
4518 * Can also be a curly-braces kind of name: {expr}.
4519 */
4520 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004521 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004522 if (alias != NULL)
4523 s = alias;
4524
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004525 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004526 ret = FAIL;
4527 else
4528 {
4529 if (**arg == '(') /* recursive! */
4530 {
4531 /* If "s" is the name of a variable of type VAR_FUNC
4532 * use its contents. */
4533 s = deref_func_name(s, &len);
4534
4535 /* Invoke the function. */
4536 ret = get_func_tv(s, len, rettv, arg,
4537 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004538 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004539 /* Stop the expression evaluation when immediately
4540 * aborting on error, or when an interrupt occurred or
4541 * an exception was thrown but not caught. */
4542 if (aborting())
4543 {
4544 if (ret == OK)
4545 clear_tv(rettv);
4546 ret = FAIL;
4547 }
4548 }
4549 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004550 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004551 else
4552 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004553 }
4554
4555 if (alias != NULL)
4556 vim_free(alias);
4557 }
4558
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 *arg = skipwhite(*arg);
4560
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004561 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4562 * expr(expr). */
4563 if (ret == OK)
4564 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565
4566 /*
4567 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4568 */
4569 if (ret == OK && evaluate && end_leader > start_leader)
4570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004571 int error = FALSE;
4572
4573 val = get_tv_number_chk(rettv, &error);
4574 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004576 clear_tv(rettv);
4577 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004579 else
4580 {
4581 while (end_leader > start_leader)
4582 {
4583 --end_leader;
4584 if (*end_leader == '!')
4585 val = !val;
4586 else if (*end_leader == '-')
4587 val = -val;
4588 }
4589 clear_tv(rettv);
4590 rettv->v_type = VAR_NUMBER;
4591 rettv->vval.v_number = val;
4592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
4594
4595 return ret;
4596}
4597
4598/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004599 * Evaluate an "[expr]" or "[expr:expr]" index.
4600 * "*arg" points to the '['.
4601 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4602 */
4603 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004604eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004605 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004606 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004607 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004608 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609{
4610 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004611 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004612 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004613 long len = -1;
4614 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004616 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004618 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004620 if (verbose)
4621 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622 return FAIL;
4623 }
4624
Bram Moolenaar8c711452005-01-14 21:53:12 +00004625 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004627 /*
4628 * dict.name
4629 */
4630 key = *arg + 1;
4631 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4632 ;
4633 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004634 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004635 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004636 }
4637 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004638 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004639 /*
4640 * something[idx]
4641 *
4642 * Get the (first) variable from inside the [].
4643 */
4644 *arg = skipwhite(*arg + 1);
4645 if (**arg == ':')
4646 empty1 = TRUE;
4647 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4648 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004649 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4650 {
4651 /* not a number or string */
4652 clear_tv(&var1);
4653 return FAIL;
4654 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004655
4656 /*
4657 * Get the second variable from inside the [:].
4658 */
4659 if (**arg == ':')
4660 {
4661 range = TRUE;
4662 *arg = skipwhite(*arg + 1);
4663 if (**arg == ']')
4664 empty2 = TRUE;
4665 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004667 if (!empty1)
4668 clear_tv(&var1);
4669 return FAIL;
4670 }
4671 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4672 {
4673 /* not a number or string */
4674 if (!empty1)
4675 clear_tv(&var1);
4676 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 return FAIL;
4678 }
4679 }
4680
4681 /* Check for the ']'. */
4682 if (**arg != ']')
4683 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004684 if (verbose)
4685 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004686 clear_tv(&var1);
4687 if (range)
4688 clear_tv(&var2);
4689 return FAIL;
4690 }
4691 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 }
4693
4694 if (evaluate)
4695 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004696 n1 = 0;
4697 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004699 n1 = get_tv_number(&var1);
4700 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004701 }
4702 if (range)
4703 {
4704 if (empty2)
4705 n2 = -1;
4706 else
4707 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004708 n2 = get_tv_number(&var2);
4709 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004710 }
4711 }
4712
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004713 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714 {
4715 case VAR_NUMBER:
4716 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004717 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 len = (long)STRLEN(s);
4719 if (range)
4720 {
4721 /* The resulting variable is a substring. If the indexes
4722 * are out of range the result is empty. */
4723 if (n1 < 0)
4724 {
4725 n1 = len + n1;
4726 if (n1 < 0)
4727 n1 = 0;
4728 }
4729 if (n2 < 0)
4730 n2 = len + n2;
4731 else if (n2 >= len)
4732 n2 = len;
4733 if (n1 >= len || n2 < 0 || n1 > n2)
4734 s = NULL;
4735 else
4736 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4737 }
4738 else
4739 {
4740 /* The resulting variable is a string of a single
4741 * character. If the index is too big or negative the
4742 * result is empty. */
4743 if (n1 >= len || n1 < 0)
4744 s = NULL;
4745 else
4746 s = vim_strnsave(s + n1, 1);
4747 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004748 clear_tv(rettv);
4749 rettv->v_type = VAR_STRING;
4750 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004751 break;
4752
4753 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 if (n1 < 0)
4756 n1 = len + n1;
4757 if (!empty1 && (n1 < 0 || n1 >= len))
4758 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004759 if (verbose)
4760 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004761 return FAIL;
4762 }
4763 if (range)
4764 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004765 list_T *l;
4766 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004767
4768 if (n2 < 0)
4769 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004770 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004772 if (verbose)
4773 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004774 return FAIL;
4775 }
4776 l = list_alloc();
4777 if (l == NULL)
4778 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 n1 <= n2; ++n1)
4781 {
4782 if (list_append_tv(l, &item->li_tv) == FAIL)
4783 {
4784 list_free(l);
4785 return FAIL;
4786 }
4787 item = item->li_next;
4788 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004789 clear_tv(rettv);
4790 rettv->v_type = VAR_LIST;
4791 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004792 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004793 }
4794 else
4795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004796 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004798 clear_tv(rettv);
4799 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004800 }
4801 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004802
4803 case VAR_DICT:
4804 if (range)
4805 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004806 if (verbose)
4807 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004808 if (len == -1)
4809 clear_tv(&var1);
4810 return FAIL;
4811 }
4812 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004813 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004814
4815 if (len == -1)
4816 {
4817 key = get_tv_string(&var1);
4818 if (*key == NUL)
4819 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004820 if (verbose)
4821 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004822 clear_tv(&var1);
4823 return FAIL;
4824 }
4825 }
4826
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004827 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004828
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004829 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004830 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004831 if (len == -1)
4832 clear_tv(&var1);
4833 if (item == NULL)
4834 return FAIL;
4835
4836 copy_tv(&item->di_tv, &var1);
4837 clear_tv(rettv);
4838 *rettv = var1;
4839 }
4840 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004841 }
4842 }
4843
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004844 return OK;
4845}
4846
4847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 * Get an option value.
4849 * "arg" points to the '&' or '+' before the option name.
4850 * "arg" is advanced to character after the option name.
4851 * Return OK or FAIL.
4852 */
4853 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004856 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 int evaluate;
4858{
4859 char_u *option_end;
4860 long numval;
4861 char_u *stringval;
4862 int opt_type;
4863 int c;
4864 int working = (**arg == '+'); /* has("+option") */
4865 int ret = OK;
4866 int opt_flags;
4867
4868 /*
4869 * Isolate the option name and find its value.
4870 */
4871 option_end = find_option_end(arg, &opt_flags);
4872 if (option_end == NULL)
4873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 EMSG2(_("E112: Option name missing: %s"), *arg);
4876 return FAIL;
4877 }
4878
4879 if (!evaluate)
4880 {
4881 *arg = option_end;
4882 return OK;
4883 }
4884
4885 c = *option_end;
4886 *option_end = NUL;
4887 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004888 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 if (opt_type == -3) /* invalid name */
4891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004892 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 EMSG2(_("E113: Unknown option: %s"), *arg);
4894 ret = FAIL;
4895 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004896 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
4898 if (opt_type == -2) /* hidden string option */
4899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004900 rettv->v_type = VAR_STRING;
4901 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903 else if (opt_type == -1) /* hidden number option */
4904 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004905 rettv->v_type = VAR_NUMBER;
4906 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
4908 else if (opt_type == 1) /* number option */
4909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004910 rettv->v_type = VAR_NUMBER;
4911 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913 else /* string option */
4914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 rettv->v_type = VAR_STRING;
4916 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 }
4919 else if (working && (opt_type == -2 || opt_type == -1))
4920 ret = FAIL;
4921
4922 *option_end = c; /* put back for error messages */
4923 *arg = option_end;
4924
4925 return ret;
4926}
4927
4928/*
4929 * Allocate a variable for a string constant.
4930 * Return OK or FAIL.
4931 */
4932 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004933get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 int evaluate;
4937{
4938 char_u *p;
4939 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 int extra = 0;
4941
4942 /*
4943 * Find the end of the string, skipping backslashed characters.
4944 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004945 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 {
4947 if (*p == '\\' && p[1] != NUL)
4948 {
4949 ++p;
4950 /* A "\<x>" form occupies at least 4 characters, and produces up
4951 * to 6 characters: reserve space for 2 extra */
4952 if (*p == '<')
4953 extra += 2;
4954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956
4957 if (*p != '"')
4958 {
4959 EMSG2(_("E114: Missing quote: %s"), *arg);
4960 return FAIL;
4961 }
4962
4963 /* If only parsing, set *arg and return here */
4964 if (!evaluate)
4965 {
4966 *arg = p + 1;
4967 return OK;
4968 }
4969
4970 /*
4971 * Copy the string into allocated memory, handling backslashed
4972 * characters.
4973 */
4974 name = alloc((unsigned)(p - *arg + extra));
4975 if (name == NULL)
4976 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977 rettv->v_type = VAR_STRING;
4978 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
4982 if (*p == '\\')
4983 {
4984 switch (*++p)
4985 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 case 'b': *name++ = BS; ++p; break;
4987 case 'e': *name++ = ESC; ++p; break;
4988 case 'f': *name++ = FF; ++p; break;
4989 case 'n': *name++ = NL; ++p; break;
4990 case 'r': *name++ = CAR; ++p; break;
4991 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993 case 'X': /* hex: "\x1", "\x12" */
4994 case 'x':
4995 case 'u': /* Unicode: "\u0023" */
4996 case 'U':
4997 if (vim_isxdigit(p[1]))
4998 {
4999 int n, nr;
5000 int c = toupper(*p);
5001
5002 if (c == 'X')
5003 n = 2;
5004 else
5005 n = 4;
5006 nr = 0;
5007 while (--n >= 0 && vim_isxdigit(p[1]))
5008 {
5009 ++p;
5010 nr = (nr << 4) + hex2nr(*p);
5011 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005012 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013#ifdef FEAT_MBYTE
5014 /* For "\u" store the number according to
5015 * 'encoding'. */
5016 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005017 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 else
5019#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 break;
5023
5024 /* octal: "\1", "\12", "\123" */
5025 case '0':
5026 case '1':
5027 case '2':
5028 case '3':
5029 case '4':
5030 case '5':
5031 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 case '7': *name = *p++ - '0';
5033 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 *name = (*name << 3) + *p++ - '0';
5036 if (*p >= '0' && *p <= '7')
5037 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 break;
5041
5042 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 if (extra != 0)
5045 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 break;
5048 }
5049 /* FALLTHROUGH */
5050
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 break;
5053 }
5054 }
5055 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 *arg = p + 1;
5061
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 return OK;
5063}
5064
5065/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 * Return OK or FAIL.
5068 */
5069 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005070get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 int evaluate;
5074{
5075 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005077 int reduce = 0;
5078
5079 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005081 */
5082 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5083 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005084 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005085 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005087 break;
5088 ++reduce;
5089 ++p;
5090 }
5091 }
5092
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005094 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005096 return FAIL;
5097 }
5098
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005100 if (!evaluate)
5101 {
5102 *arg = p + 1;
5103 return OK;
5104 }
5105
5106 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005107 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005108 */
5109 str = alloc((unsigned)((p - *arg) - reduce));
5110 if (str == NULL)
5111 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005112 rettv->v_type = VAR_STRING;
5113 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005114
Bram Moolenaar8c711452005-01-14 21:53:12 +00005115 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005116 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005118 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005120 break;
5121 ++p;
5122 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005123 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005124 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005126 *arg = p + 1;
5127
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005128 return OK;
5129}
5130
5131/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005132 * Allocate a variable for a List and fill it from "*arg".
5133 * Return OK or FAIL.
5134 */
5135 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005136get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005137 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005138 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139 int evaluate;
5140{
Bram Moolenaar33570922005-01-25 22:26:29 +00005141 list_T *l = NULL;
5142 typval_T tv;
5143 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005144
5145 if (evaluate)
5146 {
5147 l = list_alloc();
5148 if (l == NULL)
5149 return FAIL;
5150 }
5151
5152 *arg = skipwhite(*arg + 1);
5153 while (**arg != ']' && **arg != NUL)
5154 {
5155 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5156 goto failret;
5157 if (evaluate)
5158 {
5159 item = listitem_alloc();
5160 if (item != NULL)
5161 {
5162 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005163 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164 list_append(l, item);
5165 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005166 else
5167 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005168 }
5169
5170 if (**arg == ']')
5171 break;
5172 if (**arg != ',')
5173 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005175 goto failret;
5176 }
5177 *arg = skipwhite(*arg + 1);
5178 }
5179
5180 if (**arg != ']')
5181 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005183failret:
5184 if (evaluate)
5185 list_free(l);
5186 return FAIL;
5187 }
5188
5189 *arg = skipwhite(*arg + 1);
5190 if (evaluate)
5191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005192 rettv->v_type = VAR_LIST;
5193 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005194 ++l->lv_refcount;
5195 }
5196
5197 return OK;
5198}
5199
5200/*
5201 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005202 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005204 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005205list_alloc()
5206{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005207 list_T *l;
5208
5209 l = (list_T *)alloc_clear(sizeof(list_T));
5210 if (l != NULL)
5211 {
5212 /* Prepend the list to the list of lists for garbage collection. */
5213 if (first_list != NULL)
5214 first_list->lv_used_prev = l;
5215 l->lv_used_prev = NULL;
5216 l->lv_used_next = first_list;
5217 first_list = l;
5218 }
5219 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005220}
5221
5222/*
5223 * Unreference a list: decrement the reference count and free it when it
5224 * becomes zero.
5225 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005226 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005227list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005228 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005230 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5231 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232}
5233
5234/*
5235 * Free a list, including all items it points to.
5236 * Ignores the reference count.
5237 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005238 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005240 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241{
Bram Moolenaar33570922005-01-25 22:26:29 +00005242 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243
Bram Moolenaard9fba312005-06-26 22:34:35 +00005244 /* Avoid that recursive reference to the list frees us again. */
5245 l->lv_refcount = DEL_REFCOUNT;
5246
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005247 /* Remove the list from the list of lists for garbage collection. */
5248 if (l->lv_used_prev == NULL)
5249 first_list = l->lv_used_next;
5250 else
5251 l->lv_used_prev->lv_used_next = l->lv_used_next;
5252 if (l->lv_used_next != NULL)
5253 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5254
Bram Moolenaard9fba312005-06-26 22:34:35 +00005255 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005257 /* Remove the item before deleting it. */
5258 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 listitem_free(item);
5260 }
5261 vim_free(l);
5262}
5263
5264/*
5265 * Allocate a list item.
5266 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005267 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268listitem_alloc()
5269{
Bram Moolenaar33570922005-01-25 22:26:29 +00005270 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271}
5272
5273/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005274 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 */
5276 static void
5277listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005278 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005280 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 vim_free(item);
5282}
5283
5284/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005285 * Remove a list item from a List and free it. Also clears the value.
5286 */
5287 static void
5288listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005289 list_T *l;
5290 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005291{
5292 list_remove(l, item, item);
5293 listitem_free(item);
5294}
5295
5296/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 * Get the number of items in a list.
5298 */
5299 static long
5300list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005301 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 if (l == NULL)
5304 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005305 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306}
5307
5308/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005309 * Return TRUE when two lists have exactly the same values.
5310 */
5311 static int
5312list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005313 list_T *l1;
5314 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005315 int ic; /* ignore case for strings */
5316{
Bram Moolenaar33570922005-01-25 22:26:29 +00005317 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005318
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005319 if (list_len(l1) != list_len(l2))
5320 return FALSE;
5321
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005322 for (item1 = l1->lv_first, item2 = l2->lv_first;
5323 item1 != NULL && item2 != NULL;
5324 item1 = item1->li_next, item2 = item2->li_next)
5325 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5326 return FALSE;
5327 return item1 == NULL && item2 == NULL;
5328}
5329
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005330#if defined(FEAT_PYTHON) || defined(PROTO)
5331/*
5332 * Return the dictitem that an entry in a hashtable points to.
5333 */
5334 dictitem_T *
5335dict_lookup(hi)
5336 hashitem_T *hi;
5337{
5338 return HI2DI(hi);
5339}
5340#endif
5341
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005342/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005343 * Return TRUE when two dictionaries have exactly the same key/values.
5344 */
5345 static int
5346dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005347 dict_T *d1;
5348 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005349 int ic; /* ignore case for strings */
5350{
Bram Moolenaar33570922005-01-25 22:26:29 +00005351 hashitem_T *hi;
5352 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005353 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005354
5355 if (dict_len(d1) != dict_len(d2))
5356 return FALSE;
5357
Bram Moolenaar33570922005-01-25 22:26:29 +00005358 todo = d1->dv_hashtab.ht_used;
5359 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005360 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005361 if (!HASHITEM_EMPTY(hi))
5362 {
5363 item2 = dict_find(d2, hi->hi_key, -1);
5364 if (item2 == NULL)
5365 return FALSE;
5366 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5367 return FALSE;
5368 --todo;
5369 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005370 }
5371 return TRUE;
5372}
5373
5374/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005375 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005376 * Compares the items just like "==" would compare them, but strings and
5377 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005378 */
5379 static int
5380tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005381 typval_T *tv1;
5382 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005383 int ic; /* ignore case */
5384{
5385 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005386 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005387
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005388 if (tv1->v_type != tv2->v_type)
5389 return FALSE;
5390
5391 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005392 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005393 case VAR_LIST:
5394 /* recursive! */
5395 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5396
5397 case VAR_DICT:
5398 /* recursive! */
5399 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5400
5401 case VAR_FUNC:
5402 return (tv1->vval.v_string != NULL
5403 && tv2->vval.v_string != NULL
5404 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5405
5406 case VAR_NUMBER:
5407 return tv1->vval.v_number == tv2->vval.v_number;
5408
5409 case VAR_STRING:
5410 s1 = get_tv_string_buf(tv1, buf1);
5411 s2 = get_tv_string_buf(tv2, buf2);
5412 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005413 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005414
5415 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005416 return TRUE;
5417}
5418
5419/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 * Locate item with index "n" in list "l" and return it.
5421 * A negative index is counted from the end; -1 is the last item.
5422 * Returns NULL when "n" is out of range.
5423 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005424 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 long n;
5428{
Bram Moolenaar33570922005-01-25 22:26:29 +00005429 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 long idx;
5431
5432 if (l == NULL)
5433 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005434
5435 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005437 n = l->lv_len + n;
5438
5439 /* Check for index out of range. */
5440 if (n < 0 || n >= l->lv_len)
5441 return NULL;
5442
5443 /* When there is a cached index may start search from there. */
5444 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005446 if (n < l->lv_idx / 2)
5447 {
5448 /* closest to the start of the list */
5449 item = l->lv_first;
5450 idx = 0;
5451 }
5452 else if (n > (l->lv_idx + l->lv_len) / 2)
5453 {
5454 /* closest to the end of the list */
5455 item = l->lv_last;
5456 idx = l->lv_len - 1;
5457 }
5458 else
5459 {
5460 /* closest to the cached index */
5461 item = l->lv_idx_item;
5462 idx = l->lv_idx;
5463 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 }
5465 else
5466 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005467 if (n < l->lv_len / 2)
5468 {
5469 /* closest to the start of the list */
5470 item = l->lv_first;
5471 idx = 0;
5472 }
5473 else
5474 {
5475 /* closest to the end of the list */
5476 item = l->lv_last;
5477 idx = l->lv_len - 1;
5478 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005480
5481 while (n > idx)
5482 {
5483 /* search forward */
5484 item = item->li_next;
5485 ++idx;
5486 }
5487 while (n < idx)
5488 {
5489 /* search backward */
5490 item = item->li_prev;
5491 --idx;
5492 }
5493
5494 /* cache the used index */
5495 l->lv_idx = idx;
5496 l->lv_idx_item = item;
5497
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005498 return item;
5499}
5500
5501/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005502 * Locate "item" list "l" and return its index.
5503 * Returns -1 when "item" is not in the list.
5504 */
5505 static long
5506list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005507 list_T *l;
5508 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005509{
5510 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005511 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005512
5513 if (l == NULL)
5514 return -1;
5515 idx = 0;
5516 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5517 ++idx;
5518 if (li == NULL)
5519 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005520 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005521}
5522
5523/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 * Append item "item" to the end of list "l".
5525 */
5526 static void
5527list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005528 list_T *l;
5529 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530{
5531 if (l->lv_last == NULL)
5532 {
5533 /* empty list */
5534 l->lv_first = item;
5535 l->lv_last = item;
5536 item->li_prev = NULL;
5537 }
5538 else
5539 {
5540 l->lv_last->li_next = item;
5541 item->li_prev = l->lv_last;
5542 l->lv_last = item;
5543 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005544 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 item->li_next = NULL;
5546}
5547
5548/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005549 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005550 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551 */
5552 static int
5553list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005554 list_T *l;
5555 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005557 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558
Bram Moolenaar05159a02005-02-26 23:04:13 +00005559 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005561 copy_tv(tv, &li->li_tv);
5562 list_append(l, li);
5563 return OK;
5564}
5565
5566/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005567 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005568 * Return FAIL when out of memory.
5569 */
5570 int
5571list_append_dict(list, dict)
5572 list_T *list;
5573 dict_T *dict;
5574{
5575 listitem_T *li = listitem_alloc();
5576
5577 if (li == NULL)
5578 return FAIL;
5579 li->li_tv.v_type = VAR_DICT;
5580 li->li_tv.v_lock = 0;
5581 li->li_tv.vval.v_dict = dict;
5582 list_append(list, li);
5583 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 return OK;
5585}
5586
5587/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005588 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005589 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005590 * Returns FAIL when out of memory.
5591 */
5592 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005593list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005594 list_T *l;
5595 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005596 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005597{
5598 listitem_T *li = listitem_alloc();
5599
5600 if (li == NULL)
5601 return FAIL;
5602 list_append(l, li);
5603 li->li_tv.v_type = VAR_STRING;
5604 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005605 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5606 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005607 return FAIL;
5608 return OK;
5609}
5610
5611/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005612 * Append "n" to list "l".
5613 * Returns FAIL when out of memory.
5614 */
5615 static int
5616list_append_number(l, n)
5617 list_T *l;
5618 varnumber_T n;
5619{
5620 listitem_T *li;
5621
5622 li = listitem_alloc();
5623 if (li == NULL)
5624 return FAIL;
5625 li->li_tv.v_type = VAR_NUMBER;
5626 li->li_tv.v_lock = 0;
5627 li->li_tv.vval.v_number = n;
5628 list_append(l, li);
5629 return OK;
5630}
5631
5632/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005633 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005634 * If "item" is NULL append at the end.
5635 * Return FAIL when out of memory.
5636 */
5637 static int
5638list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005639 list_T *l;
5640 typval_T *tv;
5641 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005642{
Bram Moolenaar33570922005-01-25 22:26:29 +00005643 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005644
5645 if (ni == NULL)
5646 return FAIL;
5647 copy_tv(tv, &ni->li_tv);
5648 if (item == NULL)
5649 /* Append new item at end of list. */
5650 list_append(l, ni);
5651 else
5652 {
5653 /* Insert new item before existing item. */
5654 ni->li_prev = item->li_prev;
5655 ni->li_next = item;
5656 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005657 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005658 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005659 ++l->lv_idx;
5660 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005661 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005662 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005663 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005664 l->lv_idx_item = NULL;
5665 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005666 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005667 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005668 }
5669 return OK;
5670}
5671
5672/*
5673 * Extend "l1" with "l2".
5674 * If "bef" is NULL append at the end, otherwise insert before this item.
5675 * Returns FAIL when out of memory.
5676 */
5677 static int
5678list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005679 list_T *l1;
5680 list_T *l2;
5681 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005682{
Bram Moolenaar33570922005-01-25 22:26:29 +00005683 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005684
5685 for (item = l2->lv_first; item != NULL; item = item->li_next)
5686 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5687 return FAIL;
5688 return OK;
5689}
5690
5691/*
5692 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5693 * Return FAIL when out of memory.
5694 */
5695 static int
5696list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005697 list_T *l1;
5698 list_T *l2;
5699 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005700{
Bram Moolenaar33570922005-01-25 22:26:29 +00005701 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005702
5703 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005704 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005705 if (l == NULL)
5706 return FAIL;
5707 tv->v_type = VAR_LIST;
5708 tv->vval.v_list = l;
5709
5710 /* append all items from the second list */
5711 return list_extend(l, l2, NULL);
5712}
5713
5714/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005715 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005716 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005717 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005718 * Returns NULL when out of memory.
5719 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005720 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005721list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005722 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005723 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005724 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005725{
Bram Moolenaar33570922005-01-25 22:26:29 +00005726 list_T *copy;
5727 listitem_T *item;
5728 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005729
5730 if (orig == NULL)
5731 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732
5733 copy = list_alloc();
5734 if (copy != NULL)
5735 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005736 if (copyID != 0)
5737 {
5738 /* Do this before adding the items, because one of the items may
5739 * refer back to this list. */
5740 orig->lv_copyID = copyID;
5741 orig->lv_copylist = copy;
5742 }
5743 for (item = orig->lv_first; item != NULL && !got_int;
5744 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745 {
5746 ni = listitem_alloc();
5747 if (ni == NULL)
5748 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005749 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005750 {
5751 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5752 {
5753 vim_free(ni);
5754 break;
5755 }
5756 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005757 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005758 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 list_append(copy, ni);
5760 }
5761 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005762 if (item != NULL)
5763 {
5764 list_unref(copy);
5765 copy = NULL;
5766 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005767 }
5768
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769 return copy;
5770}
5771
5772/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005773 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005774 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005776 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005777list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005778 list_T *l;
5779 listitem_T *item;
5780 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005781{
Bram Moolenaar33570922005-01-25 22:26:29 +00005782 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005784 /* notify watchers */
5785 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005786 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005787 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005788 list_fix_watch(l, ip);
5789 if (ip == item2)
5790 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005791 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005792
5793 if (item2->li_next == NULL)
5794 l->lv_last = item->li_prev;
5795 else
5796 item2->li_next->li_prev = item->li_prev;
5797 if (item->li_prev == NULL)
5798 l->lv_first = item2->li_next;
5799 else
5800 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005801 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802}
5803
5804/*
5805 * Return an allocated string with the string representation of a list.
5806 * May return NULL.
5807 */
5808 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005809list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005810 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005811 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005812{
5813 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814
5815 if (tv->vval.v_list == NULL)
5816 return NULL;
5817 ga_init2(&ga, (int)sizeof(char), 80);
5818 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005819 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005820 {
5821 vim_free(ga.ga_data);
5822 return NULL;
5823 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824 ga_append(&ga, ']');
5825 ga_append(&ga, NUL);
5826 return (char_u *)ga.ga_data;
5827}
5828
5829/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005830 * Join list "l" into a string in "*gap", using separator "sep".
5831 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005832 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005834 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005835list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005836 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005837 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838 char_u *sep;
5839 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005840 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005841{
5842 int first = TRUE;
5843 char_u *tofree;
5844 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005845 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005846 char_u *s;
5847
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005848 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 {
5850 if (first)
5851 first = FALSE;
5852 else
5853 ga_concat(gap, sep);
5854
5855 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005856 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005858 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 if (s != NULL)
5860 ga_concat(gap, s);
5861 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005862 if (s == NULL)
5863 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005865 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005866}
5867
5868/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005869 * Garbage collection for lists and dictionaries.
5870 *
5871 * We use reference counts to be able to free most items right away when they
5872 * are no longer used. But for composite items it's possible that it becomes
5873 * unused while the reference count is > 0: When there is a recursive
5874 * reference. Example:
5875 * :let l = [1, 2, 3]
5876 * :let d = {9: l}
5877 * :let l[1] = d
5878 *
5879 * Since this is quite unusual we handle this with garbage collection: every
5880 * once in a while find out which lists and dicts are not referenced from any
5881 * variable.
5882 *
5883 * Here is a good reference text about garbage collection (refers to Python
5884 * but it applies to all reference-counting mechanisms):
5885 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005886 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005887
5888/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005889 * Do garbage collection for lists and dicts.
5890 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005891 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005892 int
5893garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005894{
5895 dict_T *dd;
5896 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005897 int copyID = ++current_copyID;
5898 buf_T *buf;
5899 win_T *wp;
5900 int i;
5901 funccall_T *fc;
5902 int did_free = FALSE;
5903
5904 /*
5905 * 1. Go through all accessible variables and mark all lists and dicts
5906 * with copyID.
5907 */
5908 /* script-local variables */
5909 for (i = 1; i <= ga_scripts.ga_len; ++i)
5910 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5911
5912 /* buffer-local variables */
5913 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5914 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5915
5916 /* window-local variables */
5917 FOR_ALL_WINDOWS(wp)
5918 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5919
5920 /* global variables */
5921 set_ref_in_ht(&globvarht, copyID);
5922
5923 /* function-local variables */
5924 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5925 {
5926 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5927 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5928 }
5929
5930 /*
5931 * 2. Go through the list of dicts and free items without the copyID.
5932 */
5933 for (dd = first_dict; dd != NULL; )
5934 if (dd->dv_copyID != copyID)
5935 {
5936 dict_free(dd);
5937 did_free = TRUE;
5938
5939 /* restart, next dict may also have been freed */
5940 dd = first_dict;
5941 }
5942 else
5943 dd = dd->dv_used_next;
5944
5945 /*
5946 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005947 * But don't free a list that has a watcher (used in a for loop), these
5948 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005949 */
5950 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005951 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005952 {
5953 list_free(ll);
5954 did_free = TRUE;
5955
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005956 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005957 ll = first_list;
5958 }
5959 else
5960 ll = ll->lv_used_next;
5961
5962 return did_free;
5963}
5964
5965/*
5966 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5967 */
5968 static void
5969set_ref_in_ht(ht, copyID)
5970 hashtab_T *ht;
5971 int copyID;
5972{
5973 int todo;
5974 hashitem_T *hi;
5975
5976 todo = ht->ht_used;
5977 for (hi = ht->ht_array; todo > 0; ++hi)
5978 if (!HASHITEM_EMPTY(hi))
5979 {
5980 --todo;
5981 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5982 }
5983}
5984
5985/*
5986 * Mark all lists and dicts referenced through list "l" with "copyID".
5987 */
5988 static void
5989set_ref_in_list(l, copyID)
5990 list_T *l;
5991 int copyID;
5992{
5993 listitem_T *li;
5994
5995 for (li = l->lv_first; li != NULL; li = li->li_next)
5996 set_ref_in_item(&li->li_tv, copyID);
5997}
5998
5999/*
6000 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6001 */
6002 static void
6003set_ref_in_item(tv, copyID)
6004 typval_T *tv;
6005 int copyID;
6006{
6007 dict_T *dd;
6008 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006009
6010 switch (tv->v_type)
6011 {
6012 case VAR_DICT:
6013 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006014 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006015 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006016 /* Didn't see this dict yet. */
6017 dd->dv_copyID = copyID;
6018 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006019 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006020 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006021
6022 case VAR_LIST:
6023 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006024 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006025 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006026 /* Didn't see this list yet. */
6027 ll->lv_copyID = copyID;
6028 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006029 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006030 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006031 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006032 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006033}
6034
6035/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006036 * Allocate an empty header for a dictionary.
6037 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006038 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006039dict_alloc()
6040{
Bram Moolenaar33570922005-01-25 22:26:29 +00006041 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006042
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006044 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006045 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006046 /* Add the list to the hashtable for garbage collection. */
6047 if (first_dict != NULL)
6048 first_dict->dv_used_prev = d;
6049 d->dv_used_next = first_dict;
6050 d->dv_used_prev = NULL;
6051
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006053 d->dv_lock = 0;
6054 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006055 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006056 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006057 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006058}
6059
6060/*
6061 * Unreference a Dictionary: decrement the reference count and free it when it
6062 * becomes zero.
6063 */
6064 static void
6065dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006066 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006067{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006068 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6069 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006070}
6071
6072/*
6073 * Free a Dictionary, including all items it contains.
6074 * Ignores the reference count.
6075 */
6076 static void
6077dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006079{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006080 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006082 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006083
Bram Moolenaard9fba312005-06-26 22:34:35 +00006084 /* Avoid that recursive reference to the dict frees us again. */
6085 d->dv_refcount = DEL_REFCOUNT;
6086
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006087 /* Remove the dict from the list of dicts for garbage collection. */
6088 if (d->dv_used_prev == NULL)
6089 first_dict = d->dv_used_next;
6090 else
6091 d->dv_used_prev->dv_used_next = d->dv_used_next;
6092 if (d->dv_used_next != NULL)
6093 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6094
6095 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006096 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 todo = d->dv_hashtab.ht_used;
6098 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006099 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006100 if (!HASHITEM_EMPTY(hi))
6101 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006102 /* Remove the item before deleting it, just in case there is
6103 * something recursive causing trouble. */
6104 di = HI2DI(hi);
6105 hash_remove(&d->dv_hashtab, hi);
6106 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006107 --todo;
6108 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006109 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006110 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006111 vim_free(d);
6112}
6113
6114/*
6115 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006116 * The "key" is copied to the new item.
6117 * Note that the value of the item "di_tv" still needs to be initialized!
6118 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006119 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006120 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006121dictitem_alloc(key)
6122 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006123{
Bram Moolenaar33570922005-01-25 22:26:29 +00006124 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006125
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006127 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006128 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006129 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006130 di->di_flags = 0;
6131 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006132 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006133}
6134
6135/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006136 * Make a copy of a Dictionary item.
6137 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006139dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006141{
Bram Moolenaar33570922005-01-25 22:26:29 +00006142 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006143
Bram Moolenaar33570922005-01-25 22:26:29 +00006144 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006145 if (di != NULL)
6146 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006147 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006148 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006149 copy_tv(&org->di_tv, &di->di_tv);
6150 }
6151 return di;
6152}
6153
6154/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155 * Remove item "item" from Dictionary "dict" and free it.
6156 */
6157 static void
6158dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006159 dict_T *dict;
6160 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006161{
Bram Moolenaar33570922005-01-25 22:26:29 +00006162 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006163
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006165 if (HASHITEM_EMPTY(hi))
6166 EMSG2(_(e_intern2), "dictitem_remove()");
6167 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006168 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006169 dictitem_free(item);
6170}
6171
6172/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006173 * Free a dict item. Also clears the value.
6174 */
6175 static void
6176dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006178{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006179 clear_tv(&item->di_tv);
6180 vim_free(item);
6181}
6182
6183/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006184 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6185 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006186 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006187 * Returns NULL when out of memory.
6188 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006189 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006190dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006191 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006192 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006193 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006194{
Bram Moolenaar33570922005-01-25 22:26:29 +00006195 dict_T *copy;
6196 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006197 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006198 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006199
6200 if (orig == NULL)
6201 return NULL;
6202
6203 copy = dict_alloc();
6204 if (copy != NULL)
6205 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006206 if (copyID != 0)
6207 {
6208 orig->dv_copyID = copyID;
6209 orig->dv_copydict = copy;
6210 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006211 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006212 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006213 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006214 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006215 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006216 --todo;
6217
6218 di = dictitem_alloc(hi->hi_key);
6219 if (di == NULL)
6220 break;
6221 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006222 {
6223 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6224 copyID) == FAIL)
6225 {
6226 vim_free(di);
6227 break;
6228 }
6229 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006230 else
6231 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6232 if (dict_add(copy, di) == FAIL)
6233 {
6234 dictitem_free(di);
6235 break;
6236 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006237 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006238 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006239
Bram Moolenaare9a41262005-01-15 22:18:47 +00006240 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006241 if (todo > 0)
6242 {
6243 dict_unref(copy);
6244 copy = NULL;
6245 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006246 }
6247
6248 return copy;
6249}
6250
6251/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006252 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006253 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006254 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006255 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006256dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 dict_T *d;
6258 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006259{
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006261}
6262
Bram Moolenaar8c711452005-01-14 21:53:12 +00006263/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006264 * Add a number or string entry to dictionary "d".
6265 * When "str" is NULL use number "nr", otherwise use "str".
6266 * Returns FAIL when out of memory and when key already exists.
6267 */
6268 int
6269dict_add_nr_str(d, key, nr, str)
6270 dict_T *d;
6271 char *key;
6272 long nr;
6273 char_u *str;
6274{
6275 dictitem_T *item;
6276
6277 item = dictitem_alloc((char_u *)key);
6278 if (item == NULL)
6279 return FAIL;
6280 item->di_tv.v_lock = 0;
6281 if (str == NULL)
6282 {
6283 item->di_tv.v_type = VAR_NUMBER;
6284 item->di_tv.vval.v_number = nr;
6285 }
6286 else
6287 {
6288 item->di_tv.v_type = VAR_STRING;
6289 item->di_tv.vval.v_string = vim_strsave(str);
6290 }
6291 if (dict_add(d, item) == FAIL)
6292 {
6293 dictitem_free(item);
6294 return FAIL;
6295 }
6296 return OK;
6297}
6298
6299/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006300 * Get the number of items in a Dictionary.
6301 */
6302 static long
6303dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006305{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006306 if (d == NULL)
6307 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006308 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006309}
6310
6311/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006312 * Find item "key[len]" in Dictionary "d".
6313 * If "len" is negative use strlen(key).
6314 * Returns NULL when not found.
6315 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006316 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006317dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006318 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006319 char_u *key;
6320 int len;
6321{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006322#define AKEYLEN 200
6323 char_u buf[AKEYLEN];
6324 char_u *akey;
6325 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006326 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006327
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006328 if (len < 0)
6329 akey = key;
6330 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006331 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006332 tofree = akey = vim_strnsave(key, len);
6333 if (akey == NULL)
6334 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006335 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006336 else
6337 {
6338 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006339 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006340 akey = buf;
6341 }
6342
Bram Moolenaar33570922005-01-25 22:26:29 +00006343 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006344 vim_free(tofree);
6345 if (HASHITEM_EMPTY(hi))
6346 return NULL;
6347 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006348}
6349
6350/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006351 * Get a string item from a dictionary.
6352 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006353 * Returns NULL if the entry doesn't exist or out of memory.
6354 */
6355 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006356get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006357 dict_T *d;
6358 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006359 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006360{
6361 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006362 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006363
6364 di = dict_find(d, key, -1);
6365 if (di == NULL)
6366 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006367 s = get_tv_string(&di->di_tv);
6368 if (save && s != NULL)
6369 s = vim_strsave(s);
6370 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006371}
6372
6373/*
6374 * Get a number item from a dictionary.
6375 * Returns 0 if the entry doesn't exist or out of memory.
6376 */
6377 long
6378get_dict_number(d, key)
6379 dict_T *d;
6380 char_u *key;
6381{
6382 dictitem_T *di;
6383
6384 di = dict_find(d, key, -1);
6385 if (di == NULL)
6386 return 0;
6387 return get_tv_number(&di->di_tv);
6388}
6389
6390/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006391 * Return an allocated string with the string representation of a Dictionary.
6392 * May return NULL.
6393 */
6394 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006395dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006396 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006397 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006398{
6399 garray_T ga;
6400 int first = TRUE;
6401 char_u *tofree;
6402 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006404 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006405 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006406 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006407
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006409 return NULL;
6410 ga_init2(&ga, (int)sizeof(char), 80);
6411 ga_append(&ga, '{');
6412
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006414 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006415 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006416 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006417 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006418 --todo;
6419
6420 if (first)
6421 first = FALSE;
6422 else
6423 ga_concat(&ga, (char_u *)", ");
6424
6425 tofree = string_quote(hi->hi_key, FALSE);
6426 if (tofree != NULL)
6427 {
6428 ga_concat(&ga, tofree);
6429 vim_free(tofree);
6430 }
6431 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006432 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006433 if (s != NULL)
6434 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006435 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006436 if (s == NULL)
6437 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006438 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006439 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006440 if (todo > 0)
6441 {
6442 vim_free(ga.ga_data);
6443 return NULL;
6444 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006445
6446 ga_append(&ga, '}');
6447 ga_append(&ga, NUL);
6448 return (char_u *)ga.ga_data;
6449}
6450
6451/*
6452 * Allocate a variable for a Dictionary and fill it from "*arg".
6453 * Return OK or FAIL. Returns NOTDONE for {expr}.
6454 */
6455 static int
6456get_dict_tv(arg, rettv, evaluate)
6457 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006459 int evaluate;
6460{
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 dict_T *d = NULL;
6462 typval_T tvkey;
6463 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006464 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006466 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006467 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006468
6469 /*
6470 * First check if it's not a curly-braces thing: {expr}.
6471 * Must do this without evaluating, otherwise a function may be called
6472 * twice. Unfortunately this means we need to call eval1() twice for the
6473 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006474 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006475 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006476 if (*start != '}')
6477 {
6478 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6479 return FAIL;
6480 if (*start == '}')
6481 return NOTDONE;
6482 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006483
6484 if (evaluate)
6485 {
6486 d = dict_alloc();
6487 if (d == NULL)
6488 return FAIL;
6489 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006490 tvkey.v_type = VAR_UNKNOWN;
6491 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006492
6493 *arg = skipwhite(*arg + 1);
6494 while (**arg != '}' && **arg != NUL)
6495 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006496 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006497 goto failret;
6498 if (**arg != ':')
6499 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006500 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006501 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006502 goto failret;
6503 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006504 key = get_tv_string_buf_chk(&tvkey, buf);
6505 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006507 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6508 if (key != NULL)
6509 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006510 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006511 goto failret;
6512 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006513
6514 *arg = skipwhite(*arg + 1);
6515 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6516 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006517 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006518 goto failret;
6519 }
6520 if (evaluate)
6521 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006522 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006523 if (item != NULL)
6524 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006525 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006526 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006527 clear_tv(&tv);
6528 goto failret;
6529 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006530 item = dictitem_alloc(key);
6531 clear_tv(&tvkey);
6532 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006533 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006534 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006535 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006536 if (dict_add(d, item) == FAIL)
6537 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006538 }
6539 }
6540
6541 if (**arg == '}')
6542 break;
6543 if (**arg != ',')
6544 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006545 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006546 goto failret;
6547 }
6548 *arg = skipwhite(*arg + 1);
6549 }
6550
6551 if (**arg != '}')
6552 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006553 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006554failret:
6555 if (evaluate)
6556 dict_free(d);
6557 return FAIL;
6558 }
6559
6560 *arg = skipwhite(*arg + 1);
6561 if (evaluate)
6562 {
6563 rettv->v_type = VAR_DICT;
6564 rettv->vval.v_dict = d;
6565 ++d->dv_refcount;
6566 }
6567
6568 return OK;
6569}
6570
Bram Moolenaar8c711452005-01-14 21:53:12 +00006571/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 * Return a string with the string representation of a variable.
6573 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006575 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006576 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577 * May return NULL;
6578 */
6579 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006580echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006584 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006586 static int recurse = 0;
6587 char_u *r = NULL;
6588
Bram Moolenaar33570922005-01-25 22:26:29 +00006589 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006590 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006591 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006592 *tofree = NULL;
6593 return NULL;
6594 }
6595 ++recurse;
6596
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597 switch (tv->v_type)
6598 {
6599 case VAR_FUNC:
6600 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006601 r = tv->vval.v_string;
6602 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006603
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006604 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006605 if (tv->vval.v_list == NULL)
6606 {
6607 *tofree = NULL;
6608 r = NULL;
6609 }
6610 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6611 {
6612 *tofree = NULL;
6613 r = (char_u *)"[...]";
6614 }
6615 else
6616 {
6617 tv->vval.v_list->lv_copyID = copyID;
6618 *tofree = list2string(tv, copyID);
6619 r = *tofree;
6620 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006621 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006622
Bram Moolenaar8c711452005-01-14 21:53:12 +00006623 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006624 if (tv->vval.v_dict == NULL)
6625 {
6626 *tofree = NULL;
6627 r = NULL;
6628 }
6629 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6630 {
6631 *tofree = NULL;
6632 r = (char_u *)"{...}";
6633 }
6634 else
6635 {
6636 tv->vval.v_dict->dv_copyID = copyID;
6637 *tofree = dict2string(tv, copyID);
6638 r = *tofree;
6639 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006640 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006641
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006642 case VAR_STRING:
6643 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006644 *tofree = NULL;
6645 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006647
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006648 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006649 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006650 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006651 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006652
6653 --recurse;
6654 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006655}
6656
6657/*
6658 * Return a string with the string representation of a variable.
6659 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6660 * "numbuf" is used for a number.
6661 * Puts quotes around strings, so that they can be parsed back by eval().
6662 * May return NULL;
6663 */
6664 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006665tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006666 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006667 char_u **tofree;
6668 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006669 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006670{
6671 switch (tv->v_type)
6672 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006673 case VAR_FUNC:
6674 *tofree = string_quote(tv->vval.v_string, TRUE);
6675 return *tofree;
6676 case VAR_STRING:
6677 *tofree = string_quote(tv->vval.v_string, FALSE);
6678 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006679 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006680 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006681 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006682 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006683 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006684 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006685 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006686 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687}
6688
6689/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006690 * Return string "str" in ' quotes, doubling ' characters.
6691 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006692 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006693 */
6694 static char_u *
6695string_quote(str, function)
6696 char_u *str;
6697 int function;
6698{
Bram Moolenaar33570922005-01-25 22:26:29 +00006699 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006700 char_u *p, *r, *s;
6701
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 len = (function ? 13 : 3);
6703 if (str != NULL)
6704 {
6705 len += STRLEN(str);
6706 for (p = str; *p != NUL; mb_ptr_adv(p))
6707 if (*p == '\'')
6708 ++len;
6709 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006710 s = r = alloc(len);
6711 if (r != NULL)
6712 {
6713 if (function)
6714 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006715 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006716 r += 10;
6717 }
6718 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006719 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006720 if (str != NULL)
6721 for (p = str; *p != NUL; )
6722 {
6723 if (*p == '\'')
6724 *r++ = '\'';
6725 MB_COPY_CHAR(p, r);
6726 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006727 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006728 if (function)
6729 *r++ = ')';
6730 *r++ = NUL;
6731 }
6732 return s;
6733}
6734
6735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 * Get the value of an environment variable.
6737 * "arg" is pointing to the '$'. It is advanced to after the name.
6738 * If the environment variable was not set, silently assume it is empty.
6739 * Always return OK.
6740 */
6741 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006742get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 int evaluate;
6746{
6747 char_u *string = NULL;
6748 int len;
6749 int cc;
6750 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006751 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
6753 ++*arg;
6754 name = *arg;
6755 len = get_env_len(arg);
6756 if (evaluate)
6757 {
6758 if (len != 0)
6759 {
6760 cc = name[len];
6761 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006762 /* first try vim_getenv(), fast for normal environment vars */
6763 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006765 {
6766 if (!mustfree)
6767 string = vim_strsave(string);
6768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 else
6770 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006771 if (mustfree)
6772 vim_free(string);
6773
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 /* next try expanding things like $VIM and ${HOME} */
6775 string = expand_env_save(name - 1);
6776 if (string != NULL && *string == '$')
6777 {
6778 vim_free(string);
6779 string = NULL;
6780 }
6781 }
6782 name[len] = cc;
6783 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006784 rettv->v_type = VAR_STRING;
6785 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 }
6787
6788 return OK;
6789}
6790
6791/*
6792 * Array with names and number of arguments of all internal functions
6793 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6794 */
6795static struct fst
6796{
6797 char *f_name; /* function name */
6798 char f_min_argc; /* minimal number of arguments */
6799 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006800 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006801 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802} functions[] =
6803{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006804 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 {"append", 2, 2, f_append},
6806 {"argc", 0, 0, f_argc},
6807 {"argidx", 0, 0, f_argidx},
6808 {"argv", 1, 1, f_argv},
6809 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006810 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {"bufexists", 1, 1, f_bufexists},
6812 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6813 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6814 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6815 {"buflisted", 1, 1, f_buflisted},
6816 {"bufloaded", 1, 1, f_bufloaded},
6817 {"bufname", 1, 1, f_bufname},
6818 {"bufnr", 1, 1, f_bufnr},
6819 {"bufwinnr", 1, 1, f_bufwinnr},
6820 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006821 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006822 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 {"char2nr", 1, 1, f_char2nr},
6824 {"cindent", 1, 1, f_cindent},
6825 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006826#if defined(FEAT_INS_EXPAND)
6827 {"complete_add", 1, 1, f_complete_add},
6828 {"complete_check", 0, 0, f_complete_check},
6829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006831 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006832 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 {"cscope_connection",0,3, f_cscope_connection},
6834 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006835 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 {"delete", 1, 1, f_delete},
6837 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006838 {"diff_filler", 1, 1, f_diff_filler},
6839 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006840 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006842 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 {"eventhandler", 0, 0, f_eventhandler},
6844 {"executable", 1, 1, f_executable},
6845 {"exists", 1, 1, f_exists},
6846 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006847 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6849 {"filereadable", 1, 1, f_filereadable},
6850 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006851 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006852 {"finddir", 1, 3, f_finddir},
6853 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 {"fnamemodify", 2, 2, f_fnamemodify},
6855 {"foldclosed", 1, 1, f_foldclosed},
6856 {"foldclosedend", 1, 1, f_foldclosedend},
6857 {"foldlevel", 1, 1, f_foldlevel},
6858 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006859 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006861 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006863 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006864 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 {"getbufvar", 2, 2, f_getbufvar},
6866 {"getchar", 0, 1, f_getchar},
6867 {"getcharmod", 0, 0, f_getcharmod},
6868 {"getcmdline", 0, 0, f_getcmdline},
6869 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006870 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006872 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006873 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 {"getfsize", 1, 1, f_getfsize},
6875 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006876 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006877 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006878 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006879 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006880 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {"getregtype", 0, 1, f_getregtype},
6882 {"getwinposx", 0, 0, f_getwinposx},
6883 {"getwinposy", 0, 0, f_getwinposy},
6884 {"getwinvar", 2, 2, f_getwinvar},
6885 {"glob", 1, 1, f_glob},
6886 {"globpath", 2, 2, f_globpath},
6887 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006888 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {"hasmapto", 1, 2, f_hasmapto},
6890 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6891 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6892 {"histadd", 2, 2, f_histadd},
6893 {"histdel", 1, 2, f_histdel},
6894 {"histget", 1, 2, f_histget},
6895 {"histnr", 1, 1, f_histnr},
6896 {"hlID", 1, 1, f_hlID},
6897 {"hlexists", 1, 1, f_hlexists},
6898 {"hostname", 0, 0, f_hostname},
6899 {"iconv", 3, 3, f_iconv},
6900 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006901 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006902 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006904 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 {"inputrestore", 0, 0, f_inputrestore},
6906 {"inputsave", 0, 0, f_inputsave},
6907 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006908 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006910 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006911 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006912 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006913 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006915 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 {"libcall", 3, 3, f_libcall},
6917 {"libcallnr", 3, 3, f_libcallnr},
6918 {"line", 1, 1, f_line},
6919 {"line2byte", 1, 1, f_line2byte},
6920 {"lispindent", 1, 1, f_lispindent},
6921 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006922 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 {"maparg", 1, 2, f_maparg},
6924 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006925 {"match", 2, 4, f_match},
6926 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006927 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006928 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006929 {"max", 1, 1, f_max},
6930 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006931#ifdef vim_mkdir
6932 {"mkdir", 1, 3, f_mkdir},
6933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 {"mode", 0, 0, f_mode},
6935 {"nextnonblank", 1, 1, f_nextnonblank},
6936 {"nr2char", 1, 1, f_nr2char},
6937 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006938 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006939 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006940 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006941 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 {"remote_expr", 2, 3, f_remote_expr},
6943 {"remote_foreground", 1, 1, f_remote_foreground},
6944 {"remote_peek", 1, 2, f_remote_peek},
6945 {"remote_read", 1, 1, f_remote_read},
6946 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006947 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006949 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006951 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006953 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 {"searchpair", 3, 5, f_searchpair},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006955 {"searchpairpos", 3, 5, f_searchpairpos},
6956 {"searchpos", 1, 2, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {"server2client", 2, 2, f_server2client},
6958 {"serverlist", 0, 0, f_serverlist},
6959 {"setbufvar", 3, 3, f_setbufvar},
6960 {"setcmdpos", 1, 1, f_setcmdpos},
6961 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006962 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006963 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {"setreg", 2, 3, f_setreg},
6965 {"setwinvar", 3, 3, f_setwinvar},
6966 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006967 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006968 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006969 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006970 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006971 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972#ifdef HAVE_STRFTIME
6973 {"strftime", 1, 2, f_strftime},
6974#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006975 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006976 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {"strlen", 1, 1, f_strlen},
6978 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006979 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 {"strtrans", 1, 1, f_strtrans},
6981 {"submatch", 1, 1, f_submatch},
6982 {"substitute", 4, 4, f_substitute},
6983 {"synID", 3, 3, f_synID},
6984 {"synIDattr", 2, 3, f_synIDattr},
6985 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006986 {"system", 1, 2, f_system},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006987 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006988 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006989 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006991 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {"tolower", 1, 1, f_tolower},
6993 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006994 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006996 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {"virtcol", 1, 1, f_virtcol},
6998 {"visualmode", 0, 1, f_visualmode},
6999 {"winbufnr", 1, 1, f_winbufnr},
7000 {"wincol", 0, 0, f_wincol},
7001 {"winheight", 1, 1, f_winheight},
7002 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007003 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 {"winrestcmd", 0, 0, f_winrestcmd},
7005 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007006 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007};
7008
7009#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7010
7011/*
7012 * Function given to ExpandGeneric() to obtain the list of internal
7013 * or user defined function names.
7014 */
7015 char_u *
7016get_function_name(xp, idx)
7017 expand_T *xp;
7018 int idx;
7019{
7020 static int intidx = -1;
7021 char_u *name;
7022
7023 if (idx == 0)
7024 intidx = -1;
7025 if (intidx < 0)
7026 {
7027 name = get_user_func_name(xp, idx);
7028 if (name != NULL)
7029 return name;
7030 }
7031 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7032 {
7033 STRCPY(IObuff, functions[intidx].f_name);
7034 STRCAT(IObuff, "(");
7035 if (functions[intidx].f_max_argc == 0)
7036 STRCAT(IObuff, ")");
7037 return IObuff;
7038 }
7039
7040 return NULL;
7041}
7042
7043/*
7044 * Function given to ExpandGeneric() to obtain the list of internal or
7045 * user defined variable or function names.
7046 */
7047/*ARGSUSED*/
7048 char_u *
7049get_expr_name(xp, idx)
7050 expand_T *xp;
7051 int idx;
7052{
7053 static int intidx = -1;
7054 char_u *name;
7055
7056 if (idx == 0)
7057 intidx = -1;
7058 if (intidx < 0)
7059 {
7060 name = get_function_name(xp, idx);
7061 if (name != NULL)
7062 return name;
7063 }
7064 return get_user_var_name(xp, ++intidx);
7065}
7066
7067#endif /* FEAT_CMDL_COMPL */
7068
7069/*
7070 * Find internal function in table above.
7071 * Return index, or -1 if not found
7072 */
7073 static int
7074find_internal_func(name)
7075 char_u *name; /* name of the function */
7076{
7077 int first = 0;
7078 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7079 int cmp;
7080 int x;
7081
7082 /*
7083 * Find the function name in the table. Binary search.
7084 */
7085 while (first <= last)
7086 {
7087 x = first + ((unsigned)(last - first) >> 1);
7088 cmp = STRCMP(name, functions[x].f_name);
7089 if (cmp < 0)
7090 last = x - 1;
7091 else if (cmp > 0)
7092 first = x + 1;
7093 else
7094 return x;
7095 }
7096 return -1;
7097}
7098
7099/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007100 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7101 * name it contains, otherwise return "name".
7102 */
7103 static char_u *
7104deref_func_name(name, lenp)
7105 char_u *name;
7106 int *lenp;
7107{
Bram Moolenaar33570922005-01-25 22:26:29 +00007108 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007109 int cc;
7110
7111 cc = name[*lenp];
7112 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007113 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007114 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007116 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007117 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007118 {
7119 *lenp = 0;
7120 return (char_u *)""; /* just in case */
7121 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 *lenp = STRLEN(v->di_tv.vval.v_string);
7123 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007124 }
7125
7126 return name;
7127}
7128
7129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 * Allocate a variable for the result of a function.
7131 * Return OK or FAIL.
7132 */
7133 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007134get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7135 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 char_u *name; /* name of the function */
7137 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 char_u **arg; /* argument, pointing to the '(' */
7140 linenr_T firstline; /* first line of range */
7141 linenr_T lastline; /* last line of range */
7142 int *doesrange; /* return: function handled range */
7143 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007144 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145{
7146 char_u *argp;
7147 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 int argcount = 0; /* number of arguments found */
7150
7151 /*
7152 * Get the arguments.
7153 */
7154 argp = *arg;
7155 while (argcount < MAX_FUNC_ARGS)
7156 {
7157 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7158 if (*argp == ')' || *argp == ',' || *argp == NUL)
7159 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7161 {
7162 ret = FAIL;
7163 break;
7164 }
7165 ++argcount;
7166 if (*argp != ',')
7167 break;
7168 }
7169 if (*argp == ')')
7170 ++argp;
7171 else
7172 ret = FAIL;
7173
7174 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007175 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007176 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007178 {
7179 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007180 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007182 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
7185 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007186 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
7188 *arg = skipwhite(argp);
7189 return ret;
7190}
7191
7192
7193/*
7194 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007195 * Return OK when the function can't be called, FAIL otherwise.
7196 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 */
7198 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007199call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007200 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 char_u *name; /* name of the function */
7202 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007205 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 linenr_T firstline; /* first line of range */
7207 linenr_T lastline; /* last line of range */
7208 int *doesrange; /* return: function handled range */
7209 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007210 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211{
7212 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213#define ERROR_UNKNOWN 0
7214#define ERROR_TOOMANY 1
7215#define ERROR_TOOFEW 2
7216#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007217#define ERROR_DICT 4
7218#define ERROR_NONE 5
7219#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 int error = ERROR_NONE;
7221 int i;
7222 int llen;
7223 ufunc_T *fp;
7224 int cc;
7225#define FLEN_FIXED 40
7226 char_u fname_buf[FLEN_FIXED + 1];
7227 char_u *fname;
7228
7229 /*
7230 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7231 * Change <SNR>123_name() to K_SNR 123_name().
7232 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7233 */
7234 cc = name[len];
7235 name[len] = NUL;
7236 llen = eval_fname_script(name);
7237 if (llen > 0)
7238 {
7239 fname_buf[0] = K_SPECIAL;
7240 fname_buf[1] = KS_EXTRA;
7241 fname_buf[2] = (int)KE_SNR;
7242 i = 3;
7243 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7244 {
7245 if (current_SID <= 0)
7246 error = ERROR_SCRIPT;
7247 else
7248 {
7249 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7250 i = (int)STRLEN(fname_buf);
7251 }
7252 }
7253 if (i + STRLEN(name + llen) < FLEN_FIXED)
7254 {
7255 STRCPY(fname_buf + i, name + llen);
7256 fname = fname_buf;
7257 }
7258 else
7259 {
7260 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7261 if (fname == NULL)
7262 error = ERROR_OTHER;
7263 else
7264 {
7265 mch_memmove(fname, fname_buf, (size_t)i);
7266 STRCPY(fname + i, name + llen);
7267 }
7268 }
7269 }
7270 else
7271 fname = name;
7272
7273 *doesrange = FALSE;
7274
7275
7276 /* execute the function if no errors detected and executing */
7277 if (evaluate && error == ERROR_NONE)
7278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007279 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 error = ERROR_UNKNOWN;
7281
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007282 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 {
7284 /*
7285 * User defined function.
7286 */
7287 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007288
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007290 /* Trigger FuncUndefined event, may load the function. */
7291 if (fp == NULL
7292 && apply_autocmds(EVENT_FUNCUNDEFINED,
7293 fname, fname, TRUE, NULL)
7294 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007296 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 fp = find_func(fname);
7298 }
7299#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007300 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007301 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007302 {
7303 /* loaded a package, search for the function again */
7304 fp = find_func(fname);
7305 }
7306
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 if (fp != NULL)
7308 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007309 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007311 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007313 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007315 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007316 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 else
7318 {
7319 /*
7320 * Call the user function.
7321 * Save and restore search patterns, script variables and
7322 * redo buffer.
7323 */
7324 save_search_patterns();
7325 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007326 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007327 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007328 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007329 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7330 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7331 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007332 /* Function was unreferenced while being used, free it
7333 * now. */
7334 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 restoreRedobuff();
7336 restore_search_patterns();
7337 error = ERROR_NONE;
7338 }
7339 }
7340 }
7341 else
7342 {
7343 /*
7344 * Find the function name in the table, call its implementation.
7345 */
7346 i = find_internal_func(fname);
7347 if (i >= 0)
7348 {
7349 if (argcount < functions[i].f_min_argc)
7350 error = ERROR_TOOFEW;
7351 else if (argcount > functions[i].f_max_argc)
7352 error = ERROR_TOOMANY;
7353 else
7354 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007355 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007356 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 error = ERROR_NONE;
7358 }
7359 }
7360 }
7361 /*
7362 * The function call (or "FuncUndefined" autocommand sequence) might
7363 * have been aborted by an error, an interrupt, or an explicitly thrown
7364 * exception that has not been caught so far. This situation can be
7365 * tested for by calling aborting(). For an error in an internal
7366 * function or for the "E132" error in call_user_func(), however, the
7367 * throw point at which the "force_abort" flag (temporarily reset by
7368 * emsg()) is normally updated has not been reached yet. We need to
7369 * update that flag first to make aborting() reliable.
7370 */
7371 update_force_abort();
7372 }
7373 if (error == ERROR_NONE)
7374 ret = OK;
7375
7376 /*
7377 * Report an error unless the argument evaluation or function call has been
7378 * cancelled due to an aborting error, an interrupt, or an exception.
7379 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007380 if (!aborting())
7381 {
7382 switch (error)
7383 {
7384 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007385 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 break;
7387 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007388 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007389 break;
7390 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007391 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392 name);
7393 break;
7394 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007395 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 name);
7397 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007399 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007400 name);
7401 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 }
7403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404
7405 name[len] = cc;
7406 if (fname != name && fname != fname_buf)
7407 vim_free(fname);
7408
7409 return ret;
7410}
7411
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007412/*
7413 * Give an error message with a function name. Handle <SNR> things.
7414 */
7415 static void
7416emsg_funcname(msg, name)
7417 char *msg;
7418 char_u *name;
7419{
7420 char_u *p;
7421
7422 if (*name == K_SPECIAL)
7423 p = concat_str((char_u *)"<SNR>", name + 3);
7424 else
7425 p = name;
7426 EMSG2(_(msg), p);
7427 if (p != name)
7428 vim_free(p);
7429}
7430
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431/*********************************************
7432 * Implementation of the built-in functions
7433 */
7434
7435/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007436 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 */
7438 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007439f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007440 typval_T *argvars;
7441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442{
Bram Moolenaar33570922005-01-25 22:26:29 +00007443 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007445 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007446 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007448 if ((l = argvars[0].vval.v_list) != NULL
7449 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7450 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007451 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007452 }
7453 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007454 EMSG(_(e_listreq));
7455}
7456
7457/*
7458 * "append(lnum, string/list)" function
7459 */
7460 static void
7461f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007462 typval_T *argvars;
7463 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007464{
7465 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007466 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007467 list_T *l = NULL;
7468 listitem_T *li = NULL;
7469 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007470 long added = 0;
7471
Bram Moolenaar0d660222005-01-07 21:51:51 +00007472 lnum = get_tv_lnum(argvars);
7473 if (lnum >= 0
7474 && lnum <= curbuf->b_ml.ml_line_count
7475 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007476 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007477 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007478 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007479 l = argvars[1].vval.v_list;
7480 if (l == NULL)
7481 return;
7482 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007483 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007484 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007485 for (;;)
7486 {
7487 if (l == NULL)
7488 tv = &argvars[1]; /* append a string */
7489 else if (li == NULL)
7490 break; /* end of list */
7491 else
7492 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007493 line = get_tv_string_chk(tv);
7494 if (line == NULL) /* type error */
7495 {
7496 rettv->vval.v_number = 1; /* Failed */
7497 break;
7498 }
7499 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007500 ++added;
7501 if (l == NULL)
7502 break;
7503 li = li->li_next;
7504 }
7505
7506 appended_lines_mark(lnum, added);
7507 if (curwin->w_cursor.lnum > lnum)
7508 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007510 else
7511 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512}
7513
7514/*
7515 * "argc()" function
7516 */
7517/* ARGSUSED */
7518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007519f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007520 typval_T *argvars;
7521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007523 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524}
7525
7526/*
7527 * "argidx()" function
7528 */
7529/* ARGSUSED */
7530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007531f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007532 typval_T *argvars;
7533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007535 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536}
7537
7538/*
7539 * "argv(nr)" function
7540 */
7541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007542f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 typval_T *argvars;
7544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545{
7546 int idx;
7547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007548 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007550 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007552 rettv->vval.v_string = NULL;
7553 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554}
7555
7556/*
7557 * "browse(save, title, initdir, default)" function
7558 */
7559/* ARGSUSED */
7560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007561f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007562 typval_T *argvars;
7563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564{
7565#ifdef FEAT_BROWSE
7566 int save;
7567 char_u *title;
7568 char_u *initdir;
7569 char_u *defname;
7570 char_u buf[NUMBUFLEN];
7571 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007572 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007574 save = get_tv_number_chk(&argvars[0], &error);
7575 title = get_tv_string_chk(&argvars[1]);
7576 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7577 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007579 if (error || title == NULL || initdir == NULL || defname == NULL)
7580 rettv->vval.v_string = NULL;
7581 else
7582 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007583 do_browse(save ? BROWSE_SAVE : 0,
7584 title, defname, NULL, initdir, NULL, curbuf);
7585#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007586 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007587#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007588 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007589}
7590
7591/*
7592 * "browsedir(title, initdir)" function
7593 */
7594/* ARGSUSED */
7595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007596f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 typval_T *argvars;
7598 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007599{
7600#ifdef FEAT_BROWSE
7601 char_u *title;
7602 char_u *initdir;
7603 char_u buf[NUMBUFLEN];
7604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007605 title = get_tv_string_chk(&argvars[0]);
7606 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007608 if (title == NULL || initdir == NULL)
7609 rettv->vval.v_string = NULL;
7610 else
7611 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007612 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007614 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007616 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617}
7618
Bram Moolenaar33570922005-01-25 22:26:29 +00007619static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007620
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621/*
7622 * Find a buffer by number or exact name.
7623 */
7624 static buf_T *
7625find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627{
7628 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007630 if (avar->v_type == VAR_NUMBER)
7631 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007632 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007634 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007635 if (buf == NULL)
7636 {
7637 /* No full path name match, try a match with a URL or a "nofile"
7638 * buffer, these don't use the full path. */
7639 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7640 if (buf->b_fname != NULL
7641 && (path_with_url(buf->b_fname)
7642#ifdef FEAT_QUICKFIX
7643 || bt_nofile(buf)
7644#endif
7645 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007646 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007647 break;
7648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 }
7650 return buf;
7651}
7652
7653/*
7654 * "bufexists(expr)" function
7655 */
7656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007657f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007658 typval_T *argvars;
7659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007661 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662}
7663
7664/*
7665 * "buflisted(expr)" function
7666 */
7667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007668f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007669 typval_T *argvars;
7670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
7672 buf_T *buf;
7673
7674 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007675 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676}
7677
7678/*
7679 * "bufloaded(expr)" function
7680 */
7681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007682f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007683 typval_T *argvars;
7684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685{
7686 buf_T *buf;
7687
7688 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690}
7691
Bram Moolenaar33570922005-01-25 22:26:29 +00007692static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007693
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694/*
7695 * Get buffer by number or pattern.
7696 */
7697 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007698get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007699 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007701 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 int save_magic;
7703 char_u *save_cpo;
7704 buf_T *buf;
7705
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007706 if (tv->v_type == VAR_NUMBER)
7707 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007708 if (tv->v_type != VAR_STRING)
7709 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 if (name == NULL || *name == NUL)
7711 return curbuf;
7712 if (name[0] == '$' && name[1] == NUL)
7713 return lastbuf;
7714
7715 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7716 save_magic = p_magic;
7717 p_magic = TRUE;
7718 save_cpo = p_cpo;
7719 p_cpo = (char_u *)"";
7720
7721 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7722 TRUE, FALSE));
7723
7724 p_magic = save_magic;
7725 p_cpo = save_cpo;
7726
7727 /* If not found, try expanding the name, like done for bufexists(). */
7728 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007729 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730
7731 return buf;
7732}
7733
7734/*
7735 * "bufname(expr)" function
7736 */
7737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007738f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007739 typval_T *argvars;
7740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741{
7742 buf_T *buf;
7743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007744 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007746 buf = get_buf_tv(&argvars[0]);
7747 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007749 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007751 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 --emsg_off;
7753}
7754
7755/*
7756 * "bufnr(expr)" function
7757 */
7758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007759f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007760 typval_T *argvars;
7761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762{
7763 buf_T *buf;
7764
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007765 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007767 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007769 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007771 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 --emsg_off;
7773}
7774
7775/*
7776 * "bufwinnr(nr)" function
7777 */
7778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007779f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007780 typval_T *argvars;
7781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
7783#ifdef FEAT_WINDOWS
7784 win_T *wp;
7785 int winnr = 0;
7786#endif
7787 buf_T *buf;
7788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007789 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007791 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792#ifdef FEAT_WINDOWS
7793 for (wp = firstwin; wp; wp = wp->w_next)
7794 {
7795 ++winnr;
7796 if (wp->w_buffer == buf)
7797 break;
7798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007799 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007801 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802#endif
7803 --emsg_off;
7804}
7805
7806/*
7807 * "byte2line(byte)" function
7808 */
7809/*ARGSUSED*/
7810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007811f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007812 typval_T *argvars;
7813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814{
7815#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007816 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817#else
7818 long boff = 0;
7819
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007820 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007822 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007824 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 (linenr_T)0, &boff);
7826#endif
7827}
7828
7829/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007830 * "byteidx()" function
7831 */
7832/*ARGSUSED*/
7833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007834f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007835 typval_T *argvars;
7836 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007837{
7838#ifdef FEAT_MBYTE
7839 char_u *t;
7840#endif
7841 char_u *str;
7842 long idx;
7843
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007844 str = get_tv_string_chk(&argvars[0]);
7845 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007846 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007847 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007848 return;
7849
7850#ifdef FEAT_MBYTE
7851 t = str;
7852 for ( ; idx > 0; idx--)
7853 {
7854 if (*t == NUL) /* EOL reached */
7855 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007856 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007857 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007858 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007859#else
7860 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007861 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007862#endif
7863}
7864
7865/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007866 * "call(func, arglist)" function
7867 */
7868 static void
7869f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007870 typval_T *argvars;
7871 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007872{
7873 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007874 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007875 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007876 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007877 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007878 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007879
7880 rettv->vval.v_number = 0;
7881 if (argvars[1].v_type != VAR_LIST)
7882 {
7883 EMSG(_(e_listreq));
7884 return;
7885 }
7886 if (argvars[1].vval.v_list == NULL)
7887 return;
7888
7889 if (argvars[0].v_type == VAR_FUNC)
7890 func = argvars[0].vval.v_string;
7891 else
7892 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007893 if (*func == NUL)
7894 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007895
Bram Moolenaare9a41262005-01-15 22:18:47 +00007896 if (argvars[2].v_type != VAR_UNKNOWN)
7897 {
7898 if (argvars[2].v_type != VAR_DICT)
7899 {
7900 EMSG(_(e_dictreq));
7901 return;
7902 }
7903 selfdict = argvars[2].vval.v_dict;
7904 }
7905
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007906 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7907 item = item->li_next)
7908 {
7909 if (argc == MAX_FUNC_ARGS)
7910 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007911 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007912 break;
7913 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007914 /* Make a copy of each argument. This is needed to be able to set
7915 * v_lock to VAR_FIXED in the copy without changing the original list.
7916 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007917 copy_tv(&item->li_tv, &argv[argc++]);
7918 }
7919
7920 if (item == NULL)
7921 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007922 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7923 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007924
7925 /* Free the arguments. */
7926 while (argc > 0)
7927 clear_tv(&argv[--argc]);
7928}
7929
7930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 * "char2nr(string)" function
7932 */
7933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007934f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007935 typval_T *argvars;
7936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
7938#ifdef FEAT_MBYTE
7939 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007940 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 else
7942#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007943 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944}
7945
7946/*
7947 * "cindent(lnum)" function
7948 */
7949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007950f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007951 typval_T *argvars;
7952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953{
7954#ifdef FEAT_CINDENT
7955 pos_T pos;
7956 linenr_T lnum;
7957
7958 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007959 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7961 {
7962 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007963 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 curwin->w_cursor = pos;
7965 }
7966 else
7967#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969}
7970
7971/*
7972 * "col(string)" function
7973 */
7974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007975f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007976 typval_T *argvars;
7977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
7979 colnr_T col = 0;
7980 pos_T *fp;
7981
7982 fp = var2fpos(&argvars[0], FALSE);
7983 if (fp != NULL)
7984 {
7985 if (fp->col == MAXCOL)
7986 {
7987 /* '> can be MAXCOL, get the length of the line then */
7988 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7989 col = STRLEN(ml_get(fp->lnum)) + 1;
7990 else
7991 col = MAXCOL;
7992 }
7993 else
7994 {
7995 col = fp->col + 1;
7996#ifdef FEAT_VIRTUALEDIT
7997 /* col(".") when the cursor is on the NUL at the end of the line
7998 * because of "coladd" can be seen as an extra column. */
7999 if (virtual_active() && fp == &curwin->w_cursor)
8000 {
8001 char_u *p = ml_get_cursor();
8002
8003 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8004 curwin->w_virtcol - curwin->w_cursor.coladd))
8005 {
8006# ifdef FEAT_MBYTE
8007 int l;
8008
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008009 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 col += l;
8011# else
8012 if (*p != NUL && p[1] == NUL)
8013 ++col;
8014# endif
8015 }
8016 }
8017#endif
8018 }
8019 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008020 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021}
8022
Bram Moolenaar572cb562005-08-05 21:35:02 +00008023#if defined(FEAT_INS_EXPAND)
8024/*
8025 * "complete_add()" function
8026 */
8027/*ARGSUSED*/
8028 static void
8029f_complete_add(argvars, rettv)
8030 typval_T *argvars;
8031 typval_T *rettv;
8032{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008033 char_u *word;
8034 char_u *extra = NULL;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008035
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008036 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8037 {
8038 word = get_dict_string(argvars[0].vval.v_dict,
8039 (char_u *)"word", FALSE);
8040 extra = get_dict_string(argvars[0].vval.v_dict,
8041 (char_u *)"menu", FALSE);
8042 }
8043 else
8044 word = get_tv_string_chk(&argvars[0]);
8045 if (word != NULL)
8046 rettv->vval.v_number = ins_compl_add(word, -1, NULL, extra, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008047}
8048
8049/*
8050 * "complete_check()" function
8051 */
8052/*ARGSUSED*/
8053 static void
8054f_complete_check(argvars, rettv)
8055 typval_T *argvars;
8056 typval_T *rettv;
8057{
8058 int saved = RedrawingDisabled;
8059
8060 RedrawingDisabled = 0;
8061 ins_compl_check_keys(0);
8062 rettv->vval.v_number = compl_interrupted;
8063 RedrawingDisabled = saved;
8064}
8065#endif
8066
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067/*
8068 * "confirm(message, buttons[, default [, type]])" function
8069 */
8070/*ARGSUSED*/
8071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008072f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008073 typval_T *argvars;
8074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075{
8076#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8077 char_u *message;
8078 char_u *buttons = NULL;
8079 char_u buf[NUMBUFLEN];
8080 char_u buf2[NUMBUFLEN];
8081 int def = 1;
8082 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008083 char_u *typestr;
8084 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008086 message = get_tv_string_chk(&argvars[0]);
8087 if (message == NULL)
8088 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008089 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008091 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8092 if (buttons == NULL)
8093 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008094 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008096 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008097 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008099 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8100 if (typestr == NULL)
8101 error = TRUE;
8102 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008104 switch (TOUPPER_ASC(*typestr))
8105 {
8106 case 'E': type = VIM_ERROR; break;
8107 case 'Q': type = VIM_QUESTION; break;
8108 case 'I': type = VIM_INFO; break;
8109 case 'W': type = VIM_WARNING; break;
8110 case 'G': type = VIM_GENERIC; break;
8111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 }
8113 }
8114 }
8115 }
8116
8117 if (buttons == NULL || *buttons == NUL)
8118 buttons = (char_u *)_("&Ok");
8119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008120 if (error)
8121 rettv->vval.v_number = 0;
8122 else
8123 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 def, NULL);
8125#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008126 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127#endif
8128}
8129
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008130/*
8131 * "copy()" function
8132 */
8133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008134f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008135 typval_T *argvars;
8136 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008137{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008138 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008139}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140
8141/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008142 * "count()" function
8143 */
8144 static void
8145f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008146 typval_T *argvars;
8147 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008148{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008149 long n = 0;
8150 int ic = FALSE;
8151
Bram Moolenaare9a41262005-01-15 22:18:47 +00008152 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008153 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008154 listitem_T *li;
8155 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008156 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008157
Bram Moolenaare9a41262005-01-15 22:18:47 +00008158 if ((l = argvars[0].vval.v_list) != NULL)
8159 {
8160 li = l->lv_first;
8161 if (argvars[2].v_type != VAR_UNKNOWN)
8162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008163 int error = FALSE;
8164
8165 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008166 if (argvars[3].v_type != VAR_UNKNOWN)
8167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008168 idx = get_tv_number_chk(&argvars[3], &error);
8169 if (!error)
8170 {
8171 li = list_find(l, idx);
8172 if (li == NULL)
8173 EMSGN(_(e_listidx), idx);
8174 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008175 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008176 if (error)
8177 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008178 }
8179
8180 for ( ; li != NULL; li = li->li_next)
8181 if (tv_equal(&li->li_tv, &argvars[1], ic))
8182 ++n;
8183 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008184 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008185 else if (argvars[0].v_type == VAR_DICT)
8186 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008187 int todo;
8188 dict_T *d;
8189 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008190
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008191 if ((d = argvars[0].vval.v_dict) != NULL)
8192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008193 int error = FALSE;
8194
Bram Moolenaare9a41262005-01-15 22:18:47 +00008195 if (argvars[2].v_type != VAR_UNKNOWN)
8196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008197 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008198 if (argvars[3].v_type != VAR_UNKNOWN)
8199 EMSG(_(e_invarg));
8200 }
8201
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008202 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008203 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008204 {
8205 if (!HASHITEM_EMPTY(hi))
8206 {
8207 --todo;
8208 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8209 ++n;
8210 }
8211 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008212 }
8213 }
8214 else
8215 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008216 rettv->vval.v_number = n;
8217}
8218
8219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8221 *
8222 * Checks the existence of a cscope connection.
8223 */
8224/*ARGSUSED*/
8225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008226f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008227 typval_T *argvars;
8228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
8230#ifdef FEAT_CSCOPE
8231 int num = 0;
8232 char_u *dbpath = NULL;
8233 char_u *prepend = NULL;
8234 char_u buf[NUMBUFLEN];
8235
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008236 if (argvars[0].v_type != VAR_UNKNOWN
8237 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008239 num = (int)get_tv_number(&argvars[0]);
8240 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008241 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008242 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 }
8244
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008245 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008247 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248#endif
8249}
8250
8251/*
8252 * "cursor(lnum, col)" function
8253 *
8254 * Moves the cursor to the specified line and column
8255 */
8256/*ARGSUSED*/
8257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008258f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008259 typval_T *argvars;
8260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261{
8262 long line, col;
8263
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008264 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008265 col = get_tv_number_chk(&argvars[1], NULL);
8266 if (line < 0 || col < 0)
8267 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 if (line > 0)
8269 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 if (col > 0)
8271 curwin->w_cursor.col = col - 1;
8272#ifdef FEAT_VIRTUALEDIT
8273 curwin->w_cursor.coladd = 0;
8274#endif
8275
8276 /* Make sure the cursor is in a valid position. */
8277 check_cursor();
8278#ifdef FEAT_MBYTE
8279 /* Correct cursor for multi-byte character. */
8280 if (has_mbyte)
8281 mb_adjust_cursor();
8282#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008283
8284 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285}
8286
8287/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008288 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 */
8290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008291f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008292 typval_T *argvars;
8293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008295 int noref = 0;
8296
8297 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008298 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008299 if (noref < 0 || noref > 1)
8300 EMSG(_(e_invarg));
8301 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008302 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303}
8304
8305/*
8306 * "delete()" function
8307 */
8308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008309f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008310 typval_T *argvars;
8311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312{
8313 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008314 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008316 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317}
8318
8319/*
8320 * "did_filetype()" function
8321 */
8322/*ARGSUSED*/
8323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008324f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008325 typval_T *argvars;
8326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327{
8328#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008329 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008331 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332#endif
8333}
8334
8335/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008336 * "diff_filler()" function
8337 */
8338/*ARGSUSED*/
8339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008340f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008341 typval_T *argvars;
8342 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008343{
8344#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008345 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008346#endif
8347}
8348
8349/*
8350 * "diff_hlID()" function
8351 */
8352/*ARGSUSED*/
8353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008354f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008355 typval_T *argvars;
8356 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008357{
8358#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008359 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008360 static linenr_T prev_lnum = 0;
8361 static int changedtick = 0;
8362 static int fnum = 0;
8363 static int change_start = 0;
8364 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008365 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008366 int filler_lines;
8367 int col;
8368
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008369 if (lnum < 0) /* ignore type error in {lnum} arg */
8370 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008371 if (lnum != prev_lnum
8372 || changedtick != curbuf->b_changedtick
8373 || fnum != curbuf->b_fnum)
8374 {
8375 /* New line, buffer, change: need to get the values. */
8376 filler_lines = diff_check(curwin, lnum);
8377 if (filler_lines < 0)
8378 {
8379 if (filler_lines == -1)
8380 {
8381 change_start = MAXCOL;
8382 change_end = -1;
8383 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8384 hlID = HLF_ADD; /* added line */
8385 else
8386 hlID = HLF_CHD; /* changed line */
8387 }
8388 else
8389 hlID = HLF_ADD; /* added line */
8390 }
8391 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008392 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008393 prev_lnum = lnum;
8394 changedtick = curbuf->b_changedtick;
8395 fnum = curbuf->b_fnum;
8396 }
8397
8398 if (hlID == HLF_CHD || hlID == HLF_TXD)
8399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008400 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008401 if (col >= change_start && col <= change_end)
8402 hlID = HLF_TXD; /* changed text */
8403 else
8404 hlID = HLF_CHD; /* changed line */
8405 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008406 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008407#endif
8408}
8409
8410/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008411 * "empty({expr})" function
8412 */
8413 static void
8414f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008415 typval_T *argvars;
8416 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008417{
8418 int n;
8419
8420 switch (argvars[0].v_type)
8421 {
8422 case VAR_STRING:
8423 case VAR_FUNC:
8424 n = argvars[0].vval.v_string == NULL
8425 || *argvars[0].vval.v_string == NUL;
8426 break;
8427 case VAR_NUMBER:
8428 n = argvars[0].vval.v_number == 0;
8429 break;
8430 case VAR_LIST:
8431 n = argvars[0].vval.v_list == NULL
8432 || argvars[0].vval.v_list->lv_first == NULL;
8433 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008434 case VAR_DICT:
8435 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008436 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008437 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008438 default:
8439 EMSG2(_(e_intern2), "f_empty()");
8440 n = 0;
8441 }
8442
8443 rettv->vval.v_number = n;
8444}
8445
8446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 * "escape({string}, {chars})" function
8448 */
8449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008451 typval_T *argvars;
8452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453{
8454 char_u buf[NUMBUFLEN];
8455
Bram Moolenaar758711c2005-02-02 23:11:38 +00008456 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8457 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008458 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459}
8460
8461/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008462 * "eval()" function
8463 */
8464/*ARGSUSED*/
8465 static void
8466f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008467 typval_T *argvars;
8468 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008469{
8470 char_u *s;
8471
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008472 s = get_tv_string_chk(&argvars[0]);
8473 if (s != NULL)
8474 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008476 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8477 {
8478 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008479 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008480 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008481 else if (*s != NUL)
8482 EMSG(_(e_trailing));
8483}
8484
8485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 * "eventhandler()" function
8487 */
8488/*ARGSUSED*/
8489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008490f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008491 typval_T *argvars;
8492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008494 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495}
8496
8497/*
8498 * "executable()" function
8499 */
8500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008501f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008502 typval_T *argvars;
8503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008505 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506}
8507
8508/*
8509 * "exists()" function
8510 */
8511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008512f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008513 typval_T *argvars;
8514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515{
8516 char_u *p;
8517 char_u *name;
8518 int n = FALSE;
8519 int len = 0;
8520
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008521 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 if (*p == '$') /* environment variable */
8523 {
8524 /* first try "normal" environment variables (fast) */
8525 if (mch_getenv(p + 1) != NULL)
8526 n = TRUE;
8527 else
8528 {
8529 /* try expanding things like $VIM and ${HOME} */
8530 p = expand_env_save(p);
8531 if (p != NULL && *p != '$')
8532 n = TRUE;
8533 vim_free(p);
8534 }
8535 }
8536 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008537 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 else if (*p == '*') /* internal or user defined function */
8539 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008540 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 }
8542 else if (*p == ':')
8543 {
8544 n = cmd_exists(p + 1);
8545 }
8546 else if (*p == '#')
8547 {
8548#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008549 if (p[1] == '#')
8550 n = autocmd_supported(p + 2);
8551 else
8552 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553#endif
8554 }
8555 else /* internal variable */
8556 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008557 char_u *tofree;
8558 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008560 /* get_name_len() takes care of expanding curly braces */
8561 name = p;
8562 len = get_name_len(&p, &tofree, TRUE, FALSE);
8563 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008565 if (tofree != NULL)
8566 name = tofree;
8567 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8568 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008570 /* handle d.key, l[idx], f(expr) */
8571 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8572 if (n)
8573 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 }
8575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008577 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 }
8579
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581}
8582
8583/*
8584 * "expand()" function
8585 */
8586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008588 typval_T *argvars;
8589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590{
8591 char_u *s;
8592 int len;
8593 char_u *errormsg;
8594 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8595 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008596 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598 rettv->v_type = VAR_STRING;
8599 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 if (*s == '%' || *s == '#' || *s == '<')
8601 {
8602 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008603 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 --emsg_off;
8605 }
8606 else
8607 {
8608 /* When the optional second argument is non-zero, don't remove matches
8609 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008610 if (argvars[1].v_type != VAR_UNKNOWN
8611 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008613 if (!error)
8614 {
8615 ExpandInit(&xpc);
8616 xpc.xp_context = EXPAND_FILES;
8617 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8618 ExpandCleanup(&xpc);
8619 }
8620 else
8621 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 }
8623}
8624
8625/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008626 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008627 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008628 */
8629 static void
8630f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008631 typval_T *argvars;
8632 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008633{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008634 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008635 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008636 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008637 list_T *l1, *l2;
8638 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008639 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008640 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008641
Bram Moolenaare9a41262005-01-15 22:18:47 +00008642 l1 = argvars[0].vval.v_list;
8643 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008644 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8645 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008646 {
8647 if (argvars[2].v_type != VAR_UNKNOWN)
8648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008649 before = get_tv_number_chk(&argvars[2], &error);
8650 if (error)
8651 return; /* type error; errmsg already given */
8652
Bram Moolenaar758711c2005-02-02 23:11:38 +00008653 if (before == l1->lv_len)
8654 item = NULL;
8655 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008656 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008657 item = list_find(l1, before);
8658 if (item == NULL)
8659 {
8660 EMSGN(_(e_listidx), before);
8661 return;
8662 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008663 }
8664 }
8665 else
8666 item = NULL;
8667 list_extend(l1, l2, item);
8668
Bram Moolenaare9a41262005-01-15 22:18:47 +00008669 copy_tv(&argvars[0], rettv);
8670 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008671 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008672 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8673 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008674 dict_T *d1, *d2;
8675 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008676 char_u *action;
8677 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008678 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008679 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008680
8681 d1 = argvars[0].vval.v_dict;
8682 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008683 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8684 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008685 {
8686 /* Check the third argument. */
8687 if (argvars[2].v_type != VAR_UNKNOWN)
8688 {
8689 static char *(av[]) = {"keep", "force", "error"};
8690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008691 action = get_tv_string_chk(&argvars[2]);
8692 if (action == NULL)
8693 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008694 for (i = 0; i < 3; ++i)
8695 if (STRCMP(action, av[i]) == 0)
8696 break;
8697 if (i == 3)
8698 {
8699 EMSGN(_(e_invarg2), action);
8700 return;
8701 }
8702 }
8703 else
8704 action = (char_u *)"force";
8705
8706 /* Go over all entries in the second dict and add them to the
8707 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008708 todo = d2->dv_hashtab.ht_used;
8709 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008710 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008711 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008712 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008713 --todo;
8714 di1 = dict_find(d1, hi2->hi_key, -1);
8715 if (di1 == NULL)
8716 {
8717 di1 = dictitem_copy(HI2DI(hi2));
8718 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8719 dictitem_free(di1);
8720 }
8721 else if (*action == 'e')
8722 {
8723 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8724 break;
8725 }
8726 else if (*action == 'f')
8727 {
8728 clear_tv(&di1->di_tv);
8729 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8730 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008731 }
8732 }
8733
Bram Moolenaare9a41262005-01-15 22:18:47 +00008734 copy_tv(&argvars[0], rettv);
8735 }
8736 }
8737 else
8738 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008739}
8740
8741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 * "filereadable()" function
8743 */
8744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008745f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008746 typval_T *argvars;
8747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
8749 FILE *fd;
8750 char_u *p;
8751 int n;
8752
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8755 {
8756 n = TRUE;
8757 fclose(fd);
8758 }
8759 else
8760 n = FALSE;
8761
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763}
8764
8765/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008766 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 * rights to write into.
8768 */
8769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *argvars;
8772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008774 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775}
8776
Bram Moolenaar33570922005-01-25 22:26:29 +00008777static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008778
8779 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008780findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008781 typval_T *argvars;
8782 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008783 int dir;
8784{
8785#ifdef FEAT_SEARCHPATH
8786 char_u *fname;
8787 char_u *fresult = NULL;
8788 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8789 char_u *p;
8790 char_u pathbuf[NUMBUFLEN];
8791 int count = 1;
8792 int first = TRUE;
8793
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008795
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008796 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008798 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8799 if (p == NULL)
8800 count = -1; /* error */
8801 else
8802 {
8803 if (*p != NUL)
8804 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008806 if (argvars[2].v_type != VAR_UNKNOWN)
8807 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8808 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008809 }
8810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008811 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008813 do
8814 {
8815 vim_free(fresult);
8816 fresult = find_file_in_path_option(first ? fname : NULL,
8817 first ? (int)STRLEN(fname) : 0,
8818 0, first, path, dir, NULL);
8819 first = FALSE;
8820 } while (--count > 0 && fresult != NULL);
8821 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008822
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008824#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008826#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008827 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008828}
8829
Bram Moolenaar33570922005-01-25 22:26:29 +00008830static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8831static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008832
8833/*
8834 * Implementation of map() and filter().
8835 */
8836 static void
8837filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008838 typval_T *argvars;
8839 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008840 int map;
8841{
8842 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008843 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008844 listitem_T *li, *nli;
8845 list_T *l = NULL;
8846 dictitem_T *di;
8847 hashtab_T *ht;
8848 hashitem_T *hi;
8849 dict_T *d = NULL;
8850 typval_T save_val;
8851 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008852 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008853 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008854 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar280f1262006-01-30 00:14:18 +00008855 int save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008856
8857 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008858 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008859 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008860 if ((l = argvars[0].vval.v_list) == NULL
8861 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008862 return;
8863 }
8864 else if (argvars[0].v_type == VAR_DICT)
8865 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008866 if ((d = argvars[0].vval.v_dict) == NULL
8867 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008868 return;
8869 }
8870 else
8871 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008872 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008873 return;
8874 }
8875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008876 expr = get_tv_string_buf_chk(&argvars[1], buf);
8877 /* On type errors, the preceding call has already displayed an error
8878 * message. Avoid a misleading error message for an empty string that
8879 * was not passed as argument. */
8880 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008882 prepare_vimvar(VV_VAL, &save_val);
8883 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008884
Bram Moolenaar280f1262006-01-30 00:14:18 +00008885 /* We reset "called_emsg" to be able to detect whether an error
8886 * occurred during evaluation of the expression. "did_emsg" can't be
8887 * used, because it is reset when calling a function. */
8888 save_called_emsg = called_emsg;
8889 called_emsg = FALSE;
8890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008891 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008892 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008893 prepare_vimvar(VV_KEY, &save_key);
8894 vimvars[VV_KEY].vv_type = VAR_STRING;
8895
8896 ht = &d->dv_hashtab;
8897 hash_lock(ht);
8898 todo = ht->ht_used;
8899 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008900 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008901 if (!HASHITEM_EMPTY(hi))
8902 {
8903 --todo;
8904 di = HI2DI(hi);
8905 if (tv_check_lock(di->di_tv.v_lock, msg))
8906 break;
8907 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008908 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
8909 || called_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 break;
8911 if (!map && rem)
8912 dictitem_remove(d, di);
8913 clear_tv(&vimvars[VV_KEY].vv_tv);
8914 }
8915 }
8916 hash_unlock(ht);
8917
8918 restore_vimvar(VV_KEY, &save_key);
8919 }
8920 else
8921 {
8922 for (li = l->lv_first; li != NULL; li = nli)
8923 {
8924 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008925 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008926 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008927 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
8928 || called_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008929 break;
8930 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008931 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008932 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008933 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008935 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008936
8937 called_emsg |= save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008938 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008939
8940 copy_tv(&argvars[0], rettv);
8941}
8942
8943 static int
8944filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008945 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008946 char_u *expr;
8947 int map;
8948 int *remp;
8949{
Bram Moolenaar33570922005-01-25 22:26:29 +00008950 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008951 char_u *s;
8952
Bram Moolenaar33570922005-01-25 22:26:29 +00008953 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008954 s = expr;
8955 if (eval1(&s, &rettv, TRUE) == FAIL)
8956 return FAIL;
8957 if (*s != NUL) /* check for trailing chars after expr */
8958 {
8959 EMSG2(_(e_invexpr2), s);
8960 return FAIL;
8961 }
8962 if (map)
8963 {
8964 /* map(): replace the list item value */
8965 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008966 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008967 *tv = rettv;
8968 }
8969 else
8970 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008971 int error = FALSE;
8972
Bram Moolenaare9a41262005-01-15 22:18:47 +00008973 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008974 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008975 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008976 /* On type error, nothing has been removed; return FAIL to stop the
8977 * loop. The error message was given by get_tv_number_chk(). */
8978 if (error)
8979 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008980 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008981 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008982 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008983}
8984
8985/*
8986 * "filter()" function
8987 */
8988 static void
8989f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008990 typval_T *argvars;
8991 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008992{
8993 filter_map(argvars, rettv, FALSE);
8994}
8995
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008996/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008997 * "finddir({fname}[, {path}[, {count}]])" function
8998 */
8999 static void
9000f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 typval_T *argvars;
9002 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009003{
9004 findfilendir(argvars, rettv, TRUE);
9005}
9006
9007/*
9008 * "findfile({fname}[, {path}[, {count}]])" function
9009 */
9010 static void
9011f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009012 typval_T *argvars;
9013 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009014{
9015 findfilendir(argvars, rettv, FALSE);
9016}
9017
9018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 * "fnamemodify({fname}, {mods})" function
9020 */
9021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009022f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009023 typval_T *argvars;
9024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025{
9026 char_u *fname;
9027 char_u *mods;
9028 int usedlen = 0;
9029 int len;
9030 char_u *fbuf = NULL;
9031 char_u buf[NUMBUFLEN];
9032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009033 fname = get_tv_string_chk(&argvars[0]);
9034 mods = get_tv_string_buf_chk(&argvars[1], buf);
9035 if (fname == NULL || mods == NULL)
9036 fname = NULL;
9037 else
9038 {
9039 len = (int)STRLEN(fname);
9040 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009043 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009047 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 vim_free(fbuf);
9049}
9050
Bram Moolenaar33570922005-01-25 22:26:29 +00009051static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052
9053/*
9054 * "foldclosed()" function
9055 */
9056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009058 typval_T *argvars;
9059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 int end;
9061{
9062#ifdef FEAT_FOLDING
9063 linenr_T lnum;
9064 linenr_T first, last;
9065
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009066 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9068 {
9069 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9070 {
9071 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009074 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 return;
9076 }
9077 }
9078#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009079 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080}
9081
9082/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009083 * "foldclosed()" function
9084 */
9085 static void
9086f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009087 typval_T *argvars;
9088 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009089{
9090 foldclosed_both(argvars, rettv, FALSE);
9091}
9092
9093/*
9094 * "foldclosedend()" function
9095 */
9096 static void
9097f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009098 typval_T *argvars;
9099 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009100{
9101 foldclosed_both(argvars, rettv, TRUE);
9102}
9103
9104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 * "foldlevel()" function
9106 */
9107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009109 typval_T *argvars;
9110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
9112#ifdef FEAT_FOLDING
9113 linenr_T lnum;
9114
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009117 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 else
9119#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121}
9122
9123/*
9124 * "foldtext()" function
9125 */
9126/*ARGSUSED*/
9127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009128f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009129 typval_T *argvars;
9130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131{
9132#ifdef FEAT_FOLDING
9133 linenr_T lnum;
9134 char_u *s;
9135 char_u *r;
9136 int len;
9137 char *txt;
9138#endif
9139
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140 rettv->v_type = VAR_STRING;
9141 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009143 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9144 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9145 <= curbuf->b_ml.ml_line_count
9146 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 {
9148 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009149 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9150 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 {
9152 if (!linewhite(lnum))
9153 break;
9154 ++lnum;
9155 }
9156
9157 /* Find interesting text in this line. */
9158 s = skipwhite(ml_get(lnum));
9159 /* skip C comment-start */
9160 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009161 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009163 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009164 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009165 {
9166 s = skipwhite(ml_get(lnum + 1));
9167 if (*s == '*')
9168 s = skipwhite(s + 1);
9169 }
9170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 txt = _("+-%s%3ld lines: ");
9172 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009173 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 + 20 /* for %3ld */
9175 + STRLEN(s))); /* concatenated */
9176 if (r != NULL)
9177 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009178 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9179 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9180 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 len = (int)STRLEN(r);
9182 STRCAT(r, s);
9183 /* remove 'foldmarker' and 'commentstring' */
9184 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009185 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 }
9187 }
9188#endif
9189}
9190
9191/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009192 * "foldtextresult(lnum)" function
9193 */
9194/*ARGSUSED*/
9195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009197 typval_T *argvars;
9198 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009199{
9200#ifdef FEAT_FOLDING
9201 linenr_T lnum;
9202 char_u *text;
9203 char_u buf[51];
9204 foldinfo_T foldinfo;
9205 int fold_count;
9206#endif
9207
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009208 rettv->v_type = VAR_STRING;
9209 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009210#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009211 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009212 /* treat illegal types and illegal string values for {lnum} the same */
9213 if (lnum < 0)
9214 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009215 fold_count = foldedCount(curwin, lnum, &foldinfo);
9216 if (fold_count > 0)
9217 {
9218 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9219 &foldinfo, buf);
9220 if (text == buf)
9221 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009222 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009223 }
9224#endif
9225}
9226
9227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 * "foreground()" function
9229 */
9230/*ARGSUSED*/
9231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009232f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009233 typval_T *argvars;
9234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009236 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237#ifdef FEAT_GUI
9238 if (gui.in_use)
9239 gui_mch_set_foreground();
9240#else
9241# ifdef WIN32
9242 win32_set_foreground();
9243# endif
9244#endif
9245}
9246
9247/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009248 * "function()" function
9249 */
9250/*ARGSUSED*/
9251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009252f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009253 typval_T *argvars;
9254 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009255{
9256 char_u *s;
9257
Bram Moolenaara7043832005-01-21 11:56:39 +00009258 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009259 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009260 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009261 EMSG2(_(e_invarg2), s);
9262 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009263 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009264 else
9265 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009266 rettv->vval.v_string = vim_strsave(s);
9267 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009268 }
9269}
9270
9271/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009272 * "garbagecollect()" function
9273 */
9274/*ARGSUSED*/
9275 static void
9276f_garbagecollect(argvars, rettv)
9277 typval_T *argvars;
9278 typval_T *rettv;
9279{
9280 garbage_collect();
9281}
9282
9283/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009284 * "get()" function
9285 */
9286 static void
9287f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009288 typval_T *argvars;
9289 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009290{
Bram Moolenaar33570922005-01-25 22:26:29 +00009291 listitem_T *li;
9292 list_T *l;
9293 dictitem_T *di;
9294 dict_T *d;
9295 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009296
Bram Moolenaare9a41262005-01-15 22:18:47 +00009297 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009298 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009299 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009301 int error = FALSE;
9302
9303 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9304 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009305 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009306 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009307 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009308 else if (argvars[0].v_type == VAR_DICT)
9309 {
9310 if ((d = argvars[0].vval.v_dict) != NULL)
9311 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009312 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009313 if (di != NULL)
9314 tv = &di->di_tv;
9315 }
9316 }
9317 else
9318 EMSG2(_(e_listdictarg), "get()");
9319
9320 if (tv == NULL)
9321 {
9322 if (argvars[2].v_type == VAR_UNKNOWN)
9323 rettv->vval.v_number = 0;
9324 else
9325 copy_tv(&argvars[2], rettv);
9326 }
9327 else
9328 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009329}
9330
Bram Moolenaar342337a2005-07-21 21:11:17 +00009331static 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 +00009332
9333/*
9334 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009335 * Return a range (from start to end) of lines in rettv from the specified
9336 * buffer.
9337 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009338 */
9339 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009340get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009341 buf_T *buf;
9342 linenr_T start;
9343 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009344 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009345 typval_T *rettv;
9346{
9347 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009348 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009349
Bram Moolenaar342337a2005-07-21 21:11:17 +00009350 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009351 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009352 l = list_alloc();
9353 if (l == NULL)
9354 return;
9355
9356 rettv->vval.v_list = l;
9357 rettv->v_type = VAR_LIST;
9358 ++l->lv_refcount;
9359 }
9360 else
9361 rettv->vval.v_number = 0;
9362
9363 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9364 return;
9365
9366 if (!retlist)
9367 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009368 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9369 p = ml_get_buf(buf, start, FALSE);
9370 else
9371 p = (char_u *)"";
9372
9373 rettv->v_type = VAR_STRING;
9374 rettv->vval.v_string = vim_strsave(p);
9375 }
9376 else
9377 {
9378 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009379 return;
9380
9381 if (start < 1)
9382 start = 1;
9383 if (end > buf->b_ml.ml_line_count)
9384 end = buf->b_ml.ml_line_count;
9385 while (start <= end)
Bram Moolenaar4463f292005-09-25 22:20:24 +00009386 if (list_append_string(l, ml_get_buf(buf, start++, FALSE), -1)
9387 == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009388 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009389 }
9390}
9391
9392/*
9393 * "getbufline()" function
9394 */
9395 static void
9396f_getbufline(argvars, rettv)
9397 typval_T *argvars;
9398 typval_T *rettv;
9399{
9400 linenr_T lnum;
9401 linenr_T end;
9402 buf_T *buf;
9403
9404 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9405 ++emsg_off;
9406 buf = get_buf_tv(&argvars[0]);
9407 --emsg_off;
9408
Bram Moolenaar661b1822005-07-28 22:36:45 +00009409 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009410 if (argvars[2].v_type == VAR_UNKNOWN)
9411 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009412 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009413 end = get_tv_lnum_buf(&argvars[2], buf);
9414
Bram Moolenaar342337a2005-07-21 21:11:17 +00009415 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009416}
9417
Bram Moolenaar0d660222005-01-07 21:51:51 +00009418/*
9419 * "getbufvar()" function
9420 */
9421 static void
9422f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009423 typval_T *argvars;
9424 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009425{
9426 buf_T *buf;
9427 buf_T *save_curbuf;
9428 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009429 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009431 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9432 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009433 ++emsg_off;
9434 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009435
9436 rettv->v_type = VAR_STRING;
9437 rettv->vval.v_string = NULL;
9438
9439 if (buf != NULL && varname != NULL)
9440 {
9441 if (*varname == '&') /* buffer-local-option */
9442 {
9443 /* set curbuf to be our buf, temporarily */
9444 save_curbuf = curbuf;
9445 curbuf = buf;
9446
9447 get_option_tv(&varname, rettv, TRUE);
9448
9449 /* restore previous notion of curbuf */
9450 curbuf = save_curbuf;
9451 }
9452 else
9453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009454 if (*varname == NUL)
9455 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9456 * scope prefix before the NUL byte is required by
9457 * find_var_in_ht(). */
9458 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009459 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009460 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009461 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009462 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009463 }
9464 }
9465
9466 --emsg_off;
9467}
9468
9469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 * "getchar()" function
9471 */
9472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009473f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009474 typval_T *argvars;
9475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476{
9477 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009478 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479
9480 ++no_mapping;
9481 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009482 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 /* getchar(): blocking wait. */
9484 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009485 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 /* getchar(1): only check if char avail */
9487 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009488 else if (error || vpeekc() == NUL)
9489 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 n = 0;
9491 else
9492 /* getchar(0) and char avail: return char */
9493 n = safe_vgetc();
9494 --no_mapping;
9495 --allow_keys;
9496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 if (IS_SPECIAL(n) || mod_mask != 0)
9499 {
9500 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9501 int i = 0;
9502
9503 /* Turn a special key into three bytes, plus modifier. */
9504 if (mod_mask != 0)
9505 {
9506 temp[i++] = K_SPECIAL;
9507 temp[i++] = KS_MODIFIER;
9508 temp[i++] = mod_mask;
9509 }
9510 if (IS_SPECIAL(n))
9511 {
9512 temp[i++] = K_SPECIAL;
9513 temp[i++] = K_SECOND(n);
9514 temp[i++] = K_THIRD(n);
9515 }
9516#ifdef FEAT_MBYTE
9517 else if (has_mbyte)
9518 i += (*mb_char2bytes)(n, temp + i);
9519#endif
9520 else
9521 temp[i++] = n;
9522 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523 rettv->v_type = VAR_STRING;
9524 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 }
9526}
9527
9528/*
9529 * "getcharmod()" function
9530 */
9531/*ARGSUSED*/
9532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009534 typval_T *argvars;
9535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009537 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538}
9539
9540/*
9541 * "getcmdline()" function
9542 */
9543/*ARGSUSED*/
9544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009546 typval_T *argvars;
9547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549 rettv->v_type = VAR_STRING;
9550 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551}
9552
9553/*
9554 * "getcmdpos()" function
9555 */
9556/*ARGSUSED*/
9557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009559 typval_T *argvars;
9560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563}
9564
9565/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009566 * "getcmdtype()" function
9567 */
9568/*ARGSUSED*/
9569 static void
9570f_getcmdtype(argvars, rettv)
9571 typval_T *argvars;
9572 typval_T *rettv;
9573{
9574 rettv->v_type = VAR_STRING;
9575 rettv->vval.v_string = alloc(2);
9576 if (rettv->vval.v_string != NULL)
9577 {
9578 rettv->vval.v_string[0] = get_cmdline_type();
9579 rettv->vval.v_string[1] = NUL;
9580 }
9581}
9582
9583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 * "getcwd()" function
9585 */
9586/*ARGSUSED*/
9587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009588f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009589 typval_T *argvars;
9590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591{
9592 char_u cwd[MAXPATHL];
9593
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 else
9598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009599 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009601 if (rettv->vval.v_string != NULL)
9602 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603#endif
9604 }
9605}
9606
9607/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009608 * "getfontname()" function
9609 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009610/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009613 typval_T *argvars;
9614 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009615{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 rettv->v_type = VAR_STRING;
9617 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009618#ifdef FEAT_GUI
9619 if (gui.in_use)
9620 {
9621 GuiFont font;
9622 char_u *name = NULL;
9623
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009624 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009625 {
9626 /* Get the "Normal" font. Either the name saved by
9627 * hl_set_font_name() or from the font ID. */
9628 font = gui.norm_font;
9629 name = hl_get_font_name();
9630 }
9631 else
9632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009633 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009634 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9635 return;
9636 font = gui_mch_get_font(name, FALSE);
9637 if (font == NOFONT)
9638 return; /* Invalid font name, return empty string. */
9639 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009641 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009642 gui_mch_free_font(font);
9643 }
9644#endif
9645}
9646
9647/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009648 * "getfperm({fname})" function
9649 */
9650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009652 typval_T *argvars;
9653 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009654{
9655 char_u *fname;
9656 struct stat st;
9657 char_u *perm = NULL;
9658 char_u flags[] = "rwx";
9659 int i;
9660
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009662
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009663 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009664 if (mch_stat((char *)fname, &st) >= 0)
9665 {
9666 perm = vim_strsave((char_u *)"---------");
9667 if (perm != NULL)
9668 {
9669 for (i = 0; i < 9; i++)
9670 {
9671 if (st.st_mode & (1 << (8 - i)))
9672 perm[i] = flags[i % 3];
9673 }
9674 }
9675 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009677}
9678
9679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 * "getfsize({fname})" function
9681 */
9682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009684 typval_T *argvars;
9685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686{
9687 char_u *fname;
9688 struct stat st;
9689
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009690 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693
9694 if (mch_stat((char *)fname, &st) >= 0)
9695 {
9696 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 }
9701 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703}
9704
9705/*
9706 * "getftime({fname})" function
9707 */
9708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009710 typval_T *argvars;
9711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712{
9713 char_u *fname;
9714 struct stat st;
9715
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717
9718 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722}
9723
9724/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009725 * "getftype({fname})" function
9726 */
9727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009728f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009729 typval_T *argvars;
9730 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009731{
9732 char_u *fname;
9733 struct stat st;
9734 char_u *type = NULL;
9735 char *t;
9736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009738
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009739 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009740 if (mch_lstat((char *)fname, &st) >= 0)
9741 {
9742#ifdef S_ISREG
9743 if (S_ISREG(st.st_mode))
9744 t = "file";
9745 else if (S_ISDIR(st.st_mode))
9746 t = "dir";
9747# ifdef S_ISLNK
9748 else if (S_ISLNK(st.st_mode))
9749 t = "link";
9750# endif
9751# ifdef S_ISBLK
9752 else if (S_ISBLK(st.st_mode))
9753 t = "bdev";
9754# endif
9755# ifdef S_ISCHR
9756 else if (S_ISCHR(st.st_mode))
9757 t = "cdev";
9758# endif
9759# ifdef S_ISFIFO
9760 else if (S_ISFIFO(st.st_mode))
9761 t = "fifo";
9762# endif
9763# ifdef S_ISSOCK
9764 else if (S_ISSOCK(st.st_mode))
9765 t = "fifo";
9766# endif
9767 else
9768 t = "other";
9769#else
9770# ifdef S_IFMT
9771 switch (st.st_mode & S_IFMT)
9772 {
9773 case S_IFREG: t = "file"; break;
9774 case S_IFDIR: t = "dir"; break;
9775# ifdef S_IFLNK
9776 case S_IFLNK: t = "link"; break;
9777# endif
9778# ifdef S_IFBLK
9779 case S_IFBLK: t = "bdev"; break;
9780# endif
9781# ifdef S_IFCHR
9782 case S_IFCHR: t = "cdev"; break;
9783# endif
9784# ifdef S_IFIFO
9785 case S_IFIFO: t = "fifo"; break;
9786# endif
9787# ifdef S_IFSOCK
9788 case S_IFSOCK: t = "socket"; break;
9789# endif
9790 default: t = "other";
9791 }
9792# else
9793 if (mch_isdir(fname))
9794 t = "dir";
9795 else
9796 t = "file";
9797# endif
9798#endif
9799 type = vim_strsave((char_u *)t);
9800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009802}
9803
9804/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009805 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009806 */
9807 static void
9808f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009809 typval_T *argvars;
9810 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009811{
9812 linenr_T lnum;
9813 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009814 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009815
9816 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009817 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009818 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009819 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009820 retlist = FALSE;
9821 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009822 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009823 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009824 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009825 retlist = TRUE;
9826 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009827
Bram Moolenaar342337a2005-07-21 21:11:17 +00009828 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009829}
9830
9831/*
Bram Moolenaar280f1262006-01-30 00:14:18 +00009832 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +00009833 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00009834/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +00009835 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +00009836f_getqflist(argvars, rettv)
9837 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009838 typval_T *rettv;
9839{
9840#ifdef FEAT_QUICKFIX
9841 list_T *l;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009842 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009843#endif
9844
9845 rettv->vval.v_number = FALSE;
9846#ifdef FEAT_QUICKFIX
9847 l = list_alloc();
9848 if (l != NULL)
9849 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009850 rettv->vval.v_list = l;
9851 rettv->v_type = VAR_LIST;
9852 ++l->lv_refcount;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009853 wp = NULL;
9854 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
9855 {
9856 wp = find_win_by_nr(&argvars[0]);
9857 if (wp == NULL)
9858 return;
9859 }
9860
Bram Moolenaar17c7c012006-01-26 22:25:15 +00009861 (void)get_errorlist(wp, l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009862 }
9863#endif
9864}
9865
9866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 * "getreg()" function
9868 */
9869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009871 typval_T *argvars;
9872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873{
9874 char_u *strregname;
9875 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009876 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009877 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009879 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009881 strregname = get_tv_string_chk(&argvars[0]);
9882 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009883 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009884 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009887 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 regname = (strregname == NULL ? '"' : *strregname);
9889 if (regname == 0)
9890 regname = '"';
9891
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009892 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009893 rettv->vval.v_string = error ? NULL :
9894 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895}
9896
9897/*
9898 * "getregtype()" function
9899 */
9900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009902 typval_T *argvars;
9903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904{
9905 char_u *strregname;
9906 int regname;
9907 char_u buf[NUMBUFLEN + 2];
9908 long reglen = 0;
9909
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009910 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009911 {
9912 strregname = get_tv_string_chk(&argvars[0]);
9913 if (strregname == NULL) /* type error; errmsg already given */
9914 {
9915 rettv->v_type = VAR_STRING;
9916 rettv->vval.v_string = NULL;
9917 return;
9918 }
9919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 else
9921 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009922 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923
9924 regname = (strregname == NULL ? '"' : *strregname);
9925 if (regname == 0)
9926 regname = '"';
9927
9928 buf[0] = NUL;
9929 buf[1] = NUL;
9930 switch (get_reg_type(regname, &reglen))
9931 {
9932 case MLINE: buf[0] = 'V'; break;
9933 case MCHAR: buf[0] = 'v'; break;
9934#ifdef FEAT_VISUAL
9935 case MBLOCK:
9936 buf[0] = Ctrl_V;
9937 sprintf((char *)buf + 1, "%ld", reglen + 1);
9938 break;
9939#endif
9940 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009941 rettv->v_type = VAR_STRING;
9942 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943}
9944
9945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 * "getwinposx()" function
9947 */
9948/*ARGSUSED*/
9949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009950f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009951 typval_T *argvars;
9952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009954 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955#ifdef FEAT_GUI
9956 if (gui.in_use)
9957 {
9958 int x, y;
9959
9960 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009961 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 }
9963#endif
9964}
9965
9966/*
9967 * "getwinposy()" function
9968 */
9969/*ARGSUSED*/
9970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009972 typval_T *argvars;
9973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976#ifdef FEAT_GUI
9977 if (gui.in_use)
9978 {
9979 int x, y;
9980
9981 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 }
9984#endif
9985}
9986
Bram Moolenaara40058a2005-07-11 22:42:07 +00009987 static win_T *
9988find_win_by_nr(vp)
9989 typval_T *vp;
9990{
9991#ifdef FEAT_WINDOWS
9992 win_T *wp;
9993#endif
9994 int nr;
9995
9996 nr = get_tv_number_chk(vp, NULL);
9997
9998#ifdef FEAT_WINDOWS
9999 if (nr < 0)
10000 return NULL;
10001 if (nr == 0)
10002 return curwin;
10003
10004 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10005 if (--nr <= 0)
10006 break;
10007 return wp;
10008#else
10009 if (nr == 0 || nr == 1)
10010 return curwin;
10011 return NULL;
10012#endif
10013}
10014
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015/*
10016 * "getwinvar()" function
10017 */
10018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010019f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010020 typval_T *argvars;
10021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022{
10023 win_T *win, *oldcurwin;
10024 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010025 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010028 varname = get_tv_string_chk(&argvars[1]);
10029 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031 rettv->v_type = VAR_STRING;
10032 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033
10034 if (win != NULL && varname != NULL)
10035 {
10036 if (*varname == '&') /* window-local-option */
10037 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010038 /* Set curwin to be our win, temporarily. Also set curbuf, so
10039 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 oldcurwin = curwin;
10041 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010042 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045
10046 /* restore previous notion of curwin */
10047 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010048 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 }
10050 else
10051 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010052 if (*varname == NUL)
10053 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10054 * scope prefix before the NUL byte is required by
10055 * find_var_in_ht(). */
10056 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010058 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010060 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 }
10062 }
10063
10064 --emsg_off;
10065}
10066
10067/*
10068 * "glob()" function
10069 */
10070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010072 typval_T *argvars;
10073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074{
10075 expand_T xpc;
10076
10077 ExpandInit(&xpc);
10078 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079 rettv->v_type = VAR_STRING;
10080 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10082 ExpandCleanup(&xpc);
10083}
10084
10085/*
10086 * "globpath()" function
10087 */
10088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010089f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010090 typval_T *argvars;
10091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092{
10093 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010094 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010096 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010097 if (file == NULL)
10098 rettv->vval.v_string = NULL;
10099 else
10100 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101}
10102
10103/*
10104 * "has()" function
10105 */
10106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010108 typval_T *argvars;
10109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110{
10111 int i;
10112 char_u *name;
10113 int n = FALSE;
10114 static char *(has_list[]) =
10115 {
10116#ifdef AMIGA
10117 "amiga",
10118# ifdef FEAT_ARP
10119 "arp",
10120# endif
10121#endif
10122#ifdef __BEOS__
10123 "beos",
10124#endif
10125#ifdef MSDOS
10126# ifdef DJGPP
10127 "dos32",
10128# else
10129 "dos16",
10130# endif
10131#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010132#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 "mac",
10134#endif
10135#if defined(MACOS_X_UNIX)
10136 "macunix",
10137#endif
10138#ifdef OS2
10139 "os2",
10140#endif
10141#ifdef __QNX__
10142 "qnx",
10143#endif
10144#ifdef RISCOS
10145 "riscos",
10146#endif
10147#ifdef UNIX
10148 "unix",
10149#endif
10150#ifdef VMS
10151 "vms",
10152#endif
10153#ifdef WIN16
10154 "win16",
10155#endif
10156#ifdef WIN32
10157 "win32",
10158#endif
10159#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10160 "win32unix",
10161#endif
10162#ifdef WIN64
10163 "win64",
10164#endif
10165#ifdef EBCDIC
10166 "ebcdic",
10167#endif
10168#ifndef CASE_INSENSITIVE_FILENAME
10169 "fname_case",
10170#endif
10171#ifdef FEAT_ARABIC
10172 "arabic",
10173#endif
10174#ifdef FEAT_AUTOCMD
10175 "autocmd",
10176#endif
10177#ifdef FEAT_BEVAL
10178 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010179# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10180 "balloon_multiline",
10181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182#endif
10183#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10184 "builtin_terms",
10185# ifdef ALL_BUILTIN_TCAPS
10186 "all_builtin_terms",
10187# endif
10188#endif
10189#ifdef FEAT_BYTEOFF
10190 "byte_offset",
10191#endif
10192#ifdef FEAT_CINDENT
10193 "cindent",
10194#endif
10195#ifdef FEAT_CLIENTSERVER
10196 "clientserver",
10197#endif
10198#ifdef FEAT_CLIPBOARD
10199 "clipboard",
10200#endif
10201#ifdef FEAT_CMDL_COMPL
10202 "cmdline_compl",
10203#endif
10204#ifdef FEAT_CMDHIST
10205 "cmdline_hist",
10206#endif
10207#ifdef FEAT_COMMENTS
10208 "comments",
10209#endif
10210#ifdef FEAT_CRYPT
10211 "cryptv",
10212#endif
10213#ifdef FEAT_CSCOPE
10214 "cscope",
10215#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010216#ifdef CURSOR_SHAPE
10217 "cursorshape",
10218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219#ifdef DEBUG
10220 "debug",
10221#endif
10222#ifdef FEAT_CON_DIALOG
10223 "dialog_con",
10224#endif
10225#ifdef FEAT_GUI_DIALOG
10226 "dialog_gui",
10227#endif
10228#ifdef FEAT_DIFF
10229 "diff",
10230#endif
10231#ifdef FEAT_DIGRAPHS
10232 "digraphs",
10233#endif
10234#ifdef FEAT_DND
10235 "dnd",
10236#endif
10237#ifdef FEAT_EMACS_TAGS
10238 "emacs_tags",
10239#endif
10240 "eval", /* always present, of course! */
10241#ifdef FEAT_EX_EXTRA
10242 "ex_extra",
10243#endif
10244#ifdef FEAT_SEARCH_EXTRA
10245 "extra_search",
10246#endif
10247#ifdef FEAT_FKMAP
10248 "farsi",
10249#endif
10250#ifdef FEAT_SEARCHPATH
10251 "file_in_path",
10252#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010253#if defined(UNIX) && !defined(USE_SYSTEM)
10254 "filterpipe",
10255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256#ifdef FEAT_FIND_ID
10257 "find_in_path",
10258#endif
10259#ifdef FEAT_FOLDING
10260 "folding",
10261#endif
10262#ifdef FEAT_FOOTER
10263 "footer",
10264#endif
10265#if !defined(USE_SYSTEM) && defined(UNIX)
10266 "fork",
10267#endif
10268#ifdef FEAT_GETTEXT
10269 "gettext",
10270#endif
10271#ifdef FEAT_GUI
10272 "gui",
10273#endif
10274#ifdef FEAT_GUI_ATHENA
10275# ifdef FEAT_GUI_NEXTAW
10276 "gui_neXtaw",
10277# else
10278 "gui_athena",
10279# endif
10280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281#ifdef FEAT_GUI_GTK
10282 "gui_gtk",
10283# ifdef HAVE_GTK2
10284 "gui_gtk2",
10285# endif
10286#endif
10287#ifdef FEAT_GUI_MAC
10288 "gui_mac",
10289#endif
10290#ifdef FEAT_GUI_MOTIF
10291 "gui_motif",
10292#endif
10293#ifdef FEAT_GUI_PHOTON
10294 "gui_photon",
10295#endif
10296#ifdef FEAT_GUI_W16
10297 "gui_win16",
10298#endif
10299#ifdef FEAT_GUI_W32
10300 "gui_win32",
10301#endif
10302#ifdef FEAT_HANGULIN
10303 "hangul_input",
10304#endif
10305#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10306 "iconv",
10307#endif
10308#ifdef FEAT_INS_EXPAND
10309 "insert_expand",
10310#endif
10311#ifdef FEAT_JUMPLIST
10312 "jumplist",
10313#endif
10314#ifdef FEAT_KEYMAP
10315 "keymap",
10316#endif
10317#ifdef FEAT_LANGMAP
10318 "langmap",
10319#endif
10320#ifdef FEAT_LIBCALL
10321 "libcall",
10322#endif
10323#ifdef FEAT_LINEBREAK
10324 "linebreak",
10325#endif
10326#ifdef FEAT_LISP
10327 "lispindent",
10328#endif
10329#ifdef FEAT_LISTCMDS
10330 "listcmds",
10331#endif
10332#ifdef FEAT_LOCALMAP
10333 "localmap",
10334#endif
10335#ifdef FEAT_MENU
10336 "menu",
10337#endif
10338#ifdef FEAT_SESSION
10339 "mksession",
10340#endif
10341#ifdef FEAT_MODIFY_FNAME
10342 "modify_fname",
10343#endif
10344#ifdef FEAT_MOUSE
10345 "mouse",
10346#endif
10347#ifdef FEAT_MOUSESHAPE
10348 "mouseshape",
10349#endif
10350#if defined(UNIX) || defined(VMS)
10351# ifdef FEAT_MOUSE_DEC
10352 "mouse_dec",
10353# endif
10354# ifdef FEAT_MOUSE_GPM
10355 "mouse_gpm",
10356# endif
10357# ifdef FEAT_MOUSE_JSB
10358 "mouse_jsbterm",
10359# endif
10360# ifdef FEAT_MOUSE_NET
10361 "mouse_netterm",
10362# endif
10363# ifdef FEAT_MOUSE_PTERM
10364 "mouse_pterm",
10365# endif
10366# ifdef FEAT_MOUSE_XTERM
10367 "mouse_xterm",
10368# endif
10369#endif
10370#ifdef FEAT_MBYTE
10371 "multi_byte",
10372#endif
10373#ifdef FEAT_MBYTE_IME
10374 "multi_byte_ime",
10375#endif
10376#ifdef FEAT_MULTI_LANG
10377 "multi_lang",
10378#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010379#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010380#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010381 "mzscheme",
10382#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384#ifdef FEAT_OLE
10385 "ole",
10386#endif
10387#ifdef FEAT_OSFILETYPE
10388 "osfiletype",
10389#endif
10390#ifdef FEAT_PATH_EXTRA
10391 "path_extra",
10392#endif
10393#ifdef FEAT_PERL
10394#ifndef DYNAMIC_PERL
10395 "perl",
10396#endif
10397#endif
10398#ifdef FEAT_PYTHON
10399#ifndef DYNAMIC_PYTHON
10400 "python",
10401#endif
10402#endif
10403#ifdef FEAT_POSTSCRIPT
10404 "postscript",
10405#endif
10406#ifdef FEAT_PRINTER
10407 "printer",
10408#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010409#ifdef FEAT_PROFILE
10410 "profile",
10411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412#ifdef FEAT_QUICKFIX
10413 "quickfix",
10414#endif
10415#ifdef FEAT_RIGHTLEFT
10416 "rightleft",
10417#endif
10418#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10419 "ruby",
10420#endif
10421#ifdef FEAT_SCROLLBIND
10422 "scrollbind",
10423#endif
10424#ifdef FEAT_CMDL_INFO
10425 "showcmd",
10426 "cmdline_info",
10427#endif
10428#ifdef FEAT_SIGNS
10429 "signs",
10430#endif
10431#ifdef FEAT_SMARTINDENT
10432 "smartindent",
10433#endif
10434#ifdef FEAT_SNIFF
10435 "sniff",
10436#endif
10437#ifdef FEAT_STL_OPT
10438 "statusline",
10439#endif
10440#ifdef FEAT_SUN_WORKSHOP
10441 "sun_workshop",
10442#endif
10443#ifdef FEAT_NETBEANS_INTG
10444 "netbeans_intg",
10445#endif
10446#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010447 "spell",
10448#endif
10449#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 "syntax",
10451#endif
10452#if defined(USE_SYSTEM) || !defined(UNIX)
10453 "system",
10454#endif
10455#ifdef FEAT_TAG_BINS
10456 "tag_binary",
10457#endif
10458#ifdef FEAT_TAG_OLDSTATIC
10459 "tag_old_static",
10460#endif
10461#ifdef FEAT_TAG_ANYWHITE
10462 "tag_any_white",
10463#endif
10464#ifdef FEAT_TCL
10465# ifndef DYNAMIC_TCL
10466 "tcl",
10467# endif
10468#endif
10469#ifdef TERMINFO
10470 "terminfo",
10471#endif
10472#ifdef FEAT_TERMRESPONSE
10473 "termresponse",
10474#endif
10475#ifdef FEAT_TEXTOBJ
10476 "textobjects",
10477#endif
10478#ifdef HAVE_TGETENT
10479 "tgetent",
10480#endif
10481#ifdef FEAT_TITLE
10482 "title",
10483#endif
10484#ifdef FEAT_TOOLBAR
10485 "toolbar",
10486#endif
10487#ifdef FEAT_USR_CMDS
10488 "user-commands", /* was accidentally included in 5.4 */
10489 "user_commands",
10490#endif
10491#ifdef FEAT_VIMINFO
10492 "viminfo",
10493#endif
10494#ifdef FEAT_VERTSPLIT
10495 "vertsplit",
10496#endif
10497#ifdef FEAT_VIRTUALEDIT
10498 "virtualedit",
10499#endif
10500#ifdef FEAT_VISUAL
10501 "visual",
10502#endif
10503#ifdef FEAT_VISUALEXTRA
10504 "visualextra",
10505#endif
10506#ifdef FEAT_VREPLACE
10507 "vreplace",
10508#endif
10509#ifdef FEAT_WILDIGN
10510 "wildignore",
10511#endif
10512#ifdef FEAT_WILDMENU
10513 "wildmenu",
10514#endif
10515#ifdef FEAT_WINDOWS
10516 "windows",
10517#endif
10518#ifdef FEAT_WAK
10519 "winaltkeys",
10520#endif
10521#ifdef FEAT_WRITEBACKUP
10522 "writebackup",
10523#endif
10524#ifdef FEAT_XIM
10525 "xim",
10526#endif
10527#ifdef FEAT_XFONTSET
10528 "xfontset",
10529#endif
10530#ifdef USE_XSMP
10531 "xsmp",
10532#endif
10533#ifdef USE_XSMP_INTERACT
10534 "xsmp_interact",
10535#endif
10536#ifdef FEAT_XCLIPBOARD
10537 "xterm_clipboard",
10538#endif
10539#ifdef FEAT_XTERM_SAVE
10540 "xterm_save",
10541#endif
10542#if defined(UNIX) && defined(FEAT_X11)
10543 "X11",
10544#endif
10545 NULL
10546 };
10547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010548 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 for (i = 0; has_list[i] != NULL; ++i)
10550 if (STRICMP(name, has_list[i]) == 0)
10551 {
10552 n = TRUE;
10553 break;
10554 }
10555
10556 if (n == FALSE)
10557 {
10558 if (STRNICMP(name, "patch", 5) == 0)
10559 n = has_patch(atoi((char *)name + 5));
10560 else if (STRICMP(name, "vim_starting") == 0)
10561 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010562#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10563 else if (STRICMP(name, "balloon_multiline") == 0)
10564 n = multiline_balloon_available();
10565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566#ifdef DYNAMIC_TCL
10567 else if (STRICMP(name, "tcl") == 0)
10568 n = tcl_enabled(FALSE);
10569#endif
10570#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10571 else if (STRICMP(name, "iconv") == 0)
10572 n = iconv_enabled(FALSE);
10573#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010574#ifdef DYNAMIC_MZSCHEME
10575 else if (STRICMP(name, "mzscheme") == 0)
10576 n = mzscheme_enabled(FALSE);
10577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578#ifdef DYNAMIC_RUBY
10579 else if (STRICMP(name, "ruby") == 0)
10580 n = ruby_enabled(FALSE);
10581#endif
10582#ifdef DYNAMIC_PYTHON
10583 else if (STRICMP(name, "python") == 0)
10584 n = python_enabled(FALSE);
10585#endif
10586#ifdef DYNAMIC_PERL
10587 else if (STRICMP(name, "perl") == 0)
10588 n = perl_enabled(FALSE);
10589#endif
10590#ifdef FEAT_GUI
10591 else if (STRICMP(name, "gui_running") == 0)
10592 n = (gui.in_use || gui.starting);
10593# ifdef FEAT_GUI_W32
10594 else if (STRICMP(name, "gui_win32s") == 0)
10595 n = gui_is_win32s();
10596# endif
10597# ifdef FEAT_BROWSE
10598 else if (STRICMP(name, "browse") == 0)
10599 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10600# endif
10601#endif
10602#ifdef FEAT_SYN_HL
10603 else if (STRICMP(name, "syntax_items") == 0)
10604 n = syntax_present(curbuf);
10605#endif
10606#if defined(WIN3264)
10607 else if (STRICMP(name, "win95") == 0)
10608 n = mch_windows95();
10609#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010610#ifdef FEAT_NETBEANS_INTG
10611 else if (STRICMP(name, "netbeans_enabled") == 0)
10612 n = usingNetbeans;
10613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 }
10615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010616 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617}
10618
10619/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010620 * "has_key()" function
10621 */
10622 static void
10623f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010624 typval_T *argvars;
10625 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010626{
10627 rettv->vval.v_number = 0;
10628 if (argvars[0].v_type != VAR_DICT)
10629 {
10630 EMSG(_(e_dictreq));
10631 return;
10632 }
10633 if (argvars[0].vval.v_dict == NULL)
10634 return;
10635
10636 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010637 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010638}
10639
10640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 * "hasmapto()" function
10642 */
10643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010644f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010645 typval_T *argvars;
10646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647{
10648 char_u *name;
10649 char_u *mode;
10650 char_u buf[NUMBUFLEN];
10651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010652 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010653 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 mode = (char_u *)"nvo";
10655 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657
10658 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010659 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010661 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662}
10663
10664/*
10665 * "histadd()" function
10666 */
10667/*ARGSUSED*/
10668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010669f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010670 typval_T *argvars;
10671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672{
10673#ifdef FEAT_CMDHIST
10674 int histype;
10675 char_u *str;
10676 char_u buf[NUMBUFLEN];
10677#endif
10678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010679 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 if (check_restricted() || check_secure())
10681 return;
10682#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010683 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10684 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685 if (histype >= 0)
10686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010687 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 if (*str != NUL)
10689 {
10690 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 return;
10693 }
10694 }
10695#endif
10696}
10697
10698/*
10699 * "histdel()" function
10700 */
10701/*ARGSUSED*/
10702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010703f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010704 typval_T *argvars;
10705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706{
10707#ifdef FEAT_CMDHIST
10708 int n;
10709 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010712 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10713 if (str == NULL)
10714 n = 0;
10715 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010717 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010718 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010720 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010721 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 else
10723 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010724 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010725 get_tv_string_buf(&argvars[1], buf));
10726 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010728 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729#endif
10730}
10731
10732/*
10733 * "histget()" function
10734 */
10735/*ARGSUSED*/
10736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010737f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010738 typval_T *argvars;
10739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740{
10741#ifdef FEAT_CMDHIST
10742 int type;
10743 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010744 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010746 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10747 if (str == NULL)
10748 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010750 {
10751 type = get_histtype(str);
10752 if (argvars[1].v_type == VAR_UNKNOWN)
10753 idx = get_history_idx(type);
10754 else
10755 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10756 /* -1 on type error */
10757 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010760 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010762 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763}
10764
10765/*
10766 * "histnr()" function
10767 */
10768/*ARGSUSED*/
10769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010771 typval_T *argvars;
10772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773{
10774 int i;
10775
10776#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010777 char_u *history = get_tv_string_chk(&argvars[0]);
10778
10779 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 if (i >= HIST_CMD && i < HIST_COUNT)
10781 i = get_history_idx(i);
10782 else
10783#endif
10784 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010785 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786}
10787
10788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 * "highlightID(name)" function
10790 */
10791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010792f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010793 typval_T *argvars;
10794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797}
10798
10799/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010800 * "highlight_exists()" function
10801 */
10802 static void
10803f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010804 typval_T *argvars;
10805 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010806{
10807 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10808}
10809
10810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 * "hostname()" function
10812 */
10813/*ARGSUSED*/
10814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010815f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010816 typval_T *argvars;
10817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818{
10819 char_u hostname[256];
10820
10821 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010822 rettv->v_type = VAR_STRING;
10823 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824}
10825
10826/*
10827 * iconv() function
10828 */
10829/*ARGSUSED*/
10830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010832 typval_T *argvars;
10833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834{
10835#ifdef FEAT_MBYTE
10836 char_u buf1[NUMBUFLEN];
10837 char_u buf2[NUMBUFLEN];
10838 char_u *from, *to, *str;
10839 vimconv_T vimconv;
10840#endif
10841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010842 rettv->v_type = VAR_STRING;
10843 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844
10845#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846 str = get_tv_string(&argvars[0]);
10847 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10848 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 vimconv.vc_type = CONV_NONE;
10850 convert_setup(&vimconv, from, to);
10851
10852 /* If the encodings are equal, no conversion needed. */
10853 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010854 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857
10858 convert_setup(&vimconv, NULL, NULL);
10859 vim_free(from);
10860 vim_free(to);
10861#endif
10862}
10863
10864/*
10865 * "indent()" function
10866 */
10867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010868f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010869 typval_T *argvars;
10870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871{
10872 linenr_T lnum;
10873
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010876 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879}
10880
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010881/*
10882 * "index()" function
10883 */
10884 static void
10885f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010886 typval_T *argvars;
10887 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010888{
Bram Moolenaar33570922005-01-25 22:26:29 +000010889 list_T *l;
10890 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010891 long idx = 0;
10892 int ic = FALSE;
10893
10894 rettv->vval.v_number = -1;
10895 if (argvars[0].v_type != VAR_LIST)
10896 {
10897 EMSG(_(e_listreq));
10898 return;
10899 }
10900 l = argvars[0].vval.v_list;
10901 if (l != NULL)
10902 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010903 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010904 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010906 int error = FALSE;
10907
Bram Moolenaar758711c2005-02-02 23:11:38 +000010908 /* Start at specified item. Use the cached index that list_find()
10909 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010910 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010911 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010912 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010913 ic = get_tv_number_chk(&argvars[3], &error);
10914 if (error)
10915 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010916 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010917
Bram Moolenaar758711c2005-02-02 23:11:38 +000010918 for ( ; item != NULL; item = item->li_next, ++idx)
10919 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010920 {
10921 rettv->vval.v_number = idx;
10922 break;
10923 }
10924 }
10925}
10926
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927static int inputsecret_flag = 0;
10928
10929/*
10930 * "input()" function
10931 * Also handles inputsecret() when inputsecret is set.
10932 */
10933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010934f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010935 typval_T *argvars;
10936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010938 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 char_u *p = NULL;
10940 int c;
10941 char_u buf[NUMBUFLEN];
10942 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010943 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010944 int xp_type = EXPAND_NOTHING;
10945 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010947 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948
10949#ifdef NO_CONSOLE_INPUT
10950 /* While starting up, there is no place to enter text. */
10951 if (no_console_input())
10952 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010953 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 return;
10955 }
10956#endif
10957
10958 cmd_silent = FALSE; /* Want to see the prompt. */
10959 if (prompt != NULL)
10960 {
10961 /* Only the part of the message after the last NL is considered as
10962 * prompt for the command line */
10963 p = vim_strrchr(prompt, '\n');
10964 if (p == NULL)
10965 p = prompt;
10966 else
10967 {
10968 ++p;
10969 c = *p;
10970 *p = NUL;
10971 msg_start();
10972 msg_clr_eos();
10973 msg_puts_attr(prompt, echo_attr);
10974 msg_didout = FALSE;
10975 msg_starthere();
10976 *p = c;
10977 }
10978 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010980 if (argvars[1].v_type != VAR_UNKNOWN)
10981 {
10982 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10983 if (defstr != NULL)
10984 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985
Bram Moolenaar4463f292005-09-25 22:20:24 +000010986 if (argvars[2].v_type != VAR_UNKNOWN)
10987 {
10988 char_u *xp_name;
10989 int xp_namelen;
10990 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010991
Bram Moolenaar4463f292005-09-25 22:20:24 +000010992 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010993
Bram Moolenaar4463f292005-09-25 22:20:24 +000010994 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
10995 if (xp_name == NULL)
10996 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010997
Bram Moolenaar4463f292005-09-25 22:20:24 +000010998 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010999
Bram Moolenaar4463f292005-09-25 22:20:24 +000011000 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11001 &xp_arg) == FAIL)
11002 return;
11003 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011004 }
11005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011006 if (defstr != NULL)
11007 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011008 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11009 xp_type, xp_arg);
11010
11011 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011013 /* since the user typed this, no need to wait for return */
11014 need_wait_return = FALSE;
11015 msg_didout = FALSE;
11016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 cmd_silent = cmd_silent_save;
11018}
11019
11020/*
11021 * "inputdialog()" function
11022 */
11023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011025 typval_T *argvars;
11026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027{
11028#if defined(FEAT_GUI_TEXTDIALOG)
11029 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11030 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11031 {
11032 char_u *message;
11033 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011034 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011036 message = get_tv_string_chk(&argvars[0]);
11037 if (argvars[1].v_type != VAR_UNKNOWN
11038 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011039 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 else
11041 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011042 if (message != NULL && defstr != NULL
11043 && do_dialog(VIM_QUESTION, NULL, message,
11044 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 else
11047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011048 if (message != NULL && defstr != NULL
11049 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011050 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 rettv->vval.v_string = vim_strsave(
11052 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011056 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 }
11058 else
11059#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011060 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061}
11062
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011063/*
11064 * "inputlist()" function
11065 */
11066 static void
11067f_inputlist(argvars, rettv)
11068 typval_T *argvars;
11069 typval_T *rettv;
11070{
11071 listitem_T *li;
11072 int selected;
11073 int mouse_used;
11074
11075 rettv->vval.v_number = 0;
11076#ifdef NO_CONSOLE_INPUT
11077 /* While starting up, there is no place to enter text. */
11078 if (no_console_input())
11079 return;
11080#endif
11081 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11082 {
11083 EMSG2(_(e_listarg), "inputlist()");
11084 return;
11085 }
11086
11087 msg_start();
11088 lines_left = Rows; /* avoid more prompt */
11089 msg_scroll = TRUE;
11090 msg_clr_eos();
11091
11092 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11093 {
11094 msg_puts(get_tv_string(&li->li_tv));
11095 msg_putchar('\n');
11096 }
11097
11098 /* Ask for choice. */
11099 selected = prompt_for_number(&mouse_used);
11100 if (mouse_used)
11101 selected -= lines_left;
11102
11103 rettv->vval.v_number = selected;
11104}
11105
11106
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11108
11109/*
11110 * "inputrestore()" function
11111 */
11112/*ARGSUSED*/
11113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011114f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011115 typval_T *argvars;
11116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117{
11118 if (ga_userinput.ga_len > 0)
11119 {
11120 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11122 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 }
11125 else if (p_verbose > 1)
11126 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011127 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011128 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 }
11130}
11131
11132/*
11133 * "inputsave()" function
11134 */
11135/*ARGSUSED*/
11136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011138 typval_T *argvars;
11139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140{
11141 /* Add an entry to the stack of typehead storage. */
11142 if (ga_grow(&ga_userinput, 1) == OK)
11143 {
11144 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11145 + ga_userinput.ga_len);
11146 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011147 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 }
11149 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151}
11152
11153/*
11154 * "inputsecret()" function
11155 */
11156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011157f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011158 typval_T *argvars;
11159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160{
11161 ++cmdline_star;
11162 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011163 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164 --cmdline_star;
11165 --inputsecret_flag;
11166}
11167
11168/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011169 * "insert()" function
11170 */
11171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011173 typval_T *argvars;
11174 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011175{
11176 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011177 listitem_T *item;
11178 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011179 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011180
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011181 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011182 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011183 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011184 else if ((l = argvars[0].vval.v_list) != NULL
11185 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011186 {
11187 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011188 before = get_tv_number_chk(&argvars[2], &error);
11189 if (error)
11190 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011191
Bram Moolenaar758711c2005-02-02 23:11:38 +000011192 if (before == l->lv_len)
11193 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011194 else
11195 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011196 item = list_find(l, before);
11197 if (item == NULL)
11198 {
11199 EMSGN(_(e_listidx), before);
11200 l = NULL;
11201 }
11202 }
11203 if (l != NULL)
11204 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011205 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011206 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011207 }
11208 }
11209}
11210
11211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 * "isdirectory()" function
11213 */
11214 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011215f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011216 typval_T *argvars;
11217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220}
11221
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011222/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011223 * "islocked()" function
11224 */
11225 static void
11226f_islocked(argvars, rettv)
11227 typval_T *argvars;
11228 typval_T *rettv;
11229{
11230 lval_T lv;
11231 char_u *end;
11232 dictitem_T *di;
11233
11234 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011235 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11236 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011237 if (end != NULL && lv.ll_name != NULL)
11238 {
11239 if (*end != NUL)
11240 EMSG(_(e_trailing));
11241 else
11242 {
11243 if (lv.ll_tv == NULL)
11244 {
11245 if (check_changedtick(lv.ll_name))
11246 rettv->vval.v_number = 1; /* always locked */
11247 else
11248 {
11249 di = find_var(lv.ll_name, NULL);
11250 if (di != NULL)
11251 {
11252 /* Consider a variable locked when:
11253 * 1. the variable itself is locked
11254 * 2. the value of the variable is locked.
11255 * 3. the List or Dict value is locked.
11256 */
11257 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11258 || tv_islocked(&di->di_tv));
11259 }
11260 }
11261 }
11262 else if (lv.ll_range)
11263 EMSG(_("E745: Range not allowed"));
11264 else if (lv.ll_newkey != NULL)
11265 EMSG2(_(e_dictkey), lv.ll_newkey);
11266 else if (lv.ll_list != NULL)
11267 /* List item. */
11268 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11269 else
11270 /* Dictionary item. */
11271 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11272 }
11273 }
11274
11275 clear_lval(&lv);
11276}
11277
Bram Moolenaar33570922005-01-25 22:26:29 +000011278static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011279
11280/*
11281 * Turn a dict into a list:
11282 * "what" == 0: list of keys
11283 * "what" == 1: list of values
11284 * "what" == 2: list of items
11285 */
11286 static void
11287dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011288 typval_T *argvars;
11289 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011290 int what;
11291{
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 list_T *l;
11293 list_T *l2;
11294 dictitem_T *di;
11295 hashitem_T *hi;
11296 listitem_T *li;
11297 listitem_T *li2;
11298 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011299 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011300
11301 rettv->vval.v_number = 0;
11302 if (argvars[0].v_type != VAR_DICT)
11303 {
11304 EMSG(_(e_dictreq));
11305 return;
11306 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011307 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011308 return;
11309
11310 l = list_alloc();
11311 if (l == NULL)
11312 return;
11313 rettv->v_type = VAR_LIST;
11314 rettv->vval.v_list = l;
11315 ++l->lv_refcount;
11316
Bram Moolenaar33570922005-01-25 22:26:29 +000011317 todo = d->dv_hashtab.ht_used;
11318 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011319 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011320 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011322 --todo;
11323 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011324
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011325 li = listitem_alloc();
11326 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011327 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011328 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011329
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011330 if (what == 0)
11331 {
11332 /* keys() */
11333 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011334 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011335 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11336 }
11337 else if (what == 1)
11338 {
11339 /* values() */
11340 copy_tv(&di->di_tv, &li->li_tv);
11341 }
11342 else
11343 {
11344 /* items() */
11345 l2 = list_alloc();
11346 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011347 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011348 li->li_tv.vval.v_list = l2;
11349 if (l2 == NULL)
11350 break;
11351 ++l2->lv_refcount;
11352
11353 li2 = listitem_alloc();
11354 if (li2 == NULL)
11355 break;
11356 list_append(l2, li2);
11357 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011358 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011359 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11360
11361 li2 = listitem_alloc();
11362 if (li2 == NULL)
11363 break;
11364 list_append(l2, li2);
11365 copy_tv(&di->di_tv, &li2->li_tv);
11366 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011367 }
11368 }
11369}
11370
11371/*
11372 * "items(dict)" function
11373 */
11374 static void
11375f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011376 typval_T *argvars;
11377 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011378{
11379 dict_list(argvars, rettv, 2);
11380}
11381
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011383 * "join()" function
11384 */
11385 static void
11386f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011387 typval_T *argvars;
11388 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011389{
11390 garray_T ga;
11391 char_u *sep;
11392
11393 rettv->vval.v_number = 0;
11394 if (argvars[0].v_type != VAR_LIST)
11395 {
11396 EMSG(_(e_listreq));
11397 return;
11398 }
11399 if (argvars[0].vval.v_list == NULL)
11400 return;
11401 if (argvars[1].v_type == VAR_UNKNOWN)
11402 sep = (char_u *)" ";
11403 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011404 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011405
11406 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011407
11408 if (sep != NULL)
11409 {
11410 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011411 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011412 ga_append(&ga, NUL);
11413 rettv->vval.v_string = (char_u *)ga.ga_data;
11414 }
11415 else
11416 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011417}
11418
11419/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011420 * "keys()" function
11421 */
11422 static void
11423f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011424 typval_T *argvars;
11425 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011426{
11427 dict_list(argvars, rettv, 0);
11428}
11429
11430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 * "last_buffer_nr()" function.
11432 */
11433/*ARGSUSED*/
11434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011436 typval_T *argvars;
11437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438{
11439 int n = 0;
11440 buf_T *buf;
11441
11442 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11443 if (n < buf->b_fnum)
11444 n = buf->b_fnum;
11445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011447}
11448
11449/*
11450 * "len()" function
11451 */
11452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011454 typval_T *argvars;
11455 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011456{
11457 switch (argvars[0].v_type)
11458 {
11459 case VAR_STRING:
11460 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461 rettv->vval.v_number = (varnumber_T)STRLEN(
11462 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011463 break;
11464 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011465 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011466 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011467 case VAR_DICT:
11468 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11469 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011470 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011471 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011472 break;
11473 }
11474}
11475
Bram Moolenaar33570922005-01-25 22:26:29 +000011476static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011477
11478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011479libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011480 typval_T *argvars;
11481 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011482 int type;
11483{
11484#ifdef FEAT_LIBCALL
11485 char_u *string_in;
11486 char_u **string_result;
11487 int nr_result;
11488#endif
11489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011491 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011495
11496 if (check_restricted() || check_secure())
11497 return;
11498
11499#ifdef FEAT_LIBCALL
11500 /* The first two args must be strings, otherwise its meaningless */
11501 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11502 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011503 string_in = NULL;
11504 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011505 string_in = argvars[2].vval.v_string;
11506 if (type == VAR_NUMBER)
11507 string_result = NULL;
11508 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011509 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011510 if (mch_libcall(argvars[0].vval.v_string,
11511 argvars[1].vval.v_string,
11512 string_in,
11513 argvars[2].vval.v_number,
11514 string_result,
11515 &nr_result) == OK
11516 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011518 }
11519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520}
11521
11522/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011523 * "libcall()" function
11524 */
11525 static void
11526f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011527 typval_T *argvars;
11528 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011529{
11530 libcall_common(argvars, rettv, VAR_STRING);
11531}
11532
11533/*
11534 * "libcallnr()" function
11535 */
11536 static void
11537f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011538 typval_T *argvars;
11539 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011540{
11541 libcall_common(argvars, rettv, VAR_NUMBER);
11542}
11543
11544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545 * "line(string)" function
11546 */
11547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011549 typval_T *argvars;
11550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551{
11552 linenr_T lnum = 0;
11553 pos_T *fp;
11554
11555 fp = var2fpos(&argvars[0], TRUE);
11556 if (fp != NULL)
11557 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559}
11560
11561/*
11562 * "line2byte(lnum)" function
11563 */
11564/*ARGSUSED*/
11565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011567 typval_T *argvars;
11568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569{
11570#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011571 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572#else
11573 linenr_T lnum;
11574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011575 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11580 if (rettv->vval.v_number >= 0)
11581 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582#endif
11583}
11584
11585/*
11586 * "lispindent(lnum)" function
11587 */
11588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011590 typval_T *argvars;
11591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592{
11593#ifdef FEAT_LISP
11594 pos_T pos;
11595 linenr_T lnum;
11596
11597 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011598 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11600 {
11601 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 curwin->w_cursor = pos;
11604 }
11605 else
11606#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011607 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608}
11609
11610/*
11611 * "localtime()" function
11612 */
11613/*ARGSUSED*/
11614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011615f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011616 typval_T *argvars;
11617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620}
11621
Bram Moolenaar33570922005-01-25 22:26:29 +000011622static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623
11624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011625get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011626 typval_T *argvars;
11627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 int exact;
11629{
11630 char_u *keys;
11631 char_u *which;
11632 char_u buf[NUMBUFLEN];
11633 char_u *keys_buf = NULL;
11634 char_u *rhs;
11635 int mode;
11636 garray_T ga;
11637
11638 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011639 rettv->v_type = VAR_STRING;
11640 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011642 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 if (*keys == NUL)
11644 return;
11645
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011646 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011647 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648 else
11649 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011650 if (which == NULL)
11651 return;
11652
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653 mode = get_map_mode(&which, 0);
11654
11655 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011656 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 vim_free(keys_buf);
11658 if (rhs != NULL)
11659 {
11660 ga_init(&ga);
11661 ga.ga_itemsize = 1;
11662 ga.ga_growsize = 40;
11663
11664 while (*rhs != NUL)
11665 ga_concat(&ga, str2special(&rhs, FALSE));
11666
11667 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 }
11670}
11671
11672/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011673 * "map()" function
11674 */
11675 static void
11676f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011677 typval_T *argvars;
11678 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011679{
11680 filter_map(argvars, rettv, TRUE);
11681}
11682
11683/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011684 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685 */
11686 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011687f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011688 typval_T *argvars;
11689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011691 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692}
11693
11694/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011695 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 */
11697 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011698f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011699 typval_T *argvars;
11700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011702 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703}
11704
Bram Moolenaar33570922005-01-25 22:26:29 +000011705static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706
11707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011708find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011709 typval_T *argvars;
11710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711 int type;
11712{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011713 char_u *str = NULL;
11714 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 char_u *pat;
11716 regmatch_T regmatch;
11717 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011718 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 char_u *save_cpo;
11720 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011721 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011722 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011723 list_T *l = NULL;
11724 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011725 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011726 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727
11728 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11729 save_cpo = p_cpo;
11730 p_cpo = (char_u *)"";
11731
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011732 rettv->vval.v_number = -1;
11733 if (type == 3)
11734 {
11735 /* return empty list when there are no matches */
11736 if ((rettv->vval.v_list = list_alloc()) == NULL)
11737 goto theend;
11738 rettv->v_type = VAR_LIST;
11739 ++rettv->vval.v_list->lv_refcount;
11740 }
11741 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011743 rettv->v_type = VAR_STRING;
11744 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011747 if (argvars[0].v_type == VAR_LIST)
11748 {
11749 if ((l = argvars[0].vval.v_list) == NULL)
11750 goto theend;
11751 li = l->lv_first;
11752 }
11753 else
11754 expr = str = get_tv_string(&argvars[0]);
11755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011756 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11757 if (pat == NULL)
11758 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011759
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011760 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011762 int error = FALSE;
11763
11764 start = get_tv_number_chk(&argvars[2], &error);
11765 if (error)
11766 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011767 if (l != NULL)
11768 {
11769 li = list_find(l, start);
11770 if (li == NULL)
11771 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011772 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011773 }
11774 else
11775 {
11776 if (start < 0)
11777 start = 0;
11778 if (start > (long)STRLEN(str))
11779 goto theend;
11780 str += start;
11781 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011782
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011783 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011784 nth = get_tv_number_chk(&argvars[3], &error);
11785 if (error)
11786 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787 }
11788
11789 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11790 if (regmatch.regprog != NULL)
11791 {
11792 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011793
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011794 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011795 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011796 if (l != NULL)
11797 {
11798 if (li == NULL)
11799 {
11800 match = FALSE;
11801 break;
11802 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011803 vim_free(tofree);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011804 str = echo_string(&li->li_tv, &tofree, strbuf,0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011805 if (str == NULL)
11806 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011807 }
11808
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011809 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011810
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011811 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011812 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011813 if (l == NULL && !match)
11814 break;
11815
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011816 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011817 if (l != NULL)
11818 {
11819 li = li->li_next;
11820 ++idx;
11821 }
11822 else
11823 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011824#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011825 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011826#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011827 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011828#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011829 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011830 }
11831
11832 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011834 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011835 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011836 int i;
11837
11838 /* return list with matched string and submatches */
11839 for (i = 0; i < NSUBEXP; ++i)
11840 {
11841 if (regmatch.endp[i] == NULL)
11842 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011843 if (list_append_string(rettv->vval.v_list,
11844 regmatch.startp[i],
11845 (int)(regmatch.endp[i] - regmatch.startp[i]))
11846 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011847 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011848 }
11849 }
11850 else if (type == 2)
11851 {
11852 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011853 if (l != NULL)
11854 copy_tv(&li->li_tv, rettv);
11855 else
11856 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011858 }
11859 else if (l != NULL)
11860 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861 else
11862 {
11863 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 (varnumber_T)(regmatch.startp[0] - str);
11866 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011867 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011869 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870 }
11871 }
11872 vim_free(regmatch.regprog);
11873 }
11874
11875theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011876 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 p_cpo = save_cpo;
11878}
11879
11880/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011881 * "match()" function
11882 */
11883 static void
11884f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011885 typval_T *argvars;
11886 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011887{
11888 find_some_match(argvars, rettv, 1);
11889}
11890
11891/*
11892 * "matchend()" function
11893 */
11894 static void
11895f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011896 typval_T *argvars;
11897 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011898{
11899 find_some_match(argvars, rettv, 0);
11900}
11901
11902/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011903 * "matchlist()" function
11904 */
11905 static void
11906f_matchlist(argvars, rettv)
11907 typval_T *argvars;
11908 typval_T *rettv;
11909{
11910 find_some_match(argvars, rettv, 3);
11911}
11912
11913/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011914 * "matchstr()" function
11915 */
11916 static void
11917f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011918 typval_T *argvars;
11919 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011920{
11921 find_some_match(argvars, rettv, 2);
11922}
11923
Bram Moolenaar33570922005-01-25 22:26:29 +000011924static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011925
11926 static void
11927max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011928 typval_T *argvars;
11929 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011930 int domax;
11931{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011932 long n = 0;
11933 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011934 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011935
11936 if (argvars[0].v_type == VAR_LIST)
11937 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011938 list_T *l;
11939 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011940
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011941 l = argvars[0].vval.v_list;
11942 if (l != NULL)
11943 {
11944 li = l->lv_first;
11945 if (li != NULL)
11946 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011947 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011948 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011949 {
11950 li = li->li_next;
11951 if (li == NULL)
11952 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011953 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011954 if (domax ? i > n : i < n)
11955 n = i;
11956 }
11957 }
11958 }
11959 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011960 else if (argvars[0].v_type == VAR_DICT)
11961 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011962 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011963 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011964 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011965 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011966
11967 d = argvars[0].vval.v_dict;
11968 if (d != NULL)
11969 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011970 todo = d->dv_hashtab.ht_used;
11971 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011972 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011973 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011974 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011975 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011976 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011977 if (first)
11978 {
11979 n = i;
11980 first = FALSE;
11981 }
11982 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011983 n = i;
11984 }
11985 }
11986 }
11987 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011988 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011989 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011990 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011991}
11992
11993/*
11994 * "max()" function
11995 */
11996 static void
11997f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011998 typval_T *argvars;
11999 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012000{
12001 max_min(argvars, rettv, TRUE);
12002}
12003
12004/*
12005 * "min()" function
12006 */
12007 static void
12008f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012009 typval_T *argvars;
12010 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012011{
12012 max_min(argvars, rettv, FALSE);
12013}
12014
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012015static int mkdir_recurse __ARGS((char_u *dir, int prot));
12016
12017/*
12018 * Create the directory in which "dir" is located, and higher levels when
12019 * needed.
12020 */
12021 static int
12022mkdir_recurse(dir, prot)
12023 char_u *dir;
12024 int prot;
12025{
12026 char_u *p;
12027 char_u *updir;
12028 int r = FAIL;
12029
12030 /* Get end of directory name in "dir".
12031 * We're done when it's "/" or "c:/". */
12032 p = gettail_sep(dir);
12033 if (p <= get_past_head(dir))
12034 return OK;
12035
12036 /* If the directory exists we're done. Otherwise: create it.*/
12037 updir = vim_strnsave(dir, (int)(p - dir));
12038 if (updir == NULL)
12039 return FAIL;
12040 if (mch_isdir(updir))
12041 r = OK;
12042 else if (mkdir_recurse(updir, prot) == OK)
12043 r = vim_mkdir_emsg(updir, prot);
12044 vim_free(updir);
12045 return r;
12046}
12047
12048#ifdef vim_mkdir
12049/*
12050 * "mkdir()" function
12051 */
12052 static void
12053f_mkdir(argvars, rettv)
12054 typval_T *argvars;
12055 typval_T *rettv;
12056{
12057 char_u *dir;
12058 char_u buf[NUMBUFLEN];
12059 int prot = 0755;
12060
12061 rettv->vval.v_number = FAIL;
12062 if (check_restricted() || check_secure())
12063 return;
12064
12065 dir = get_tv_string_buf(&argvars[0], buf);
12066 if (argvars[1].v_type != VAR_UNKNOWN)
12067 {
12068 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012069 prot = get_tv_number_chk(&argvars[2], NULL);
12070 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012071 mkdir_recurse(dir, prot);
12072 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012073 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012074}
12075#endif
12076
Bram Moolenaar0d660222005-01-07 21:51:51 +000012077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078 * "mode()" function
12079 */
12080/*ARGSUSED*/
12081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012083 typval_T *argvars;
12084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085{
12086 char_u buf[2];
12087
12088#ifdef FEAT_VISUAL
12089 if (VIsual_active)
12090 {
12091 if (VIsual_select)
12092 buf[0] = VIsual_mode + 's' - 'v';
12093 else
12094 buf[0] = VIsual_mode;
12095 }
12096 else
12097#endif
12098 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12099 buf[0] = 'r';
12100 else if (State & INSERT)
12101 {
12102 if (State & REPLACE_FLAG)
12103 buf[0] = 'R';
12104 else
12105 buf[0] = 'i';
12106 }
12107 else if (State & CMDLINE)
12108 buf[0] = 'c';
12109 else
12110 buf[0] = 'n';
12111
12112 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012113 rettv->vval.v_string = vim_strsave(buf);
12114 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115}
12116
12117/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012118 * "nextnonblank()" function
12119 */
12120 static void
12121f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012122 typval_T *argvars;
12123 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012124{
12125 linenr_T lnum;
12126
12127 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012129 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012130 {
12131 lnum = 0;
12132 break;
12133 }
12134 if (*skipwhite(ml_get(lnum)) != NUL)
12135 break;
12136 }
12137 rettv->vval.v_number = lnum;
12138}
12139
12140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 * "nr2char()" function
12142 */
12143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012144f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012145 typval_T *argvars;
12146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147{
12148 char_u buf[NUMBUFLEN];
12149
12150#ifdef FEAT_MBYTE
12151 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012152 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 else
12154#endif
12155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 buf[1] = NUL;
12158 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012159 rettv->v_type = VAR_STRING;
12160 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012161}
12162
12163/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012164 * "prevnonblank()" function
12165 */
12166 static void
12167f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012168 typval_T *argvars;
12169 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012170{
12171 linenr_T lnum;
12172
12173 lnum = get_tv_lnum(argvars);
12174 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12175 lnum = 0;
12176 else
12177 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12178 --lnum;
12179 rettv->vval.v_number = lnum;
12180}
12181
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012182#ifdef HAVE_STDARG_H
12183/* This dummy va_list is here because:
12184 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12185 * - locally in the function results in a "used before set" warning
12186 * - using va_start() to initialize it gives "function with fixed args" error */
12187static va_list ap;
12188#endif
12189
Bram Moolenaar8c711452005-01-14 21:53:12 +000012190/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012191 * "printf()" function
12192 */
12193 static void
12194f_printf(argvars, rettv)
12195 typval_T *argvars;
12196 typval_T *rettv;
12197{
12198 rettv->v_type = VAR_STRING;
12199 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012200#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012201 {
12202 char_u buf[NUMBUFLEN];
12203 int len;
12204 char_u *s;
12205 int saved_did_emsg = did_emsg;
12206 char *fmt;
12207
12208 /* Get the required length, allocate the buffer and do it for real. */
12209 did_emsg = FALSE;
12210 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012211 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012212 if (!did_emsg)
12213 {
12214 s = alloc(len + 1);
12215 if (s != NULL)
12216 {
12217 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012218 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012219 }
12220 }
12221 did_emsg |= saved_did_emsg;
12222 }
12223#endif
12224}
12225
12226/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012227 * "pumvisible()" function
12228 */
12229/*ARGSUSED*/
12230 static void
12231f_pumvisible(argvars, rettv)
12232 typval_T *argvars;
12233 typval_T *rettv;
12234{
12235 rettv->vval.v_number = 0;
12236#ifdef FEAT_INS_EXPAND
12237 if (pum_visible())
12238 rettv->vval.v_number = 1;
12239#endif
12240}
12241
12242/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012243 * "range()" function
12244 */
12245 static void
12246f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012247 typval_T *argvars;
12248 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012249{
12250 long start;
12251 long end;
12252 long stride = 1;
12253 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012254 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012255 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012257 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012258 if (argvars[1].v_type == VAR_UNKNOWN)
12259 {
12260 end = start - 1;
12261 start = 0;
12262 }
12263 else
12264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012265 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012266 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012267 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012268 }
12269
12270 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012271 if (error)
12272 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012273 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012274 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012275 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012276 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012277 else
12278 {
12279 l = list_alloc();
12280 if (l != NULL)
12281 {
12282 rettv->v_type = VAR_LIST;
12283 rettv->vval.v_list = l;
12284 ++l->lv_refcount;
12285
12286 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012287 if (list_append_number(l, (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012288 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012289 }
12290 }
12291}
12292
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012293/*
12294 * "readfile()" function
12295 */
12296 static void
12297f_readfile(argvars, rettv)
12298 typval_T *argvars;
12299 typval_T *rettv;
12300{
12301 int binary = FALSE;
12302 char_u *fname;
12303 FILE *fd;
12304 list_T *l;
12305 listitem_T *li;
12306#define FREAD_SIZE 200 /* optimized for text lines */
12307 char_u buf[FREAD_SIZE];
12308 int readlen; /* size of last fread() */
12309 int buflen; /* nr of valid chars in buf[] */
12310 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12311 int tolist; /* first byte in buf[] still to be put in list */
12312 int chop; /* how many CR to chop off */
12313 char_u *prev = NULL; /* previously read bytes, if any */
12314 int prevlen = 0; /* length of "prev" if not NULL */
12315 char_u *s;
12316 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012317 long maxline = MAXLNUM;
12318 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012319
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012320 if (argvars[1].v_type != VAR_UNKNOWN)
12321 {
12322 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12323 binary = TRUE;
12324 if (argvars[2].v_type != VAR_UNKNOWN)
12325 maxline = get_tv_number(&argvars[2]);
12326 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012327
12328 l = list_alloc();
12329 if (l == NULL)
12330 return;
12331 rettv->v_type = VAR_LIST;
12332 rettv->vval.v_list = l;
12333 l->lv_refcount = 1;
12334
12335 /* Always open the file in binary mode, library functions have a mind of
12336 * their own about CR-LF conversion. */
12337 fname = get_tv_string(&argvars[0]);
12338 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12339 {
12340 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12341 return;
12342 }
12343
12344 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012345 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012346 {
12347 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12348 buflen = filtd + readlen;
12349 tolist = 0;
12350 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12351 {
12352 if (buf[filtd] == '\n' || readlen <= 0)
12353 {
12354 /* Only when in binary mode add an empty list item when the
12355 * last line ends in a '\n'. */
12356 if (!binary && readlen == 0 && filtd == 0)
12357 break;
12358
12359 /* Found end-of-line or end-of-file: add a text line to the
12360 * list. */
12361 chop = 0;
12362 if (!binary)
12363 while (filtd - chop - 1 >= tolist
12364 && buf[filtd - chop - 1] == '\r')
12365 ++chop;
12366 len = filtd - tolist - chop;
12367 if (prev == NULL)
12368 s = vim_strnsave(buf + tolist, len);
12369 else
12370 {
12371 s = alloc((unsigned)(prevlen + len + 1));
12372 if (s != NULL)
12373 {
12374 mch_memmove(s, prev, prevlen);
12375 vim_free(prev);
12376 prev = NULL;
12377 mch_memmove(s + prevlen, buf + tolist, len);
12378 s[prevlen + len] = NUL;
12379 }
12380 }
12381 tolist = filtd + 1;
12382
12383 li = listitem_alloc();
12384 if (li == NULL)
12385 {
12386 vim_free(s);
12387 break;
12388 }
12389 li->li_tv.v_type = VAR_STRING;
12390 li->li_tv.v_lock = 0;
12391 li->li_tv.vval.v_string = s;
12392 list_append(l, li);
12393
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012394 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012395 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012396 if (readlen <= 0)
12397 break;
12398 }
12399 else if (buf[filtd] == NUL)
12400 buf[filtd] = '\n';
12401 }
12402 if (readlen <= 0)
12403 break;
12404
12405 if (tolist == 0)
12406 {
12407 /* "buf" is full, need to move text to an allocated buffer */
12408 if (prev == NULL)
12409 {
12410 prev = vim_strnsave(buf, buflen);
12411 prevlen = buflen;
12412 }
12413 else
12414 {
12415 s = alloc((unsigned)(prevlen + buflen));
12416 if (s != NULL)
12417 {
12418 mch_memmove(s, prev, prevlen);
12419 mch_memmove(s + prevlen, buf, buflen);
12420 vim_free(prev);
12421 prev = s;
12422 prevlen += buflen;
12423 }
12424 }
12425 filtd = 0;
12426 }
12427 else
12428 {
12429 mch_memmove(buf, buf + tolist, buflen - tolist);
12430 filtd -= tolist;
12431 }
12432 }
12433
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012434 /*
12435 * For a negative line count use only the lines at the end of the file,
12436 * free the rest.
12437 */
12438 if (maxline < 0)
12439 while (cnt > -maxline)
12440 {
12441 listitem_remove(l, l->lv_first);
12442 --cnt;
12443 }
12444
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012445 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012446 fclose(fd);
12447}
12448
12449
Bram Moolenaar0d660222005-01-07 21:51:51 +000012450#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12451static void make_connection __ARGS((void));
12452static int check_connection __ARGS((void));
12453
12454 static void
12455make_connection()
12456{
12457 if (X_DISPLAY == NULL
12458# ifdef FEAT_GUI
12459 && !gui.in_use
12460# endif
12461 )
12462 {
12463 x_force_connect = TRUE;
12464 setup_term_clip();
12465 x_force_connect = FALSE;
12466 }
12467}
12468
12469 static int
12470check_connection()
12471{
12472 make_connection();
12473 if (X_DISPLAY == NULL)
12474 {
12475 EMSG(_("E240: No connection to Vim server"));
12476 return FAIL;
12477 }
12478 return OK;
12479}
12480#endif
12481
12482#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012483static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012484
12485 static void
12486remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012487 typval_T *argvars;
12488 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012489 int expr;
12490{
12491 char_u *server_name;
12492 char_u *keys;
12493 char_u *r = NULL;
12494 char_u buf[NUMBUFLEN];
12495# ifdef WIN32
12496 HWND w;
12497# else
12498 Window w;
12499# endif
12500
12501 if (check_restricted() || check_secure())
12502 return;
12503
12504# ifdef FEAT_X11
12505 if (check_connection() == FAIL)
12506 return;
12507# endif
12508
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012509 server_name = get_tv_string_chk(&argvars[0]);
12510 if (server_name == NULL)
12511 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012512 keys = get_tv_string_buf(&argvars[1], buf);
12513# ifdef WIN32
12514 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12515# else
12516 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12517 < 0)
12518# endif
12519 {
12520 if (r != NULL)
12521 EMSG(r); /* sending worked but evaluation failed */
12522 else
12523 EMSG2(_("E241: Unable to send to %s"), server_name);
12524 return;
12525 }
12526
12527 rettv->vval.v_string = r;
12528
12529 if (argvars[2].v_type != VAR_UNKNOWN)
12530 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012531 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012532 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012533 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012534
12535 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012536 v.di_tv.v_type = VAR_STRING;
12537 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012538 idvar = get_tv_string_chk(&argvars[2]);
12539 if (idvar != NULL)
12540 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012541 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012542 }
12543}
12544#endif
12545
12546/*
12547 * "remote_expr()" function
12548 */
12549/*ARGSUSED*/
12550 static void
12551f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012552 typval_T *argvars;
12553 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012554{
12555 rettv->v_type = VAR_STRING;
12556 rettv->vval.v_string = NULL;
12557#ifdef FEAT_CLIENTSERVER
12558 remote_common(argvars, rettv, TRUE);
12559#endif
12560}
12561
12562/*
12563 * "remote_foreground()" function
12564 */
12565/*ARGSUSED*/
12566 static void
12567f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012568 typval_T *argvars;
12569 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012570{
12571 rettv->vval.v_number = 0;
12572#ifdef FEAT_CLIENTSERVER
12573# ifdef WIN32
12574 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012575 {
12576 char_u *server_name = get_tv_string_chk(&argvars[0]);
12577
12578 if (server_name != NULL)
12579 serverForeground(server_name);
12580 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012581# else
12582 /* Send a foreground() expression to the server. */
12583 argvars[1].v_type = VAR_STRING;
12584 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12585 argvars[2].v_type = VAR_UNKNOWN;
12586 remote_common(argvars, rettv, TRUE);
12587 vim_free(argvars[1].vval.v_string);
12588# endif
12589#endif
12590}
12591
12592/*ARGSUSED*/
12593 static void
12594f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012595 typval_T *argvars;
12596 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012597{
12598#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012599 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012600 char_u *s = NULL;
12601# ifdef WIN32
12602 int n = 0;
12603# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012604 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012605
12606 if (check_restricted() || check_secure())
12607 {
12608 rettv->vval.v_number = -1;
12609 return;
12610 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012611 serverid = get_tv_string_chk(&argvars[0]);
12612 if (serverid == NULL)
12613 {
12614 rettv->vval.v_number = -1;
12615 return; /* type error; errmsg already given */
12616 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012617# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012618 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619 if (n == 0)
12620 rettv->vval.v_number = -1;
12621 else
12622 {
12623 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12624 rettv->vval.v_number = (s != NULL);
12625 }
12626# else
12627 rettv->vval.v_number = 0;
12628 if (check_connection() == FAIL)
12629 return;
12630
12631 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012632 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012633# endif
12634
12635 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12636 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012637 char_u *retvar;
12638
Bram Moolenaar33570922005-01-25 22:26:29 +000012639 v.di_tv.v_type = VAR_STRING;
12640 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012641 retvar = get_tv_string_chk(&argvars[1]);
12642 if (retvar != NULL)
12643 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012644 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012645 }
12646#else
12647 rettv->vval.v_number = -1;
12648#endif
12649}
12650
12651/*ARGSUSED*/
12652 static void
12653f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012654 typval_T *argvars;
12655 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012656{
12657 char_u *r = NULL;
12658
12659#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012660 char_u *serverid = get_tv_string_chk(&argvars[0]);
12661
12662 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012663 {
12664# ifdef WIN32
12665 /* The server's HWND is encoded in the 'id' parameter */
12666 int n = 0;
12667
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012668 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012669 if (n != 0)
12670 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12671 if (r == NULL)
12672# else
12673 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012674 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012675# endif
12676 EMSG(_("E277: Unable to read a server reply"));
12677 }
12678#endif
12679 rettv->v_type = VAR_STRING;
12680 rettv->vval.v_string = r;
12681}
12682
12683/*
12684 * "remote_send()" function
12685 */
12686/*ARGSUSED*/
12687 static void
12688f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012689 typval_T *argvars;
12690 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012691{
12692 rettv->v_type = VAR_STRING;
12693 rettv->vval.v_string = NULL;
12694#ifdef FEAT_CLIENTSERVER
12695 remote_common(argvars, rettv, FALSE);
12696#endif
12697}
12698
12699/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012700 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012701 */
12702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012704 typval_T *argvars;
12705 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012706{
Bram Moolenaar33570922005-01-25 22:26:29 +000012707 list_T *l;
12708 listitem_T *item, *item2;
12709 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012710 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012711 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012712 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012713 dict_T *d;
12714 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012715
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012716 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012717 if (argvars[0].v_type == VAR_DICT)
12718 {
12719 if (argvars[2].v_type != VAR_UNKNOWN)
12720 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012721 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012722 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012724 key = get_tv_string_chk(&argvars[1]);
12725 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012727 di = dict_find(d, key, -1);
12728 if (di == NULL)
12729 EMSG2(_(e_dictkey), key);
12730 else
12731 {
12732 *rettv = di->di_tv;
12733 init_tv(&di->di_tv);
12734 dictitem_remove(d, di);
12735 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012737 }
12738 }
12739 else if (argvars[0].v_type != VAR_LIST)
12740 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012741 else if ((l = argvars[0].vval.v_list) != NULL
12742 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012744 int error = FALSE;
12745
12746 idx = get_tv_number_chk(&argvars[1], &error);
12747 if (error)
12748 ; /* type error: do nothing, errmsg already given */
12749 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012750 EMSGN(_(e_listidx), idx);
12751 else
12752 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012753 if (argvars[2].v_type == VAR_UNKNOWN)
12754 {
12755 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012756 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012757 *rettv = item->li_tv;
12758 vim_free(item);
12759 }
12760 else
12761 {
12762 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012763 end = get_tv_number_chk(&argvars[2], &error);
12764 if (error)
12765 ; /* type error: do nothing */
12766 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012767 EMSGN(_(e_listidx), end);
12768 else
12769 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012770 int cnt = 0;
12771
12772 for (li = item; li != NULL; li = li->li_next)
12773 {
12774 ++cnt;
12775 if (li == item2)
12776 break;
12777 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012778 if (li == NULL) /* didn't find "item2" after "item" */
12779 EMSG(_(e_invrange));
12780 else
12781 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012782 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012783 l = list_alloc();
12784 if (l != NULL)
12785 {
12786 rettv->v_type = VAR_LIST;
12787 rettv->vval.v_list = l;
12788 l->lv_first = item;
12789 l->lv_last = item2;
12790 l->lv_refcount = 1;
12791 item->li_prev = NULL;
12792 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012793 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012794 }
12795 }
12796 }
12797 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012798 }
12799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800}
12801
12802/*
12803 * "rename({from}, {to})" function
12804 */
12805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012806f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012807 typval_T *argvars;
12808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809{
12810 char_u buf[NUMBUFLEN];
12811
12812 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012813 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012815 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12816 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817}
12818
12819/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012820 * "repeat()" function
12821 */
12822/*ARGSUSED*/
12823 static void
12824f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012825 typval_T *argvars;
12826 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012827{
12828 char_u *p;
12829 int n;
12830 int slen;
12831 int len;
12832 char_u *r;
12833 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012834 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012835
12836 n = get_tv_number(&argvars[1]);
12837 if (argvars[0].v_type == VAR_LIST)
12838 {
12839 l = list_alloc();
12840 if (l != NULL && argvars[0].vval.v_list != NULL)
12841 {
12842 l->lv_refcount = 1;
12843 while (n-- > 0)
12844 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12845 break;
12846 }
12847 rettv->v_type = VAR_LIST;
12848 rettv->vval.v_list = l;
12849 }
12850 else
12851 {
12852 p = get_tv_string(&argvars[0]);
12853 rettv->v_type = VAR_STRING;
12854 rettv->vval.v_string = NULL;
12855
12856 slen = (int)STRLEN(p);
12857 len = slen * n;
12858 if (len <= 0)
12859 return;
12860
12861 r = alloc(len + 1);
12862 if (r != NULL)
12863 {
12864 for (i = 0; i < n; i++)
12865 mch_memmove(r + i * slen, p, (size_t)slen);
12866 r[len] = NUL;
12867 }
12868
12869 rettv->vval.v_string = r;
12870 }
12871}
12872
12873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 * "resolve()" function
12875 */
12876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012878 typval_T *argvars;
12879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880{
12881 char_u *p;
12882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884#ifdef FEAT_SHORTCUT
12885 {
12886 char_u *v = NULL;
12887
12888 v = mch_resolve_shortcut(p);
12889 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012890 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012892 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893 }
12894#else
12895# ifdef HAVE_READLINK
12896 {
12897 char_u buf[MAXPATHL + 1];
12898 char_u *cpy;
12899 int len;
12900 char_u *remain = NULL;
12901 char_u *q;
12902 int is_relative_to_current = FALSE;
12903 int has_trailing_pathsep = FALSE;
12904 int limit = 100;
12905
12906 p = vim_strsave(p);
12907
12908 if (p[0] == '.' && (vim_ispathsep(p[1])
12909 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12910 is_relative_to_current = TRUE;
12911
12912 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012913 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914 has_trailing_pathsep = TRUE;
12915
12916 q = getnextcomp(p);
12917 if (*q != NUL)
12918 {
12919 /* Separate the first path component in "p", and keep the
12920 * remainder (beginning with the path separator). */
12921 remain = vim_strsave(q - 1);
12922 q[-1] = NUL;
12923 }
12924
12925 for (;;)
12926 {
12927 for (;;)
12928 {
12929 len = readlink((char *)p, (char *)buf, MAXPATHL);
12930 if (len <= 0)
12931 break;
12932 buf[len] = NUL;
12933
12934 if (limit-- == 0)
12935 {
12936 vim_free(p);
12937 vim_free(remain);
12938 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 goto fail;
12941 }
12942
12943 /* Ensure that the result will have a trailing path separator
12944 * if the argument has one. */
12945 if (remain == NULL && has_trailing_pathsep)
12946 add_pathsep(buf);
12947
12948 /* Separate the first path component in the link value and
12949 * concatenate the remainders. */
12950 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12951 if (*q != NUL)
12952 {
12953 if (remain == NULL)
12954 remain = vim_strsave(q - 1);
12955 else
12956 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000012957 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 if (cpy != NULL)
12959 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960 vim_free(remain);
12961 remain = cpy;
12962 }
12963 }
12964 q[-1] = NUL;
12965 }
12966
12967 q = gettail(p);
12968 if (q > p && *q == NUL)
12969 {
12970 /* Ignore trailing path separator. */
12971 q[-1] = NUL;
12972 q = gettail(p);
12973 }
12974 if (q > p && !mch_isFullName(buf))
12975 {
12976 /* symlink is relative to directory of argument */
12977 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12978 if (cpy != NULL)
12979 {
12980 STRCPY(cpy, p);
12981 STRCPY(gettail(cpy), buf);
12982 vim_free(p);
12983 p = cpy;
12984 }
12985 }
12986 else
12987 {
12988 vim_free(p);
12989 p = vim_strsave(buf);
12990 }
12991 }
12992
12993 if (remain == NULL)
12994 break;
12995
12996 /* Append the first path component of "remain" to "p". */
12997 q = getnextcomp(remain + 1);
12998 len = q - remain - (*q != NUL);
12999 cpy = vim_strnsave(p, STRLEN(p) + len);
13000 if (cpy != NULL)
13001 {
13002 STRNCAT(cpy, remain, len);
13003 vim_free(p);
13004 p = cpy;
13005 }
13006 /* Shorten "remain". */
13007 if (*q != NUL)
13008 STRCPY(remain, q - 1);
13009 else
13010 {
13011 vim_free(remain);
13012 remain = NULL;
13013 }
13014 }
13015
13016 /* If the result is a relative path name, make it explicitly relative to
13017 * the current directory if and only if the argument had this form. */
13018 if (!vim_ispathsep(*p))
13019 {
13020 if (is_relative_to_current
13021 && *p != NUL
13022 && !(p[0] == '.'
13023 && (p[1] == NUL
13024 || vim_ispathsep(p[1])
13025 || (p[1] == '.'
13026 && (p[2] == NUL
13027 || vim_ispathsep(p[2]))))))
13028 {
13029 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013030 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 if (cpy != NULL)
13032 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033 vim_free(p);
13034 p = cpy;
13035 }
13036 }
13037 else if (!is_relative_to_current)
13038 {
13039 /* Strip leading "./". */
13040 q = p;
13041 while (q[0] == '.' && vim_ispathsep(q[1]))
13042 q += 2;
13043 if (q > p)
13044 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13045 }
13046 }
13047
13048 /* Ensure that the result will have no trailing path separator
13049 * if the argument had none. But keep "/" or "//". */
13050 if (!has_trailing_pathsep)
13051 {
13052 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013053 if (after_pathsep(p, q))
13054 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055 }
13056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058 }
13059# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013060 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061# endif
13062#endif
13063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013064 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065
13066#ifdef HAVE_READLINK
13067fail:
13068#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013069 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070}
13071
13072/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013073 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 */
13075 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013076f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013077 typval_T *argvars;
13078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079{
Bram Moolenaar33570922005-01-25 22:26:29 +000013080 list_T *l;
13081 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082
Bram Moolenaar0d660222005-01-07 21:51:51 +000013083 rettv->vval.v_number = 0;
13084 if (argvars[0].v_type != VAR_LIST)
13085 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013086 else if ((l = argvars[0].vval.v_list) != NULL
13087 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013088 {
13089 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013090 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013091 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013092 while (li != NULL)
13093 {
13094 ni = li->li_prev;
13095 list_append(l, li);
13096 li = ni;
13097 }
13098 rettv->vval.v_list = l;
13099 rettv->v_type = VAR_LIST;
13100 ++l->lv_refcount;
13101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102}
13103
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013104#define SP_NOMOVE 1 /* don't move cursor */
13105#define SP_REPEAT 2 /* repeat to find outer pair */
13106#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000013107#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013108
Bram Moolenaar33570922005-01-25 22:26:29 +000013109static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013110
13111/*
13112 * Get flags for a search function.
13113 * Possibly sets "p_ws".
13114 * Returns BACKWARD, FORWARD or zero (for an error).
13115 */
13116 static int
13117get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013118 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013119 int *flagsp;
13120{
13121 int dir = FORWARD;
13122 char_u *flags;
13123 char_u nbuf[NUMBUFLEN];
13124 int mask;
13125
13126 if (varp->v_type != VAR_UNKNOWN)
13127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013128 flags = get_tv_string_buf_chk(varp, nbuf);
13129 if (flags == NULL)
13130 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013131 while (*flags != NUL)
13132 {
13133 switch (*flags)
13134 {
13135 case 'b': dir = BACKWARD; break;
13136 case 'w': p_ws = TRUE; break;
13137 case 'W': p_ws = FALSE; break;
13138 default: mask = 0;
13139 if (flagsp != NULL)
13140 switch (*flags)
13141 {
13142 case 'n': mask = SP_NOMOVE; break;
13143 case 'r': mask = SP_REPEAT; break;
13144 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013145 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013146 }
13147 if (mask == 0)
13148 {
13149 EMSG2(_(e_invarg2), flags);
13150 dir = 0;
13151 }
13152 else
13153 *flagsp |= mask;
13154 }
13155 if (dir == 0)
13156 break;
13157 ++flags;
13158 }
13159 }
13160 return dir;
13161}
13162
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013164 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013166 static int
13167search_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013168 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013169 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170{
13171 char_u *pat;
13172 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013173 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 int save_p_ws = p_ws;
13175 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013176 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013177 int retval = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013179 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013180 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13181 if (dir == 0)
13182 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013183 /*
13184 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13185 * Check to make sure only those flags are set.
13186 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13187 * flags cannot be set. Check for that condition also.
13188 */
13189 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13190 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013192 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013193 goto theend;
13194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013196 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13198 SEARCH_KEEP, RE_SEARCH) != FAIL)
13199 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013200 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013201 if (flags & SP_SETPCMARK)
13202 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013204 if (match_pos != NULL)
13205 {
13206 /* Store the match cursor position */
13207 match_pos->lnum = pos.lnum;
13208 match_pos->col = pos.col + 1;
13209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 /* "/$" will put the cursor after the end of the line, may need to
13211 * correct that here */
13212 check_cursor();
13213 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013214
13215 /* If 'n' flag is used: restore cursor position. */
13216 if (flags & SP_NOMOVE)
13217 curwin->w_cursor = save_cursor;
13218theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013220
13221 return retval;
13222}
13223
13224/*
13225 * "search()" function
13226 */
13227 static void
13228f_search(argvars, rettv)
13229 typval_T *argvars;
13230 typval_T *rettv;
13231{
13232 rettv->vval.v_number = search_cmn(argvars, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233}
13234
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013236 * "searchdecl()" function
13237 */
13238 static void
13239f_searchdecl(argvars, rettv)
13240 typval_T *argvars;
13241 typval_T *rettv;
13242{
13243 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013244 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013245 int error = FALSE;
13246 char_u *name;
13247
13248 rettv->vval.v_number = 1; /* default: FAIL */
13249
13250 name = get_tv_string_chk(&argvars[0]);
13251 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013252 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013253 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013254 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13255 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13256 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013257 if (!error && name != NULL)
13258 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013259 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013260}
13261
13262/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013263 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013265 static int
13266searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013267 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013268 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269{
13270 char_u *spat, *mpat, *epat;
13271 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273 int dir;
13274 int flags = 0;
13275 char_u nbuf1[NUMBUFLEN];
13276 char_u nbuf2[NUMBUFLEN];
13277 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013278 int retval = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013281 spat = get_tv_string_chk(&argvars[0]);
13282 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13283 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13284 if (spat == NULL || mpat == NULL || epat == NULL)
13285 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 /* Handle the optional fourth argument: flags */
13288 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013289 if (dir == 0)
13290 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013291 /*
13292 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13293 */
13294 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13295 {
13296 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13297 goto theend;
13298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013300 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013301 if (argvars[3].v_type == VAR_UNKNOWN
13302 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303 skip = (char_u *)"";
13304 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013305 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13306 if (skip == NULL)
13307 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013309 retval = do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013310
13311theend:
13312 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013313
13314 return retval;
13315}
13316
13317/*
13318 * "searchpair()" function
13319 */
13320 static void
13321f_searchpair(argvars, rettv)
13322 typval_T *argvars;
13323 typval_T *rettv;
13324{
13325 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13326}
13327
13328/*
13329 * "searchpairpos()" function
13330 */
13331 static void
13332f_searchpairpos(argvars, rettv)
13333 typval_T *argvars;
13334 typval_T *rettv;
13335{
13336 list_T *l;
13337 pos_T match_pos;
13338 int lnum = 0;
13339 int col = 0;
13340
13341 rettv->vval.v_number = 0;
13342
13343 l = list_alloc();
13344 if (l == NULL)
13345 return;
13346 rettv->v_type = VAR_LIST;
13347 rettv->vval.v_list = l;
13348 ++l->lv_refcount;
13349
13350 if (searchpair_cmn(argvars, &match_pos) > 0)
13351 {
13352 lnum = match_pos.lnum;
13353 col = match_pos.col;
13354 }
13355
13356 list_append_number(l, (varnumber_T)lnum);
13357 list_append_number(l, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013358}
13359
13360/*
13361 * Search for a start/middle/end thing.
13362 * Used by searchpair(), see its documentation for the details.
13363 * Returns 0 or -1 for no match,
13364 */
13365 long
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013366do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013367 char_u *spat; /* start pattern */
13368 char_u *mpat; /* middle pattern */
13369 char_u *epat; /* end pattern */
13370 int dir; /* BACKWARD or FORWARD */
13371 char_u *skip; /* skip expression */
13372 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013373 pos_T *match_pos;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013374{
13375 char_u *save_cpo;
13376 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13377 long retval = 0;
13378 pos_T pos;
13379 pos_T firstpos;
13380 pos_T foundpos;
13381 pos_T save_cursor;
13382 pos_T save_pos;
13383 int n;
13384 int r;
13385 int nest = 1;
13386 int err;
13387
13388 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13389 save_cpo = p_cpo;
13390 p_cpo = (char_u *)"";
13391
13392 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13393 * start/middle/end (pat3, for the top pair). */
13394 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13395 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13396 if (pat2 == NULL || pat3 == NULL)
13397 goto theend;
13398 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13399 if (*mpat == NUL)
13400 STRCPY(pat3, pat2);
13401 else
13402 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13403 spat, epat, mpat);
13404
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405 save_cursor = curwin->w_cursor;
13406 pos = curwin->w_cursor;
13407 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013408 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 pat = pat3;
13410 for (;;)
13411 {
13412 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13413 SEARCH_KEEP, RE_SEARCH);
13414 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13415 /* didn't find it or found the first match again: FAIL */
13416 break;
13417
13418 if (firstpos.lnum == 0)
13419 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013420 if (equalpos(pos, foundpos))
13421 {
13422 /* Found the same position again. Can happen with a pattern that
13423 * has "\zs" at the end and searching backwards. Advance one
13424 * character and try again. */
13425 if (dir == BACKWARD)
13426 decl(&pos);
13427 else
13428 incl(&pos);
13429 }
13430 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431
13432 /* If the skip pattern matches, ignore this match. */
13433 if (*skip != NUL)
13434 {
13435 save_pos = curwin->w_cursor;
13436 curwin->w_cursor = pos;
13437 r = eval_to_bool(skip, &err, NULL, FALSE);
13438 curwin->w_cursor = save_pos;
13439 if (err)
13440 {
13441 /* Evaluating {skip} caused an error, break here. */
13442 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013443 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444 break;
13445 }
13446 if (r)
13447 continue;
13448 }
13449
13450 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13451 {
13452 /* Found end when searching backwards or start when searching
13453 * forward: nested pair. */
13454 ++nest;
13455 pat = pat2; /* nested, don't search for middle */
13456 }
13457 else
13458 {
13459 /* Found end when searching forward or start when searching
13460 * backward: end of (nested) pair; or found middle in outer pair. */
13461 if (--nest == 1)
13462 pat = pat3; /* outer level, search for middle */
13463 }
13464
13465 if (nest == 0)
13466 {
13467 /* Found the match: return matchcount or line number. */
13468 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013469 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013471 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013472 if (flags & SP_SETPCMARK)
13473 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 curwin->w_cursor = pos;
13475 if (!(flags & SP_REPEAT))
13476 break;
13477 nest = 1; /* search for next unmatched */
13478 }
13479 }
13480
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013481 if (match_pos != NULL)
13482 {
13483 /* Store the match cursor position */
13484 match_pos->lnum = curwin->w_cursor.lnum;
13485 match_pos->col = curwin->w_cursor.col + 1;
13486 }
13487
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013489 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490 curwin->w_cursor = save_cursor;
13491
13492theend:
13493 vim_free(pat2);
13494 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013496
13497 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498}
13499
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013500/*
13501 * "searchpos()" function
13502 */
13503 static void
13504f_searchpos(argvars, rettv)
13505 typval_T *argvars;
13506 typval_T *rettv;
13507{
13508 list_T *l;
13509 pos_T match_pos;
13510 int lnum = 0;
13511 int col = 0;
13512
13513 rettv->vval.v_number = 0;
13514
13515 l = list_alloc();
13516 if (l == NULL)
13517 return;
13518 rettv->v_type = VAR_LIST;
13519 rettv->vval.v_list = l;
13520 ++l->lv_refcount;
13521
13522 if (search_cmn(argvars, &match_pos) > 0)
13523 {
13524 lnum = match_pos.lnum;
13525 col = match_pos.col;
13526 }
13527
13528 list_append_number(l, (varnumber_T)lnum);
13529 list_append_number(l, (varnumber_T)col);
13530
13531}
13532
13533
Bram Moolenaar0d660222005-01-07 21:51:51 +000013534/*ARGSUSED*/
13535 static void
13536f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013537 typval_T *argvars;
13538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013540#ifdef FEAT_CLIENTSERVER
13541 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013542 char_u *server = get_tv_string_chk(&argvars[0]);
13543 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544
Bram Moolenaar0d660222005-01-07 21:51:51 +000013545 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013546 if (server == NULL || reply == NULL)
13547 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013548 if (check_restricted() || check_secure())
13549 return;
13550# ifdef FEAT_X11
13551 if (check_connection() == FAIL)
13552 return;
13553# endif
13554
13555 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013557 EMSG(_("E258: Unable to send to client"));
13558 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013560 rettv->vval.v_number = 0;
13561#else
13562 rettv->vval.v_number = -1;
13563#endif
13564}
13565
13566/*ARGSUSED*/
13567 static void
13568f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 typval_T *argvars;
13570 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013571{
13572 char_u *r = NULL;
13573
13574#ifdef FEAT_CLIENTSERVER
13575# ifdef WIN32
13576 r = serverGetVimNames();
13577# else
13578 make_connection();
13579 if (X_DISPLAY != NULL)
13580 r = serverGetVimNames(X_DISPLAY);
13581# endif
13582#endif
13583 rettv->v_type = VAR_STRING;
13584 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585}
13586
13587/*
13588 * "setbufvar()" function
13589 */
13590/*ARGSUSED*/
13591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013593 typval_T *argvars;
13594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595{
13596 buf_T *buf;
13597#ifdef FEAT_AUTOCMD
13598 aco_save_T aco;
13599#else
13600 buf_T *save_curbuf;
13601#endif
13602 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013603 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604 char_u nbuf[NUMBUFLEN];
13605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013606 rettv->vval.v_number = 0;
13607
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 if (check_restricted() || check_secure())
13609 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013610 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13611 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013612 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 varp = &argvars[2];
13614
13615 if (buf != NULL && varname != NULL && varp != NULL)
13616 {
13617 /* set curbuf to be our buf, temporarily */
13618#ifdef FEAT_AUTOCMD
13619 aucmd_prepbuf(&aco, buf);
13620#else
13621 save_curbuf = curbuf;
13622 curbuf = buf;
13623#endif
13624
13625 if (*varname == '&')
13626 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013627 long numval;
13628 char_u *strval;
13629 int error = FALSE;
13630
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013632 numval = get_tv_number_chk(varp, &error);
13633 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013634 if (!error && strval != NULL)
13635 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 }
13637 else
13638 {
13639 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13640 if (bufvarname != NULL)
13641 {
13642 STRCPY(bufvarname, "b:");
13643 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013644 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 vim_free(bufvarname);
13646 }
13647 }
13648
13649 /* reset notion of buffer */
13650#ifdef FEAT_AUTOCMD
13651 aucmd_restbuf(&aco);
13652#else
13653 curbuf = save_curbuf;
13654#endif
13655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656}
13657
13658/*
13659 * "setcmdpos()" function
13660 */
13661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013662f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013663 typval_T *argvars;
13664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013666 int pos = (int)get_tv_number(&argvars[0]) - 1;
13667
13668 if (pos >= 0)
13669 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670}
13671
13672/*
13673 * "setline()" function
13674 */
13675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013676f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013677 typval_T *argvars;
13678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679{
13680 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013681 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013682 list_T *l = NULL;
13683 listitem_T *li = NULL;
13684 long added = 0;
13685 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013687 lnum = get_tv_lnum(&argvars[0]);
13688 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013690 l = argvars[1].vval.v_list;
13691 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013693 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013694 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013695
13696 rettv->vval.v_number = 0; /* OK */
13697 for (;;)
13698 {
13699 if (l != NULL)
13700 {
13701 /* list argument, get next string */
13702 if (li == NULL)
13703 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013704 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013705 li = li->li_next;
13706 }
13707
13708 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013709 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013710 break;
13711 if (lnum <= curbuf->b_ml.ml_line_count)
13712 {
13713 /* existing line, replace it */
13714 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13715 {
13716 changed_bytes(lnum, 0);
13717 check_cursor_col();
13718 rettv->vval.v_number = 0; /* OK */
13719 }
13720 }
13721 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13722 {
13723 /* lnum is one past the last line, append the line */
13724 ++added;
13725 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13726 rettv->vval.v_number = 0; /* OK */
13727 }
13728
13729 if (l == NULL) /* only one string argument */
13730 break;
13731 ++lnum;
13732 }
13733
13734 if (added > 0)
13735 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736}
13737
13738/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013739 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013740 */
13741/*ARGSUSED*/
13742 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013743set_qf_ll_list(wp, list_arg, action_arg, rettv)
13744 win_T *wp;
13745 typval_T *list_arg;
13746 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013747 typval_T *rettv;
13748{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013749#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013750 char_u *act;
13751 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013752#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013753
Bram Moolenaar2641f772005-03-25 21:58:17 +000013754 rettv->vval.v_number = -1;
13755
13756#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013757 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013758 EMSG(_(e_listreq));
13759 else
13760 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013761 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013762
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013763 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013764 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013765 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013766 if (act == NULL)
13767 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013768 if (*act == 'a' || *act == 'r')
13769 action = *act;
13770 }
13771
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013772 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013773 rettv->vval.v_number = 0;
13774 }
13775#endif
13776}
13777
13778/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013779 * "setloclist()" function
13780 */
13781/*ARGSUSED*/
13782 static void
13783f_setloclist(argvars, rettv)
13784 typval_T *argvars;
13785 typval_T *rettv;
13786{
13787 win_T *win;
13788
13789 rettv->vval.v_number = -1;
13790
13791 win = find_win_by_nr(&argvars[0]);
13792 if (win != NULL)
13793 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13794}
13795
13796/*
13797 * "setqflist()" function
13798 */
13799/*ARGSUSED*/
13800 static void
13801f_setqflist(argvars, rettv)
13802 typval_T *argvars;
13803 typval_T *rettv;
13804{
13805 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
13806}
13807
13808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809 * "setreg()" function
13810 */
13811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013812f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013813 typval_T *argvars;
13814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815{
13816 int regname;
13817 char_u *strregname;
13818 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013819 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 int append;
13821 char_u yank_type;
13822 long block_len;
13823
13824 block_len = -1;
13825 yank_type = MAUTO;
13826 append = FALSE;
13827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013828 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013829 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013831 if (strregname == NULL)
13832 return; /* type error; errmsg already given */
13833 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 if (regname == 0 || regname == '@')
13835 regname = '"';
13836 else if (regname == '=')
13837 return;
13838
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013839 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013841 stropt = get_tv_string_chk(&argvars[2]);
13842 if (stropt == NULL)
13843 return; /* type error */
13844 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845 switch (*stropt)
13846 {
13847 case 'a': case 'A': /* append */
13848 append = TRUE;
13849 break;
13850 case 'v': case 'c': /* character-wise selection */
13851 yank_type = MCHAR;
13852 break;
13853 case 'V': case 'l': /* line-wise selection */
13854 yank_type = MLINE;
13855 break;
13856#ifdef FEAT_VISUAL
13857 case 'b': case Ctrl_V: /* block-wise selection */
13858 yank_type = MBLOCK;
13859 if (VIM_ISDIGIT(stropt[1]))
13860 {
13861 ++stropt;
13862 block_len = getdigits(&stropt) - 1;
13863 --stropt;
13864 }
13865 break;
13866#endif
13867 }
13868 }
13869
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013870 strval = get_tv_string_chk(&argvars[1]);
13871 if (strval != NULL)
13872 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013874 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875}
13876
13877
13878/*
13879 * "setwinvar(expr)" function
13880 */
13881/*ARGSUSED*/
13882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013883f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013884 typval_T *argvars;
13885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886{
13887 win_T *win;
13888#ifdef FEAT_WINDOWS
13889 win_T *save_curwin;
13890#endif
13891 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013892 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893 char_u nbuf[NUMBUFLEN];
13894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013895 rettv->vval.v_number = 0;
13896
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897 if (check_restricted() || check_secure())
13898 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013900 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 varp = &argvars[2];
13902
13903 if (win != NULL && varname != NULL && varp != NULL)
13904 {
13905#ifdef FEAT_WINDOWS
13906 /* set curwin to be our win, temporarily */
13907 save_curwin = curwin;
13908 curwin = win;
13909 curbuf = curwin->w_buffer;
13910#endif
13911
13912 if (*varname == '&')
13913 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013914 long numval;
13915 char_u *strval;
13916 int error = FALSE;
13917
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013919 numval = get_tv_number_chk(varp, &error);
13920 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013921 if (!error && strval != NULL)
13922 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923 }
13924 else
13925 {
13926 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13927 if (winvarname != NULL)
13928 {
13929 STRCPY(winvarname, "w:");
13930 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013931 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932 vim_free(winvarname);
13933 }
13934 }
13935
13936#ifdef FEAT_WINDOWS
13937 /* Restore current window, if it's still valid (autocomands can make
13938 * it invalid). */
13939 if (win_valid(save_curwin))
13940 {
13941 curwin = save_curwin;
13942 curbuf = curwin->w_buffer;
13943 }
13944#endif
13945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946}
13947
13948/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013949 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 */
13951 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013952f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013953 typval_T *argvars;
13954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013956 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957
Bram Moolenaar0d660222005-01-07 21:51:51 +000013958 p = get_tv_string(&argvars[0]);
13959 rettv->vval.v_string = vim_strsave(p);
13960 simplify_filename(rettv->vval.v_string); /* simplify in place */
13961 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962}
13963
Bram Moolenaar0d660222005-01-07 21:51:51 +000013964static int
13965#ifdef __BORLANDC__
13966 _RTLENTRYF
13967#endif
13968 item_compare __ARGS((const void *s1, const void *s2));
13969static int
13970#ifdef __BORLANDC__
13971 _RTLENTRYF
13972#endif
13973 item_compare2 __ARGS((const void *s1, const void *s2));
13974
13975static int item_compare_ic;
13976static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013977static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013978#define ITEM_COMPARE_FAIL 999
13979
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013981 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013983 static int
13984#ifdef __BORLANDC__
13985_RTLENTRYF
13986#endif
13987item_compare(s1, s2)
13988 const void *s1;
13989 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013991 char_u *p1, *p2;
13992 char_u *tofree1, *tofree2;
13993 int res;
13994 char_u numbuf1[NUMBUFLEN];
13995 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013997 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
13998 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013999 if (item_compare_ic)
14000 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014002 res = STRCMP(p1, p2);
14003 vim_free(tofree1);
14004 vim_free(tofree2);
14005 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006}
14007
14008 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014009#ifdef __BORLANDC__
14010_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014012item_compare2(s1, s2)
14013 const void *s1;
14014 const void *s2;
14015{
14016 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014017 typval_T rettv;
14018 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014019 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014021 /* shortcut after failure in previous call; compare all items equal */
14022 if (item_compare_func_err)
14023 return 0;
14024
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014025 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14026 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014027 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14028 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014029
14030 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14031 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014032 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014033 clear_tv(&argv[0]);
14034 clear_tv(&argv[1]);
14035
14036 if (res == FAIL)
14037 res = ITEM_COMPARE_FAIL;
14038 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014039 /* return value has wrong type */
14040 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14041 if (item_compare_func_err)
14042 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014043 clear_tv(&rettv);
14044 return res;
14045}
14046
14047/*
14048 * "sort({list})" function
14049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014051f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014052 typval_T *argvars;
14053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054{
Bram Moolenaar33570922005-01-25 22:26:29 +000014055 list_T *l;
14056 listitem_T *li;
14057 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014058 long len;
14059 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060
Bram Moolenaar0d660222005-01-07 21:51:51 +000014061 rettv->vval.v_number = 0;
14062 if (argvars[0].v_type != VAR_LIST)
14063 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064 else
14065 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014066 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014067 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014068 return;
14069 rettv->vval.v_list = l;
14070 rettv->v_type = VAR_LIST;
14071 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072
Bram Moolenaar0d660222005-01-07 21:51:51 +000014073 len = list_len(l);
14074 if (len <= 1)
14075 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076
Bram Moolenaar0d660222005-01-07 21:51:51 +000014077 item_compare_ic = FALSE;
14078 item_compare_func = NULL;
14079 if (argvars[1].v_type != VAR_UNKNOWN)
14080 {
14081 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014082 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014083 else
14084 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014085 int error = FALSE;
14086
14087 i = get_tv_number_chk(&argvars[1], &error);
14088 if (error)
14089 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014090 if (i == 1)
14091 item_compare_ic = TRUE;
14092 else
14093 item_compare_func = get_tv_string(&argvars[1]);
14094 }
14095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096
Bram Moolenaar0d660222005-01-07 21:51:51 +000014097 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014098 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014099 if (ptrs == NULL)
14100 return;
14101 i = 0;
14102 for (li = l->lv_first; li != NULL; li = li->li_next)
14103 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014105 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014106 /* test the compare function */
14107 if (item_compare_func != NULL
14108 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14109 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014110 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014112 {
14113 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014114 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014115 item_compare_func == NULL ? item_compare : item_compare2);
14116
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014117 if (!item_compare_func_err)
14118 {
14119 /* Clear the List and append the items in the sorted order. */
14120 l->lv_first = l->lv_last = NULL;
14121 l->lv_len = 0;
14122 for (i = 0; i < len; ++i)
14123 list_append(l, ptrs[i]);
14124 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014125 }
14126
14127 vim_free(ptrs);
14128 }
14129}
14130
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014131/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014132 * "soundfold({word})" function
14133 */
14134 static void
14135f_soundfold(argvars, rettv)
14136 typval_T *argvars;
14137 typval_T *rettv;
14138{
14139 char_u *s;
14140
14141 rettv->v_type = VAR_STRING;
14142 s = get_tv_string(&argvars[0]);
14143#ifdef FEAT_SYN_HL
14144 rettv->vval.v_string = eval_soundfold(s);
14145#else
14146 rettv->vval.v_string = vim_strsave(s);
14147#endif
14148}
14149
14150/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014151 * "spellbadword()" function
14152 */
14153/* ARGSUSED */
14154 static void
14155f_spellbadword(argvars, rettv)
14156 typval_T *argvars;
14157 typval_T *rettv;
14158{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014159 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014160 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014161 int len = 0;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014162 list_T *l;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014163
Bram Moolenaar4463f292005-09-25 22:20:24 +000014164 l = list_alloc();
14165 if (l == NULL)
14166 return;
14167 rettv->v_type = VAR_LIST;
14168 rettv->vval.v_list = l;
14169 ++l->lv_refcount;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014170
14171#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014172 if (argvars[0].v_type == VAR_UNKNOWN)
14173 {
14174 /* Find the start and length of the badly spelled word. */
14175 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14176 if (len != 0)
14177 word = ml_get_cursor();
14178 }
14179 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14180 {
14181 char_u *str = get_tv_string_chk(&argvars[0]);
14182 int capcol = -1;
14183
14184 if (str != NULL)
14185 {
14186 /* Check the argument for spelling. */
14187 while (*str != NUL)
14188 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014189 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014190 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014191 {
14192 word = str;
14193 break;
14194 }
14195 str += len;
14196 }
14197 }
14198 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014199#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014200
14201 list_append_string(l, word, len);
14202 list_append_string(l, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014203 attr == HLF_SPB ? "bad" :
14204 attr == HLF_SPR ? "rare" :
14205 attr == HLF_SPL ? "local" :
14206 attr == HLF_SPC ? "caps" :
14207 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014208}
14209
14210/*
14211 * "spellsuggest()" function
14212 */
14213 static void
14214f_spellsuggest(argvars, rettv)
14215 typval_T *argvars;
14216 typval_T *rettv;
14217{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014218 list_T *l;
14219#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014220 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014221 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014222 int maxcount;
14223 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014224 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014225 listitem_T *li;
14226 int need_capital = FALSE;
14227#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014228
14229 l = list_alloc();
14230 if (l == NULL)
14231 return;
14232 rettv->v_type = VAR_LIST;
14233 rettv->vval.v_list = l;
14234 ++l->lv_refcount;
14235
14236#ifdef FEAT_SYN_HL
14237 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14238 {
14239 str = get_tv_string(&argvars[0]);
14240 if (argvars[1].v_type != VAR_UNKNOWN)
14241 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014242 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014243 if (maxcount <= 0)
14244 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014245 if (argvars[2].v_type != VAR_UNKNOWN)
14246 {
14247 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14248 if (typeerr)
14249 return;
14250 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014251 }
14252 else
14253 maxcount = 25;
14254
Bram Moolenaar4770d092006-01-12 23:22:24 +000014255 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014256
14257 for (i = 0; i < ga.ga_len; ++i)
14258 {
14259 str = ((char_u **)ga.ga_data)[i];
14260
14261 li = listitem_alloc();
14262 if (li == NULL)
14263 vim_free(str);
14264 else
14265 {
14266 li->li_tv.v_type = VAR_STRING;
14267 li->li_tv.v_lock = 0;
14268 li->li_tv.vval.v_string = str;
14269 list_append(l, li);
14270 }
14271 }
14272 ga_clear(&ga);
14273 }
14274#endif
14275}
14276
Bram Moolenaar0d660222005-01-07 21:51:51 +000014277 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014278f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014279 typval_T *argvars;
14280 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014281{
14282 char_u *str;
14283 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014284 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014285 regmatch_T regmatch;
14286 char_u patbuf[NUMBUFLEN];
14287 char_u *save_cpo;
14288 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000014289 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014290 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014291 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014292 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014293
14294 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14295 save_cpo = p_cpo;
14296 p_cpo = (char_u *)"";
14297
14298 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014299 if (argvars[1].v_type != VAR_UNKNOWN)
14300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014301 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14302 if (pat == NULL)
14303 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014304 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014305 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014306 }
14307 if (pat == NULL || *pat == NUL)
14308 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014309
14310 l = list_alloc();
14311 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014313 rettv->v_type = VAR_LIST;
14314 rettv->vval.v_list = l;
14315 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014316 if (typeerr)
14317 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318
Bram Moolenaar0d660222005-01-07 21:51:51 +000014319 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14320 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014322 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014323 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014324 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014325 if (*str == NUL)
14326 match = FALSE; /* empty item at the end */
14327 else
14328 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014329 if (match)
14330 end = regmatch.startp[0];
14331 else
14332 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014333 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
14334 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014335 {
Bram Moolenaar4463f292005-09-25 22:20:24 +000014336 if (list_append_string(l, str, (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014337 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014338 }
14339 if (!match)
14340 break;
14341 /* Advance to just after the match. */
14342 if (regmatch.endp[0] > str)
14343 col = 0;
14344 else
14345 {
14346 /* Don't get stuck at the same match. */
14347#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014348 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014349#else
14350 col = 1;
14351#endif
14352 }
14353 str = regmatch.endp[0];
14354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355
Bram Moolenaar0d660222005-01-07 21:51:51 +000014356 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358
Bram Moolenaar0d660222005-01-07 21:51:51 +000014359 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360}
14361
14362#ifdef HAVE_STRFTIME
14363/*
14364 * "strftime({format}[, {time}])" function
14365 */
14366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014367f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014368 typval_T *argvars;
14369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370{
14371 char_u result_buf[256];
14372 struct tm *curtime;
14373 time_t seconds;
14374 char_u *p;
14375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014378 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014379 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 seconds = time(NULL);
14381 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014382 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 curtime = localtime(&seconds);
14384 /* MSVC returns NULL for an invalid value of seconds. */
14385 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014386 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 else
14388 {
14389# ifdef FEAT_MBYTE
14390 vimconv_T conv;
14391 char_u *enc;
14392
14393 conv.vc_type = CONV_NONE;
14394 enc = enc_locale();
14395 convert_setup(&conv, p_enc, enc);
14396 if (conv.vc_type != CONV_NONE)
14397 p = string_convert(&conv, p, NULL);
14398# endif
14399 if (p != NULL)
14400 (void)strftime((char *)result_buf, sizeof(result_buf),
14401 (char *)p, curtime);
14402 else
14403 result_buf[0] = NUL;
14404
14405# ifdef FEAT_MBYTE
14406 if (conv.vc_type != CONV_NONE)
14407 vim_free(p);
14408 convert_setup(&conv, enc, p_enc);
14409 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014410 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411 else
14412# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014413 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414
14415# ifdef FEAT_MBYTE
14416 /* Release conversion descriptors */
14417 convert_setup(&conv, NULL, NULL);
14418 vim_free(enc);
14419# endif
14420 }
14421}
14422#endif
14423
14424/*
14425 * "stridx()" function
14426 */
14427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014428f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014429 typval_T *argvars;
14430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431{
14432 char_u buf[NUMBUFLEN];
14433 char_u *needle;
14434 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014435 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014437 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014439 needle = get_tv_string_chk(&argvars[1]);
14440 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014441 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014442 if (needle == NULL || haystack == NULL)
14443 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444
Bram Moolenaar33570922005-01-25 22:26:29 +000014445 if (argvars[2].v_type != VAR_UNKNOWN)
14446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014447 int error = FALSE;
14448
14449 start_idx = get_tv_number_chk(&argvars[2], &error);
14450 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014451 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014452 if (start_idx >= 0)
14453 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014454 }
14455
14456 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14457 if (pos != NULL)
14458 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459}
14460
14461/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014462 * "string()" function
14463 */
14464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014465f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014466 typval_T *argvars;
14467 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014468{
14469 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014470 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014472 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014473 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014474 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476}
14477
14478/*
14479 * "strlen()" function
14480 */
14481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014482f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014483 typval_T *argvars;
14484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014486 rettv->vval.v_number = (varnumber_T)(STRLEN(
14487 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488}
14489
14490/*
14491 * "strpart()" function
14492 */
14493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014494f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014495 typval_T *argvars;
14496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497{
14498 char_u *p;
14499 int n;
14500 int len;
14501 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014502 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014504 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505 slen = (int)STRLEN(p);
14506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014507 n = get_tv_number_chk(&argvars[1], &error);
14508 if (error)
14509 len = 0;
14510 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014511 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 else
14513 len = slen - n; /* default len: all bytes that are available. */
14514
14515 /*
14516 * Only return the overlap between the specified part and the actual
14517 * string.
14518 */
14519 if (n < 0)
14520 {
14521 len += n;
14522 n = 0;
14523 }
14524 else if (n > slen)
14525 n = slen;
14526 if (len < 0)
14527 len = 0;
14528 else if (n + len > slen)
14529 len = slen - n;
14530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014531 rettv->v_type = VAR_STRING;
14532 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533}
14534
14535/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014536 * "strridx()" function
14537 */
14538 static void
14539f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014540 typval_T *argvars;
14541 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014542{
14543 char_u buf[NUMBUFLEN];
14544 char_u *needle;
14545 char_u *haystack;
14546 char_u *rest;
14547 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014548 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014550 needle = get_tv_string_chk(&argvars[1]);
14551 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014552 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014553
14554 rettv->vval.v_number = -1;
14555 if (needle == NULL || haystack == NULL)
14556 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014557 if (argvars[2].v_type != VAR_UNKNOWN)
14558 {
14559 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014560 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014561 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014562 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014563 }
14564 else
14565 end_idx = haystack_len;
14566
Bram Moolenaar0d660222005-01-07 21:51:51 +000014567 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014568 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014569 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014570 lastmatch = haystack + end_idx;
14571 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014572 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014573 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014574 for (rest = haystack; *rest != '\0'; ++rest)
14575 {
14576 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014577 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014578 break;
14579 lastmatch = rest;
14580 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014581 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014582
14583 if (lastmatch == NULL)
14584 rettv->vval.v_number = -1;
14585 else
14586 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14587}
14588
14589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590 * "strtrans()" function
14591 */
14592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014593f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014594 typval_T *argvars;
14595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014597 rettv->v_type = VAR_STRING;
14598 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599}
14600
14601/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014602 * "submatch()" function
14603 */
14604 static void
14605f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014606 typval_T *argvars;
14607 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014608{
14609 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014610 rettv->vval.v_string =
14611 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014612}
14613
14614/*
14615 * "substitute()" function
14616 */
14617 static void
14618f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014619 typval_T *argvars;
14620 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014621{
14622 char_u patbuf[NUMBUFLEN];
14623 char_u subbuf[NUMBUFLEN];
14624 char_u flagsbuf[NUMBUFLEN];
14625
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014626 char_u *str = get_tv_string_chk(&argvars[0]);
14627 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14628 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14629 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14630
Bram Moolenaar0d660222005-01-07 21:51:51 +000014631 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014632 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14633 rettv->vval.v_string = NULL;
14634 else
14635 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014636}
14637
14638/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014639 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 */
14641/*ARGSUSED*/
14642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014643f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014644 typval_T *argvars;
14645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646{
14647 int id = 0;
14648#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014649 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650 long col;
14651 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014652 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014654 lnum = get_tv_lnum(argvars); /* -1 on type error */
14655 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14656 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014658 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014659 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014660 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661#endif
14662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014663 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664}
14665
14666/*
14667 * "synIDattr(id, what [, mode])" function
14668 */
14669/*ARGSUSED*/
14670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014671f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014672 typval_T *argvars;
14673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674{
14675 char_u *p = NULL;
14676#ifdef FEAT_SYN_HL
14677 int id;
14678 char_u *what;
14679 char_u *mode;
14680 char_u modebuf[NUMBUFLEN];
14681 int modec;
14682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014683 id = get_tv_number(&argvars[0]);
14684 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014685 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688 modec = TOLOWER_ASC(mode[0]);
14689 if (modec != 't' && modec != 'c'
14690#ifdef FEAT_GUI
14691 && modec != 'g'
14692#endif
14693 )
14694 modec = 0; /* replace invalid with current */
14695 }
14696 else
14697 {
14698#ifdef FEAT_GUI
14699 if (gui.in_use)
14700 modec = 'g';
14701 else
14702#endif
14703 if (t_colors > 1)
14704 modec = 'c';
14705 else
14706 modec = 't';
14707 }
14708
14709
14710 switch (TOLOWER_ASC(what[0]))
14711 {
14712 case 'b':
14713 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14714 p = highlight_color(id, what, modec);
14715 else /* bold */
14716 p = highlight_has_attr(id, HL_BOLD, modec);
14717 break;
14718
14719 case 'f': /* fg[#] */
14720 p = highlight_color(id, what, modec);
14721 break;
14722
14723 case 'i':
14724 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14725 p = highlight_has_attr(id, HL_INVERSE, modec);
14726 else /* italic */
14727 p = highlight_has_attr(id, HL_ITALIC, modec);
14728 break;
14729
14730 case 'n': /* name */
14731 p = get_highlight_name(NULL, id - 1);
14732 break;
14733
14734 case 'r': /* reverse */
14735 p = highlight_has_attr(id, HL_INVERSE, modec);
14736 break;
14737
14738 case 's': /* standout */
14739 p = highlight_has_attr(id, HL_STANDOUT, modec);
14740 break;
14741
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014742 case 'u':
14743 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14744 /* underline */
14745 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14746 else
14747 /* undercurl */
14748 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 break;
14750 }
14751
14752 if (p != NULL)
14753 p = vim_strsave(p);
14754#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014755 rettv->v_type = VAR_STRING;
14756 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757}
14758
14759/*
14760 * "synIDtrans(id)" function
14761 */
14762/*ARGSUSED*/
14763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014764f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014765 typval_T *argvars;
14766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767{
14768 int id;
14769
14770#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014771 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014772
14773 if (id > 0)
14774 id = syn_get_final_id(id);
14775 else
14776#endif
14777 id = 0;
14778
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014779 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014780}
14781
14782/*
14783 * "system()" function
14784 */
14785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014786f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014787 typval_T *argvars;
14788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014790 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014792 char_u *infile = NULL;
14793 char_u buf[NUMBUFLEN];
14794 int err = FALSE;
14795 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014797 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014798 {
14799 /*
14800 * Write the string to a temp file, to be used for input of the shell
14801 * command.
14802 */
14803 if ((infile = vim_tempname('i')) == NULL)
14804 {
14805 EMSG(_(e_notmp));
14806 return;
14807 }
14808
14809 fd = mch_fopen((char *)infile, WRITEBIN);
14810 if (fd == NULL)
14811 {
14812 EMSG2(_(e_notopen), infile);
14813 goto done;
14814 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014815 p = get_tv_string_buf_chk(&argvars[1], buf);
14816 if (p == NULL)
14817 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014818 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14819 err = TRUE;
14820 if (fclose(fd) != 0)
14821 err = TRUE;
14822 if (err)
14823 {
14824 EMSG(_("E677: Error writing temp file"));
14825 goto done;
14826 }
14827 }
14828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014829 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014830
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831#ifdef USE_CR
14832 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014833 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 {
14835 char_u *s;
14836
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014837 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 {
14839 if (*s == CAR)
14840 *s = NL;
14841 }
14842 }
14843#else
14844# ifdef USE_CRNL
14845 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014846 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847 {
14848 char_u *s, *d;
14849
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014850 d = res;
14851 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 {
14853 if (s[0] == CAR && s[1] == NL)
14854 ++s;
14855 *d++ = *s;
14856 }
14857 *d = NUL;
14858 }
14859# endif
14860#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014861
14862done:
14863 if (infile != NULL)
14864 {
14865 mch_remove(infile);
14866 vim_free(infile);
14867 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014868 rettv->v_type = VAR_STRING;
14869 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870}
14871
14872/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000014873 * "tabpagenr()" function
14874 */
14875/* ARGSUSED */
14876 static void
14877f_tabpagenr(argvars, rettv)
14878 typval_T *argvars;
14879 typval_T *rettv;
14880{
14881 int nr = 1;
14882#ifdef FEAT_WINDOWS
14883 tabpage_T *tp;
14884 char_u *arg;
14885
14886 if (argvars[0].v_type != VAR_UNKNOWN)
14887 {
14888 arg = get_tv_string_chk(&argvars[0]);
14889 nr = 0;
14890 if (arg != NULL)
14891 {
14892 if (STRCMP(arg, "$") == 0)
14893 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
14894 ++nr;
14895 else
14896 EMSG2(_(e_invexpr2), arg);
14897 }
14898 }
14899 else
14900 for (tp = first_tabpage; tp != curtab; tp = tp->tp_next)
14901 ++nr;
14902#endif
14903 rettv->vval.v_number = nr;
14904}
14905
14906/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014907 * "tagfiles()" function
14908 */
14909/*ARGSUSED*/
14910 static void
14911f_tagfiles(argvars, rettv)
14912 typval_T *argvars;
14913 typval_T *rettv;
14914{
14915 char_u fname[MAXPATHL + 1];
14916 list_T *l;
14917
14918 l = list_alloc();
14919 if (l == NULL)
14920 {
14921 rettv->vval.v_number = 0;
14922 return;
14923 }
14924 rettv->vval.v_list = l;
14925 rettv->v_type = VAR_LIST;
14926 ++l->lv_refcount;
14927
14928 get_tagfname(TRUE, NULL);
14929 for (;;)
14930 if (get_tagfname(FALSE, fname) == FAIL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014931 || list_append_string(l, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014932 break;
14933}
14934
14935/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014936 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014937 */
14938 static void
14939f_taglist(argvars, rettv)
14940 typval_T *argvars;
14941 typval_T *rettv;
14942{
14943 char_u *tag_pattern;
14944 list_T *l;
14945
14946 tag_pattern = get_tv_string(&argvars[0]);
14947
14948 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014949 if (*tag_pattern == NUL)
14950 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014951
14952 l = list_alloc();
14953 if (l != NULL)
14954 {
14955 if (get_tags(l, tag_pattern) != FAIL)
14956 {
14957 rettv->vval.v_list = l;
14958 rettv->v_type = VAR_LIST;
14959 ++l->lv_refcount;
14960 }
14961 else
14962 list_free(l);
14963 }
14964}
14965
14966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967 * "tempname()" function
14968 */
14969/*ARGSUSED*/
14970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014971f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014972 typval_T *argvars;
14973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974{
14975 static int x = 'A';
14976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014977 rettv->v_type = VAR_STRING;
14978 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979
14980 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14981 * names. Skip 'I' and 'O', they are used for shell redirection. */
14982 do
14983 {
14984 if (x == 'Z')
14985 x = '0';
14986 else if (x == '9')
14987 x = 'A';
14988 else
14989 {
14990#ifdef EBCDIC
14991 if (x == 'I')
14992 x = 'J';
14993 else if (x == 'R')
14994 x = 'S';
14995 else
14996#endif
14997 ++x;
14998 }
14999 } while (x == 'I' || x == 'O');
15000}
15001
15002/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015003 * "test(list)" function: Just checking the walls...
15004 */
15005/*ARGSUSED*/
15006 static void
15007f_test(argvars, rettv)
15008 typval_T *argvars;
15009 typval_T *rettv;
15010{
15011 /* Used for unit testing. Change the code below to your liking. */
15012#if 0
15013 listitem_T *li;
15014 list_T *l;
15015 char_u *bad, *good;
15016
15017 if (argvars[0].v_type != VAR_LIST)
15018 return;
15019 l = argvars[0].vval.v_list;
15020 if (l == NULL)
15021 return;
15022 li = l->lv_first;
15023 if (li == NULL)
15024 return;
15025 bad = get_tv_string(&li->li_tv);
15026 li = li->li_next;
15027 if (li == NULL)
15028 return;
15029 good = get_tv_string(&li->li_tv);
15030 rettv->vval.v_number = test_edit_score(bad, good);
15031#endif
15032}
15033
15034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035 * "tolower(string)" function
15036 */
15037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015038f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015039 typval_T *argvars;
15040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041{
15042 char_u *p;
15043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015044 p = vim_strsave(get_tv_string(&argvars[0]));
15045 rettv->v_type = VAR_STRING;
15046 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047
15048 if (p != NULL)
15049 while (*p != NUL)
15050 {
15051#ifdef FEAT_MBYTE
15052 int l;
15053
15054 if (enc_utf8)
15055 {
15056 int c, lc;
15057
15058 c = utf_ptr2char(p);
15059 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015060 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061 /* TODO: reallocate string when byte count changes. */
15062 if (utf_char2len(lc) == l)
15063 utf_char2bytes(lc, p);
15064 p += l;
15065 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015066 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015067 p += l; /* skip multi-byte character */
15068 else
15069#endif
15070 {
15071 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15072 ++p;
15073 }
15074 }
15075}
15076
15077/*
15078 * "toupper(string)" function
15079 */
15080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015081f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015082 typval_T *argvars;
15083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015085 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015086 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087}
15088
15089/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015090 * "tr(string, fromstr, tostr)" function
15091 */
15092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015093f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015094 typval_T *argvars;
15095 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015096{
15097 char_u *instr;
15098 char_u *fromstr;
15099 char_u *tostr;
15100 char_u *p;
15101#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015102 int inlen;
15103 int fromlen;
15104 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015105 int idx;
15106 char_u *cpstr;
15107 int cplen;
15108 int first = TRUE;
15109#endif
15110 char_u buf[NUMBUFLEN];
15111 char_u buf2[NUMBUFLEN];
15112 garray_T ga;
15113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015114 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015115 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15116 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015117
15118 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015119 rettv->v_type = VAR_STRING;
15120 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015121 if (fromstr == NULL || tostr == NULL)
15122 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015123 ga_init2(&ga, (int)sizeof(char), 80);
15124
15125#ifdef FEAT_MBYTE
15126 if (!has_mbyte)
15127#endif
15128 /* not multi-byte: fromstr and tostr must be the same length */
15129 if (STRLEN(fromstr) != STRLEN(tostr))
15130 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015131#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015132error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015133#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015134 EMSG2(_(e_invarg2), fromstr);
15135 ga_clear(&ga);
15136 return;
15137 }
15138
15139 /* fromstr and tostr have to contain the same number of chars */
15140 while (*instr != NUL)
15141 {
15142#ifdef FEAT_MBYTE
15143 if (has_mbyte)
15144 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015145 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015146 cpstr = instr;
15147 cplen = inlen;
15148 idx = 0;
15149 for (p = fromstr; *p != NUL; p += fromlen)
15150 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015151 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015152 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15153 {
15154 for (p = tostr; *p != NUL; p += tolen)
15155 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015156 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015157 if (idx-- == 0)
15158 {
15159 cplen = tolen;
15160 cpstr = p;
15161 break;
15162 }
15163 }
15164 if (*p == NUL) /* tostr is shorter than fromstr */
15165 goto error;
15166 break;
15167 }
15168 ++idx;
15169 }
15170
15171 if (first && cpstr == instr)
15172 {
15173 /* Check that fromstr and tostr have the same number of
15174 * (multi-byte) characters. Done only once when a character
15175 * of instr doesn't appear in fromstr. */
15176 first = FALSE;
15177 for (p = tostr; *p != NUL; p += tolen)
15178 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015179 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015180 --idx;
15181 }
15182 if (idx != 0)
15183 goto error;
15184 }
15185
15186 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015187 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015188 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015189
15190 instr += inlen;
15191 }
15192 else
15193#endif
15194 {
15195 /* When not using multi-byte chars we can do it faster. */
15196 p = vim_strchr(fromstr, *instr);
15197 if (p != NULL)
15198 ga_append(&ga, tostr[p - fromstr]);
15199 else
15200 ga_append(&ga, *instr);
15201 ++instr;
15202 }
15203 }
15204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015205 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015206}
15207
15208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209 * "type(expr)" function
15210 */
15211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015212f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015213 typval_T *argvars;
15214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015216 int n;
15217
15218 switch (argvars[0].v_type)
15219 {
15220 case VAR_NUMBER: n = 0; break;
15221 case VAR_STRING: n = 1; break;
15222 case VAR_FUNC: n = 2; break;
15223 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015224 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015225 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15226 }
15227 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228}
15229
15230/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015231 * "values(dict)" function
15232 */
15233 static void
15234f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015235 typval_T *argvars;
15236 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015237{
15238 dict_list(argvars, rettv, 1);
15239}
15240
15241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242 * "virtcol(string)" function
15243 */
15244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015245f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015246 typval_T *argvars;
15247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015248{
15249 colnr_T vcol = 0;
15250 pos_T *fp;
15251
15252 fp = var2fpos(&argvars[0], FALSE);
15253 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
15254 {
15255 getvvcol(curwin, fp, NULL, NULL, &vcol);
15256 ++vcol;
15257 }
15258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015259 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260}
15261
15262/*
15263 * "visualmode()" function
15264 */
15265/*ARGSUSED*/
15266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015267f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015268 typval_T *argvars;
15269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270{
15271#ifdef FEAT_VISUAL
15272 char_u str[2];
15273
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015274 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275 str[0] = curbuf->b_visual_mode_eval;
15276 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015277 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278
15279 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015280 if ((argvars[0].v_type == VAR_NUMBER
15281 && argvars[0].vval.v_number != 0)
15282 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015283 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015284 curbuf->b_visual_mode_eval = NUL;
15285#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015286 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287#endif
15288}
15289
15290/*
15291 * "winbufnr(nr)" function
15292 */
15293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015294f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015295 typval_T *argvars;
15296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297{
15298 win_T *wp;
15299
15300 wp = find_win_by_nr(&argvars[0]);
15301 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015302 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015304 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305}
15306
15307/*
15308 * "wincol()" function
15309 */
15310/*ARGSUSED*/
15311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015312f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015313 typval_T *argvars;
15314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315{
15316 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015317 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318}
15319
15320/*
15321 * "winheight(nr)" function
15322 */
15323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015324f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015325 typval_T *argvars;
15326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327{
15328 win_T *wp;
15329
15330 wp = find_win_by_nr(&argvars[0]);
15331 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015332 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015334 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335}
15336
15337/*
15338 * "winline()" function
15339 */
15340/*ARGSUSED*/
15341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015342f_winline(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 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015347 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348}
15349
15350/*
15351 * "winnr()" function
15352 */
15353/* ARGSUSED */
15354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015355f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015356 typval_T *argvars;
15357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358{
15359 int nr = 1;
15360#ifdef FEAT_WINDOWS
15361 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015362 win_T *twin = curwin;
15363 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015365 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015366 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015367 arg = get_tv_string_chk(&argvars[0]);
15368 if (arg == NULL)
15369 nr = 0; /* type error; errmsg already given */
15370 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015371 twin = lastwin;
15372 else if (STRCMP(arg, "#") == 0)
15373 {
15374 twin = prevwin;
15375 if (prevwin == NULL)
15376 nr = 0;
15377 }
15378 else
15379 {
15380 EMSG2(_(e_invexpr2), arg);
15381 nr = 0;
15382 }
15383 }
15384
15385 if (nr > 0)
15386 for (wp = firstwin; wp != twin; wp = wp->w_next)
15387 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015389 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390}
15391
15392/*
15393 * "winrestcmd()" function
15394 */
15395/* ARGSUSED */
15396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015398 typval_T *argvars;
15399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400{
15401#ifdef FEAT_WINDOWS
15402 win_T *wp;
15403 int winnr = 1;
15404 garray_T ga;
15405 char_u buf[50];
15406
15407 ga_init2(&ga, (int)sizeof(char), 70);
15408 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15409 {
15410 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15411 ga_concat(&ga, buf);
15412# ifdef FEAT_VERTSPLIT
15413 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15414 ga_concat(&ga, buf);
15415# endif
15416 ++winnr;
15417 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015418 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015422 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015424 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425}
15426
15427/*
15428 * "winwidth(nr)" function
15429 */
15430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015431f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015432 typval_T *argvars;
15433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434{
15435 win_T *wp;
15436
15437 wp = find_win_by_nr(&argvars[0]);
15438 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015439 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 else
15441#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015442 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015444 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445#endif
15446}
15447
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015449 * "writefile()" function
15450 */
15451 static void
15452f_writefile(argvars, rettv)
15453 typval_T *argvars;
15454 typval_T *rettv;
15455{
15456 int binary = FALSE;
15457 char_u *fname;
15458 FILE *fd;
15459 listitem_T *li;
15460 char_u *s;
15461 int ret = 0;
15462 int c;
15463
15464 if (argvars[0].v_type != VAR_LIST)
15465 {
15466 EMSG2(_(e_listarg), "writefile()");
15467 return;
15468 }
15469 if (argvars[0].vval.v_list == NULL)
15470 return;
15471
15472 if (argvars[2].v_type != VAR_UNKNOWN
15473 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15474 binary = TRUE;
15475
15476 /* Always open the file in binary mode, library functions have a mind of
15477 * their own about CR-LF conversion. */
15478 fname = get_tv_string(&argvars[1]);
15479 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15480 {
15481 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15482 ret = -1;
15483 }
15484 else
15485 {
15486 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15487 li = li->li_next)
15488 {
15489 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15490 {
15491 if (*s == '\n')
15492 c = putc(NUL, fd);
15493 else
15494 c = putc(*s, fd);
15495 if (c == EOF)
15496 {
15497 ret = -1;
15498 break;
15499 }
15500 }
15501 if (!binary || li->li_next != NULL)
15502 if (putc('\n', fd) == EOF)
15503 {
15504 ret = -1;
15505 break;
15506 }
15507 if (ret < 0)
15508 {
15509 EMSG(_(e_write));
15510 break;
15511 }
15512 }
15513 fclose(fd);
15514 }
15515
15516 rettv->vval.v_number = ret;
15517}
15518
15519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015520 * Translate a String variable into a position.
15521 */
15522 static pos_T *
15523var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015524 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 int lnum; /* TRUE when $ is last line */
15526{
15527 char_u *name;
15528 static pos_T pos;
15529 pos_T *pp;
15530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015531 name = get_tv_string_chk(varp);
15532 if (name == NULL)
15533 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 if (name[0] == '.') /* cursor */
15535 return &curwin->w_cursor;
15536 if (name[0] == '\'') /* mark */
15537 {
15538 pp = getmark(name[1], FALSE);
15539 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15540 return NULL;
15541 return pp;
15542 }
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015543 if (name[0] == 'w' && lnum)
15544 {
15545 pos.col = 0;
15546 if (name[1] == '0') /* "w0": first visible line */
15547 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015548 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015549 pos.lnum = curwin->w_topline;
15550 return &pos;
15551 }
15552 else if (name[1] == '$') /* "w$": last visible line */
15553 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015554 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015555 pos.lnum = curwin->w_botline - 1;
15556 return &pos;
15557 }
15558 }
15559 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 {
15561 if (lnum)
15562 {
15563 pos.lnum = curbuf->b_ml.ml_line_count;
15564 pos.col = 0;
15565 }
15566 else
15567 {
15568 pos.lnum = curwin->w_cursor.lnum;
15569 pos.col = (colnr_T)STRLEN(ml_get_curline());
15570 }
15571 return &pos;
15572 }
15573 return NULL;
15574}
15575
15576/*
15577 * Get the length of an environment variable name.
15578 * Advance "arg" to the first character after the name.
15579 * Return 0 for error.
15580 */
15581 static int
15582get_env_len(arg)
15583 char_u **arg;
15584{
15585 char_u *p;
15586 int len;
15587
15588 for (p = *arg; vim_isIDc(*p); ++p)
15589 ;
15590 if (p == *arg) /* no name found */
15591 return 0;
15592
15593 len = (int)(p - *arg);
15594 *arg = p;
15595 return len;
15596}
15597
15598/*
15599 * Get the length of the name of a function or internal variable.
15600 * "arg" is advanced to the first non-white character after the name.
15601 * Return 0 if something is wrong.
15602 */
15603 static int
15604get_id_len(arg)
15605 char_u **arg;
15606{
15607 char_u *p;
15608 int len;
15609
15610 /* Find the end of the name. */
15611 for (p = *arg; eval_isnamec(*p); ++p)
15612 ;
15613 if (p == *arg) /* no name found */
15614 return 0;
15615
15616 len = (int)(p - *arg);
15617 *arg = skipwhite(p);
15618
15619 return len;
15620}
15621
15622/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015623 * Get the length of the name of a variable or function.
15624 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015626 * Return -1 if curly braces expansion failed.
15627 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 * If the name contains 'magic' {}'s, expand them and return the
15629 * expanded name in an allocated string via 'alias' - caller must free.
15630 */
15631 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015632get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633 char_u **arg;
15634 char_u **alias;
15635 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015636 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637{
15638 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639 char_u *p;
15640 char_u *expr_start;
15641 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642
15643 *alias = NULL; /* default to no alias */
15644
15645 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15646 && (*arg)[2] == (int)KE_SNR)
15647 {
15648 /* hard coded <SNR>, already translated */
15649 *arg += 3;
15650 return get_id_len(arg) + 3;
15651 }
15652 len = eval_fname_script(*arg);
15653 if (len > 0)
15654 {
15655 /* literal "<SID>", "s:" or "<SNR>" */
15656 *arg += len;
15657 }
15658
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015660 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015662 p = find_name_end(*arg, &expr_start, &expr_end,
15663 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664 if (expr_start != NULL)
15665 {
15666 char_u *temp_string;
15667
15668 if (!evaluate)
15669 {
15670 len += (int)(p - *arg);
15671 *arg = skipwhite(p);
15672 return len;
15673 }
15674
15675 /*
15676 * Include any <SID> etc in the expanded string:
15677 * Thus the -len here.
15678 */
15679 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15680 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015681 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 *alias = temp_string;
15683 *arg = skipwhite(p);
15684 return (int)STRLEN(temp_string);
15685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015686
15687 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015688 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689 EMSG2(_(e_invexpr2), *arg);
15690
15691 return len;
15692}
15693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015694/*
15695 * Find the end of a variable or function name, taking care of magic braces.
15696 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15697 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015698 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015699 * Return a pointer to just after the name. Equal to "arg" if there is no
15700 * valid name.
15701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015703find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704 char_u *arg;
15705 char_u **expr_start;
15706 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015707 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015709 int mb_nest = 0;
15710 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711 char_u *p;
15712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015713 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015715 *expr_start = NULL;
15716 *expr_end = NULL;
15717 }
15718
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015719 /* Quick check for valid starting character. */
15720 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15721 return arg;
15722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015723 for (p = arg; *p != NUL
15724 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015725 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015726 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015727 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015728 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015729 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015730 if (*p == '\'')
15731 {
15732 /* skip over 'string' to avoid counting [ and ] inside it. */
15733 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15734 ;
15735 if (*p == NUL)
15736 break;
15737 }
15738 else if (*p == '"')
15739 {
15740 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15741 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15742 if (*p == '\\' && p[1] != NUL)
15743 ++p;
15744 if (*p == NUL)
15745 break;
15746 }
15747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015748 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015750 if (*p == '[')
15751 ++br_nest;
15752 else if (*p == ']')
15753 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015756 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015758 if (*p == '{')
15759 {
15760 mb_nest++;
15761 if (expr_start != NULL && *expr_start == NULL)
15762 *expr_start = p;
15763 }
15764 else if (*p == '}')
15765 {
15766 mb_nest--;
15767 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15768 *expr_end = p;
15769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 }
15772
15773 return p;
15774}
15775
15776/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015777 * Expands out the 'magic' {}'s in a variable/function name.
15778 * Note that this can call itself recursively, to deal with
15779 * constructs like foo{bar}{baz}{bam}
15780 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15781 * "in_start" ^
15782 * "expr_start" ^
15783 * "expr_end" ^
15784 * "in_end" ^
15785 *
15786 * Returns a new allocated string, which the caller must free.
15787 * Returns NULL for failure.
15788 */
15789 static char_u *
15790make_expanded_name(in_start, expr_start, expr_end, in_end)
15791 char_u *in_start;
15792 char_u *expr_start;
15793 char_u *expr_end;
15794 char_u *in_end;
15795{
15796 char_u c1;
15797 char_u *retval = NULL;
15798 char_u *temp_result;
15799 char_u *nextcmd = NULL;
15800
15801 if (expr_end == NULL || in_end == NULL)
15802 return NULL;
15803 *expr_start = NUL;
15804 *expr_end = NUL;
15805 c1 = *in_end;
15806 *in_end = NUL;
15807
15808 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15809 if (temp_result != NULL && nextcmd == NULL)
15810 {
15811 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15812 + (in_end - expr_end) + 1));
15813 if (retval != NULL)
15814 {
15815 STRCPY(retval, in_start);
15816 STRCAT(retval, temp_result);
15817 STRCAT(retval, expr_end + 1);
15818 }
15819 }
15820 vim_free(temp_result);
15821
15822 *in_end = c1; /* put char back for error messages */
15823 *expr_start = '{';
15824 *expr_end = '}';
15825
15826 if (retval != NULL)
15827 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015828 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015829 if (expr_start != NULL)
15830 {
15831 /* Further expansion! */
15832 temp_result = make_expanded_name(retval, expr_start,
15833 expr_end, temp_result);
15834 vim_free(retval);
15835 retval = temp_result;
15836 }
15837 }
15838
15839 return retval;
15840}
15841
15842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015844 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845 */
15846 static int
15847eval_isnamec(c)
15848 int c;
15849{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015850 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15851}
15852
15853/*
15854 * Return TRUE if character "c" can be used as the first character in a
15855 * variable or function name (excluding '{' and '}').
15856 */
15857 static int
15858eval_isnamec1(c)
15859 int c;
15860{
15861 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862}
15863
15864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865 * Set number v: variable to "val".
15866 */
15867 void
15868set_vim_var_nr(idx, val)
15869 int idx;
15870 long val;
15871{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015872 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873}
15874
15875/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015876 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877 */
15878 long
15879get_vim_var_nr(idx)
15880 int idx;
15881{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015882 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015883}
15884
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015885#if defined(FEAT_AUTOCMD) || defined(PROTO)
15886/*
15887 * Get string v: variable value. Uses a static buffer, can only be used once.
15888 */
15889 char_u *
15890get_vim_var_str(idx)
15891 int idx;
15892{
15893 return get_tv_string(&vimvars[idx].vv_tv);
15894}
15895#endif
15896
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897/*
15898 * Set v:count, v:count1 and v:prevcount.
15899 */
15900 void
15901set_vcount(count, count1)
15902 long count;
15903 long count1;
15904{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015905 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15906 vimvars[VV_COUNT].vv_nr = count;
15907 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908}
15909
15910/*
15911 * Set string v: variable to a copy of "val".
15912 */
15913 void
15914set_vim_var_string(idx, val, len)
15915 int idx;
15916 char_u *val;
15917 int len; /* length of "val" to use or -1 (whole string) */
15918{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015919 /* Need to do this (at least) once, since we can't initialize a union.
15920 * Will always be invoked when "v:progname" is set. */
15921 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15922
Bram Moolenaare9a41262005-01-15 22:18:47 +000015923 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015924 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015925 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015926 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015927 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015929 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930}
15931
15932/*
15933 * Set v:register if needed.
15934 */
15935 void
15936set_reg_var(c)
15937 int c;
15938{
15939 char_u regname;
15940
15941 if (c == 0 || c == ' ')
15942 regname = '"';
15943 else
15944 regname = c;
15945 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015946 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947 set_vim_var_string(VV_REG, &regname, 1);
15948}
15949
15950/*
15951 * Get or set v:exception. If "oldval" == NULL, return the current value.
15952 * Otherwise, restore the value to "oldval" and return NULL.
15953 * Must always be called in pairs to save and restore v:exception! Does not
15954 * take care of memory allocations.
15955 */
15956 char_u *
15957v_exception(oldval)
15958 char_u *oldval;
15959{
15960 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015961 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015962
Bram Moolenaare9a41262005-01-15 22:18:47 +000015963 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964 return NULL;
15965}
15966
15967/*
15968 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15969 * Otherwise, restore the value to "oldval" and return NULL.
15970 * Must always be called in pairs to save and restore v:throwpoint! Does not
15971 * take care of memory allocations.
15972 */
15973 char_u *
15974v_throwpoint(oldval)
15975 char_u *oldval;
15976{
15977 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015978 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979
Bram Moolenaare9a41262005-01-15 22:18:47 +000015980 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015981 return NULL;
15982}
15983
15984#if defined(FEAT_AUTOCMD) || defined(PROTO)
15985/*
15986 * Set v:cmdarg.
15987 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15988 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15989 * Must always be called in pairs!
15990 */
15991 char_u *
15992set_cmdarg(eap, oldarg)
15993 exarg_T *eap;
15994 char_u *oldarg;
15995{
15996 char_u *oldval;
15997 char_u *newval;
15998 unsigned len;
15999
Bram Moolenaare9a41262005-01-15 22:18:47 +000016000 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016001 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016003 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016004 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016005 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006 }
16007
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016008 if (eap->force_bin == FORCE_BIN)
16009 len = 6;
16010 else if (eap->force_bin == FORCE_NOBIN)
16011 len = 8;
16012 else
16013 len = 0;
16014 if (eap->force_ff != 0)
16015 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16016# ifdef FEAT_MBYTE
16017 if (eap->force_enc != 0)
16018 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016019 if (eap->bad_char != 0)
16020 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016021# endif
16022
16023 newval = alloc(len + 1);
16024 if (newval == NULL)
16025 return NULL;
16026
16027 if (eap->force_bin == FORCE_BIN)
16028 sprintf((char *)newval, " ++bin");
16029 else if (eap->force_bin == FORCE_NOBIN)
16030 sprintf((char *)newval, " ++nobin");
16031 else
16032 *newval = NUL;
16033 if (eap->force_ff != 0)
16034 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16035 eap->cmd + eap->force_ff);
16036# ifdef FEAT_MBYTE
16037 if (eap->force_enc != 0)
16038 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16039 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016040 if (eap->bad_char != 0)
16041 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16042 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016043# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016044 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016045 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046}
16047#endif
16048
16049/*
16050 * Get the value of internal variable "name".
16051 * Return OK or FAIL.
16052 */
16053 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016054get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 char_u *name;
16056 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016057 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016058 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059{
16060 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016061 typval_T *tv = NULL;
16062 typval_T atv;
16063 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016065
16066 /* truncate the name, so that we can use strcmp() */
16067 cc = name[len];
16068 name[len] = NUL;
16069
16070 /*
16071 * Check for "b:changedtick".
16072 */
16073 if (STRCMP(name, "b:changedtick") == 0)
16074 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016075 atv.v_type = VAR_NUMBER;
16076 atv.vval.v_number = curbuf->b_changedtick;
16077 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078 }
16079
16080 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 * Check for user-defined variables.
16082 */
16083 else
16084 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016085 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016087 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 }
16089
Bram Moolenaare9a41262005-01-15 22:18:47 +000016090 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016092 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016093 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094 ret = FAIL;
16095 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016096 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016097 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098
16099 name[len] = cc;
16100
16101 return ret;
16102}
16103
16104/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016105 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16106 * Also handle function call with Funcref variable: func(expr)
16107 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16108 */
16109 static int
16110handle_subscript(arg, rettv, evaluate, verbose)
16111 char_u **arg;
16112 typval_T *rettv;
16113 int evaluate; /* do more than finding the end */
16114 int verbose; /* give error messages */
16115{
16116 int ret = OK;
16117 dict_T *selfdict = NULL;
16118 char_u *s;
16119 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016120 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016121
16122 while (ret == OK
16123 && (**arg == '['
16124 || (**arg == '.' && rettv->v_type == VAR_DICT)
16125 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16126 && !vim_iswhite(*(*arg - 1)))
16127 {
16128 if (**arg == '(')
16129 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016130 /* need to copy the funcref so that we can clear rettv */
16131 functv = *rettv;
16132 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016133
16134 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016135 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016136 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016137 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16138 &len, evaluate, selfdict);
16139
16140 /* Clear the funcref afterwards, so that deleting it while
16141 * evaluating the arguments is possible (see test55). */
16142 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016143
16144 /* Stop the expression evaluation when immediately aborting on
16145 * error, or when an interrupt occurred or an exception was thrown
16146 * but not caught. */
16147 if (aborting())
16148 {
16149 if (ret == OK)
16150 clear_tv(rettv);
16151 ret = FAIL;
16152 }
16153 dict_unref(selfdict);
16154 selfdict = NULL;
16155 }
16156 else /* **arg == '[' || **arg == '.' */
16157 {
16158 dict_unref(selfdict);
16159 if (rettv->v_type == VAR_DICT)
16160 {
16161 selfdict = rettv->vval.v_dict;
16162 if (selfdict != NULL)
16163 ++selfdict->dv_refcount;
16164 }
16165 else
16166 selfdict = NULL;
16167 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16168 {
16169 clear_tv(rettv);
16170 ret = FAIL;
16171 }
16172 }
16173 }
16174 dict_unref(selfdict);
16175 return ret;
16176}
16177
16178/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016179 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16180 * value).
16181 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016182 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016183alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016184{
Bram Moolenaar33570922005-01-25 22:26:29 +000016185 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016186}
16187
16188/*
16189 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190 * The string "s" must have been allocated, it is consumed.
16191 * Return NULL for out of memory, the variable otherwise.
16192 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016193 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016194alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 char_u *s;
16196{
Bram Moolenaar33570922005-01-25 22:26:29 +000016197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016199 rettv = alloc_tv();
16200 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016202 rettv->v_type = VAR_STRING;
16203 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 }
16205 else
16206 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016207 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208}
16209
16210/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016211 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016213 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016214free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016215 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216{
16217 if (varp != NULL)
16218 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016219 switch (varp->v_type)
16220 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016221 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016222 func_unref(varp->vval.v_string);
16223 /*FALLTHROUGH*/
16224 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016225 vim_free(varp->vval.v_string);
16226 break;
16227 case VAR_LIST:
16228 list_unref(varp->vval.v_list);
16229 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016230 case VAR_DICT:
16231 dict_unref(varp->vval.v_dict);
16232 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016233 case VAR_NUMBER:
16234 case VAR_UNKNOWN:
16235 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016236 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016237 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016238 break;
16239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240 vim_free(varp);
16241 }
16242}
16243
16244/*
16245 * Free the memory for a variable value and set the value to NULL or 0.
16246 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016247 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016248clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016249 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250{
16251 if (varp != NULL)
16252 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016253 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016255 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016256 func_unref(varp->vval.v_string);
16257 /*FALLTHROUGH*/
16258 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016259 vim_free(varp->vval.v_string);
16260 varp->vval.v_string = NULL;
16261 break;
16262 case VAR_LIST:
16263 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016264 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016265 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016266 case VAR_DICT:
16267 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016268 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016269 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016270 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016271 varp->vval.v_number = 0;
16272 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016273 case VAR_UNKNOWN:
16274 break;
16275 default:
16276 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016278 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279 }
16280}
16281
16282/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016283 * Set the value of a variable to NULL without freeing items.
16284 */
16285 static void
16286init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016287 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016288{
16289 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016290 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016291}
16292
16293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 * Get the number value of a variable.
16295 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016296 * For incompatible types, return 0.
16297 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16298 * caller of incompatible types: it sets *denote to TRUE if "denote"
16299 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300 */
16301 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016302get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016303 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016305 int error = FALSE;
16306
16307 return get_tv_number_chk(varp, &error); /* return 0L on error */
16308}
16309
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016310 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016311get_tv_number_chk(varp, denote)
16312 typval_T *varp;
16313 int *denote;
16314{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016315 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016317 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016319 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016320 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016321 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016322 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016323 break;
16324 case VAR_STRING:
16325 if (varp->vval.v_string != NULL)
16326 vim_str2nr(varp->vval.v_string, NULL, NULL,
16327 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016328 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016329 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016330 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016331 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016332 case VAR_DICT:
16333 EMSG(_("E728: Using a Dictionary as a number"));
16334 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016335 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016336 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016337 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016339 if (denote == NULL) /* useful for values that must be unsigned */
16340 n = -1;
16341 else
16342 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016343 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344}
16345
16346/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016347 * Get the lnum from the first argument.
16348 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016349 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350 */
16351 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016352get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016353 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354{
Bram Moolenaar33570922005-01-25 22:26:29 +000016355 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356 linenr_T lnum;
16357
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016358 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359 if (lnum == 0) /* no valid number, try using line() */
16360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016361 rettv.v_type = VAR_NUMBER;
16362 f_line(argvars, &rettv);
16363 lnum = rettv.vval.v_number;
16364 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365 }
16366 return lnum;
16367}
16368
16369/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016370 * Get the lnum from the first argument.
16371 * Also accepts "$", then "buf" is used.
16372 * Returns 0 on error.
16373 */
16374 static linenr_T
16375get_tv_lnum_buf(argvars, buf)
16376 typval_T *argvars;
16377 buf_T *buf;
16378{
16379 if (argvars[0].v_type == VAR_STRING
16380 && argvars[0].vval.v_string != NULL
16381 && argvars[0].vval.v_string[0] == '$'
16382 && buf != NULL)
16383 return buf->b_ml.ml_line_count;
16384 return get_tv_number_chk(&argvars[0], NULL);
16385}
16386
16387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 * Get the string value of a variable.
16389 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016390 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16391 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392 * If the String variable has never been set, return an empty string.
16393 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016394 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16395 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396 */
16397 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016398get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016399 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400{
16401 static char_u mybuf[NUMBUFLEN];
16402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016403 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404}
16405
16406 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016407get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016408 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016409 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016411 char_u *res = get_tv_string_buf_chk(varp, buf);
16412
16413 return res != NULL ? res : (char_u *)"";
16414}
16415
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016416 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016417get_tv_string_chk(varp)
16418 typval_T *varp;
16419{
16420 static char_u mybuf[NUMBUFLEN];
16421
16422 return get_tv_string_buf_chk(varp, mybuf);
16423}
16424
16425 static char_u *
16426get_tv_string_buf_chk(varp, buf)
16427 typval_T *varp;
16428 char_u *buf;
16429{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016430 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016432 case VAR_NUMBER:
16433 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16434 return buf;
16435 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016436 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016437 break;
16438 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016439 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016440 break;
16441 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016442 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016443 break;
16444 case VAR_STRING:
16445 if (varp->vval.v_string != NULL)
16446 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016447 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016448 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016449 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016450 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016452 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453}
16454
16455/*
16456 * Find variable "name" in the list of variables.
16457 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016458 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016459 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016460 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016462 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016463find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016465 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016468 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469
Bram Moolenaara7043832005-01-21 11:56:39 +000016470 ht = find_var_ht(name, &varname);
16471 if (htp != NULL)
16472 *htp = ht;
16473 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016475 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476}
16477
16478/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016479 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016480 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016482 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016483find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016484 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016485 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016486 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016487{
Bram Moolenaar33570922005-01-25 22:26:29 +000016488 hashitem_T *hi;
16489
16490 if (*varname == NUL)
16491 {
16492 /* Must be something like "s:", otherwise "ht" would be NULL. */
16493 switch (varname[-2])
16494 {
16495 case 's': return &SCRIPT_SV(current_SID).sv_var;
16496 case 'g': return &globvars_var;
16497 case 'v': return &vimvars_var;
16498 case 'b': return &curbuf->b_bufvar;
16499 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016500 case 'l': return current_funccal == NULL
16501 ? NULL : &current_funccal->l_vars_var;
16502 case 'a': return current_funccal == NULL
16503 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016504 }
16505 return NULL;
16506 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016507
16508 hi = hash_find(ht, varname);
16509 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016510 {
16511 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016512 * worked find the variable again. Don't auto-load a script if it was
16513 * loaded already, otherwise it would be loaded every time when
16514 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016515 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016516 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016517 hi = hash_find(ht, varname);
16518 if (HASHITEM_EMPTY(hi))
16519 return NULL;
16520 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016521 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016522}
16523
16524/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016525 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016526 * Set "varname" to the start of name without ':'.
16527 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016528 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016529find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530 char_u *name;
16531 char_u **varname;
16532{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016533 hashitem_T *hi;
16534
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 if (name[1] != ':')
16536 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016537 /* The name must not start with a colon or #. */
16538 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 return NULL;
16540 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016541
16542 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016543 hi = hash_find(&compat_hashtab, name);
16544 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016545 return &compat_hashtab;
16546
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016548 return &globvarht; /* global variable */
16549 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 }
16551 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016552 if (*name == 'g') /* global variable */
16553 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016554 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16555 */
16556 if (vim_strchr(name + 2, ':') != NULL
16557 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016558 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016560 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016562 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016563 if (*name == 'v') /* v: variable */
16564 return &vimvarht;
16565 if (*name == 'a' && current_funccal != NULL) /* function argument */
16566 return &current_funccal->l_avars.dv_hashtab;
16567 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16568 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569 if (*name == 's' /* script variable */
16570 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16571 return &SCRIPT_VARS(current_SID);
16572 return NULL;
16573}
16574
16575/*
16576 * Get the string value of a (global/local) variable.
16577 * Returns NULL when it doesn't exist.
16578 */
16579 char_u *
16580get_var_value(name)
16581 char_u *name;
16582{
Bram Moolenaar33570922005-01-25 22:26:29 +000016583 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584
Bram Moolenaara7043832005-01-21 11:56:39 +000016585 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 if (v == NULL)
16587 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016588 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589}
16590
16591/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016592 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593 * sourcing this script and when executing functions defined in the script.
16594 */
16595 void
16596new_script_vars(id)
16597 scid_T id;
16598{
Bram Moolenaara7043832005-01-21 11:56:39 +000016599 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016600 hashtab_T *ht;
16601 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016602
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16604 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016605 /* Re-allocating ga_data means that an ht_array pointing to
16606 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016607 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016608 for (i = 1; i <= ga_scripts.ga_len; ++i)
16609 {
16610 ht = &SCRIPT_VARS(i);
16611 if (ht->ht_mask == HT_INIT_SIZE - 1)
16612 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016613 sv = &SCRIPT_SV(i);
16614 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016615 }
16616
Bram Moolenaar071d4272004-06-13 20:20:40 +000016617 while (ga_scripts.ga_len < id)
16618 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016619 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16620 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622 }
16623 }
16624}
16625
16626/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016627 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16628 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 */
16630 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016631init_var_dict(dict, dict_var)
16632 dict_T *dict;
16633 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634{
Bram Moolenaar33570922005-01-25 22:26:29 +000016635 hash_init(&dict->dv_hashtab);
16636 dict->dv_refcount = 99999;
16637 dict_var->di_tv.vval.v_dict = dict;
16638 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016639 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016640 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16641 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642}
16643
16644/*
16645 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016646 * Frees all allocated variables and the value they contain.
16647 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 */
16649 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016650vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016651 hashtab_T *ht;
16652{
16653 vars_clear_ext(ht, TRUE);
16654}
16655
16656/*
16657 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16658 */
16659 static void
16660vars_clear_ext(ht, free_val)
16661 hashtab_T *ht;
16662 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663{
Bram Moolenaara7043832005-01-21 11:56:39 +000016664 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016665 hashitem_T *hi;
16666 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667
Bram Moolenaar33570922005-01-25 22:26:29 +000016668 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016669 todo = ht->ht_used;
16670 for (hi = ht->ht_array; todo > 0; ++hi)
16671 {
16672 if (!HASHITEM_EMPTY(hi))
16673 {
16674 --todo;
16675
Bram Moolenaar33570922005-01-25 22:26:29 +000016676 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016677 * ht_array might change then. hash_clear() takes care of it
16678 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016679 v = HI2DI(hi);
16680 if (free_val)
16681 clear_tv(&v->di_tv);
16682 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16683 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016684 }
16685 }
16686 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016687 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688}
16689
Bram Moolenaara7043832005-01-21 11:56:39 +000016690/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016691 * Delete a variable from hashtab "ht" at item "hi".
16692 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016695delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016696 hashtab_T *ht;
16697 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698{
Bram Moolenaar33570922005-01-25 22:26:29 +000016699 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016700
16701 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016702 clear_tv(&di->di_tv);
16703 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704}
16705
16706/*
16707 * List the value of one internal variable.
16708 */
16709 static void
16710list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016711 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 char_u *prefix;
16713{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016714 char_u *tofree;
16715 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016716 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016717
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016718 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000016719 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016720 s == NULL ? (char_u *)"" : s);
16721 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722}
16723
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724 static void
16725list_one_var_a(prefix, name, type, string)
16726 char_u *prefix;
16727 char_u *name;
16728 int type;
16729 char_u *string;
16730{
16731 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16732 if (name != NULL) /* "a:" vars don't have a name stored */
16733 msg_puts(name);
16734 msg_putchar(' ');
16735 msg_advance(22);
16736 if (type == VAR_NUMBER)
16737 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016738 else if (type == VAR_FUNC)
16739 msg_putchar('*');
16740 else if (type == VAR_LIST)
16741 {
16742 msg_putchar('[');
16743 if (*string == '[')
16744 ++string;
16745 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016746 else if (type == VAR_DICT)
16747 {
16748 msg_putchar('{');
16749 if (*string == '{')
16750 ++string;
16751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752 else
16753 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016754
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016756
16757 if (type == VAR_FUNC)
16758 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759}
16760
16761/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016762 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763 * If the variable already exists, the value is updated.
16764 * Otherwise the variable is created.
16765 */
16766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016767set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016769 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016770 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771{
Bram Moolenaar33570922005-01-25 22:26:29 +000016772 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016774 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016775 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016777 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016778 {
16779 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16780 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16781 ? name[2] : name[0]))
16782 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016783 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016784 return;
16785 }
16786 if (function_exists(name))
16787 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016788 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016789 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016790 return;
16791 }
16792 }
16793
Bram Moolenaara7043832005-01-21 11:56:39 +000016794 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016795 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016796 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016797 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016798 return;
16799 }
16800
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016801 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016802 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016804 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016805 if (var_check_ro(v->di_flags, name)
16806 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016807 return;
16808 if (v->di_tv.v_type != tv->v_type
16809 && !((v->di_tv.v_type == VAR_STRING
16810 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016811 && (tv->v_type == VAR_STRING
16812 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016813 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016814 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016815 return;
16816 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016817
16818 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016819 * Handle setting internal v: variables separately: we don't change
16820 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016821 */
16822 if (ht == &vimvarht)
16823 {
16824 if (v->di_tv.v_type == VAR_STRING)
16825 {
16826 vim_free(v->di_tv.vval.v_string);
16827 if (copy || tv->v_type != VAR_STRING)
16828 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16829 else
16830 {
16831 /* Take over the string to avoid an extra alloc/free. */
16832 v->di_tv.vval.v_string = tv->vval.v_string;
16833 tv->vval.v_string = NULL;
16834 }
16835 }
16836 else if (v->di_tv.v_type != VAR_NUMBER)
16837 EMSG2(_(e_intern2), "set_var()");
16838 else
16839 v->di_tv.vval.v_number = get_tv_number(tv);
16840 return;
16841 }
16842
16843 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 }
16845 else /* add a new variable */
16846 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016847 /* Make sure the variable name is valid. */
16848 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000016849 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
16850 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000016851 {
16852 EMSG2(_(e_illvar), varname);
16853 return;
16854 }
16855
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016856 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16857 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016858 if (v == NULL)
16859 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016860 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016861 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016863 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 return;
16865 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016866 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016869 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016870 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016871 else
16872 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016873 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016874 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016875 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877}
16878
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016879/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016880 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16881 * Also give an error message.
16882 */
16883 static int
16884var_check_ro(flags, name)
16885 int flags;
16886 char_u *name;
16887{
16888 if (flags & DI_FLAGS_RO)
16889 {
16890 EMSG2(_(e_readonlyvar), name);
16891 return TRUE;
16892 }
16893 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16894 {
16895 EMSG2(_(e_readonlysbx), name);
16896 return TRUE;
16897 }
16898 return FALSE;
16899}
16900
16901/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016902 * Return TRUE if typeval "tv" is set to be locked (immutable).
16903 * Also give an error message, using "name".
16904 */
16905 static int
16906tv_check_lock(lock, name)
16907 int lock;
16908 char_u *name;
16909{
16910 if (lock & VAR_LOCKED)
16911 {
16912 EMSG2(_("E741: Value is locked: %s"),
16913 name == NULL ? (char_u *)_("Unknown") : name);
16914 return TRUE;
16915 }
16916 if (lock & VAR_FIXED)
16917 {
16918 EMSG2(_("E742: Cannot change value of %s"),
16919 name == NULL ? (char_u *)_("Unknown") : name);
16920 return TRUE;
16921 }
16922 return FALSE;
16923}
16924
16925/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016926 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016927 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016928 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016931copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016932 typval_T *from;
16933 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016935 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016936 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016937 switch (from->v_type)
16938 {
16939 case VAR_NUMBER:
16940 to->vval.v_number = from->vval.v_number;
16941 break;
16942 case VAR_STRING:
16943 case VAR_FUNC:
16944 if (from->vval.v_string == NULL)
16945 to->vval.v_string = NULL;
16946 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016947 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016948 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016949 if (from->v_type == VAR_FUNC)
16950 func_ref(to->vval.v_string);
16951 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016952 break;
16953 case VAR_LIST:
16954 if (from->vval.v_list == NULL)
16955 to->vval.v_list = NULL;
16956 else
16957 {
16958 to->vval.v_list = from->vval.v_list;
16959 ++to->vval.v_list->lv_refcount;
16960 }
16961 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016962 case VAR_DICT:
16963 if (from->vval.v_dict == NULL)
16964 to->vval.v_dict = NULL;
16965 else
16966 {
16967 to->vval.v_dict = from->vval.v_dict;
16968 ++to->vval.v_dict->dv_refcount;
16969 }
16970 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016971 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016972 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016973 break;
16974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975}
16976
16977/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016978 * Make a copy of an item.
16979 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016980 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16981 * reference to an already copied list/dict can be used.
16982 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016983 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016984 static int
16985item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016986 typval_T *from;
16987 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016988 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016989 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016990{
16991 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016992 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016993
Bram Moolenaar33570922005-01-25 22:26:29 +000016994 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016995 {
16996 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016997 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016998 }
16999 ++recurse;
17000
17001 switch (from->v_type)
17002 {
17003 case VAR_NUMBER:
17004 case VAR_STRING:
17005 case VAR_FUNC:
17006 copy_tv(from, to);
17007 break;
17008 case VAR_LIST:
17009 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017010 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017011 if (from->vval.v_list == NULL)
17012 to->vval.v_list = NULL;
17013 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17014 {
17015 /* use the copy made earlier */
17016 to->vval.v_list = from->vval.v_list->lv_copylist;
17017 ++to->vval.v_list->lv_refcount;
17018 }
17019 else
17020 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17021 if (to->vval.v_list == NULL)
17022 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017023 break;
17024 case VAR_DICT:
17025 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017026 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017027 if (from->vval.v_dict == NULL)
17028 to->vval.v_dict = NULL;
17029 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17030 {
17031 /* use the copy made earlier */
17032 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17033 ++to->vval.v_dict->dv_refcount;
17034 }
17035 else
17036 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17037 if (to->vval.v_dict == NULL)
17038 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017039 break;
17040 default:
17041 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017042 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017043 }
17044 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017045 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017046}
17047
17048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049 * ":echo expr1 ..." print each argument separated with a space, add a
17050 * newline at the end.
17051 * ":echon expr1 ..." print each argument plain.
17052 */
17053 void
17054ex_echo(eap)
17055 exarg_T *eap;
17056{
17057 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017058 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017059 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060 char_u *p;
17061 int needclr = TRUE;
17062 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017063 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064
17065 if (eap->skip)
17066 ++emsg_skip;
17067 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17068 {
17069 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017070 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071 {
17072 /*
17073 * Report the invalid expression unless the expression evaluation
17074 * has been cancelled due to an aborting error, an interrupt, or an
17075 * exception.
17076 */
17077 if (!aborting())
17078 EMSG2(_(e_invexpr2), p);
17079 break;
17080 }
17081 if (!eap->skip)
17082 {
17083 if (atstart)
17084 {
17085 atstart = FALSE;
17086 /* Call msg_start() after eval1(), evaluating the expression
17087 * may cause a message to appear. */
17088 if (eap->cmdidx == CMD_echo)
17089 msg_start();
17090 }
17091 else if (eap->cmdidx == CMD_echo)
17092 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017093 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017094 if (p != NULL)
17095 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017097 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017099 if (*p != TAB && needclr)
17100 {
17101 /* remove any text still there from the command */
17102 msg_clr_eos();
17103 needclr = FALSE;
17104 }
17105 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106 }
17107 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017108 {
17109#ifdef FEAT_MBYTE
17110 if (has_mbyte)
17111 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017112 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017113
17114 (void)msg_outtrans_len_attr(p, i, echo_attr);
17115 p += i - 1;
17116 }
17117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017119 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017122 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017124 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125 arg = skipwhite(arg);
17126 }
17127 eap->nextcmd = check_nextcmd(arg);
17128
17129 if (eap->skip)
17130 --emsg_skip;
17131 else
17132 {
17133 /* remove text that may still be there from the command */
17134 if (needclr)
17135 msg_clr_eos();
17136 if (eap->cmdidx == CMD_echo)
17137 msg_end();
17138 }
17139}
17140
17141/*
17142 * ":echohl {name}".
17143 */
17144 void
17145ex_echohl(eap)
17146 exarg_T *eap;
17147{
17148 int id;
17149
17150 id = syn_name2id(eap->arg);
17151 if (id == 0)
17152 echo_attr = 0;
17153 else
17154 echo_attr = syn_id2attr(id);
17155}
17156
17157/*
17158 * ":execute expr1 ..." execute the result of an expression.
17159 * ":echomsg expr1 ..." Print a message
17160 * ":echoerr expr1 ..." Print an error
17161 * Each gets spaces around each argument and a newline at the end for
17162 * echo commands
17163 */
17164 void
17165ex_execute(eap)
17166 exarg_T *eap;
17167{
17168 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017169 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 int ret = OK;
17171 char_u *p;
17172 garray_T ga;
17173 int len;
17174 int save_did_emsg;
17175
17176 ga_init2(&ga, 1, 80);
17177
17178 if (eap->skip)
17179 ++emsg_skip;
17180 while (*arg != NUL && *arg != '|' && *arg != '\n')
17181 {
17182 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017183 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 {
17185 /*
17186 * Report the invalid expression unless the expression evaluation
17187 * has been cancelled due to an aborting error, an interrupt, or an
17188 * exception.
17189 */
17190 if (!aborting())
17191 EMSG2(_(e_invexpr2), p);
17192 ret = FAIL;
17193 break;
17194 }
17195
17196 if (!eap->skip)
17197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017198 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199 len = (int)STRLEN(p);
17200 if (ga_grow(&ga, len + 2) == FAIL)
17201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017202 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203 ret = FAIL;
17204 break;
17205 }
17206 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 ga.ga_len += len;
17210 }
17211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017212 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 arg = skipwhite(arg);
17214 }
17215
17216 if (ret != FAIL && ga.ga_data != NULL)
17217 {
17218 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017221 out_flush();
17222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 else if (eap->cmdidx == CMD_echoerr)
17224 {
17225 /* We don't want to abort following commands, restore did_emsg. */
17226 save_did_emsg = did_emsg;
17227 EMSG((char_u *)ga.ga_data);
17228 if (!force_abort)
17229 did_emsg = save_did_emsg;
17230 }
17231 else if (eap->cmdidx == CMD_execute)
17232 do_cmdline((char_u *)ga.ga_data,
17233 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17234 }
17235
17236 ga_clear(&ga);
17237
17238 if (eap->skip)
17239 --emsg_skip;
17240
17241 eap->nextcmd = check_nextcmd(arg);
17242}
17243
17244/*
17245 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17246 * "arg" points to the "&" or '+' when called, to "option" when returning.
17247 * Returns NULL when no option name found. Otherwise pointer to the char
17248 * after the option name.
17249 */
17250 static char_u *
17251find_option_end(arg, opt_flags)
17252 char_u **arg;
17253 int *opt_flags;
17254{
17255 char_u *p = *arg;
17256
17257 ++p;
17258 if (*p == 'g' && p[1] == ':')
17259 {
17260 *opt_flags = OPT_GLOBAL;
17261 p += 2;
17262 }
17263 else if (*p == 'l' && p[1] == ':')
17264 {
17265 *opt_flags = OPT_LOCAL;
17266 p += 2;
17267 }
17268 else
17269 *opt_flags = 0;
17270
17271 if (!ASCII_ISALPHA(*p))
17272 return NULL;
17273 *arg = p;
17274
17275 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17276 p += 4; /* termcap option */
17277 else
17278 while (ASCII_ISALPHA(*p))
17279 ++p;
17280 return p;
17281}
17282
17283/*
17284 * ":function"
17285 */
17286 void
17287ex_function(eap)
17288 exarg_T *eap;
17289{
17290 char_u *theline;
17291 int j;
17292 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 char_u *name = NULL;
17295 char_u *p;
17296 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017297 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 garray_T newargs;
17299 garray_T newlines;
17300 int varargs = FALSE;
17301 int mustend = FALSE;
17302 int flags = 0;
17303 ufunc_T *fp;
17304 int indent;
17305 int nesting;
17306 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017307 dictitem_T *v;
17308 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017309 static int func_nr = 0; /* number for nameless function */
17310 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017311 hashtab_T *ht;
17312 int todo;
17313 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314
17315 /*
17316 * ":function" without argument: list functions.
17317 */
17318 if (ends_excmd(*eap->arg))
17319 {
17320 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017321 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017322 todo = func_hashtab.ht_used;
17323 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017324 {
17325 if (!HASHITEM_EMPTY(hi))
17326 {
17327 --todo;
17328 fp = HI2UF(hi);
17329 if (!isdigit(*fp->uf_name))
17330 list_func_head(fp, FALSE);
17331 }
17332 }
17333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334 eap->nextcmd = check_nextcmd(eap->arg);
17335 return;
17336 }
17337
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017338 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017339 * ":function /pat": list functions matching pattern.
17340 */
17341 if (*eap->arg == '/')
17342 {
17343 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17344 if (!eap->skip)
17345 {
17346 regmatch_T regmatch;
17347
17348 c = *p;
17349 *p = NUL;
17350 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17351 *p = c;
17352 if (regmatch.regprog != NULL)
17353 {
17354 regmatch.rm_ic = p_ic;
17355
17356 todo = func_hashtab.ht_used;
17357 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17358 {
17359 if (!HASHITEM_EMPTY(hi))
17360 {
17361 --todo;
17362 fp = HI2UF(hi);
17363 if (!isdigit(*fp->uf_name)
17364 && vim_regexec(&regmatch, fp->uf_name, 0))
17365 list_func_head(fp, FALSE);
17366 }
17367 }
17368 }
17369 }
17370 if (*p == '/')
17371 ++p;
17372 eap->nextcmd = check_nextcmd(p);
17373 return;
17374 }
17375
17376 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017377 * Get the function name. There are these situations:
17378 * func normal function name
17379 * "name" == func, "fudi.fd_dict" == NULL
17380 * dict.func new dictionary entry
17381 * "name" == NULL, "fudi.fd_dict" set,
17382 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17383 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017384 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017385 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17386 * dict.func existing dict entry that's not a Funcref
17387 * "name" == NULL, "fudi.fd_dict" set,
17388 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017391 name = trans_function_name(&p, eap->skip, 0, &fudi);
17392 paren = (vim_strchr(p, '(') != NULL);
17393 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394 {
17395 /*
17396 * Return on an invalid expression in braces, unless the expression
17397 * evaluation has been cancelled due to an aborting error, an
17398 * interrupt, or an exception.
17399 */
17400 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017401 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017402 if (!eap->skip && fudi.fd_newkey != NULL)
17403 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017404 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 else
17408 eap->skip = TRUE;
17409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410 /* An error in a function call during evaluation of an expression in magic
17411 * braces should not cause the function not to be defined. */
17412 saved_did_emsg = did_emsg;
17413 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414
17415 /*
17416 * ":function func" with only function name: list function.
17417 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017418 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 {
17420 if (!ends_excmd(*skipwhite(p)))
17421 {
17422 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017423 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 }
17425 eap->nextcmd = check_nextcmd(p);
17426 if (eap->nextcmd != NULL)
17427 *p = NUL;
17428 if (!eap->skip && !got_int)
17429 {
17430 fp = find_func(name);
17431 if (fp != NULL)
17432 {
17433 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017434 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435 {
17436 msg_putchar('\n');
17437 msg_outnum((long)(j + 1));
17438 if (j < 9)
17439 msg_putchar(' ');
17440 if (j < 99)
17441 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017442 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443 out_flush(); /* show a line at a time */
17444 ui_breakcheck();
17445 }
17446 if (!got_int)
17447 {
17448 msg_putchar('\n');
17449 msg_puts((char_u *)" endfunction");
17450 }
17451 }
17452 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017453 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017455 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456 }
17457
17458 /*
17459 * ":function name(arg1, arg2)" Define function.
17460 */
17461 p = skipwhite(p);
17462 if (*p != '(')
17463 {
17464 if (!eap->skip)
17465 {
17466 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017467 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468 }
17469 /* attempt to continue by skipping some text */
17470 if (vim_strchr(p, '(') != NULL)
17471 p = vim_strchr(p, '(');
17472 }
17473 p = skipwhite(p + 1);
17474
17475 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17476 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17477
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017478 if (!eap->skip)
17479 {
17480 /* Check the name of the function. */
17481 if (name != NULL)
17482 arg = name;
17483 else
17484 arg = fudi.fd_newkey;
17485 if (arg != NULL)
17486 {
17487 if (*arg == K_SPECIAL)
17488 j = 3;
17489 else
17490 j = 0;
17491 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17492 : eval_isnamec(arg[j])))
17493 ++j;
17494 if (arg[j] != NUL)
17495 emsg_funcname(_(e_invarg2), arg);
17496 }
17497 }
17498
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499 /*
17500 * Isolate the arguments: "arg1, arg2, ...)"
17501 */
17502 while (*p != ')')
17503 {
17504 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17505 {
17506 varargs = TRUE;
17507 p += 3;
17508 mustend = TRUE;
17509 }
17510 else
17511 {
17512 arg = p;
17513 while (ASCII_ISALNUM(*p) || *p == '_')
17514 ++p;
17515 if (arg == p || isdigit(*arg)
17516 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17517 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17518 {
17519 if (!eap->skip)
17520 EMSG2(_("E125: Illegal argument: %s"), arg);
17521 break;
17522 }
17523 if (ga_grow(&newargs, 1) == FAIL)
17524 goto erret;
17525 c = *p;
17526 *p = NUL;
17527 arg = vim_strsave(arg);
17528 if (arg == NULL)
17529 goto erret;
17530 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17531 *p = c;
17532 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533 if (*p == ',')
17534 ++p;
17535 else
17536 mustend = TRUE;
17537 }
17538 p = skipwhite(p);
17539 if (mustend && *p != ')')
17540 {
17541 if (!eap->skip)
17542 EMSG2(_(e_invarg2), eap->arg);
17543 break;
17544 }
17545 }
17546 ++p; /* skip the ')' */
17547
Bram Moolenaare9a41262005-01-15 22:18:47 +000017548 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549 for (;;)
17550 {
17551 p = skipwhite(p);
17552 if (STRNCMP(p, "range", 5) == 0)
17553 {
17554 flags |= FC_RANGE;
17555 p += 5;
17556 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017557 else if (STRNCMP(p, "dict", 4) == 0)
17558 {
17559 flags |= FC_DICT;
17560 p += 4;
17561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 else if (STRNCMP(p, "abort", 5) == 0)
17563 {
17564 flags |= FC_ABORT;
17565 p += 5;
17566 }
17567 else
17568 break;
17569 }
17570
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017571 /* When there is a line break use what follows for the function body.
17572 * Makes 'exe "func Test()\n...\nendfunc"' work. */
17573 if (*p == '\n')
17574 line_arg = p + 1;
17575 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576 EMSG(_(e_trailing));
17577
17578 /*
17579 * Read the body of the function, until ":endfunction" is found.
17580 */
17581 if (KeyTyped)
17582 {
17583 /* Check if the function already exists, don't let the user type the
17584 * whole function before telling him it doesn't work! For a script we
17585 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017586 if (!eap->skip && !eap->forceit)
17587 {
17588 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17589 EMSG(_(e_funcdict));
17590 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017591 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017594 if (!eap->skip && did_emsg)
17595 goto erret;
17596
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597 msg_putchar('\n'); /* don't overwrite the function name */
17598 cmdline_row = msg_row;
17599 }
17600
17601 indent = 2;
17602 nesting = 0;
17603 for (;;)
17604 {
17605 msg_scroll = TRUE;
17606 need_wait_return = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017607 if (line_arg != NULL)
17608 {
17609 /* Use eap->arg, split up in parts by line breaks. */
17610 theline = line_arg;
17611 p = vim_strchr(theline, '\n');
17612 if (p == NULL)
17613 line_arg += STRLEN(line_arg);
17614 else
17615 {
17616 *p = NUL;
17617 line_arg = p + 1;
17618 }
17619 }
17620 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 theline = getcmdline(':', 0L, indent);
17622 else
17623 theline = eap->getline(':', eap->cookie, indent);
17624 if (KeyTyped)
17625 lines_left = Rows - 1;
17626 if (theline == NULL)
17627 {
17628 EMSG(_("E126: Missing :endfunction"));
17629 goto erret;
17630 }
17631
17632 if (skip_until != NULL)
17633 {
17634 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17635 * don't check for ":endfunc". */
17636 if (STRCMP(theline, skip_until) == 0)
17637 {
17638 vim_free(skip_until);
17639 skip_until = NULL;
17640 }
17641 }
17642 else
17643 {
17644 /* skip ':' and blanks*/
17645 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17646 ;
17647
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017648 /* Check for "endfunction". */
17649 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017651 if (line_arg == NULL)
17652 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 break;
17654 }
17655
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017656 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 * at "end". */
17658 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17659 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017660 else if (STRNCMP(p, "if", 2) == 0
17661 || STRNCMP(p, "wh", 2) == 0
17662 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 || STRNCMP(p, "try", 3) == 0)
17664 indent += 2;
17665
17666 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017667 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017669 if (*p == '!')
17670 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 p += eval_fname_script(p);
17672 if (ASCII_ISALPHA(*p))
17673 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017674 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 if (*skipwhite(p) == '(')
17676 {
17677 ++nesting;
17678 indent += 2;
17679 }
17680 }
17681 }
17682
17683 /* Check for ":append" or ":insert". */
17684 p = skip_range(p, NULL);
17685 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17686 || (p[0] == 'i'
17687 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17688 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17689 skip_until = vim_strsave((char_u *)".");
17690
17691 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17692 arg = skipwhite(skiptowhite(p));
17693 if (arg[0] == '<' && arg[1] =='<'
17694 && ((p[0] == 'p' && p[1] == 'y'
17695 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17696 || (p[0] == 'p' && p[1] == 'e'
17697 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17698 || (p[0] == 't' && p[1] == 'c'
17699 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17700 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17701 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017702 || (p[0] == 'm' && p[1] == 'z'
17703 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 ))
17705 {
17706 /* ":python <<" continues until a dot, like ":append" */
17707 p = skipwhite(arg + 2);
17708 if (*p == NUL)
17709 skip_until = vim_strsave((char_u *)".");
17710 else
17711 skip_until = vim_strsave(p);
17712 }
17713 }
17714
17715 /* Add the line to the function. */
17716 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017717 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017718 if (line_arg == NULL)
17719 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017721 }
17722
17723 /* Copy the line to newly allocated memory. get_one_sourceline()
17724 * allocates 250 bytes per line, this saves 80% on average. The cost
17725 * is an extra alloc/free. */
17726 p = vim_strsave(theline);
17727 if (p != NULL)
17728 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017729 if (line_arg == NULL)
17730 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017731 theline = p;
17732 }
17733
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17735 newlines.ga_len++;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017736
17737 /* Check for end of eap->arg. */
17738 if (line_arg != NULL && *line_arg == NUL)
17739 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740 }
17741
17742 /* Don't define the function when skipping commands or when an error was
17743 * detected. */
17744 if (eap->skip || did_emsg)
17745 goto erret;
17746
17747 /*
17748 * If there are no errors, add the function
17749 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017750 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017751 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017752 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017753 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017754 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017755 emsg_funcname("E707: Function name conflicts with variable: %s",
17756 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017757 goto erret;
17758 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017759
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017760 fp = find_func(name);
17761 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017763 if (!eap->forceit)
17764 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017765 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017766 goto erret;
17767 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017768 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017769 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017770 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017771 name);
17772 goto erret;
17773 }
17774 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017775 ga_clear_strings(&(fp->uf_args));
17776 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017777 vim_free(name);
17778 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780 }
17781 else
17782 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017783 char numbuf[20];
17784
17785 fp = NULL;
17786 if (fudi.fd_newkey == NULL && !eap->forceit)
17787 {
17788 EMSG(_(e_funcdict));
17789 goto erret;
17790 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017791 if (fudi.fd_di == NULL)
17792 {
17793 /* Can't add a function to a locked dictionary */
17794 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17795 goto erret;
17796 }
17797 /* Can't change an existing function if it is locked */
17798 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17799 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017800
17801 /* Give the function a sequential number. Can only be used with a
17802 * Funcref! */
17803 vim_free(name);
17804 sprintf(numbuf, "%d", ++func_nr);
17805 name = vim_strsave((char_u *)numbuf);
17806 if (name == NULL)
17807 goto erret;
17808 }
17809
17810 if (fp == NULL)
17811 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017812 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017813 {
17814 int slen, plen;
17815 char_u *scriptname;
17816
17817 /* Check that the autoload name matches the script name. */
17818 j = FAIL;
17819 if (sourcing_name != NULL)
17820 {
17821 scriptname = autoload_name(name);
17822 if (scriptname != NULL)
17823 {
17824 p = vim_strchr(scriptname, '/');
17825 plen = STRLEN(p);
17826 slen = STRLEN(sourcing_name);
17827 if (slen > plen && fnamecmp(p,
17828 sourcing_name + slen - plen) == 0)
17829 j = OK;
17830 vim_free(scriptname);
17831 }
17832 }
17833 if (j == FAIL)
17834 {
17835 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17836 goto erret;
17837 }
17838 }
17839
17840 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841 if (fp == NULL)
17842 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017843
17844 if (fudi.fd_dict != NULL)
17845 {
17846 if (fudi.fd_di == NULL)
17847 {
17848 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017849 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017850 if (fudi.fd_di == NULL)
17851 {
17852 vim_free(fp);
17853 goto erret;
17854 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017855 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17856 {
17857 vim_free(fudi.fd_di);
17858 goto erret;
17859 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017860 }
17861 else
17862 /* overwrite existing dict entry */
17863 clear_tv(&fudi.fd_di->di_tv);
17864 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017865 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017866 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017867 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017868 }
17869
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017871 STRCPY(fp->uf_name, name);
17872 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017874 fp->uf_args = newargs;
17875 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017876#ifdef FEAT_PROFILE
17877 fp->uf_tml_count = NULL;
17878 fp->uf_tml_total = NULL;
17879 fp->uf_tml_self = NULL;
17880 fp->uf_profiling = FALSE;
17881 if (prof_def_func())
17882 func_do_profile(fp);
17883#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017884 fp->uf_varargs = varargs;
17885 fp->uf_flags = flags;
17886 fp->uf_calls = 0;
17887 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017888 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889
17890erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891 ga_clear_strings(&newargs);
17892 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017893ret_free:
17894 vim_free(skip_until);
17895 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898}
17899
17900/*
17901 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017902 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017904 * flags:
17905 * TFN_INT: internal function name OK
17906 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907 * Advances "pp" to just after the function name (if no error).
17908 */
17909 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017910trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911 char_u **pp;
17912 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017913 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017914 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017916 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 char_u *start;
17918 char_u *end;
17919 int lead;
17920 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017922 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017923
17924 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017925 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017927
17928 /* Check for hard coded <SNR>: already translated function ID (from a user
17929 * command). */
17930 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17931 && (*pp)[2] == (int)KE_SNR)
17932 {
17933 *pp += 3;
17934 len = get_id_len(pp) + 3;
17935 return vim_strnsave(start, len);
17936 }
17937
17938 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17939 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017941 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017943
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017944 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17945 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017946 if (end == start)
17947 {
17948 if (!skip)
17949 EMSG(_("E129: Function name required"));
17950 goto theend;
17951 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017952 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017953 {
17954 /*
17955 * Report an invalid expression in braces, unless the expression
17956 * evaluation has been cancelled due to an aborting error, an
17957 * interrupt, or an exception.
17958 */
17959 if (!aborting())
17960 {
17961 if (end != NULL)
17962 EMSG2(_(e_invarg2), start);
17963 }
17964 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017965 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017966 goto theend;
17967 }
17968
17969 if (lv.ll_tv != NULL)
17970 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017971 if (fdp != NULL)
17972 {
17973 fdp->fd_dict = lv.ll_dict;
17974 fdp->fd_newkey = lv.ll_newkey;
17975 lv.ll_newkey = NULL;
17976 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017977 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017978 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17979 {
17980 name = vim_strsave(lv.ll_tv->vval.v_string);
17981 *pp = end;
17982 }
17983 else
17984 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017985 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17986 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017987 EMSG(_(e_funcref));
17988 else
17989 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017990 name = NULL;
17991 }
17992 goto theend;
17993 }
17994
17995 if (lv.ll_name == NULL)
17996 {
17997 /* Error found, but continue after the function name. */
17998 *pp = end;
17999 goto theend;
18000 }
18001
18002 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018003 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018004 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018005 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18006 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18007 {
18008 /* When there was "s:" already or the name expanded to get a
18009 * leading "s:" then remove it. */
18010 lv.ll_name += 2;
18011 len -= 2;
18012 lead = 2;
18013 }
18014 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018015 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018016 {
18017 if (lead == 2) /* skip over "s:" */
18018 lv.ll_name += 2;
18019 len = (int)(end - lv.ll_name);
18020 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018021
18022 /*
18023 * Copy the function name to allocated memory.
18024 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18025 * Accept <SNR>123_name() outside a script.
18026 */
18027 if (skip)
18028 lead = 0; /* do nothing */
18029 else if (lead > 0)
18030 {
18031 lead = 3;
18032 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18033 {
18034 if (current_SID <= 0)
18035 {
18036 EMSG(_(e_usingsid));
18037 goto theend;
18038 }
18039 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18040 lead += (int)STRLEN(sid_buf);
18041 }
18042 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018043 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018044 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018045 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018046 goto theend;
18047 }
18048 name = alloc((unsigned)(len + lead + 1));
18049 if (name != NULL)
18050 {
18051 if (lead > 0)
18052 {
18053 name[0] = K_SPECIAL;
18054 name[1] = KS_EXTRA;
18055 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018056 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018057 STRCPY(name + 3, sid_buf);
18058 }
18059 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18060 name[len + lead] = NUL;
18061 }
18062 *pp = end;
18063
18064theend:
18065 clear_lval(&lv);
18066 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067}
18068
18069/*
18070 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18071 * Return 2 if "p" starts with "s:".
18072 * Return 0 otherwise.
18073 */
18074 static int
18075eval_fname_script(p)
18076 char_u *p;
18077{
18078 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18079 || STRNICMP(p + 1, "SNR>", 4) == 0))
18080 return 5;
18081 if (p[0] == 's' && p[1] == ':')
18082 return 2;
18083 return 0;
18084}
18085
18086/*
18087 * Return TRUE if "p" starts with "<SID>" or "s:".
18088 * Only works if eval_fname_script() returned non-zero for "p"!
18089 */
18090 static int
18091eval_fname_sid(p)
18092 char_u *p;
18093{
18094 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18095}
18096
18097/*
18098 * List the head of the function: "name(arg1, arg2)".
18099 */
18100 static void
18101list_func_head(fp, indent)
18102 ufunc_T *fp;
18103 int indent;
18104{
18105 int j;
18106
18107 msg_start();
18108 if (indent)
18109 MSG_PUTS(" ");
18110 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018111 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 {
18113 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018114 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 }
18116 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018117 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018119 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 {
18121 if (j)
18122 MSG_PUTS(", ");
18123 msg_puts(FUNCARG(fp, j));
18124 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018125 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126 {
18127 if (j)
18128 MSG_PUTS(", ");
18129 MSG_PUTS("...");
18130 }
18131 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018132 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018133 if (p_verbose > 0)
18134 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135}
18136
18137/*
18138 * Find a function by name, return pointer to it in ufuncs.
18139 * Return NULL for unknown function.
18140 */
18141 static ufunc_T *
18142find_func(name)
18143 char_u *name;
18144{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018145 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018147 hi = hash_find(&func_hashtab, name);
18148 if (!HASHITEM_EMPTY(hi))
18149 return HI2UF(hi);
18150 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151}
18152
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018153#if defined(EXITFREE) || defined(PROTO)
18154 void
18155free_all_functions()
18156{
18157 hashitem_T *hi;
18158
18159 /* Need to start all over every time, because func_free() may change the
18160 * hash table. */
18161 while (func_hashtab.ht_used > 0)
18162 for (hi = func_hashtab.ht_array; ; ++hi)
18163 if (!HASHITEM_EMPTY(hi))
18164 {
18165 func_free(HI2UF(hi));
18166 break;
18167 }
18168}
18169#endif
18170
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018171/*
18172 * Return TRUE if a function "name" exists.
18173 */
18174 static int
18175function_exists(name)
18176 char_u *name;
18177{
18178 char_u *p = name;
18179 int n = FALSE;
18180
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018181 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018182 if (p != NULL)
18183 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018184 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018185 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018186 else
18187 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018188 vim_free(p);
18189 }
18190 return n;
18191}
18192
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018193/*
18194 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018195 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018196 */
18197 static int
18198builtin_function(name)
18199 char_u *name;
18200{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018201 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18202 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018203}
18204
Bram Moolenaar05159a02005-02-26 23:04:13 +000018205#if defined(FEAT_PROFILE) || defined(PROTO)
18206/*
18207 * Start profiling function "fp".
18208 */
18209 static void
18210func_do_profile(fp)
18211 ufunc_T *fp;
18212{
18213 fp->uf_tm_count = 0;
18214 profile_zero(&fp->uf_tm_self);
18215 profile_zero(&fp->uf_tm_total);
18216 if (fp->uf_tml_count == NULL)
18217 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18218 (sizeof(int) * fp->uf_lines.ga_len));
18219 if (fp->uf_tml_total == NULL)
18220 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18221 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18222 if (fp->uf_tml_self == NULL)
18223 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18224 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18225 fp->uf_tml_idx = -1;
18226 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18227 || fp->uf_tml_self == NULL)
18228 return; /* out of memory */
18229
18230 fp->uf_profiling = TRUE;
18231}
18232
18233/*
18234 * Dump the profiling results for all functions in file "fd".
18235 */
18236 void
18237func_dump_profile(fd)
18238 FILE *fd;
18239{
18240 hashitem_T *hi;
18241 int todo;
18242 ufunc_T *fp;
18243 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018244 ufunc_T **sorttab;
18245 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018246
18247 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018248 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18249
Bram Moolenaar05159a02005-02-26 23:04:13 +000018250 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18251 {
18252 if (!HASHITEM_EMPTY(hi))
18253 {
18254 --todo;
18255 fp = HI2UF(hi);
18256 if (fp->uf_profiling)
18257 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018258 if (sorttab != NULL)
18259 sorttab[st_len++] = fp;
18260
Bram Moolenaar05159a02005-02-26 23:04:13 +000018261 if (fp->uf_name[0] == K_SPECIAL)
18262 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18263 else
18264 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18265 if (fp->uf_tm_count == 1)
18266 fprintf(fd, "Called 1 time\n");
18267 else
18268 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18269 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18270 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18271 fprintf(fd, "\n");
18272 fprintf(fd, "count total (s) self (s)\n");
18273
18274 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18275 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018276 prof_func_line(fd, fp->uf_tml_count[i],
18277 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018278 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18279 }
18280 fprintf(fd, "\n");
18281 }
18282 }
18283 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018284
18285 if (sorttab != NULL && st_len > 0)
18286 {
18287 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18288 prof_total_cmp);
18289 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18290 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18291 prof_self_cmp);
18292 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18293 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018294}
Bram Moolenaar73830342005-02-28 22:48:19 +000018295
18296 static void
18297prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18298 FILE *fd;
18299 ufunc_T **sorttab;
18300 int st_len;
18301 char *title;
18302 int prefer_self; /* when equal print only self time */
18303{
18304 int i;
18305 ufunc_T *fp;
18306
18307 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18308 fprintf(fd, "count total (s) self (s) function\n");
18309 for (i = 0; i < 20 && i < st_len; ++i)
18310 {
18311 fp = sorttab[i];
18312 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18313 prefer_self);
18314 if (fp->uf_name[0] == K_SPECIAL)
18315 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18316 else
18317 fprintf(fd, " %s()\n", fp->uf_name);
18318 }
18319 fprintf(fd, "\n");
18320}
18321
18322/*
18323 * Print the count and times for one function or function line.
18324 */
18325 static void
18326prof_func_line(fd, count, total, self, prefer_self)
18327 FILE *fd;
18328 int count;
18329 proftime_T *total;
18330 proftime_T *self;
18331 int prefer_self; /* when equal print only self time */
18332{
18333 if (count > 0)
18334 {
18335 fprintf(fd, "%5d ", count);
18336 if (prefer_self && profile_equal(total, self))
18337 fprintf(fd, " ");
18338 else
18339 fprintf(fd, "%s ", profile_msg(total));
18340 if (!prefer_self && profile_equal(total, self))
18341 fprintf(fd, " ");
18342 else
18343 fprintf(fd, "%s ", profile_msg(self));
18344 }
18345 else
18346 fprintf(fd, " ");
18347}
18348
18349/*
18350 * Compare function for total time sorting.
18351 */
18352 static int
18353#ifdef __BORLANDC__
18354_RTLENTRYF
18355#endif
18356prof_total_cmp(s1, s2)
18357 const void *s1;
18358 const void *s2;
18359{
18360 ufunc_T *p1, *p2;
18361
18362 p1 = *(ufunc_T **)s1;
18363 p2 = *(ufunc_T **)s2;
18364 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18365}
18366
18367/*
18368 * Compare function for self time sorting.
18369 */
18370 static int
18371#ifdef __BORLANDC__
18372_RTLENTRYF
18373#endif
18374prof_self_cmp(s1, s2)
18375 const void *s1;
18376 const void *s2;
18377{
18378 ufunc_T *p1, *p2;
18379
18380 p1 = *(ufunc_T **)s1;
18381 p2 = *(ufunc_T **)s2;
18382 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18383}
18384
Bram Moolenaar05159a02005-02-26 23:04:13 +000018385#endif
18386
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018387/* The names of packages that once were loaded is remembered. */
18388static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18389
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018390/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018391 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018392 * Return TRUE if a package was loaded.
18393 */
18394 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018395script_autoload(name, reload)
18396 char_u *name;
18397 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018398{
18399 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018400 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018401 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018402 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018403
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018404 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018405 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018406 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018407 return FALSE;
18408
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018409 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018410
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018411 /* Find the name in the list of previously loaded package names. Skip
18412 * "autoload/", it's always the same. */
18413 for (i = 0; i < ga_loaded.ga_len; ++i)
18414 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18415 break;
18416 if (!reload && i < ga_loaded.ga_len)
18417 ret = FALSE; /* was loaded already */
18418 else
18419 {
18420 /* Remember the name if it wasn't loaded already. */
18421 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18422 {
18423 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18424 tofree = NULL;
18425 }
18426
18427 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018428 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018429 ret = TRUE;
18430 }
18431
18432 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018433 return ret;
18434}
18435
18436/*
18437 * Return the autoload script name for a function or variable name.
18438 * Returns NULL when out of memory.
18439 */
18440 static char_u *
18441autoload_name(name)
18442 char_u *name;
18443{
18444 char_u *p;
18445 char_u *scriptname;
18446
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018447 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018448 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18449 if (scriptname == NULL)
18450 return FALSE;
18451 STRCPY(scriptname, "autoload/");
18452 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018453 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018454 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018455 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018456 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018457 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018458}
18459
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18461
18462/*
18463 * Function given to ExpandGeneric() to obtain the list of user defined
18464 * function names.
18465 */
18466 char_u *
18467get_user_func_name(xp, idx)
18468 expand_T *xp;
18469 int idx;
18470{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018471 static long_u done;
18472 static hashitem_T *hi;
18473 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474
18475 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018477 done = 0;
18478 hi = func_hashtab.ht_array;
18479 }
18480 if (done < func_hashtab.ht_used)
18481 {
18482 if (done++ > 0)
18483 ++hi;
18484 while (HASHITEM_EMPTY(hi))
18485 ++hi;
18486 fp = HI2UF(hi);
18487
18488 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18489 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490
18491 cat_func_name(IObuff, fp);
18492 if (xp->xp_context != EXPAND_USER_FUNC)
18493 {
18494 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018495 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496 STRCAT(IObuff, ")");
18497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 return IObuff;
18499 }
18500 return NULL;
18501}
18502
18503#endif /* FEAT_CMDL_COMPL */
18504
18505/*
18506 * Copy the function name of "fp" to buffer "buf".
18507 * "buf" must be able to hold the function name plus three bytes.
18508 * Takes care of script-local function names.
18509 */
18510 static void
18511cat_func_name(buf, fp)
18512 char_u *buf;
18513 ufunc_T *fp;
18514{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018515 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516 {
18517 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018518 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519 }
18520 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018521 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522}
18523
18524/*
18525 * ":delfunction {name}"
18526 */
18527 void
18528ex_delfunction(eap)
18529 exarg_T *eap;
18530{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018531 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532 char_u *p;
18533 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018534 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535
18536 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018537 name = trans_function_name(&p, eap->skip, 0, &fudi);
18538 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018540 {
18541 if (fudi.fd_dict != NULL && !eap->skip)
18542 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 if (!ends_excmd(*skipwhite(p)))
18546 {
18547 vim_free(name);
18548 EMSG(_(e_trailing));
18549 return;
18550 }
18551 eap->nextcmd = check_nextcmd(p);
18552 if (eap->nextcmd != NULL)
18553 *p = NUL;
18554
18555 if (!eap->skip)
18556 fp = find_func(name);
18557 vim_free(name);
18558
18559 if (!eap->skip)
18560 {
18561 if (fp == NULL)
18562 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018563 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564 return;
18565 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018566 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 {
18568 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18569 return;
18570 }
18571
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018572 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018574 /* Delete the dict item that refers to the function, it will
18575 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018576 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018578 else
18579 func_free(fp);
18580 }
18581}
18582
18583/*
18584 * Free a function and remove it from the list of functions.
18585 */
18586 static void
18587func_free(fp)
18588 ufunc_T *fp;
18589{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018590 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018591
18592 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018593 ga_clear_strings(&(fp->uf_args));
18594 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018595#ifdef FEAT_PROFILE
18596 vim_free(fp->uf_tml_count);
18597 vim_free(fp->uf_tml_total);
18598 vim_free(fp->uf_tml_self);
18599#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018600
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018601 /* remove the function from the function hashtable */
18602 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18603 if (HASHITEM_EMPTY(hi))
18604 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018605 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018606 hash_remove(&func_hashtab, hi);
18607
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018608 vim_free(fp);
18609}
18610
18611/*
18612 * Unreference a Function: decrement the reference count and free it when it
18613 * becomes zero. Only for numbered functions.
18614 */
18615 static void
18616func_unref(name)
18617 char_u *name;
18618{
18619 ufunc_T *fp;
18620
18621 if (name != NULL && isdigit(*name))
18622 {
18623 fp = find_func(name);
18624 if (fp == NULL)
18625 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018626 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018627 {
18628 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018629 * when "uf_calls" becomes zero. */
18630 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018631 func_free(fp);
18632 }
18633 }
18634}
18635
18636/*
18637 * Count a reference to a Function.
18638 */
18639 static void
18640func_ref(name)
18641 char_u *name;
18642{
18643 ufunc_T *fp;
18644
18645 if (name != NULL && isdigit(*name))
18646 {
18647 fp = find_func(name);
18648 if (fp == NULL)
18649 EMSG2(_(e_intern2), "func_ref()");
18650 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018651 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 }
18653}
18654
18655/*
18656 * Call a user function.
18657 */
18658 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018659call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 ufunc_T *fp; /* pointer to function */
18661 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018662 typval_T *argvars; /* arguments */
18663 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 linenr_T firstline; /* first line of range */
18665 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018666 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667{
Bram Moolenaar33570922005-01-25 22:26:29 +000018668 char_u *save_sourcing_name;
18669 linenr_T save_sourcing_lnum;
18670 scid_T save_current_SID;
18671 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018672 int save_did_emsg;
18673 static int depth = 0;
18674 dictitem_T *v;
18675 int fixvar_idx = 0; /* index in fixvar[] */
18676 int i;
18677 int ai;
18678 char_u numbuf[NUMBUFLEN];
18679 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018680#ifdef FEAT_PROFILE
18681 proftime_T wait_start;
18682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683
18684 /* If depth of calling is getting too high, don't execute the function */
18685 if (depth >= p_mfd)
18686 {
18687 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018688 rettv->v_type = VAR_NUMBER;
18689 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 return;
18691 }
18692 ++depth;
18693
18694 line_breakcheck(); /* check for CTRL-C hit */
18695
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018696 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018697 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018699 fc.rettv = rettv;
18700 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701 fc.linenr = 0;
18702 fc.returned = FALSE;
18703 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018705 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706 fc.dbg_tick = debug_tick;
18707
Bram Moolenaar33570922005-01-25 22:26:29 +000018708 /*
18709 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18710 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18711 * each argument variable and saves a lot of time.
18712 */
18713 /*
18714 * Init l: variables.
18715 */
18716 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018717 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018718 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018719 /* Set l:self to "selfdict". */
18720 v = &fc.fixvar[fixvar_idx++].var;
18721 STRCPY(v->di_key, "self");
18722 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18723 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18724 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018725 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018726 v->di_tv.vval.v_dict = selfdict;
18727 ++selfdict->dv_refcount;
18728 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018729
Bram Moolenaar33570922005-01-25 22:26:29 +000018730 /*
18731 * Init a: variables.
18732 * Set a:0 to "argcount".
18733 * Set a:000 to a list with room for the "..." arguments.
18734 */
18735 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18736 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018737 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018738 v = &fc.fixvar[fixvar_idx++].var;
18739 STRCPY(v->di_key, "000");
18740 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18741 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18742 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018743 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018744 v->di_tv.vval.v_list = &fc.l_varlist;
18745 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18746 fc.l_varlist.lv_refcount = 99999;
18747
18748 /*
18749 * Set a:firstline to "firstline" and a:lastline to "lastline".
18750 * Set a:name to named arguments.
18751 * Set a:N to the "..." arguments.
18752 */
18753 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18754 (varnumber_T)firstline);
18755 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18756 (varnumber_T)lastline);
18757 for (i = 0; i < argcount; ++i)
18758 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018759 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018760 if (ai < 0)
18761 /* named argument a:name */
18762 name = FUNCARG(fp, i);
18763 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018764 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018765 /* "..." argument a:1, a:2, etc. */
18766 sprintf((char *)numbuf, "%d", ai + 1);
18767 name = numbuf;
18768 }
18769 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18770 {
18771 v = &fc.fixvar[fixvar_idx++].var;
18772 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18773 }
18774 else
18775 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018776 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18777 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018778 if (v == NULL)
18779 break;
18780 v->di_flags = DI_FLAGS_RO;
18781 }
18782 STRCPY(v->di_key, name);
18783 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18784
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018785 /* Note: the values are copied directly to avoid alloc/free.
18786 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018787 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018788 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018789
18790 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18791 {
18792 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18793 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018794 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018795 }
18796 }
18797
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 /* Don't redraw while executing the function. */
18799 ++RedrawingDisabled;
18800 save_sourcing_name = sourcing_name;
18801 save_sourcing_lnum = sourcing_lnum;
18802 sourcing_lnum = 1;
18803 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018804 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805 if (sourcing_name != NULL)
18806 {
18807 if (save_sourcing_name != NULL
18808 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18809 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18810 else
18811 STRCPY(sourcing_name, "function ");
18812 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18813
18814 if (p_verbose >= 12)
18815 {
18816 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018817 verbose_enter_scroll();
18818
Bram Moolenaar555b2802005-05-19 21:08:39 +000018819 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 if (p_verbose >= 14)
18821 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018823 char_u numbuf[NUMBUFLEN];
18824 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825
18826 msg_puts((char_u *)"(");
18827 for (i = 0; i < argcount; ++i)
18828 {
18829 if (i > 0)
18830 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018831 if (argvars[i].v_type == VAR_NUMBER)
18832 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833 else
18834 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018835 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018836 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018838 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839 }
18840 }
18841 msg_puts((char_u *)")");
18842 }
18843 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018844
18845 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846 --no_wait_return;
18847 }
18848 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018849#ifdef FEAT_PROFILE
18850 if (do_profiling)
18851 {
18852 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18853 func_do_profile(fp);
18854 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018855 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018856 {
18857 ++fp->uf_tm_count;
18858 profile_start(&fp->uf_tm_start);
18859 profile_zero(&fp->uf_tm_children);
18860 }
18861 script_prof_save(&wait_start);
18862 }
18863#endif
18864
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018866 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867 save_did_emsg = did_emsg;
18868 did_emsg = FALSE;
18869
18870 /* call do_cmdline() to execute the lines */
18871 do_cmdline(NULL, get_func_line, (void *)&fc,
18872 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18873
18874 --RedrawingDisabled;
18875
18876 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018877 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018879 clear_tv(rettv);
18880 rettv->v_type = VAR_NUMBER;
18881 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882 }
18883
Bram Moolenaar05159a02005-02-26 23:04:13 +000018884#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018885 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018886 {
18887 profile_end(&fp->uf_tm_start);
18888 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18889 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18890 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18891 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018892 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018893 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018894 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18895 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018896 }
18897 }
18898#endif
18899
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900 /* when being verbose, mention the return value */
18901 if (p_verbose >= 12)
18902 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018904 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018907 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018908 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018909 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18910 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018911 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018913 char_u buf[MSG_BUF_LEN];
18914 char_u numbuf[NUMBUFLEN];
18915 char_u *tofree;
18916
Bram Moolenaar555b2802005-05-19 21:08:39 +000018917 /* The value may be very long. Skip the middle part, so that we
18918 * have some idea how it starts and ends. smsg() would always
18919 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018920 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018921 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018922 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018923 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924 }
18925 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018926
18927 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 --no_wait_return;
18929 }
18930
18931 vim_free(sourcing_name);
18932 sourcing_name = save_sourcing_name;
18933 sourcing_lnum = save_sourcing_lnum;
18934 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018935#ifdef FEAT_PROFILE
18936 if (do_profiling)
18937 script_prof_restore(&wait_start);
18938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939
18940 if (p_verbose >= 12 && sourcing_name != NULL)
18941 {
18942 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018943 verbose_enter_scroll();
18944
Bram Moolenaar555b2802005-05-19 21:08:39 +000018945 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018947
18948 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 --no_wait_return;
18950 }
18951
18952 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018953 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954
Bram Moolenaar33570922005-01-25 22:26:29 +000018955 /* The a: variables typevals were not alloced, only free the allocated
18956 * variables. */
18957 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18958
18959 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960 --depth;
18961}
18962
18963/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018964 * Add a number variable "name" to dict "dp" with value "nr".
18965 */
18966 static void
18967add_nr_var(dp, v, name, nr)
18968 dict_T *dp;
18969 dictitem_T *v;
18970 char *name;
18971 varnumber_T nr;
18972{
18973 STRCPY(v->di_key, name);
18974 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18975 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18976 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018977 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018978 v->di_tv.vval.v_number = nr;
18979}
18980
18981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 * ":return [expr]"
18983 */
18984 void
18985ex_return(eap)
18986 exarg_T *eap;
18987{
18988 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018989 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 int returning = FALSE;
18991
18992 if (current_funccal == NULL)
18993 {
18994 EMSG(_("E133: :return not inside a function"));
18995 return;
18996 }
18997
18998 if (eap->skip)
18999 ++emsg_skip;
19000
19001 eap->nextcmd = NULL;
19002 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019003 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004 {
19005 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019006 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019008 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009 }
19010 /* It's safer to return also on error. */
19011 else if (!eap->skip)
19012 {
19013 /*
19014 * Return unless the expression evaluation has been cancelled due to an
19015 * aborting error, an interrupt, or an exception.
19016 */
19017 if (!aborting())
19018 returning = do_return(eap, FALSE, TRUE, NULL);
19019 }
19020
19021 /* When skipping or the return gets pending, advance to the next command
19022 * in this line (!returning). Otherwise, ignore the rest of the line.
19023 * Following lines will be ignored by get_func_line(). */
19024 if (returning)
19025 eap->nextcmd = NULL;
19026 else if (eap->nextcmd == NULL) /* no argument */
19027 eap->nextcmd = check_nextcmd(arg);
19028
19029 if (eap->skip)
19030 --emsg_skip;
19031}
19032
19033/*
19034 * Return from a function. Possibly makes the return pending. Also called
19035 * for a pending return at the ":endtry" or after returning from an extra
19036 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019037 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019038 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039 * FALSE when the return gets pending.
19040 */
19041 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019042do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043 exarg_T *eap;
19044 int reanimate;
19045 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019046 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047{
19048 int idx;
19049 struct condstack *cstack = eap->cstack;
19050
19051 if (reanimate)
19052 /* Undo the return. */
19053 current_funccal->returned = FALSE;
19054
19055 /*
19056 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19057 * not in its finally clause (which then is to be executed next) is found.
19058 * In this case, make the ":return" pending for execution at the ":endtry".
19059 * Otherwise, return normally.
19060 */
19061 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19062 if (idx >= 0)
19063 {
19064 cstack->cs_pending[idx] = CSTP_RETURN;
19065
19066 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019067 /* A pending return again gets pending. "rettv" points to an
19068 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019070 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 else
19072 {
19073 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019074 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019076 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019078 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 {
19080 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019081 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019082 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083 else
19084 EMSG(_(e_outofmem));
19085 }
19086 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019087 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088
19089 if (reanimate)
19090 {
19091 /* The pending return value could be overwritten by a ":return"
19092 * without argument in a finally clause; reset the default
19093 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019094 current_funccal->rettv->v_type = VAR_NUMBER;
19095 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096 }
19097 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019098 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099 }
19100 else
19101 {
19102 current_funccal->returned = TRUE;
19103
19104 /* If the return is carried out now, store the return value. For
19105 * a return immediately after reanimation, the value is already
19106 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019107 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019109 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019110 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019112 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 }
19114 }
19115
19116 return idx < 0;
19117}
19118
19119/*
19120 * Free the variable with a pending return value.
19121 */
19122 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019123discard_pending_return(rettv)
19124 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125{
Bram Moolenaar33570922005-01-25 22:26:29 +000019126 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127}
19128
19129/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019130 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 * is an allocated string. Used by report_pending() for verbose messages.
19132 */
19133 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019134get_return_cmd(rettv)
19135 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019137 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019138 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019139 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019141 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019142 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019143 if (s == NULL)
19144 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019145
19146 STRCPY(IObuff, ":return ");
19147 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19148 if (STRLEN(s) + 8 >= IOSIZE)
19149 STRCPY(IObuff + IOSIZE - 4, "...");
19150 vim_free(tofree);
19151 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152}
19153
19154/*
19155 * Get next function line.
19156 * Called by do_cmdline() to get the next line.
19157 * Returns allocated string, or NULL for end of function.
19158 */
19159/* ARGSUSED */
19160 char_u *
19161get_func_line(c, cookie, indent)
19162 int c; /* not used */
19163 void *cookie;
19164 int indent; /* not used */
19165{
Bram Moolenaar33570922005-01-25 22:26:29 +000019166 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019167 ufunc_T *fp = fcp->func;
19168 char_u *retval;
19169 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170
19171 /* If breakpoints have been added/deleted need to check for it. */
19172 if (fcp->dbg_tick != debug_tick)
19173 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019174 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175 sourcing_lnum);
19176 fcp->dbg_tick = debug_tick;
19177 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019178#ifdef FEAT_PROFILE
19179 if (do_profiling)
19180 func_line_end(cookie);
19181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182
Bram Moolenaar05159a02005-02-26 23:04:13 +000019183 gap = &fp->uf_lines;
19184 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185 retval = NULL;
19186 else if (fcp->returned || fcp->linenr >= gap->ga_len)
19187 retval = NULL;
19188 else
19189 {
19190 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19191 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019192#ifdef FEAT_PROFILE
19193 if (do_profiling)
19194 func_line_start(cookie);
19195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196 }
19197
19198 /* Did we encounter a breakpoint? */
19199 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19200 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019201 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019203 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204 sourcing_lnum);
19205 fcp->dbg_tick = debug_tick;
19206 }
19207
19208 return retval;
19209}
19210
Bram Moolenaar05159a02005-02-26 23:04:13 +000019211#if defined(FEAT_PROFILE) || defined(PROTO)
19212/*
19213 * Called when starting to read a function line.
19214 * "sourcing_lnum" must be correct!
19215 * When skipping lines it may not actually be executed, but we won't find out
19216 * until later and we need to store the time now.
19217 */
19218 void
19219func_line_start(cookie)
19220 void *cookie;
19221{
19222 funccall_T *fcp = (funccall_T *)cookie;
19223 ufunc_T *fp = fcp->func;
19224
19225 if (fp->uf_profiling && sourcing_lnum >= 1
19226 && sourcing_lnum <= fp->uf_lines.ga_len)
19227 {
19228 fp->uf_tml_idx = sourcing_lnum - 1;
19229 fp->uf_tml_execed = FALSE;
19230 profile_start(&fp->uf_tml_start);
19231 profile_zero(&fp->uf_tml_children);
19232 profile_get_wait(&fp->uf_tml_wait);
19233 }
19234}
19235
19236/*
19237 * Called when actually executing a function line.
19238 */
19239 void
19240func_line_exec(cookie)
19241 void *cookie;
19242{
19243 funccall_T *fcp = (funccall_T *)cookie;
19244 ufunc_T *fp = fcp->func;
19245
19246 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19247 fp->uf_tml_execed = TRUE;
19248}
19249
19250/*
19251 * Called when done with a function line.
19252 */
19253 void
19254func_line_end(cookie)
19255 void *cookie;
19256{
19257 funccall_T *fcp = (funccall_T *)cookie;
19258 ufunc_T *fp = fcp->func;
19259
19260 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19261 {
19262 if (fp->uf_tml_execed)
19263 {
19264 ++fp->uf_tml_count[fp->uf_tml_idx];
19265 profile_end(&fp->uf_tml_start);
19266 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
19267 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
19268 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
19269 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
19270 }
19271 fp->uf_tml_idx = -1;
19272 }
19273}
19274#endif
19275
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276/*
19277 * Return TRUE if the currently active function should be ended, because a
19278 * return was encountered or an error occured. Used inside a ":while".
19279 */
19280 int
19281func_has_ended(cookie)
19282 void *cookie;
19283{
Bram Moolenaar33570922005-01-25 22:26:29 +000019284 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285
19286 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19287 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019288 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289 || fcp->returned);
19290}
19291
19292/*
19293 * return TRUE if cookie indicates a function which "abort"s on errors.
19294 */
19295 int
19296func_has_abort(cookie)
19297 void *cookie;
19298{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019299 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300}
19301
19302#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19303typedef enum
19304{
19305 VAR_FLAVOUR_DEFAULT,
19306 VAR_FLAVOUR_SESSION,
19307 VAR_FLAVOUR_VIMINFO
19308} var_flavour_T;
19309
19310static var_flavour_T var_flavour __ARGS((char_u *varname));
19311
19312 static var_flavour_T
19313var_flavour(varname)
19314 char_u *varname;
19315{
19316 char_u *p = varname;
19317
19318 if (ASCII_ISUPPER(*p))
19319 {
19320 while (*(++p))
19321 if (ASCII_ISLOWER(*p))
19322 return VAR_FLAVOUR_SESSION;
19323 return VAR_FLAVOUR_VIMINFO;
19324 }
19325 else
19326 return VAR_FLAVOUR_DEFAULT;
19327}
19328#endif
19329
19330#if defined(FEAT_VIMINFO) || defined(PROTO)
19331/*
19332 * Restore global vars that start with a capital from the viminfo file
19333 */
19334 int
19335read_viminfo_varlist(virp, writing)
19336 vir_T *virp;
19337 int writing;
19338{
19339 char_u *tab;
19340 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342
19343 if (!writing && (find_viminfo_parameter('!') != NULL))
19344 {
19345 tab = vim_strchr(virp->vir_line + 1, '\t');
19346 if (tab != NULL)
19347 {
19348 *tab++ = '\0'; /* isolate the variable name */
19349 if (*tab == 'S') /* string var */
19350 is_string = TRUE;
19351
19352 tab = vim_strchr(tab, '\t');
19353 if (tab != NULL)
19354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355 if (is_string)
19356 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019357 tv.v_type = VAR_STRING;
19358 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 }
19361 else
19362 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019363 tv.v_type = VAR_NUMBER;
19364 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019366 set_var(virp->vir_line + 1, &tv, FALSE);
19367 if (is_string)
19368 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369 }
19370 }
19371 }
19372
19373 return viminfo_readline(virp);
19374}
19375
19376/*
19377 * Write global vars that start with a capital to the viminfo file
19378 */
19379 void
19380write_viminfo_varlist(fp)
19381 FILE *fp;
19382{
Bram Moolenaar33570922005-01-25 22:26:29 +000019383 hashitem_T *hi;
19384 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019385 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019386 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019387 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019388 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019389 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390
19391 if (find_viminfo_parameter('!') == NULL)
19392 return;
19393
19394 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019395
Bram Moolenaar33570922005-01-25 22:26:29 +000019396 todo = globvarht.ht_used;
19397 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019399 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019401 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019402 this_var = HI2DI(hi);
19403 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019404 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019405 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019406 {
19407 case VAR_STRING: s = "STR"; break;
19408 case VAR_NUMBER: s = "NUM"; break;
19409 default: continue;
19410 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019411 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019412 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019413 if (p != NULL)
19414 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019415 vim_free(tofree);
19416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 }
19418 }
19419}
19420#endif
19421
19422#if defined(FEAT_SESSION) || defined(PROTO)
19423 int
19424store_session_globals(fd)
19425 FILE *fd;
19426{
Bram Moolenaar33570922005-01-25 22:26:29 +000019427 hashitem_T *hi;
19428 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019429 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430 char_u *p, *t;
19431
Bram Moolenaar33570922005-01-25 22:26:29 +000019432 todo = globvarht.ht_used;
19433 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019435 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019437 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019438 this_var = HI2DI(hi);
19439 if ((this_var->di_tv.v_type == VAR_NUMBER
19440 || this_var->di_tv.v_type == VAR_STRING)
19441 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019442 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019443 /* Escape special characters with a backslash. Turn a LF and
19444 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019445 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019446 (char_u *)"\\\"\n\r");
19447 if (p == NULL) /* out of memory */
19448 break;
19449 for (t = p; *t != NUL; ++t)
19450 if (*t == '\n')
19451 *t = 'n';
19452 else if (*t == '\r')
19453 *t = 'r';
19454 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019455 this_var->di_key,
19456 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19457 : ' ',
19458 p,
19459 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19460 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019461 || put_eol(fd) == FAIL)
19462 {
19463 vim_free(p);
19464 return FAIL;
19465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 vim_free(p);
19467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 }
19469 }
19470 return OK;
19471}
19472#endif
19473
Bram Moolenaar661b1822005-07-28 22:36:45 +000019474/*
19475 * Display script name where an item was last set.
19476 * Should only be invoked when 'verbose' is non-zero.
19477 */
19478 void
19479last_set_msg(scriptID)
19480 scid_T scriptID;
19481{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019482 char_u *p;
19483
Bram Moolenaar661b1822005-07-28 22:36:45 +000019484 if (scriptID != 0)
19485 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019486 p = home_replace_save(NULL, get_scriptname(scriptID));
19487 if (p != NULL)
19488 {
19489 verbose_enter();
19490 MSG_PUTS(_("\n\tLast set from "));
19491 MSG_PUTS(p);
19492 vim_free(p);
19493 verbose_leave();
19494 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019495 }
19496}
19497
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498#endif /* FEAT_EVAL */
19499
19500#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19501
19502
19503#ifdef WIN3264
19504/*
19505 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19506 */
19507static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19508static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19509static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19510
19511/*
19512 * Get the short pathname of a file.
19513 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19514 */
19515 static int
19516get_short_pathname(fnamep, bufp, fnamelen)
19517 char_u **fnamep;
19518 char_u **bufp;
19519 int *fnamelen;
19520{
19521 int l,len;
19522 char_u *newbuf;
19523
19524 len = *fnamelen;
19525
19526 l = GetShortPathName(*fnamep, *fnamep, len);
19527 if (l > len - 1)
19528 {
19529 /* If that doesn't work (not enough space), then save the string
19530 * and try again with a new buffer big enough
19531 */
19532 newbuf = vim_strnsave(*fnamep, l);
19533 if (newbuf == NULL)
19534 return 0;
19535
19536 vim_free(*bufp);
19537 *fnamep = *bufp = newbuf;
19538
19539 l = GetShortPathName(*fnamep,*fnamep,l+1);
19540
19541 /* Really should always succeed, as the buffer is big enough */
19542 }
19543
19544 *fnamelen = l;
19545 return 1;
19546}
19547
19548/*
19549 * Create a short path name. Returns the length of the buffer it needs.
19550 * Doesn't copy over the end of the buffer passed in.
19551 */
19552 static int
19553shortpath_for_invalid_fname(fname, bufp, fnamelen)
19554 char_u **fname;
19555 char_u **bufp;
19556 int *fnamelen;
19557{
19558 char_u *s, *p, *pbuf2, *pbuf3;
19559 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019560 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561
19562 /* Make a copy */
19563 len2 = *fnamelen;
19564 pbuf2 = vim_strnsave(*fname, len2);
19565 pbuf3 = NULL;
19566
19567 s = pbuf2 + len2 - 1; /* Find the end */
19568 slen = 1;
19569 plen = len2;
19570
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019571 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 {
19573 --s;
19574 ++slen;
19575 --plen;
19576 }
19577
19578 do
19579 {
19580 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019581 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582 {
19583 --s;
19584 ++slen;
19585 --plen;
19586 }
19587 if (s <= pbuf2)
19588 break;
19589
19590 /* Remeber the character that is about to be blatted */
19591 ch = *s;
19592 *s = 0; /* get_short_pathname requires a null-terminated string */
19593
19594 /* Try it in situ */
19595 p = pbuf2;
19596 if (!get_short_pathname(&p, &pbuf3, &plen))
19597 {
19598 vim_free(pbuf2);
19599 return -1;
19600 }
19601 *s = ch; /* Preserve the string */
19602 } while (plen == 0);
19603
19604 if (plen > 0)
19605 {
19606 /* Remeber the length of the new string. */
19607 *fnamelen = len = plen + slen;
19608 vim_free(*bufp);
19609 if (len > len2)
19610 {
19611 /* If there's not enough space in the currently allocated string,
19612 * then copy it to a buffer big enough.
19613 */
19614 *fname= *bufp = vim_strnsave(p, len);
19615 if (*fname == NULL)
19616 return -1;
19617 }
19618 else
19619 {
19620 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19621 *fname = *bufp = pbuf2;
19622 if (p != pbuf2)
19623 strncpy(*fname, p, plen);
19624 pbuf2 = NULL;
19625 }
19626 /* Concat the next bit */
19627 strncpy(*fname + plen, s, slen);
19628 (*fname)[len] = '\0';
19629 }
19630 vim_free(pbuf3);
19631 vim_free(pbuf2);
19632 return 0;
19633}
19634
19635/*
19636 * Get a pathname for a partial path.
19637 */
19638 static int
19639shortpath_for_partial(fnamep, bufp, fnamelen)
19640 char_u **fnamep;
19641 char_u **bufp;
19642 int *fnamelen;
19643{
19644 int sepcount, len, tflen;
19645 char_u *p;
19646 char_u *pbuf, *tfname;
19647 int hasTilde;
19648
19649 /* Count up the path seperators from the RHS.. so we know which part
19650 * of the path to return.
19651 */
19652 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019653 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654 if (vim_ispathsep(*p))
19655 ++sepcount;
19656
19657 /* Need full path first (use expand_env() to remove a "~/") */
19658 hasTilde = (**fnamep == '~');
19659 if (hasTilde)
19660 pbuf = tfname = expand_env_save(*fnamep);
19661 else
19662 pbuf = tfname = FullName_save(*fnamep, FALSE);
19663
19664 len = tflen = STRLEN(tfname);
19665
19666 if (!get_short_pathname(&tfname, &pbuf, &len))
19667 return -1;
19668
19669 if (len == 0)
19670 {
19671 /* Don't have a valid filename, so shorten the rest of the
19672 * path if we can. This CAN give us invalid 8.3 filenames, but
19673 * there's not a lot of point in guessing what it might be.
19674 */
19675 len = tflen;
19676 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19677 return -1;
19678 }
19679
19680 /* Count the paths backward to find the beginning of the desired string. */
19681 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019682 {
19683#ifdef FEAT_MBYTE
19684 if (has_mbyte)
19685 p -= mb_head_off(tfname, p);
19686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687 if (vim_ispathsep(*p))
19688 {
19689 if (sepcount == 0 || (hasTilde && sepcount == 1))
19690 break;
19691 else
19692 sepcount --;
19693 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 if (hasTilde)
19696 {
19697 --p;
19698 if (p >= tfname)
19699 *p = '~';
19700 else
19701 return -1;
19702 }
19703 else
19704 ++p;
19705
19706 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19707 vim_free(*bufp);
19708 *fnamelen = (int)STRLEN(p);
19709 *bufp = pbuf;
19710 *fnamep = p;
19711
19712 return 0;
19713}
19714#endif /* WIN3264 */
19715
19716/*
19717 * Adjust a filename, according to a string of modifiers.
19718 * *fnamep must be NUL terminated when called. When returning, the length is
19719 * determined by *fnamelen.
19720 * Returns valid flags.
19721 * When there is an error, *fnamep is set to NULL.
19722 */
19723 int
19724modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19725 char_u *src; /* string with modifiers */
19726 int *usedlen; /* characters after src that are used */
19727 char_u **fnamep; /* file name so far */
19728 char_u **bufp; /* buffer for allocated file name or NULL */
19729 int *fnamelen; /* length of fnamep */
19730{
19731 int valid = 0;
19732 char_u *tail;
19733 char_u *s, *p, *pbuf;
19734 char_u dirname[MAXPATHL];
19735 int c;
19736 int has_fullname = 0;
19737#ifdef WIN3264
19738 int has_shortname = 0;
19739#endif
19740
19741repeat:
19742 /* ":p" - full path/file_name */
19743 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19744 {
19745 has_fullname = 1;
19746
19747 valid |= VALID_PATH;
19748 *usedlen += 2;
19749
19750 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19751 if ((*fnamep)[0] == '~'
19752#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19753 && ((*fnamep)[1] == '/'
19754# ifdef BACKSLASH_IN_FILENAME
19755 || (*fnamep)[1] == '\\'
19756# endif
19757 || (*fnamep)[1] == NUL)
19758
19759#endif
19760 )
19761 {
19762 *fnamep = expand_env_save(*fnamep);
19763 vim_free(*bufp); /* free any allocated file name */
19764 *bufp = *fnamep;
19765 if (*fnamep == NULL)
19766 return -1;
19767 }
19768
19769 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019770 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 {
19772 if (vim_ispathsep(*p)
19773 && p[1] == '.'
19774 && (p[2] == NUL
19775 || vim_ispathsep(p[2])
19776 || (p[2] == '.'
19777 && (p[3] == NUL || vim_ispathsep(p[3])))))
19778 break;
19779 }
19780
19781 /* FullName_save() is slow, don't use it when not needed. */
19782 if (*p != NUL || !vim_isAbsName(*fnamep))
19783 {
19784 *fnamep = FullName_save(*fnamep, *p != NUL);
19785 vim_free(*bufp); /* free any allocated file name */
19786 *bufp = *fnamep;
19787 if (*fnamep == NULL)
19788 return -1;
19789 }
19790
19791 /* Append a path separator to a directory. */
19792 if (mch_isdir(*fnamep))
19793 {
19794 /* Make room for one or two extra characters. */
19795 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19796 vim_free(*bufp); /* free any allocated file name */
19797 *bufp = *fnamep;
19798 if (*fnamep == NULL)
19799 return -1;
19800 add_pathsep(*fnamep);
19801 }
19802 }
19803
19804 /* ":." - path relative to the current directory */
19805 /* ":~" - path relative to the home directory */
19806 /* ":8" - shortname path - postponed till after */
19807 while (src[*usedlen] == ':'
19808 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19809 {
19810 *usedlen += 2;
19811 if (c == '8')
19812 {
19813#ifdef WIN3264
19814 has_shortname = 1; /* Postpone this. */
19815#endif
19816 continue;
19817 }
19818 pbuf = NULL;
19819 /* Need full path first (use expand_env() to remove a "~/") */
19820 if (!has_fullname)
19821 {
19822 if (c == '.' && **fnamep == '~')
19823 p = pbuf = expand_env_save(*fnamep);
19824 else
19825 p = pbuf = FullName_save(*fnamep, FALSE);
19826 }
19827 else
19828 p = *fnamep;
19829
19830 has_fullname = 0;
19831
19832 if (p != NULL)
19833 {
19834 if (c == '.')
19835 {
19836 mch_dirname(dirname, MAXPATHL);
19837 s = shorten_fname(p, dirname);
19838 if (s != NULL)
19839 {
19840 *fnamep = s;
19841 if (pbuf != NULL)
19842 {
19843 vim_free(*bufp); /* free any allocated file name */
19844 *bufp = pbuf;
19845 pbuf = NULL;
19846 }
19847 }
19848 }
19849 else
19850 {
19851 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19852 /* Only replace it when it starts with '~' */
19853 if (*dirname == '~')
19854 {
19855 s = vim_strsave(dirname);
19856 if (s != NULL)
19857 {
19858 *fnamep = s;
19859 vim_free(*bufp);
19860 *bufp = s;
19861 }
19862 }
19863 }
19864 vim_free(pbuf);
19865 }
19866 }
19867
19868 tail = gettail(*fnamep);
19869 *fnamelen = (int)STRLEN(*fnamep);
19870
19871 /* ":h" - head, remove "/file_name", can be repeated */
19872 /* Don't remove the first "/" or "c:\" */
19873 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19874 {
19875 valid |= VALID_HEAD;
19876 *usedlen += 2;
19877 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019878 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 --tail;
19880 *fnamelen = (int)(tail - *fnamep);
19881#ifdef VMS
19882 if (*fnamelen > 0)
19883 *fnamelen += 1; /* the path separator is part of the path */
19884#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019885 while (tail > s && !after_pathsep(s, tail))
19886 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887 }
19888
19889 /* ":8" - shortname */
19890 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19891 {
19892 *usedlen += 2;
19893#ifdef WIN3264
19894 has_shortname = 1;
19895#endif
19896 }
19897
19898#ifdef WIN3264
19899 /* Check shortname after we have done 'heads' and before we do 'tails'
19900 */
19901 if (has_shortname)
19902 {
19903 pbuf = NULL;
19904 /* Copy the string if it is shortened by :h */
19905 if (*fnamelen < (int)STRLEN(*fnamep))
19906 {
19907 p = vim_strnsave(*fnamep, *fnamelen);
19908 if (p == 0)
19909 return -1;
19910 vim_free(*bufp);
19911 *bufp = *fnamep = p;
19912 }
19913
19914 /* Split into two implementations - makes it easier. First is where
19915 * there isn't a full name already, second is where there is.
19916 */
19917 if (!has_fullname && !vim_isAbsName(*fnamep))
19918 {
19919 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19920 return -1;
19921 }
19922 else
19923 {
19924 int l;
19925
19926 /* Simple case, already have the full-name
19927 * Nearly always shorter, so try first time. */
19928 l = *fnamelen;
19929 if (!get_short_pathname(fnamep, bufp, &l))
19930 return -1;
19931
19932 if (l == 0)
19933 {
19934 /* Couldn't find the filename.. search the paths.
19935 */
19936 l = *fnamelen;
19937 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19938 return -1;
19939 }
19940 *fnamelen = l;
19941 }
19942 }
19943#endif /* WIN3264 */
19944
19945 /* ":t" - tail, just the basename */
19946 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19947 {
19948 *usedlen += 2;
19949 *fnamelen -= (int)(tail - *fnamep);
19950 *fnamep = tail;
19951 }
19952
19953 /* ":e" - extension, can be repeated */
19954 /* ":r" - root, without extension, can be repeated */
19955 while (src[*usedlen] == ':'
19956 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19957 {
19958 /* find a '.' in the tail:
19959 * - for second :e: before the current fname
19960 * - otherwise: The last '.'
19961 */
19962 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19963 s = *fnamep - 2;
19964 else
19965 s = *fnamep + *fnamelen - 1;
19966 for ( ; s > tail; --s)
19967 if (s[0] == '.')
19968 break;
19969 if (src[*usedlen + 1] == 'e') /* :e */
19970 {
19971 if (s > tail)
19972 {
19973 *fnamelen += (int)(*fnamep - (s + 1));
19974 *fnamep = s + 1;
19975#ifdef VMS
19976 /* cut version from the extension */
19977 s = *fnamep + *fnamelen - 1;
19978 for ( ; s > *fnamep; --s)
19979 if (s[0] == ';')
19980 break;
19981 if (s > *fnamep)
19982 *fnamelen = s - *fnamep;
19983#endif
19984 }
19985 else if (*fnamep <= tail)
19986 *fnamelen = 0;
19987 }
19988 else /* :r */
19989 {
19990 if (s > tail) /* remove one extension */
19991 *fnamelen = (int)(s - *fnamep);
19992 }
19993 *usedlen += 2;
19994 }
19995
19996 /* ":s?pat?foo?" - substitute */
19997 /* ":gs?pat?foo?" - global substitute */
19998 if (src[*usedlen] == ':'
19999 && (src[*usedlen + 1] == 's'
20000 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20001 {
20002 char_u *str;
20003 char_u *pat;
20004 char_u *sub;
20005 int sep;
20006 char_u *flags;
20007 int didit = FALSE;
20008
20009 flags = (char_u *)"";
20010 s = src + *usedlen + 2;
20011 if (src[*usedlen + 1] == 'g')
20012 {
20013 flags = (char_u *)"g";
20014 ++s;
20015 }
20016
20017 sep = *s++;
20018 if (sep)
20019 {
20020 /* find end of pattern */
20021 p = vim_strchr(s, sep);
20022 if (p != NULL)
20023 {
20024 pat = vim_strnsave(s, (int)(p - s));
20025 if (pat != NULL)
20026 {
20027 s = p + 1;
20028 /* find end of substitution */
20029 p = vim_strchr(s, sep);
20030 if (p != NULL)
20031 {
20032 sub = vim_strnsave(s, (int)(p - s));
20033 str = vim_strnsave(*fnamep, *fnamelen);
20034 if (sub != NULL && str != NULL)
20035 {
20036 *usedlen = (int)(p + 1 - src);
20037 s = do_string_sub(str, pat, sub, flags);
20038 if (s != NULL)
20039 {
20040 *fnamep = s;
20041 *fnamelen = (int)STRLEN(s);
20042 vim_free(*bufp);
20043 *bufp = s;
20044 didit = TRUE;
20045 }
20046 }
20047 vim_free(sub);
20048 vim_free(str);
20049 }
20050 vim_free(pat);
20051 }
20052 }
20053 /* after using ":s", repeat all the modifiers */
20054 if (didit)
20055 goto repeat;
20056 }
20057 }
20058
20059 return valid;
20060}
20061
20062/*
20063 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20064 * "flags" can be "g" to do a global substitute.
20065 * Returns an allocated string, NULL for error.
20066 */
20067 char_u *
20068do_string_sub(str, pat, sub, flags)
20069 char_u *str;
20070 char_u *pat;
20071 char_u *sub;
20072 char_u *flags;
20073{
20074 int sublen;
20075 regmatch_T regmatch;
20076 int i;
20077 int do_all;
20078 char_u *tail;
20079 garray_T ga;
20080 char_u *ret;
20081 char_u *save_cpo;
20082
20083 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20084 save_cpo = p_cpo;
20085 p_cpo = (char_u *)"";
20086
20087 ga_init2(&ga, 1, 200);
20088
20089 do_all = (flags[0] == 'g');
20090
20091 regmatch.rm_ic = p_ic;
20092 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20093 if (regmatch.regprog != NULL)
20094 {
20095 tail = str;
20096 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20097 {
20098 /*
20099 * Get some space for a temporary buffer to do the substitution
20100 * into. It will contain:
20101 * - The text up to where the match is.
20102 * - The substituted text.
20103 * - The text after the match.
20104 */
20105 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20106 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20107 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20108 {
20109 ga_clear(&ga);
20110 break;
20111 }
20112
20113 /* copy the text up to where the match is */
20114 i = (int)(regmatch.startp[0] - tail);
20115 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20116 /* add the substituted text */
20117 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20118 + ga.ga_len + i, TRUE, TRUE, FALSE);
20119 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 /* avoid getting stuck on a match with an empty string */
20121 if (tail == regmatch.endp[0])
20122 {
20123 if (*tail == NUL)
20124 break;
20125 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20126 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 }
20128 else
20129 {
20130 tail = regmatch.endp[0];
20131 if (*tail == NUL)
20132 break;
20133 }
20134 if (!do_all)
20135 break;
20136 }
20137
20138 if (ga.ga_data != NULL)
20139 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20140
20141 vim_free(regmatch.regprog);
20142 }
20143
20144 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20145 ga_clear(&ga);
20146 p_cpo = save_cpo;
20147
20148 return ret;
20149}
20150
20151#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */