blob: 9f79560b47ff8e17228e19cd4e9bf7eccfe65dce [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 Moolenaareddf53b2006-02-27 00:11:10 +0000403static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000404static listitem_T *listitem_alloc __ARGS((void));
405static void listitem_free __ARGS((listitem_T *item));
406static void listitem_remove __ARGS((list_T *l, listitem_T *item));
407static long list_len __ARGS((list_T *l));
408static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
409static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
410static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000411static listitem_T *list_find __ARGS((list_T *l, long n));
412static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static void list_append __ARGS((list_T *l, listitem_T *item));
414static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000415static int list_append_string __ARGS((list_T *l, char_u *str, int len));
416static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
418static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
419static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000420static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000422static char_u *list2string __ARGS((typval_T *tv, int copyID));
423static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000424static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
425static void set_ref_in_list __ARGS((list_T *l, int copyID));
426static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void dict_unref __ARGS((dict_T *d));
428static void dict_free __ARGS((dict_T *d));
429static dictitem_T *dictitem_alloc __ARGS((char_u *key));
430static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
431static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
432static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000433static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static int dict_add __ARGS((dict_T *d, dictitem_T *item));
435static long dict_len __ARGS((dict_T *d));
436static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000437static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000439static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
440static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static char_u *string_quote __ARGS((char_u *str, int function));
442static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
443static int find_internal_func __ARGS((char_u *name));
444static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
445static 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));
446static 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 +0000447static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448
449static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000468#if defined(FEAT_INS_EXPAND)
469static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
471#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
477static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000503static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000505static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000511static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000519static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000542static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000548static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000565static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000569#ifdef vim_mkdir
570static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
571#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000576static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000577static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000579static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000591static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000593static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000600static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000601static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000606static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000607static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
610#ifdef HAVE_STRFTIME
611static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
612#endif
613static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000625static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000626static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000627static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000628static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000629static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000631static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000646static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000647
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
649static int get_env_len __ARGS((char_u **arg));
650static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000651static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000652static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
653#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
654#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
655 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000656static 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 +0000657static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000658static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000659static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
660static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static typval_T *alloc_tv __ARGS((void));
662static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void init_tv __ARGS((typval_T *varp));
664static long get_tv_number __ARGS((typval_T *varp));
665static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000666static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static char_u *get_tv_string __ARGS((typval_T *varp));
668static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000669static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000671static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
673static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
674static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
675static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
676static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
677static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
678static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000679static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000681static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
683static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
684static int eval_fname_script __ARGS((char_u *p));
685static int eval_fname_sid __ARGS((char_u *p));
686static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static ufunc_T *find_func __ARGS((char_u *name));
688static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000689static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000690#ifdef FEAT_PROFILE
691static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000692static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
693static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
694static int
695# ifdef __BORLANDC__
696 _RTLENTRYF
697# endif
698 prof_total_cmp __ARGS((const void *s1, const void *s2));
699static int
700# ifdef __BORLANDC__
701 _RTLENTRYF
702# endif
703 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000704#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000705static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000706static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000707static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void func_free __ARGS((ufunc_T *fp));
709static void func_unref __ARGS((char_u *name));
710static void func_ref __ARGS((char_u *name));
711static 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));
712static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000713static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000714static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
715static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar33570922005-01-25 22:26:29 +0000716
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000717/* Character used as separated in autoload function/variable names. */
718#define AUTOLOAD_CHAR '#'
719
Bram Moolenaar33570922005-01-25 22:26:29 +0000720/*
721 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000722 */
723 void
724eval_init()
725{
Bram Moolenaar33570922005-01-25 22:26:29 +0000726 int i;
727 struct vimvar *p;
728
729 init_var_dict(&globvardict, &globvars_var);
730 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000731 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000732 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000733
734 for (i = 0; i < VV_LEN; ++i)
735 {
736 p = &vimvars[i];
737 STRCPY(p->vv_di.di_key, p->vv_name);
738 if (p->vv_flags & VV_RO)
739 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
740 else if (p->vv_flags & VV_RO_SBX)
741 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
742 else
743 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000744
745 /* add to v: scope dict, unless the value is not always available */
746 if (p->vv_type != VAR_UNKNOWN)
747 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000748 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000749 /* add to compat scope dict */
750 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000751 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000752}
753
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000754#if defined(EXITFREE) || defined(PROTO)
755 void
756eval_clear()
757{
758 int i;
759 struct vimvar *p;
760
761 for (i = 0; i < VV_LEN; ++i)
762 {
763 p = &vimvars[i];
764 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000765 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000766 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000767 p->vv_di.di_tv.vval.v_string = NULL;
768 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000769 }
770 hash_clear(&vimvarht);
771 hash_clear(&compat_hashtab);
772
773 /* script-local variables */
774 for (i = 1; i <= ga_scripts.ga_len; ++i)
775 vars_clear(&SCRIPT_VARS(i));
776 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000777 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000778
779 /* global variables */
780 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000781
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000782 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000783 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000784 hash_clear(&func_hashtab);
785
786 /* unreferenced lists and dicts */
787 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000788}
789#endif
790
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 * Return the name of the executed function.
793 */
794 char_u *
795func_name(cookie)
796 void *cookie;
797{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000798 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799}
800
801/*
802 * Return the address holding the next breakpoint line for a funccall cookie.
803 */
804 linenr_T *
805func_breakpoint(cookie)
806 void *cookie;
807{
Bram Moolenaar33570922005-01-25 22:26:29 +0000808 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809}
810
811/*
812 * Return the address holding the debug tick for a funccall cookie.
813 */
814 int *
815func_dbg_tick(cookie)
816 void *cookie;
817{
Bram Moolenaar33570922005-01-25 22:26:29 +0000818 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819}
820
821/*
822 * Return the nesting level for a funccall cookie.
823 */
824 int
825func_level(cookie)
826 void *cookie;
827{
Bram Moolenaar33570922005-01-25 22:26:29 +0000828 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829}
830
831/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000832funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
834/*
835 * Return TRUE when a function was ended by a ":return" command.
836 */
837 int
838current_func_returned()
839{
840 return current_funccal->returned;
841}
842
843
844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 * Set an internal variable to a string value. Creates the variable if it does
846 * not already exist.
847 */
848 void
849set_internal_string_var(name, value)
850 char_u *name;
851 char_u *value;
852{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000853 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000854 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856 val = vim_strsave(value);
857 if (val != NULL)
858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000859 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000860 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000862 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
865 }
866}
867
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000868static lval_T *redir_lval = NULL;
869static char_u *redir_endp = NULL;
870static char_u *redir_varname = NULL;
871
872/*
873 * Start recording command output to a variable
874 * Returns OK if successfully completed the setup. FAIL otherwise.
875 */
876 int
877var_redir_start(name, append)
878 char_u *name;
879 int append; /* append to an existing variable */
880{
881 int save_emsg;
882 int err;
883 typval_T tv;
884
885 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000886 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000887 {
888 EMSG(_(e_invarg));
889 return FAIL;
890 }
891
892 redir_varname = vim_strsave(name);
893 if (redir_varname == NULL)
894 return FAIL;
895
896 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
897 if (redir_lval == NULL)
898 {
899 var_redir_stop();
900 return FAIL;
901 }
902
903 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000904 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
905 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000906 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
907 {
908 if (redir_endp != NULL && *redir_endp != NUL)
909 /* Trailing characters are present after the variable name */
910 EMSG(_(e_trailing));
911 else
912 EMSG(_(e_invarg));
913 var_redir_stop();
914 return FAIL;
915 }
916
917 /* check if we can write to the variable: set it to or append an empty
918 * string */
919 save_emsg = did_emsg;
920 did_emsg = FALSE;
921 tv.v_type = VAR_STRING;
922 tv.vval.v_string = (char_u *)"";
923 if (append)
924 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
925 else
926 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
927 err = did_emsg;
928 did_emsg += save_emsg;
929 if (err)
930 {
931 var_redir_stop();
932 return FAIL;
933 }
934 if (redir_lval->ll_newkey != NULL)
935 {
936 /* Dictionary item was created, don't do it again. */
937 vim_free(redir_lval->ll_newkey);
938 redir_lval->ll_newkey = NULL;
939 }
940
941 return OK;
942}
943
944/*
945 * Append "value[len]" to the variable set by var_redir_start().
946 */
947 void
948var_redir_str(value, len)
949 char_u *value;
950 int len;
951{
952 char_u *val;
953 typval_T tv;
954 int save_emsg;
955 int err;
956
957 if (redir_lval == NULL)
958 return;
959
960 if (len == -1)
961 /* Append the entire string */
962 val = vim_strsave(value);
963 else
964 /* Append only the specified number of characters */
965 val = vim_strnsave(value, len);
966 if (val == NULL)
967 return;
968
969 tv.v_type = VAR_STRING;
970 tv.vval.v_string = val;
971
972 save_emsg = did_emsg;
973 did_emsg = FALSE;
974 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
975 err = did_emsg;
976 did_emsg += save_emsg;
977 if (err)
978 var_redir_stop();
979
980 vim_free(tv.vval.v_string);
981}
982
983/*
984 * Stop redirecting command output to a variable.
985 */
986 void
987var_redir_stop()
988{
989 if (redir_lval != NULL)
990 {
991 clear_lval(redir_lval);
992 vim_free(redir_lval);
993 redir_lval = NULL;
994 }
995 vim_free(redir_varname);
996 redir_varname = NULL;
997}
998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999# if defined(FEAT_MBYTE) || defined(PROTO)
1000 int
1001eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1002 char_u *enc_from;
1003 char_u *enc_to;
1004 char_u *fname_from;
1005 char_u *fname_to;
1006{
1007 int err = FALSE;
1008
1009 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1010 set_vim_var_string(VV_CC_TO, enc_to, -1);
1011 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1012 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1013 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1014 err = TRUE;
1015 set_vim_var_string(VV_CC_FROM, NULL, -1);
1016 set_vim_var_string(VV_CC_TO, NULL, -1);
1017 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1018 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1019
1020 if (err)
1021 return FAIL;
1022 return OK;
1023}
1024# endif
1025
1026# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1027 int
1028eval_printexpr(fname, args)
1029 char_u *fname;
1030 char_u *args;
1031{
1032 int err = FALSE;
1033
1034 set_vim_var_string(VV_FNAME_IN, fname, -1);
1035 set_vim_var_string(VV_CMDARG, args, -1);
1036 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1037 err = TRUE;
1038 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1039 set_vim_var_string(VV_CMDARG, NULL, -1);
1040
1041 if (err)
1042 {
1043 mch_remove(fname);
1044 return FAIL;
1045 }
1046 return OK;
1047}
1048# endif
1049
1050# if defined(FEAT_DIFF) || defined(PROTO)
1051 void
1052eval_diff(origfile, newfile, outfile)
1053 char_u *origfile;
1054 char_u *newfile;
1055 char_u *outfile;
1056{
1057 int err = FALSE;
1058
1059 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1060 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1061 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1062 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1063 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1064 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1065 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1066}
1067
1068 void
1069eval_patch(origfile, difffile, outfile)
1070 char_u *origfile;
1071 char_u *difffile;
1072 char_u *outfile;
1073{
1074 int err;
1075
1076 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1077 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1078 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1079 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1080 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1081 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1082 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1083}
1084# endif
1085
1086/*
1087 * Top level evaluation function, returning a boolean.
1088 * Sets "error" to TRUE if there was an error.
1089 * Return TRUE or FALSE.
1090 */
1091 int
1092eval_to_bool(arg, error, nextcmd, skip)
1093 char_u *arg;
1094 int *error;
1095 char_u **nextcmd;
1096 int skip; /* only parse, don't execute */
1097{
Bram Moolenaar33570922005-01-25 22:26:29 +00001098 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 int retval = FALSE;
1100
1101 if (skip)
1102 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001103 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 else
1106 {
1107 *error = FALSE;
1108 if (!skip)
1109 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001110 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001111 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 }
1113 }
1114 if (skip)
1115 --emsg_skip;
1116
1117 return retval;
1118}
1119
1120/*
1121 * Top level evaluation function, returning a string. If "skip" is TRUE,
1122 * only parsing to "nextcmd" is done, without reporting errors. Return
1123 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1124 */
1125 char_u *
1126eval_to_string_skip(arg, nextcmd, skip)
1127 char_u *arg;
1128 char_u **nextcmd;
1129 int skip; /* only parse, don't execute */
1130{
Bram Moolenaar33570922005-01-25 22:26:29 +00001131 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 char_u *retval;
1133
1134 if (skip)
1135 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001136 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 retval = NULL;
1138 else
1139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001140 retval = vim_strsave(get_tv_string(&tv));
1141 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
1143 if (skip)
1144 --emsg_skip;
1145
1146 return retval;
1147}
1148
1149/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001150 * Skip over an expression at "*pp".
1151 * Return FAIL for an error, OK otherwise.
1152 */
1153 int
1154skip_expr(pp)
1155 char_u **pp;
1156{
Bram Moolenaar33570922005-01-25 22:26:29 +00001157 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001158
1159 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001160 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001161}
1162
1163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 * Top level evaluation function, returning a string.
1165 * Return pointer to allocated memory, or NULL for failure.
1166 */
1167 char_u *
1168eval_to_string(arg, nextcmd)
1169 char_u *arg;
1170 char_u **nextcmd;
1171{
Bram Moolenaar33570922005-01-25 22:26:29 +00001172 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 char_u *retval;
1174
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001175 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 retval = NULL;
1177 else
1178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001179 retval = vim_strsave(get_tv_string(&tv));
1180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 }
1182
1183 return retval;
1184}
1185
1186/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001187 * Call eval_to_string() without using current local variables and using
1188 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 */
1190 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001191eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 char_u *arg;
1193 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001194 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195{
1196 char_u *retval;
1197 void *save_funccalp;
1198
1199 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001200 if (use_sandbox)
1201 ++sandbox;
1202 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 retval = eval_to_string(arg, nextcmd);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001204 if (use_sandbox)
1205 --sandbox;
1206 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 restore_funccal(save_funccalp);
1208 return retval;
1209}
1210
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211/*
1212 * Top level evaluation function, returning a number.
1213 * Evaluates "expr" silently.
1214 * Returns -1 for an error.
1215 */
1216 int
1217eval_to_number(expr)
1218 char_u *expr;
1219{
Bram Moolenaar33570922005-01-25 22:26:29 +00001220 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001222 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
1224 ++emsg_off;
1225
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001226 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 retval = -1;
1228 else
1229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001230 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001231 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 }
1233 --emsg_off;
1234
1235 return retval;
1236}
1237
Bram Moolenaara40058a2005-07-11 22:42:07 +00001238/*
1239 * Prepare v: variable "idx" to be used.
1240 * Save the current typeval in "save_tv".
1241 * When not used yet add the variable to the v: hashtable.
1242 */
1243 static void
1244prepare_vimvar(idx, save_tv)
1245 int idx;
1246 typval_T *save_tv;
1247{
1248 *save_tv = vimvars[idx].vv_tv;
1249 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1250 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1251}
1252
1253/*
1254 * Restore v: variable "idx" to typeval "save_tv".
1255 * When no longer defined, remove the variable from the v: hashtable.
1256 */
1257 static void
1258restore_vimvar(idx, save_tv)
1259 int idx;
1260 typval_T *save_tv;
1261{
1262 hashitem_T *hi;
1263
1264 clear_tv(&vimvars[idx].vv_tv);
1265 vimvars[idx].vv_tv = *save_tv;
1266 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1267 {
1268 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1269 if (HASHITEM_EMPTY(hi))
1270 EMSG2(_(e_intern2), "restore_vimvar()");
1271 else
1272 hash_remove(&vimvarht, hi);
1273 }
1274}
1275
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001276#if defined(FEAT_SYN_HL) || defined(PROTO)
1277/*
1278 * Evaluate an expression to a list with suggestions.
1279 * For the "expr:" part of 'spellsuggest'.
1280 */
1281 list_T *
1282eval_spell_expr(badword, expr)
1283 char_u *badword;
1284 char_u *expr;
1285{
1286 typval_T save_val;
1287 typval_T rettv;
1288 list_T *list = NULL;
1289 char_u *p = skipwhite(expr);
1290
1291 /* Set "v:val" to the bad word. */
1292 prepare_vimvar(VV_VAL, &save_val);
1293 vimvars[VV_VAL].vv_type = VAR_STRING;
1294 vimvars[VV_VAL].vv_str = badword;
1295 if (p_verbose == 0)
1296 ++emsg_off;
1297
1298 if (eval1(&p, &rettv, TRUE) == OK)
1299 {
1300 if (rettv.v_type != VAR_LIST)
1301 clear_tv(&rettv);
1302 else
1303 list = rettv.vval.v_list;
1304 }
1305
1306 if (p_verbose == 0)
1307 --emsg_off;
1308 vimvars[VV_VAL].vv_str = NULL;
1309 restore_vimvar(VV_VAL, &save_val);
1310
1311 return list;
1312}
1313
1314/*
1315 * "list" is supposed to contain two items: a word and a number. Return the
1316 * word in "pp" and the number as the return value.
1317 * Return -1 if anything isn't right.
1318 * Used to get the good word and score from the eval_spell_expr() result.
1319 */
1320 int
1321get_spellword(list, pp)
1322 list_T *list;
1323 char_u **pp;
1324{
1325 listitem_T *li;
1326
1327 li = list->lv_first;
1328 if (li == NULL)
1329 return -1;
1330 *pp = get_tv_string(&li->li_tv);
1331
1332 li = li->li_next;
1333 if (li == NULL)
1334 return -1;
1335 return get_tv_number(&li->li_tv);
1336}
1337#endif
1338
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001339/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001340 * Top level evaluation function.
1341 * Returns an allocated typval_T with the result.
1342 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001343 */
1344 typval_T *
1345eval_expr(arg, nextcmd)
1346 char_u *arg;
1347 char_u **nextcmd;
1348{
1349 typval_T *tv;
1350
1351 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001352 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001353 {
1354 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001355 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001356 }
1357
1358 return tv;
1359}
1360
1361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1363/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001364 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001366 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001368 static int
1369call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 char_u *func;
1371 int argc;
1372 char_u **argv;
1373 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
Bram Moolenaar33570922005-01-25 22:26:29 +00001376 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 long n;
1378 int len;
1379 int i;
1380 int doesrange;
1381 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001382 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
Bram Moolenaar33570922005-01-25 22:26:29 +00001384 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001386 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
1388 for (i = 0; i < argc; i++)
1389 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001390 /* Pass a NULL or empty argument as an empty string */
1391 if (argv[i] == NULL || *argv[i] == NUL)
1392 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001393 argvars[i].v_type = VAR_STRING;
1394 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001395 continue;
1396 }
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 /* Recognize a number argument, the others must be strings. */
1399 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1400 if (len != 0 && len == (int)STRLEN(argv[i]))
1401 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001402 argvars[i].v_type = VAR_NUMBER;
1403 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 }
1405 else
1406 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001407 argvars[i].v_type = VAR_STRING;
1408 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 }
1410 }
1411
1412 if (safe)
1413 {
1414 save_funccalp = save_funccal();
1415 ++sandbox;
1416 }
1417
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001418 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1419 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001421 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 if (safe)
1423 {
1424 --sandbox;
1425 restore_funccal(save_funccalp);
1426 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001427 vim_free(argvars);
1428
1429 if (ret == FAIL)
1430 clear_tv(rettv);
1431
1432 return ret;
1433}
1434
1435/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001436 * Call vimL function "func" and return the result as a string.
1437 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001438 * Uses argv[argc] for the function arguments.
1439 */
1440 void *
1441call_func_retstr(func, argc, argv, safe)
1442 char_u *func;
1443 int argc;
1444 char_u **argv;
1445 int safe; /* use the sandbox */
1446{
1447 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001448 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001449
1450 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1451 return NULL;
1452
1453 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001454 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 return retval;
1456}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001457
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001458#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001459/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001460 * Call vimL function "func" and return the result as a number.
1461 * Returns -1 when calling the function fails.
1462 * Uses argv[argc] for the function arguments.
1463 */
1464 long
1465call_func_retnr(func, argc, argv, safe)
1466 char_u *func;
1467 int argc;
1468 char_u **argv;
1469 int safe; /* use the sandbox */
1470{
1471 typval_T rettv;
1472 long retval;
1473
1474 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1475 return -1;
1476
1477 retval = get_tv_number_chk(&rettv, NULL);
1478 clear_tv(&rettv);
1479 return retval;
1480}
1481#endif
1482
1483/*
1484 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001485 * Uses argv[argc] for the function arguments.
1486 */
1487 void *
1488call_func_retlist(func, argc, argv, safe)
1489 char_u *func;
1490 int argc;
1491 char_u **argv;
1492 int safe; /* use the sandbox */
1493{
1494 typval_T rettv;
1495
1496 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1497 return NULL;
1498
1499 if (rettv.v_type != VAR_LIST)
1500 {
1501 clear_tv(&rettv);
1502 return NULL;
1503 }
1504
1505 return rettv.vval.v_list;
1506}
1507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508#endif
1509
1510/*
1511 * Save the current function call pointer, and set it to NULL.
1512 * Used when executing autocommands and for ":source".
1513 */
1514 void *
1515save_funccal()
1516{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001517 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 current_funccal = NULL;
1520 return (void *)fc;
1521}
1522
1523 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001524restore_funccal(vfc)
1525 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001527 funccall_T *fc = (funccall_T *)vfc;
1528
1529 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530}
1531
Bram Moolenaar05159a02005-02-26 23:04:13 +00001532#if defined(FEAT_PROFILE) || defined(PROTO)
1533/*
1534 * Prepare profiling for entering a child or something else that is not
1535 * counted for the script/function itself.
1536 * Should always be called in pair with prof_child_exit().
1537 */
1538 void
1539prof_child_enter(tm)
1540 proftime_T *tm; /* place to store waittime */
1541{
1542 funccall_T *fc = current_funccal;
1543
1544 if (fc != NULL && fc->func->uf_profiling)
1545 profile_start(&fc->prof_child);
1546 script_prof_save(tm);
1547}
1548
1549/*
1550 * Take care of time spent in a child.
1551 * Should always be called after prof_child_enter().
1552 */
1553 void
1554prof_child_exit(tm)
1555 proftime_T *tm; /* where waittime was stored */
1556{
1557 funccall_T *fc = current_funccal;
1558
1559 if (fc != NULL && fc->func->uf_profiling)
1560 {
1561 profile_end(&fc->prof_child);
1562 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1563 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1564 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1565 }
1566 script_prof_restore(tm);
1567}
1568#endif
1569
1570
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571#ifdef FEAT_FOLDING
1572/*
1573 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1574 * it in "*cp". Doesn't give error messages.
1575 */
1576 int
1577eval_foldexpr(arg, cp)
1578 char_u *arg;
1579 int *cp;
1580{
Bram Moolenaar33570922005-01-25 22:26:29 +00001581 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 int retval;
1583 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001584 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1585 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
1587 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001588 if (use_sandbox)
1589 ++sandbox;
1590 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001592 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 retval = 0;
1594 else
1595 {
1596 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001597 if (tv.v_type == VAR_NUMBER)
1598 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001599 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 retval = 0;
1601 else
1602 {
1603 /* If the result is a string, check if there is a non-digit before
1604 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001605 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (!VIM_ISDIGIT(*s) && *s != '-')
1607 *cp = *s++;
1608 retval = atol((char *)s);
1609 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001610 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001613 if (use_sandbox)
1614 --sandbox;
1615 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617 return retval;
1618}
1619#endif
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001622 * ":let" list all variable values
1623 * ":let var1 var2" list variable values
1624 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001625 * ":let var += expr" assignment command.
1626 * ":let var -= expr" assignment command.
1627 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 */
1630 void
1631ex_let(eap)
1632 exarg_T *eap;
1633{
1634 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001635 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001636 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001638 int var_count = 0;
1639 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001640 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001642 expr = skip_var_list(arg, &var_count, &semicolon);
1643 if (expr == NULL)
1644 return;
1645 expr = vim_strchr(expr, '=');
1646 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001648 /*
1649 * ":let" without "=": list variables
1650 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 if (*arg == '[')
1652 EMSG(_(e_invarg));
1653 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654 /* ":let var1 var2" */
1655 arg = list_arg_vars(eap, arg);
1656 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001659 list_glob_vars();
1660 list_buf_vars();
1661 list_win_vars();
1662 list_vim_vars();
1663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 eap->nextcmd = check_nextcmd(arg);
1665 }
1666 else
1667 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001668 op[0] = '=';
1669 op[1] = NUL;
1670 if (expr > arg)
1671 {
1672 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1673 op[0] = expr[-1]; /* +=, -= or .= */
1674 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (eap->skip)
1678 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (eap->skip)
1681 {
1682 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001683 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 --emsg_skip;
1685 }
1686 else if (i != FAIL)
1687 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001689 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001690 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 }
1692 }
1693}
1694
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695/*
1696 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1697 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698 * When "nextchars" is not NULL it points to a string with characters that
1699 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1700 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701 * Returns OK or FAIL;
1702 */
1703 static int
1704ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1705 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001706 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 int copy; /* copy values from "tv", don't move */
1708 int semicolon; /* from skip_var_list() */
1709 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001710 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711{
1712 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001713 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001715 listitem_T *item;
1716 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717
1718 if (*arg != '[')
1719 {
1720 /*
1721 * ":let var = expr" or ":for var in list"
1722 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001723 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 return FAIL;
1725 return OK;
1726 }
1727
1728 /*
1729 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1730 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001731 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 {
1733 EMSG(_(e_listreq));
1734 return FAIL;
1735 }
1736
1737 i = list_len(l);
1738 if (semicolon == 0 && var_count < i)
1739 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001740 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741 return FAIL;
1742 }
1743 if (var_count - semicolon > i)
1744 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001745 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746 return FAIL;
1747 }
1748
1749 item = l->lv_first;
1750 while (*arg != ']')
1751 {
1752 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001753 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001754 item = item->li_next;
1755 if (arg == NULL)
1756 return FAIL;
1757
1758 arg = skipwhite(arg);
1759 if (*arg == ';')
1760 {
1761 /* Put the rest of the list (may be empty) in the var after ';'.
1762 * Create a new list for this. */
1763 l = list_alloc();
1764 if (l == NULL)
1765 return FAIL;
1766 while (item != NULL)
1767 {
1768 list_append_tv(l, &item->li_tv);
1769 item = item->li_next;
1770 }
1771
1772 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001773 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 ltv.vval.v_list = l;
1775 l->lv_refcount = 1;
1776
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001777 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1778 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779 clear_tv(&ltv);
1780 if (arg == NULL)
1781 return FAIL;
1782 break;
1783 }
1784 else if (*arg != ',' && *arg != ']')
1785 {
1786 EMSG2(_(e_intern2), "ex_let_vars()");
1787 return FAIL;
1788 }
1789 }
1790
1791 return OK;
1792}
1793
1794/*
1795 * Skip over assignable variable "var" or list of variables "[var, var]".
1796 * Used for ":let varvar = expr" and ":for varvar in expr".
1797 * For "[var, var]" increment "*var_count" for each variable.
1798 * for "[var, var; var]" set "semicolon".
1799 * Return NULL for an error.
1800 */
1801 static char_u *
1802skip_var_list(arg, var_count, semicolon)
1803 char_u *arg;
1804 int *var_count;
1805 int *semicolon;
1806{
1807 char_u *p, *s;
1808
1809 if (*arg == '[')
1810 {
1811 /* "[var, var]": find the matching ']'. */
1812 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001813 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001814 {
1815 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1816 s = skip_var_one(p);
1817 if (s == p)
1818 {
1819 EMSG2(_(e_invarg2), p);
1820 return NULL;
1821 }
1822 ++*var_count;
1823
1824 p = skipwhite(s);
1825 if (*p == ']')
1826 break;
1827 else if (*p == ';')
1828 {
1829 if (*semicolon == 1)
1830 {
1831 EMSG(_("Double ; in list of variables"));
1832 return NULL;
1833 }
1834 *semicolon = 1;
1835 }
1836 else if (*p != ',')
1837 {
1838 EMSG2(_(e_invarg2), p);
1839 return NULL;
1840 }
1841 }
1842 return p + 1;
1843 }
1844 else
1845 return skip_var_one(arg);
1846}
1847
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001848/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001849 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1850 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001851 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 static char_u *
1853skip_var_one(arg)
1854 char_u *arg;
1855{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001856 if (*arg == '@' && arg[1] != NUL)
1857 return arg + 2;
1858 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1859 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860}
1861
Bram Moolenaara7043832005-01-21 11:56:39 +00001862/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001863 * List variables for hashtab "ht" with prefix "prefix".
1864 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001865 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001867list_hashtable_vars(ht, prefix, empty)
1868 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001869 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001870 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001871{
Bram Moolenaar33570922005-01-25 22:26:29 +00001872 hashitem_T *hi;
1873 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001874 int todo;
1875
1876 todo = ht->ht_used;
1877 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1878 {
1879 if (!HASHITEM_EMPTY(hi))
1880 {
1881 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 di = HI2DI(hi);
1883 if (empty || di->di_tv.v_type != VAR_STRING
1884 || di->di_tv.vval.v_string != NULL)
1885 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001886 }
1887 }
1888}
1889
1890/*
1891 * List global variables.
1892 */
1893 static void
1894list_glob_vars()
1895{
Bram Moolenaar33570922005-01-25 22:26:29 +00001896 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001897}
1898
1899/*
1900 * List buffer variables.
1901 */
1902 static void
1903list_buf_vars()
1904{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001905 char_u numbuf[NUMBUFLEN];
1906
Bram Moolenaar33570922005-01-25 22:26:29 +00001907 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001908
1909 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1910 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001911}
1912
1913/*
1914 * List window variables.
1915 */
1916 static void
1917list_win_vars()
1918{
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001920}
1921
1922/*
1923 * List Vim variables.
1924 */
1925 static void
1926list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927{
Bram Moolenaar33570922005-01-25 22:26:29 +00001928 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929}
1930
1931/*
1932 * List variables in "arg".
1933 */
1934 static char_u *
1935list_arg_vars(eap, arg)
1936 exarg_T *eap;
1937 char_u *arg;
1938{
1939 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001940 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001942 char_u *name_start;
1943 char_u *arg_subsc;
1944 char_u *tofree;
1945 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946
1947 while (!ends_excmd(*arg) && !got_int)
1948 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001949 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001951 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001952 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1953 {
1954 emsg_severe = TRUE;
1955 EMSG(_(e_trailing));
1956 break;
1957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001959 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001961 /* get_name_len() takes care of expanding curly braces */
1962 name_start = name = arg;
1963 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1964 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001965 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001966 /* This is mainly to keep test 49 working: when expanding
1967 * curly braces fails overrule the exception error message. */
1968 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001970 emsg_severe = TRUE;
1971 EMSG2(_(e_invarg2), arg);
1972 break;
1973 }
1974 error = TRUE;
1975 }
1976 else
1977 {
1978 if (tofree != NULL)
1979 name = tofree;
1980 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001982 else
1983 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001984 /* handle d.key, l[idx], f(expr) */
1985 arg_subsc = arg;
1986 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001987 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001989 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001990 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001991 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001992 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001993 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001994 case 'g': list_glob_vars(); break;
1995 case 'b': list_buf_vars(); break;
1996 case 'w': list_win_vars(); break;
1997 case 'v': list_vim_vars(); break;
1998 default:
1999 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002000 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002001 }
2002 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002003 {
2004 char_u numbuf[NUMBUFLEN];
2005 char_u *tf;
2006 int c;
2007 char_u *s;
2008
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002009 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002010 c = *arg;
2011 *arg = NUL;
2012 list_one_var_a((char_u *)"",
2013 arg == arg_subsc ? name : name_start,
2014 tv.v_type, s == NULL ? (char_u *)"" : s);
2015 *arg = c;
2016 vim_free(tf);
2017 }
2018 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002019 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020 }
2021 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002022
2023 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002025
2026 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002027 }
2028
2029 return arg;
2030}
2031
2032/*
2033 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2034 * Returns a pointer to the char just after the var name.
2035 * Returns NULL if there is an error.
2036 */
2037 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002038ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002039 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002040 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002041 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002042 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044{
2045 int c1;
2046 char_u *name;
2047 char_u *p;
2048 char_u *arg_end = NULL;
2049 int len;
2050 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002051 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052
2053 /*
2054 * ":let $VAR = expr": Set environment variable.
2055 */
2056 if (*arg == '$')
2057 {
2058 /* Find the end of the name. */
2059 ++arg;
2060 name = arg;
2061 len = get_env_len(&arg);
2062 if (len == 0)
2063 EMSG2(_(e_invarg2), name - 1);
2064 else
2065 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002066 if (op != NULL && (*op == '+' || *op == '-'))
2067 EMSG2(_(e_letwrong), op);
2068 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002069 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 EMSG(_(e_letunexp));
2071 else
2072 {
2073 c1 = name[len];
2074 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002075 p = get_tv_string_chk(tv);
2076 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002077 {
2078 int mustfree = FALSE;
2079 char_u *s = vim_getenv(name, &mustfree);
2080
2081 if (s != NULL)
2082 {
2083 p = tofree = concat_str(s, p);
2084 if (mustfree)
2085 vim_free(s);
2086 }
2087 }
2088 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002089 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002090 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002091 if (STRICMP(name, "HOME") == 0)
2092 init_homedir();
2093 else if (didset_vim && STRICMP(name, "VIM") == 0)
2094 didset_vim = FALSE;
2095 else if (didset_vimruntime
2096 && STRICMP(name, "VIMRUNTIME") == 0)
2097 didset_vimruntime = FALSE;
2098 arg_end = arg;
2099 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002102 }
2103 }
2104 }
2105
2106 /*
2107 * ":let &option = expr": Set option value.
2108 * ":let &l:option = expr": Set local option value.
2109 * ":let &g:option = expr": Set global option value.
2110 */
2111 else if (*arg == '&')
2112 {
2113 /* Find the end of the name. */
2114 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002115 if (p == NULL || (endchars != NULL
2116 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002117 EMSG(_(e_letunexp));
2118 else
2119 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002120 long n;
2121 int opt_type;
2122 long numval;
2123 char_u *stringval = NULL;
2124 char_u *s;
2125
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126 c1 = *p;
2127 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002128
2129 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002130 s = get_tv_string_chk(tv); /* != NULL if number or string */
2131 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002132 {
2133 opt_type = get_option_value(arg, &numval,
2134 &stringval, opt_flags);
2135 if ((opt_type == 1 && *op == '.')
2136 || (opt_type == 0 && *op != '.'))
2137 EMSG2(_(e_letwrong), op);
2138 else
2139 {
2140 if (opt_type == 1) /* number */
2141 {
2142 if (*op == '+')
2143 n = numval + n;
2144 else
2145 n = numval - n;
2146 }
2147 else if (opt_type == 0 && stringval != NULL) /* string */
2148 {
2149 s = concat_str(stringval, s);
2150 vim_free(stringval);
2151 stringval = s;
2152 }
2153 }
2154 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002155 if (s != NULL)
2156 {
2157 set_option_value(arg, n, s, opt_flags);
2158 arg_end = p;
2159 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002161 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162 }
2163 }
2164
2165 /*
2166 * ":let @r = expr": Set register contents.
2167 */
2168 else if (*arg == '@')
2169 {
2170 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 if (op != NULL && (*op == '+' || *op == '-'))
2172 EMSG2(_(e_letwrong), op);
2173 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002174 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 EMSG(_(e_letunexp));
2176 else
2177 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002178 char_u *tofree = NULL;
2179 char_u *s;
2180
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002181 p = get_tv_string_chk(tv);
2182 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002183 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002184 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002185 if (s != NULL)
2186 {
2187 p = tofree = concat_str(s, p);
2188 vim_free(s);
2189 }
2190 }
2191 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002192 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002193 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002194 arg_end = arg + 1;
2195 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002196 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 }
2198 }
2199
2200 /*
2201 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002204 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002206 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002208 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2212 EMSG(_(e_letunexp));
2213 else
2214 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002215 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 arg_end = p;
2217 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 }
2221
2222 else
2223 EMSG2(_(e_invarg2), arg);
2224
2225 return arg_end;
2226}
2227
2228/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002229 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2230 */
2231 static int
2232check_changedtick(arg)
2233 char_u *arg;
2234{
2235 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2236 {
2237 EMSG2(_(e_readonlyvar), arg);
2238 return TRUE;
2239 }
2240 return FALSE;
2241}
2242
2243/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244 * Get an lval: variable, Dict item or List item that can be assigned a value
2245 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2246 * "name.key", "name.key[expr]" etc.
2247 * Indexing only works if "name" is an existing List or Dictionary.
2248 * "name" points to the start of the name.
2249 * If "rettv" is not NULL it points to the value to be assigned.
2250 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2251 * wrong; must end in space or cmd separator.
2252 *
2253 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002254 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 */
2257 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002258get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002260 typval_T *rettv;
2261 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 int unlet;
2263 int skip;
2264 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002265 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 char_u *expr_start, *expr_end;
2269 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002270 dictitem_T *v;
2271 typval_T var1;
2272 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002274 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002275 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002276 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002277 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002280 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281
2282 if (skip)
2283 {
2284 /* When skipping just find the end of the name. */
2285 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002286 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002287 }
2288
2289 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002290 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 if (expr_start != NULL)
2292 {
2293 /* Don't expand the name when we already know there is an error. */
2294 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2295 && *p != '[' && *p != '.')
2296 {
2297 EMSG(_(e_trailing));
2298 return NULL;
2299 }
2300
2301 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2302 if (lp->ll_exp_name == NULL)
2303 {
2304 /* Report an invalid expression in braces, unless the
2305 * expression evaluation has been cancelled due to an
2306 * aborting error, an interrupt, or an exception. */
2307 if (!aborting() && !quiet)
2308 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002309 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 EMSG2(_(e_invarg2), name);
2311 return NULL;
2312 }
2313 }
2314 lp->ll_name = lp->ll_exp_name;
2315 }
2316 else
2317 lp->ll_name = name;
2318
2319 /* Without [idx] or .key we are done. */
2320 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2321 return p;
2322
2323 cc = *p;
2324 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002325 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326 if (v == NULL && !quiet)
2327 EMSG2(_(e_undefvar), lp->ll_name);
2328 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 if (v == NULL)
2330 return NULL;
2331
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 /*
2333 * Loop until no more [idx] or .key is following.
2334 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002335 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2339 && !(lp->ll_tv->v_type == VAR_DICT
2340 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 if (!quiet)
2343 EMSG(_("E689: Can only index a List or Dictionary"));
2344 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002346 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002348 if (!quiet)
2349 EMSG(_("E708: [:] must come last"));
2350 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002352
Bram Moolenaar8c711452005-01-14 21:53:12 +00002353 len = -1;
2354 if (*p == '.')
2355 {
2356 key = p + 1;
2357 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2358 ;
2359 if (len == 0)
2360 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002361 if (!quiet)
2362 EMSG(_(e_emptykey));
2363 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002364 }
2365 p = key + len;
2366 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002367 else
2368 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002369 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002370 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002371 if (*p == ':')
2372 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002373 else
2374 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002375 empty1 = FALSE;
2376 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 if (get_tv_string_chk(&var1) == NULL)
2379 {
2380 /* not a number or string */
2381 clear_tv(&var1);
2382 return NULL;
2383 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002384 }
2385
2386 /* Optionally get the second index [ :expr]. */
2387 if (*p == ':')
2388 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002390 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002393 if (!empty1)
2394 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002396 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 if (rettv != NULL && (rettv->v_type != VAR_LIST
2398 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002399 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002400 if (!quiet)
2401 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002402 if (!empty1)
2403 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002404 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002405 }
2406 p = skipwhite(p + 1);
2407 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002409 else
2410 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002412 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2413 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414 if (!empty1)
2415 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002416 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002418 if (get_tv_string_chk(&var2) == NULL)
2419 {
2420 /* not a number or string */
2421 if (!empty1)
2422 clear_tv(&var1);
2423 clear_tv(&var2);
2424 return NULL;
2425 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002426 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002428 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002429 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002431
Bram Moolenaar8c711452005-01-14 21:53:12 +00002432 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002433 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 if (!quiet)
2435 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002436 if (!empty1)
2437 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002439 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002441 }
2442
2443 /* Skip to past ']'. */
2444 ++p;
2445 }
2446
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 {
2449 if (len == -1)
2450 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 if (*key == NUL)
2454 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 if (!quiet)
2456 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002459 }
2460 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 lp->ll_list = NULL;
2462 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002463 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002465 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002466 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002468 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002471 if (len == -1)
2472 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 }
2475 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002479 if (len == -1)
2480 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002482 p = NULL;
2483 break;
2484 }
2485 if (len == -1)
2486 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 }
2489 else
2490 {
2491 /*
2492 * Get the number and item for the only or first index of the List.
2493 */
2494 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 else
2497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 clear_tv(&var1);
2500 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 lp->ll_list = lp->ll_tv->vval.v_list;
2503 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2504 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 if (!quiet)
2507 EMSGN(_(e_listidx), lp->ll_n1);
2508 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 }
2512
2513 /*
2514 * May need to find the item or absolute index for the second
2515 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 * When no index given: "lp->ll_empty2" is TRUE.
2517 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002518 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 if (ni == NULL)
2527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 if (!quiet)
2529 EMSGN(_(e_listidx), lp->ll_n2);
2530 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 }
2534
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2536 if (lp->ll_n1 < 0)
2537 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2538 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002539 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 if (!quiet)
2541 EMSGN(_(e_listidx), lp->ll_n2);
2542 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002543 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002544 }
2545
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002547 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002548 }
2549
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 return p;
2551}
2552
2553/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 */
2556 static void
2557clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002558 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559{
2560 vim_free(lp->ll_exp_name);
2561 vim_free(lp->ll_newkey);
2562}
2563
2564/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002565 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002567 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 */
2569 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002570set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002573 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002575 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576{
2577 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002578 listitem_T *ri;
2579 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580
2581 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 cc = *endp;
2586 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002587 if (op != NULL && *op != '=')
2588 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002590
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002591 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002592 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2593 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002594 {
2595 if (tv_op(&tv, rettv, op) == OK)
2596 set_var(lp->ll_name, &tv, FALSE);
2597 clear_tv(&tv);
2598 }
2599 }
2600 else
2601 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002605 else if (tv_check_lock(lp->ll_newkey == NULL
2606 ? lp->ll_tv->v_lock
2607 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2608 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 else if (lp->ll_range)
2610 {
2611 /*
2612 * Assign the List values to the list items.
2613 */
2614 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002616 if (op != NULL && *op != '=')
2617 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2618 else
2619 {
2620 clear_tv(&lp->ll_li->li_tv);
2621 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2622 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 ri = ri->li_next;
2624 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2625 break;
2626 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002629 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 ri = NULL;
2632 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002633 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 lp->ll_li = lp->ll_li->li_next;
2636 ++lp->ll_n1;
2637 }
2638 if (ri != NULL)
2639 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002640 else if (lp->ll_empty2
2641 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 : lp->ll_n1 != lp->ll_n2)
2643 EMSG(_("E711: List value has not enough items"));
2644 }
2645 else
2646 {
2647 /*
2648 * Assign to a List or Dictionary item.
2649 */
2650 if (lp->ll_newkey != NULL)
2651 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002652 if (op != NULL && *op != '=')
2653 {
2654 EMSG2(_(e_letwrong), op);
2655 return;
2656 }
2657
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002659 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (di == NULL)
2661 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002662 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2663 {
2664 vim_free(di);
2665 return;
2666 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002668 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002669 else if (op != NULL && *op != '=')
2670 {
2671 tv_op(lp->ll_tv, rettv, op);
2672 return;
2673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002674 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 /*
2678 * Assign the value to the variable or list item.
2679 */
2680 if (copy)
2681 copy_tv(rettv, lp->ll_tv);
2682 else
2683 {
2684 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002685 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002687 }
2688 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002689}
2690
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002691/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002692 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2693 * Returns OK or FAIL.
2694 */
2695 static int
2696tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002697 typval_T *tv1;
2698 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002699 char_u *op;
2700{
2701 long n;
2702 char_u numbuf[NUMBUFLEN];
2703 char_u *s;
2704
2705 /* Can't do anything with a Funcref or a Dict on the right. */
2706 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2707 {
2708 switch (tv1->v_type)
2709 {
2710 case VAR_DICT:
2711 case VAR_FUNC:
2712 break;
2713
2714 case VAR_LIST:
2715 if (*op != '+' || tv2->v_type != VAR_LIST)
2716 break;
2717 /* List += List */
2718 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2719 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2720 return OK;
2721
2722 case VAR_NUMBER:
2723 case VAR_STRING:
2724 if (tv2->v_type == VAR_LIST)
2725 break;
2726 if (*op == '+' || *op == '-')
2727 {
2728 /* nr += nr or nr -= nr*/
2729 n = get_tv_number(tv1);
2730 if (*op == '+')
2731 n += get_tv_number(tv2);
2732 else
2733 n -= get_tv_number(tv2);
2734 clear_tv(tv1);
2735 tv1->v_type = VAR_NUMBER;
2736 tv1->vval.v_number = n;
2737 }
2738 else
2739 {
2740 /* str .= str */
2741 s = get_tv_string(tv1);
2742 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2743 clear_tv(tv1);
2744 tv1->v_type = VAR_STRING;
2745 tv1->vval.v_string = s;
2746 }
2747 return OK;
2748 }
2749 }
2750
2751 EMSG2(_(e_letwrong), op);
2752 return FAIL;
2753}
2754
2755/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002756 * Add a watcher to a list.
2757 */
2758 static void
2759list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002760 list_T *l;
2761 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002762{
2763 lw->lw_next = l->lv_watch;
2764 l->lv_watch = lw;
2765}
2766
2767/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002768 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002769 * No warning when it isn't found...
2770 */
2771 static void
2772list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002773 list_T *l;
2774 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002775{
Bram Moolenaar33570922005-01-25 22:26:29 +00002776 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002777
2778 lwp = &l->lv_watch;
2779 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2780 {
2781 if (lw == lwrem)
2782 {
2783 *lwp = lw->lw_next;
2784 break;
2785 }
2786 lwp = &lw->lw_next;
2787 }
2788}
2789
2790/*
2791 * Just before removing an item from a list: advance watchers to the next
2792 * item.
2793 */
2794 static void
2795list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002796 list_T *l;
2797 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002798{
Bram Moolenaar33570922005-01-25 22:26:29 +00002799 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002800
2801 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2802 if (lw->lw_item == item)
2803 lw->lw_item = item->li_next;
2804}
2805
2806/*
2807 * Evaluate the expression used in a ":for var in expr" command.
2808 * "arg" points to "var".
2809 * Set "*errp" to TRUE for an error, FALSE otherwise;
2810 * Return a pointer that holds the info. Null when there is an error.
2811 */
2812 void *
2813eval_for_line(arg, errp, nextcmdp, skip)
2814 char_u *arg;
2815 int *errp;
2816 char_u **nextcmdp;
2817 int skip;
2818{
Bram Moolenaar33570922005-01-25 22:26:29 +00002819 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002820 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002821 typval_T tv;
2822 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002823
2824 *errp = TRUE; /* default: there is an error */
2825
Bram Moolenaar33570922005-01-25 22:26:29 +00002826 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002827 if (fi == NULL)
2828 return NULL;
2829
2830 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2831 if (expr == NULL)
2832 return fi;
2833
2834 expr = skipwhite(expr);
2835 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2836 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002837 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002838 return fi;
2839 }
2840
2841 if (skip)
2842 ++emsg_skip;
2843 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2844 {
2845 *errp = FALSE;
2846 if (!skip)
2847 {
2848 l = tv.vval.v_list;
2849 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002850 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002851 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002852 clear_tv(&tv);
2853 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002854 else
2855 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002856 /* No need to increment the refcount, it's already set for the
2857 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002858 fi->fi_list = l;
2859 list_add_watch(l, &fi->fi_lw);
2860 fi->fi_lw.lw_item = l->lv_first;
2861 }
2862 }
2863 }
2864 if (skip)
2865 --emsg_skip;
2866
2867 return fi;
2868}
2869
2870/*
2871 * Use the first item in a ":for" list. Advance to the next.
2872 * Assign the values to the variable (list). "arg" points to the first one.
2873 * Return TRUE when a valid item was found, FALSE when at end of list or
2874 * something wrong.
2875 */
2876 int
2877next_for_item(fi_void, arg)
2878 void *fi_void;
2879 char_u *arg;
2880{
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002884
2885 item = fi->fi_lw.lw_item;
2886 if (item == NULL)
2887 result = FALSE;
2888 else
2889 {
2890 fi->fi_lw.lw_item = item->li_next;
2891 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2892 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2893 }
2894 return result;
2895}
2896
2897/*
2898 * Free the structure used to store info used by ":for".
2899 */
2900 void
2901free_for_info(fi_void)
2902 void *fi_void;
2903{
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002905
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002906 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002907 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002908 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002909 list_unref(fi->fi_list);
2910 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911 vim_free(fi);
2912}
2913
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2915
2916 void
2917set_context_for_expression(xp, arg, cmdidx)
2918 expand_T *xp;
2919 char_u *arg;
2920 cmdidx_T cmdidx;
2921{
2922 int got_eq = FALSE;
2923 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002924 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002926 if (cmdidx == CMD_let)
2927 {
2928 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002929 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002930 {
2931 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002932 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002933 {
2934 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002935 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002936 if (vim_iswhite(*p))
2937 break;
2938 }
2939 return;
2940 }
2941 }
2942 else
2943 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2944 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 while ((xp->xp_pattern = vim_strpbrk(arg,
2946 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2947 {
2948 c = *xp->xp_pattern;
2949 if (c == '&')
2950 {
2951 c = xp->xp_pattern[1];
2952 if (c == '&')
2953 {
2954 ++xp->xp_pattern;
2955 xp->xp_context = cmdidx != CMD_let || got_eq
2956 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2957 }
2958 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002959 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002961 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2962 xp->xp_pattern += 2;
2963
2964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 }
2966 else if (c == '$')
2967 {
2968 /* environment variable */
2969 xp->xp_context = EXPAND_ENV_VARS;
2970 }
2971 else if (c == '=')
2972 {
2973 got_eq = TRUE;
2974 xp->xp_context = EXPAND_EXPRESSION;
2975 }
2976 else if (c == '<'
2977 && xp->xp_context == EXPAND_FUNCTIONS
2978 && vim_strchr(xp->xp_pattern, '(') == NULL)
2979 {
2980 /* Function name can start with "<SNR>" */
2981 break;
2982 }
2983 else if (cmdidx != CMD_let || got_eq)
2984 {
2985 if (c == '"') /* string */
2986 {
2987 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2988 if (c == '\\' && xp->xp_pattern[1] != NUL)
2989 ++xp->xp_pattern;
2990 xp->xp_context = EXPAND_NOTHING;
2991 }
2992 else if (c == '\'') /* literal string */
2993 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002994 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2996 /* skip */ ;
2997 xp->xp_context = EXPAND_NOTHING;
2998 }
2999 else if (c == '|')
3000 {
3001 if (xp->xp_pattern[1] == '|')
3002 {
3003 ++xp->xp_pattern;
3004 xp->xp_context = EXPAND_EXPRESSION;
3005 }
3006 else
3007 xp->xp_context = EXPAND_COMMANDS;
3008 }
3009 else
3010 xp->xp_context = EXPAND_EXPRESSION;
3011 }
3012 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003013 /* Doesn't look like something valid, expand as an expression
3014 * anyway. */
3015 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 arg = xp->xp_pattern;
3017 if (*arg != NUL)
3018 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3019 /* skip */ ;
3020 }
3021 xp->xp_pattern = arg;
3022}
3023
3024#endif /* FEAT_CMDL_COMPL */
3025
3026/*
3027 * ":1,25call func(arg1, arg2)" function call.
3028 */
3029 void
3030ex_call(eap)
3031 exarg_T *eap;
3032{
3033 char_u *arg = eap->arg;
3034 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003038 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 linenr_T lnum;
3040 int doesrange;
3041 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003042 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003044 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3045 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003046 if (tofree == NULL)
3047 return;
3048
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003049 /* Increase refcount on dictionary, it could get deleted when evaluating
3050 * the arguments. */
3051 if (fudi.fd_dict != NULL)
3052 ++fudi.fd_dict->dv_refcount;
3053
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3055 len = STRLEN(tofree);
3056 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057
Bram Moolenaar532c7802005-01-27 14:44:31 +00003058 /* Skip white space to allow ":call func ()". Not good, but required for
3059 * backward compatibility. */
3060 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003061 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
3063 if (*startarg != '(')
3064 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003065 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 goto end;
3067 }
3068
3069 /*
3070 * When skipping, evaluate the function once, to find the end of the
3071 * arguments.
3072 * When the function takes a range, this is discovered after the first
3073 * call, and the loop is broken.
3074 */
3075 if (eap->skip)
3076 {
3077 ++emsg_skip;
3078 lnum = eap->line2; /* do it once, also with an invalid range */
3079 }
3080 else
3081 lnum = eap->line1;
3082 for ( ; lnum <= eap->line2; ++lnum)
3083 {
3084 if (!eap->skip && eap->addr_count > 0)
3085 {
3086 curwin->w_cursor.lnum = lnum;
3087 curwin->w_cursor.col = 0;
3088 }
3089 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003091 eap->line1, eap->line2, &doesrange,
3092 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 {
3094 failed = TRUE;
3095 break;
3096 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003097 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 if (doesrange || eap->skip)
3099 break;
3100 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003101 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003102 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003103 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 if (aborting())
3105 break;
3106 }
3107 if (eap->skip)
3108 --emsg_skip;
3109
3110 if (!failed)
3111 {
3112 /* Check for trailing illegal characters and a following command. */
3113 if (!ends_excmd(*arg))
3114 {
3115 emsg_severe = TRUE;
3116 EMSG(_(e_trailing));
3117 }
3118 else
3119 eap->nextcmd = check_nextcmd(arg);
3120 }
3121
3122end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003123 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003124 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125}
3126
3127/*
3128 * ":unlet[!] var1 ... " command.
3129 */
3130 void
3131ex_unlet(eap)
3132 exarg_T *eap;
3133{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003134 ex_unletlock(eap, eap->arg, 0);
3135}
3136
3137/*
3138 * ":lockvar" and ":unlockvar" commands
3139 */
3140 void
3141ex_lockvar(eap)
3142 exarg_T *eap;
3143{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003145 int deep = 2;
3146
3147 if (eap->forceit)
3148 deep = -1;
3149 else if (vim_isdigit(*arg))
3150 {
3151 deep = getdigits(&arg);
3152 arg = skipwhite(arg);
3153 }
3154
3155 ex_unletlock(eap, arg, deep);
3156}
3157
3158/*
3159 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3160 */
3161 static void
3162ex_unletlock(eap, argstart, deep)
3163 exarg_T *eap;
3164 char_u *argstart;
3165 int deep;
3166{
3167 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003170 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
3172 do
3173 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003174 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003175 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3176 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003177 if (lv.ll_name == NULL)
3178 error = TRUE; /* error but continue parsing */
3179 if (name_end == NULL || (!vim_iswhite(*name_end)
3180 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003182 if (name_end != NULL)
3183 {
3184 emsg_severe = TRUE;
3185 EMSG(_(e_trailing));
3186 }
3187 if (!(eap->skip || error))
3188 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 break;
3190 }
3191
3192 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003193 {
3194 if (eap->cmdidx == CMD_unlet)
3195 {
3196 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3197 error = TRUE;
3198 }
3199 else
3200 {
3201 if (do_lock_var(&lv, name_end, deep,
3202 eap->cmdidx == CMD_lockvar) == FAIL)
3203 error = TRUE;
3204 }
3205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003207 if (!eap->skip)
3208 clear_lval(&lv);
3209
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 arg = skipwhite(name_end);
3211 } while (!ends_excmd(*arg));
3212
3213 eap->nextcmd = check_nextcmd(arg);
3214}
3215
Bram Moolenaar8c711452005-01-14 21:53:12 +00003216 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003217do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003219 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003220 int forceit;
3221{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003222 int ret = OK;
3223 int cc;
3224
3225 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003226 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003227 cc = *name_end;
3228 *name_end = NUL;
3229
3230 /* Normal name or expanded name. */
3231 if (check_changedtick(lp->ll_name))
3232 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003233 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003234 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003235 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003236 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003237 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3238 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003239 else if (lp->ll_range)
3240 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003241 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003242
3243 /* Delete a range of List items. */
3244 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3245 {
3246 li = lp->ll_li->li_next;
3247 listitem_remove(lp->ll_list, lp->ll_li);
3248 lp->ll_li = li;
3249 ++lp->ll_n1;
3250 }
3251 }
3252 else
3253 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003254 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003255 /* unlet a List item. */
3256 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003257 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003258 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003259 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003260 }
3261
3262 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003263}
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265/*
3266 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003267 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 */
3269 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003270do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003272 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 hashtab_T *ht;
3275 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003276 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277
Bram Moolenaar33570922005-01-25 22:26:29 +00003278 ht = find_var_ht(name, &varname);
3279 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003281 hi = hash_find(ht, varname);
3282 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003283 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003284 if (var_check_ro(HI2DI(hi)->di_flags, name))
3285 return FAIL;
3286 delete_var(ht, hi);
3287 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003290 if (forceit)
3291 return OK;
3292 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 return FAIL;
3294}
3295
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003296/*
3297 * Lock or unlock variable indicated by "lp".
3298 * "deep" is the levels to go (-1 for unlimited);
3299 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3300 */
3301 static int
3302do_lock_var(lp, name_end, deep, lock)
3303 lval_T *lp;
3304 char_u *name_end;
3305 int deep;
3306 int lock;
3307{
3308 int ret = OK;
3309 int cc;
3310 dictitem_T *di;
3311
3312 if (deep == 0) /* nothing to do */
3313 return OK;
3314
3315 if (lp->ll_tv == NULL)
3316 {
3317 cc = *name_end;
3318 *name_end = NUL;
3319
3320 /* Normal name or expanded name. */
3321 if (check_changedtick(lp->ll_name))
3322 ret = FAIL;
3323 else
3324 {
3325 di = find_var(lp->ll_name, NULL);
3326 if (di == NULL)
3327 ret = FAIL;
3328 else
3329 {
3330 if (lock)
3331 di->di_flags |= DI_FLAGS_LOCK;
3332 else
3333 di->di_flags &= ~DI_FLAGS_LOCK;
3334 item_lock(&di->di_tv, deep, lock);
3335 }
3336 }
3337 *name_end = cc;
3338 }
3339 else if (lp->ll_range)
3340 {
3341 listitem_T *li = lp->ll_li;
3342
3343 /* (un)lock a range of List items. */
3344 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3345 {
3346 item_lock(&li->li_tv, deep, lock);
3347 li = li->li_next;
3348 ++lp->ll_n1;
3349 }
3350 }
3351 else if (lp->ll_list != NULL)
3352 /* (un)lock a List item. */
3353 item_lock(&lp->ll_li->li_tv, deep, lock);
3354 else
3355 /* un(lock) a Dictionary item. */
3356 item_lock(&lp->ll_di->di_tv, deep, lock);
3357
3358 return ret;
3359}
3360
3361/*
3362 * Lock or unlock an item. "deep" is nr of levels to go.
3363 */
3364 static void
3365item_lock(tv, deep, lock)
3366 typval_T *tv;
3367 int deep;
3368 int lock;
3369{
3370 static int recurse = 0;
3371 list_T *l;
3372 listitem_T *li;
3373 dict_T *d;
3374 hashitem_T *hi;
3375 int todo;
3376
3377 if (recurse >= DICT_MAXNEST)
3378 {
3379 EMSG(_("E743: variable nested too deep for (un)lock"));
3380 return;
3381 }
3382 if (deep == 0)
3383 return;
3384 ++recurse;
3385
3386 /* lock/unlock the item itself */
3387 if (lock)
3388 tv->v_lock |= VAR_LOCKED;
3389 else
3390 tv->v_lock &= ~VAR_LOCKED;
3391
3392 switch (tv->v_type)
3393 {
3394 case VAR_LIST:
3395 if ((l = tv->vval.v_list) != NULL)
3396 {
3397 if (lock)
3398 l->lv_lock |= VAR_LOCKED;
3399 else
3400 l->lv_lock &= ~VAR_LOCKED;
3401 if (deep < 0 || deep > 1)
3402 /* recursive: lock/unlock the items the List contains */
3403 for (li = l->lv_first; li != NULL; li = li->li_next)
3404 item_lock(&li->li_tv, deep - 1, lock);
3405 }
3406 break;
3407 case VAR_DICT:
3408 if ((d = tv->vval.v_dict) != NULL)
3409 {
3410 if (lock)
3411 d->dv_lock |= VAR_LOCKED;
3412 else
3413 d->dv_lock &= ~VAR_LOCKED;
3414 if (deep < 0 || deep > 1)
3415 {
3416 /* recursive: lock/unlock the items the List contains */
3417 todo = d->dv_hashtab.ht_used;
3418 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3419 {
3420 if (!HASHITEM_EMPTY(hi))
3421 {
3422 --todo;
3423 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3424 }
3425 }
3426 }
3427 }
3428 }
3429 --recurse;
3430}
3431
Bram Moolenaara40058a2005-07-11 22:42:07 +00003432/*
3433 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3434 * it refers to a List or Dictionary that is locked.
3435 */
3436 static int
3437tv_islocked(tv)
3438 typval_T *tv;
3439{
3440 return (tv->v_lock & VAR_LOCKED)
3441 || (tv->v_type == VAR_LIST
3442 && tv->vval.v_list != NULL
3443 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3444 || (tv->v_type == VAR_DICT
3445 && tv->vval.v_dict != NULL
3446 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3447}
3448
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3450/*
3451 * Delete all "menutrans_" variables.
3452 */
3453 void
3454del_menutrans_vars()
3455{
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003457 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 hash_lock(&globvarht);
3460 todo = globvarht.ht_used;
3461 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003462 {
3463 if (!HASHITEM_EMPTY(hi))
3464 {
3465 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003466 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3467 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003468 }
3469 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003470 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471}
3472#endif
3473
3474#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3475
3476/*
3477 * Local string buffer for the next two functions to store a variable name
3478 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3479 * get_user_var_name().
3480 */
3481
3482static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3483
3484static char_u *varnamebuf = NULL;
3485static int varnamebuflen = 0;
3486
3487/*
3488 * Function to concatenate a prefix and a variable name.
3489 */
3490 static char_u *
3491cat_prefix_varname(prefix, name)
3492 int prefix;
3493 char_u *name;
3494{
3495 int len;
3496
3497 len = (int)STRLEN(name) + 3;
3498 if (len > varnamebuflen)
3499 {
3500 vim_free(varnamebuf);
3501 len += 10; /* some additional space */
3502 varnamebuf = alloc(len);
3503 if (varnamebuf == NULL)
3504 {
3505 varnamebuflen = 0;
3506 return NULL;
3507 }
3508 varnamebuflen = len;
3509 }
3510 *varnamebuf = prefix;
3511 varnamebuf[1] = ':';
3512 STRCPY(varnamebuf + 2, name);
3513 return varnamebuf;
3514}
3515
3516/*
3517 * Function given to ExpandGeneric() to obtain the list of user defined
3518 * (global/buffer/window/built-in) variable names.
3519 */
3520/*ARGSUSED*/
3521 char_u *
3522get_user_var_name(xp, idx)
3523 expand_T *xp;
3524 int idx;
3525{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003526 static long_u gdone;
3527 static long_u bdone;
3528 static long_u wdone;
3529 static int vidx;
3530 static hashitem_T *hi;
3531 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532
3533 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003534 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003535
3536 /* Global variables */
3537 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003539 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003540 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003541 else
3542 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003543 while (HASHITEM_EMPTY(hi))
3544 ++hi;
3545 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3546 return cat_prefix_varname('g', hi->hi_key);
3547 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003549
3550 /* b: variables */
3551 ht = &curbuf->b_vars.dv_hashtab;
3552 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003554 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003555 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003556 else
3557 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003558 while (HASHITEM_EMPTY(hi))
3559 ++hi;
3560 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003562 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003564 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 return (char_u *)"b:changedtick";
3566 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003567
3568 /* w: variables */
3569 ht = &curwin->w_vars.dv_hashtab;
3570 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003572 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003573 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003574 else
3575 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003576 while (HASHITEM_EMPTY(hi))
3577 ++hi;
3578 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003580
3581 /* v: variables */
3582 if (vidx < VV_LEN)
3583 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584
3585 vim_free(varnamebuf);
3586 varnamebuf = NULL;
3587 varnamebuflen = 0;
3588 return NULL;
3589}
3590
3591#endif /* FEAT_CMDL_COMPL */
3592
3593/*
3594 * types for expressions.
3595 */
3596typedef enum
3597{
3598 TYPE_UNKNOWN = 0
3599 , TYPE_EQUAL /* == */
3600 , TYPE_NEQUAL /* != */
3601 , TYPE_GREATER /* > */
3602 , TYPE_GEQUAL /* >= */
3603 , TYPE_SMALLER /* < */
3604 , TYPE_SEQUAL /* <= */
3605 , TYPE_MATCH /* =~ */
3606 , TYPE_NOMATCH /* !~ */
3607} exptype_T;
3608
3609/*
3610 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003611 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3613 */
3614
3615/*
3616 * Handle zero level expression.
3617 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003618 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003619 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 * Return OK or FAIL.
3621 */
3622 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003623eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 char_u **nextcmd;
3627 int evaluate;
3628{
3629 int ret;
3630 char_u *p;
3631
3632 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003633 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 if (ret == FAIL || !ends_excmd(*p))
3635 {
3636 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003637 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 /*
3639 * Report the invalid expression unless the expression evaluation has
3640 * been cancelled due to an aborting error, an interrupt, or an
3641 * exception.
3642 */
3643 if (!aborting())
3644 EMSG2(_(e_invexpr2), arg);
3645 ret = FAIL;
3646 }
3647 if (nextcmd != NULL)
3648 *nextcmd = check_nextcmd(p);
3649
3650 return ret;
3651}
3652
3653/*
3654 * Handle top level expression:
3655 * expr1 ? expr0 : expr0
3656 *
3657 * "arg" must point to the first non-white of the expression.
3658 * "arg" is advanced to the next non-white after the recognized expression.
3659 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003660 * Note: "rettv.v_lock" is not set.
3661 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 * Return OK or FAIL.
3663 */
3664 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003665eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 int evaluate;
3669{
3670 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003671 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
3673 /*
3674 * Get the first variable.
3675 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003676 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 return FAIL;
3678
3679 if ((*arg)[0] == '?')
3680 {
3681 result = FALSE;
3682 if (evaluate)
3683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003684 int error = FALSE;
3685
3686 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003688 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003689 if (error)
3690 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692
3693 /*
3694 * Get the second variable.
3695 */
3696 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003697 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 return FAIL;
3699
3700 /*
3701 * Check for the ":".
3702 */
3703 if ((*arg)[0] != ':')
3704 {
3705 EMSG(_("E109: Missing ':' after '?'"));
3706 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003707 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 return FAIL;
3709 }
3710
3711 /*
3712 * Get the third variable.
3713 */
3714 *arg = skipwhite(*arg + 1);
3715 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3716 {
3717 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003718 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 return FAIL;
3720 }
3721 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003722 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
3724
3725 return OK;
3726}
3727
3728/*
3729 * Handle first level expression:
3730 * expr2 || expr2 || expr2 logical OR
3731 *
3732 * "arg" must point to the first non-white of the expression.
3733 * "arg" is advanced to the next non-white after the recognized expression.
3734 *
3735 * Return OK or FAIL.
3736 */
3737 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003738eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 int evaluate;
3742{
Bram Moolenaar33570922005-01-25 22:26:29 +00003743 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 long result;
3745 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003746 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
3748 /*
3749 * Get the first variable.
3750 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003751 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 return FAIL;
3753
3754 /*
3755 * Repeat until there is no following "||".
3756 */
3757 first = TRUE;
3758 result = FALSE;
3759 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3760 {
3761 if (evaluate && first)
3762 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003763 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003765 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003766 if (error)
3767 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 first = FALSE;
3769 }
3770
3771 /*
3772 * Get the second variable.
3773 */
3774 *arg = skipwhite(*arg + 2);
3775 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3776 return FAIL;
3777
3778 /*
3779 * Compute the result.
3780 */
3781 if (evaluate && !result)
3782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003783 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003785 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003786 if (error)
3787 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
3789 if (evaluate)
3790 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003791 rettv->v_type = VAR_NUMBER;
3792 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
3794 }
3795
3796 return OK;
3797}
3798
3799/*
3800 * Handle second level expression:
3801 * expr3 && expr3 && expr3 logical AND
3802 *
3803 * "arg" must point to the first non-white of the expression.
3804 * "arg" is advanced to the next non-white after the recognized expression.
3805 *
3806 * Return OK or FAIL.
3807 */
3808 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003809eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 int evaluate;
3813{
Bram Moolenaar33570922005-01-25 22:26:29 +00003814 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 long result;
3816 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003817 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
3819 /*
3820 * Get the first variable.
3821 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003822 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 return FAIL;
3824
3825 /*
3826 * Repeat until there is no following "&&".
3827 */
3828 first = TRUE;
3829 result = TRUE;
3830 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3831 {
3832 if (evaluate && first)
3833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003834 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003836 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003837 if (error)
3838 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 first = FALSE;
3840 }
3841
3842 /*
3843 * Get the second variable.
3844 */
3845 *arg = skipwhite(*arg + 2);
3846 if (eval4(arg, &var2, evaluate && result) == FAIL)
3847 return FAIL;
3848
3849 /*
3850 * Compute the result.
3851 */
3852 if (evaluate && result)
3853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003854 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003856 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003857 if (error)
3858 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 }
3860 if (evaluate)
3861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003862 rettv->v_type = VAR_NUMBER;
3863 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
3865 }
3866
3867 return OK;
3868}
3869
3870/*
3871 * Handle third level expression:
3872 * var1 == var2
3873 * var1 =~ var2
3874 * var1 != var2
3875 * var1 !~ var2
3876 * var1 > var2
3877 * var1 >= var2
3878 * var1 < var2
3879 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003880 * var1 is var2
3881 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 *
3883 * "arg" must point to the first non-white of the expression.
3884 * "arg" is advanced to the next non-white after the recognized expression.
3885 *
3886 * Return OK or FAIL.
3887 */
3888 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003889eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 int evaluate;
3893{
Bram Moolenaar33570922005-01-25 22:26:29 +00003894 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 char_u *p;
3896 int i;
3897 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003898 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 int len = 2;
3900 long n1, n2;
3901 char_u *s1, *s2;
3902 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3903 regmatch_T regmatch;
3904 int ic;
3905 char_u *save_cpo;
3906
3907 /*
3908 * Get the first variable.
3909 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003910 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 return FAIL;
3912
3913 p = *arg;
3914 switch (p[0])
3915 {
3916 case '=': if (p[1] == '=')
3917 type = TYPE_EQUAL;
3918 else if (p[1] == '~')
3919 type = TYPE_MATCH;
3920 break;
3921 case '!': if (p[1] == '=')
3922 type = TYPE_NEQUAL;
3923 else if (p[1] == '~')
3924 type = TYPE_NOMATCH;
3925 break;
3926 case '>': if (p[1] != '=')
3927 {
3928 type = TYPE_GREATER;
3929 len = 1;
3930 }
3931 else
3932 type = TYPE_GEQUAL;
3933 break;
3934 case '<': if (p[1] != '=')
3935 {
3936 type = TYPE_SMALLER;
3937 len = 1;
3938 }
3939 else
3940 type = TYPE_SEQUAL;
3941 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003942 case 'i': if (p[1] == 's')
3943 {
3944 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3945 len = 5;
3946 if (!vim_isIDc(p[len]))
3947 {
3948 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3949 type_is = TRUE;
3950 }
3951 }
3952 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
3954
3955 /*
3956 * If there is a comparitive operator, use it.
3957 */
3958 if (type != TYPE_UNKNOWN)
3959 {
3960 /* extra question mark appended: ignore case */
3961 if (p[len] == '?')
3962 {
3963 ic = TRUE;
3964 ++len;
3965 }
3966 /* extra '#' appended: match case */
3967 else if (p[len] == '#')
3968 {
3969 ic = FALSE;
3970 ++len;
3971 }
3972 /* nothing appened: use 'ignorecase' */
3973 else
3974 ic = p_ic;
3975
3976 /*
3977 * Get the second variable.
3978 */
3979 *arg = skipwhite(p + len);
3980 if (eval5(arg, &var2, evaluate) == FAIL)
3981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003982 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 return FAIL;
3984 }
3985
3986 if (evaluate)
3987 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003988 if (type_is && rettv->v_type != var2.v_type)
3989 {
3990 /* For "is" a different type always means FALSE, for "notis"
3991 * it means TRUE. */
3992 n1 = (type == TYPE_NEQUAL);
3993 }
3994 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3995 {
3996 if (type_is)
3997 {
3998 n1 = (rettv->v_type == var2.v_type
3999 && rettv->vval.v_list == var2.vval.v_list);
4000 if (type == TYPE_NEQUAL)
4001 n1 = !n1;
4002 }
4003 else if (rettv->v_type != var2.v_type
4004 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4005 {
4006 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004007 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004008 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004009 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004010 clear_tv(rettv);
4011 clear_tv(&var2);
4012 return FAIL;
4013 }
4014 else
4015 {
4016 /* Compare two Lists for being equal or unequal. */
4017 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4018 if (type == TYPE_NEQUAL)
4019 n1 = !n1;
4020 }
4021 }
4022
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004023 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4024 {
4025 if (type_is)
4026 {
4027 n1 = (rettv->v_type == var2.v_type
4028 && rettv->vval.v_dict == var2.vval.v_dict);
4029 if (type == TYPE_NEQUAL)
4030 n1 = !n1;
4031 }
4032 else if (rettv->v_type != var2.v_type
4033 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4034 {
4035 if (rettv->v_type != var2.v_type)
4036 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4037 else
4038 EMSG(_("E736: Invalid operation for Dictionary"));
4039 clear_tv(rettv);
4040 clear_tv(&var2);
4041 return FAIL;
4042 }
4043 else
4044 {
4045 /* Compare two Dictionaries for being equal or unequal. */
4046 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4047 if (type == TYPE_NEQUAL)
4048 n1 = !n1;
4049 }
4050 }
4051
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004052 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4053 {
4054 if (rettv->v_type != var2.v_type
4055 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4056 {
4057 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004058 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004059 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004060 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004061 clear_tv(rettv);
4062 clear_tv(&var2);
4063 return FAIL;
4064 }
4065 else
4066 {
4067 /* Compare two Funcrefs for being equal or unequal. */
4068 if (rettv->vval.v_string == NULL
4069 || var2.vval.v_string == NULL)
4070 n1 = FALSE;
4071 else
4072 n1 = STRCMP(rettv->vval.v_string,
4073 var2.vval.v_string) == 0;
4074 if (type == TYPE_NEQUAL)
4075 n1 = !n1;
4076 }
4077 }
4078
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 /*
4080 * If one of the two variables is a number, compare as a number.
4081 * When using "=~" or "!~", always compare as string.
4082 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004083 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 n1 = get_tv_number(rettv);
4087 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 switch (type)
4089 {
4090 case TYPE_EQUAL: n1 = (n1 == n2); break;
4091 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4092 case TYPE_GREATER: n1 = (n1 > n2); break;
4093 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4094 case TYPE_SMALLER: n1 = (n1 < n2); break;
4095 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4096 case TYPE_UNKNOWN:
4097 case TYPE_MATCH:
4098 case TYPE_NOMATCH: break; /* avoid gcc warning */
4099 }
4100 }
4101 else
4102 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 s1 = get_tv_string_buf(rettv, buf1);
4104 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4106 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4107 else
4108 i = 0;
4109 n1 = FALSE;
4110 switch (type)
4111 {
4112 case TYPE_EQUAL: n1 = (i == 0); break;
4113 case TYPE_NEQUAL: n1 = (i != 0); break;
4114 case TYPE_GREATER: n1 = (i > 0); break;
4115 case TYPE_GEQUAL: n1 = (i >= 0); break;
4116 case TYPE_SMALLER: n1 = (i < 0); break;
4117 case TYPE_SEQUAL: n1 = (i <= 0); break;
4118
4119 case TYPE_MATCH:
4120 case TYPE_NOMATCH:
4121 /* avoid 'l' flag in 'cpoptions' */
4122 save_cpo = p_cpo;
4123 p_cpo = (char_u *)"";
4124 regmatch.regprog = vim_regcomp(s2,
4125 RE_MAGIC + RE_STRING);
4126 regmatch.rm_ic = ic;
4127 if (regmatch.regprog != NULL)
4128 {
4129 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4130 vim_free(regmatch.regprog);
4131 if (type == TYPE_NOMATCH)
4132 n1 = !n1;
4133 }
4134 p_cpo = save_cpo;
4135 break;
4136
4137 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4138 }
4139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 clear_tv(rettv);
4141 clear_tv(&var2);
4142 rettv->v_type = VAR_NUMBER;
4143 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
4145 }
4146
4147 return OK;
4148}
4149
4150/*
4151 * Handle fourth level expression:
4152 * + number addition
4153 * - number subtraction
4154 * . string concatenation
4155 *
4156 * "arg" must point to the first non-white of the expression.
4157 * "arg" is advanced to the next non-white after the recognized expression.
4158 *
4159 * Return OK or FAIL.
4160 */
4161 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004162eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 int evaluate;
4166{
Bram Moolenaar33570922005-01-25 22:26:29 +00004167 typval_T var2;
4168 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 int op;
4170 long n1, n2;
4171 char_u *s1, *s2;
4172 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4173 char_u *p;
4174
4175 /*
4176 * Get the first variable.
4177 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 return FAIL;
4180
4181 /*
4182 * Repeat computing, until no '+', '-' or '.' is following.
4183 */
4184 for (;;)
4185 {
4186 op = **arg;
4187 if (op != '+' && op != '-' && op != '.')
4188 break;
4189
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (op != '+' || rettv->v_type != VAR_LIST)
4191 {
4192 /* For "list + ...", an illegal use of the first operand as
4193 * a number cannot be determined before evaluating the 2nd
4194 * operand: if this is also a list, all is ok.
4195 * For "something . ...", "something - ..." or "non-list + ...",
4196 * we know that the first operand needs to be a string or number
4197 * without evaluating the 2nd operand. So check before to avoid
4198 * side effects after an error. */
4199 if (evaluate && get_tv_string_chk(rettv) == NULL)
4200 {
4201 clear_tv(rettv);
4202 return FAIL;
4203 }
4204 }
4205
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 /*
4207 * Get the second variable.
4208 */
4209 *arg = skipwhite(*arg + 1);
4210 if (eval6(arg, &var2, evaluate) == FAIL)
4211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 return FAIL;
4214 }
4215
4216 if (evaluate)
4217 {
4218 /*
4219 * Compute the result.
4220 */
4221 if (op == '.')
4222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4224 s2 = get_tv_string_buf_chk(&var2, buf2);
4225 if (s2 == NULL) /* type error ? */
4226 {
4227 clear_tv(rettv);
4228 clear_tv(&var2);
4229 return FAIL;
4230 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004231 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 clear_tv(rettv);
4233 rettv->v_type = VAR_STRING;
4234 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004236 else if (op == '+' && rettv->v_type == VAR_LIST
4237 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004238 {
4239 /* concatenate Lists */
4240 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4241 &var3) == FAIL)
4242 {
4243 clear_tv(rettv);
4244 clear_tv(&var2);
4245 return FAIL;
4246 }
4247 clear_tv(rettv);
4248 *rettv = var3;
4249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 else
4251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004252 int error = FALSE;
4253
4254 n1 = get_tv_number_chk(rettv, &error);
4255 if (error)
4256 {
4257 /* This can only happen for "list + non-list".
4258 * For "non-list + ..." or "something - ...", we returned
4259 * before evaluating the 2nd operand. */
4260 clear_tv(rettv);
4261 return FAIL;
4262 }
4263 n2 = get_tv_number_chk(&var2, &error);
4264 if (error)
4265 {
4266 clear_tv(rettv);
4267 clear_tv(&var2);
4268 return FAIL;
4269 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (op == '+')
4272 n1 = n1 + n2;
4273 else
4274 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004275 rettv->v_type = VAR_NUMBER;
4276 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 }
4281 return OK;
4282}
4283
4284/*
4285 * Handle fifth level expression:
4286 * * number multiplication
4287 * / number division
4288 * % number modulo
4289 *
4290 * "arg" must point to the first non-white of the expression.
4291 * "arg" is advanced to the next non-white after the recognized expression.
4292 *
4293 * Return OK or FAIL.
4294 */
4295 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 int evaluate;
4300{
Bram Moolenaar33570922005-01-25 22:26:29 +00004301 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 int op;
4303 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004304 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 /*
4307 * Get the first variable.
4308 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 return FAIL;
4311
4312 /*
4313 * Repeat computing, until no '*', '/' or '%' is following.
4314 */
4315 for (;;)
4316 {
4317 op = **arg;
4318 if (op != '*' && op != '/' && op != '%')
4319 break;
4320
4321 if (evaluate)
4322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004325 if (error)
4326 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 else
4329 n1 = 0;
4330
4331 /*
4332 * Get the second variable.
4333 */
4334 *arg = skipwhite(*arg + 1);
4335 if (eval7(arg, &var2, evaluate) == FAIL)
4336 return FAIL;
4337
4338 if (evaluate)
4339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (error)
4343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
4345 /*
4346 * Compute the result.
4347 */
4348 if (op == '*')
4349 n1 = n1 * n2;
4350 else if (op == '/')
4351 {
4352 if (n2 == 0) /* give an error message? */
4353 n1 = 0x7fffffffL;
4354 else
4355 n1 = n1 / n2;
4356 }
4357 else
4358 {
4359 if (n2 == 0) /* give an error message? */
4360 n1 = 0;
4361 else
4362 n1 = n1 % n2;
4363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 rettv->v_type = VAR_NUMBER;
4365 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 }
4368
4369 return OK;
4370}
4371
4372/*
4373 * Handle sixth level expression:
4374 * number number constant
4375 * "string" string contstant
4376 * 'string' literal string contstant
4377 * &option-name option value
4378 * @r register contents
4379 * identifier variable value
4380 * function() function call
4381 * $VAR environment variable
4382 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004383 * [expr, expr] List
4384 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 *
4386 * Also handle:
4387 * ! in front logical NOT
4388 * - in front unary minus
4389 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004390 * trailing [] subscript in String or List
4391 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 *
4393 * "arg" must point to the first non-white of the expression.
4394 * "arg" is advanced to the next non-white after the recognized expression.
4395 *
4396 * Return OK or FAIL.
4397 */
4398 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004399eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 int evaluate;
4403{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 long n;
4405 int len;
4406 char_u *s;
4407 int val;
4408 char_u *start_leader, *end_leader;
4409 int ret = OK;
4410 char_u *alias;
4411
4412 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004413 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004414 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004416 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418 /*
4419 * Skip '!' and '-' characters. They are handled later.
4420 */
4421 start_leader = *arg;
4422 while (**arg == '!' || **arg == '-' || **arg == '+')
4423 *arg = skipwhite(*arg + 1);
4424 end_leader = *arg;
4425
4426 switch (**arg)
4427 {
4428 /*
4429 * Number constant.
4430 */
4431 case '0':
4432 case '1':
4433 case '2':
4434 case '3':
4435 case '4':
4436 case '5':
4437 case '6':
4438 case '7':
4439 case '8':
4440 case '9':
4441 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4442 *arg += len;
4443 if (evaluate)
4444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004445 rettv->v_type = VAR_NUMBER;
4446 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 break;
4449
4450 /*
4451 * String constant: "string".
4452 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004453 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 break;
4455
4456 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004457 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004459 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460 break;
4461
4462 /*
4463 * List: [expr, expr]
4464 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004465 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 break;
4467
4468 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004469 * Dictionary: {key: val, key: val}
4470 */
4471 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4472 break;
4473
4474 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004475 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004477 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 break;
4479
4480 /*
4481 * Environment variable: $VAR.
4482 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004483 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 break;
4485
4486 /*
4487 * Register contents: @r.
4488 */
4489 case '@': ++*arg;
4490 if (evaluate)
4491 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004492 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004493 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 }
4495 if (**arg != NUL)
4496 ++*arg;
4497 break;
4498
4499 /*
4500 * nested expression: (expression).
4501 */
4502 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004503 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if (**arg == ')')
4505 ++*arg;
4506 else if (ret == OK)
4507 {
4508 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004509 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 ret = FAIL;
4511 }
4512 break;
4513
Bram Moolenaar8c711452005-01-14 21:53:12 +00004514 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 break;
4516 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004517
4518 if (ret == NOTDONE)
4519 {
4520 /*
4521 * Must be a variable or function name.
4522 * Can also be a curly-braces kind of name: {expr}.
4523 */
4524 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004525 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004526 if (alias != NULL)
4527 s = alias;
4528
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004529 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004530 ret = FAIL;
4531 else
4532 {
4533 if (**arg == '(') /* recursive! */
4534 {
4535 /* If "s" is the name of a variable of type VAR_FUNC
4536 * use its contents. */
4537 s = deref_func_name(s, &len);
4538
4539 /* Invoke the function. */
4540 ret = get_func_tv(s, len, rettv, arg,
4541 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004542 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004543 /* Stop the expression evaluation when immediately
4544 * aborting on error, or when an interrupt occurred or
4545 * an exception was thrown but not caught. */
4546 if (aborting())
4547 {
4548 if (ret == OK)
4549 clear_tv(rettv);
4550 ret = FAIL;
4551 }
4552 }
4553 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004554 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004555 else
4556 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557 }
4558
4559 if (alias != NULL)
4560 vim_free(alias);
4561 }
4562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 *arg = skipwhite(*arg);
4564
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004565 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4566 * expr(expr). */
4567 if (ret == OK)
4568 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569
4570 /*
4571 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4572 */
4573 if (ret == OK && evaluate && end_leader > start_leader)
4574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004575 int error = FALSE;
4576
4577 val = get_tv_number_chk(rettv, &error);
4578 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004580 clear_tv(rettv);
4581 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004583 else
4584 {
4585 while (end_leader > start_leader)
4586 {
4587 --end_leader;
4588 if (*end_leader == '!')
4589 val = !val;
4590 else if (*end_leader == '-')
4591 val = -val;
4592 }
4593 clear_tv(rettv);
4594 rettv->v_type = VAR_NUMBER;
4595 rettv->vval.v_number = val;
4596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598
4599 return ret;
4600}
4601
4602/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004603 * Evaluate an "[expr]" or "[expr:expr]" index.
4604 * "*arg" points to the '['.
4605 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4606 */
4607 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004608eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004610 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004611 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004612 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004613{
4614 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004615 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617 long len = -1;
4618 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004620 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004624 if (verbose)
4625 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004626 return FAIL;
4627 }
4628
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004630 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004631 /*
4632 * dict.name
4633 */
4634 key = *arg + 1;
4635 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4636 ;
4637 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004638 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004639 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004640 }
4641 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004642 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004643 /*
4644 * something[idx]
4645 *
4646 * Get the (first) variable from inside the [].
4647 */
4648 *arg = skipwhite(*arg + 1);
4649 if (**arg == ':')
4650 empty1 = TRUE;
4651 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4652 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004653 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4654 {
4655 /* not a number or string */
4656 clear_tv(&var1);
4657 return FAIL;
4658 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004659
4660 /*
4661 * Get the second variable from inside the [:].
4662 */
4663 if (**arg == ':')
4664 {
4665 range = TRUE;
4666 *arg = skipwhite(*arg + 1);
4667 if (**arg == ']')
4668 empty2 = TRUE;
4669 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004671 if (!empty1)
4672 clear_tv(&var1);
4673 return FAIL;
4674 }
4675 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4676 {
4677 /* not a number or string */
4678 if (!empty1)
4679 clear_tv(&var1);
4680 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004681 return FAIL;
4682 }
4683 }
4684
4685 /* Check for the ']'. */
4686 if (**arg != ']')
4687 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004688 if (verbose)
4689 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004690 clear_tv(&var1);
4691 if (range)
4692 clear_tv(&var2);
4693 return FAIL;
4694 }
4695 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004696 }
4697
4698 if (evaluate)
4699 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700 n1 = 0;
4701 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004702 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004703 n1 = get_tv_number(&var1);
4704 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004705 }
4706 if (range)
4707 {
4708 if (empty2)
4709 n2 = -1;
4710 else
4711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004712 n2 = get_tv_number(&var2);
4713 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714 }
4715 }
4716
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004717 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 {
4719 case VAR_NUMBER:
4720 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004721 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 len = (long)STRLEN(s);
4723 if (range)
4724 {
4725 /* The resulting variable is a substring. If the indexes
4726 * are out of range the result is empty. */
4727 if (n1 < 0)
4728 {
4729 n1 = len + n1;
4730 if (n1 < 0)
4731 n1 = 0;
4732 }
4733 if (n2 < 0)
4734 n2 = len + n2;
4735 else if (n2 >= len)
4736 n2 = len;
4737 if (n1 >= len || n2 < 0 || n1 > n2)
4738 s = NULL;
4739 else
4740 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4741 }
4742 else
4743 {
4744 /* The resulting variable is a string of a single
4745 * character. If the index is too big or negative the
4746 * result is empty. */
4747 if (n1 >= len || n1 < 0)
4748 s = NULL;
4749 else
4750 s = vim_strnsave(s + n1, 1);
4751 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752 clear_tv(rettv);
4753 rettv->v_type = VAR_STRING;
4754 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 break;
4756
4757 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759 if (n1 < 0)
4760 n1 = len + n1;
4761 if (!empty1 && (n1 < 0 || n1 >= len))
4762 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004763 if (verbose)
4764 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 return FAIL;
4766 }
4767 if (range)
4768 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004769 list_T *l;
4770 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771
4772 if (n2 < 0)
4773 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004774 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004776 if (verbose)
4777 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778 return FAIL;
4779 }
4780 l = list_alloc();
4781 if (l == NULL)
4782 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004784 n1 <= n2; ++n1)
4785 {
4786 if (list_append_tv(l, &item->li_tv) == FAIL)
4787 {
4788 list_free(l);
4789 return FAIL;
4790 }
4791 item = item->li_next;
4792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004793 clear_tv(rettv);
4794 rettv->v_type = VAR_LIST;
4795 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004796 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797 }
4798 else
4799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004800 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004802 clear_tv(rettv);
4803 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004804 }
4805 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004806
4807 case VAR_DICT:
4808 if (range)
4809 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004810 if (verbose)
4811 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004812 if (len == -1)
4813 clear_tv(&var1);
4814 return FAIL;
4815 }
4816 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004817 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004818
4819 if (len == -1)
4820 {
4821 key = get_tv_string(&var1);
4822 if (*key == NUL)
4823 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004824 if (verbose)
4825 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004826 clear_tv(&var1);
4827 return FAIL;
4828 }
4829 }
4830
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004831 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004832
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004833 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004834 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004835 if (len == -1)
4836 clear_tv(&var1);
4837 if (item == NULL)
4838 return FAIL;
4839
4840 copy_tv(&item->di_tv, &var1);
4841 clear_tv(rettv);
4842 *rettv = var1;
4843 }
4844 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004845 }
4846 }
4847
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004848 return OK;
4849}
4850
4851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 * Get an option value.
4853 * "arg" points to the '&' or '+' before the option name.
4854 * "arg" is advanced to character after the option name.
4855 * Return OK or FAIL.
4856 */
4857 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004858get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004860 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 int evaluate;
4862{
4863 char_u *option_end;
4864 long numval;
4865 char_u *stringval;
4866 int opt_type;
4867 int c;
4868 int working = (**arg == '+'); /* has("+option") */
4869 int ret = OK;
4870 int opt_flags;
4871
4872 /*
4873 * Isolate the option name and find its value.
4874 */
4875 option_end = find_option_end(arg, &opt_flags);
4876 if (option_end == NULL)
4877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004878 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 EMSG2(_("E112: Option name missing: %s"), *arg);
4880 return FAIL;
4881 }
4882
4883 if (!evaluate)
4884 {
4885 *arg = option_end;
4886 return OK;
4887 }
4888
4889 c = *option_end;
4890 *option_end = NUL;
4891 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004892 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893
4894 if (opt_type == -3) /* invalid name */
4895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004896 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 EMSG2(_("E113: Unknown option: %s"), *arg);
4898 ret = FAIL;
4899 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004900 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 {
4902 if (opt_type == -2) /* hidden string option */
4903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 rettv->v_type = VAR_STRING;
4905 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907 else if (opt_type == -1) /* hidden number option */
4908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909 rettv->v_type = VAR_NUMBER;
4910 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 else if (opt_type == 1) /* number option */
4913 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 rettv->v_type = VAR_NUMBER;
4915 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 else /* string option */
4918 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004919 rettv->v_type = VAR_STRING;
4920 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 }
4922 }
4923 else if (working && (opt_type == -2 || opt_type == -1))
4924 ret = FAIL;
4925
4926 *option_end = c; /* put back for error messages */
4927 *arg = option_end;
4928
4929 return ret;
4930}
4931
4932/*
4933 * Allocate a variable for a string constant.
4934 * Return OK or FAIL.
4935 */
4936 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004937get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 int evaluate;
4941{
4942 char_u *p;
4943 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 int extra = 0;
4945
4946 /*
4947 * Find the end of the string, skipping backslashed characters.
4948 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004949 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
4951 if (*p == '\\' && p[1] != NUL)
4952 {
4953 ++p;
4954 /* A "\<x>" form occupies at least 4 characters, and produces up
4955 * to 6 characters: reserve space for 2 extra */
4956 if (*p == '<')
4957 extra += 2;
4958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 }
4960
4961 if (*p != '"')
4962 {
4963 EMSG2(_("E114: Missing quote: %s"), *arg);
4964 return FAIL;
4965 }
4966
4967 /* If only parsing, set *arg and return here */
4968 if (!evaluate)
4969 {
4970 *arg = p + 1;
4971 return OK;
4972 }
4973
4974 /*
4975 * Copy the string into allocated memory, handling backslashed
4976 * characters.
4977 */
4978 name = alloc((unsigned)(p - *arg + extra));
4979 if (name == NULL)
4980 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981 rettv->v_type = VAR_STRING;
4982 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983
Bram Moolenaar8c711452005-01-14 21:53:12 +00004984 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 if (*p == '\\')
4987 {
4988 switch (*++p)
4989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 case 'b': *name++ = BS; ++p; break;
4991 case 'e': *name++ = ESC; ++p; break;
4992 case 'f': *name++ = FF; ++p; break;
4993 case 'n': *name++ = NL; ++p; break;
4994 case 'r': *name++ = CAR; ++p; break;
4995 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996
4997 case 'X': /* hex: "\x1", "\x12" */
4998 case 'x':
4999 case 'u': /* Unicode: "\u0023" */
5000 case 'U':
5001 if (vim_isxdigit(p[1]))
5002 {
5003 int n, nr;
5004 int c = toupper(*p);
5005
5006 if (c == 'X')
5007 n = 2;
5008 else
5009 n = 4;
5010 nr = 0;
5011 while (--n >= 0 && vim_isxdigit(p[1]))
5012 {
5013 ++p;
5014 nr = (nr << 4) + hex2nr(*p);
5015 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017#ifdef FEAT_MBYTE
5018 /* For "\u" store the number according to
5019 * 'encoding'. */
5020 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005021 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 else
5023#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005024 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 break;
5027
5028 /* octal: "\1", "\12", "\123" */
5029 case '0':
5030 case '1':
5031 case '2':
5032 case '3':
5033 case '4':
5034 case '5':
5035 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 case '7': *name = *p++ - '0';
5037 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 *name = (*name << 3) + *p++ - '0';
5040 if (*p >= '0' && *p <= '7')
5041 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 break;
5045
5046 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005047 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 if (extra != 0)
5049 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005050 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 break;
5052 }
5053 /* FALLTHROUGH */
5054
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 break;
5057 }
5058 }
5059 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 *arg = p + 1;
5065
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 return OK;
5067}
5068
5069/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 * Return OK or FAIL.
5072 */
5073 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 int evaluate;
5078{
5079 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005080 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005081 int reduce = 0;
5082
5083 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005084 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005085 */
5086 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5087 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005089 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005091 break;
5092 ++reduce;
5093 ++p;
5094 }
5095 }
5096
Bram Moolenaar8c711452005-01-14 21:53:12 +00005097 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005098 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005100 return FAIL;
5101 }
5102
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005104 if (!evaluate)
5105 {
5106 *arg = p + 1;
5107 return OK;
5108 }
5109
5110 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005112 */
5113 str = alloc((unsigned)((p - *arg) - reduce));
5114 if (str == NULL)
5115 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116 rettv->v_type = VAR_STRING;
5117 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005118
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005120 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005122 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005123 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005124 break;
5125 ++p;
5126 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005128 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005130 *arg = p + 1;
5131
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005132 return OK;
5133}
5134
5135/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005136 * Allocate a variable for a List and fill it from "*arg".
5137 * Return OK or FAIL.
5138 */
5139 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005140get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005142 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143 int evaluate;
5144{
Bram Moolenaar33570922005-01-25 22:26:29 +00005145 list_T *l = NULL;
5146 typval_T tv;
5147 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005148
5149 if (evaluate)
5150 {
5151 l = list_alloc();
5152 if (l == NULL)
5153 return FAIL;
5154 }
5155
5156 *arg = skipwhite(*arg + 1);
5157 while (**arg != ']' && **arg != NUL)
5158 {
5159 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5160 goto failret;
5161 if (evaluate)
5162 {
5163 item = listitem_alloc();
5164 if (item != NULL)
5165 {
5166 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005167 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005168 list_append(l, item);
5169 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005170 else
5171 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005172 }
5173
5174 if (**arg == ']')
5175 break;
5176 if (**arg != ',')
5177 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 goto failret;
5180 }
5181 *arg = skipwhite(*arg + 1);
5182 }
5183
5184 if (**arg != ']')
5185 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187failret:
5188 if (evaluate)
5189 list_free(l);
5190 return FAIL;
5191 }
5192
5193 *arg = skipwhite(*arg + 1);
5194 if (evaluate)
5195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005196 rettv->v_type = VAR_LIST;
5197 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005198 ++l->lv_refcount;
5199 }
5200
5201 return OK;
5202}
5203
5204/*
5205 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005206 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005207 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005208 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005209list_alloc()
5210{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005211 list_T *l;
5212
5213 l = (list_T *)alloc_clear(sizeof(list_T));
5214 if (l != NULL)
5215 {
5216 /* Prepend the list to the list of lists for garbage collection. */
5217 if (first_list != NULL)
5218 first_list->lv_used_prev = l;
5219 l->lv_used_prev = NULL;
5220 l->lv_used_next = first_list;
5221 first_list = l;
5222 }
5223 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224}
5225
5226/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005227 * Allocate an empty list for a return value.
5228 * Returns OK or FAIL.
5229 */
5230 static int
5231rettv_list_alloc(rettv)
5232 typval_T *rettv;
5233{
5234 list_T *l = list_alloc();
5235
5236 if (l == NULL)
5237 return FAIL;
5238
5239 rettv->vval.v_list = l;
5240 rettv->v_type = VAR_LIST;
5241 ++l->lv_refcount;
5242 return OK;
5243}
5244
5245/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246 * Unreference a list: decrement the reference count and free it when it
5247 * becomes zero.
5248 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005249 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005251 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005253 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5254 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255}
5256
5257/*
5258 * Free a list, including all items it points to.
5259 * Ignores the reference count.
5260 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005261 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264{
Bram Moolenaar33570922005-01-25 22:26:29 +00005265 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266
Bram Moolenaard9fba312005-06-26 22:34:35 +00005267 /* Avoid that recursive reference to the list frees us again. */
5268 l->lv_refcount = DEL_REFCOUNT;
5269
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005270 /* Remove the list from the list of lists for garbage collection. */
5271 if (l->lv_used_prev == NULL)
5272 first_list = l->lv_used_next;
5273 else
5274 l->lv_used_prev->lv_used_next = l->lv_used_next;
5275 if (l->lv_used_next != NULL)
5276 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5277
Bram Moolenaard9fba312005-06-26 22:34:35 +00005278 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005280 /* Remove the item before deleting it. */
5281 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 listitem_free(item);
5283 }
5284 vim_free(l);
5285}
5286
5287/*
5288 * Allocate a list item.
5289 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005290 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291listitem_alloc()
5292{
Bram Moolenaar33570922005-01-25 22:26:29 +00005293 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294}
5295
5296/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005297 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 */
5299 static void
5300listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005301 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005303 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 vim_free(item);
5305}
5306
5307/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005308 * Remove a list item from a List and free it. Also clears the value.
5309 */
5310 static void
5311listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005312 list_T *l;
5313 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005314{
5315 list_remove(l, item, item);
5316 listitem_free(item);
5317}
5318
5319/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 * Get the number of items in a list.
5321 */
5322 static long
5323list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005324 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326 if (l == NULL)
5327 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005328 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329}
5330
5331/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005332 * Return TRUE when two lists have exactly the same values.
5333 */
5334 static int
5335list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005336 list_T *l1;
5337 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005338 int ic; /* ignore case for strings */
5339{
Bram Moolenaar33570922005-01-25 22:26:29 +00005340 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005341
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005342 if (list_len(l1) != list_len(l2))
5343 return FALSE;
5344
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005345 for (item1 = l1->lv_first, item2 = l2->lv_first;
5346 item1 != NULL && item2 != NULL;
5347 item1 = item1->li_next, item2 = item2->li_next)
5348 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5349 return FALSE;
5350 return item1 == NULL && item2 == NULL;
5351}
5352
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005353#if defined(FEAT_PYTHON) || defined(PROTO)
5354/*
5355 * Return the dictitem that an entry in a hashtable points to.
5356 */
5357 dictitem_T *
5358dict_lookup(hi)
5359 hashitem_T *hi;
5360{
5361 return HI2DI(hi);
5362}
5363#endif
5364
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005365/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005366 * Return TRUE when two dictionaries have exactly the same key/values.
5367 */
5368 static int
5369dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005370 dict_T *d1;
5371 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005372 int ic; /* ignore case for strings */
5373{
Bram Moolenaar33570922005-01-25 22:26:29 +00005374 hashitem_T *hi;
5375 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005376 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005377
5378 if (dict_len(d1) != dict_len(d2))
5379 return FALSE;
5380
Bram Moolenaar33570922005-01-25 22:26:29 +00005381 todo = d1->dv_hashtab.ht_used;
5382 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005383 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005384 if (!HASHITEM_EMPTY(hi))
5385 {
5386 item2 = dict_find(d2, hi->hi_key, -1);
5387 if (item2 == NULL)
5388 return FALSE;
5389 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5390 return FALSE;
5391 --todo;
5392 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005393 }
5394 return TRUE;
5395}
5396
5397/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005398 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005399 * Compares the items just like "==" would compare them, but strings and
5400 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005401 */
5402 static int
5403tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005404 typval_T *tv1;
5405 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005406 int ic; /* ignore case */
5407{
5408 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005409 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005410
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005411 if (tv1->v_type != tv2->v_type)
5412 return FALSE;
5413
5414 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005415 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005416 case VAR_LIST:
5417 /* recursive! */
5418 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5419
5420 case VAR_DICT:
5421 /* recursive! */
5422 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5423
5424 case VAR_FUNC:
5425 return (tv1->vval.v_string != NULL
5426 && tv2->vval.v_string != NULL
5427 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5428
5429 case VAR_NUMBER:
5430 return tv1->vval.v_number == tv2->vval.v_number;
5431
5432 case VAR_STRING:
5433 s1 = get_tv_string_buf(tv1, buf1);
5434 s2 = get_tv_string_buf(tv2, buf2);
5435 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005436 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005437
5438 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005439 return TRUE;
5440}
5441
5442/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005443 * Locate item with index "n" in list "l" and return it.
5444 * A negative index is counted from the end; -1 is the last item.
5445 * Returns NULL when "n" is out of range.
5446 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005447 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005449 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 long n;
5451{
Bram Moolenaar33570922005-01-25 22:26:29 +00005452 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 long idx;
5454
5455 if (l == NULL)
5456 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005457
5458 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005460 n = l->lv_len + n;
5461
5462 /* Check for index out of range. */
5463 if (n < 0 || n >= l->lv_len)
5464 return NULL;
5465
5466 /* When there is a cached index may start search from there. */
5467 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005469 if (n < l->lv_idx / 2)
5470 {
5471 /* closest to the start of the list */
5472 item = l->lv_first;
5473 idx = 0;
5474 }
5475 else if (n > (l->lv_idx + l->lv_len) / 2)
5476 {
5477 /* closest to the end of the list */
5478 item = l->lv_last;
5479 idx = l->lv_len - 1;
5480 }
5481 else
5482 {
5483 /* closest to the cached index */
5484 item = l->lv_idx_item;
5485 idx = l->lv_idx;
5486 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 }
5488 else
5489 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005490 if (n < l->lv_len / 2)
5491 {
5492 /* closest to the start of the list */
5493 item = l->lv_first;
5494 idx = 0;
5495 }
5496 else
5497 {
5498 /* closest to the end of the list */
5499 item = l->lv_last;
5500 idx = l->lv_len - 1;
5501 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005503
5504 while (n > idx)
5505 {
5506 /* search forward */
5507 item = item->li_next;
5508 ++idx;
5509 }
5510 while (n < idx)
5511 {
5512 /* search backward */
5513 item = item->li_prev;
5514 --idx;
5515 }
5516
5517 /* cache the used index */
5518 l->lv_idx = idx;
5519 l->lv_idx_item = item;
5520
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 return item;
5522}
5523
5524/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005525 * Locate "item" list "l" and return its index.
5526 * Returns -1 when "item" is not in the list.
5527 */
5528 static long
5529list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005530 list_T *l;
5531 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005532{
5533 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005534 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005535
5536 if (l == NULL)
5537 return -1;
5538 idx = 0;
5539 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5540 ++idx;
5541 if (li == NULL)
5542 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005543 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005544}
5545
5546/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 * Append item "item" to the end of list "l".
5548 */
5549 static void
5550list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005551 list_T *l;
5552 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553{
5554 if (l->lv_last == NULL)
5555 {
5556 /* empty list */
5557 l->lv_first = item;
5558 l->lv_last = item;
5559 item->li_prev = NULL;
5560 }
5561 else
5562 {
5563 l->lv_last->li_next = item;
5564 item->li_prev = l->lv_last;
5565 l->lv_last = item;
5566 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005567 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 item->li_next = NULL;
5569}
5570
5571/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005572 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005573 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005574 */
5575 static int
5576list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005577 list_T *l;
5578 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005580 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581
Bram Moolenaar05159a02005-02-26 23:04:13 +00005582 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005583 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005584 copy_tv(tv, &li->li_tv);
5585 list_append(l, li);
5586 return OK;
5587}
5588
5589/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005590 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005591 * Return FAIL when out of memory.
5592 */
5593 int
5594list_append_dict(list, dict)
5595 list_T *list;
5596 dict_T *dict;
5597{
5598 listitem_T *li = listitem_alloc();
5599
5600 if (li == NULL)
5601 return FAIL;
5602 li->li_tv.v_type = VAR_DICT;
5603 li->li_tv.v_lock = 0;
5604 li->li_tv.vval.v_dict = dict;
5605 list_append(list, li);
5606 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005607 return OK;
5608}
5609
5610/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005611 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005612 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005613 * Returns FAIL when out of memory.
5614 */
5615 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005616list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005617 list_T *l;
5618 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005619 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005620{
5621 listitem_T *li = listitem_alloc();
5622
5623 if (li == NULL)
5624 return FAIL;
5625 list_append(l, li);
5626 li->li_tv.v_type = VAR_STRING;
5627 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005628 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5629 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005630 return FAIL;
5631 return OK;
5632}
5633
5634/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005635 * Append "n" to list "l".
5636 * Returns FAIL when out of memory.
5637 */
5638 static int
5639list_append_number(l, n)
5640 list_T *l;
5641 varnumber_T n;
5642{
5643 listitem_T *li;
5644
5645 li = listitem_alloc();
5646 if (li == NULL)
5647 return FAIL;
5648 li->li_tv.v_type = VAR_NUMBER;
5649 li->li_tv.v_lock = 0;
5650 li->li_tv.vval.v_number = n;
5651 list_append(l, li);
5652 return OK;
5653}
5654
5655/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005656 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005657 * If "item" is NULL append at the end.
5658 * Return FAIL when out of memory.
5659 */
5660 static int
5661list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005662 list_T *l;
5663 typval_T *tv;
5664 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005665{
Bram Moolenaar33570922005-01-25 22:26:29 +00005666 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005667
5668 if (ni == NULL)
5669 return FAIL;
5670 copy_tv(tv, &ni->li_tv);
5671 if (item == NULL)
5672 /* Append new item at end of list. */
5673 list_append(l, ni);
5674 else
5675 {
5676 /* Insert new item before existing item. */
5677 ni->li_prev = item->li_prev;
5678 ni->li_next = item;
5679 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005680 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005681 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005682 ++l->lv_idx;
5683 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005684 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005685 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005686 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005687 l->lv_idx_item = NULL;
5688 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005689 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005690 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005691 }
5692 return OK;
5693}
5694
5695/*
5696 * Extend "l1" with "l2".
5697 * If "bef" is NULL append at the end, otherwise insert before this item.
5698 * Returns FAIL when out of memory.
5699 */
5700 static int
5701list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005702 list_T *l1;
5703 list_T *l2;
5704 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005705{
Bram Moolenaar33570922005-01-25 22:26:29 +00005706 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005707
5708 for (item = l2->lv_first; item != NULL; item = item->li_next)
5709 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5710 return FAIL;
5711 return OK;
5712}
5713
5714/*
5715 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5716 * Return FAIL when out of memory.
5717 */
5718 static int
5719list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005720 list_T *l1;
5721 list_T *l2;
5722 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005723{
Bram Moolenaar33570922005-01-25 22:26:29 +00005724 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005725
5726 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005727 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005728 if (l == NULL)
5729 return FAIL;
5730 tv->v_type = VAR_LIST;
5731 tv->vval.v_list = l;
5732
5733 /* append all items from the second list */
5734 return list_extend(l, l2, NULL);
5735}
5736
5737/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005738 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005739 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005740 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005741 * Returns NULL when out of memory.
5742 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005743 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005744list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005745 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005747 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005748{
Bram Moolenaar33570922005-01-25 22:26:29 +00005749 list_T *copy;
5750 listitem_T *item;
5751 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005752
5753 if (orig == NULL)
5754 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755
5756 copy = list_alloc();
5757 if (copy != NULL)
5758 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005759 if (copyID != 0)
5760 {
5761 /* Do this before adding the items, because one of the items may
5762 * refer back to this list. */
5763 orig->lv_copyID = copyID;
5764 orig->lv_copylist = copy;
5765 }
5766 for (item = orig->lv_first; item != NULL && !got_int;
5767 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768 {
5769 ni = listitem_alloc();
5770 if (ni == NULL)
5771 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005772 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005773 {
5774 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5775 {
5776 vim_free(ni);
5777 break;
5778 }
5779 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005781 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782 list_append(copy, ni);
5783 }
5784 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005785 if (item != NULL)
5786 {
5787 list_unref(copy);
5788 copy = NULL;
5789 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005790 }
5791
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792 return copy;
5793}
5794
5795/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005796 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005797 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005799 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005800list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005801 list_T *l;
5802 listitem_T *item;
5803 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005804{
Bram Moolenaar33570922005-01-25 22:26:29 +00005805 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005807 /* notify watchers */
5808 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005809 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005810 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005811 list_fix_watch(l, ip);
5812 if (ip == item2)
5813 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005815
5816 if (item2->li_next == NULL)
5817 l->lv_last = item->li_prev;
5818 else
5819 item2->li_next->li_prev = item->li_prev;
5820 if (item->li_prev == NULL)
5821 l->lv_first = item2->li_next;
5822 else
5823 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005824 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825}
5826
5827/*
5828 * Return an allocated string with the string representation of a list.
5829 * May return NULL.
5830 */
5831 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005832list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005833 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005834 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835{
5836 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837
5838 if (tv->vval.v_list == NULL)
5839 return NULL;
5840 ga_init2(&ga, (int)sizeof(char), 80);
5841 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005842 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005843 {
5844 vim_free(ga.ga_data);
5845 return NULL;
5846 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 ga_append(&ga, ']');
5848 ga_append(&ga, NUL);
5849 return (char_u *)ga.ga_data;
5850}
5851
5852/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 * Join list "l" into a string in "*gap", using separator "sep".
5854 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005855 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005857 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005858list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005860 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 char_u *sep;
5862 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005863 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864{
5865 int first = TRUE;
5866 char_u *tofree;
5867 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005868 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 char_u *s;
5870
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005871 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 {
5873 if (first)
5874 first = FALSE;
5875 else
5876 ga_concat(gap, sep);
5877
5878 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005879 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005881 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 if (s != NULL)
5883 ga_concat(gap, s);
5884 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005885 if (s == NULL)
5886 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005888 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889}
5890
5891/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005892 * Garbage collection for lists and dictionaries.
5893 *
5894 * We use reference counts to be able to free most items right away when they
5895 * are no longer used. But for composite items it's possible that it becomes
5896 * unused while the reference count is > 0: When there is a recursive
5897 * reference. Example:
5898 * :let l = [1, 2, 3]
5899 * :let d = {9: l}
5900 * :let l[1] = d
5901 *
5902 * Since this is quite unusual we handle this with garbage collection: every
5903 * once in a while find out which lists and dicts are not referenced from any
5904 * variable.
5905 *
5906 * Here is a good reference text about garbage collection (refers to Python
5907 * but it applies to all reference-counting mechanisms):
5908 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005909 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005910
5911/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005912 * Do garbage collection for lists and dicts.
5913 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005914 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005915 int
5916garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005917{
5918 dict_T *dd;
5919 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005920 int copyID = ++current_copyID;
5921 buf_T *buf;
5922 win_T *wp;
5923 int i;
5924 funccall_T *fc;
5925 int did_free = FALSE;
5926
5927 /*
5928 * 1. Go through all accessible variables and mark all lists and dicts
5929 * with copyID.
5930 */
5931 /* script-local variables */
5932 for (i = 1; i <= ga_scripts.ga_len; ++i)
5933 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5934
5935 /* buffer-local variables */
5936 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5937 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5938
5939 /* window-local variables */
5940 FOR_ALL_WINDOWS(wp)
5941 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5942
5943 /* global variables */
5944 set_ref_in_ht(&globvarht, copyID);
5945
5946 /* function-local variables */
5947 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5948 {
5949 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5950 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5951 }
5952
5953 /*
5954 * 2. Go through the list of dicts and free items without the copyID.
5955 */
5956 for (dd = first_dict; dd != NULL; )
5957 if (dd->dv_copyID != copyID)
5958 {
5959 dict_free(dd);
5960 did_free = TRUE;
5961
5962 /* restart, next dict may also have been freed */
5963 dd = first_dict;
5964 }
5965 else
5966 dd = dd->dv_used_next;
5967
5968 /*
5969 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005970 * But don't free a list that has a watcher (used in a for loop), these
5971 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005972 */
5973 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005974 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005975 {
5976 list_free(ll);
5977 did_free = TRUE;
5978
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005979 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005980 ll = first_list;
5981 }
5982 else
5983 ll = ll->lv_used_next;
5984
5985 return did_free;
5986}
5987
5988/*
5989 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5990 */
5991 static void
5992set_ref_in_ht(ht, copyID)
5993 hashtab_T *ht;
5994 int copyID;
5995{
5996 int todo;
5997 hashitem_T *hi;
5998
5999 todo = ht->ht_used;
6000 for (hi = ht->ht_array; todo > 0; ++hi)
6001 if (!HASHITEM_EMPTY(hi))
6002 {
6003 --todo;
6004 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6005 }
6006}
6007
6008/*
6009 * Mark all lists and dicts referenced through list "l" with "copyID".
6010 */
6011 static void
6012set_ref_in_list(l, copyID)
6013 list_T *l;
6014 int copyID;
6015{
6016 listitem_T *li;
6017
6018 for (li = l->lv_first; li != NULL; li = li->li_next)
6019 set_ref_in_item(&li->li_tv, copyID);
6020}
6021
6022/*
6023 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6024 */
6025 static void
6026set_ref_in_item(tv, copyID)
6027 typval_T *tv;
6028 int copyID;
6029{
6030 dict_T *dd;
6031 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006032
6033 switch (tv->v_type)
6034 {
6035 case VAR_DICT:
6036 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006037 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006038 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006039 /* Didn't see this dict yet. */
6040 dd->dv_copyID = copyID;
6041 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006042 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006043 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006044
6045 case VAR_LIST:
6046 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006047 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006048 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006049 /* Didn't see this list yet. */
6050 ll->lv_copyID = copyID;
6051 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006052 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006053 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006054 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006055 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006056}
6057
6058/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006059 * Allocate an empty header for a dictionary.
6060 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006061 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006062dict_alloc()
6063{
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006065
Bram Moolenaar33570922005-01-25 22:26:29 +00006066 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006067 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006068 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006069 /* Add the list to the hashtable for garbage collection. */
6070 if (first_dict != NULL)
6071 first_dict->dv_used_prev = d;
6072 d->dv_used_next = first_dict;
6073 d->dv_used_prev = NULL;
6074
Bram Moolenaar33570922005-01-25 22:26:29 +00006075 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006076 d->dv_lock = 0;
6077 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006078 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006079 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006080 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006081}
6082
6083/*
6084 * Unreference a Dictionary: decrement the reference count and free it when it
6085 * becomes zero.
6086 */
6087 static void
6088dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006089 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006090{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006091 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6092 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006093}
6094
6095/*
6096 * Free a Dictionary, including all items it contains.
6097 * Ignores the reference count.
6098 */
6099 static void
6100dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006101 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006102{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006103 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006105 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006106
Bram Moolenaard9fba312005-06-26 22:34:35 +00006107 /* Avoid that recursive reference to the dict frees us again. */
6108 d->dv_refcount = DEL_REFCOUNT;
6109
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006110 /* Remove the dict from the list of dicts for garbage collection. */
6111 if (d->dv_used_prev == NULL)
6112 first_dict = d->dv_used_next;
6113 else
6114 d->dv_used_prev->dv_used_next = d->dv_used_next;
6115 if (d->dv_used_next != NULL)
6116 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6117
6118 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006119 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006120 todo = d->dv_hashtab.ht_used;
6121 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006122 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006123 if (!HASHITEM_EMPTY(hi))
6124 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006125 /* Remove the item before deleting it, just in case there is
6126 * something recursive causing trouble. */
6127 di = HI2DI(hi);
6128 hash_remove(&d->dv_hashtab, hi);
6129 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006130 --todo;
6131 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006132 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006134 vim_free(d);
6135}
6136
6137/*
6138 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006139 * The "key" is copied to the new item.
6140 * Note that the value of the item "di_tv" still needs to be initialized!
6141 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006142 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006144dictitem_alloc(key)
6145 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006146{
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006148
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006150 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006151 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006152 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006153 di->di_flags = 0;
6154 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006156}
6157
6158/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006159 * Make a copy of a Dictionary item.
6160 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006161 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006162dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006163 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006164{
Bram Moolenaar33570922005-01-25 22:26:29 +00006165 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006166
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006168 if (di != NULL)
6169 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006170 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006171 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006172 copy_tv(&org->di_tv, &di->di_tv);
6173 }
6174 return di;
6175}
6176
6177/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006178 * Remove item "item" from Dictionary "dict" and free it.
6179 */
6180 static void
6181dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006182 dict_T *dict;
6183 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006184{
Bram Moolenaar33570922005-01-25 22:26:29 +00006185 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006186
Bram Moolenaar33570922005-01-25 22:26:29 +00006187 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006188 if (HASHITEM_EMPTY(hi))
6189 EMSG2(_(e_intern2), "dictitem_remove()");
6190 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006191 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006192 dictitem_free(item);
6193}
6194
6195/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006196 * Free a dict item. Also clears the value.
6197 */
6198 static void
6199dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006200 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006201{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006202 clear_tv(&item->di_tv);
6203 vim_free(item);
6204}
6205
6206/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006207 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6208 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006209 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006210 * Returns NULL when out of memory.
6211 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006212 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006213dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006214 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006215 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006216 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006217{
Bram Moolenaar33570922005-01-25 22:26:29 +00006218 dict_T *copy;
6219 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006220 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006221 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006222
6223 if (orig == NULL)
6224 return NULL;
6225
6226 copy = dict_alloc();
6227 if (copy != NULL)
6228 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006229 if (copyID != 0)
6230 {
6231 orig->dv_copyID = copyID;
6232 orig->dv_copydict = copy;
6233 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006234 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006235 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006236 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006237 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006238 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006239 --todo;
6240
6241 di = dictitem_alloc(hi->hi_key);
6242 if (di == NULL)
6243 break;
6244 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006245 {
6246 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6247 copyID) == FAIL)
6248 {
6249 vim_free(di);
6250 break;
6251 }
6252 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006253 else
6254 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6255 if (dict_add(copy, di) == FAIL)
6256 {
6257 dictitem_free(di);
6258 break;
6259 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006260 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006261 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006262
Bram Moolenaare9a41262005-01-15 22:18:47 +00006263 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006264 if (todo > 0)
6265 {
6266 dict_unref(copy);
6267 copy = NULL;
6268 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006269 }
6270
6271 return copy;
6272}
6273
6274/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006275 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006276 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006277 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006278 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006279dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006280 dict_T *d;
6281 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006282{
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006284}
6285
Bram Moolenaar8c711452005-01-14 21:53:12 +00006286/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006287 * Add a number or string entry to dictionary "d".
6288 * When "str" is NULL use number "nr", otherwise use "str".
6289 * Returns FAIL when out of memory and when key already exists.
6290 */
6291 int
6292dict_add_nr_str(d, key, nr, str)
6293 dict_T *d;
6294 char *key;
6295 long nr;
6296 char_u *str;
6297{
6298 dictitem_T *item;
6299
6300 item = dictitem_alloc((char_u *)key);
6301 if (item == NULL)
6302 return FAIL;
6303 item->di_tv.v_lock = 0;
6304 if (str == NULL)
6305 {
6306 item->di_tv.v_type = VAR_NUMBER;
6307 item->di_tv.vval.v_number = nr;
6308 }
6309 else
6310 {
6311 item->di_tv.v_type = VAR_STRING;
6312 item->di_tv.vval.v_string = vim_strsave(str);
6313 }
6314 if (dict_add(d, item) == FAIL)
6315 {
6316 dictitem_free(item);
6317 return FAIL;
6318 }
6319 return OK;
6320}
6321
6322/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006323 * Get the number of items in a Dictionary.
6324 */
6325 static long
6326dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006328{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006329 if (d == NULL)
6330 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006331 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006332}
6333
6334/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006335 * Find item "key[len]" in Dictionary "d".
6336 * If "len" is negative use strlen(key).
6337 * Returns NULL when not found.
6338 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006340dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006341 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342 char_u *key;
6343 int len;
6344{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006345#define AKEYLEN 200
6346 char_u buf[AKEYLEN];
6347 char_u *akey;
6348 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006350
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006351 if (len < 0)
6352 akey = key;
6353 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006355 tofree = akey = vim_strnsave(key, len);
6356 if (akey == NULL)
6357 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006358 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006359 else
6360 {
6361 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006362 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006363 akey = buf;
6364 }
6365
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006367 vim_free(tofree);
6368 if (HASHITEM_EMPTY(hi))
6369 return NULL;
6370 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006371}
6372
6373/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006374 * Get a string item from a dictionary.
6375 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006376 * Returns NULL if the entry doesn't exist or out of memory.
6377 */
6378 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006379get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006380 dict_T *d;
6381 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006382 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006383{
6384 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006385 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006386
6387 di = dict_find(d, key, -1);
6388 if (di == NULL)
6389 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006390 s = get_tv_string(&di->di_tv);
6391 if (save && s != NULL)
6392 s = vim_strsave(s);
6393 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006394}
6395
6396/*
6397 * Get a number item from a dictionary.
6398 * Returns 0 if the entry doesn't exist or out of memory.
6399 */
6400 long
6401get_dict_number(d, key)
6402 dict_T *d;
6403 char_u *key;
6404{
6405 dictitem_T *di;
6406
6407 di = dict_find(d, key, -1);
6408 if (di == NULL)
6409 return 0;
6410 return get_tv_number(&di->di_tv);
6411}
6412
6413/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006414 * Return an allocated string with the string representation of a Dictionary.
6415 * May return NULL.
6416 */
6417 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006418dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006420 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006421{
6422 garray_T ga;
6423 int first = TRUE;
6424 char_u *tofree;
6425 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006427 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006428 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006429 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006430
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006431 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006432 return NULL;
6433 ga_init2(&ga, (int)sizeof(char), 80);
6434 ga_append(&ga, '{');
6435
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006437 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006438 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006439 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006440 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006441 --todo;
6442
6443 if (first)
6444 first = FALSE;
6445 else
6446 ga_concat(&ga, (char_u *)", ");
6447
6448 tofree = string_quote(hi->hi_key, FALSE);
6449 if (tofree != NULL)
6450 {
6451 ga_concat(&ga, tofree);
6452 vim_free(tofree);
6453 }
6454 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006455 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006456 if (s != NULL)
6457 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006458 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006459 if (s == NULL)
6460 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006461 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006462 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006463 if (todo > 0)
6464 {
6465 vim_free(ga.ga_data);
6466 return NULL;
6467 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006468
6469 ga_append(&ga, '}');
6470 ga_append(&ga, NUL);
6471 return (char_u *)ga.ga_data;
6472}
6473
6474/*
6475 * Allocate a variable for a Dictionary and fill it from "*arg".
6476 * Return OK or FAIL. Returns NOTDONE for {expr}.
6477 */
6478 static int
6479get_dict_tv(arg, rettv, evaluate)
6480 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006481 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006482 int evaluate;
6483{
Bram Moolenaar33570922005-01-25 22:26:29 +00006484 dict_T *d = NULL;
6485 typval_T tvkey;
6486 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006487 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006488 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006489 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006490 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006491
6492 /*
6493 * First check if it's not a curly-braces thing: {expr}.
6494 * Must do this without evaluating, otherwise a function may be called
6495 * twice. Unfortunately this means we need to call eval1() twice for the
6496 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006497 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006498 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006499 if (*start != '}')
6500 {
6501 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6502 return FAIL;
6503 if (*start == '}')
6504 return NOTDONE;
6505 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506
6507 if (evaluate)
6508 {
6509 d = dict_alloc();
6510 if (d == NULL)
6511 return FAIL;
6512 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006513 tvkey.v_type = VAR_UNKNOWN;
6514 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006515
6516 *arg = skipwhite(*arg + 1);
6517 while (**arg != '}' && **arg != NUL)
6518 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006519 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006520 goto failret;
6521 if (**arg != ':')
6522 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006523 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006524 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006525 goto failret;
6526 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006527 key = get_tv_string_buf_chk(&tvkey, buf);
6528 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006530 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6531 if (key != NULL)
6532 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006533 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006534 goto failret;
6535 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006536
6537 *arg = skipwhite(*arg + 1);
6538 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6539 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006540 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006541 goto failret;
6542 }
6543 if (evaluate)
6544 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006545 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006546 if (item != NULL)
6547 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006548 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006549 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006550 clear_tv(&tv);
6551 goto failret;
6552 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006553 item = dictitem_alloc(key);
6554 clear_tv(&tvkey);
6555 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006556 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006557 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006558 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006559 if (dict_add(d, item) == FAIL)
6560 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006561 }
6562 }
6563
6564 if (**arg == '}')
6565 break;
6566 if (**arg != ',')
6567 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006568 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006569 goto failret;
6570 }
6571 *arg = skipwhite(*arg + 1);
6572 }
6573
6574 if (**arg != '}')
6575 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006576 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006577failret:
6578 if (evaluate)
6579 dict_free(d);
6580 return FAIL;
6581 }
6582
6583 *arg = skipwhite(*arg + 1);
6584 if (evaluate)
6585 {
6586 rettv->v_type = VAR_DICT;
6587 rettv->vval.v_dict = d;
6588 ++d->dv_refcount;
6589 }
6590
6591 return OK;
6592}
6593
Bram Moolenaar8c711452005-01-14 21:53:12 +00006594/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595 * Return a string with the string representation of a variable.
6596 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006597 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006598 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006599 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600 * May return NULL;
6601 */
6602 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006603echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006604 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006606 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006607 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006609 static int recurse = 0;
6610 char_u *r = NULL;
6611
Bram Moolenaar33570922005-01-25 22:26:29 +00006612 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006613 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006614 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615 *tofree = NULL;
6616 return NULL;
6617 }
6618 ++recurse;
6619
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 switch (tv->v_type)
6621 {
6622 case VAR_FUNC:
6623 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006624 r = tv->vval.v_string;
6625 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006626
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006628 if (tv->vval.v_list == NULL)
6629 {
6630 *tofree = NULL;
6631 r = NULL;
6632 }
6633 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6634 {
6635 *tofree = NULL;
6636 r = (char_u *)"[...]";
6637 }
6638 else
6639 {
6640 tv->vval.v_list->lv_copyID = copyID;
6641 *tofree = list2string(tv, copyID);
6642 r = *tofree;
6643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006644 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006645
Bram Moolenaar8c711452005-01-14 21:53:12 +00006646 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006647 if (tv->vval.v_dict == NULL)
6648 {
6649 *tofree = NULL;
6650 r = NULL;
6651 }
6652 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6653 {
6654 *tofree = NULL;
6655 r = (char_u *)"{...}";
6656 }
6657 else
6658 {
6659 tv->vval.v_dict->dv_copyID = copyID;
6660 *tofree = dict2string(tv, copyID);
6661 r = *tofree;
6662 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006663 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006665 case VAR_STRING:
6666 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006667 *tofree = NULL;
6668 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006670
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006671 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006672 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006673 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006674 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006675
6676 --recurse;
6677 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006678}
6679
6680/*
6681 * Return a string with the string representation of a variable.
6682 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6683 * "numbuf" is used for a number.
6684 * Puts quotes around strings, so that they can be parsed back by eval().
6685 * May return NULL;
6686 */
6687 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006688tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006689 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006690 char_u **tofree;
6691 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006692 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006693{
6694 switch (tv->v_type)
6695 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006696 case VAR_FUNC:
6697 *tofree = string_quote(tv->vval.v_string, TRUE);
6698 return *tofree;
6699 case VAR_STRING:
6700 *tofree = string_quote(tv->vval.v_string, FALSE);
6701 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006702 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006704 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006705 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006706 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006707 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006708 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006709 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006710}
6711
6712/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006713 * Return string "str" in ' quotes, doubling ' characters.
6714 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006715 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006716 */
6717 static char_u *
6718string_quote(str, function)
6719 char_u *str;
6720 int function;
6721{
Bram Moolenaar33570922005-01-25 22:26:29 +00006722 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006723 char_u *p, *r, *s;
6724
Bram Moolenaar33570922005-01-25 22:26:29 +00006725 len = (function ? 13 : 3);
6726 if (str != NULL)
6727 {
6728 len += STRLEN(str);
6729 for (p = str; *p != NUL; mb_ptr_adv(p))
6730 if (*p == '\'')
6731 ++len;
6732 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006733 s = r = alloc(len);
6734 if (r != NULL)
6735 {
6736 if (function)
6737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006738 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006739 r += 10;
6740 }
6741 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006742 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006743 if (str != NULL)
6744 for (p = str; *p != NUL; )
6745 {
6746 if (*p == '\'')
6747 *r++ = '\'';
6748 MB_COPY_CHAR(p, r);
6749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006750 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006751 if (function)
6752 *r++ = ')';
6753 *r++ = NUL;
6754 }
6755 return s;
6756}
6757
6758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 * Get the value of an environment variable.
6760 * "arg" is pointing to the '$'. It is advanced to after the name.
6761 * If the environment variable was not set, silently assume it is empty.
6762 * Always return OK.
6763 */
6764 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006765get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 int evaluate;
6769{
6770 char_u *string = NULL;
6771 int len;
6772 int cc;
6773 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006774 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
6776 ++*arg;
6777 name = *arg;
6778 len = get_env_len(arg);
6779 if (evaluate)
6780 {
6781 if (len != 0)
6782 {
6783 cc = name[len];
6784 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006785 /* first try vim_getenv(), fast for normal environment vars */
6786 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006788 {
6789 if (!mustfree)
6790 string = vim_strsave(string);
6791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 else
6793 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006794 if (mustfree)
6795 vim_free(string);
6796
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 /* next try expanding things like $VIM and ${HOME} */
6798 string = expand_env_save(name - 1);
6799 if (string != NULL && *string == '$')
6800 {
6801 vim_free(string);
6802 string = NULL;
6803 }
6804 }
6805 name[len] = cc;
6806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006807 rettv->v_type = VAR_STRING;
6808 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
6810
6811 return OK;
6812}
6813
6814/*
6815 * Array with names and number of arguments of all internal functions
6816 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6817 */
6818static struct fst
6819{
6820 char *f_name; /* function name */
6821 char f_min_argc; /* minimal number of arguments */
6822 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006823 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006824 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825} functions[] =
6826{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006827 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 {"append", 2, 2, f_append},
6829 {"argc", 0, 0, f_argc},
6830 {"argidx", 0, 0, f_argidx},
6831 {"argv", 1, 1, f_argv},
6832 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006833 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {"bufexists", 1, 1, f_bufexists},
6835 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6836 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6837 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6838 {"buflisted", 1, 1, f_buflisted},
6839 {"bufloaded", 1, 1, f_bufloaded},
6840 {"bufname", 1, 1, f_bufname},
6841 {"bufnr", 1, 1, f_bufnr},
6842 {"bufwinnr", 1, 1, f_bufwinnr},
6843 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006844 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006845 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 {"char2nr", 1, 1, f_char2nr},
6847 {"cindent", 1, 1, f_cindent},
6848 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006849#if defined(FEAT_INS_EXPAND)
6850 {"complete_add", 1, 1, f_complete_add},
6851 {"complete_check", 0, 0, f_complete_check},
6852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006854 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006855 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 {"cscope_connection",0,3, f_cscope_connection},
6857 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006858 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {"delete", 1, 1, f_delete},
6860 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006861 {"diff_filler", 1, 1, f_diff_filler},
6862 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006863 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006865 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {"eventhandler", 0, 0, f_eventhandler},
6867 {"executable", 1, 1, f_executable},
6868 {"exists", 1, 1, f_exists},
6869 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006870 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6872 {"filereadable", 1, 1, f_filereadable},
6873 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006874 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006875 {"finddir", 1, 3, f_finddir},
6876 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {"fnamemodify", 2, 2, f_fnamemodify},
6878 {"foldclosed", 1, 1, f_foldclosed},
6879 {"foldclosedend", 1, 1, f_foldclosedend},
6880 {"foldlevel", 1, 1, f_foldlevel},
6881 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006882 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006884 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006886 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006887 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 {"getbufvar", 2, 2, f_getbufvar},
6889 {"getchar", 0, 1, f_getchar},
6890 {"getcharmod", 0, 0, f_getcharmod},
6891 {"getcmdline", 0, 0, f_getcmdline},
6892 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006893 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006895 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006896 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 {"getfsize", 1, 1, f_getfsize},
6898 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006899 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006900 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006901 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006902 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006903 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {"getregtype", 0, 1, f_getregtype},
6905 {"getwinposx", 0, 0, f_getwinposx},
6906 {"getwinposy", 0, 0, f_getwinposy},
6907 {"getwinvar", 2, 2, f_getwinvar},
6908 {"glob", 1, 1, f_glob},
6909 {"globpath", 2, 2, f_globpath},
6910 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006911 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 {"hasmapto", 1, 2, f_hasmapto},
6913 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6914 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6915 {"histadd", 2, 2, f_histadd},
6916 {"histdel", 1, 2, f_histdel},
6917 {"histget", 1, 2, f_histget},
6918 {"histnr", 1, 1, f_histnr},
6919 {"hlID", 1, 1, f_hlID},
6920 {"hlexists", 1, 1, f_hlexists},
6921 {"hostname", 0, 0, f_hostname},
6922 {"iconv", 3, 3, f_iconv},
6923 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006924 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006925 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006927 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {"inputrestore", 0, 0, f_inputrestore},
6929 {"inputsave", 0, 0, f_inputsave},
6930 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006931 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006933 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006934 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006935 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006936 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006938 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 {"libcall", 3, 3, f_libcall},
6940 {"libcallnr", 3, 3, f_libcallnr},
6941 {"line", 1, 1, f_line},
6942 {"line2byte", 1, 1, f_line2byte},
6943 {"lispindent", 1, 1, f_lispindent},
6944 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006945 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 {"maparg", 1, 2, f_maparg},
6947 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006948 {"match", 2, 4, f_match},
6949 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006950 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006951 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006952 {"max", 1, 1, f_max},
6953 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006954#ifdef vim_mkdir
6955 {"mkdir", 1, 3, f_mkdir},
6956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {"mode", 0, 0, f_mode},
6958 {"nextnonblank", 1, 1, f_nextnonblank},
6959 {"nr2char", 1, 1, f_nr2char},
6960 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006961 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006962 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006963 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006964 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {"remote_expr", 2, 3, f_remote_expr},
6966 {"remote_foreground", 1, 1, f_remote_foreground},
6967 {"remote_peek", 1, 2, f_remote_peek},
6968 {"remote_read", 1, 1, f_remote_read},
6969 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006970 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006972 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006974 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006975 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006976 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006977 {"searchpair", 3, 6, f_searchpair},
6978 {"searchpairpos", 3, 6, f_searchpairpos},
6979 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 {"server2client", 2, 2, f_server2client},
6981 {"serverlist", 0, 0, f_serverlist},
6982 {"setbufvar", 3, 3, f_setbufvar},
6983 {"setcmdpos", 1, 1, f_setcmdpos},
6984 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006985 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006986 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 {"setreg", 2, 3, f_setreg},
6988 {"setwinvar", 3, 3, f_setwinvar},
6989 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006990 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006991 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006992 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006993 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006994 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995#ifdef HAVE_STRFTIME
6996 {"strftime", 1, 2, f_strftime},
6997#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006998 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006999 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {"strlen", 1, 1, f_strlen},
7001 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007002 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {"strtrans", 1, 1, f_strtrans},
7004 {"submatch", 1, 1, f_submatch},
7005 {"substitute", 4, 4, f_substitute},
7006 {"synID", 3, 3, f_synID},
7007 {"synIDattr", 2, 3, f_synIDattr},
7008 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007009 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007010 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007011 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007012 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007013 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007014 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007016 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 {"tolower", 1, 1, f_tolower},
7018 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007019 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007021 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 {"virtcol", 1, 1, f_virtcol},
7023 {"visualmode", 0, 1, f_visualmode},
7024 {"winbufnr", 1, 1, f_winbufnr},
7025 {"wincol", 0, 0, f_wincol},
7026 {"winheight", 1, 1, f_winheight},
7027 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007028 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {"winrestcmd", 0, 0, f_winrestcmd},
7030 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007031 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032};
7033
7034#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7035
7036/*
7037 * Function given to ExpandGeneric() to obtain the list of internal
7038 * or user defined function names.
7039 */
7040 char_u *
7041get_function_name(xp, idx)
7042 expand_T *xp;
7043 int idx;
7044{
7045 static int intidx = -1;
7046 char_u *name;
7047
7048 if (idx == 0)
7049 intidx = -1;
7050 if (intidx < 0)
7051 {
7052 name = get_user_func_name(xp, idx);
7053 if (name != NULL)
7054 return name;
7055 }
7056 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7057 {
7058 STRCPY(IObuff, functions[intidx].f_name);
7059 STRCAT(IObuff, "(");
7060 if (functions[intidx].f_max_argc == 0)
7061 STRCAT(IObuff, ")");
7062 return IObuff;
7063 }
7064
7065 return NULL;
7066}
7067
7068/*
7069 * Function given to ExpandGeneric() to obtain the list of internal or
7070 * user defined variable or function names.
7071 */
7072/*ARGSUSED*/
7073 char_u *
7074get_expr_name(xp, idx)
7075 expand_T *xp;
7076 int idx;
7077{
7078 static int intidx = -1;
7079 char_u *name;
7080
7081 if (idx == 0)
7082 intidx = -1;
7083 if (intidx < 0)
7084 {
7085 name = get_function_name(xp, idx);
7086 if (name != NULL)
7087 return name;
7088 }
7089 return get_user_var_name(xp, ++intidx);
7090}
7091
7092#endif /* FEAT_CMDL_COMPL */
7093
7094/*
7095 * Find internal function in table above.
7096 * Return index, or -1 if not found
7097 */
7098 static int
7099find_internal_func(name)
7100 char_u *name; /* name of the function */
7101{
7102 int first = 0;
7103 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7104 int cmp;
7105 int x;
7106
7107 /*
7108 * Find the function name in the table. Binary search.
7109 */
7110 while (first <= last)
7111 {
7112 x = first + ((unsigned)(last - first) >> 1);
7113 cmp = STRCMP(name, functions[x].f_name);
7114 if (cmp < 0)
7115 last = x - 1;
7116 else if (cmp > 0)
7117 first = x + 1;
7118 else
7119 return x;
7120 }
7121 return -1;
7122}
7123
7124/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007125 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7126 * name it contains, otherwise return "name".
7127 */
7128 static char_u *
7129deref_func_name(name, lenp)
7130 char_u *name;
7131 int *lenp;
7132{
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007134 int cc;
7135
7136 cc = name[*lenp];
7137 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007138 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007139 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007140 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007141 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007142 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007143 {
7144 *lenp = 0;
7145 return (char_u *)""; /* just in case */
7146 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007147 *lenp = STRLEN(v->di_tv.vval.v_string);
7148 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007149 }
7150
7151 return name;
7152}
7153
7154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 * Allocate a variable for the result of a function.
7156 * Return OK or FAIL.
7157 */
7158 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007159get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7160 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 char_u *name; /* name of the function */
7162 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 char_u **arg; /* argument, pointing to the '(' */
7165 linenr_T firstline; /* first line of range */
7166 linenr_T lastline; /* last line of range */
7167 int *doesrange; /* return: function handled range */
7168 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007169 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 char_u *argp;
7172 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 int argcount = 0; /* number of arguments found */
7175
7176 /*
7177 * Get the arguments.
7178 */
7179 argp = *arg;
7180 while (argcount < MAX_FUNC_ARGS)
7181 {
7182 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7183 if (*argp == ')' || *argp == ',' || *argp == NUL)
7184 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7186 {
7187 ret = FAIL;
7188 break;
7189 }
7190 ++argcount;
7191 if (*argp != ',')
7192 break;
7193 }
7194 if (*argp == ')')
7195 ++argp;
7196 else
7197 ret = FAIL;
7198
7199 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007200 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007201 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 {
7204 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007205 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007206 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007207 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209
7210 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007211 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
7213 *arg = skipwhite(argp);
7214 return ret;
7215}
7216
7217
7218/*
7219 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007220 * Return OK when the function can't be called, FAIL otherwise.
7221 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 */
7223 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007224call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007225 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 char_u *name; /* name of the function */
7227 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007228 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007230 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 linenr_T firstline; /* first line of range */
7232 linenr_T lastline; /* last line of range */
7233 int *doesrange; /* return: function handled range */
7234 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007235 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236{
7237 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238#define ERROR_UNKNOWN 0
7239#define ERROR_TOOMANY 1
7240#define ERROR_TOOFEW 2
7241#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007242#define ERROR_DICT 4
7243#define ERROR_NONE 5
7244#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 int error = ERROR_NONE;
7246 int i;
7247 int llen;
7248 ufunc_T *fp;
7249 int cc;
7250#define FLEN_FIXED 40
7251 char_u fname_buf[FLEN_FIXED + 1];
7252 char_u *fname;
7253
7254 /*
7255 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7256 * Change <SNR>123_name() to K_SNR 123_name().
7257 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7258 */
7259 cc = name[len];
7260 name[len] = NUL;
7261 llen = eval_fname_script(name);
7262 if (llen > 0)
7263 {
7264 fname_buf[0] = K_SPECIAL;
7265 fname_buf[1] = KS_EXTRA;
7266 fname_buf[2] = (int)KE_SNR;
7267 i = 3;
7268 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7269 {
7270 if (current_SID <= 0)
7271 error = ERROR_SCRIPT;
7272 else
7273 {
7274 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7275 i = (int)STRLEN(fname_buf);
7276 }
7277 }
7278 if (i + STRLEN(name + llen) < FLEN_FIXED)
7279 {
7280 STRCPY(fname_buf + i, name + llen);
7281 fname = fname_buf;
7282 }
7283 else
7284 {
7285 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7286 if (fname == NULL)
7287 error = ERROR_OTHER;
7288 else
7289 {
7290 mch_memmove(fname, fname_buf, (size_t)i);
7291 STRCPY(fname + i, name + llen);
7292 }
7293 }
7294 }
7295 else
7296 fname = name;
7297
7298 *doesrange = FALSE;
7299
7300
7301 /* execute the function if no errors detected and executing */
7302 if (evaluate && error == ERROR_NONE)
7303 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007304 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 error = ERROR_UNKNOWN;
7306
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007307 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 {
7309 /*
7310 * User defined function.
7311 */
7312 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007313
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007315 /* Trigger FuncUndefined event, may load the function. */
7316 if (fp == NULL
7317 && apply_autocmds(EVENT_FUNCUNDEFINED,
7318 fname, fname, TRUE, NULL)
7319 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007321 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 fp = find_func(fname);
7323 }
7324#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007325 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007326 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007327 {
7328 /* loaded a package, search for the function again */
7329 fp = find_func(fname);
7330 }
7331
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 if (fp != NULL)
7333 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007334 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007336 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007338 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007340 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007341 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 else
7343 {
7344 /*
7345 * Call the user function.
7346 * Save and restore search patterns, script variables and
7347 * redo buffer.
7348 */
7349 save_search_patterns();
7350 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007351 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007352 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007353 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007354 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7355 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7356 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007357 /* Function was unreferenced while being used, free it
7358 * now. */
7359 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 restoreRedobuff();
7361 restore_search_patterns();
7362 error = ERROR_NONE;
7363 }
7364 }
7365 }
7366 else
7367 {
7368 /*
7369 * Find the function name in the table, call its implementation.
7370 */
7371 i = find_internal_func(fname);
7372 if (i >= 0)
7373 {
7374 if (argcount < functions[i].f_min_argc)
7375 error = ERROR_TOOFEW;
7376 else if (argcount > functions[i].f_max_argc)
7377 error = ERROR_TOOMANY;
7378 else
7379 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007380 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007381 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 error = ERROR_NONE;
7383 }
7384 }
7385 }
7386 /*
7387 * The function call (or "FuncUndefined" autocommand sequence) might
7388 * have been aborted by an error, an interrupt, or an explicitly thrown
7389 * exception that has not been caught so far. This situation can be
7390 * tested for by calling aborting(). For an error in an internal
7391 * function or for the "E132" error in call_user_func(), however, the
7392 * throw point at which the "force_abort" flag (temporarily reset by
7393 * emsg()) is normally updated has not been reached yet. We need to
7394 * update that flag first to make aborting() reliable.
7395 */
7396 update_force_abort();
7397 }
7398 if (error == ERROR_NONE)
7399 ret = OK;
7400
7401 /*
7402 * Report an error unless the argument evaluation or function call has been
7403 * cancelled due to an aborting error, an interrupt, or an exception.
7404 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405 if (!aborting())
7406 {
7407 switch (error)
7408 {
7409 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007410 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411 break;
7412 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007413 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414 break;
7415 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007416 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417 name);
7418 break;
7419 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007420 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421 name);
7422 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007424 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 name);
7426 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427 }
7428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429
7430 name[len] = cc;
7431 if (fname != name && fname != fname_buf)
7432 vim_free(fname);
7433
7434 return ret;
7435}
7436
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007437/*
7438 * Give an error message with a function name. Handle <SNR> things.
7439 */
7440 static void
7441emsg_funcname(msg, name)
7442 char *msg;
7443 char_u *name;
7444{
7445 char_u *p;
7446
7447 if (*name == K_SPECIAL)
7448 p = concat_str((char_u *)"<SNR>", name + 3);
7449 else
7450 p = name;
7451 EMSG2(_(msg), p);
7452 if (p != name)
7453 vim_free(p);
7454}
7455
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456/*********************************************
7457 * Implementation of the built-in functions
7458 */
7459
7460/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007461 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 */
7463 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007464f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007465 typval_T *argvars;
7466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467{
Bram Moolenaar33570922005-01-25 22:26:29 +00007468 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007470 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007471 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007473 if ((l = argvars[0].vval.v_list) != NULL
7474 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7475 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007476 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007477 }
7478 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007479 EMSG(_(e_listreq));
7480}
7481
7482/*
7483 * "append(lnum, string/list)" function
7484 */
7485 static void
7486f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007487 typval_T *argvars;
7488 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007489{
7490 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007491 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 list_T *l = NULL;
7493 listitem_T *li = NULL;
7494 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007495 long added = 0;
7496
Bram Moolenaar0d660222005-01-07 21:51:51 +00007497 lnum = get_tv_lnum(argvars);
7498 if (lnum >= 0
7499 && lnum <= curbuf->b_ml.ml_line_count
7500 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007501 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007502 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007503 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007504 l = argvars[1].vval.v_list;
7505 if (l == NULL)
7506 return;
7507 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007508 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007509 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007510 for (;;)
7511 {
7512 if (l == NULL)
7513 tv = &argvars[1]; /* append a string */
7514 else if (li == NULL)
7515 break; /* end of list */
7516 else
7517 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007518 line = get_tv_string_chk(tv);
7519 if (line == NULL) /* type error */
7520 {
7521 rettv->vval.v_number = 1; /* Failed */
7522 break;
7523 }
7524 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007525 ++added;
7526 if (l == NULL)
7527 break;
7528 li = li->li_next;
7529 }
7530
7531 appended_lines_mark(lnum, added);
7532 if (curwin->w_cursor.lnum > lnum)
7533 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007535 else
7536 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537}
7538
7539/*
7540 * "argc()" function
7541 */
7542/* ARGSUSED */
7543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007544f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007545 typval_T *argvars;
7546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007548 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549}
7550
7551/*
7552 * "argidx()" function
7553 */
7554/* ARGSUSED */
7555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007556f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007557 typval_T *argvars;
7558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007560 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561}
7562
7563/*
7564 * "argv(nr)" function
7565 */
7566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007567f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 typval_T *argvars;
7569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570{
7571 int idx;
7572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007573 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007575 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007577 rettv->vval.v_string = NULL;
7578 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579}
7580
7581/*
7582 * "browse(save, title, initdir, default)" function
7583 */
7584/* ARGSUSED */
7585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007586f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 typval_T *argvars;
7588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589{
7590#ifdef FEAT_BROWSE
7591 int save;
7592 char_u *title;
7593 char_u *initdir;
7594 char_u *defname;
7595 char_u buf[NUMBUFLEN];
7596 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007597 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007599 save = get_tv_number_chk(&argvars[0], &error);
7600 title = get_tv_string_chk(&argvars[1]);
7601 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7602 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007604 if (error || title == NULL || initdir == NULL || defname == NULL)
7605 rettv->vval.v_string = NULL;
7606 else
7607 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007608 do_browse(save ? BROWSE_SAVE : 0,
7609 title, defname, NULL, initdir, NULL, curbuf);
7610#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007611 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007612#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007613 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007614}
7615
7616/*
7617 * "browsedir(title, initdir)" function
7618 */
7619/* ARGSUSED */
7620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007621f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007622 typval_T *argvars;
7623 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007624{
7625#ifdef FEAT_BROWSE
7626 char_u *title;
7627 char_u *initdir;
7628 char_u buf[NUMBUFLEN];
7629
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007630 title = get_tv_string_chk(&argvars[0]);
7631 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007632
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007633 if (title == NULL || initdir == NULL)
7634 rettv->vval.v_string = NULL;
7635 else
7636 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007637 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007639 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642}
7643
Bram Moolenaar33570922005-01-25 22:26:29 +00007644static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007645
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646/*
7647 * Find a buffer by number or exact name.
7648 */
7649 static buf_T *
7650find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007651 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652{
7653 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007655 if (avar->v_type == VAR_NUMBER)
7656 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007657 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007659 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007660 if (buf == NULL)
7661 {
7662 /* No full path name match, try a match with a URL or a "nofile"
7663 * buffer, these don't use the full path. */
7664 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7665 if (buf->b_fname != NULL
7666 && (path_with_url(buf->b_fname)
7667#ifdef FEAT_QUICKFIX
7668 || bt_nofile(buf)
7669#endif
7670 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007671 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007672 break;
7673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 }
7675 return buf;
7676}
7677
7678/*
7679 * "bufexists(expr)" function
7680 */
7681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007682f_bufexists(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687}
7688
7689/*
7690 * "buflisted(expr)" function
7691 */
7692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007693f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 typval_T *argvars;
7695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696{
7697 buf_T *buf;
7698
7699 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007700 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701}
7702
7703/*
7704 * "bufloaded(expr)" function
7705 */
7706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007707f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007708 typval_T *argvars;
7709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710{
7711 buf_T *buf;
7712
7713 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007714 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715}
7716
Bram Moolenaar33570922005-01-25 22:26:29 +00007717static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007718
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719/*
7720 * Get buffer by number or pattern.
7721 */
7722 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007723get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007724 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007726 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 int save_magic;
7728 char_u *save_cpo;
7729 buf_T *buf;
7730
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007731 if (tv->v_type == VAR_NUMBER)
7732 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007733 if (tv->v_type != VAR_STRING)
7734 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 if (name == NULL || *name == NUL)
7736 return curbuf;
7737 if (name[0] == '$' && name[1] == NUL)
7738 return lastbuf;
7739
7740 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7741 save_magic = p_magic;
7742 p_magic = TRUE;
7743 save_cpo = p_cpo;
7744 p_cpo = (char_u *)"";
7745
7746 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7747 TRUE, FALSE));
7748
7749 p_magic = save_magic;
7750 p_cpo = save_cpo;
7751
7752 /* If not found, try expanding the name, like done for bufexists(). */
7753 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007754 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755
7756 return buf;
7757}
7758
7759/*
7760 * "bufname(expr)" function
7761 */
7762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007763f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 typval_T *argvars;
7765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
7767 buf_T *buf;
7768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007769 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007771 buf = get_buf_tv(&argvars[0]);
7772 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 --emsg_off;
7778}
7779
7780/*
7781 * "bufnr(expr)" function
7782 */
7783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007784f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007785 typval_T *argvars;
7786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787{
7788 buf_T *buf;
7789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007790 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007792 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007796 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 --emsg_off;
7798}
7799
7800/*
7801 * "bufwinnr(nr)" function
7802 */
7803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007805 typval_T *argvars;
7806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
7808#ifdef FEAT_WINDOWS
7809 win_T *wp;
7810 int winnr = 0;
7811#endif
7812 buf_T *buf;
7813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007814 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007816 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817#ifdef FEAT_WINDOWS
7818 for (wp = firstwin; wp; wp = wp->w_next)
7819 {
7820 ++winnr;
7821 if (wp->w_buffer == buf)
7822 break;
7823 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007824 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007826 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827#endif
7828 --emsg_off;
7829}
7830
7831/*
7832 * "byte2line(byte)" function
7833 */
7834/*ARGSUSED*/
7835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007836f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007837 typval_T *argvars;
7838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839{
7840#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842#else
7843 long boff = 0;
7844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007845 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007847 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007849 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 (linenr_T)0, &boff);
7851#endif
7852}
7853
7854/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007855 * "byteidx()" function
7856 */
7857/*ARGSUSED*/
7858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007859f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 typval_T *argvars;
7861 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007862{
7863#ifdef FEAT_MBYTE
7864 char_u *t;
7865#endif
7866 char_u *str;
7867 long idx;
7868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007869 str = get_tv_string_chk(&argvars[0]);
7870 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007872 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007873 return;
7874
7875#ifdef FEAT_MBYTE
7876 t = str;
7877 for ( ; idx > 0; idx--)
7878 {
7879 if (*t == NUL) /* EOL reached */
7880 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007881 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007882 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007883 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007884#else
7885 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007886 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007887#endif
7888}
7889
7890/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007891 * "call(func, arglist)" function
7892 */
7893 static void
7894f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 typval_T *argvars;
7896 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007897{
7898 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007899 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007900 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007901 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007902 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007904
7905 rettv->vval.v_number = 0;
7906 if (argvars[1].v_type != VAR_LIST)
7907 {
7908 EMSG(_(e_listreq));
7909 return;
7910 }
7911 if (argvars[1].vval.v_list == NULL)
7912 return;
7913
7914 if (argvars[0].v_type == VAR_FUNC)
7915 func = argvars[0].vval.v_string;
7916 else
7917 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007918 if (*func == NUL)
7919 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007920
Bram Moolenaare9a41262005-01-15 22:18:47 +00007921 if (argvars[2].v_type != VAR_UNKNOWN)
7922 {
7923 if (argvars[2].v_type != VAR_DICT)
7924 {
7925 EMSG(_(e_dictreq));
7926 return;
7927 }
7928 selfdict = argvars[2].vval.v_dict;
7929 }
7930
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007931 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7932 item = item->li_next)
7933 {
7934 if (argc == MAX_FUNC_ARGS)
7935 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007936 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007937 break;
7938 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007939 /* Make a copy of each argument. This is needed to be able to set
7940 * v_lock to VAR_FIXED in the copy without changing the original list.
7941 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007942 copy_tv(&item->li_tv, &argv[argc++]);
7943 }
7944
7945 if (item == NULL)
7946 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007947 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7948 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007949
7950 /* Free the arguments. */
7951 while (argc > 0)
7952 clear_tv(&argv[--argc]);
7953}
7954
7955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 * "char2nr(string)" function
7957 */
7958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007959f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007960 typval_T *argvars;
7961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962{
7963#ifdef FEAT_MBYTE
7964 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007965 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 else
7967#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969}
7970
7971/*
7972 * "cindent(lnum)" function
7973 */
7974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007975f_cindent(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#ifdef FEAT_CINDENT
7980 pos_T pos;
7981 linenr_T lnum;
7982
7983 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7986 {
7987 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007988 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 curwin->w_cursor = pos;
7990 }
7991 else
7992#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994}
7995
7996/*
7997 * "col(string)" function
7998 */
7999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008000f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008001 typval_T *argvars;
8002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003{
8004 colnr_T col = 0;
8005 pos_T *fp;
8006
8007 fp = var2fpos(&argvars[0], FALSE);
8008 if (fp != NULL)
8009 {
8010 if (fp->col == MAXCOL)
8011 {
8012 /* '> can be MAXCOL, get the length of the line then */
8013 if (fp->lnum <= curbuf->b_ml.ml_line_count)
8014 col = STRLEN(ml_get(fp->lnum)) + 1;
8015 else
8016 col = MAXCOL;
8017 }
8018 else
8019 {
8020 col = fp->col + 1;
8021#ifdef FEAT_VIRTUALEDIT
8022 /* col(".") when the cursor is on the NUL at the end of the line
8023 * because of "coladd" can be seen as an extra column. */
8024 if (virtual_active() && fp == &curwin->w_cursor)
8025 {
8026 char_u *p = ml_get_cursor();
8027
8028 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8029 curwin->w_virtcol - curwin->w_cursor.coladd))
8030 {
8031# ifdef FEAT_MBYTE
8032 int l;
8033
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008034 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 col += l;
8036# else
8037 if (*p != NUL && p[1] == NUL)
8038 ++col;
8039# endif
8040 }
8041 }
8042#endif
8043 }
8044 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008045 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046}
8047
Bram Moolenaar572cb562005-08-05 21:35:02 +00008048#if defined(FEAT_INS_EXPAND)
8049/*
8050 * "complete_add()" function
8051 */
8052/*ARGSUSED*/
8053 static void
8054f_complete_add(argvars, rettv)
8055 typval_T *argvars;
8056 typval_T *rettv;
8057{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008058 char_u *word;
8059 char_u *extra = NULL;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008060 int icase = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008061
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008062 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8063 {
8064 word = get_dict_string(argvars[0].vval.v_dict,
8065 (char_u *)"word", FALSE);
8066 extra = get_dict_string(argvars[0].vval.v_dict,
8067 (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008068 icase = get_dict_number(argvars[0].vval.v_dict, (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008069 }
8070 else
8071 word = get_tv_string_chk(&argvars[0]);
8072 if (word != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008073 rettv->vval.v_number = ins_compl_add(word, -1, icase,
8074 NULL, extra, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008075}
8076
8077/*
8078 * "complete_check()" function
8079 */
8080/*ARGSUSED*/
8081 static void
8082f_complete_check(argvars, rettv)
8083 typval_T *argvars;
8084 typval_T *rettv;
8085{
8086 int saved = RedrawingDisabled;
8087
8088 RedrawingDisabled = 0;
8089 ins_compl_check_keys(0);
8090 rettv->vval.v_number = compl_interrupted;
8091 RedrawingDisabled = saved;
8092}
8093#endif
8094
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095/*
8096 * "confirm(message, buttons[, default [, type]])" function
8097 */
8098/*ARGSUSED*/
8099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008100f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008101 typval_T *argvars;
8102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103{
8104#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8105 char_u *message;
8106 char_u *buttons = NULL;
8107 char_u buf[NUMBUFLEN];
8108 char_u buf2[NUMBUFLEN];
8109 int def = 1;
8110 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008111 char_u *typestr;
8112 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008114 message = get_tv_string_chk(&argvars[0]);
8115 if (message == NULL)
8116 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008117 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008119 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8120 if (buttons == NULL)
8121 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008122 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008124 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008125 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008127 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8128 if (typestr == NULL)
8129 error = TRUE;
8130 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008132 switch (TOUPPER_ASC(*typestr))
8133 {
8134 case 'E': type = VIM_ERROR; break;
8135 case 'Q': type = VIM_QUESTION; break;
8136 case 'I': type = VIM_INFO; break;
8137 case 'W': type = VIM_WARNING; break;
8138 case 'G': type = VIM_GENERIC; break;
8139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 }
8141 }
8142 }
8143 }
8144
8145 if (buttons == NULL || *buttons == NUL)
8146 buttons = (char_u *)_("&Ok");
8147
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008148 if (error)
8149 rettv->vval.v_number = 0;
8150 else
8151 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 def, NULL);
8153#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008154 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155#endif
8156}
8157
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008158/*
8159 * "copy()" function
8160 */
8161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008162f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008163 typval_T *argvars;
8164 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008165{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008166 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008167}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168
8169/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008170 * "count()" function
8171 */
8172 static void
8173f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008174 typval_T *argvars;
8175 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008176{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008177 long n = 0;
8178 int ic = FALSE;
8179
Bram Moolenaare9a41262005-01-15 22:18:47 +00008180 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008181 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008182 listitem_T *li;
8183 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008184 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008185
Bram Moolenaare9a41262005-01-15 22:18:47 +00008186 if ((l = argvars[0].vval.v_list) != NULL)
8187 {
8188 li = l->lv_first;
8189 if (argvars[2].v_type != VAR_UNKNOWN)
8190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008191 int error = FALSE;
8192
8193 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008194 if (argvars[3].v_type != VAR_UNKNOWN)
8195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008196 idx = get_tv_number_chk(&argvars[3], &error);
8197 if (!error)
8198 {
8199 li = list_find(l, idx);
8200 if (li == NULL)
8201 EMSGN(_(e_listidx), idx);
8202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008203 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008204 if (error)
8205 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008206 }
8207
8208 for ( ; li != NULL; li = li->li_next)
8209 if (tv_equal(&li->li_tv, &argvars[1], ic))
8210 ++n;
8211 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008212 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008213 else if (argvars[0].v_type == VAR_DICT)
8214 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008215 int todo;
8216 dict_T *d;
8217 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008218
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008219 if ((d = argvars[0].vval.v_dict) != NULL)
8220 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008221 int error = FALSE;
8222
Bram Moolenaare9a41262005-01-15 22:18:47 +00008223 if (argvars[2].v_type != VAR_UNKNOWN)
8224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008225 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008226 if (argvars[3].v_type != VAR_UNKNOWN)
8227 EMSG(_(e_invarg));
8228 }
8229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008230 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008231 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008232 {
8233 if (!HASHITEM_EMPTY(hi))
8234 {
8235 --todo;
8236 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8237 ++n;
8238 }
8239 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008240 }
8241 }
8242 else
8243 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008244 rettv->vval.v_number = n;
8245}
8246
8247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8249 *
8250 * Checks the existence of a cscope connection.
8251 */
8252/*ARGSUSED*/
8253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008254f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008255 typval_T *argvars;
8256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257{
8258#ifdef FEAT_CSCOPE
8259 int num = 0;
8260 char_u *dbpath = NULL;
8261 char_u *prepend = NULL;
8262 char_u buf[NUMBUFLEN];
8263
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008264 if (argvars[0].v_type != VAR_UNKNOWN
8265 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008267 num = (int)get_tv_number(&argvars[0]);
8268 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008269 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008270 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 }
8272
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008273 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008275 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276#endif
8277}
8278
8279/*
8280 * "cursor(lnum, col)" function
8281 *
8282 * Moves the cursor to the specified line and column
8283 */
8284/*ARGSUSED*/
8285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008286f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008287 typval_T *argvars;
8288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289{
8290 long line, col;
8291
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008292 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008293 col = get_tv_number_chk(&argvars[1], NULL);
8294 if (line < 0 || col < 0)
8295 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 if (line > 0)
8297 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 if (col > 0)
8299 curwin->w_cursor.col = col - 1;
8300#ifdef FEAT_VIRTUALEDIT
8301 curwin->w_cursor.coladd = 0;
8302#endif
8303
8304 /* Make sure the cursor is in a valid position. */
8305 check_cursor();
8306#ifdef FEAT_MBYTE
8307 /* Correct cursor for multi-byte character. */
8308 if (has_mbyte)
8309 mb_adjust_cursor();
8310#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008311
8312 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313}
8314
8315/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008316 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 */
8318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008319f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008320 typval_T *argvars;
8321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008323 int noref = 0;
8324
8325 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008326 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008327 if (noref < 0 || noref > 1)
8328 EMSG(_(e_invarg));
8329 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008330 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331}
8332
8333/*
8334 * "delete()" function
8335 */
8336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008337f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008338 typval_T *argvars;
8339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340{
8341 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008342 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008344 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345}
8346
8347/*
8348 * "did_filetype()" function
8349 */
8350/*ARGSUSED*/
8351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008352f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008353 typval_T *argvars;
8354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355{
8356#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008357 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008359 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360#endif
8361}
8362
8363/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008364 * "diff_filler()" function
8365 */
8366/*ARGSUSED*/
8367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008368f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 typval_T *argvars;
8370 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008371{
8372#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008373 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008374#endif
8375}
8376
8377/*
8378 * "diff_hlID()" function
8379 */
8380/*ARGSUSED*/
8381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008382f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008383 typval_T *argvars;
8384 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008385{
8386#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008387 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008388 static linenr_T prev_lnum = 0;
8389 static int changedtick = 0;
8390 static int fnum = 0;
8391 static int change_start = 0;
8392 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008393 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008394 int filler_lines;
8395 int col;
8396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008397 if (lnum < 0) /* ignore type error in {lnum} arg */
8398 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008399 if (lnum != prev_lnum
8400 || changedtick != curbuf->b_changedtick
8401 || fnum != curbuf->b_fnum)
8402 {
8403 /* New line, buffer, change: need to get the values. */
8404 filler_lines = diff_check(curwin, lnum);
8405 if (filler_lines < 0)
8406 {
8407 if (filler_lines == -1)
8408 {
8409 change_start = MAXCOL;
8410 change_end = -1;
8411 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8412 hlID = HLF_ADD; /* added line */
8413 else
8414 hlID = HLF_CHD; /* changed line */
8415 }
8416 else
8417 hlID = HLF_ADD; /* added line */
8418 }
8419 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008420 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008421 prev_lnum = lnum;
8422 changedtick = curbuf->b_changedtick;
8423 fnum = curbuf->b_fnum;
8424 }
8425
8426 if (hlID == HLF_CHD || hlID == HLF_TXD)
8427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008428 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008429 if (col >= change_start && col <= change_end)
8430 hlID = HLF_TXD; /* changed text */
8431 else
8432 hlID = HLF_CHD; /* changed line */
8433 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008434 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008435#endif
8436}
8437
8438/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008439 * "empty({expr})" function
8440 */
8441 static void
8442f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008443 typval_T *argvars;
8444 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008445{
8446 int n;
8447
8448 switch (argvars[0].v_type)
8449 {
8450 case VAR_STRING:
8451 case VAR_FUNC:
8452 n = argvars[0].vval.v_string == NULL
8453 || *argvars[0].vval.v_string == NUL;
8454 break;
8455 case VAR_NUMBER:
8456 n = argvars[0].vval.v_number == 0;
8457 break;
8458 case VAR_LIST:
8459 n = argvars[0].vval.v_list == NULL
8460 || argvars[0].vval.v_list->lv_first == NULL;
8461 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008462 case VAR_DICT:
8463 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008464 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008465 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008466 default:
8467 EMSG2(_(e_intern2), "f_empty()");
8468 n = 0;
8469 }
8470
8471 rettv->vval.v_number = n;
8472}
8473
8474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 * "escape({string}, {chars})" function
8476 */
8477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008478f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008479 typval_T *argvars;
8480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481{
8482 char_u buf[NUMBUFLEN];
8483
Bram Moolenaar758711c2005-02-02 23:11:38 +00008484 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8485 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008486 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487}
8488
8489/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008490 * "eval()" function
8491 */
8492/*ARGSUSED*/
8493 static void
8494f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008495 typval_T *argvars;
8496 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008497{
8498 char_u *s;
8499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008500 s = get_tv_string_chk(&argvars[0]);
8501 if (s != NULL)
8502 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008503
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008504 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8505 {
8506 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008507 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008508 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008509 else if (*s != NUL)
8510 EMSG(_(e_trailing));
8511}
8512
8513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 * "eventhandler()" function
8515 */
8516/*ARGSUSED*/
8517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008518f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008519 typval_T *argvars;
8520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008522 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523}
8524
8525/*
8526 * "executable()" function
8527 */
8528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008529f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008530 typval_T *argvars;
8531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008533 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534}
8535
8536/*
8537 * "exists()" function
8538 */
8539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008540f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008541 typval_T *argvars;
8542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
8544 char_u *p;
8545 char_u *name;
8546 int n = FALSE;
8547 int len = 0;
8548
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008549 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 if (*p == '$') /* environment variable */
8551 {
8552 /* first try "normal" environment variables (fast) */
8553 if (mch_getenv(p + 1) != NULL)
8554 n = TRUE;
8555 else
8556 {
8557 /* try expanding things like $VIM and ${HOME} */
8558 p = expand_env_save(p);
8559 if (p != NULL && *p != '$')
8560 n = TRUE;
8561 vim_free(p);
8562 }
8563 }
8564 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008565 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 else if (*p == '*') /* internal or user defined function */
8567 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008568 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 }
8570 else if (*p == ':')
8571 {
8572 n = cmd_exists(p + 1);
8573 }
8574 else if (*p == '#')
8575 {
8576#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008577 if (p[1] == '#')
8578 n = autocmd_supported(p + 2);
8579 else
8580 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581#endif
8582 }
8583 else /* internal variable */
8584 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008585 char_u *tofree;
8586 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008588 /* get_name_len() takes care of expanding curly braces */
8589 name = p;
8590 len = get_name_len(&p, &tofree, TRUE, FALSE);
8591 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008593 if (tofree != NULL)
8594 name = tofree;
8595 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8596 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008598 /* handle d.key, l[idx], f(expr) */
8599 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8600 if (n)
8601 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 }
8603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008605 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 }
8607
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008608 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609}
8610
8611/*
8612 * "expand()" function
8613 */
8614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008615f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008616 typval_T *argvars;
8617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618{
8619 char_u *s;
8620 int len;
8621 char_u *errormsg;
8622 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8623 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008624 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008626 rettv->v_type = VAR_STRING;
8627 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 if (*s == '%' || *s == '#' || *s == '<')
8629 {
8630 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008631 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 --emsg_off;
8633 }
8634 else
8635 {
8636 /* When the optional second argument is non-zero, don't remove matches
8637 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008638 if (argvars[1].v_type != VAR_UNKNOWN
8639 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008641 if (!error)
8642 {
8643 ExpandInit(&xpc);
8644 xpc.xp_context = EXPAND_FILES;
8645 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8646 ExpandCleanup(&xpc);
8647 }
8648 else
8649 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 }
8651}
8652
8653/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008654 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008655 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008656 */
8657 static void
8658f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008659 typval_T *argvars;
8660 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008661{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008662 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008663 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008664 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008665 list_T *l1, *l2;
8666 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008667 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008668 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008669
Bram Moolenaare9a41262005-01-15 22:18:47 +00008670 l1 = argvars[0].vval.v_list;
8671 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008672 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8673 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008674 {
8675 if (argvars[2].v_type != VAR_UNKNOWN)
8676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008677 before = get_tv_number_chk(&argvars[2], &error);
8678 if (error)
8679 return; /* type error; errmsg already given */
8680
Bram Moolenaar758711c2005-02-02 23:11:38 +00008681 if (before == l1->lv_len)
8682 item = NULL;
8683 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008684 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008685 item = list_find(l1, before);
8686 if (item == NULL)
8687 {
8688 EMSGN(_(e_listidx), before);
8689 return;
8690 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008691 }
8692 }
8693 else
8694 item = NULL;
8695 list_extend(l1, l2, item);
8696
Bram Moolenaare9a41262005-01-15 22:18:47 +00008697 copy_tv(&argvars[0], rettv);
8698 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008699 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008700 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8701 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008702 dict_T *d1, *d2;
8703 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008704 char_u *action;
8705 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008706 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008707 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008708
8709 d1 = argvars[0].vval.v_dict;
8710 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008711 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8712 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008713 {
8714 /* Check the third argument. */
8715 if (argvars[2].v_type != VAR_UNKNOWN)
8716 {
8717 static char *(av[]) = {"keep", "force", "error"};
8718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008719 action = get_tv_string_chk(&argvars[2]);
8720 if (action == NULL)
8721 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008722 for (i = 0; i < 3; ++i)
8723 if (STRCMP(action, av[i]) == 0)
8724 break;
8725 if (i == 3)
8726 {
8727 EMSGN(_(e_invarg2), action);
8728 return;
8729 }
8730 }
8731 else
8732 action = (char_u *)"force";
8733
8734 /* Go over all entries in the second dict and add them to the
8735 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008736 todo = d2->dv_hashtab.ht_used;
8737 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008738 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008739 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008741 --todo;
8742 di1 = dict_find(d1, hi2->hi_key, -1);
8743 if (di1 == NULL)
8744 {
8745 di1 = dictitem_copy(HI2DI(hi2));
8746 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8747 dictitem_free(di1);
8748 }
8749 else if (*action == 'e')
8750 {
8751 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8752 break;
8753 }
8754 else if (*action == 'f')
8755 {
8756 clear_tv(&di1->di_tv);
8757 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8758 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008759 }
8760 }
8761
Bram Moolenaare9a41262005-01-15 22:18:47 +00008762 copy_tv(&argvars[0], rettv);
8763 }
8764 }
8765 else
8766 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008767}
8768
8769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 * "filereadable()" function
8771 */
8772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008773f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008774 typval_T *argvars;
8775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
8777 FILE *fd;
8778 char_u *p;
8779 int n;
8780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008781 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8783 {
8784 n = TRUE;
8785 fclose(fd);
8786 }
8787 else
8788 n = FALSE;
8789
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791}
8792
8793/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008794 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 * rights to write into.
8796 */
8797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008798f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008799 typval_T *argvars;
8800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008802 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803}
8804
Bram Moolenaar33570922005-01-25 22:26:29 +00008805static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008806
8807 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008808findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008809 typval_T *argvars;
8810 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008811 int dir;
8812{
8813#ifdef FEAT_SEARCHPATH
8814 char_u *fname;
8815 char_u *fresult = NULL;
8816 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8817 char_u *p;
8818 char_u pathbuf[NUMBUFLEN];
8819 int count = 1;
8820 int first = TRUE;
8821
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008822 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008823
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008824 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008826 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8827 if (p == NULL)
8828 count = -1; /* error */
8829 else
8830 {
8831 if (*p != NUL)
8832 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008833
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008834 if (argvars[2].v_type != VAR_UNKNOWN)
8835 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8836 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008837 }
8838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008839 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008841 do
8842 {
8843 vim_free(fresult);
8844 fresult = find_file_in_path_option(first ? fname : NULL,
8845 first ? (int)STRLEN(fname) : 0,
8846 0, first, path, dir, NULL);
8847 first = FALSE;
8848 } while (--count > 0 && fresult != NULL);
8849 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008850
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008851 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008852#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008854#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008856}
8857
Bram Moolenaar33570922005-01-25 22:26:29 +00008858static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8859static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008860
8861/*
8862 * Implementation of map() and filter().
8863 */
8864 static void
8865filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 typval_T *argvars;
8867 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008868 int map;
8869{
8870 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008871 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 listitem_T *li, *nli;
8873 list_T *l = NULL;
8874 dictitem_T *di;
8875 hashtab_T *ht;
8876 hashitem_T *hi;
8877 dict_T *d = NULL;
8878 typval_T save_val;
8879 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008880 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008881 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008882 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar280f1262006-01-30 00:14:18 +00008883 int save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008884
8885 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008886 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008887 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008888 if ((l = argvars[0].vval.v_list) == NULL
8889 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008890 return;
8891 }
8892 else if (argvars[0].v_type == VAR_DICT)
8893 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008894 if ((d = argvars[0].vval.v_dict) == NULL
8895 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008896 return;
8897 }
8898 else
8899 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008900 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008901 return;
8902 }
8903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008904 expr = get_tv_string_buf_chk(&argvars[1], buf);
8905 /* On type errors, the preceding call has already displayed an error
8906 * message. Avoid a misleading error message for an empty string that
8907 * was not passed as argument. */
8908 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 prepare_vimvar(VV_VAL, &save_val);
8911 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008912
Bram Moolenaar280f1262006-01-30 00:14:18 +00008913 /* We reset "called_emsg" to be able to detect whether an error
8914 * occurred during evaluation of the expression. "did_emsg" can't be
8915 * used, because it is reset when calling a function. */
8916 save_called_emsg = called_emsg;
8917 called_emsg = FALSE;
8918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008919 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008920 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008921 prepare_vimvar(VV_KEY, &save_key);
8922 vimvars[VV_KEY].vv_type = VAR_STRING;
8923
8924 ht = &d->dv_hashtab;
8925 hash_lock(ht);
8926 todo = ht->ht_used;
8927 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008928 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008929 if (!HASHITEM_EMPTY(hi))
8930 {
8931 --todo;
8932 di = HI2DI(hi);
8933 if (tv_check_lock(di->di_tv.v_lock, msg))
8934 break;
8935 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008936 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
8937 || called_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008938 break;
8939 if (!map && rem)
8940 dictitem_remove(d, di);
8941 clear_tv(&vimvars[VV_KEY].vv_tv);
8942 }
8943 }
8944 hash_unlock(ht);
8945
8946 restore_vimvar(VV_KEY, &save_key);
8947 }
8948 else
8949 {
8950 for (li = l->lv_first; li != NULL; li = nli)
8951 {
8952 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008953 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008954 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008955 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
8956 || called_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008957 break;
8958 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008959 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008960 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008961 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008963 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008964
8965 called_emsg |= save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008966 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008967
8968 copy_tv(&argvars[0], rettv);
8969}
8970
8971 static int
8972filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008973 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008974 char_u *expr;
8975 int map;
8976 int *remp;
8977{
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008979 char_u *s;
8980
Bram Moolenaar33570922005-01-25 22:26:29 +00008981 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008982 s = expr;
8983 if (eval1(&s, &rettv, TRUE) == FAIL)
8984 return FAIL;
8985 if (*s != NUL) /* check for trailing chars after expr */
8986 {
8987 EMSG2(_(e_invexpr2), s);
8988 return FAIL;
8989 }
8990 if (map)
8991 {
8992 /* map(): replace the list item value */
8993 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008994 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008995 *tv = rettv;
8996 }
8997 else
8998 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008999 int error = FALSE;
9000
Bram Moolenaare9a41262005-01-15 22:18:47 +00009001 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009002 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009003 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009004 /* On type error, nothing has been removed; return FAIL to stop the
9005 * loop. The error message was given by get_tv_number_chk(). */
9006 if (error)
9007 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009008 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009009 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009010 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009011}
9012
9013/*
9014 * "filter()" function
9015 */
9016 static void
9017f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009018 typval_T *argvars;
9019 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009020{
9021 filter_map(argvars, rettv, FALSE);
9022}
9023
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009024/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009025 * "finddir({fname}[, {path}[, {count}]])" function
9026 */
9027 static void
9028f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009029 typval_T *argvars;
9030 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009031{
9032 findfilendir(argvars, rettv, TRUE);
9033}
9034
9035/*
9036 * "findfile({fname}[, {path}[, {count}]])" function
9037 */
9038 static void
9039f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009040 typval_T *argvars;
9041 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009042{
9043 findfilendir(argvars, rettv, FALSE);
9044}
9045
9046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 * "fnamemodify({fname}, {mods})" function
9048 */
9049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *argvars;
9052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
9054 char_u *fname;
9055 char_u *mods;
9056 int usedlen = 0;
9057 int len;
9058 char_u *fbuf = NULL;
9059 char_u buf[NUMBUFLEN];
9060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009061 fname = get_tv_string_chk(&argvars[0]);
9062 mods = get_tv_string_buf_chk(&argvars[1], buf);
9063 if (fname == NULL || mods == NULL)
9064 fname = NULL;
9065 else
9066 {
9067 len = (int)STRLEN(fname);
9068 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 vim_free(fbuf);
9077}
9078
Bram Moolenaar33570922005-01-25 22:26:29 +00009079static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080
9081/*
9082 * "foldclosed()" function
9083 */
9084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009086 typval_T *argvars;
9087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 int end;
9089{
9090#ifdef FEAT_FOLDING
9091 linenr_T lnum;
9092 linenr_T first, last;
9093
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9096 {
9097 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9098 {
9099 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009100 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 return;
9104 }
9105 }
9106#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108}
9109
9110/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009111 * "foldclosed()" function
9112 */
9113 static void
9114f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009115 typval_T *argvars;
9116 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009117{
9118 foldclosed_both(argvars, rettv, FALSE);
9119}
9120
9121/*
9122 * "foldclosedend()" function
9123 */
9124 static void
9125f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009126 typval_T *argvars;
9127 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009128{
9129 foldclosed_both(argvars, rettv, TRUE);
9130}
9131
9132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 * "foldlevel()" function
9134 */
9135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009136f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009137 typval_T *argvars;
9138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139{
9140#ifdef FEAT_FOLDING
9141 linenr_T lnum;
9142
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009143 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009145 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 else
9147#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009148 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149}
9150
9151/*
9152 * "foldtext()" function
9153 */
9154/*ARGSUSED*/
9155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009157 typval_T *argvars;
9158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159{
9160#ifdef FEAT_FOLDING
9161 linenr_T lnum;
9162 char_u *s;
9163 char_u *r;
9164 int len;
9165 char *txt;
9166#endif
9167
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009168 rettv->v_type = VAR_STRING;
9169 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009171 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9172 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9173 <= curbuf->b_ml.ml_line_count
9174 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 {
9176 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009177 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9178 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 {
9180 if (!linewhite(lnum))
9181 break;
9182 ++lnum;
9183 }
9184
9185 /* Find interesting text in this line. */
9186 s = skipwhite(ml_get(lnum));
9187 /* skip C comment-start */
9188 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009191 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009192 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009193 {
9194 s = skipwhite(ml_get(lnum + 1));
9195 if (*s == '*')
9196 s = skipwhite(s + 1);
9197 }
9198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 txt = _("+-%s%3ld lines: ");
9200 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009201 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 + 20 /* for %3ld */
9203 + STRLEN(s))); /* concatenated */
9204 if (r != NULL)
9205 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009206 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9207 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9208 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 len = (int)STRLEN(r);
9210 STRCAT(r, s);
9211 /* remove 'foldmarker' and 'commentstring' */
9212 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009213 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 }
9215 }
9216#endif
9217}
9218
9219/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009220 * "foldtextresult(lnum)" function
9221 */
9222/*ARGSUSED*/
9223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009225 typval_T *argvars;
9226 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009227{
9228#ifdef FEAT_FOLDING
9229 linenr_T lnum;
9230 char_u *text;
9231 char_u buf[51];
9232 foldinfo_T foldinfo;
9233 int fold_count;
9234#endif
9235
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009236 rettv->v_type = VAR_STRING;
9237 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009238#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009239 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009240 /* treat illegal types and illegal string values for {lnum} the same */
9241 if (lnum < 0)
9242 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009243 fold_count = foldedCount(curwin, lnum, &foldinfo);
9244 if (fold_count > 0)
9245 {
9246 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9247 &foldinfo, buf);
9248 if (text == buf)
9249 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009250 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009251 }
9252#endif
9253}
9254
9255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 * "foreground()" function
9257 */
9258/*ARGSUSED*/
9259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009260f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009261 typval_T *argvars;
9262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009264 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265#ifdef FEAT_GUI
9266 if (gui.in_use)
9267 gui_mch_set_foreground();
9268#else
9269# ifdef WIN32
9270 win32_set_foreground();
9271# endif
9272#endif
9273}
9274
9275/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009276 * "function()" function
9277 */
9278/*ARGSUSED*/
9279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009280f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009281 typval_T *argvars;
9282 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009283{
9284 char_u *s;
9285
Bram Moolenaara7043832005-01-21 11:56:39 +00009286 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009287 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009288 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009289 EMSG2(_(e_invarg2), s);
9290 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009291 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009292 else
9293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294 rettv->vval.v_string = vim_strsave(s);
9295 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009296 }
9297}
9298
9299/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009300 * "garbagecollect()" function
9301 */
9302/*ARGSUSED*/
9303 static void
9304f_garbagecollect(argvars, rettv)
9305 typval_T *argvars;
9306 typval_T *rettv;
9307{
9308 garbage_collect();
9309}
9310
9311/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009312 * "get()" function
9313 */
9314 static void
9315f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009316 typval_T *argvars;
9317 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009318{
Bram Moolenaar33570922005-01-25 22:26:29 +00009319 listitem_T *li;
9320 list_T *l;
9321 dictitem_T *di;
9322 dict_T *d;
9323 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009324
Bram Moolenaare9a41262005-01-15 22:18:47 +00009325 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009326 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009327 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 int error = FALSE;
9330
9331 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9332 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009333 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009334 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009335 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009336 else if (argvars[0].v_type == VAR_DICT)
9337 {
9338 if ((d = argvars[0].vval.v_dict) != NULL)
9339 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009340 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009341 if (di != NULL)
9342 tv = &di->di_tv;
9343 }
9344 }
9345 else
9346 EMSG2(_(e_listdictarg), "get()");
9347
9348 if (tv == NULL)
9349 {
9350 if (argvars[2].v_type == VAR_UNKNOWN)
9351 rettv->vval.v_number = 0;
9352 else
9353 copy_tv(&argvars[2], rettv);
9354 }
9355 else
9356 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009357}
9358
Bram Moolenaar342337a2005-07-21 21:11:17 +00009359static 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 +00009360
9361/*
9362 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009363 * Return a range (from start to end) of lines in rettv from the specified
9364 * buffer.
9365 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009366 */
9367 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009368get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009369 buf_T *buf;
9370 linenr_T start;
9371 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009372 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009373 typval_T *rettv;
9374{
9375 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009376
Bram Moolenaar342337a2005-07-21 21:11:17 +00009377 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009378 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009379 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009380 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009381 }
9382 else
9383 rettv->vval.v_number = 0;
9384
9385 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9386 return;
9387
9388 if (!retlist)
9389 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009390 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9391 p = ml_get_buf(buf, start, FALSE);
9392 else
9393 p = (char_u *)"";
9394
9395 rettv->v_type = VAR_STRING;
9396 rettv->vval.v_string = vim_strsave(p);
9397 }
9398 else
9399 {
9400 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009401 return;
9402
9403 if (start < 1)
9404 start = 1;
9405 if (end > buf->b_ml.ml_line_count)
9406 end = buf->b_ml.ml_line_count;
9407 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009408 if (list_append_string(rettv->vval.v_list,
9409 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009410 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009411 }
9412}
9413
9414/*
9415 * "getbufline()" function
9416 */
9417 static void
9418f_getbufline(argvars, rettv)
9419 typval_T *argvars;
9420 typval_T *rettv;
9421{
9422 linenr_T lnum;
9423 linenr_T end;
9424 buf_T *buf;
9425
9426 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9427 ++emsg_off;
9428 buf = get_buf_tv(&argvars[0]);
9429 --emsg_off;
9430
Bram Moolenaar661b1822005-07-28 22:36:45 +00009431 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009432 if (argvars[2].v_type == VAR_UNKNOWN)
9433 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009434 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009435 end = get_tv_lnum_buf(&argvars[2], buf);
9436
Bram Moolenaar342337a2005-07-21 21:11:17 +00009437 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009438}
9439
Bram Moolenaar0d660222005-01-07 21:51:51 +00009440/*
9441 * "getbufvar()" function
9442 */
9443 static void
9444f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009445 typval_T *argvars;
9446 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009447{
9448 buf_T *buf;
9449 buf_T *save_curbuf;
9450 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009451 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009453 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9454 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009455 ++emsg_off;
9456 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009457
9458 rettv->v_type = VAR_STRING;
9459 rettv->vval.v_string = NULL;
9460
9461 if (buf != NULL && varname != NULL)
9462 {
9463 if (*varname == '&') /* buffer-local-option */
9464 {
9465 /* set curbuf to be our buf, temporarily */
9466 save_curbuf = curbuf;
9467 curbuf = buf;
9468
9469 get_option_tv(&varname, rettv, TRUE);
9470
9471 /* restore previous notion of curbuf */
9472 curbuf = save_curbuf;
9473 }
9474 else
9475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009476 if (*varname == NUL)
9477 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9478 * scope prefix before the NUL byte is required by
9479 * find_var_in_ht(). */
9480 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009481 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009482 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009483 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009484 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009485 }
9486 }
9487
9488 --emsg_off;
9489}
9490
9491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 * "getchar()" function
9493 */
9494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009496 typval_T *argvars;
9497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498{
9499 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009500 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
9502 ++no_mapping;
9503 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009504 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 /* getchar(): blocking wait. */
9506 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009507 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 /* getchar(1): only check if char avail */
9509 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009510 else if (error || vpeekc() == NUL)
9511 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 n = 0;
9513 else
9514 /* getchar(0) and char avail: return char */
9515 n = safe_vgetc();
9516 --no_mapping;
9517 --allow_keys;
9518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 if (IS_SPECIAL(n) || mod_mask != 0)
9521 {
9522 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9523 int i = 0;
9524
9525 /* Turn a special key into three bytes, plus modifier. */
9526 if (mod_mask != 0)
9527 {
9528 temp[i++] = K_SPECIAL;
9529 temp[i++] = KS_MODIFIER;
9530 temp[i++] = mod_mask;
9531 }
9532 if (IS_SPECIAL(n))
9533 {
9534 temp[i++] = K_SPECIAL;
9535 temp[i++] = K_SECOND(n);
9536 temp[i++] = K_THIRD(n);
9537 }
9538#ifdef FEAT_MBYTE
9539 else if (has_mbyte)
9540 i += (*mb_char2bytes)(n, temp + i);
9541#endif
9542 else
9543 temp[i++] = n;
9544 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545 rettv->v_type = VAR_STRING;
9546 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 }
9548}
9549
9550/*
9551 * "getcharmod()" function
9552 */
9553/*ARGSUSED*/
9554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009556 typval_T *argvars;
9557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560}
9561
9562/*
9563 * "getcmdline()" function
9564 */
9565/*ARGSUSED*/
9566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009568 typval_T *argvars;
9569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571 rettv->v_type = VAR_STRING;
9572 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573}
9574
9575/*
9576 * "getcmdpos()" function
9577 */
9578/*ARGSUSED*/
9579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009580f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009581 typval_T *argvars;
9582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585}
9586
9587/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009588 * "getcmdtype()" function
9589 */
9590/*ARGSUSED*/
9591 static void
9592f_getcmdtype(argvars, rettv)
9593 typval_T *argvars;
9594 typval_T *rettv;
9595{
9596 rettv->v_type = VAR_STRING;
9597 rettv->vval.v_string = alloc(2);
9598 if (rettv->vval.v_string != NULL)
9599 {
9600 rettv->vval.v_string[0] = get_cmdline_type();
9601 rettv->vval.v_string[1] = NUL;
9602 }
9603}
9604
9605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 * "getcwd()" function
9607 */
9608/*ARGSUSED*/
9609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009611 typval_T *argvars;
9612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613{
9614 char_u cwd[MAXPATHL];
9615
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 else
9620 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009623 if (rettv->vval.v_string != NULL)
9624 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625#endif
9626 }
9627}
9628
9629/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009630 * "getfontname()" function
9631 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009632/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009635 typval_T *argvars;
9636 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009637{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 rettv->v_type = VAR_STRING;
9639 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009640#ifdef FEAT_GUI
9641 if (gui.in_use)
9642 {
9643 GuiFont font;
9644 char_u *name = NULL;
9645
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009646 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009647 {
9648 /* Get the "Normal" font. Either the name saved by
9649 * hl_set_font_name() or from the font ID. */
9650 font = gui.norm_font;
9651 name = hl_get_font_name();
9652 }
9653 else
9654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009656 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9657 return;
9658 font = gui_mch_get_font(name, FALSE);
9659 if (font == NOFONT)
9660 return; /* Invalid font name, return empty string. */
9661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009662 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009663 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009664 gui_mch_free_font(font);
9665 }
9666#endif
9667}
9668
9669/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009670 * "getfperm({fname})" function
9671 */
9672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009674 typval_T *argvars;
9675 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009676{
9677 char_u *fname;
9678 struct stat st;
9679 char_u *perm = NULL;
9680 char_u flags[] = "rwx";
9681 int i;
9682
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009684
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009685 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009686 if (mch_stat((char *)fname, &st) >= 0)
9687 {
9688 perm = vim_strsave((char_u *)"---------");
9689 if (perm != NULL)
9690 {
9691 for (i = 0; i < 9; i++)
9692 {
9693 if (st.st_mode & (1 << (8 - i)))
9694 perm[i] = flags[i % 3];
9695 }
9696 }
9697 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009698 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009699}
9700
9701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 * "getfsize({fname})" function
9703 */
9704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009706 typval_T *argvars;
9707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708{
9709 char_u *fname;
9710 struct stat st;
9711
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715
9716 if (mch_stat((char *)fname, &st) >= 0)
9717 {
9718 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 }
9723 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725}
9726
9727/*
9728 * "getftime({fname})" function
9729 */
9730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009731f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009732 typval_T *argvars;
9733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734{
9735 char_u *fname;
9736 struct stat st;
9737
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739
9740 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009743 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744}
9745
9746/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009747 * "getftype({fname})" function
9748 */
9749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009751 typval_T *argvars;
9752 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009753{
9754 char_u *fname;
9755 struct stat st;
9756 char_u *type = NULL;
9757 char *t;
9758
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009759 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009760
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009761 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009762 if (mch_lstat((char *)fname, &st) >= 0)
9763 {
9764#ifdef S_ISREG
9765 if (S_ISREG(st.st_mode))
9766 t = "file";
9767 else if (S_ISDIR(st.st_mode))
9768 t = "dir";
9769# ifdef S_ISLNK
9770 else if (S_ISLNK(st.st_mode))
9771 t = "link";
9772# endif
9773# ifdef S_ISBLK
9774 else if (S_ISBLK(st.st_mode))
9775 t = "bdev";
9776# endif
9777# ifdef S_ISCHR
9778 else if (S_ISCHR(st.st_mode))
9779 t = "cdev";
9780# endif
9781# ifdef S_ISFIFO
9782 else if (S_ISFIFO(st.st_mode))
9783 t = "fifo";
9784# endif
9785# ifdef S_ISSOCK
9786 else if (S_ISSOCK(st.st_mode))
9787 t = "fifo";
9788# endif
9789 else
9790 t = "other";
9791#else
9792# ifdef S_IFMT
9793 switch (st.st_mode & S_IFMT)
9794 {
9795 case S_IFREG: t = "file"; break;
9796 case S_IFDIR: t = "dir"; break;
9797# ifdef S_IFLNK
9798 case S_IFLNK: t = "link"; break;
9799# endif
9800# ifdef S_IFBLK
9801 case S_IFBLK: t = "bdev"; break;
9802# endif
9803# ifdef S_IFCHR
9804 case S_IFCHR: t = "cdev"; break;
9805# endif
9806# ifdef S_IFIFO
9807 case S_IFIFO: t = "fifo"; break;
9808# endif
9809# ifdef S_IFSOCK
9810 case S_IFSOCK: t = "socket"; break;
9811# endif
9812 default: t = "other";
9813 }
9814# else
9815 if (mch_isdir(fname))
9816 t = "dir";
9817 else
9818 t = "file";
9819# endif
9820#endif
9821 type = vim_strsave((char_u *)t);
9822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009823 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009824}
9825
9826/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009827 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009828 */
9829 static void
9830f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009831 typval_T *argvars;
9832 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009833{
9834 linenr_T lnum;
9835 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009836 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009837
9838 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009839 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009840 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009841 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009842 retlist = FALSE;
9843 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009844 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009845 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009846 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009847 retlist = TRUE;
9848 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009849
Bram Moolenaar342337a2005-07-21 21:11:17 +00009850 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009851}
9852
9853/*
Bram Moolenaar280f1262006-01-30 00:14:18 +00009854 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +00009855 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00009856/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +00009857 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +00009858f_getqflist(argvars, rettv)
9859 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009860 typval_T *rettv;
9861{
9862#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +00009863 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009864#endif
9865
9866 rettv->vval.v_number = FALSE;
9867#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009868 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +00009869 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00009870 wp = NULL;
9871 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
9872 {
9873 wp = find_win_by_nr(&argvars[0]);
9874 if (wp == NULL)
9875 return;
9876 }
9877
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009878 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009879 }
9880#endif
9881}
9882
9883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 * "getreg()" function
9885 */
9886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009888 typval_T *argvars;
9889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890{
9891 char_u *strregname;
9892 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009893 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009894 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009896 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009898 strregname = get_tv_string_chk(&argvars[0]);
9899 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009900 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009901 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009904 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 regname = (strregname == NULL ? '"' : *strregname);
9906 if (regname == 0)
9907 regname = '"';
9908
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009909 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009910 rettv->vval.v_string = error ? NULL :
9911 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912}
9913
9914/*
9915 * "getregtype()" function
9916 */
9917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009919 typval_T *argvars;
9920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921{
9922 char_u *strregname;
9923 int regname;
9924 char_u buf[NUMBUFLEN + 2];
9925 long reglen = 0;
9926
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009927 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009928 {
9929 strregname = get_tv_string_chk(&argvars[0]);
9930 if (strregname == NULL) /* type error; errmsg already given */
9931 {
9932 rettv->v_type = VAR_STRING;
9933 rettv->vval.v_string = NULL;
9934 return;
9935 }
9936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 else
9938 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009939 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940
9941 regname = (strregname == NULL ? '"' : *strregname);
9942 if (regname == 0)
9943 regname = '"';
9944
9945 buf[0] = NUL;
9946 buf[1] = NUL;
9947 switch (get_reg_type(regname, &reglen))
9948 {
9949 case MLINE: buf[0] = 'V'; break;
9950 case MCHAR: buf[0] = 'v'; break;
9951#ifdef FEAT_VISUAL
9952 case MBLOCK:
9953 buf[0] = Ctrl_V;
9954 sprintf((char *)buf + 1, "%ld", reglen + 1);
9955 break;
9956#endif
9957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009958 rettv->v_type = VAR_STRING;
9959 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960}
9961
9962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 * "getwinposx()" function
9964 */
9965/*ARGSUSED*/
9966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009968 typval_T *argvars;
9969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972#ifdef FEAT_GUI
9973 if (gui.in_use)
9974 {
9975 int x, y;
9976
9977 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 }
9980#endif
9981}
9982
9983/*
9984 * "getwinposy()" function
9985 */
9986/*ARGSUSED*/
9987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009988f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009989 typval_T *argvars;
9990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009992 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993#ifdef FEAT_GUI
9994 if (gui.in_use)
9995 {
9996 int x, y;
9997
9998 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009999 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 }
10001#endif
10002}
10003
Bram Moolenaara40058a2005-07-11 22:42:07 +000010004 static win_T *
10005find_win_by_nr(vp)
10006 typval_T *vp;
10007{
10008#ifdef FEAT_WINDOWS
10009 win_T *wp;
10010#endif
10011 int nr;
10012
10013 nr = get_tv_number_chk(vp, NULL);
10014
10015#ifdef FEAT_WINDOWS
10016 if (nr < 0)
10017 return NULL;
10018 if (nr == 0)
10019 return curwin;
10020
10021 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10022 if (--nr <= 0)
10023 break;
10024 return wp;
10025#else
10026 if (nr == 0 || nr == 1)
10027 return curwin;
10028 return NULL;
10029#endif
10030}
10031
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032/*
10033 * "getwinvar()" function
10034 */
10035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010036f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010037 typval_T *argvars;
10038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039{
10040 win_T *win, *oldcurwin;
10041 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010042 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010045 varname = get_tv_string_chk(&argvars[1]);
10046 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 rettv->v_type = VAR_STRING;
10049 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
10051 if (win != NULL && varname != NULL)
10052 {
10053 if (*varname == '&') /* window-local-option */
10054 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010055 /* Set curwin to be our win, temporarily. Also set curbuf, so
10056 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 oldcurwin = curwin;
10058 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010059 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062
10063 /* restore previous notion of curwin */
10064 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010065 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 }
10067 else
10068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010069 if (*varname == NUL)
10070 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10071 * scope prefix before the NUL byte is required by
10072 * find_var_in_ht(). */
10073 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010075 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010077 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 }
10079 }
10080
10081 --emsg_off;
10082}
10083
10084/*
10085 * "glob()" function
10086 */
10087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010088f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010089 typval_T *argvars;
10090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091{
10092 expand_T xpc;
10093
10094 ExpandInit(&xpc);
10095 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010096 rettv->v_type = VAR_STRING;
10097 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10099 ExpandCleanup(&xpc);
10100}
10101
10102/*
10103 * "globpath()" function
10104 */
10105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010106f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010107 typval_T *argvars;
10108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109{
10110 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010114 if (file == NULL)
10115 rettv->vval.v_string = NULL;
10116 else
10117 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118}
10119
10120/*
10121 * "has()" function
10122 */
10123 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010125 typval_T *argvars;
10126 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127{
10128 int i;
10129 char_u *name;
10130 int n = FALSE;
10131 static char *(has_list[]) =
10132 {
10133#ifdef AMIGA
10134 "amiga",
10135# ifdef FEAT_ARP
10136 "arp",
10137# endif
10138#endif
10139#ifdef __BEOS__
10140 "beos",
10141#endif
10142#ifdef MSDOS
10143# ifdef DJGPP
10144 "dos32",
10145# else
10146 "dos16",
10147# endif
10148#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010149#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 "mac",
10151#endif
10152#if defined(MACOS_X_UNIX)
10153 "macunix",
10154#endif
10155#ifdef OS2
10156 "os2",
10157#endif
10158#ifdef __QNX__
10159 "qnx",
10160#endif
10161#ifdef RISCOS
10162 "riscos",
10163#endif
10164#ifdef UNIX
10165 "unix",
10166#endif
10167#ifdef VMS
10168 "vms",
10169#endif
10170#ifdef WIN16
10171 "win16",
10172#endif
10173#ifdef WIN32
10174 "win32",
10175#endif
10176#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10177 "win32unix",
10178#endif
10179#ifdef WIN64
10180 "win64",
10181#endif
10182#ifdef EBCDIC
10183 "ebcdic",
10184#endif
10185#ifndef CASE_INSENSITIVE_FILENAME
10186 "fname_case",
10187#endif
10188#ifdef FEAT_ARABIC
10189 "arabic",
10190#endif
10191#ifdef FEAT_AUTOCMD
10192 "autocmd",
10193#endif
10194#ifdef FEAT_BEVAL
10195 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010196# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10197 "balloon_multiline",
10198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199#endif
10200#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10201 "builtin_terms",
10202# ifdef ALL_BUILTIN_TCAPS
10203 "all_builtin_terms",
10204# endif
10205#endif
10206#ifdef FEAT_BYTEOFF
10207 "byte_offset",
10208#endif
10209#ifdef FEAT_CINDENT
10210 "cindent",
10211#endif
10212#ifdef FEAT_CLIENTSERVER
10213 "clientserver",
10214#endif
10215#ifdef FEAT_CLIPBOARD
10216 "clipboard",
10217#endif
10218#ifdef FEAT_CMDL_COMPL
10219 "cmdline_compl",
10220#endif
10221#ifdef FEAT_CMDHIST
10222 "cmdline_hist",
10223#endif
10224#ifdef FEAT_COMMENTS
10225 "comments",
10226#endif
10227#ifdef FEAT_CRYPT
10228 "cryptv",
10229#endif
10230#ifdef FEAT_CSCOPE
10231 "cscope",
10232#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010233#ifdef CURSOR_SHAPE
10234 "cursorshape",
10235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236#ifdef DEBUG
10237 "debug",
10238#endif
10239#ifdef FEAT_CON_DIALOG
10240 "dialog_con",
10241#endif
10242#ifdef FEAT_GUI_DIALOG
10243 "dialog_gui",
10244#endif
10245#ifdef FEAT_DIFF
10246 "diff",
10247#endif
10248#ifdef FEAT_DIGRAPHS
10249 "digraphs",
10250#endif
10251#ifdef FEAT_DND
10252 "dnd",
10253#endif
10254#ifdef FEAT_EMACS_TAGS
10255 "emacs_tags",
10256#endif
10257 "eval", /* always present, of course! */
10258#ifdef FEAT_EX_EXTRA
10259 "ex_extra",
10260#endif
10261#ifdef FEAT_SEARCH_EXTRA
10262 "extra_search",
10263#endif
10264#ifdef FEAT_FKMAP
10265 "farsi",
10266#endif
10267#ifdef FEAT_SEARCHPATH
10268 "file_in_path",
10269#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010270#if defined(UNIX) && !defined(USE_SYSTEM)
10271 "filterpipe",
10272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273#ifdef FEAT_FIND_ID
10274 "find_in_path",
10275#endif
10276#ifdef FEAT_FOLDING
10277 "folding",
10278#endif
10279#ifdef FEAT_FOOTER
10280 "footer",
10281#endif
10282#if !defined(USE_SYSTEM) && defined(UNIX)
10283 "fork",
10284#endif
10285#ifdef FEAT_GETTEXT
10286 "gettext",
10287#endif
10288#ifdef FEAT_GUI
10289 "gui",
10290#endif
10291#ifdef FEAT_GUI_ATHENA
10292# ifdef FEAT_GUI_NEXTAW
10293 "gui_neXtaw",
10294# else
10295 "gui_athena",
10296# endif
10297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298#ifdef FEAT_GUI_GTK
10299 "gui_gtk",
10300# ifdef HAVE_GTK2
10301 "gui_gtk2",
10302# endif
10303#endif
10304#ifdef FEAT_GUI_MAC
10305 "gui_mac",
10306#endif
10307#ifdef FEAT_GUI_MOTIF
10308 "gui_motif",
10309#endif
10310#ifdef FEAT_GUI_PHOTON
10311 "gui_photon",
10312#endif
10313#ifdef FEAT_GUI_W16
10314 "gui_win16",
10315#endif
10316#ifdef FEAT_GUI_W32
10317 "gui_win32",
10318#endif
10319#ifdef FEAT_HANGULIN
10320 "hangul_input",
10321#endif
10322#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10323 "iconv",
10324#endif
10325#ifdef FEAT_INS_EXPAND
10326 "insert_expand",
10327#endif
10328#ifdef FEAT_JUMPLIST
10329 "jumplist",
10330#endif
10331#ifdef FEAT_KEYMAP
10332 "keymap",
10333#endif
10334#ifdef FEAT_LANGMAP
10335 "langmap",
10336#endif
10337#ifdef FEAT_LIBCALL
10338 "libcall",
10339#endif
10340#ifdef FEAT_LINEBREAK
10341 "linebreak",
10342#endif
10343#ifdef FEAT_LISP
10344 "lispindent",
10345#endif
10346#ifdef FEAT_LISTCMDS
10347 "listcmds",
10348#endif
10349#ifdef FEAT_LOCALMAP
10350 "localmap",
10351#endif
10352#ifdef FEAT_MENU
10353 "menu",
10354#endif
10355#ifdef FEAT_SESSION
10356 "mksession",
10357#endif
10358#ifdef FEAT_MODIFY_FNAME
10359 "modify_fname",
10360#endif
10361#ifdef FEAT_MOUSE
10362 "mouse",
10363#endif
10364#ifdef FEAT_MOUSESHAPE
10365 "mouseshape",
10366#endif
10367#if defined(UNIX) || defined(VMS)
10368# ifdef FEAT_MOUSE_DEC
10369 "mouse_dec",
10370# endif
10371# ifdef FEAT_MOUSE_GPM
10372 "mouse_gpm",
10373# endif
10374# ifdef FEAT_MOUSE_JSB
10375 "mouse_jsbterm",
10376# endif
10377# ifdef FEAT_MOUSE_NET
10378 "mouse_netterm",
10379# endif
10380# ifdef FEAT_MOUSE_PTERM
10381 "mouse_pterm",
10382# endif
10383# ifdef FEAT_MOUSE_XTERM
10384 "mouse_xterm",
10385# endif
10386#endif
10387#ifdef FEAT_MBYTE
10388 "multi_byte",
10389#endif
10390#ifdef FEAT_MBYTE_IME
10391 "multi_byte_ime",
10392#endif
10393#ifdef FEAT_MULTI_LANG
10394 "multi_lang",
10395#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010396#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010397#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010398 "mzscheme",
10399#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401#ifdef FEAT_OLE
10402 "ole",
10403#endif
10404#ifdef FEAT_OSFILETYPE
10405 "osfiletype",
10406#endif
10407#ifdef FEAT_PATH_EXTRA
10408 "path_extra",
10409#endif
10410#ifdef FEAT_PERL
10411#ifndef DYNAMIC_PERL
10412 "perl",
10413#endif
10414#endif
10415#ifdef FEAT_PYTHON
10416#ifndef DYNAMIC_PYTHON
10417 "python",
10418#endif
10419#endif
10420#ifdef FEAT_POSTSCRIPT
10421 "postscript",
10422#endif
10423#ifdef FEAT_PRINTER
10424 "printer",
10425#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010426#ifdef FEAT_PROFILE
10427 "profile",
10428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429#ifdef FEAT_QUICKFIX
10430 "quickfix",
10431#endif
10432#ifdef FEAT_RIGHTLEFT
10433 "rightleft",
10434#endif
10435#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10436 "ruby",
10437#endif
10438#ifdef FEAT_SCROLLBIND
10439 "scrollbind",
10440#endif
10441#ifdef FEAT_CMDL_INFO
10442 "showcmd",
10443 "cmdline_info",
10444#endif
10445#ifdef FEAT_SIGNS
10446 "signs",
10447#endif
10448#ifdef FEAT_SMARTINDENT
10449 "smartindent",
10450#endif
10451#ifdef FEAT_SNIFF
10452 "sniff",
10453#endif
10454#ifdef FEAT_STL_OPT
10455 "statusline",
10456#endif
10457#ifdef FEAT_SUN_WORKSHOP
10458 "sun_workshop",
10459#endif
10460#ifdef FEAT_NETBEANS_INTG
10461 "netbeans_intg",
10462#endif
10463#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010464 "spell",
10465#endif
10466#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 "syntax",
10468#endif
10469#if defined(USE_SYSTEM) || !defined(UNIX)
10470 "system",
10471#endif
10472#ifdef FEAT_TAG_BINS
10473 "tag_binary",
10474#endif
10475#ifdef FEAT_TAG_OLDSTATIC
10476 "tag_old_static",
10477#endif
10478#ifdef FEAT_TAG_ANYWHITE
10479 "tag_any_white",
10480#endif
10481#ifdef FEAT_TCL
10482# ifndef DYNAMIC_TCL
10483 "tcl",
10484# endif
10485#endif
10486#ifdef TERMINFO
10487 "terminfo",
10488#endif
10489#ifdef FEAT_TERMRESPONSE
10490 "termresponse",
10491#endif
10492#ifdef FEAT_TEXTOBJ
10493 "textobjects",
10494#endif
10495#ifdef HAVE_TGETENT
10496 "tgetent",
10497#endif
10498#ifdef FEAT_TITLE
10499 "title",
10500#endif
10501#ifdef FEAT_TOOLBAR
10502 "toolbar",
10503#endif
10504#ifdef FEAT_USR_CMDS
10505 "user-commands", /* was accidentally included in 5.4 */
10506 "user_commands",
10507#endif
10508#ifdef FEAT_VIMINFO
10509 "viminfo",
10510#endif
10511#ifdef FEAT_VERTSPLIT
10512 "vertsplit",
10513#endif
10514#ifdef FEAT_VIRTUALEDIT
10515 "virtualedit",
10516#endif
10517#ifdef FEAT_VISUAL
10518 "visual",
10519#endif
10520#ifdef FEAT_VISUALEXTRA
10521 "visualextra",
10522#endif
10523#ifdef FEAT_VREPLACE
10524 "vreplace",
10525#endif
10526#ifdef FEAT_WILDIGN
10527 "wildignore",
10528#endif
10529#ifdef FEAT_WILDMENU
10530 "wildmenu",
10531#endif
10532#ifdef FEAT_WINDOWS
10533 "windows",
10534#endif
10535#ifdef FEAT_WAK
10536 "winaltkeys",
10537#endif
10538#ifdef FEAT_WRITEBACKUP
10539 "writebackup",
10540#endif
10541#ifdef FEAT_XIM
10542 "xim",
10543#endif
10544#ifdef FEAT_XFONTSET
10545 "xfontset",
10546#endif
10547#ifdef USE_XSMP
10548 "xsmp",
10549#endif
10550#ifdef USE_XSMP_INTERACT
10551 "xsmp_interact",
10552#endif
10553#ifdef FEAT_XCLIPBOARD
10554 "xterm_clipboard",
10555#endif
10556#ifdef FEAT_XTERM_SAVE
10557 "xterm_save",
10558#endif
10559#if defined(UNIX) && defined(FEAT_X11)
10560 "X11",
10561#endif
10562 NULL
10563 };
10564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 for (i = 0; has_list[i] != NULL; ++i)
10567 if (STRICMP(name, has_list[i]) == 0)
10568 {
10569 n = TRUE;
10570 break;
10571 }
10572
10573 if (n == FALSE)
10574 {
10575 if (STRNICMP(name, "patch", 5) == 0)
10576 n = has_patch(atoi((char *)name + 5));
10577 else if (STRICMP(name, "vim_starting") == 0)
10578 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010579#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10580 else if (STRICMP(name, "balloon_multiline") == 0)
10581 n = multiline_balloon_available();
10582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583#ifdef DYNAMIC_TCL
10584 else if (STRICMP(name, "tcl") == 0)
10585 n = tcl_enabled(FALSE);
10586#endif
10587#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10588 else if (STRICMP(name, "iconv") == 0)
10589 n = iconv_enabled(FALSE);
10590#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010591#ifdef DYNAMIC_MZSCHEME
10592 else if (STRICMP(name, "mzscheme") == 0)
10593 n = mzscheme_enabled(FALSE);
10594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595#ifdef DYNAMIC_RUBY
10596 else if (STRICMP(name, "ruby") == 0)
10597 n = ruby_enabled(FALSE);
10598#endif
10599#ifdef DYNAMIC_PYTHON
10600 else if (STRICMP(name, "python") == 0)
10601 n = python_enabled(FALSE);
10602#endif
10603#ifdef DYNAMIC_PERL
10604 else if (STRICMP(name, "perl") == 0)
10605 n = perl_enabled(FALSE);
10606#endif
10607#ifdef FEAT_GUI
10608 else if (STRICMP(name, "gui_running") == 0)
10609 n = (gui.in_use || gui.starting);
10610# ifdef FEAT_GUI_W32
10611 else if (STRICMP(name, "gui_win32s") == 0)
10612 n = gui_is_win32s();
10613# endif
10614# ifdef FEAT_BROWSE
10615 else if (STRICMP(name, "browse") == 0)
10616 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10617# endif
10618#endif
10619#ifdef FEAT_SYN_HL
10620 else if (STRICMP(name, "syntax_items") == 0)
10621 n = syntax_present(curbuf);
10622#endif
10623#if defined(WIN3264)
10624 else if (STRICMP(name, "win95") == 0)
10625 n = mch_windows95();
10626#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010627#ifdef FEAT_NETBEANS_INTG
10628 else if (STRICMP(name, "netbeans_enabled") == 0)
10629 n = usingNetbeans;
10630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 }
10632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010633 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634}
10635
10636/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010637 * "has_key()" function
10638 */
10639 static void
10640f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010641 typval_T *argvars;
10642 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010643{
10644 rettv->vval.v_number = 0;
10645 if (argvars[0].v_type != VAR_DICT)
10646 {
10647 EMSG(_(e_dictreq));
10648 return;
10649 }
10650 if (argvars[0].vval.v_dict == NULL)
10651 return;
10652
10653 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010654 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010655}
10656
10657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 * "hasmapto()" function
10659 */
10660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010661f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010662 typval_T *argvars;
10663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664{
10665 char_u *name;
10666 char_u *mode;
10667 char_u buf[NUMBUFLEN];
10668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010669 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010670 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671 mode = (char_u *)"nvo";
10672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010673 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674
10675 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010676 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010678 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679}
10680
10681/*
10682 * "histadd()" function
10683 */
10684/*ARGSUSED*/
10685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010686f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010687 typval_T *argvars;
10688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689{
10690#ifdef FEAT_CMDHIST
10691 int histype;
10692 char_u *str;
10693 char_u buf[NUMBUFLEN];
10694#endif
10695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010696 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 if (check_restricted() || check_secure())
10698 return;
10699#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010700 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10701 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 if (histype >= 0)
10703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 if (*str != NUL)
10706 {
10707 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010708 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 return;
10710 }
10711 }
10712#endif
10713}
10714
10715/*
10716 * "histdel()" function
10717 */
10718/*ARGSUSED*/
10719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010720f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010721 typval_T *argvars;
10722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723{
10724#ifdef FEAT_CMDHIST
10725 int n;
10726 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010727 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010729 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10730 if (str == NULL)
10731 n = 0;
10732 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010734 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010735 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010737 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010738 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 else
10740 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010742 get_tv_string_buf(&argvars[1], buf));
10743 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010745 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746#endif
10747}
10748
10749/*
10750 * "histget()" function
10751 */
10752/*ARGSUSED*/
10753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010755 typval_T *argvars;
10756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757{
10758#ifdef FEAT_CMDHIST
10759 int type;
10760 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010761 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010763 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10764 if (str == NULL)
10765 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010767 {
10768 type = get_histtype(str);
10769 if (argvars[1].v_type == VAR_UNKNOWN)
10770 idx = get_history_idx(type);
10771 else
10772 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10773 /* -1 on type error */
10774 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010779 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780}
10781
10782/*
10783 * "histnr()" function
10784 */
10785/*ARGSUSED*/
10786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010787f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010788 typval_T *argvars;
10789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790{
10791 int i;
10792
10793#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010794 char_u *history = get_tv_string_chk(&argvars[0]);
10795
10796 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 if (i >= HIST_CMD && i < HIST_COUNT)
10798 i = get_history_idx(i);
10799 else
10800#endif
10801 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010802 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803}
10804
10805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 * "highlightID(name)" function
10807 */
10808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010810 typval_T *argvars;
10811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814}
10815
10816/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010817 * "highlight_exists()" function
10818 */
10819 static void
10820f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010821 typval_T *argvars;
10822 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010823{
10824 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10825}
10826
10827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 * "hostname()" function
10829 */
10830/*ARGSUSED*/
10831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010832f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010833 typval_T *argvars;
10834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835{
10836 char_u hostname[256];
10837
10838 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010839 rettv->v_type = VAR_STRING;
10840 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841}
10842
10843/*
10844 * iconv() function
10845 */
10846/*ARGSUSED*/
10847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010848f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010849 typval_T *argvars;
10850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851{
10852#ifdef FEAT_MBYTE
10853 char_u buf1[NUMBUFLEN];
10854 char_u buf2[NUMBUFLEN];
10855 char_u *from, *to, *str;
10856 vimconv_T vimconv;
10857#endif
10858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010859 rettv->v_type = VAR_STRING;
10860 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861
10862#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010863 str = get_tv_string(&argvars[0]);
10864 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10865 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 vimconv.vc_type = CONV_NONE;
10867 convert_setup(&vimconv, from, to);
10868
10869 /* If the encodings are equal, no conversion needed. */
10870 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010871 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010873 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874
10875 convert_setup(&vimconv, NULL, NULL);
10876 vim_free(from);
10877 vim_free(to);
10878#endif
10879}
10880
10881/*
10882 * "indent()" function
10883 */
10884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010886 typval_T *argvars;
10887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888{
10889 linenr_T lnum;
10890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010891 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010893 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010895 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896}
10897
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010898/*
10899 * "index()" function
10900 */
10901 static void
10902f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010903 typval_T *argvars;
10904 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010905{
Bram Moolenaar33570922005-01-25 22:26:29 +000010906 list_T *l;
10907 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010908 long idx = 0;
10909 int ic = FALSE;
10910
10911 rettv->vval.v_number = -1;
10912 if (argvars[0].v_type != VAR_LIST)
10913 {
10914 EMSG(_(e_listreq));
10915 return;
10916 }
10917 l = argvars[0].vval.v_list;
10918 if (l != NULL)
10919 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010920 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010921 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010923 int error = FALSE;
10924
Bram Moolenaar758711c2005-02-02 23:11:38 +000010925 /* Start at specified item. Use the cached index that list_find()
10926 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010927 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010928 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010929 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 ic = get_tv_number_chk(&argvars[3], &error);
10931 if (error)
10932 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010933 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010934
Bram Moolenaar758711c2005-02-02 23:11:38 +000010935 for ( ; item != NULL; item = item->li_next, ++idx)
10936 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010937 {
10938 rettv->vval.v_number = idx;
10939 break;
10940 }
10941 }
10942}
10943
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944static int inputsecret_flag = 0;
10945
10946/*
10947 * "input()" function
10948 * Also handles inputsecret() when inputsecret is set.
10949 */
10950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010951f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010952 typval_T *argvars;
10953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010955 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 char_u *p = NULL;
10957 int c;
10958 char_u buf[NUMBUFLEN];
10959 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010960 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010961 int xp_type = EXPAND_NOTHING;
10962 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965
10966#ifdef NO_CONSOLE_INPUT
10967 /* While starting up, there is no place to enter text. */
10968 if (no_console_input())
10969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010970 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 return;
10972 }
10973#endif
10974
10975 cmd_silent = FALSE; /* Want to see the prompt. */
10976 if (prompt != NULL)
10977 {
10978 /* Only the part of the message after the last NL is considered as
10979 * prompt for the command line */
10980 p = vim_strrchr(prompt, '\n');
10981 if (p == NULL)
10982 p = prompt;
10983 else
10984 {
10985 ++p;
10986 c = *p;
10987 *p = NUL;
10988 msg_start();
10989 msg_clr_eos();
10990 msg_puts_attr(prompt, echo_attr);
10991 msg_didout = FALSE;
10992 msg_starthere();
10993 *p = c;
10994 }
10995 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010997 if (argvars[1].v_type != VAR_UNKNOWN)
10998 {
10999 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11000 if (defstr != NULL)
11001 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002
Bram Moolenaar4463f292005-09-25 22:20:24 +000011003 if (argvars[2].v_type != VAR_UNKNOWN)
11004 {
11005 char_u *xp_name;
11006 int xp_namelen;
11007 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011008
Bram Moolenaar4463f292005-09-25 22:20:24 +000011009 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011010
Bram Moolenaar4463f292005-09-25 22:20:24 +000011011 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11012 if (xp_name == NULL)
11013 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011014
Bram Moolenaar4463f292005-09-25 22:20:24 +000011015 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011016
Bram Moolenaar4463f292005-09-25 22:20:24 +000011017 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11018 &xp_arg) == FAIL)
11019 return;
11020 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011021 }
11022
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011023 if (defstr != NULL)
11024 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011025 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11026 xp_type, xp_arg);
11027
11028 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011030 /* since the user typed this, no need to wait for return */
11031 need_wait_return = FALSE;
11032 msg_didout = FALSE;
11033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 cmd_silent = cmd_silent_save;
11035}
11036
11037/*
11038 * "inputdialog()" function
11039 */
11040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011042 typval_T *argvars;
11043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044{
11045#if defined(FEAT_GUI_TEXTDIALOG)
11046 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11047 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11048 {
11049 char_u *message;
11050 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011051 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011053 message = get_tv_string_chk(&argvars[0]);
11054 if (argvars[1].v_type != VAR_UNKNOWN
11055 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011056 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 else
11058 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011059 if (message != NULL && defstr != NULL
11060 && do_dialog(VIM_QUESTION, NULL, message,
11061 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 else
11064 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011065 if (message != NULL && defstr != NULL
11066 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011067 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011068 rettv->vval.v_string = vim_strsave(
11069 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 }
11075 else
11076#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078}
11079
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011080/*
11081 * "inputlist()" function
11082 */
11083 static void
11084f_inputlist(argvars, rettv)
11085 typval_T *argvars;
11086 typval_T *rettv;
11087{
11088 listitem_T *li;
11089 int selected;
11090 int mouse_used;
11091
11092 rettv->vval.v_number = 0;
11093#ifdef NO_CONSOLE_INPUT
11094 /* While starting up, there is no place to enter text. */
11095 if (no_console_input())
11096 return;
11097#endif
11098 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11099 {
11100 EMSG2(_(e_listarg), "inputlist()");
11101 return;
11102 }
11103
11104 msg_start();
11105 lines_left = Rows; /* avoid more prompt */
11106 msg_scroll = TRUE;
11107 msg_clr_eos();
11108
11109 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11110 {
11111 msg_puts(get_tv_string(&li->li_tv));
11112 msg_putchar('\n');
11113 }
11114
11115 /* Ask for choice. */
11116 selected = prompt_for_number(&mouse_used);
11117 if (mouse_used)
11118 selected -= lines_left;
11119
11120 rettv->vval.v_number = selected;
11121}
11122
11123
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11125
11126/*
11127 * "inputrestore()" function
11128 */
11129/*ARGSUSED*/
11130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011131f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011132 typval_T *argvars;
11133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134{
11135 if (ga_userinput.ga_len > 0)
11136 {
11137 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11139 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 }
11142 else if (p_verbose > 1)
11143 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011144 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011145 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 }
11147}
11148
11149/*
11150 * "inputsave()" function
11151 */
11152/*ARGSUSED*/
11153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011154f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011155 typval_T *argvars;
11156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157{
11158 /* Add an entry to the stack of typehead storage. */
11159 if (ga_grow(&ga_userinput, 1) == OK)
11160 {
11161 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11162 + ga_userinput.ga_len);
11163 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 }
11166 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011167 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168}
11169
11170/*
11171 * "inputsecret()" function
11172 */
11173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011174f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011175 typval_T *argvars;
11176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177{
11178 ++cmdline_star;
11179 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011180 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 --cmdline_star;
11182 --inputsecret_flag;
11183}
11184
11185/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011186 * "insert()" function
11187 */
11188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011189f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011190 typval_T *argvars;
11191 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011192{
11193 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011194 listitem_T *item;
11195 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011196 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011197
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011198 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011199 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011200 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011201 else if ((l = argvars[0].vval.v_list) != NULL
11202 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011203 {
11204 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011205 before = get_tv_number_chk(&argvars[2], &error);
11206 if (error)
11207 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011208
Bram Moolenaar758711c2005-02-02 23:11:38 +000011209 if (before == l->lv_len)
11210 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011211 else
11212 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011213 item = list_find(l, before);
11214 if (item == NULL)
11215 {
11216 EMSGN(_(e_listidx), before);
11217 l = NULL;
11218 }
11219 }
11220 if (l != NULL)
11221 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011222 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011223 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011224 }
11225 }
11226}
11227
11228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 * "isdirectory()" function
11230 */
11231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011233 typval_T *argvars;
11234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237}
11238
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011239/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011240 * "islocked()" function
11241 */
11242 static void
11243f_islocked(argvars, rettv)
11244 typval_T *argvars;
11245 typval_T *rettv;
11246{
11247 lval_T lv;
11248 char_u *end;
11249 dictitem_T *di;
11250
11251 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011252 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11253 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011254 if (end != NULL && lv.ll_name != NULL)
11255 {
11256 if (*end != NUL)
11257 EMSG(_(e_trailing));
11258 else
11259 {
11260 if (lv.ll_tv == NULL)
11261 {
11262 if (check_changedtick(lv.ll_name))
11263 rettv->vval.v_number = 1; /* always locked */
11264 else
11265 {
11266 di = find_var(lv.ll_name, NULL);
11267 if (di != NULL)
11268 {
11269 /* Consider a variable locked when:
11270 * 1. the variable itself is locked
11271 * 2. the value of the variable is locked.
11272 * 3. the List or Dict value is locked.
11273 */
11274 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11275 || tv_islocked(&di->di_tv));
11276 }
11277 }
11278 }
11279 else if (lv.ll_range)
11280 EMSG(_("E745: Range not allowed"));
11281 else if (lv.ll_newkey != NULL)
11282 EMSG2(_(e_dictkey), lv.ll_newkey);
11283 else if (lv.ll_list != NULL)
11284 /* List item. */
11285 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11286 else
11287 /* Dictionary item. */
11288 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11289 }
11290 }
11291
11292 clear_lval(&lv);
11293}
11294
Bram Moolenaar33570922005-01-25 22:26:29 +000011295static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011296
11297/*
11298 * Turn a dict into a list:
11299 * "what" == 0: list of keys
11300 * "what" == 1: list of values
11301 * "what" == 2: list of items
11302 */
11303 static void
11304dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011305 typval_T *argvars;
11306 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011307 int what;
11308{
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 list_T *l2;
11310 dictitem_T *di;
11311 hashitem_T *hi;
11312 listitem_T *li;
11313 listitem_T *li2;
11314 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011315 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011316
11317 rettv->vval.v_number = 0;
11318 if (argvars[0].v_type != VAR_DICT)
11319 {
11320 EMSG(_(e_dictreq));
11321 return;
11322 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011323 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011324 return;
11325
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011326 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011327 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011328
Bram Moolenaar33570922005-01-25 22:26:29 +000011329 todo = d->dv_hashtab.ht_used;
11330 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011331 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011332 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011333 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011334 --todo;
11335 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011336
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011337 li = listitem_alloc();
11338 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011339 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011340 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011341
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011342 if (what == 0)
11343 {
11344 /* keys() */
11345 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011346 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011347 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11348 }
11349 else if (what == 1)
11350 {
11351 /* values() */
11352 copy_tv(&di->di_tv, &li->li_tv);
11353 }
11354 else
11355 {
11356 /* items() */
11357 l2 = list_alloc();
11358 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011359 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011360 li->li_tv.vval.v_list = l2;
11361 if (l2 == NULL)
11362 break;
11363 ++l2->lv_refcount;
11364
11365 li2 = listitem_alloc();
11366 if (li2 == NULL)
11367 break;
11368 list_append(l2, li2);
11369 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011370 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011371 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11372
11373 li2 = listitem_alloc();
11374 if (li2 == NULL)
11375 break;
11376 list_append(l2, li2);
11377 copy_tv(&di->di_tv, &li2->li_tv);
11378 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011379 }
11380 }
11381}
11382
11383/*
11384 * "items(dict)" function
11385 */
11386 static void
11387f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011388 typval_T *argvars;
11389 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011390{
11391 dict_list(argvars, rettv, 2);
11392}
11393
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011395 * "join()" function
11396 */
11397 static void
11398f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011399 typval_T *argvars;
11400 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011401{
11402 garray_T ga;
11403 char_u *sep;
11404
11405 rettv->vval.v_number = 0;
11406 if (argvars[0].v_type != VAR_LIST)
11407 {
11408 EMSG(_(e_listreq));
11409 return;
11410 }
11411 if (argvars[0].vval.v_list == NULL)
11412 return;
11413 if (argvars[1].v_type == VAR_UNKNOWN)
11414 sep = (char_u *)" ";
11415 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011417
11418 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011419
11420 if (sep != NULL)
11421 {
11422 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011423 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011424 ga_append(&ga, NUL);
11425 rettv->vval.v_string = (char_u *)ga.ga_data;
11426 }
11427 else
11428 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011429}
11430
11431/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011432 * "keys()" function
11433 */
11434 static void
11435f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011436 typval_T *argvars;
11437 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011438{
11439 dict_list(argvars, rettv, 0);
11440}
11441
11442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 * "last_buffer_nr()" function.
11444 */
11445/*ARGSUSED*/
11446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011448 typval_T *argvars;
11449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450{
11451 int n = 0;
11452 buf_T *buf;
11453
11454 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11455 if (n < buf->b_fnum)
11456 n = buf->b_fnum;
11457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011458 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011459}
11460
11461/*
11462 * "len()" function
11463 */
11464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011465f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011466 typval_T *argvars;
11467 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011468{
11469 switch (argvars[0].v_type)
11470 {
11471 case VAR_STRING:
11472 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 rettv->vval.v_number = (varnumber_T)STRLEN(
11474 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011475 break;
11476 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011478 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011479 case VAR_DICT:
11480 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11481 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011482 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011483 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011484 break;
11485 }
11486}
11487
Bram Moolenaar33570922005-01-25 22:26:29 +000011488static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011489
11490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011492 typval_T *argvars;
11493 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011494 int type;
11495{
11496#ifdef FEAT_LIBCALL
11497 char_u *string_in;
11498 char_u **string_result;
11499 int nr_result;
11500#endif
11501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011503 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011504 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011505 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011506 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011507
11508 if (check_restricted() || check_secure())
11509 return;
11510
11511#ifdef FEAT_LIBCALL
11512 /* The first two args must be strings, otherwise its meaningless */
11513 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11514 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011515 string_in = NULL;
11516 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011517 string_in = argvars[2].vval.v_string;
11518 if (type == VAR_NUMBER)
11519 string_result = NULL;
11520 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011522 if (mch_libcall(argvars[0].vval.v_string,
11523 argvars[1].vval.v_string,
11524 string_in,
11525 argvars[2].vval.v_number,
11526 string_result,
11527 &nr_result) == OK
11528 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011530 }
11531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532}
11533
11534/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011535 * "libcall()" function
11536 */
11537 static void
11538f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011539 typval_T *argvars;
11540 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011541{
11542 libcall_common(argvars, rettv, VAR_STRING);
11543}
11544
11545/*
11546 * "libcallnr()" function
11547 */
11548 static void
11549f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011550 typval_T *argvars;
11551 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011552{
11553 libcall_common(argvars, rettv, VAR_NUMBER);
11554}
11555
11556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 * "line(string)" function
11558 */
11559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011561 typval_T *argvars;
11562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563{
11564 linenr_T lnum = 0;
11565 pos_T *fp;
11566
11567 fp = var2fpos(&argvars[0], TRUE);
11568 if (fp != NULL)
11569 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571}
11572
11573/*
11574 * "line2byte(lnum)" function
11575 */
11576/*ARGSUSED*/
11577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011578f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011579 typval_T *argvars;
11580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581{
11582#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011583 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584#else
11585 linenr_T lnum;
11586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011591 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11592 if (rettv->vval.v_number >= 0)
11593 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594#endif
11595}
11596
11597/*
11598 * "lispindent(lnum)" function
11599 */
11600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011601f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011602 typval_T *argvars;
11603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604{
11605#ifdef FEAT_LISP
11606 pos_T pos;
11607 linenr_T lnum;
11608
11609 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011610 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11612 {
11613 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 curwin->w_cursor = pos;
11616 }
11617 else
11618#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620}
11621
11622/*
11623 * "localtime()" function
11624 */
11625/*ARGSUSED*/
11626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011628 typval_T *argvars;
11629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011631 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632}
11633
Bram Moolenaar33570922005-01-25 22:26:29 +000011634static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635
11636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011637get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011638 typval_T *argvars;
11639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640 int exact;
11641{
11642 char_u *keys;
11643 char_u *which;
11644 char_u buf[NUMBUFLEN];
11645 char_u *keys_buf = NULL;
11646 char_u *rhs;
11647 int mode;
11648 garray_T ga;
11649
11650 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 rettv->v_type = VAR_STRING;
11652 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011654 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655 if (*keys == NUL)
11656 return;
11657
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011658 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011659 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 else
11661 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011662 if (which == NULL)
11663 return;
11664
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665 mode = get_map_mode(&which, 0);
11666
11667 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011668 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 vim_free(keys_buf);
11670 if (rhs != NULL)
11671 {
11672 ga_init(&ga);
11673 ga.ga_itemsize = 1;
11674 ga.ga_growsize = 40;
11675
11676 while (*rhs != NUL)
11677 ga_concat(&ga, str2special(&rhs, FALSE));
11678
11679 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011680 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 }
11682}
11683
11684/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011685 * "map()" function
11686 */
11687 static void
11688f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011689 typval_T *argvars;
11690 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011691{
11692 filter_map(argvars, rettv, TRUE);
11693}
11694
11695/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011696 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 */
11698 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011699f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011700 typval_T *argvars;
11701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011703 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704}
11705
11706/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011707 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708 */
11709 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011710f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011711 typval_T *argvars;
11712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011714 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715}
11716
Bram Moolenaar33570922005-01-25 22:26:29 +000011717static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718
11719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011720find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011721 typval_T *argvars;
11722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723 int type;
11724{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011725 char_u *str = NULL;
11726 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 char_u *pat;
11728 regmatch_T regmatch;
11729 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011730 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 char_u *save_cpo;
11732 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011733 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011734 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011735 list_T *l = NULL;
11736 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011737 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011738 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739
11740 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11741 save_cpo = p_cpo;
11742 p_cpo = (char_u *)"";
11743
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011744 rettv->vval.v_number = -1;
11745 if (type == 3)
11746 {
11747 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011748 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011749 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011750 }
11751 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->v_type = VAR_STRING;
11754 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011757 if (argvars[0].v_type == VAR_LIST)
11758 {
11759 if ((l = argvars[0].vval.v_list) == NULL)
11760 goto theend;
11761 li = l->lv_first;
11762 }
11763 else
11764 expr = str = get_tv_string(&argvars[0]);
11765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011766 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11767 if (pat == NULL)
11768 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011769
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011770 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011772 int error = FALSE;
11773
11774 start = get_tv_number_chk(&argvars[2], &error);
11775 if (error)
11776 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011777 if (l != NULL)
11778 {
11779 li = list_find(l, start);
11780 if (li == NULL)
11781 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011782 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011783 }
11784 else
11785 {
11786 if (start < 0)
11787 start = 0;
11788 if (start > (long)STRLEN(str))
11789 goto theend;
11790 str += start;
11791 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011792
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011793 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011794 nth = get_tv_number_chk(&argvars[3], &error);
11795 if (error)
11796 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 }
11798
11799 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11800 if (regmatch.regprog != NULL)
11801 {
11802 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011803
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011804 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011805 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011806 if (l != NULL)
11807 {
11808 if (li == NULL)
11809 {
11810 match = FALSE;
11811 break;
11812 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011813 vim_free(tofree);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011814 str = echo_string(&li->li_tv, &tofree, strbuf,0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011815 if (str == NULL)
11816 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011817 }
11818
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011819 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011820
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011821 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011822 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011823 if (l == NULL && !match)
11824 break;
11825
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011826 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011827 if (l != NULL)
11828 {
11829 li = li->li_next;
11830 ++idx;
11831 }
11832 else
11833 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011834#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011835 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011836#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011837 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011838#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011839 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011840 }
11841
11842 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011844 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011845 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011846 int i;
11847
11848 /* return list with matched string and submatches */
11849 for (i = 0; i < NSUBEXP; ++i)
11850 {
11851 if (regmatch.endp[i] == NULL)
11852 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011853 if (list_append_string(rettv->vval.v_list,
11854 regmatch.startp[i],
11855 (int)(regmatch.endp[i] - regmatch.startp[i]))
11856 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011857 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011858 }
11859 }
11860 else if (type == 2)
11861 {
11862 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011863 if (l != NULL)
11864 copy_tv(&li->li_tv, rettv);
11865 else
11866 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011868 }
11869 else if (l != NULL)
11870 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871 else
11872 {
11873 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011874 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875 (varnumber_T)(regmatch.startp[0] - str);
11876 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011877 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880 }
11881 }
11882 vim_free(regmatch.regprog);
11883 }
11884
11885theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011886 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887 p_cpo = save_cpo;
11888}
11889
11890/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011891 * "match()" function
11892 */
11893 static void
11894f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011895 typval_T *argvars;
11896 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011897{
11898 find_some_match(argvars, rettv, 1);
11899}
11900
11901/*
11902 * "matchend()" function
11903 */
11904 static void
11905f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011906 typval_T *argvars;
11907 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011908{
11909 find_some_match(argvars, rettv, 0);
11910}
11911
11912/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011913 * "matchlist()" function
11914 */
11915 static void
11916f_matchlist(argvars, rettv)
11917 typval_T *argvars;
11918 typval_T *rettv;
11919{
11920 find_some_match(argvars, rettv, 3);
11921}
11922
11923/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011924 * "matchstr()" function
11925 */
11926 static void
11927f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011928 typval_T *argvars;
11929 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011930{
11931 find_some_match(argvars, rettv, 2);
11932}
11933
Bram Moolenaar33570922005-01-25 22:26:29 +000011934static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011935
11936 static void
11937max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011938 typval_T *argvars;
11939 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011940 int domax;
11941{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011942 long n = 0;
11943 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011944 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011945
11946 if (argvars[0].v_type == VAR_LIST)
11947 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011948 list_T *l;
11949 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011950
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011951 l = argvars[0].vval.v_list;
11952 if (l != NULL)
11953 {
11954 li = l->lv_first;
11955 if (li != NULL)
11956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011957 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011958 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011959 {
11960 li = li->li_next;
11961 if (li == NULL)
11962 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011963 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011964 if (domax ? i > n : i < n)
11965 n = i;
11966 }
11967 }
11968 }
11969 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011970 else if (argvars[0].v_type == VAR_DICT)
11971 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011972 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011973 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011974 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011975 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011976
11977 d = argvars[0].vval.v_dict;
11978 if (d != NULL)
11979 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011980 todo = d->dv_hashtab.ht_used;
11981 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011982 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011983 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011984 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011985 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011986 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011987 if (first)
11988 {
11989 n = i;
11990 first = FALSE;
11991 }
11992 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011993 n = i;
11994 }
11995 }
11996 }
11997 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011998 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011999 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012000 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012001}
12002
12003/*
12004 * "max()" function
12005 */
12006 static void
12007f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012008 typval_T *argvars;
12009 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012010{
12011 max_min(argvars, rettv, TRUE);
12012}
12013
12014/*
12015 * "min()" function
12016 */
12017 static void
12018f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012019 typval_T *argvars;
12020 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012021{
12022 max_min(argvars, rettv, FALSE);
12023}
12024
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012025static int mkdir_recurse __ARGS((char_u *dir, int prot));
12026
12027/*
12028 * Create the directory in which "dir" is located, and higher levels when
12029 * needed.
12030 */
12031 static int
12032mkdir_recurse(dir, prot)
12033 char_u *dir;
12034 int prot;
12035{
12036 char_u *p;
12037 char_u *updir;
12038 int r = FAIL;
12039
12040 /* Get end of directory name in "dir".
12041 * We're done when it's "/" or "c:/". */
12042 p = gettail_sep(dir);
12043 if (p <= get_past_head(dir))
12044 return OK;
12045
12046 /* If the directory exists we're done. Otherwise: create it.*/
12047 updir = vim_strnsave(dir, (int)(p - dir));
12048 if (updir == NULL)
12049 return FAIL;
12050 if (mch_isdir(updir))
12051 r = OK;
12052 else if (mkdir_recurse(updir, prot) == OK)
12053 r = vim_mkdir_emsg(updir, prot);
12054 vim_free(updir);
12055 return r;
12056}
12057
12058#ifdef vim_mkdir
12059/*
12060 * "mkdir()" function
12061 */
12062 static void
12063f_mkdir(argvars, rettv)
12064 typval_T *argvars;
12065 typval_T *rettv;
12066{
12067 char_u *dir;
12068 char_u buf[NUMBUFLEN];
12069 int prot = 0755;
12070
12071 rettv->vval.v_number = FAIL;
12072 if (check_restricted() || check_secure())
12073 return;
12074
12075 dir = get_tv_string_buf(&argvars[0], buf);
12076 if (argvars[1].v_type != VAR_UNKNOWN)
12077 {
12078 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012079 prot = get_tv_number_chk(&argvars[2], NULL);
12080 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012081 mkdir_recurse(dir, prot);
12082 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012083 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012084}
12085#endif
12086
Bram Moolenaar0d660222005-01-07 21:51:51 +000012087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088 * "mode()" function
12089 */
12090/*ARGSUSED*/
12091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012093 typval_T *argvars;
12094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095{
12096 char_u buf[2];
12097
12098#ifdef FEAT_VISUAL
12099 if (VIsual_active)
12100 {
12101 if (VIsual_select)
12102 buf[0] = VIsual_mode + 's' - 'v';
12103 else
12104 buf[0] = VIsual_mode;
12105 }
12106 else
12107#endif
12108 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12109 buf[0] = 'r';
12110 else if (State & INSERT)
12111 {
12112 if (State & REPLACE_FLAG)
12113 buf[0] = 'R';
12114 else
12115 buf[0] = 'i';
12116 }
12117 else if (State & CMDLINE)
12118 buf[0] = 'c';
12119 else
12120 buf[0] = 'n';
12121
12122 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123 rettv->vval.v_string = vim_strsave(buf);
12124 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125}
12126
12127/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012128 * "nextnonblank()" function
12129 */
12130 static void
12131f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012132 typval_T *argvars;
12133 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012134{
12135 linenr_T lnum;
12136
12137 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12138 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012139 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012140 {
12141 lnum = 0;
12142 break;
12143 }
12144 if (*skipwhite(ml_get(lnum)) != NUL)
12145 break;
12146 }
12147 rettv->vval.v_number = lnum;
12148}
12149
12150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 * "nr2char()" function
12152 */
12153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012154f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012155 typval_T *argvars;
12156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157{
12158 char_u buf[NUMBUFLEN];
12159
12160#ifdef FEAT_MBYTE
12161 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012162 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163 else
12164#endif
12165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012166 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167 buf[1] = NUL;
12168 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169 rettv->v_type = VAR_STRING;
12170 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012171}
12172
12173/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012174 * "prevnonblank()" function
12175 */
12176 static void
12177f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012178 typval_T *argvars;
12179 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012180{
12181 linenr_T lnum;
12182
12183 lnum = get_tv_lnum(argvars);
12184 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12185 lnum = 0;
12186 else
12187 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12188 --lnum;
12189 rettv->vval.v_number = lnum;
12190}
12191
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012192#ifdef HAVE_STDARG_H
12193/* This dummy va_list is here because:
12194 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12195 * - locally in the function results in a "used before set" warning
12196 * - using va_start() to initialize it gives "function with fixed args" error */
12197static va_list ap;
12198#endif
12199
Bram Moolenaar8c711452005-01-14 21:53:12 +000012200/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012201 * "printf()" function
12202 */
12203 static void
12204f_printf(argvars, rettv)
12205 typval_T *argvars;
12206 typval_T *rettv;
12207{
12208 rettv->v_type = VAR_STRING;
12209 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012210#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012211 {
12212 char_u buf[NUMBUFLEN];
12213 int len;
12214 char_u *s;
12215 int saved_did_emsg = did_emsg;
12216 char *fmt;
12217
12218 /* Get the required length, allocate the buffer and do it for real. */
12219 did_emsg = FALSE;
12220 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012221 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012222 if (!did_emsg)
12223 {
12224 s = alloc(len + 1);
12225 if (s != NULL)
12226 {
12227 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012228 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012229 }
12230 }
12231 did_emsg |= saved_did_emsg;
12232 }
12233#endif
12234}
12235
12236/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012237 * "pumvisible()" function
12238 */
12239/*ARGSUSED*/
12240 static void
12241f_pumvisible(argvars, rettv)
12242 typval_T *argvars;
12243 typval_T *rettv;
12244{
12245 rettv->vval.v_number = 0;
12246#ifdef FEAT_INS_EXPAND
12247 if (pum_visible())
12248 rettv->vval.v_number = 1;
12249#endif
12250}
12251
12252/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012253 * "range()" function
12254 */
12255 static void
12256f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012257 typval_T *argvars;
12258 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012259{
12260 long start;
12261 long end;
12262 long stride = 1;
12263 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012264 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012266 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012267 if (argvars[1].v_type == VAR_UNKNOWN)
12268 {
12269 end = start - 1;
12270 start = 0;
12271 }
12272 else
12273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012274 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012275 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012276 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012277 }
12278
12279 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012280 if (error)
12281 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012282 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012283 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012284 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012285 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012286 else
12287 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012288 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012289 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012290 if (list_append_number(rettv->vval.v_list,
12291 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012292 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012293 }
12294}
12295
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012296/*
12297 * "readfile()" function
12298 */
12299 static void
12300f_readfile(argvars, rettv)
12301 typval_T *argvars;
12302 typval_T *rettv;
12303{
12304 int binary = FALSE;
12305 char_u *fname;
12306 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012307 listitem_T *li;
12308#define FREAD_SIZE 200 /* optimized for text lines */
12309 char_u buf[FREAD_SIZE];
12310 int readlen; /* size of last fread() */
12311 int buflen; /* nr of valid chars in buf[] */
12312 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12313 int tolist; /* first byte in buf[] still to be put in list */
12314 int chop; /* how many CR to chop off */
12315 char_u *prev = NULL; /* previously read bytes, if any */
12316 int prevlen = 0; /* length of "prev" if not NULL */
12317 char_u *s;
12318 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012319 long maxline = MAXLNUM;
12320 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012321
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012322 if (argvars[1].v_type != VAR_UNKNOWN)
12323 {
12324 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12325 binary = TRUE;
12326 if (argvars[2].v_type != VAR_UNKNOWN)
12327 maxline = get_tv_number(&argvars[2]);
12328 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012329
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012330 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012331 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012332
12333 /* Always open the file in binary mode, library functions have a mind of
12334 * their own about CR-LF conversion. */
12335 fname = get_tv_string(&argvars[0]);
12336 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12337 {
12338 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12339 return;
12340 }
12341
12342 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012343 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012344 {
12345 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12346 buflen = filtd + readlen;
12347 tolist = 0;
12348 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12349 {
12350 if (buf[filtd] == '\n' || readlen <= 0)
12351 {
12352 /* Only when in binary mode add an empty list item when the
12353 * last line ends in a '\n'. */
12354 if (!binary && readlen == 0 && filtd == 0)
12355 break;
12356
12357 /* Found end-of-line or end-of-file: add a text line to the
12358 * list. */
12359 chop = 0;
12360 if (!binary)
12361 while (filtd - chop - 1 >= tolist
12362 && buf[filtd - chop - 1] == '\r')
12363 ++chop;
12364 len = filtd - tolist - chop;
12365 if (prev == NULL)
12366 s = vim_strnsave(buf + tolist, len);
12367 else
12368 {
12369 s = alloc((unsigned)(prevlen + len + 1));
12370 if (s != NULL)
12371 {
12372 mch_memmove(s, prev, prevlen);
12373 vim_free(prev);
12374 prev = NULL;
12375 mch_memmove(s + prevlen, buf + tolist, len);
12376 s[prevlen + len] = NUL;
12377 }
12378 }
12379 tolist = filtd + 1;
12380
12381 li = listitem_alloc();
12382 if (li == NULL)
12383 {
12384 vim_free(s);
12385 break;
12386 }
12387 li->li_tv.v_type = VAR_STRING;
12388 li->li_tv.v_lock = 0;
12389 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012390 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012391
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012392 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012393 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012394 if (readlen <= 0)
12395 break;
12396 }
12397 else if (buf[filtd] == NUL)
12398 buf[filtd] = '\n';
12399 }
12400 if (readlen <= 0)
12401 break;
12402
12403 if (tolist == 0)
12404 {
12405 /* "buf" is full, need to move text to an allocated buffer */
12406 if (prev == NULL)
12407 {
12408 prev = vim_strnsave(buf, buflen);
12409 prevlen = buflen;
12410 }
12411 else
12412 {
12413 s = alloc((unsigned)(prevlen + buflen));
12414 if (s != NULL)
12415 {
12416 mch_memmove(s, prev, prevlen);
12417 mch_memmove(s + prevlen, buf, buflen);
12418 vim_free(prev);
12419 prev = s;
12420 prevlen += buflen;
12421 }
12422 }
12423 filtd = 0;
12424 }
12425 else
12426 {
12427 mch_memmove(buf, buf + tolist, buflen - tolist);
12428 filtd -= tolist;
12429 }
12430 }
12431
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012432 /*
12433 * For a negative line count use only the lines at the end of the file,
12434 * free the rest.
12435 */
12436 if (maxline < 0)
12437 while (cnt > -maxline)
12438 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012439 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012440 --cnt;
12441 }
12442
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012443 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012444 fclose(fd);
12445}
12446
12447
Bram Moolenaar0d660222005-01-07 21:51:51 +000012448#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12449static void make_connection __ARGS((void));
12450static int check_connection __ARGS((void));
12451
12452 static void
12453make_connection()
12454{
12455 if (X_DISPLAY == NULL
12456# ifdef FEAT_GUI
12457 && !gui.in_use
12458# endif
12459 )
12460 {
12461 x_force_connect = TRUE;
12462 setup_term_clip();
12463 x_force_connect = FALSE;
12464 }
12465}
12466
12467 static int
12468check_connection()
12469{
12470 make_connection();
12471 if (X_DISPLAY == NULL)
12472 {
12473 EMSG(_("E240: No connection to Vim server"));
12474 return FAIL;
12475 }
12476 return OK;
12477}
12478#endif
12479
12480#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012481static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012482
12483 static void
12484remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012485 typval_T *argvars;
12486 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012487 int expr;
12488{
12489 char_u *server_name;
12490 char_u *keys;
12491 char_u *r = NULL;
12492 char_u buf[NUMBUFLEN];
12493# ifdef WIN32
12494 HWND w;
12495# else
12496 Window w;
12497# endif
12498
12499 if (check_restricted() || check_secure())
12500 return;
12501
12502# ifdef FEAT_X11
12503 if (check_connection() == FAIL)
12504 return;
12505# endif
12506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012507 server_name = get_tv_string_chk(&argvars[0]);
12508 if (server_name == NULL)
12509 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012510 keys = get_tv_string_buf(&argvars[1], buf);
12511# ifdef WIN32
12512 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12513# else
12514 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12515 < 0)
12516# endif
12517 {
12518 if (r != NULL)
12519 EMSG(r); /* sending worked but evaluation failed */
12520 else
12521 EMSG2(_("E241: Unable to send to %s"), server_name);
12522 return;
12523 }
12524
12525 rettv->vval.v_string = r;
12526
12527 if (argvars[2].v_type != VAR_UNKNOWN)
12528 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012529 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012530 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012531 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012532
12533 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012534 v.di_tv.v_type = VAR_STRING;
12535 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012536 idvar = get_tv_string_chk(&argvars[2]);
12537 if (idvar != NULL)
12538 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012539 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540 }
12541}
12542#endif
12543
12544/*
12545 * "remote_expr()" function
12546 */
12547/*ARGSUSED*/
12548 static void
12549f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012550 typval_T *argvars;
12551 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012552{
12553 rettv->v_type = VAR_STRING;
12554 rettv->vval.v_string = NULL;
12555#ifdef FEAT_CLIENTSERVER
12556 remote_common(argvars, rettv, TRUE);
12557#endif
12558}
12559
12560/*
12561 * "remote_foreground()" function
12562 */
12563/*ARGSUSED*/
12564 static void
12565f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012566 typval_T *argvars;
12567 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012568{
12569 rettv->vval.v_number = 0;
12570#ifdef FEAT_CLIENTSERVER
12571# ifdef WIN32
12572 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012573 {
12574 char_u *server_name = get_tv_string_chk(&argvars[0]);
12575
12576 if (server_name != NULL)
12577 serverForeground(server_name);
12578 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012579# else
12580 /* Send a foreground() expression to the server. */
12581 argvars[1].v_type = VAR_STRING;
12582 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12583 argvars[2].v_type = VAR_UNKNOWN;
12584 remote_common(argvars, rettv, TRUE);
12585 vim_free(argvars[1].vval.v_string);
12586# endif
12587#endif
12588}
12589
12590/*ARGSUSED*/
12591 static void
12592f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012593 typval_T *argvars;
12594 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012595{
12596#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012597 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012598 char_u *s = NULL;
12599# ifdef WIN32
12600 int n = 0;
12601# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012602 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012603
12604 if (check_restricted() || check_secure())
12605 {
12606 rettv->vval.v_number = -1;
12607 return;
12608 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012609 serverid = get_tv_string_chk(&argvars[0]);
12610 if (serverid == NULL)
12611 {
12612 rettv->vval.v_number = -1;
12613 return; /* type error; errmsg already given */
12614 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012615# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012616 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012617 if (n == 0)
12618 rettv->vval.v_number = -1;
12619 else
12620 {
12621 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12622 rettv->vval.v_number = (s != NULL);
12623 }
12624# else
12625 rettv->vval.v_number = 0;
12626 if (check_connection() == FAIL)
12627 return;
12628
12629 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012630 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012631# endif
12632
12633 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012635 char_u *retvar;
12636
Bram Moolenaar33570922005-01-25 22:26:29 +000012637 v.di_tv.v_type = VAR_STRING;
12638 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012639 retvar = get_tv_string_chk(&argvars[1]);
12640 if (retvar != NULL)
12641 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012642 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012643 }
12644#else
12645 rettv->vval.v_number = -1;
12646#endif
12647}
12648
12649/*ARGSUSED*/
12650 static void
12651f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012652 typval_T *argvars;
12653 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012654{
12655 char_u *r = NULL;
12656
12657#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012658 char_u *serverid = get_tv_string_chk(&argvars[0]);
12659
12660 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012661 {
12662# ifdef WIN32
12663 /* The server's HWND is encoded in the 'id' parameter */
12664 int n = 0;
12665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012666 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012667 if (n != 0)
12668 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12669 if (r == NULL)
12670# else
12671 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012672 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012673# endif
12674 EMSG(_("E277: Unable to read a server reply"));
12675 }
12676#endif
12677 rettv->v_type = VAR_STRING;
12678 rettv->vval.v_string = r;
12679}
12680
12681/*
12682 * "remote_send()" function
12683 */
12684/*ARGSUSED*/
12685 static void
12686f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012687 typval_T *argvars;
12688 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012689{
12690 rettv->v_type = VAR_STRING;
12691 rettv->vval.v_string = NULL;
12692#ifdef FEAT_CLIENTSERVER
12693 remote_common(argvars, rettv, FALSE);
12694#endif
12695}
12696
12697/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012698 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012699 */
12700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012701f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012702 typval_T *argvars;
12703 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012704{
Bram Moolenaar33570922005-01-25 22:26:29 +000012705 list_T *l;
12706 listitem_T *item, *item2;
12707 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012708 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012709 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012710 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012711 dict_T *d;
12712 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012713
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012714 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012715 if (argvars[0].v_type == VAR_DICT)
12716 {
12717 if (argvars[2].v_type != VAR_UNKNOWN)
12718 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012719 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012720 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012722 key = get_tv_string_chk(&argvars[1]);
12723 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012725 di = dict_find(d, key, -1);
12726 if (di == NULL)
12727 EMSG2(_(e_dictkey), key);
12728 else
12729 {
12730 *rettv = di->di_tv;
12731 init_tv(&di->di_tv);
12732 dictitem_remove(d, di);
12733 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012735 }
12736 }
12737 else if (argvars[0].v_type != VAR_LIST)
12738 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012739 else if ((l = argvars[0].vval.v_list) != NULL
12740 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012742 int error = FALSE;
12743
12744 idx = get_tv_number_chk(&argvars[1], &error);
12745 if (error)
12746 ; /* type error: do nothing, errmsg already given */
12747 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012748 EMSGN(_(e_listidx), idx);
12749 else
12750 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012751 if (argvars[2].v_type == VAR_UNKNOWN)
12752 {
12753 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012754 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012755 *rettv = item->li_tv;
12756 vim_free(item);
12757 }
12758 else
12759 {
12760 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012761 end = get_tv_number_chk(&argvars[2], &error);
12762 if (error)
12763 ; /* type error: do nothing */
12764 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012765 EMSGN(_(e_listidx), end);
12766 else
12767 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012768 int cnt = 0;
12769
12770 for (li = item; li != NULL; li = li->li_next)
12771 {
12772 ++cnt;
12773 if (li == item2)
12774 break;
12775 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012776 if (li == NULL) /* didn't find "item2" after "item" */
12777 EMSG(_(e_invrange));
12778 else
12779 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012780 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012781 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012782 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012783 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012784 l->lv_first = item;
12785 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012786 item->li_prev = NULL;
12787 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012788 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012789 }
12790 }
12791 }
12792 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012793 }
12794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795}
12796
12797/*
12798 * "rename({from}, {to})" function
12799 */
12800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012802 typval_T *argvars;
12803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804{
12805 char_u buf[NUMBUFLEN];
12806
12807 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12811 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812}
12813
12814/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012815 * "repeat()" function
12816 */
12817/*ARGSUSED*/
12818 static void
12819f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012820 typval_T *argvars;
12821 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012822{
12823 char_u *p;
12824 int n;
12825 int slen;
12826 int len;
12827 char_u *r;
12828 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012829
12830 n = get_tv_number(&argvars[1]);
12831 if (argvars[0].v_type == VAR_LIST)
12832 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012833 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012834 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012835 if (list_extend(rettv->vval.v_list,
12836 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012837 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012838 }
12839 else
12840 {
12841 p = get_tv_string(&argvars[0]);
12842 rettv->v_type = VAR_STRING;
12843 rettv->vval.v_string = NULL;
12844
12845 slen = (int)STRLEN(p);
12846 len = slen * n;
12847 if (len <= 0)
12848 return;
12849
12850 r = alloc(len + 1);
12851 if (r != NULL)
12852 {
12853 for (i = 0; i < n; i++)
12854 mch_memmove(r + i * slen, p, (size_t)slen);
12855 r[len] = NUL;
12856 }
12857
12858 rettv->vval.v_string = r;
12859 }
12860}
12861
12862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863 * "resolve()" function
12864 */
12865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012866f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012867 typval_T *argvars;
12868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869{
12870 char_u *p;
12871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873#ifdef FEAT_SHORTCUT
12874 {
12875 char_u *v = NULL;
12876
12877 v = mch_resolve_shortcut(p);
12878 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882 }
12883#else
12884# ifdef HAVE_READLINK
12885 {
12886 char_u buf[MAXPATHL + 1];
12887 char_u *cpy;
12888 int len;
12889 char_u *remain = NULL;
12890 char_u *q;
12891 int is_relative_to_current = FALSE;
12892 int has_trailing_pathsep = FALSE;
12893 int limit = 100;
12894
12895 p = vim_strsave(p);
12896
12897 if (p[0] == '.' && (vim_ispathsep(p[1])
12898 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12899 is_relative_to_current = TRUE;
12900
12901 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012902 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903 has_trailing_pathsep = TRUE;
12904
12905 q = getnextcomp(p);
12906 if (*q != NUL)
12907 {
12908 /* Separate the first path component in "p", and keep the
12909 * remainder (beginning with the path separator). */
12910 remain = vim_strsave(q - 1);
12911 q[-1] = NUL;
12912 }
12913
12914 for (;;)
12915 {
12916 for (;;)
12917 {
12918 len = readlink((char *)p, (char *)buf, MAXPATHL);
12919 if (len <= 0)
12920 break;
12921 buf[len] = NUL;
12922
12923 if (limit-- == 0)
12924 {
12925 vim_free(p);
12926 vim_free(remain);
12927 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 goto fail;
12930 }
12931
12932 /* Ensure that the result will have a trailing path separator
12933 * if the argument has one. */
12934 if (remain == NULL && has_trailing_pathsep)
12935 add_pathsep(buf);
12936
12937 /* Separate the first path component in the link value and
12938 * concatenate the remainders. */
12939 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12940 if (*q != NUL)
12941 {
12942 if (remain == NULL)
12943 remain = vim_strsave(q - 1);
12944 else
12945 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000012946 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947 if (cpy != NULL)
12948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949 vim_free(remain);
12950 remain = cpy;
12951 }
12952 }
12953 q[-1] = NUL;
12954 }
12955
12956 q = gettail(p);
12957 if (q > p && *q == NUL)
12958 {
12959 /* Ignore trailing path separator. */
12960 q[-1] = NUL;
12961 q = gettail(p);
12962 }
12963 if (q > p && !mch_isFullName(buf))
12964 {
12965 /* symlink is relative to directory of argument */
12966 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12967 if (cpy != NULL)
12968 {
12969 STRCPY(cpy, p);
12970 STRCPY(gettail(cpy), buf);
12971 vim_free(p);
12972 p = cpy;
12973 }
12974 }
12975 else
12976 {
12977 vim_free(p);
12978 p = vim_strsave(buf);
12979 }
12980 }
12981
12982 if (remain == NULL)
12983 break;
12984
12985 /* Append the first path component of "remain" to "p". */
12986 q = getnextcomp(remain + 1);
12987 len = q - remain - (*q != NUL);
12988 cpy = vim_strnsave(p, STRLEN(p) + len);
12989 if (cpy != NULL)
12990 {
12991 STRNCAT(cpy, remain, len);
12992 vim_free(p);
12993 p = cpy;
12994 }
12995 /* Shorten "remain". */
12996 if (*q != NUL)
12997 STRCPY(remain, q - 1);
12998 else
12999 {
13000 vim_free(remain);
13001 remain = NULL;
13002 }
13003 }
13004
13005 /* If the result is a relative path name, make it explicitly relative to
13006 * the current directory if and only if the argument had this form. */
13007 if (!vim_ispathsep(*p))
13008 {
13009 if (is_relative_to_current
13010 && *p != NUL
13011 && !(p[0] == '.'
13012 && (p[1] == NUL
13013 || vim_ispathsep(p[1])
13014 || (p[1] == '.'
13015 && (p[2] == NUL
13016 || vim_ispathsep(p[2]))))))
13017 {
13018 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013019 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020 if (cpy != NULL)
13021 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022 vim_free(p);
13023 p = cpy;
13024 }
13025 }
13026 else if (!is_relative_to_current)
13027 {
13028 /* Strip leading "./". */
13029 q = p;
13030 while (q[0] == '.' && vim_ispathsep(q[1]))
13031 q += 2;
13032 if (q > p)
13033 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13034 }
13035 }
13036
13037 /* Ensure that the result will have no trailing path separator
13038 * if the argument had none. But keep "/" or "//". */
13039 if (!has_trailing_pathsep)
13040 {
13041 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013042 if (after_pathsep(p, q))
13043 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 }
13045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013046 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047 }
13048# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013049 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050# endif
13051#endif
13052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013053 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054
13055#ifdef HAVE_READLINK
13056fail:
13057#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013058 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059}
13060
13061/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013062 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063 */
13064 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013065f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013066 typval_T *argvars;
13067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068{
Bram Moolenaar33570922005-01-25 22:26:29 +000013069 list_T *l;
13070 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071
Bram Moolenaar0d660222005-01-07 21:51:51 +000013072 rettv->vval.v_number = 0;
13073 if (argvars[0].v_type != VAR_LIST)
13074 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013075 else if ((l = argvars[0].vval.v_list) != NULL
13076 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013077 {
13078 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013079 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013080 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013081 while (li != NULL)
13082 {
13083 ni = li->li_prev;
13084 list_append(l, li);
13085 li = ni;
13086 }
13087 rettv->vval.v_list = l;
13088 rettv->v_type = VAR_LIST;
13089 ++l->lv_refcount;
13090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091}
13092
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013093#define SP_NOMOVE 1 /* don't move cursor */
13094#define SP_REPEAT 2 /* repeat to find outer pair */
13095#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000013096#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013097
Bram Moolenaar33570922005-01-25 22:26:29 +000013098static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013099
13100/*
13101 * Get flags for a search function.
13102 * Possibly sets "p_ws".
13103 * Returns BACKWARD, FORWARD or zero (for an error).
13104 */
13105 static int
13106get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013107 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013108 int *flagsp;
13109{
13110 int dir = FORWARD;
13111 char_u *flags;
13112 char_u nbuf[NUMBUFLEN];
13113 int mask;
13114
13115 if (varp->v_type != VAR_UNKNOWN)
13116 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013117 flags = get_tv_string_buf_chk(varp, nbuf);
13118 if (flags == NULL)
13119 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013120 while (*flags != NUL)
13121 {
13122 switch (*flags)
13123 {
13124 case 'b': dir = BACKWARD; break;
13125 case 'w': p_ws = TRUE; break;
13126 case 'W': p_ws = FALSE; break;
13127 default: mask = 0;
13128 if (flagsp != NULL)
13129 switch (*flags)
13130 {
13131 case 'n': mask = SP_NOMOVE; break;
13132 case 'r': mask = SP_REPEAT; break;
13133 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013134 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013135 }
13136 if (mask == 0)
13137 {
13138 EMSG2(_(e_invarg2), flags);
13139 dir = 0;
13140 }
13141 else
13142 *flagsp |= mask;
13143 }
13144 if (dir == 0)
13145 break;
13146 ++flags;
13147 }
13148 }
13149 return dir;
13150}
13151
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013153 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013155 static int
13156search_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013157 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013158 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159{
13160 char_u *pat;
13161 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013162 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 int save_p_ws = p_ws;
13164 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013165 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013166 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013167 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013169 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013170 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13171 if (dir == 0)
13172 goto theend;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013173
13174 /* Optional extra argument: line number to stop searching. */
13175 if (argvars[1].v_type != VAR_UNKNOWN
13176 && argvars[2].v_type != VAR_UNKNOWN)
13177 {
13178 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13179 if (lnum_stop < 0)
13180 goto theend;
13181 }
13182
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,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013198 SEARCH_KEEP, RE_SEARCH, (linenr_T)lnum_stop) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199 {
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 Moolenaareddf53b2006-02-27 00:11:10 +000013279 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013282 spat = get_tv_string_chk(&argvars[0]);
13283 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13284 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13285 if (spat == NULL || mpat == NULL || epat == NULL)
13286 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288 /* Handle the optional fourth argument: flags */
13289 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013290 if (dir == 0)
13291 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013292 /*
13293 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13294 */
13295 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13296 {
13297 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13298 goto theend;
13299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013301 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013302 if (argvars[3].v_type == VAR_UNKNOWN
13303 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 skip = (char_u *)"";
13305 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013307 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013308 if (argvars[5].v_type != VAR_UNKNOWN)
13309 {
13310 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13311 if (lnum_stop < 0)
13312 goto theend;
13313 }
13314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013315 if (skip == NULL)
13316 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013318 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13319 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013320
13321theend:
13322 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013323
13324 return retval;
13325}
13326
13327/*
13328 * "searchpair()" function
13329 */
13330 static void
13331f_searchpair(argvars, rettv)
13332 typval_T *argvars;
13333 typval_T *rettv;
13334{
13335 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13336}
13337
13338/*
13339 * "searchpairpos()" function
13340 */
13341 static void
13342f_searchpairpos(argvars, rettv)
13343 typval_T *argvars;
13344 typval_T *rettv;
13345{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013346 pos_T match_pos;
13347 int lnum = 0;
13348 int col = 0;
13349
13350 rettv->vval.v_number = 0;
13351
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013352 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013353 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013354
13355 if (searchpair_cmn(argvars, &match_pos) > 0)
13356 {
13357 lnum = match_pos.lnum;
13358 col = match_pos.col;
13359 }
13360
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013361 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13362 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013363}
13364
13365/*
13366 * Search for a start/middle/end thing.
13367 * Used by searchpair(), see its documentation for the details.
13368 * Returns 0 or -1 for no match,
13369 */
13370 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013371do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013372 char_u *spat; /* start pattern */
13373 char_u *mpat; /* middle pattern */
13374 char_u *epat; /* end pattern */
13375 int dir; /* BACKWARD or FORWARD */
13376 char_u *skip; /* skip expression */
13377 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013378 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013379 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013380{
13381 char_u *save_cpo;
13382 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13383 long retval = 0;
13384 pos_T pos;
13385 pos_T firstpos;
13386 pos_T foundpos;
13387 pos_T save_cursor;
13388 pos_T save_pos;
13389 int n;
13390 int r;
13391 int nest = 1;
13392 int err;
13393
13394 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13395 save_cpo = p_cpo;
13396 p_cpo = (char_u *)"";
13397
13398 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13399 * start/middle/end (pat3, for the top pair). */
13400 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13401 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13402 if (pat2 == NULL || pat3 == NULL)
13403 goto theend;
13404 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13405 if (*mpat == NUL)
13406 STRCPY(pat3, pat2);
13407 else
13408 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13409 spat, epat, mpat);
13410
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411 save_cursor = curwin->w_cursor;
13412 pos = curwin->w_cursor;
13413 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013414 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415 pat = pat3;
13416 for (;;)
13417 {
13418 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013419 SEARCH_KEEP, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13421 /* didn't find it or found the first match again: FAIL */
13422 break;
13423
13424 if (firstpos.lnum == 0)
13425 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013426 if (equalpos(pos, foundpos))
13427 {
13428 /* Found the same position again. Can happen with a pattern that
13429 * has "\zs" at the end and searching backwards. Advance one
13430 * character and try again. */
13431 if (dir == BACKWARD)
13432 decl(&pos);
13433 else
13434 incl(&pos);
13435 }
13436 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437
13438 /* If the skip pattern matches, ignore this match. */
13439 if (*skip != NUL)
13440 {
13441 save_pos = curwin->w_cursor;
13442 curwin->w_cursor = pos;
13443 r = eval_to_bool(skip, &err, NULL, FALSE);
13444 curwin->w_cursor = save_pos;
13445 if (err)
13446 {
13447 /* Evaluating {skip} caused an error, break here. */
13448 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013449 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 break;
13451 }
13452 if (r)
13453 continue;
13454 }
13455
13456 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13457 {
13458 /* Found end when searching backwards or start when searching
13459 * forward: nested pair. */
13460 ++nest;
13461 pat = pat2; /* nested, don't search for middle */
13462 }
13463 else
13464 {
13465 /* Found end when searching forward or start when searching
13466 * backward: end of (nested) pair; or found middle in outer pair. */
13467 if (--nest == 1)
13468 pat = pat3; /* outer level, search for middle */
13469 }
13470
13471 if (nest == 0)
13472 {
13473 /* Found the match: return matchcount or line number. */
13474 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013475 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013477 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013478 if (flags & SP_SETPCMARK)
13479 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 curwin->w_cursor = pos;
13481 if (!(flags & SP_REPEAT))
13482 break;
13483 nest = 1; /* search for next unmatched */
13484 }
13485 }
13486
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013487 if (match_pos != NULL)
13488 {
13489 /* Store the match cursor position */
13490 match_pos->lnum = curwin->w_cursor.lnum;
13491 match_pos->col = curwin->w_cursor.col + 1;
13492 }
13493
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013495 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 curwin->w_cursor = save_cursor;
13497
13498theend:
13499 vim_free(pat2);
13500 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013502
13503 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504}
13505
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013506/*
13507 * "searchpos()" function
13508 */
13509 static void
13510f_searchpos(argvars, rettv)
13511 typval_T *argvars;
13512 typval_T *rettv;
13513{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013514 pos_T match_pos;
13515 int lnum = 0;
13516 int col = 0;
13517
13518 rettv->vval.v_number = 0;
13519
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013520 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013521 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013522
13523 if (search_cmn(argvars, &match_pos) > 0)
13524 {
13525 lnum = match_pos.lnum;
13526 col = match_pos.col;
13527 }
13528
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013529 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13530 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013531
13532}
13533
13534
Bram Moolenaar0d660222005-01-07 21:51:51 +000013535/*ARGSUSED*/
13536 static void
13537f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013538 typval_T *argvars;
13539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013541#ifdef FEAT_CLIENTSERVER
13542 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013543 char_u *server = get_tv_string_chk(&argvars[0]);
13544 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545
Bram Moolenaar0d660222005-01-07 21:51:51 +000013546 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013547 if (server == NULL || reply == NULL)
13548 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013549 if (check_restricted() || check_secure())
13550 return;
13551# ifdef FEAT_X11
13552 if (check_connection() == FAIL)
13553 return;
13554# endif
13555
13556 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013558 EMSG(_("E258: Unable to send to client"));
13559 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013561 rettv->vval.v_number = 0;
13562#else
13563 rettv->vval.v_number = -1;
13564#endif
13565}
13566
13567/*ARGSUSED*/
13568 static void
13569f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013570 typval_T *argvars;
13571 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013572{
13573 char_u *r = NULL;
13574
13575#ifdef FEAT_CLIENTSERVER
13576# ifdef WIN32
13577 r = serverGetVimNames();
13578# else
13579 make_connection();
13580 if (X_DISPLAY != NULL)
13581 r = serverGetVimNames(X_DISPLAY);
13582# endif
13583#endif
13584 rettv->v_type = VAR_STRING;
13585 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586}
13587
13588/*
13589 * "setbufvar()" function
13590 */
13591/*ARGSUSED*/
13592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013593f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013594 typval_T *argvars;
13595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596{
13597 buf_T *buf;
13598#ifdef FEAT_AUTOCMD
13599 aco_save_T aco;
13600#else
13601 buf_T *save_curbuf;
13602#endif
13603 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013604 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 char_u nbuf[NUMBUFLEN];
13606
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013607 rettv->vval.v_number = 0;
13608
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 if (check_restricted() || check_secure())
13610 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013611 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13612 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614 varp = &argvars[2];
13615
13616 if (buf != NULL && varname != NULL && varp != NULL)
13617 {
13618 /* set curbuf to be our buf, temporarily */
13619#ifdef FEAT_AUTOCMD
13620 aucmd_prepbuf(&aco, buf);
13621#else
13622 save_curbuf = curbuf;
13623 curbuf = buf;
13624#endif
13625
13626 if (*varname == '&')
13627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013628 long numval;
13629 char_u *strval;
13630 int error = FALSE;
13631
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013633 numval = get_tv_number_chk(varp, &error);
13634 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013635 if (!error && strval != NULL)
13636 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 }
13638 else
13639 {
13640 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13641 if (bufvarname != NULL)
13642 {
13643 STRCPY(bufvarname, "b:");
13644 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013645 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646 vim_free(bufvarname);
13647 }
13648 }
13649
13650 /* reset notion of buffer */
13651#ifdef FEAT_AUTOCMD
13652 aucmd_restbuf(&aco);
13653#else
13654 curbuf = save_curbuf;
13655#endif
13656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657}
13658
13659/*
13660 * "setcmdpos()" function
13661 */
13662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013663f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013664 typval_T *argvars;
13665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013667 int pos = (int)get_tv_number(&argvars[0]) - 1;
13668
13669 if (pos >= 0)
13670 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671}
13672
13673/*
13674 * "setline()" function
13675 */
13676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013678 typval_T *argvars;
13679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680{
13681 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013682 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013683 list_T *l = NULL;
13684 listitem_T *li = NULL;
13685 long added = 0;
13686 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013688 lnum = get_tv_lnum(&argvars[0]);
13689 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013691 l = argvars[1].vval.v_list;
13692 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013694 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013695 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013696
13697 rettv->vval.v_number = 0; /* OK */
13698 for (;;)
13699 {
13700 if (l != NULL)
13701 {
13702 /* list argument, get next string */
13703 if (li == NULL)
13704 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013705 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013706 li = li->li_next;
13707 }
13708
13709 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013710 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013711 break;
13712 if (lnum <= curbuf->b_ml.ml_line_count)
13713 {
13714 /* existing line, replace it */
13715 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13716 {
13717 changed_bytes(lnum, 0);
13718 check_cursor_col();
13719 rettv->vval.v_number = 0; /* OK */
13720 }
13721 }
13722 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13723 {
13724 /* lnum is one past the last line, append the line */
13725 ++added;
13726 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13727 rettv->vval.v_number = 0; /* OK */
13728 }
13729
13730 if (l == NULL) /* only one string argument */
13731 break;
13732 ++lnum;
13733 }
13734
13735 if (added > 0)
13736 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737}
13738
13739/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013740 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013741 */
13742/*ARGSUSED*/
13743 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013744set_qf_ll_list(wp, list_arg, action_arg, rettv)
13745 win_T *wp;
13746 typval_T *list_arg;
13747 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013748 typval_T *rettv;
13749{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013750#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013751 char_u *act;
13752 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013753#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013754
Bram Moolenaar2641f772005-03-25 21:58:17 +000013755 rettv->vval.v_number = -1;
13756
13757#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013758 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013759 EMSG(_(e_listreq));
13760 else
13761 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013762 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013763
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013764 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013765 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013766 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013767 if (act == NULL)
13768 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013769 if (*act == 'a' || *act == 'r')
13770 action = *act;
13771 }
13772
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013773 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013774 rettv->vval.v_number = 0;
13775 }
13776#endif
13777}
13778
13779/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013780 * "setloclist()" function
13781 */
13782/*ARGSUSED*/
13783 static void
13784f_setloclist(argvars, rettv)
13785 typval_T *argvars;
13786 typval_T *rettv;
13787{
13788 win_T *win;
13789
13790 rettv->vval.v_number = -1;
13791
13792 win = find_win_by_nr(&argvars[0]);
13793 if (win != NULL)
13794 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13795}
13796
13797/*
13798 * "setqflist()" function
13799 */
13800/*ARGSUSED*/
13801 static void
13802f_setqflist(argvars, rettv)
13803 typval_T *argvars;
13804 typval_T *rettv;
13805{
13806 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
13807}
13808
13809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810 * "setreg()" function
13811 */
13812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013813f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013814 typval_T *argvars;
13815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816{
13817 int regname;
13818 char_u *strregname;
13819 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013820 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 int append;
13822 char_u yank_type;
13823 long block_len;
13824
13825 block_len = -1;
13826 yank_type = MAUTO;
13827 append = FALSE;
13828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013829 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013830 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013832 if (strregname == NULL)
13833 return; /* type error; errmsg already given */
13834 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 if (regname == 0 || regname == '@')
13836 regname = '"';
13837 else if (regname == '=')
13838 return;
13839
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013840 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013842 stropt = get_tv_string_chk(&argvars[2]);
13843 if (stropt == NULL)
13844 return; /* type error */
13845 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 switch (*stropt)
13847 {
13848 case 'a': case 'A': /* append */
13849 append = TRUE;
13850 break;
13851 case 'v': case 'c': /* character-wise selection */
13852 yank_type = MCHAR;
13853 break;
13854 case 'V': case 'l': /* line-wise selection */
13855 yank_type = MLINE;
13856 break;
13857#ifdef FEAT_VISUAL
13858 case 'b': case Ctrl_V: /* block-wise selection */
13859 yank_type = MBLOCK;
13860 if (VIM_ISDIGIT(stropt[1]))
13861 {
13862 ++stropt;
13863 block_len = getdigits(&stropt) - 1;
13864 --stropt;
13865 }
13866 break;
13867#endif
13868 }
13869 }
13870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013871 strval = get_tv_string_chk(&argvars[1]);
13872 if (strval != NULL)
13873 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013875 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876}
13877
13878
13879/*
13880 * "setwinvar(expr)" function
13881 */
13882/*ARGSUSED*/
13883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013884f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013885 typval_T *argvars;
13886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887{
13888 win_T *win;
13889#ifdef FEAT_WINDOWS
13890 win_T *save_curwin;
13891#endif
13892 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013893 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894 char_u nbuf[NUMBUFLEN];
13895
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013896 rettv->vval.v_number = 0;
13897
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898 if (check_restricted() || check_secure())
13899 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013901 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902 varp = &argvars[2];
13903
13904 if (win != NULL && varname != NULL && varp != NULL)
13905 {
13906#ifdef FEAT_WINDOWS
13907 /* set curwin to be our win, temporarily */
13908 save_curwin = curwin;
13909 curwin = win;
13910 curbuf = curwin->w_buffer;
13911#endif
13912
13913 if (*varname == '&')
13914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013915 long numval;
13916 char_u *strval;
13917 int error = FALSE;
13918
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013920 numval = get_tv_number_chk(varp, &error);
13921 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013922 if (!error && strval != NULL)
13923 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924 }
13925 else
13926 {
13927 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13928 if (winvarname != NULL)
13929 {
13930 STRCPY(winvarname, "w:");
13931 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013932 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 vim_free(winvarname);
13934 }
13935 }
13936
13937#ifdef FEAT_WINDOWS
13938 /* Restore current window, if it's still valid (autocomands can make
13939 * it invalid). */
13940 if (win_valid(save_curwin))
13941 {
13942 curwin = save_curwin;
13943 curbuf = curwin->w_buffer;
13944 }
13945#endif
13946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947}
13948
13949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013950 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 */
13952 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013953f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013954 typval_T *argvars;
13955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013957 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958
Bram Moolenaar0d660222005-01-07 21:51:51 +000013959 p = get_tv_string(&argvars[0]);
13960 rettv->vval.v_string = vim_strsave(p);
13961 simplify_filename(rettv->vval.v_string); /* simplify in place */
13962 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963}
13964
Bram Moolenaar0d660222005-01-07 21:51:51 +000013965static int
13966#ifdef __BORLANDC__
13967 _RTLENTRYF
13968#endif
13969 item_compare __ARGS((const void *s1, const void *s2));
13970static int
13971#ifdef __BORLANDC__
13972 _RTLENTRYF
13973#endif
13974 item_compare2 __ARGS((const void *s1, const void *s2));
13975
13976static int item_compare_ic;
13977static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013978static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013979#define ITEM_COMPARE_FAIL 999
13980
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013982 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013984 static int
13985#ifdef __BORLANDC__
13986_RTLENTRYF
13987#endif
13988item_compare(s1, s2)
13989 const void *s1;
13990 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013992 char_u *p1, *p2;
13993 char_u *tofree1, *tofree2;
13994 int res;
13995 char_u numbuf1[NUMBUFLEN];
13996 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013998 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
13999 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014000 if (item_compare_ic)
14001 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014003 res = STRCMP(p1, p2);
14004 vim_free(tofree1);
14005 vim_free(tofree2);
14006 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007}
14008
14009 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014010#ifdef __BORLANDC__
14011_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014013item_compare2(s1, s2)
14014 const void *s1;
14015 const void *s2;
14016{
14017 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014018 typval_T rettv;
14019 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014020 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014022 /* shortcut after failure in previous call; compare all items equal */
14023 if (item_compare_func_err)
14024 return 0;
14025
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014026 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14027 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014028 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14029 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014030
14031 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14032 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014033 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014034 clear_tv(&argv[0]);
14035 clear_tv(&argv[1]);
14036
14037 if (res == FAIL)
14038 res = ITEM_COMPARE_FAIL;
14039 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014040 /* return value has wrong type */
14041 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14042 if (item_compare_func_err)
14043 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014044 clear_tv(&rettv);
14045 return res;
14046}
14047
14048/*
14049 * "sort({list})" function
14050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014052f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014053 typval_T *argvars;
14054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055{
Bram Moolenaar33570922005-01-25 22:26:29 +000014056 list_T *l;
14057 listitem_T *li;
14058 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014059 long len;
14060 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061
Bram Moolenaar0d660222005-01-07 21:51:51 +000014062 rettv->vval.v_number = 0;
14063 if (argvars[0].v_type != VAR_LIST)
14064 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 else
14066 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014067 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014068 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014069 return;
14070 rettv->vval.v_list = l;
14071 rettv->v_type = VAR_LIST;
14072 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073
Bram Moolenaar0d660222005-01-07 21:51:51 +000014074 len = list_len(l);
14075 if (len <= 1)
14076 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077
Bram Moolenaar0d660222005-01-07 21:51:51 +000014078 item_compare_ic = FALSE;
14079 item_compare_func = NULL;
14080 if (argvars[1].v_type != VAR_UNKNOWN)
14081 {
14082 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014083 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014084 else
14085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014086 int error = FALSE;
14087
14088 i = get_tv_number_chk(&argvars[1], &error);
14089 if (error)
14090 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014091 if (i == 1)
14092 item_compare_ic = TRUE;
14093 else
14094 item_compare_func = get_tv_string(&argvars[1]);
14095 }
14096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097
Bram Moolenaar0d660222005-01-07 21:51:51 +000014098 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014099 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014100 if (ptrs == NULL)
14101 return;
14102 i = 0;
14103 for (li = l->lv_first; li != NULL; li = li->li_next)
14104 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014106 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014107 /* test the compare function */
14108 if (item_compare_func != NULL
14109 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14110 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014111 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014113 {
14114 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014115 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014116 item_compare_func == NULL ? item_compare : item_compare2);
14117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014118 if (!item_compare_func_err)
14119 {
14120 /* Clear the List and append the items in the sorted order. */
14121 l->lv_first = l->lv_last = NULL;
14122 l->lv_len = 0;
14123 for (i = 0; i < len; ++i)
14124 list_append(l, ptrs[i]);
14125 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014126 }
14127
14128 vim_free(ptrs);
14129 }
14130}
14131
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014132/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014133 * "soundfold({word})" function
14134 */
14135 static void
14136f_soundfold(argvars, rettv)
14137 typval_T *argvars;
14138 typval_T *rettv;
14139{
14140 char_u *s;
14141
14142 rettv->v_type = VAR_STRING;
14143 s = get_tv_string(&argvars[0]);
14144#ifdef FEAT_SYN_HL
14145 rettv->vval.v_string = eval_soundfold(s);
14146#else
14147 rettv->vval.v_string = vim_strsave(s);
14148#endif
14149}
14150
14151/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014152 * "spellbadword()" function
14153 */
14154/* ARGSUSED */
14155 static void
14156f_spellbadword(argvars, rettv)
14157 typval_T *argvars;
14158 typval_T *rettv;
14159{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014160 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014161 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014162 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014163
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014164 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014165 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014166
14167#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014168 if (argvars[0].v_type == VAR_UNKNOWN)
14169 {
14170 /* Find the start and length of the badly spelled word. */
14171 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14172 if (len != 0)
14173 word = ml_get_cursor();
14174 }
14175 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14176 {
14177 char_u *str = get_tv_string_chk(&argvars[0]);
14178 int capcol = -1;
14179
14180 if (str != NULL)
14181 {
14182 /* Check the argument for spelling. */
14183 while (*str != NUL)
14184 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014185 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014186 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014187 {
14188 word = str;
14189 break;
14190 }
14191 str += len;
14192 }
14193 }
14194 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014195#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014196
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014197 list_append_string(rettv->vval.v_list, word, len);
14198 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014199 attr == HLF_SPB ? "bad" :
14200 attr == HLF_SPR ? "rare" :
14201 attr == HLF_SPL ? "local" :
14202 attr == HLF_SPC ? "caps" :
14203 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014204}
14205
14206/*
14207 * "spellsuggest()" function
14208 */
14209 static void
14210f_spellsuggest(argvars, rettv)
14211 typval_T *argvars;
14212 typval_T *rettv;
14213{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014214#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014215 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014216 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014217 int maxcount;
14218 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014219 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014220 listitem_T *li;
14221 int need_capital = FALSE;
14222#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014223
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014224 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014225 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014226
14227#ifdef FEAT_SYN_HL
14228 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14229 {
14230 str = get_tv_string(&argvars[0]);
14231 if (argvars[1].v_type != VAR_UNKNOWN)
14232 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014233 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014234 if (maxcount <= 0)
14235 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014236 if (argvars[2].v_type != VAR_UNKNOWN)
14237 {
14238 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14239 if (typeerr)
14240 return;
14241 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014242 }
14243 else
14244 maxcount = 25;
14245
Bram Moolenaar4770d092006-01-12 23:22:24 +000014246 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014247
14248 for (i = 0; i < ga.ga_len; ++i)
14249 {
14250 str = ((char_u **)ga.ga_data)[i];
14251
14252 li = listitem_alloc();
14253 if (li == NULL)
14254 vim_free(str);
14255 else
14256 {
14257 li->li_tv.v_type = VAR_STRING;
14258 li->li_tv.v_lock = 0;
14259 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014260 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014261 }
14262 }
14263 ga_clear(&ga);
14264 }
14265#endif
14266}
14267
Bram Moolenaar0d660222005-01-07 21:51:51 +000014268 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014269f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014270 typval_T *argvars;
14271 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014272{
14273 char_u *str;
14274 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014275 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014276 regmatch_T regmatch;
14277 char_u patbuf[NUMBUFLEN];
14278 char_u *save_cpo;
14279 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014280 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014281 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014282 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014283
14284 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14285 save_cpo = p_cpo;
14286 p_cpo = (char_u *)"";
14287
14288 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014289 if (argvars[1].v_type != VAR_UNKNOWN)
14290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014291 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14292 if (pat == NULL)
14293 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014294 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014295 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014296 }
14297 if (pat == NULL || *pat == NUL)
14298 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014299
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014300 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014302 if (typeerr)
14303 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304
Bram Moolenaar0d660222005-01-07 21:51:51 +000014305 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14306 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014308 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014309 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014310 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014311 if (*str == NUL)
14312 match = FALSE; /* empty item at the end */
14313 else
14314 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014315 if (match)
14316 end = regmatch.startp[0];
14317 else
14318 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014319 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14320 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014321 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014322 if (list_append_string(rettv->vval.v_list, str,
14323 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014324 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014325 }
14326 if (!match)
14327 break;
14328 /* Advance to just after the match. */
14329 if (regmatch.endp[0] > str)
14330 col = 0;
14331 else
14332 {
14333 /* Don't get stuck at the same match. */
14334#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014335 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014336#else
14337 col = 1;
14338#endif
14339 }
14340 str = regmatch.endp[0];
14341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342
Bram Moolenaar0d660222005-01-07 21:51:51 +000014343 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345
Bram Moolenaar0d660222005-01-07 21:51:51 +000014346 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347}
14348
14349#ifdef HAVE_STRFTIME
14350/*
14351 * "strftime({format}[, {time}])" function
14352 */
14353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014354f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014355 typval_T *argvars;
14356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357{
14358 char_u result_buf[256];
14359 struct tm *curtime;
14360 time_t seconds;
14361 char_u *p;
14362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014366 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 seconds = time(NULL);
14368 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014369 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 curtime = localtime(&seconds);
14371 /* MSVC returns NULL for an invalid value of seconds. */
14372 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014373 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374 else
14375 {
14376# ifdef FEAT_MBYTE
14377 vimconv_T conv;
14378 char_u *enc;
14379
14380 conv.vc_type = CONV_NONE;
14381 enc = enc_locale();
14382 convert_setup(&conv, p_enc, enc);
14383 if (conv.vc_type != CONV_NONE)
14384 p = string_convert(&conv, p, NULL);
14385# endif
14386 if (p != NULL)
14387 (void)strftime((char *)result_buf, sizeof(result_buf),
14388 (char *)p, curtime);
14389 else
14390 result_buf[0] = NUL;
14391
14392# ifdef FEAT_MBYTE
14393 if (conv.vc_type != CONV_NONE)
14394 vim_free(p);
14395 convert_setup(&conv, enc, p_enc);
14396 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014397 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398 else
14399# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014400 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401
14402# ifdef FEAT_MBYTE
14403 /* Release conversion descriptors */
14404 convert_setup(&conv, NULL, NULL);
14405 vim_free(enc);
14406# endif
14407 }
14408}
14409#endif
14410
14411/*
14412 * "stridx()" function
14413 */
14414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014415f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014416 typval_T *argvars;
14417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418{
14419 char_u buf[NUMBUFLEN];
14420 char_u *needle;
14421 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014422 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014424 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014426 needle = get_tv_string_chk(&argvars[1]);
14427 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014428 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014429 if (needle == NULL || haystack == NULL)
14430 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431
Bram Moolenaar33570922005-01-25 22:26:29 +000014432 if (argvars[2].v_type != VAR_UNKNOWN)
14433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014434 int error = FALSE;
14435
14436 start_idx = get_tv_number_chk(&argvars[2], &error);
14437 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014438 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014439 if (start_idx >= 0)
14440 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014441 }
14442
14443 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14444 if (pos != NULL)
14445 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446}
14447
14448/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014449 * "string()" function
14450 */
14451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014452f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014453 typval_T *argvars;
14454 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014455{
14456 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014457 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014459 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014460 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014461 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014462 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463}
14464
14465/*
14466 * "strlen()" function
14467 */
14468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014469f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014470 typval_T *argvars;
14471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014473 rettv->vval.v_number = (varnumber_T)(STRLEN(
14474 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475}
14476
14477/*
14478 * "strpart()" function
14479 */
14480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014481f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014482 typval_T *argvars;
14483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484{
14485 char_u *p;
14486 int n;
14487 int len;
14488 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014489 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014491 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492 slen = (int)STRLEN(p);
14493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014494 n = get_tv_number_chk(&argvars[1], &error);
14495 if (error)
14496 len = 0;
14497 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014498 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 else
14500 len = slen - n; /* default len: all bytes that are available. */
14501
14502 /*
14503 * Only return the overlap between the specified part and the actual
14504 * string.
14505 */
14506 if (n < 0)
14507 {
14508 len += n;
14509 n = 0;
14510 }
14511 else if (n > slen)
14512 n = slen;
14513 if (len < 0)
14514 len = 0;
14515 else if (n + len > slen)
14516 len = slen - n;
14517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014518 rettv->v_type = VAR_STRING;
14519 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520}
14521
14522/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014523 * "strridx()" function
14524 */
14525 static void
14526f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014527 typval_T *argvars;
14528 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014529{
14530 char_u buf[NUMBUFLEN];
14531 char_u *needle;
14532 char_u *haystack;
14533 char_u *rest;
14534 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014535 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014537 needle = get_tv_string_chk(&argvars[1]);
14538 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014539 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014540
14541 rettv->vval.v_number = -1;
14542 if (needle == NULL || haystack == NULL)
14543 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014544 if (argvars[2].v_type != VAR_UNKNOWN)
14545 {
14546 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014547 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014548 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014549 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014550 }
14551 else
14552 end_idx = haystack_len;
14553
Bram Moolenaar0d660222005-01-07 21:51:51 +000014554 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014555 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014556 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014557 lastmatch = haystack + end_idx;
14558 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014559 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014560 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014561 for (rest = haystack; *rest != '\0'; ++rest)
14562 {
14563 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014564 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014565 break;
14566 lastmatch = rest;
14567 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014568 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014569
14570 if (lastmatch == NULL)
14571 rettv->vval.v_number = -1;
14572 else
14573 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14574}
14575
14576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577 * "strtrans()" function
14578 */
14579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014580f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014581 typval_T *argvars;
14582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014584 rettv->v_type = VAR_STRING;
14585 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586}
14587
14588/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014589 * "submatch()" function
14590 */
14591 static void
14592f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014593 typval_T *argvars;
14594 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014595{
14596 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014597 rettv->vval.v_string =
14598 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014599}
14600
14601/*
14602 * "substitute()" function
14603 */
14604 static void
14605f_substitute(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 char_u patbuf[NUMBUFLEN];
14610 char_u subbuf[NUMBUFLEN];
14611 char_u flagsbuf[NUMBUFLEN];
14612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014613 char_u *str = get_tv_string_chk(&argvars[0]);
14614 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14615 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14616 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14617
Bram Moolenaar0d660222005-01-07 21:51:51 +000014618 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014619 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14620 rettv->vval.v_string = NULL;
14621 else
14622 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014623}
14624
14625/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014626 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627 */
14628/*ARGSUSED*/
14629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014631 typval_T *argvars;
14632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633{
14634 int id = 0;
14635#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014636 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637 long col;
14638 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014639 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014641 lnum = get_tv_lnum(argvars); /* -1 on type error */
14642 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14643 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014645 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014646 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014647 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648#endif
14649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014650 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014651}
14652
14653/*
14654 * "synIDattr(id, what [, mode])" function
14655 */
14656/*ARGSUSED*/
14657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014658f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014659 typval_T *argvars;
14660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661{
14662 char_u *p = NULL;
14663#ifdef FEAT_SYN_HL
14664 int id;
14665 char_u *what;
14666 char_u *mode;
14667 char_u modebuf[NUMBUFLEN];
14668 int modec;
14669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014670 id = get_tv_number(&argvars[0]);
14671 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014672 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014674 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675 modec = TOLOWER_ASC(mode[0]);
14676 if (modec != 't' && modec != 'c'
14677#ifdef FEAT_GUI
14678 && modec != 'g'
14679#endif
14680 )
14681 modec = 0; /* replace invalid with current */
14682 }
14683 else
14684 {
14685#ifdef FEAT_GUI
14686 if (gui.in_use)
14687 modec = 'g';
14688 else
14689#endif
14690 if (t_colors > 1)
14691 modec = 'c';
14692 else
14693 modec = 't';
14694 }
14695
14696
14697 switch (TOLOWER_ASC(what[0]))
14698 {
14699 case 'b':
14700 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14701 p = highlight_color(id, what, modec);
14702 else /* bold */
14703 p = highlight_has_attr(id, HL_BOLD, modec);
14704 break;
14705
14706 case 'f': /* fg[#] */
14707 p = highlight_color(id, what, modec);
14708 break;
14709
14710 case 'i':
14711 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14712 p = highlight_has_attr(id, HL_INVERSE, modec);
14713 else /* italic */
14714 p = highlight_has_attr(id, HL_ITALIC, modec);
14715 break;
14716
14717 case 'n': /* name */
14718 p = get_highlight_name(NULL, id - 1);
14719 break;
14720
14721 case 'r': /* reverse */
14722 p = highlight_has_attr(id, HL_INVERSE, modec);
14723 break;
14724
14725 case 's': /* standout */
14726 p = highlight_has_attr(id, HL_STANDOUT, modec);
14727 break;
14728
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014729 case 'u':
14730 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14731 /* underline */
14732 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14733 else
14734 /* undercurl */
14735 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 break;
14737 }
14738
14739 if (p != NULL)
14740 p = vim_strsave(p);
14741#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014742 rettv->v_type = VAR_STRING;
14743 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744}
14745
14746/*
14747 * "synIDtrans(id)" function
14748 */
14749/*ARGSUSED*/
14750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014751f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014752 typval_T *argvars;
14753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754{
14755 int id;
14756
14757#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014758 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759
14760 if (id > 0)
14761 id = syn_get_final_id(id);
14762 else
14763#endif
14764 id = 0;
14765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014766 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767}
14768
14769/*
14770 * "system()" function
14771 */
14772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014773f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014774 typval_T *argvars;
14775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014777 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014779 char_u *infile = NULL;
14780 char_u buf[NUMBUFLEN];
14781 int err = FALSE;
14782 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014783
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014784 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014785 {
14786 /*
14787 * Write the string to a temp file, to be used for input of the shell
14788 * command.
14789 */
14790 if ((infile = vim_tempname('i')) == NULL)
14791 {
14792 EMSG(_(e_notmp));
14793 return;
14794 }
14795
14796 fd = mch_fopen((char *)infile, WRITEBIN);
14797 if (fd == NULL)
14798 {
14799 EMSG2(_(e_notopen), infile);
14800 goto done;
14801 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014802 p = get_tv_string_buf_chk(&argvars[1], buf);
14803 if (p == NULL)
14804 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014805 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14806 err = TRUE;
14807 if (fclose(fd) != 0)
14808 err = TRUE;
14809 if (err)
14810 {
14811 EMSG(_("E677: Error writing temp file"));
14812 goto done;
14813 }
14814 }
14815
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014816 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014817
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818#ifdef USE_CR
14819 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014820 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821 {
14822 char_u *s;
14823
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014824 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825 {
14826 if (*s == CAR)
14827 *s = NL;
14828 }
14829 }
14830#else
14831# ifdef USE_CRNL
14832 /* translate <CR><NL> 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, *d;
14836
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014837 d = res;
14838 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839 {
14840 if (s[0] == CAR && s[1] == NL)
14841 ++s;
14842 *d++ = *s;
14843 }
14844 *d = NUL;
14845 }
14846# endif
14847#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014848
14849done:
14850 if (infile != NULL)
14851 {
14852 mch_remove(infile);
14853 vim_free(infile);
14854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014855 rettv->v_type = VAR_STRING;
14856 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857}
14858
14859/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014860 * "tabpagebuflist()" function
14861 */
14862/* ARGSUSED */
14863 static void
14864f_tabpagebuflist(argvars, rettv)
14865 typval_T *argvars;
14866 typval_T *rettv;
14867{
14868#ifndef FEAT_WINDOWS
14869 rettv->vval.v_number = 0;
14870#else
14871 tabpage_T *tp;
14872 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014873
14874 if (argvars[0].v_type == VAR_UNKNOWN)
14875 wp = firstwin;
14876 else
14877 {
14878 tp = find_tabpage((int)get_tv_number(&argvars[0]));
14879 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000014880 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014881 }
14882 if (wp == NULL)
14883 rettv->vval.v_number = 0;
14884 else
14885 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014886 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014887 rettv->vval.v_number = 0;
14888 else
14889 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014890 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014891 if (list_append_number(rettv->vval.v_list,
14892 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014893 break;
14894 }
14895 }
14896#endif
14897}
14898
14899
14900/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000014901 * "tabpagenr()" function
14902 */
14903/* ARGSUSED */
14904 static void
14905f_tabpagenr(argvars, rettv)
14906 typval_T *argvars;
14907 typval_T *rettv;
14908{
14909 int nr = 1;
14910#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000014911 char_u *arg;
14912
14913 if (argvars[0].v_type != VAR_UNKNOWN)
14914 {
14915 arg = get_tv_string_chk(&argvars[0]);
14916 nr = 0;
14917 if (arg != NULL)
14918 {
14919 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000014920 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000014921 else
14922 EMSG2(_(e_invexpr2), arg);
14923 }
14924 }
14925 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000014926 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000014927#endif
14928 rettv->vval.v_number = nr;
14929}
14930
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014931
14932#ifdef FEAT_WINDOWS
14933static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
14934
14935/*
14936 * Common code for tabpagewinnr() and winnr().
14937 */
14938 static int
14939get_winnr(tp, argvar)
14940 tabpage_T *tp;
14941 typval_T *argvar;
14942{
14943 win_T *twin;
14944 int nr = 1;
14945 win_T *wp;
14946 char_u *arg;
14947
14948 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14949 if (argvar->v_type != VAR_UNKNOWN)
14950 {
14951 arg = get_tv_string_chk(argvar);
14952 if (arg == NULL)
14953 nr = 0; /* type error; errmsg already given */
14954 else if (STRCMP(arg, "$") == 0)
14955 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14956 else if (STRCMP(arg, "#") == 0)
14957 {
14958 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14959 if (twin == NULL)
14960 nr = 0;
14961 }
14962 else
14963 {
14964 EMSG2(_(e_invexpr2), arg);
14965 nr = 0;
14966 }
14967 }
14968
14969 if (nr > 0)
14970 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14971 wp != twin; wp = wp->w_next)
14972 ++nr;
14973 return nr;
14974}
14975#endif
14976
14977/*
14978 * "tabpagewinnr()" function
14979 */
14980/* ARGSUSED */
14981 static void
14982f_tabpagewinnr(argvars, rettv)
14983 typval_T *argvars;
14984 typval_T *rettv;
14985{
14986 int nr = 1;
14987#ifdef FEAT_WINDOWS
14988 tabpage_T *tp;
14989
14990 tp = find_tabpage((int)get_tv_number(&argvars[0]));
14991 if (tp == NULL)
14992 nr = 0;
14993 else
14994 nr = get_winnr(tp, &argvars[1]);
14995#endif
14996 rettv->vval.v_number = nr;
14997}
14998
14999
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015000/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015001 * "tagfiles()" function
15002 */
15003/*ARGSUSED*/
15004 static void
15005f_tagfiles(argvars, rettv)
15006 typval_T *argvars;
15007 typval_T *rettv;
15008{
15009 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015010 tagname_T tn;
15011 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015012
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015013 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015014 {
15015 rettv->vval.v_number = 0;
15016 return;
15017 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015018
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015019 for (first = TRUE; ; first = FALSE)
15020 if (get_tagfname(&tn, first, fname) == FAIL
15021 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015022 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015023 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015024}
15025
15026/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015027 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015028 */
15029 static void
15030f_taglist(argvars, rettv)
15031 typval_T *argvars;
15032 typval_T *rettv;
15033{
15034 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015035
15036 tag_pattern = get_tv_string(&argvars[0]);
15037
15038 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015039 if (*tag_pattern == NUL)
15040 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015041
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015042 if (rettv_list_alloc(rettv) == OK)
15043 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015044}
15045
15046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047 * "tempname()" function
15048 */
15049/*ARGSUSED*/
15050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015051f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015052 typval_T *argvars;
15053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054{
15055 static int x = 'A';
15056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015057 rettv->v_type = VAR_STRING;
15058 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059
15060 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15061 * names. Skip 'I' and 'O', they are used for shell redirection. */
15062 do
15063 {
15064 if (x == 'Z')
15065 x = '0';
15066 else if (x == '9')
15067 x = 'A';
15068 else
15069 {
15070#ifdef EBCDIC
15071 if (x == 'I')
15072 x = 'J';
15073 else if (x == 'R')
15074 x = 'S';
15075 else
15076#endif
15077 ++x;
15078 }
15079 } while (x == 'I' || x == 'O');
15080}
15081
15082/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015083 * "test(list)" function: Just checking the walls...
15084 */
15085/*ARGSUSED*/
15086 static void
15087f_test(argvars, rettv)
15088 typval_T *argvars;
15089 typval_T *rettv;
15090{
15091 /* Used for unit testing. Change the code below to your liking. */
15092#if 0
15093 listitem_T *li;
15094 list_T *l;
15095 char_u *bad, *good;
15096
15097 if (argvars[0].v_type != VAR_LIST)
15098 return;
15099 l = argvars[0].vval.v_list;
15100 if (l == NULL)
15101 return;
15102 li = l->lv_first;
15103 if (li == NULL)
15104 return;
15105 bad = get_tv_string(&li->li_tv);
15106 li = li->li_next;
15107 if (li == NULL)
15108 return;
15109 good = get_tv_string(&li->li_tv);
15110 rettv->vval.v_number = test_edit_score(bad, good);
15111#endif
15112}
15113
15114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015115 * "tolower(string)" function
15116 */
15117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015118f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015119 typval_T *argvars;
15120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121{
15122 char_u *p;
15123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015124 p = vim_strsave(get_tv_string(&argvars[0]));
15125 rettv->v_type = VAR_STRING;
15126 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127
15128 if (p != NULL)
15129 while (*p != NUL)
15130 {
15131#ifdef FEAT_MBYTE
15132 int l;
15133
15134 if (enc_utf8)
15135 {
15136 int c, lc;
15137
15138 c = utf_ptr2char(p);
15139 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015140 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 /* TODO: reallocate string when byte count changes. */
15142 if (utf_char2len(lc) == l)
15143 utf_char2bytes(lc, p);
15144 p += l;
15145 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015146 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147 p += l; /* skip multi-byte character */
15148 else
15149#endif
15150 {
15151 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15152 ++p;
15153 }
15154 }
15155}
15156
15157/*
15158 * "toupper(string)" function
15159 */
15160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015161f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015162 typval_T *argvars;
15163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015165 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015166 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167}
15168
15169/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015170 * "tr(string, fromstr, tostr)" function
15171 */
15172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015173f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015174 typval_T *argvars;
15175 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015176{
15177 char_u *instr;
15178 char_u *fromstr;
15179 char_u *tostr;
15180 char_u *p;
15181#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015182 int inlen;
15183 int fromlen;
15184 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015185 int idx;
15186 char_u *cpstr;
15187 int cplen;
15188 int first = TRUE;
15189#endif
15190 char_u buf[NUMBUFLEN];
15191 char_u buf2[NUMBUFLEN];
15192 garray_T ga;
15193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015194 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015195 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15196 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015197
15198 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015199 rettv->v_type = VAR_STRING;
15200 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015201 if (fromstr == NULL || tostr == NULL)
15202 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015203 ga_init2(&ga, (int)sizeof(char), 80);
15204
15205#ifdef FEAT_MBYTE
15206 if (!has_mbyte)
15207#endif
15208 /* not multi-byte: fromstr and tostr must be the same length */
15209 if (STRLEN(fromstr) != STRLEN(tostr))
15210 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015211#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015212error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015213#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015214 EMSG2(_(e_invarg2), fromstr);
15215 ga_clear(&ga);
15216 return;
15217 }
15218
15219 /* fromstr and tostr have to contain the same number of chars */
15220 while (*instr != NUL)
15221 {
15222#ifdef FEAT_MBYTE
15223 if (has_mbyte)
15224 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015225 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015226 cpstr = instr;
15227 cplen = inlen;
15228 idx = 0;
15229 for (p = fromstr; *p != NUL; p += fromlen)
15230 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015231 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015232 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15233 {
15234 for (p = tostr; *p != NUL; p += tolen)
15235 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015236 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015237 if (idx-- == 0)
15238 {
15239 cplen = tolen;
15240 cpstr = p;
15241 break;
15242 }
15243 }
15244 if (*p == NUL) /* tostr is shorter than fromstr */
15245 goto error;
15246 break;
15247 }
15248 ++idx;
15249 }
15250
15251 if (first && cpstr == instr)
15252 {
15253 /* Check that fromstr and tostr have the same number of
15254 * (multi-byte) characters. Done only once when a character
15255 * of instr doesn't appear in fromstr. */
15256 first = FALSE;
15257 for (p = tostr; *p != NUL; p += tolen)
15258 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015259 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015260 --idx;
15261 }
15262 if (idx != 0)
15263 goto error;
15264 }
15265
15266 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015267 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015268 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015269
15270 instr += inlen;
15271 }
15272 else
15273#endif
15274 {
15275 /* When not using multi-byte chars we can do it faster. */
15276 p = vim_strchr(fromstr, *instr);
15277 if (p != NULL)
15278 ga_append(&ga, tostr[p - fromstr]);
15279 else
15280 ga_append(&ga, *instr);
15281 ++instr;
15282 }
15283 }
15284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015285 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015286}
15287
15288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015289 * "type(expr)" function
15290 */
15291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015292f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015293 typval_T *argvars;
15294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015296 int n;
15297
15298 switch (argvars[0].v_type)
15299 {
15300 case VAR_NUMBER: n = 0; break;
15301 case VAR_STRING: n = 1; break;
15302 case VAR_FUNC: n = 2; break;
15303 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015304 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015305 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15306 }
15307 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308}
15309
15310/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015311 * "values(dict)" function
15312 */
15313 static void
15314f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015315 typval_T *argvars;
15316 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015317{
15318 dict_list(argvars, rettv, 1);
15319}
15320
15321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 * "virtcol(string)" function
15323 */
15324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015325f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015326 typval_T *argvars;
15327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328{
15329 colnr_T vcol = 0;
15330 pos_T *fp;
15331
15332 fp = var2fpos(&argvars[0], FALSE);
15333 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
15334 {
15335 getvvcol(curwin, fp, NULL, NULL, &vcol);
15336 ++vcol;
15337 }
15338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015339 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340}
15341
15342/*
15343 * "visualmode()" function
15344 */
15345/*ARGSUSED*/
15346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015347f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015348 typval_T *argvars;
15349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350{
15351#ifdef FEAT_VISUAL
15352 char_u str[2];
15353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015354 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355 str[0] = curbuf->b_visual_mode_eval;
15356 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015357 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358
15359 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015360 if ((argvars[0].v_type == VAR_NUMBER
15361 && argvars[0].vval.v_number != 0)
15362 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 curbuf->b_visual_mode_eval = NUL;
15365#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367#endif
15368}
15369
15370/*
15371 * "winbufnr(nr)" function
15372 */
15373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015374f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015375 typval_T *argvars;
15376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377{
15378 win_T *wp;
15379
15380 wp = find_win_by_nr(&argvars[0]);
15381 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015382 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015384 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385}
15386
15387/*
15388 * "wincol()" function
15389 */
15390/*ARGSUSED*/
15391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015392f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015393 typval_T *argvars;
15394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395{
15396 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398}
15399
15400/*
15401 * "winheight(nr)" function
15402 */
15403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015404f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015405 typval_T *argvars;
15406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407{
15408 win_T *wp;
15409
15410 wp = find_win_by_nr(&argvars[0]);
15411 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015412 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015414 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415}
15416
15417/*
15418 * "winline()" function
15419 */
15420/*ARGSUSED*/
15421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015422f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015423 typval_T *argvars;
15424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425{
15426 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015427 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428}
15429
15430/*
15431 * "winnr()" function
15432 */
15433/* ARGSUSED */
15434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015435f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015436 typval_T *argvars;
15437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438{
15439 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015440
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015442 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015444 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445}
15446
15447/*
15448 * "winrestcmd()" function
15449 */
15450/* ARGSUSED */
15451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015452f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015453 typval_T *argvars;
15454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455{
15456#ifdef FEAT_WINDOWS
15457 win_T *wp;
15458 int winnr = 1;
15459 garray_T ga;
15460 char_u buf[50];
15461
15462 ga_init2(&ga, (int)sizeof(char), 70);
15463 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15464 {
15465 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15466 ga_concat(&ga, buf);
15467# ifdef FEAT_VERTSPLIT
15468 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15469 ga_concat(&ga, buf);
15470# endif
15471 ++winnr;
15472 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015473 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015475 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015477 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015479 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480}
15481
15482/*
15483 * "winwidth(nr)" function
15484 */
15485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015486f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015487 typval_T *argvars;
15488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489{
15490 win_T *wp;
15491
15492 wp = find_win_by_nr(&argvars[0]);
15493 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015494 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495 else
15496#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015497 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015499 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500#endif
15501}
15502
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015504 * "writefile()" function
15505 */
15506 static void
15507f_writefile(argvars, rettv)
15508 typval_T *argvars;
15509 typval_T *rettv;
15510{
15511 int binary = FALSE;
15512 char_u *fname;
15513 FILE *fd;
15514 listitem_T *li;
15515 char_u *s;
15516 int ret = 0;
15517 int c;
15518
15519 if (argvars[0].v_type != VAR_LIST)
15520 {
15521 EMSG2(_(e_listarg), "writefile()");
15522 return;
15523 }
15524 if (argvars[0].vval.v_list == NULL)
15525 return;
15526
15527 if (argvars[2].v_type != VAR_UNKNOWN
15528 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15529 binary = TRUE;
15530
15531 /* Always open the file in binary mode, library functions have a mind of
15532 * their own about CR-LF conversion. */
15533 fname = get_tv_string(&argvars[1]);
15534 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15535 {
15536 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15537 ret = -1;
15538 }
15539 else
15540 {
15541 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15542 li = li->li_next)
15543 {
15544 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15545 {
15546 if (*s == '\n')
15547 c = putc(NUL, fd);
15548 else
15549 c = putc(*s, fd);
15550 if (c == EOF)
15551 {
15552 ret = -1;
15553 break;
15554 }
15555 }
15556 if (!binary || li->li_next != NULL)
15557 if (putc('\n', fd) == EOF)
15558 {
15559 ret = -1;
15560 break;
15561 }
15562 if (ret < 0)
15563 {
15564 EMSG(_(e_write));
15565 break;
15566 }
15567 }
15568 fclose(fd);
15569 }
15570
15571 rettv->vval.v_number = ret;
15572}
15573
15574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015576 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577 */
15578 static pos_T *
15579var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015580 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581 int lnum; /* TRUE when $ is last line */
15582{
15583 char_u *name;
15584 static pos_T pos;
15585 pos_T *pp;
15586
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015587 /* Argument can be [lnum, col]. */
15588 if (varp->v_type == VAR_LIST)
15589 {
15590 list_T *l;
15591 listitem_T *li;
15592 int len;
15593
15594 l = varp->vval.v_list;
15595 if (l == NULL)
15596 return NULL;
15597
15598 /* Get the line number */
15599 li = list_find(l, 0L);
15600 if (li == NULL)
15601 return NULL;
15602 pos.lnum = get_tv_number(&li->li_tv);
15603 if (pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
15604 return NULL; /* invalid line number */
15605
15606 /* Get the column number */
15607 li = list_find(l, 1L);
15608 if (li == NULL)
15609 return NULL;
15610 pos.col = get_tv_number(&li->li_tv);
15611 len = (long)STRLEN(ml_get(pos.lnum));
15612 if (pos.col <= 0 || ((len == 0 && pos.col > 1)
15613 || (len > 0 && pos.col > len)))
15614 return NULL; /* invalid column number */
15615
15616 pos.col--;
15617 return &pos;
15618 }
15619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015620 name = get_tv_string_chk(varp);
15621 if (name == NULL)
15622 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 if (name[0] == '.') /* cursor */
15624 return &curwin->w_cursor;
15625 if (name[0] == '\'') /* mark */
15626 {
15627 pp = getmark(name[1], FALSE);
15628 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15629 return NULL;
15630 return pp;
15631 }
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015632 if (name[0] == 'w' && lnum)
15633 {
15634 pos.col = 0;
15635 if (name[1] == '0') /* "w0": first visible line */
15636 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015637 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015638 pos.lnum = curwin->w_topline;
15639 return &pos;
15640 }
15641 else if (name[1] == '$') /* "w$": last visible line */
15642 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015643 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015644 pos.lnum = curwin->w_botline - 1;
15645 return &pos;
15646 }
15647 }
15648 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649 {
15650 if (lnum)
15651 {
15652 pos.lnum = curbuf->b_ml.ml_line_count;
15653 pos.col = 0;
15654 }
15655 else
15656 {
15657 pos.lnum = curwin->w_cursor.lnum;
15658 pos.col = (colnr_T)STRLEN(ml_get_curline());
15659 }
15660 return &pos;
15661 }
15662 return NULL;
15663}
15664
15665/*
15666 * Get the length of an environment variable name.
15667 * Advance "arg" to the first character after the name.
15668 * Return 0 for error.
15669 */
15670 static int
15671get_env_len(arg)
15672 char_u **arg;
15673{
15674 char_u *p;
15675 int len;
15676
15677 for (p = *arg; vim_isIDc(*p); ++p)
15678 ;
15679 if (p == *arg) /* no name found */
15680 return 0;
15681
15682 len = (int)(p - *arg);
15683 *arg = p;
15684 return len;
15685}
15686
15687/*
15688 * Get the length of the name of a function or internal variable.
15689 * "arg" is advanced to the first non-white character after the name.
15690 * Return 0 if something is wrong.
15691 */
15692 static int
15693get_id_len(arg)
15694 char_u **arg;
15695{
15696 char_u *p;
15697 int len;
15698
15699 /* Find the end of the name. */
15700 for (p = *arg; eval_isnamec(*p); ++p)
15701 ;
15702 if (p == *arg) /* no name found */
15703 return 0;
15704
15705 len = (int)(p - *arg);
15706 *arg = skipwhite(p);
15707
15708 return len;
15709}
15710
15711/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015712 * Get the length of the name of a variable or function.
15713 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015715 * Return -1 if curly braces expansion failed.
15716 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717 * If the name contains 'magic' {}'s, expand them and return the
15718 * expanded name in an allocated string via 'alias' - caller must free.
15719 */
15720 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015721get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722 char_u **arg;
15723 char_u **alias;
15724 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015725 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726{
15727 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 char_u *p;
15729 char_u *expr_start;
15730 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731
15732 *alias = NULL; /* default to no alias */
15733
15734 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15735 && (*arg)[2] == (int)KE_SNR)
15736 {
15737 /* hard coded <SNR>, already translated */
15738 *arg += 3;
15739 return get_id_len(arg) + 3;
15740 }
15741 len = eval_fname_script(*arg);
15742 if (len > 0)
15743 {
15744 /* literal "<SID>", "s:" or "<SNR>" */
15745 *arg += len;
15746 }
15747
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015749 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015751 p = find_name_end(*arg, &expr_start, &expr_end,
15752 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 if (expr_start != NULL)
15754 {
15755 char_u *temp_string;
15756
15757 if (!evaluate)
15758 {
15759 len += (int)(p - *arg);
15760 *arg = skipwhite(p);
15761 return len;
15762 }
15763
15764 /*
15765 * Include any <SID> etc in the expanded string:
15766 * Thus the -len here.
15767 */
15768 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15769 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015770 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 *alias = temp_string;
15772 *arg = skipwhite(p);
15773 return (int)STRLEN(temp_string);
15774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775
15776 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015777 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 EMSG2(_(e_invexpr2), *arg);
15779
15780 return len;
15781}
15782
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015783/*
15784 * Find the end of a variable or function name, taking care of magic braces.
15785 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15786 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015787 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015788 * Return a pointer to just after the name. Equal to "arg" if there is no
15789 * valid name.
15790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015792find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793 char_u *arg;
15794 char_u **expr_start;
15795 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015796 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015798 int mb_nest = 0;
15799 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800 char_u *p;
15801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015802 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015804 *expr_start = NULL;
15805 *expr_end = NULL;
15806 }
15807
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015808 /* Quick check for valid starting character. */
15809 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15810 return arg;
15811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015812 for (p = arg; *p != NUL
15813 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015814 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015815 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015816 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015817 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015818 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015819 if (*p == '\'')
15820 {
15821 /* skip over 'string' to avoid counting [ and ] inside it. */
15822 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15823 ;
15824 if (*p == NUL)
15825 break;
15826 }
15827 else if (*p == '"')
15828 {
15829 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15830 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15831 if (*p == '\\' && p[1] != NUL)
15832 ++p;
15833 if (*p == NUL)
15834 break;
15835 }
15836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015837 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015839 if (*p == '[')
15840 ++br_nest;
15841 else if (*p == ']')
15842 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015845 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015847 if (*p == '{')
15848 {
15849 mb_nest++;
15850 if (expr_start != NULL && *expr_start == NULL)
15851 *expr_start = p;
15852 }
15853 else if (*p == '}')
15854 {
15855 mb_nest--;
15856 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15857 *expr_end = p;
15858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015860 }
15861
15862 return p;
15863}
15864
15865/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015866 * Expands out the 'magic' {}'s in a variable/function name.
15867 * Note that this can call itself recursively, to deal with
15868 * constructs like foo{bar}{baz}{bam}
15869 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15870 * "in_start" ^
15871 * "expr_start" ^
15872 * "expr_end" ^
15873 * "in_end" ^
15874 *
15875 * Returns a new allocated string, which the caller must free.
15876 * Returns NULL for failure.
15877 */
15878 static char_u *
15879make_expanded_name(in_start, expr_start, expr_end, in_end)
15880 char_u *in_start;
15881 char_u *expr_start;
15882 char_u *expr_end;
15883 char_u *in_end;
15884{
15885 char_u c1;
15886 char_u *retval = NULL;
15887 char_u *temp_result;
15888 char_u *nextcmd = NULL;
15889
15890 if (expr_end == NULL || in_end == NULL)
15891 return NULL;
15892 *expr_start = NUL;
15893 *expr_end = NUL;
15894 c1 = *in_end;
15895 *in_end = NUL;
15896
15897 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15898 if (temp_result != NULL && nextcmd == NULL)
15899 {
15900 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15901 + (in_end - expr_end) + 1));
15902 if (retval != NULL)
15903 {
15904 STRCPY(retval, in_start);
15905 STRCAT(retval, temp_result);
15906 STRCAT(retval, expr_end + 1);
15907 }
15908 }
15909 vim_free(temp_result);
15910
15911 *in_end = c1; /* put char back for error messages */
15912 *expr_start = '{';
15913 *expr_end = '}';
15914
15915 if (retval != NULL)
15916 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015917 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015918 if (expr_start != NULL)
15919 {
15920 /* Further expansion! */
15921 temp_result = make_expanded_name(retval, expr_start,
15922 expr_end, temp_result);
15923 vim_free(retval);
15924 retval = temp_result;
15925 }
15926 }
15927
15928 return retval;
15929}
15930
15931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015933 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934 */
15935 static int
15936eval_isnamec(c)
15937 int c;
15938{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015939 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15940}
15941
15942/*
15943 * Return TRUE if character "c" can be used as the first character in a
15944 * variable or function name (excluding '{' and '}').
15945 */
15946 static int
15947eval_isnamec1(c)
15948 int c;
15949{
15950 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951}
15952
15953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954 * Set number v: variable to "val".
15955 */
15956 void
15957set_vim_var_nr(idx, val)
15958 int idx;
15959 long val;
15960{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015961 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015962}
15963
15964/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015965 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 */
15967 long
15968get_vim_var_nr(idx)
15969 int idx;
15970{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015971 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972}
15973
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015974#if defined(FEAT_AUTOCMD) || defined(PROTO)
15975/*
15976 * Get string v: variable value. Uses a static buffer, can only be used once.
15977 */
15978 char_u *
15979get_vim_var_str(idx)
15980 int idx;
15981{
15982 return get_tv_string(&vimvars[idx].vv_tv);
15983}
15984#endif
15985
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986/*
15987 * Set v:count, v:count1 and v:prevcount.
15988 */
15989 void
15990set_vcount(count, count1)
15991 long count;
15992 long count1;
15993{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015994 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15995 vimvars[VV_COUNT].vv_nr = count;
15996 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997}
15998
15999/*
16000 * Set string v: variable to a copy of "val".
16001 */
16002 void
16003set_vim_var_string(idx, val, len)
16004 int idx;
16005 char_u *val;
16006 int len; /* length of "val" to use or -1 (whole string) */
16007{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016008 /* Need to do this (at least) once, since we can't initialize a union.
16009 * Will always be invoked when "v:progname" is set. */
16010 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16011
Bram Moolenaare9a41262005-01-15 22:18:47 +000016012 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016014 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016016 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016018 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019}
16020
16021/*
16022 * Set v:register if needed.
16023 */
16024 void
16025set_reg_var(c)
16026 int c;
16027{
16028 char_u regname;
16029
16030 if (c == 0 || c == ' ')
16031 regname = '"';
16032 else
16033 regname = c;
16034 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016035 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036 set_vim_var_string(VV_REG, &regname, 1);
16037}
16038
16039/*
16040 * Get or set v:exception. If "oldval" == NULL, return the current value.
16041 * Otherwise, restore the value to "oldval" and return NULL.
16042 * Must always be called in pairs to save and restore v:exception! Does not
16043 * take care of memory allocations.
16044 */
16045 char_u *
16046v_exception(oldval)
16047 char_u *oldval;
16048{
16049 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016050 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051
Bram Moolenaare9a41262005-01-15 22:18:47 +000016052 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053 return NULL;
16054}
16055
16056/*
16057 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16058 * Otherwise, restore the value to "oldval" and return NULL.
16059 * Must always be called in pairs to save and restore v:throwpoint! Does not
16060 * take care of memory allocations.
16061 */
16062 char_u *
16063v_throwpoint(oldval)
16064 char_u *oldval;
16065{
16066 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016067 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068
Bram Moolenaare9a41262005-01-15 22:18:47 +000016069 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070 return NULL;
16071}
16072
16073#if defined(FEAT_AUTOCMD) || defined(PROTO)
16074/*
16075 * Set v:cmdarg.
16076 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16077 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16078 * Must always be called in pairs!
16079 */
16080 char_u *
16081set_cmdarg(eap, oldarg)
16082 exarg_T *eap;
16083 char_u *oldarg;
16084{
16085 char_u *oldval;
16086 char_u *newval;
16087 unsigned len;
16088
Bram Moolenaare9a41262005-01-15 22:18:47 +000016089 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016090 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016092 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016093 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016094 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 }
16096
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016097 if (eap->force_bin == FORCE_BIN)
16098 len = 6;
16099 else if (eap->force_bin == FORCE_NOBIN)
16100 len = 8;
16101 else
16102 len = 0;
16103 if (eap->force_ff != 0)
16104 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16105# ifdef FEAT_MBYTE
16106 if (eap->force_enc != 0)
16107 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016108 if (eap->bad_char != 0)
16109 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016110# endif
16111
16112 newval = alloc(len + 1);
16113 if (newval == NULL)
16114 return NULL;
16115
16116 if (eap->force_bin == FORCE_BIN)
16117 sprintf((char *)newval, " ++bin");
16118 else if (eap->force_bin == FORCE_NOBIN)
16119 sprintf((char *)newval, " ++nobin");
16120 else
16121 *newval = NUL;
16122 if (eap->force_ff != 0)
16123 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16124 eap->cmd + eap->force_ff);
16125# ifdef FEAT_MBYTE
16126 if (eap->force_enc != 0)
16127 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16128 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016129 if (eap->bad_char != 0)
16130 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16131 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016132# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016133 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016134 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135}
16136#endif
16137
16138/*
16139 * Get the value of internal variable "name".
16140 * Return OK or FAIL.
16141 */
16142 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016143get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144 char_u *name;
16145 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016146 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016147 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148{
16149 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016150 typval_T *tv = NULL;
16151 typval_T atv;
16152 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154
16155 /* truncate the name, so that we can use strcmp() */
16156 cc = name[len];
16157 name[len] = NUL;
16158
16159 /*
16160 * Check for "b:changedtick".
16161 */
16162 if (STRCMP(name, "b:changedtick") == 0)
16163 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016164 atv.v_type = VAR_NUMBER;
16165 atv.vval.v_number = curbuf->b_changedtick;
16166 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167 }
16168
16169 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170 * Check for user-defined variables.
16171 */
16172 else
16173 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016174 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016176 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177 }
16178
Bram Moolenaare9a41262005-01-15 22:18:47 +000016179 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016181 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016182 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183 ret = FAIL;
16184 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016185 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016186 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187
16188 name[len] = cc;
16189
16190 return ret;
16191}
16192
16193/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016194 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16195 * Also handle function call with Funcref variable: func(expr)
16196 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16197 */
16198 static int
16199handle_subscript(arg, rettv, evaluate, verbose)
16200 char_u **arg;
16201 typval_T *rettv;
16202 int evaluate; /* do more than finding the end */
16203 int verbose; /* give error messages */
16204{
16205 int ret = OK;
16206 dict_T *selfdict = NULL;
16207 char_u *s;
16208 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016209 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016210
16211 while (ret == OK
16212 && (**arg == '['
16213 || (**arg == '.' && rettv->v_type == VAR_DICT)
16214 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16215 && !vim_iswhite(*(*arg - 1)))
16216 {
16217 if (**arg == '(')
16218 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016219 /* need to copy the funcref so that we can clear rettv */
16220 functv = *rettv;
16221 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016222
16223 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016224 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016225 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016226 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16227 &len, evaluate, selfdict);
16228
16229 /* Clear the funcref afterwards, so that deleting it while
16230 * evaluating the arguments is possible (see test55). */
16231 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016232
16233 /* Stop the expression evaluation when immediately aborting on
16234 * error, or when an interrupt occurred or an exception was thrown
16235 * but not caught. */
16236 if (aborting())
16237 {
16238 if (ret == OK)
16239 clear_tv(rettv);
16240 ret = FAIL;
16241 }
16242 dict_unref(selfdict);
16243 selfdict = NULL;
16244 }
16245 else /* **arg == '[' || **arg == '.' */
16246 {
16247 dict_unref(selfdict);
16248 if (rettv->v_type == VAR_DICT)
16249 {
16250 selfdict = rettv->vval.v_dict;
16251 if (selfdict != NULL)
16252 ++selfdict->dv_refcount;
16253 }
16254 else
16255 selfdict = NULL;
16256 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16257 {
16258 clear_tv(rettv);
16259 ret = FAIL;
16260 }
16261 }
16262 }
16263 dict_unref(selfdict);
16264 return ret;
16265}
16266
16267/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016268 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16269 * value).
16270 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016271 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016272alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016273{
Bram Moolenaar33570922005-01-25 22:26:29 +000016274 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016275}
16276
16277/*
16278 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279 * The string "s" must have been allocated, it is consumed.
16280 * Return NULL for out of memory, the variable otherwise.
16281 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016282 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016283alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 char_u *s;
16285{
Bram Moolenaar33570922005-01-25 22:26:29 +000016286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016288 rettv = alloc_tv();
16289 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016291 rettv->v_type = VAR_STRING;
16292 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293 }
16294 else
16295 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016296 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297}
16298
16299/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016300 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016302 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016303free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016304 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305{
16306 if (varp != NULL)
16307 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016308 switch (varp->v_type)
16309 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016310 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016311 func_unref(varp->vval.v_string);
16312 /*FALLTHROUGH*/
16313 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016314 vim_free(varp->vval.v_string);
16315 break;
16316 case VAR_LIST:
16317 list_unref(varp->vval.v_list);
16318 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016319 case VAR_DICT:
16320 dict_unref(varp->vval.v_dict);
16321 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016322 case VAR_NUMBER:
16323 case VAR_UNKNOWN:
16324 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016325 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016326 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016327 break;
16328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329 vim_free(varp);
16330 }
16331}
16332
16333/*
16334 * Free the memory for a variable value and set the value to NULL or 0.
16335 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016336 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016337clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016338 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339{
16340 if (varp != NULL)
16341 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016342 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016344 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016345 func_unref(varp->vval.v_string);
16346 /*FALLTHROUGH*/
16347 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016348 vim_free(varp->vval.v_string);
16349 varp->vval.v_string = NULL;
16350 break;
16351 case VAR_LIST:
16352 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016353 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016354 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016355 case VAR_DICT:
16356 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016357 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016358 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016359 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016360 varp->vval.v_number = 0;
16361 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016362 case VAR_UNKNOWN:
16363 break;
16364 default:
16365 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016367 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368 }
16369}
16370
16371/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016372 * Set the value of a variable to NULL without freeing items.
16373 */
16374 static void
16375init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016376 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016377{
16378 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016379 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016380}
16381
16382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383 * Get the number value of a variable.
16384 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016385 * For incompatible types, return 0.
16386 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16387 * caller of incompatible types: it sets *denote to TRUE if "denote"
16388 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389 */
16390 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016391get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016392 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016394 int error = FALSE;
16395
16396 return get_tv_number_chk(varp, &error); /* return 0L on error */
16397}
16398
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016399 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016400get_tv_number_chk(varp, denote)
16401 typval_T *varp;
16402 int *denote;
16403{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016404 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016406 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016408 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016409 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016410 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016411 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016412 break;
16413 case VAR_STRING:
16414 if (varp->vval.v_string != NULL)
16415 vim_str2nr(varp->vval.v_string, NULL, NULL,
16416 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016417 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016418 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016419 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016420 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016421 case VAR_DICT:
16422 EMSG(_("E728: Using a Dictionary as a number"));
16423 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016424 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016425 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016426 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016428 if (denote == NULL) /* useful for values that must be unsigned */
16429 n = -1;
16430 else
16431 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016432 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433}
16434
16435/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016436 * Get the lnum from the first argument.
16437 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016438 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016439 */
16440 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016441get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016442 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443{
Bram Moolenaar33570922005-01-25 22:26:29 +000016444 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 linenr_T lnum;
16446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016447 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448 if (lnum == 0) /* no valid number, try using line() */
16449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016450 rettv.v_type = VAR_NUMBER;
16451 f_line(argvars, &rettv);
16452 lnum = rettv.vval.v_number;
16453 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 }
16455 return lnum;
16456}
16457
16458/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016459 * Get the lnum from the first argument.
16460 * Also accepts "$", then "buf" is used.
16461 * Returns 0 on error.
16462 */
16463 static linenr_T
16464get_tv_lnum_buf(argvars, buf)
16465 typval_T *argvars;
16466 buf_T *buf;
16467{
16468 if (argvars[0].v_type == VAR_STRING
16469 && argvars[0].vval.v_string != NULL
16470 && argvars[0].vval.v_string[0] == '$'
16471 && buf != NULL)
16472 return buf->b_ml.ml_line_count;
16473 return get_tv_number_chk(&argvars[0], NULL);
16474}
16475
16476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477 * Get the string value of a variable.
16478 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016479 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16480 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481 * If the String variable has never been set, return an empty string.
16482 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016483 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16484 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485 */
16486 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016487get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016488 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016489{
16490 static char_u mybuf[NUMBUFLEN];
16491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016492 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493}
16494
16495 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016496get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016497 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016498 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016500 char_u *res = get_tv_string_buf_chk(varp, buf);
16501
16502 return res != NULL ? res : (char_u *)"";
16503}
16504
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016505 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016506get_tv_string_chk(varp)
16507 typval_T *varp;
16508{
16509 static char_u mybuf[NUMBUFLEN];
16510
16511 return get_tv_string_buf_chk(varp, mybuf);
16512}
16513
16514 static char_u *
16515get_tv_string_buf_chk(varp, buf)
16516 typval_T *varp;
16517 char_u *buf;
16518{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016519 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016521 case VAR_NUMBER:
16522 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16523 return buf;
16524 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016525 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016526 break;
16527 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016528 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016529 break;
16530 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016531 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016532 break;
16533 case VAR_STRING:
16534 if (varp->vval.v_string != NULL)
16535 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016536 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016537 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016538 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016539 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016541 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542}
16543
16544/*
16545 * Find variable "name" in the list of variables.
16546 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016547 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016548 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016549 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016551 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016552find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016554 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016557 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558
Bram Moolenaara7043832005-01-21 11:56:39 +000016559 ht = find_var_ht(name, &varname);
16560 if (htp != NULL)
16561 *htp = ht;
16562 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016563 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016564 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565}
16566
16567/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016568 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016569 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016571 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016572find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016573 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016574 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016575 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016576{
Bram Moolenaar33570922005-01-25 22:26:29 +000016577 hashitem_T *hi;
16578
16579 if (*varname == NUL)
16580 {
16581 /* Must be something like "s:", otherwise "ht" would be NULL. */
16582 switch (varname[-2])
16583 {
16584 case 's': return &SCRIPT_SV(current_SID).sv_var;
16585 case 'g': return &globvars_var;
16586 case 'v': return &vimvars_var;
16587 case 'b': return &curbuf->b_bufvar;
16588 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016589 case 'l': return current_funccal == NULL
16590 ? NULL : &current_funccal->l_vars_var;
16591 case 'a': return current_funccal == NULL
16592 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016593 }
16594 return NULL;
16595 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016596
16597 hi = hash_find(ht, varname);
16598 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016599 {
16600 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016601 * worked find the variable again. Don't auto-load a script if it was
16602 * loaded already, otherwise it would be loaded every time when
16603 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016604 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016605 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016606 hi = hash_find(ht, varname);
16607 if (HASHITEM_EMPTY(hi))
16608 return NULL;
16609 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016610 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016611}
16612
16613/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016614 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016615 * Set "varname" to the start of name without ':'.
16616 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016617 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016618find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619 char_u *name;
16620 char_u **varname;
16621{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016622 hashitem_T *hi;
16623
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624 if (name[1] != ':')
16625 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016626 /* The name must not start with a colon or #. */
16627 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 return NULL;
16629 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016630
16631 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016632 hi = hash_find(&compat_hashtab, name);
16633 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016634 return &compat_hashtab;
16635
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016637 return &globvarht; /* global variable */
16638 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 }
16640 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016641 if (*name == 'g') /* global variable */
16642 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016643 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16644 */
16645 if (vim_strchr(name + 2, ':') != NULL
16646 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016647 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016649 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016651 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016652 if (*name == 'v') /* v: variable */
16653 return &vimvarht;
16654 if (*name == 'a' && current_funccal != NULL) /* function argument */
16655 return &current_funccal->l_avars.dv_hashtab;
16656 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16657 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658 if (*name == 's' /* script variable */
16659 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16660 return &SCRIPT_VARS(current_SID);
16661 return NULL;
16662}
16663
16664/*
16665 * Get the string value of a (global/local) variable.
16666 * Returns NULL when it doesn't exist.
16667 */
16668 char_u *
16669get_var_value(name)
16670 char_u *name;
16671{
Bram Moolenaar33570922005-01-25 22:26:29 +000016672 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673
Bram Moolenaara7043832005-01-21 11:56:39 +000016674 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675 if (v == NULL)
16676 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016677 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678}
16679
16680/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016681 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682 * sourcing this script and when executing functions defined in the script.
16683 */
16684 void
16685new_script_vars(id)
16686 scid_T id;
16687{
Bram Moolenaara7043832005-01-21 11:56:39 +000016688 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016689 hashtab_T *ht;
16690 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016691
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16693 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016694 /* Re-allocating ga_data means that an ht_array pointing to
16695 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016696 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016697 for (i = 1; i <= ga_scripts.ga_len; ++i)
16698 {
16699 ht = &SCRIPT_VARS(i);
16700 if (ht->ht_mask == HT_INIT_SIZE - 1)
16701 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016702 sv = &SCRIPT_SV(i);
16703 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016704 }
16705
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706 while (ga_scripts.ga_len < id)
16707 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016708 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16709 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711 }
16712 }
16713}
16714
16715/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016716 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16717 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 */
16719 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016720init_var_dict(dict, dict_var)
16721 dict_T *dict;
16722 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723{
Bram Moolenaar33570922005-01-25 22:26:29 +000016724 hash_init(&dict->dv_hashtab);
16725 dict->dv_refcount = 99999;
16726 dict_var->di_tv.vval.v_dict = dict;
16727 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016728 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016729 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16730 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731}
16732
16733/*
16734 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016735 * Frees all allocated variables and the value they contain.
16736 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737 */
16738 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016739vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016740 hashtab_T *ht;
16741{
16742 vars_clear_ext(ht, TRUE);
16743}
16744
16745/*
16746 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16747 */
16748 static void
16749vars_clear_ext(ht, free_val)
16750 hashtab_T *ht;
16751 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752{
Bram Moolenaara7043832005-01-21 11:56:39 +000016753 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016754 hashitem_T *hi;
16755 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756
Bram Moolenaar33570922005-01-25 22:26:29 +000016757 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016758 todo = ht->ht_used;
16759 for (hi = ht->ht_array; todo > 0; ++hi)
16760 {
16761 if (!HASHITEM_EMPTY(hi))
16762 {
16763 --todo;
16764
Bram Moolenaar33570922005-01-25 22:26:29 +000016765 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016766 * ht_array might change then. hash_clear() takes care of it
16767 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016768 v = HI2DI(hi);
16769 if (free_val)
16770 clear_tv(&v->di_tv);
16771 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16772 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016773 }
16774 }
16775 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016776 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777}
16778
Bram Moolenaara7043832005-01-21 11:56:39 +000016779/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016780 * Delete a variable from hashtab "ht" at item "hi".
16781 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016784delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016785 hashtab_T *ht;
16786 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787{
Bram Moolenaar33570922005-01-25 22:26:29 +000016788 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016789
16790 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016791 clear_tv(&di->di_tv);
16792 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016793}
16794
16795/*
16796 * List the value of one internal variable.
16797 */
16798 static void
16799list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016800 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801 char_u *prefix;
16802{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016803 char_u *tofree;
16804 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016805 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016806
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016807 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000016808 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016809 s == NULL ? (char_u *)"" : s);
16810 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811}
16812
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 static void
16814list_one_var_a(prefix, name, type, string)
16815 char_u *prefix;
16816 char_u *name;
16817 int type;
16818 char_u *string;
16819{
16820 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16821 if (name != NULL) /* "a:" vars don't have a name stored */
16822 msg_puts(name);
16823 msg_putchar(' ');
16824 msg_advance(22);
16825 if (type == VAR_NUMBER)
16826 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016827 else if (type == VAR_FUNC)
16828 msg_putchar('*');
16829 else if (type == VAR_LIST)
16830 {
16831 msg_putchar('[');
16832 if (*string == '[')
16833 ++string;
16834 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016835 else if (type == VAR_DICT)
16836 {
16837 msg_putchar('{');
16838 if (*string == '{')
16839 ++string;
16840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 else
16842 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016843
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016845
16846 if (type == VAR_FUNC)
16847 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848}
16849
16850/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016851 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 * If the variable already exists, the value is updated.
16853 * Otherwise the variable is created.
16854 */
16855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016856set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016858 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016859 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860{
Bram Moolenaar33570922005-01-25 22:26:29 +000016861 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016863 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016864 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016866 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016867 {
16868 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16869 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16870 ? name[2] : name[0]))
16871 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016872 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016873 return;
16874 }
16875 if (function_exists(name))
16876 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016877 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016878 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016879 return;
16880 }
16881 }
16882
Bram Moolenaara7043832005-01-21 11:56:39 +000016883 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016884 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016885 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016886 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016887 return;
16888 }
16889
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016890 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016891 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016893 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016894 if (var_check_ro(v->di_flags, name)
16895 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016896 return;
16897 if (v->di_tv.v_type != tv->v_type
16898 && !((v->di_tv.v_type == VAR_STRING
16899 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016900 && (tv->v_type == VAR_STRING
16901 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016902 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016903 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016904 return;
16905 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016906
16907 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016908 * Handle setting internal v: variables separately: we don't change
16909 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016910 */
16911 if (ht == &vimvarht)
16912 {
16913 if (v->di_tv.v_type == VAR_STRING)
16914 {
16915 vim_free(v->di_tv.vval.v_string);
16916 if (copy || tv->v_type != VAR_STRING)
16917 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16918 else
16919 {
16920 /* Take over the string to avoid an extra alloc/free. */
16921 v->di_tv.vval.v_string = tv->vval.v_string;
16922 tv->vval.v_string = NULL;
16923 }
16924 }
16925 else if (v->di_tv.v_type != VAR_NUMBER)
16926 EMSG2(_(e_intern2), "set_var()");
16927 else
16928 v->di_tv.vval.v_number = get_tv_number(tv);
16929 return;
16930 }
16931
16932 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 }
16934 else /* add a new variable */
16935 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016936 /* Make sure the variable name is valid. */
16937 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000016938 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
16939 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000016940 {
16941 EMSG2(_(e_illvar), varname);
16942 return;
16943 }
16944
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016945 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16946 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016947 if (v == NULL)
16948 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016949 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016950 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016952 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953 return;
16954 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016955 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016958 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016959 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016960 else
16961 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016962 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016963 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016964 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966}
16967
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016968/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016969 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16970 * Also give an error message.
16971 */
16972 static int
16973var_check_ro(flags, name)
16974 int flags;
16975 char_u *name;
16976{
16977 if (flags & DI_FLAGS_RO)
16978 {
16979 EMSG2(_(e_readonlyvar), name);
16980 return TRUE;
16981 }
16982 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16983 {
16984 EMSG2(_(e_readonlysbx), name);
16985 return TRUE;
16986 }
16987 return FALSE;
16988}
16989
16990/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016991 * Return TRUE if typeval "tv" is set to be locked (immutable).
16992 * Also give an error message, using "name".
16993 */
16994 static int
16995tv_check_lock(lock, name)
16996 int lock;
16997 char_u *name;
16998{
16999 if (lock & VAR_LOCKED)
17000 {
17001 EMSG2(_("E741: Value is locked: %s"),
17002 name == NULL ? (char_u *)_("Unknown") : name);
17003 return TRUE;
17004 }
17005 if (lock & VAR_FIXED)
17006 {
17007 EMSG2(_("E742: Cannot change value of %s"),
17008 name == NULL ? (char_u *)_("Unknown") : name);
17009 return TRUE;
17010 }
17011 return FALSE;
17012}
17013
17014/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017015 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017016 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017017 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017020copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017021 typval_T *from;
17022 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017024 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017025 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017026 switch (from->v_type)
17027 {
17028 case VAR_NUMBER:
17029 to->vval.v_number = from->vval.v_number;
17030 break;
17031 case VAR_STRING:
17032 case VAR_FUNC:
17033 if (from->vval.v_string == NULL)
17034 to->vval.v_string = NULL;
17035 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017036 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017037 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017038 if (from->v_type == VAR_FUNC)
17039 func_ref(to->vval.v_string);
17040 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017041 break;
17042 case VAR_LIST:
17043 if (from->vval.v_list == NULL)
17044 to->vval.v_list = NULL;
17045 else
17046 {
17047 to->vval.v_list = from->vval.v_list;
17048 ++to->vval.v_list->lv_refcount;
17049 }
17050 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017051 case VAR_DICT:
17052 if (from->vval.v_dict == NULL)
17053 to->vval.v_dict = NULL;
17054 else
17055 {
17056 to->vval.v_dict = from->vval.v_dict;
17057 ++to->vval.v_dict->dv_refcount;
17058 }
17059 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017060 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017061 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017062 break;
17063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064}
17065
17066/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017067 * Make a copy of an item.
17068 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017069 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17070 * reference to an already copied list/dict can be used.
17071 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017072 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017073 static int
17074item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017075 typval_T *from;
17076 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017077 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017078 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017079{
17080 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017081 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017082
Bram Moolenaar33570922005-01-25 22:26:29 +000017083 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017084 {
17085 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017086 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017087 }
17088 ++recurse;
17089
17090 switch (from->v_type)
17091 {
17092 case VAR_NUMBER:
17093 case VAR_STRING:
17094 case VAR_FUNC:
17095 copy_tv(from, to);
17096 break;
17097 case VAR_LIST:
17098 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017099 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017100 if (from->vval.v_list == NULL)
17101 to->vval.v_list = NULL;
17102 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17103 {
17104 /* use the copy made earlier */
17105 to->vval.v_list = from->vval.v_list->lv_copylist;
17106 ++to->vval.v_list->lv_refcount;
17107 }
17108 else
17109 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17110 if (to->vval.v_list == NULL)
17111 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017112 break;
17113 case VAR_DICT:
17114 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017115 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017116 if (from->vval.v_dict == NULL)
17117 to->vval.v_dict = NULL;
17118 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17119 {
17120 /* use the copy made earlier */
17121 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17122 ++to->vval.v_dict->dv_refcount;
17123 }
17124 else
17125 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17126 if (to->vval.v_dict == NULL)
17127 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017128 break;
17129 default:
17130 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017131 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017132 }
17133 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017134 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017135}
17136
17137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138 * ":echo expr1 ..." print each argument separated with a space, add a
17139 * newline at the end.
17140 * ":echon expr1 ..." print each argument plain.
17141 */
17142 void
17143ex_echo(eap)
17144 exarg_T *eap;
17145{
17146 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017147 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017148 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149 char_u *p;
17150 int needclr = TRUE;
17151 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017152 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153
17154 if (eap->skip)
17155 ++emsg_skip;
17156 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17157 {
17158 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017159 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160 {
17161 /*
17162 * Report the invalid expression unless the expression evaluation
17163 * has been cancelled due to an aborting error, an interrupt, or an
17164 * exception.
17165 */
17166 if (!aborting())
17167 EMSG2(_(e_invexpr2), p);
17168 break;
17169 }
17170 if (!eap->skip)
17171 {
17172 if (atstart)
17173 {
17174 atstart = FALSE;
17175 /* Call msg_start() after eval1(), evaluating the expression
17176 * may cause a message to appear. */
17177 if (eap->cmdidx == CMD_echo)
17178 msg_start();
17179 }
17180 else if (eap->cmdidx == CMD_echo)
17181 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017182 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017183 if (p != NULL)
17184 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017186 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017188 if (*p != TAB && needclr)
17189 {
17190 /* remove any text still there from the command */
17191 msg_clr_eos();
17192 needclr = FALSE;
17193 }
17194 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195 }
17196 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017197 {
17198#ifdef FEAT_MBYTE
17199 if (has_mbyte)
17200 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017201 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017202
17203 (void)msg_outtrans_len_attr(p, i, echo_attr);
17204 p += i - 1;
17205 }
17206 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017208 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017211 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017213 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017214 arg = skipwhite(arg);
17215 }
17216 eap->nextcmd = check_nextcmd(arg);
17217
17218 if (eap->skip)
17219 --emsg_skip;
17220 else
17221 {
17222 /* remove text that may still be there from the command */
17223 if (needclr)
17224 msg_clr_eos();
17225 if (eap->cmdidx == CMD_echo)
17226 msg_end();
17227 }
17228}
17229
17230/*
17231 * ":echohl {name}".
17232 */
17233 void
17234ex_echohl(eap)
17235 exarg_T *eap;
17236{
17237 int id;
17238
17239 id = syn_name2id(eap->arg);
17240 if (id == 0)
17241 echo_attr = 0;
17242 else
17243 echo_attr = syn_id2attr(id);
17244}
17245
17246/*
17247 * ":execute expr1 ..." execute the result of an expression.
17248 * ":echomsg expr1 ..." Print a message
17249 * ":echoerr expr1 ..." Print an error
17250 * Each gets spaces around each argument and a newline at the end for
17251 * echo commands
17252 */
17253 void
17254ex_execute(eap)
17255 exarg_T *eap;
17256{
17257 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017258 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259 int ret = OK;
17260 char_u *p;
17261 garray_T ga;
17262 int len;
17263 int save_did_emsg;
17264
17265 ga_init2(&ga, 1, 80);
17266
17267 if (eap->skip)
17268 ++emsg_skip;
17269 while (*arg != NUL && *arg != '|' && *arg != '\n')
17270 {
17271 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017272 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 {
17274 /*
17275 * Report the invalid expression unless the expression evaluation
17276 * has been cancelled due to an aborting error, an interrupt, or an
17277 * exception.
17278 */
17279 if (!aborting())
17280 EMSG2(_(e_invexpr2), p);
17281 ret = FAIL;
17282 break;
17283 }
17284
17285 if (!eap->skip)
17286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017287 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288 len = (int)STRLEN(p);
17289 if (ga_grow(&ga, len + 2) == FAIL)
17290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017291 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 ret = FAIL;
17293 break;
17294 }
17295 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 ga.ga_len += len;
17299 }
17300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017301 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302 arg = skipwhite(arg);
17303 }
17304
17305 if (ret != FAIL && ga.ga_data != NULL)
17306 {
17307 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017308 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017310 out_flush();
17311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 else if (eap->cmdidx == CMD_echoerr)
17313 {
17314 /* We don't want to abort following commands, restore did_emsg. */
17315 save_did_emsg = did_emsg;
17316 EMSG((char_u *)ga.ga_data);
17317 if (!force_abort)
17318 did_emsg = save_did_emsg;
17319 }
17320 else if (eap->cmdidx == CMD_execute)
17321 do_cmdline((char_u *)ga.ga_data,
17322 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17323 }
17324
17325 ga_clear(&ga);
17326
17327 if (eap->skip)
17328 --emsg_skip;
17329
17330 eap->nextcmd = check_nextcmd(arg);
17331}
17332
17333/*
17334 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17335 * "arg" points to the "&" or '+' when called, to "option" when returning.
17336 * Returns NULL when no option name found. Otherwise pointer to the char
17337 * after the option name.
17338 */
17339 static char_u *
17340find_option_end(arg, opt_flags)
17341 char_u **arg;
17342 int *opt_flags;
17343{
17344 char_u *p = *arg;
17345
17346 ++p;
17347 if (*p == 'g' && p[1] == ':')
17348 {
17349 *opt_flags = OPT_GLOBAL;
17350 p += 2;
17351 }
17352 else if (*p == 'l' && p[1] == ':')
17353 {
17354 *opt_flags = OPT_LOCAL;
17355 p += 2;
17356 }
17357 else
17358 *opt_flags = 0;
17359
17360 if (!ASCII_ISALPHA(*p))
17361 return NULL;
17362 *arg = p;
17363
17364 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17365 p += 4; /* termcap option */
17366 else
17367 while (ASCII_ISALPHA(*p))
17368 ++p;
17369 return p;
17370}
17371
17372/*
17373 * ":function"
17374 */
17375 void
17376ex_function(eap)
17377 exarg_T *eap;
17378{
17379 char_u *theline;
17380 int j;
17381 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 char_u *name = NULL;
17384 char_u *p;
17385 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017386 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 garray_T newargs;
17388 garray_T newlines;
17389 int varargs = FALSE;
17390 int mustend = FALSE;
17391 int flags = 0;
17392 ufunc_T *fp;
17393 int indent;
17394 int nesting;
17395 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017396 dictitem_T *v;
17397 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017398 static int func_nr = 0; /* number for nameless function */
17399 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017400 hashtab_T *ht;
17401 int todo;
17402 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403
17404 /*
17405 * ":function" without argument: list functions.
17406 */
17407 if (ends_excmd(*eap->arg))
17408 {
17409 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017410 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017411 todo = func_hashtab.ht_used;
17412 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017413 {
17414 if (!HASHITEM_EMPTY(hi))
17415 {
17416 --todo;
17417 fp = HI2UF(hi);
17418 if (!isdigit(*fp->uf_name))
17419 list_func_head(fp, FALSE);
17420 }
17421 }
17422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423 eap->nextcmd = check_nextcmd(eap->arg);
17424 return;
17425 }
17426
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017427 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017428 * ":function /pat": list functions matching pattern.
17429 */
17430 if (*eap->arg == '/')
17431 {
17432 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17433 if (!eap->skip)
17434 {
17435 regmatch_T regmatch;
17436
17437 c = *p;
17438 *p = NUL;
17439 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17440 *p = c;
17441 if (regmatch.regprog != NULL)
17442 {
17443 regmatch.rm_ic = p_ic;
17444
17445 todo = func_hashtab.ht_used;
17446 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17447 {
17448 if (!HASHITEM_EMPTY(hi))
17449 {
17450 --todo;
17451 fp = HI2UF(hi);
17452 if (!isdigit(*fp->uf_name)
17453 && vim_regexec(&regmatch, fp->uf_name, 0))
17454 list_func_head(fp, FALSE);
17455 }
17456 }
17457 }
17458 }
17459 if (*p == '/')
17460 ++p;
17461 eap->nextcmd = check_nextcmd(p);
17462 return;
17463 }
17464
17465 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017466 * Get the function name. There are these situations:
17467 * func normal function name
17468 * "name" == func, "fudi.fd_dict" == NULL
17469 * dict.func new dictionary entry
17470 * "name" == NULL, "fudi.fd_dict" set,
17471 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17472 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017473 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017474 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17475 * dict.func existing dict entry that's not a Funcref
17476 * "name" == NULL, "fudi.fd_dict" set,
17477 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017480 name = trans_function_name(&p, eap->skip, 0, &fudi);
17481 paren = (vim_strchr(p, '(') != NULL);
17482 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483 {
17484 /*
17485 * Return on an invalid expression in braces, unless the expression
17486 * evaluation has been cancelled due to an aborting error, an
17487 * interrupt, or an exception.
17488 */
17489 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017490 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017491 if (!eap->skip && fudi.fd_newkey != NULL)
17492 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017493 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496 else
17497 eap->skip = TRUE;
17498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499 /* An error in a function call during evaluation of an expression in magic
17500 * braces should not cause the function not to be defined. */
17501 saved_did_emsg = did_emsg;
17502 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503
17504 /*
17505 * ":function func" with only function name: list function.
17506 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017507 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508 {
17509 if (!ends_excmd(*skipwhite(p)))
17510 {
17511 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017512 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513 }
17514 eap->nextcmd = check_nextcmd(p);
17515 if (eap->nextcmd != NULL)
17516 *p = NUL;
17517 if (!eap->skip && !got_int)
17518 {
17519 fp = find_func(name);
17520 if (fp != NULL)
17521 {
17522 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017523 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524 {
17525 msg_putchar('\n');
17526 msg_outnum((long)(j + 1));
17527 if (j < 9)
17528 msg_putchar(' ');
17529 if (j < 99)
17530 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017531 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532 out_flush(); /* show a line at a time */
17533 ui_breakcheck();
17534 }
17535 if (!got_int)
17536 {
17537 msg_putchar('\n');
17538 msg_puts((char_u *)" endfunction");
17539 }
17540 }
17541 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017542 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017544 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545 }
17546
17547 /*
17548 * ":function name(arg1, arg2)" Define function.
17549 */
17550 p = skipwhite(p);
17551 if (*p != '(')
17552 {
17553 if (!eap->skip)
17554 {
17555 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017556 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557 }
17558 /* attempt to continue by skipping some text */
17559 if (vim_strchr(p, '(') != NULL)
17560 p = vim_strchr(p, '(');
17561 }
17562 p = skipwhite(p + 1);
17563
17564 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17565 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17566
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017567 if (!eap->skip)
17568 {
17569 /* Check the name of the function. */
17570 if (name != NULL)
17571 arg = name;
17572 else
17573 arg = fudi.fd_newkey;
17574 if (arg != NULL)
17575 {
17576 if (*arg == K_SPECIAL)
17577 j = 3;
17578 else
17579 j = 0;
17580 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17581 : eval_isnamec(arg[j])))
17582 ++j;
17583 if (arg[j] != NUL)
17584 emsg_funcname(_(e_invarg2), arg);
17585 }
17586 }
17587
Bram Moolenaar071d4272004-06-13 20:20:40 +000017588 /*
17589 * Isolate the arguments: "arg1, arg2, ...)"
17590 */
17591 while (*p != ')')
17592 {
17593 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17594 {
17595 varargs = TRUE;
17596 p += 3;
17597 mustend = TRUE;
17598 }
17599 else
17600 {
17601 arg = p;
17602 while (ASCII_ISALNUM(*p) || *p == '_')
17603 ++p;
17604 if (arg == p || isdigit(*arg)
17605 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17606 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17607 {
17608 if (!eap->skip)
17609 EMSG2(_("E125: Illegal argument: %s"), arg);
17610 break;
17611 }
17612 if (ga_grow(&newargs, 1) == FAIL)
17613 goto erret;
17614 c = *p;
17615 *p = NUL;
17616 arg = vim_strsave(arg);
17617 if (arg == NULL)
17618 goto erret;
17619 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17620 *p = c;
17621 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 if (*p == ',')
17623 ++p;
17624 else
17625 mustend = TRUE;
17626 }
17627 p = skipwhite(p);
17628 if (mustend && *p != ')')
17629 {
17630 if (!eap->skip)
17631 EMSG2(_(e_invarg2), eap->arg);
17632 break;
17633 }
17634 }
17635 ++p; /* skip the ')' */
17636
Bram Moolenaare9a41262005-01-15 22:18:47 +000017637 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 for (;;)
17639 {
17640 p = skipwhite(p);
17641 if (STRNCMP(p, "range", 5) == 0)
17642 {
17643 flags |= FC_RANGE;
17644 p += 5;
17645 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017646 else if (STRNCMP(p, "dict", 4) == 0)
17647 {
17648 flags |= FC_DICT;
17649 p += 4;
17650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 else if (STRNCMP(p, "abort", 5) == 0)
17652 {
17653 flags |= FC_ABORT;
17654 p += 5;
17655 }
17656 else
17657 break;
17658 }
17659
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017660 /* When there is a line break use what follows for the function body.
17661 * Makes 'exe "func Test()\n...\nendfunc"' work. */
17662 if (*p == '\n')
17663 line_arg = p + 1;
17664 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 EMSG(_(e_trailing));
17666
17667 /*
17668 * Read the body of the function, until ":endfunction" is found.
17669 */
17670 if (KeyTyped)
17671 {
17672 /* Check if the function already exists, don't let the user type the
17673 * whole function before telling him it doesn't work! For a script we
17674 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017675 if (!eap->skip && !eap->forceit)
17676 {
17677 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17678 EMSG(_(e_funcdict));
17679 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017680 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017683 if (!eap->skip && did_emsg)
17684 goto erret;
17685
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 msg_putchar('\n'); /* don't overwrite the function name */
17687 cmdline_row = msg_row;
17688 }
17689
17690 indent = 2;
17691 nesting = 0;
17692 for (;;)
17693 {
17694 msg_scroll = TRUE;
17695 need_wait_return = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017696 if (line_arg != NULL)
17697 {
17698 /* Use eap->arg, split up in parts by line breaks. */
17699 theline = line_arg;
17700 p = vim_strchr(theline, '\n');
17701 if (p == NULL)
17702 line_arg += STRLEN(line_arg);
17703 else
17704 {
17705 *p = NUL;
17706 line_arg = p + 1;
17707 }
17708 }
17709 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 theline = getcmdline(':', 0L, indent);
17711 else
17712 theline = eap->getline(':', eap->cookie, indent);
17713 if (KeyTyped)
17714 lines_left = Rows - 1;
17715 if (theline == NULL)
17716 {
17717 EMSG(_("E126: Missing :endfunction"));
17718 goto erret;
17719 }
17720
17721 if (skip_until != NULL)
17722 {
17723 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17724 * don't check for ":endfunc". */
17725 if (STRCMP(theline, skip_until) == 0)
17726 {
17727 vim_free(skip_until);
17728 skip_until = NULL;
17729 }
17730 }
17731 else
17732 {
17733 /* skip ':' and blanks*/
17734 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17735 ;
17736
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017737 /* Check for "endfunction". */
17738 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017740 if (line_arg == NULL)
17741 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742 break;
17743 }
17744
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017745 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 * at "end". */
17747 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17748 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017749 else if (STRNCMP(p, "if", 2) == 0
17750 || STRNCMP(p, "wh", 2) == 0
17751 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752 || STRNCMP(p, "try", 3) == 0)
17753 indent += 2;
17754
17755 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017756 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017758 if (*p == '!')
17759 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 p += eval_fname_script(p);
17761 if (ASCII_ISALPHA(*p))
17762 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017763 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 if (*skipwhite(p) == '(')
17765 {
17766 ++nesting;
17767 indent += 2;
17768 }
17769 }
17770 }
17771
17772 /* Check for ":append" or ":insert". */
17773 p = skip_range(p, NULL);
17774 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17775 || (p[0] == 'i'
17776 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17777 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17778 skip_until = vim_strsave((char_u *)".");
17779
17780 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17781 arg = skipwhite(skiptowhite(p));
17782 if (arg[0] == '<' && arg[1] =='<'
17783 && ((p[0] == 'p' && p[1] == 'y'
17784 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17785 || (p[0] == 'p' && p[1] == 'e'
17786 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17787 || (p[0] == 't' && p[1] == 'c'
17788 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17789 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17790 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017791 || (p[0] == 'm' && p[1] == 'z'
17792 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793 ))
17794 {
17795 /* ":python <<" continues until a dot, like ":append" */
17796 p = skipwhite(arg + 2);
17797 if (*p == NUL)
17798 skip_until = vim_strsave((char_u *)".");
17799 else
17800 skip_until = vim_strsave(p);
17801 }
17802 }
17803
17804 /* Add the line to the function. */
17805 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017806 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017807 if (line_arg == NULL)
17808 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017809 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017810 }
17811
17812 /* Copy the line to newly allocated memory. get_one_sourceline()
17813 * allocates 250 bytes per line, this saves 80% on average. The cost
17814 * is an extra alloc/free. */
17815 p = vim_strsave(theline);
17816 if (p != NULL)
17817 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017818 if (line_arg == NULL)
17819 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017820 theline = p;
17821 }
17822
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17824 newlines.ga_len++;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017825
17826 /* Check for end of eap->arg. */
17827 if (line_arg != NULL && *line_arg == NUL)
17828 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829 }
17830
17831 /* Don't define the function when skipping commands or when an error was
17832 * detected. */
17833 if (eap->skip || did_emsg)
17834 goto erret;
17835
17836 /*
17837 * If there are no errors, add the function
17838 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017839 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017840 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017841 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017842 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017843 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017844 emsg_funcname("E707: Function name conflicts with variable: %s",
17845 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017846 goto erret;
17847 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017848
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017849 fp = find_func(name);
17850 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017852 if (!eap->forceit)
17853 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017854 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017855 goto erret;
17856 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017857 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017858 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017859 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017860 name);
17861 goto erret;
17862 }
17863 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017864 ga_clear_strings(&(fp->uf_args));
17865 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017866 vim_free(name);
17867 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869 }
17870 else
17871 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017872 char numbuf[20];
17873
17874 fp = NULL;
17875 if (fudi.fd_newkey == NULL && !eap->forceit)
17876 {
17877 EMSG(_(e_funcdict));
17878 goto erret;
17879 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017880 if (fudi.fd_di == NULL)
17881 {
17882 /* Can't add a function to a locked dictionary */
17883 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17884 goto erret;
17885 }
17886 /* Can't change an existing function if it is locked */
17887 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17888 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017889
17890 /* Give the function a sequential number. Can only be used with a
17891 * Funcref! */
17892 vim_free(name);
17893 sprintf(numbuf, "%d", ++func_nr);
17894 name = vim_strsave((char_u *)numbuf);
17895 if (name == NULL)
17896 goto erret;
17897 }
17898
17899 if (fp == NULL)
17900 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017901 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017902 {
17903 int slen, plen;
17904 char_u *scriptname;
17905
17906 /* Check that the autoload name matches the script name. */
17907 j = FAIL;
17908 if (sourcing_name != NULL)
17909 {
17910 scriptname = autoload_name(name);
17911 if (scriptname != NULL)
17912 {
17913 p = vim_strchr(scriptname, '/');
17914 plen = STRLEN(p);
17915 slen = STRLEN(sourcing_name);
17916 if (slen > plen && fnamecmp(p,
17917 sourcing_name + slen - plen) == 0)
17918 j = OK;
17919 vim_free(scriptname);
17920 }
17921 }
17922 if (j == FAIL)
17923 {
17924 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17925 goto erret;
17926 }
17927 }
17928
17929 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 if (fp == NULL)
17931 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017932
17933 if (fudi.fd_dict != NULL)
17934 {
17935 if (fudi.fd_di == NULL)
17936 {
17937 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017938 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017939 if (fudi.fd_di == NULL)
17940 {
17941 vim_free(fp);
17942 goto erret;
17943 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017944 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17945 {
17946 vim_free(fudi.fd_di);
17947 goto erret;
17948 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017949 }
17950 else
17951 /* overwrite existing dict entry */
17952 clear_tv(&fudi.fd_di->di_tv);
17953 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017954 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017955 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017956 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017957 }
17958
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017960 STRCPY(fp->uf_name, name);
17961 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017963 fp->uf_args = newargs;
17964 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017965#ifdef FEAT_PROFILE
17966 fp->uf_tml_count = NULL;
17967 fp->uf_tml_total = NULL;
17968 fp->uf_tml_self = NULL;
17969 fp->uf_profiling = FALSE;
17970 if (prof_def_func())
17971 func_do_profile(fp);
17972#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017973 fp->uf_varargs = varargs;
17974 fp->uf_flags = flags;
17975 fp->uf_calls = 0;
17976 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017977 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978
17979erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980 ga_clear_strings(&newargs);
17981 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017982ret_free:
17983 vim_free(skip_until);
17984 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017985 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987}
17988
17989/*
17990 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017991 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017993 * flags:
17994 * TFN_INT: internal function name OK
17995 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996 * Advances "pp" to just after the function name (if no error).
17997 */
17998 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017999trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000 char_u **pp;
18001 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018002 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018003 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018005 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006 char_u *start;
18007 char_u *end;
18008 int lead;
18009 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018011 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018012
18013 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018014 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018016
18017 /* Check for hard coded <SNR>: already translated function ID (from a user
18018 * command). */
18019 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18020 && (*pp)[2] == (int)KE_SNR)
18021 {
18022 *pp += 3;
18023 len = get_id_len(pp) + 3;
18024 return vim_strnsave(start, len);
18025 }
18026
18027 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18028 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018030 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018032
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018033 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18034 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018035 if (end == start)
18036 {
18037 if (!skip)
18038 EMSG(_("E129: Function name required"));
18039 goto theend;
18040 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018041 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018042 {
18043 /*
18044 * Report an invalid expression in braces, unless the expression
18045 * evaluation has been cancelled due to an aborting error, an
18046 * interrupt, or an exception.
18047 */
18048 if (!aborting())
18049 {
18050 if (end != NULL)
18051 EMSG2(_(e_invarg2), start);
18052 }
18053 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018054 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018055 goto theend;
18056 }
18057
18058 if (lv.ll_tv != NULL)
18059 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018060 if (fdp != NULL)
18061 {
18062 fdp->fd_dict = lv.ll_dict;
18063 fdp->fd_newkey = lv.ll_newkey;
18064 lv.ll_newkey = NULL;
18065 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018066 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018067 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18068 {
18069 name = vim_strsave(lv.ll_tv->vval.v_string);
18070 *pp = end;
18071 }
18072 else
18073 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018074 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18075 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018076 EMSG(_(e_funcref));
18077 else
18078 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018079 name = NULL;
18080 }
18081 goto theend;
18082 }
18083
18084 if (lv.ll_name == NULL)
18085 {
18086 /* Error found, but continue after the function name. */
18087 *pp = end;
18088 goto theend;
18089 }
18090
18091 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018092 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018093 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018094 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18095 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18096 {
18097 /* When there was "s:" already or the name expanded to get a
18098 * leading "s:" then remove it. */
18099 lv.ll_name += 2;
18100 len -= 2;
18101 lead = 2;
18102 }
18103 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018104 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018105 {
18106 if (lead == 2) /* skip over "s:" */
18107 lv.ll_name += 2;
18108 len = (int)(end - lv.ll_name);
18109 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018110
18111 /*
18112 * Copy the function name to allocated memory.
18113 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18114 * Accept <SNR>123_name() outside a script.
18115 */
18116 if (skip)
18117 lead = 0; /* do nothing */
18118 else if (lead > 0)
18119 {
18120 lead = 3;
18121 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18122 {
18123 if (current_SID <= 0)
18124 {
18125 EMSG(_(e_usingsid));
18126 goto theend;
18127 }
18128 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18129 lead += (int)STRLEN(sid_buf);
18130 }
18131 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018132 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018133 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018134 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018135 goto theend;
18136 }
18137 name = alloc((unsigned)(len + lead + 1));
18138 if (name != NULL)
18139 {
18140 if (lead > 0)
18141 {
18142 name[0] = K_SPECIAL;
18143 name[1] = KS_EXTRA;
18144 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018145 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018146 STRCPY(name + 3, sid_buf);
18147 }
18148 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18149 name[len + lead] = NUL;
18150 }
18151 *pp = end;
18152
18153theend:
18154 clear_lval(&lv);
18155 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156}
18157
18158/*
18159 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18160 * Return 2 if "p" starts with "s:".
18161 * Return 0 otherwise.
18162 */
18163 static int
18164eval_fname_script(p)
18165 char_u *p;
18166{
18167 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18168 || STRNICMP(p + 1, "SNR>", 4) == 0))
18169 return 5;
18170 if (p[0] == 's' && p[1] == ':')
18171 return 2;
18172 return 0;
18173}
18174
18175/*
18176 * Return TRUE if "p" starts with "<SID>" or "s:".
18177 * Only works if eval_fname_script() returned non-zero for "p"!
18178 */
18179 static int
18180eval_fname_sid(p)
18181 char_u *p;
18182{
18183 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18184}
18185
18186/*
18187 * List the head of the function: "name(arg1, arg2)".
18188 */
18189 static void
18190list_func_head(fp, indent)
18191 ufunc_T *fp;
18192 int indent;
18193{
18194 int j;
18195
18196 msg_start();
18197 if (indent)
18198 MSG_PUTS(" ");
18199 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018200 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201 {
18202 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018203 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 }
18205 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018206 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018208 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 {
18210 if (j)
18211 MSG_PUTS(", ");
18212 msg_puts(FUNCARG(fp, j));
18213 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018214 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215 {
18216 if (j)
18217 MSG_PUTS(", ");
18218 MSG_PUTS("...");
18219 }
18220 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018221 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018222 if (p_verbose > 0)
18223 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224}
18225
18226/*
18227 * Find a function by name, return pointer to it in ufuncs.
18228 * Return NULL for unknown function.
18229 */
18230 static ufunc_T *
18231find_func(name)
18232 char_u *name;
18233{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018234 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018236 hi = hash_find(&func_hashtab, name);
18237 if (!HASHITEM_EMPTY(hi))
18238 return HI2UF(hi);
18239 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240}
18241
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018242#if defined(EXITFREE) || defined(PROTO)
18243 void
18244free_all_functions()
18245{
18246 hashitem_T *hi;
18247
18248 /* Need to start all over every time, because func_free() may change the
18249 * hash table. */
18250 while (func_hashtab.ht_used > 0)
18251 for (hi = func_hashtab.ht_array; ; ++hi)
18252 if (!HASHITEM_EMPTY(hi))
18253 {
18254 func_free(HI2UF(hi));
18255 break;
18256 }
18257}
18258#endif
18259
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018260/*
18261 * Return TRUE if a function "name" exists.
18262 */
18263 static int
18264function_exists(name)
18265 char_u *name;
18266{
18267 char_u *p = name;
18268 int n = FALSE;
18269
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018270 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018271 if (p != NULL)
18272 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018273 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018274 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018275 else
18276 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018277 vim_free(p);
18278 }
18279 return n;
18280}
18281
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018282/*
18283 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018284 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018285 */
18286 static int
18287builtin_function(name)
18288 char_u *name;
18289{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018290 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18291 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018292}
18293
Bram Moolenaar05159a02005-02-26 23:04:13 +000018294#if defined(FEAT_PROFILE) || defined(PROTO)
18295/*
18296 * Start profiling function "fp".
18297 */
18298 static void
18299func_do_profile(fp)
18300 ufunc_T *fp;
18301{
18302 fp->uf_tm_count = 0;
18303 profile_zero(&fp->uf_tm_self);
18304 profile_zero(&fp->uf_tm_total);
18305 if (fp->uf_tml_count == NULL)
18306 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18307 (sizeof(int) * fp->uf_lines.ga_len));
18308 if (fp->uf_tml_total == NULL)
18309 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18310 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18311 if (fp->uf_tml_self == NULL)
18312 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18313 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18314 fp->uf_tml_idx = -1;
18315 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18316 || fp->uf_tml_self == NULL)
18317 return; /* out of memory */
18318
18319 fp->uf_profiling = TRUE;
18320}
18321
18322/*
18323 * Dump the profiling results for all functions in file "fd".
18324 */
18325 void
18326func_dump_profile(fd)
18327 FILE *fd;
18328{
18329 hashitem_T *hi;
18330 int todo;
18331 ufunc_T *fp;
18332 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018333 ufunc_T **sorttab;
18334 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018335
18336 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018337 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18338
Bram Moolenaar05159a02005-02-26 23:04:13 +000018339 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18340 {
18341 if (!HASHITEM_EMPTY(hi))
18342 {
18343 --todo;
18344 fp = HI2UF(hi);
18345 if (fp->uf_profiling)
18346 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018347 if (sorttab != NULL)
18348 sorttab[st_len++] = fp;
18349
Bram Moolenaar05159a02005-02-26 23:04:13 +000018350 if (fp->uf_name[0] == K_SPECIAL)
18351 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18352 else
18353 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18354 if (fp->uf_tm_count == 1)
18355 fprintf(fd, "Called 1 time\n");
18356 else
18357 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18358 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18359 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18360 fprintf(fd, "\n");
18361 fprintf(fd, "count total (s) self (s)\n");
18362
18363 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18364 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018365 prof_func_line(fd, fp->uf_tml_count[i],
18366 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018367 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18368 }
18369 fprintf(fd, "\n");
18370 }
18371 }
18372 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018373
18374 if (sorttab != NULL && st_len > 0)
18375 {
18376 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18377 prof_total_cmp);
18378 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18379 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18380 prof_self_cmp);
18381 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18382 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018383}
Bram Moolenaar73830342005-02-28 22:48:19 +000018384
18385 static void
18386prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18387 FILE *fd;
18388 ufunc_T **sorttab;
18389 int st_len;
18390 char *title;
18391 int prefer_self; /* when equal print only self time */
18392{
18393 int i;
18394 ufunc_T *fp;
18395
18396 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18397 fprintf(fd, "count total (s) self (s) function\n");
18398 for (i = 0; i < 20 && i < st_len; ++i)
18399 {
18400 fp = sorttab[i];
18401 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18402 prefer_self);
18403 if (fp->uf_name[0] == K_SPECIAL)
18404 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18405 else
18406 fprintf(fd, " %s()\n", fp->uf_name);
18407 }
18408 fprintf(fd, "\n");
18409}
18410
18411/*
18412 * Print the count and times for one function or function line.
18413 */
18414 static void
18415prof_func_line(fd, count, total, self, prefer_self)
18416 FILE *fd;
18417 int count;
18418 proftime_T *total;
18419 proftime_T *self;
18420 int prefer_self; /* when equal print only self time */
18421{
18422 if (count > 0)
18423 {
18424 fprintf(fd, "%5d ", count);
18425 if (prefer_self && profile_equal(total, self))
18426 fprintf(fd, " ");
18427 else
18428 fprintf(fd, "%s ", profile_msg(total));
18429 if (!prefer_self && profile_equal(total, self))
18430 fprintf(fd, " ");
18431 else
18432 fprintf(fd, "%s ", profile_msg(self));
18433 }
18434 else
18435 fprintf(fd, " ");
18436}
18437
18438/*
18439 * Compare function for total time sorting.
18440 */
18441 static int
18442#ifdef __BORLANDC__
18443_RTLENTRYF
18444#endif
18445prof_total_cmp(s1, s2)
18446 const void *s1;
18447 const void *s2;
18448{
18449 ufunc_T *p1, *p2;
18450
18451 p1 = *(ufunc_T **)s1;
18452 p2 = *(ufunc_T **)s2;
18453 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18454}
18455
18456/*
18457 * Compare function for self time sorting.
18458 */
18459 static int
18460#ifdef __BORLANDC__
18461_RTLENTRYF
18462#endif
18463prof_self_cmp(s1, s2)
18464 const void *s1;
18465 const void *s2;
18466{
18467 ufunc_T *p1, *p2;
18468
18469 p1 = *(ufunc_T **)s1;
18470 p2 = *(ufunc_T **)s2;
18471 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18472}
18473
Bram Moolenaar05159a02005-02-26 23:04:13 +000018474#endif
18475
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018476/* The names of packages that once were loaded is remembered. */
18477static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18478
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018479/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018480 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018481 * Return TRUE if a package was loaded.
18482 */
18483 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018484script_autoload(name, reload)
18485 char_u *name;
18486 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018487{
18488 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018489 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018490 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018491 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018492
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018493 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018494 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018495 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018496 return FALSE;
18497
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018498 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018499
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018500 /* Find the name in the list of previously loaded package names. Skip
18501 * "autoload/", it's always the same. */
18502 for (i = 0; i < ga_loaded.ga_len; ++i)
18503 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18504 break;
18505 if (!reload && i < ga_loaded.ga_len)
18506 ret = FALSE; /* was loaded already */
18507 else
18508 {
18509 /* Remember the name if it wasn't loaded already. */
18510 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18511 {
18512 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18513 tofree = NULL;
18514 }
18515
18516 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018517 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018518 ret = TRUE;
18519 }
18520
18521 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018522 return ret;
18523}
18524
18525/*
18526 * Return the autoload script name for a function or variable name.
18527 * Returns NULL when out of memory.
18528 */
18529 static char_u *
18530autoload_name(name)
18531 char_u *name;
18532{
18533 char_u *p;
18534 char_u *scriptname;
18535
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018536 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018537 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18538 if (scriptname == NULL)
18539 return FALSE;
18540 STRCPY(scriptname, "autoload/");
18541 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018542 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018543 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018544 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018545 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018546 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018547}
18548
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18550
18551/*
18552 * Function given to ExpandGeneric() to obtain the list of user defined
18553 * function names.
18554 */
18555 char_u *
18556get_user_func_name(xp, idx)
18557 expand_T *xp;
18558 int idx;
18559{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018560 static long_u done;
18561 static hashitem_T *hi;
18562 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563
18564 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018566 done = 0;
18567 hi = func_hashtab.ht_array;
18568 }
18569 if (done < func_hashtab.ht_used)
18570 {
18571 if (done++ > 0)
18572 ++hi;
18573 while (HASHITEM_EMPTY(hi))
18574 ++hi;
18575 fp = HI2UF(hi);
18576
18577 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18578 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579
18580 cat_func_name(IObuff, fp);
18581 if (xp->xp_context != EXPAND_USER_FUNC)
18582 {
18583 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018584 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585 STRCAT(IObuff, ")");
18586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 return IObuff;
18588 }
18589 return NULL;
18590}
18591
18592#endif /* FEAT_CMDL_COMPL */
18593
18594/*
18595 * Copy the function name of "fp" to buffer "buf".
18596 * "buf" must be able to hold the function name plus three bytes.
18597 * Takes care of script-local function names.
18598 */
18599 static void
18600cat_func_name(buf, fp)
18601 char_u *buf;
18602 ufunc_T *fp;
18603{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018604 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 {
18606 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018607 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608 }
18609 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018610 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611}
18612
18613/*
18614 * ":delfunction {name}"
18615 */
18616 void
18617ex_delfunction(eap)
18618 exarg_T *eap;
18619{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018620 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 char_u *p;
18622 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018623 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624
18625 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018626 name = trans_function_name(&p, eap->skip, 0, &fudi);
18627 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018629 {
18630 if (fudi.fd_dict != NULL && !eap->skip)
18631 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634 if (!ends_excmd(*skipwhite(p)))
18635 {
18636 vim_free(name);
18637 EMSG(_(e_trailing));
18638 return;
18639 }
18640 eap->nextcmd = check_nextcmd(p);
18641 if (eap->nextcmd != NULL)
18642 *p = NUL;
18643
18644 if (!eap->skip)
18645 fp = find_func(name);
18646 vim_free(name);
18647
18648 if (!eap->skip)
18649 {
18650 if (fp == NULL)
18651 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018652 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 return;
18654 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018655 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 {
18657 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18658 return;
18659 }
18660
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018661 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018663 /* Delete the dict item that refers to the function, it will
18664 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018665 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018667 else
18668 func_free(fp);
18669 }
18670}
18671
18672/*
18673 * Free a function and remove it from the list of functions.
18674 */
18675 static void
18676func_free(fp)
18677 ufunc_T *fp;
18678{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018679 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018680
18681 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018682 ga_clear_strings(&(fp->uf_args));
18683 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018684#ifdef FEAT_PROFILE
18685 vim_free(fp->uf_tml_count);
18686 vim_free(fp->uf_tml_total);
18687 vim_free(fp->uf_tml_self);
18688#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018689
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018690 /* remove the function from the function hashtable */
18691 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18692 if (HASHITEM_EMPTY(hi))
18693 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018694 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018695 hash_remove(&func_hashtab, hi);
18696
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018697 vim_free(fp);
18698}
18699
18700/*
18701 * Unreference a Function: decrement the reference count and free it when it
18702 * becomes zero. Only for numbered functions.
18703 */
18704 static void
18705func_unref(name)
18706 char_u *name;
18707{
18708 ufunc_T *fp;
18709
18710 if (name != NULL && isdigit(*name))
18711 {
18712 fp = find_func(name);
18713 if (fp == NULL)
18714 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018715 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018716 {
18717 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018718 * when "uf_calls" becomes zero. */
18719 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018720 func_free(fp);
18721 }
18722 }
18723}
18724
18725/*
18726 * Count a reference to a Function.
18727 */
18728 static void
18729func_ref(name)
18730 char_u *name;
18731{
18732 ufunc_T *fp;
18733
18734 if (name != NULL && isdigit(*name))
18735 {
18736 fp = find_func(name);
18737 if (fp == NULL)
18738 EMSG2(_(e_intern2), "func_ref()");
18739 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018740 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 }
18742}
18743
18744/*
18745 * Call a user function.
18746 */
18747 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018748call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749 ufunc_T *fp; /* pointer to function */
18750 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018751 typval_T *argvars; /* arguments */
18752 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753 linenr_T firstline; /* first line of range */
18754 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018755 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756{
Bram Moolenaar33570922005-01-25 22:26:29 +000018757 char_u *save_sourcing_name;
18758 linenr_T save_sourcing_lnum;
18759 scid_T save_current_SID;
18760 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018761 int save_did_emsg;
18762 static int depth = 0;
18763 dictitem_T *v;
18764 int fixvar_idx = 0; /* index in fixvar[] */
18765 int i;
18766 int ai;
18767 char_u numbuf[NUMBUFLEN];
18768 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018769#ifdef FEAT_PROFILE
18770 proftime_T wait_start;
18771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772
18773 /* If depth of calling is getting too high, don't execute the function */
18774 if (depth >= p_mfd)
18775 {
18776 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018777 rettv->v_type = VAR_NUMBER;
18778 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 return;
18780 }
18781 ++depth;
18782
18783 line_breakcheck(); /* check for CTRL-C hit */
18784
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018785 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018786 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018788 fc.rettv = rettv;
18789 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 fc.linenr = 0;
18791 fc.returned = FALSE;
18792 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018794 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795 fc.dbg_tick = debug_tick;
18796
Bram Moolenaar33570922005-01-25 22:26:29 +000018797 /*
18798 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18799 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18800 * each argument variable and saves a lot of time.
18801 */
18802 /*
18803 * Init l: variables.
18804 */
18805 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018806 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018807 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018808 /* Set l:self to "selfdict". */
18809 v = &fc.fixvar[fixvar_idx++].var;
18810 STRCPY(v->di_key, "self");
18811 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18812 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18813 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018814 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018815 v->di_tv.vval.v_dict = selfdict;
18816 ++selfdict->dv_refcount;
18817 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018818
Bram Moolenaar33570922005-01-25 22:26:29 +000018819 /*
18820 * Init a: variables.
18821 * Set a:0 to "argcount".
18822 * Set a:000 to a list with room for the "..." arguments.
18823 */
18824 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18825 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018826 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018827 v = &fc.fixvar[fixvar_idx++].var;
18828 STRCPY(v->di_key, "000");
18829 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18830 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18831 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018832 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018833 v->di_tv.vval.v_list = &fc.l_varlist;
18834 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18835 fc.l_varlist.lv_refcount = 99999;
18836
18837 /*
18838 * Set a:firstline to "firstline" and a:lastline to "lastline".
18839 * Set a:name to named arguments.
18840 * Set a:N to the "..." arguments.
18841 */
18842 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18843 (varnumber_T)firstline);
18844 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18845 (varnumber_T)lastline);
18846 for (i = 0; i < argcount; ++i)
18847 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018848 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018849 if (ai < 0)
18850 /* named argument a:name */
18851 name = FUNCARG(fp, i);
18852 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018853 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018854 /* "..." argument a:1, a:2, etc. */
18855 sprintf((char *)numbuf, "%d", ai + 1);
18856 name = numbuf;
18857 }
18858 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18859 {
18860 v = &fc.fixvar[fixvar_idx++].var;
18861 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18862 }
18863 else
18864 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018865 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18866 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018867 if (v == NULL)
18868 break;
18869 v->di_flags = DI_FLAGS_RO;
18870 }
18871 STRCPY(v->di_key, name);
18872 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18873
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018874 /* Note: the values are copied directly to avoid alloc/free.
18875 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018876 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018877 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018878
18879 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18880 {
18881 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18882 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018883 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018884 }
18885 }
18886
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887 /* Don't redraw while executing the function. */
18888 ++RedrawingDisabled;
18889 save_sourcing_name = sourcing_name;
18890 save_sourcing_lnum = sourcing_lnum;
18891 sourcing_lnum = 1;
18892 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018893 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894 if (sourcing_name != NULL)
18895 {
18896 if (save_sourcing_name != NULL
18897 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18898 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18899 else
18900 STRCPY(sourcing_name, "function ");
18901 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18902
18903 if (p_verbose >= 12)
18904 {
18905 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018906 verbose_enter_scroll();
18907
Bram Moolenaar555b2802005-05-19 21:08:39 +000018908 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909 if (p_verbose >= 14)
18910 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018912 char_u numbuf[NUMBUFLEN];
18913 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914
18915 msg_puts((char_u *)"(");
18916 for (i = 0; i < argcount; ++i)
18917 {
18918 if (i > 0)
18919 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018920 if (argvars[i].v_type == VAR_NUMBER)
18921 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 else
18923 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018924 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018925 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018927 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 }
18929 }
18930 msg_puts((char_u *)")");
18931 }
18932 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018933
18934 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 --no_wait_return;
18936 }
18937 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018938#ifdef FEAT_PROFILE
18939 if (do_profiling)
18940 {
18941 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18942 func_do_profile(fp);
18943 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018944 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018945 {
18946 ++fp->uf_tm_count;
18947 profile_start(&fp->uf_tm_start);
18948 profile_zero(&fp->uf_tm_children);
18949 }
18950 script_prof_save(&wait_start);
18951 }
18952#endif
18953
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018955 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956 save_did_emsg = did_emsg;
18957 did_emsg = FALSE;
18958
18959 /* call do_cmdline() to execute the lines */
18960 do_cmdline(NULL, get_func_line, (void *)&fc,
18961 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18962
18963 --RedrawingDisabled;
18964
18965 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018966 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018968 clear_tv(rettv);
18969 rettv->v_type = VAR_NUMBER;
18970 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971 }
18972
Bram Moolenaar05159a02005-02-26 23:04:13 +000018973#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018974 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018975 {
18976 profile_end(&fp->uf_tm_start);
18977 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18978 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18979 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18980 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018981 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018982 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018983 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18984 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018985 }
18986 }
18987#endif
18988
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 /* when being verbose, mention the return value */
18990 if (p_verbose >= 12)
18991 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018993 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018996 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018997 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018998 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18999 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019000 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019002 char_u buf[MSG_BUF_LEN];
19003 char_u numbuf[NUMBUFLEN];
19004 char_u *tofree;
19005
Bram Moolenaar555b2802005-05-19 21:08:39 +000019006 /* The value may be very long. Skip the middle part, so that we
19007 * have some idea how it starts and ends. smsg() would always
19008 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019009 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019010 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019011 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019012 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013 }
19014 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019015
19016 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017 --no_wait_return;
19018 }
19019
19020 vim_free(sourcing_name);
19021 sourcing_name = save_sourcing_name;
19022 sourcing_lnum = save_sourcing_lnum;
19023 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019024#ifdef FEAT_PROFILE
19025 if (do_profiling)
19026 script_prof_restore(&wait_start);
19027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028
19029 if (p_verbose >= 12 && sourcing_name != NULL)
19030 {
19031 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019032 verbose_enter_scroll();
19033
Bram Moolenaar555b2802005-05-19 21:08:39 +000019034 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019036
19037 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038 --no_wait_return;
19039 }
19040
19041 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019042 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043
Bram Moolenaar33570922005-01-25 22:26:29 +000019044 /* The a: variables typevals were not alloced, only free the allocated
19045 * variables. */
19046 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19047
19048 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049 --depth;
19050}
19051
19052/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019053 * Add a number variable "name" to dict "dp" with value "nr".
19054 */
19055 static void
19056add_nr_var(dp, v, name, nr)
19057 dict_T *dp;
19058 dictitem_T *v;
19059 char *name;
19060 varnumber_T nr;
19061{
19062 STRCPY(v->di_key, name);
19063 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19064 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19065 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019066 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019067 v->di_tv.vval.v_number = nr;
19068}
19069
19070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 * ":return [expr]"
19072 */
19073 void
19074ex_return(eap)
19075 exarg_T *eap;
19076{
19077 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019078 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 int returning = FALSE;
19080
19081 if (current_funccal == NULL)
19082 {
19083 EMSG(_("E133: :return not inside a function"));
19084 return;
19085 }
19086
19087 if (eap->skip)
19088 ++emsg_skip;
19089
19090 eap->nextcmd = NULL;
19091 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019092 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 {
19094 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019095 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019097 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 }
19099 /* It's safer to return also on error. */
19100 else if (!eap->skip)
19101 {
19102 /*
19103 * Return unless the expression evaluation has been cancelled due to an
19104 * aborting error, an interrupt, or an exception.
19105 */
19106 if (!aborting())
19107 returning = do_return(eap, FALSE, TRUE, NULL);
19108 }
19109
19110 /* When skipping or the return gets pending, advance to the next command
19111 * in this line (!returning). Otherwise, ignore the rest of the line.
19112 * Following lines will be ignored by get_func_line(). */
19113 if (returning)
19114 eap->nextcmd = NULL;
19115 else if (eap->nextcmd == NULL) /* no argument */
19116 eap->nextcmd = check_nextcmd(arg);
19117
19118 if (eap->skip)
19119 --emsg_skip;
19120}
19121
19122/*
19123 * Return from a function. Possibly makes the return pending. Also called
19124 * for a pending return at the ":endtry" or after returning from an extra
19125 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019126 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019127 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 * FALSE when the return gets pending.
19129 */
19130 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019131do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 exarg_T *eap;
19133 int reanimate;
19134 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019135 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136{
19137 int idx;
19138 struct condstack *cstack = eap->cstack;
19139
19140 if (reanimate)
19141 /* Undo the return. */
19142 current_funccal->returned = FALSE;
19143
19144 /*
19145 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19146 * not in its finally clause (which then is to be executed next) is found.
19147 * In this case, make the ":return" pending for execution at the ":endtry".
19148 * Otherwise, return normally.
19149 */
19150 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19151 if (idx >= 0)
19152 {
19153 cstack->cs_pending[idx] = CSTP_RETURN;
19154
19155 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019156 /* A pending return again gets pending. "rettv" points to an
19157 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019159 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 else
19161 {
19162 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019163 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019165 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019167 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 {
19169 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019170 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019171 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172 else
19173 EMSG(_(e_outofmem));
19174 }
19175 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019176 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177
19178 if (reanimate)
19179 {
19180 /* The pending return value could be overwritten by a ":return"
19181 * without argument in a finally clause; reset the default
19182 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019183 current_funccal->rettv->v_type = VAR_NUMBER;
19184 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185 }
19186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019187 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188 }
19189 else
19190 {
19191 current_funccal->returned = TRUE;
19192
19193 /* If the return is carried out now, store the return value. For
19194 * a return immediately after reanimation, the value is already
19195 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019196 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019198 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019199 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019200 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019201 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202 }
19203 }
19204
19205 return idx < 0;
19206}
19207
19208/*
19209 * Free the variable with a pending return value.
19210 */
19211 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019212discard_pending_return(rettv)
19213 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214{
Bram Moolenaar33570922005-01-25 22:26:29 +000019215 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216}
19217
19218/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019219 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220 * is an allocated string. Used by report_pending() for verbose messages.
19221 */
19222 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019223get_return_cmd(rettv)
19224 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019226 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019227 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019228 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019230 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019231 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019232 if (s == NULL)
19233 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019234
19235 STRCPY(IObuff, ":return ");
19236 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19237 if (STRLEN(s) + 8 >= IOSIZE)
19238 STRCPY(IObuff + IOSIZE - 4, "...");
19239 vim_free(tofree);
19240 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241}
19242
19243/*
19244 * Get next function line.
19245 * Called by do_cmdline() to get the next line.
19246 * Returns allocated string, or NULL for end of function.
19247 */
19248/* ARGSUSED */
19249 char_u *
19250get_func_line(c, cookie, indent)
19251 int c; /* not used */
19252 void *cookie;
19253 int indent; /* not used */
19254{
Bram Moolenaar33570922005-01-25 22:26:29 +000019255 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019256 ufunc_T *fp = fcp->func;
19257 char_u *retval;
19258 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259
19260 /* If breakpoints have been added/deleted need to check for it. */
19261 if (fcp->dbg_tick != debug_tick)
19262 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019263 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264 sourcing_lnum);
19265 fcp->dbg_tick = debug_tick;
19266 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019267#ifdef FEAT_PROFILE
19268 if (do_profiling)
19269 func_line_end(cookie);
19270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271
Bram Moolenaar05159a02005-02-26 23:04:13 +000019272 gap = &fp->uf_lines;
19273 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274 retval = NULL;
19275 else if (fcp->returned || fcp->linenr >= gap->ga_len)
19276 retval = NULL;
19277 else
19278 {
19279 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19280 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019281#ifdef FEAT_PROFILE
19282 if (do_profiling)
19283 func_line_start(cookie);
19284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285 }
19286
19287 /* Did we encounter a breakpoint? */
19288 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19289 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019290 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019292 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293 sourcing_lnum);
19294 fcp->dbg_tick = debug_tick;
19295 }
19296
19297 return retval;
19298}
19299
Bram Moolenaar05159a02005-02-26 23:04:13 +000019300#if defined(FEAT_PROFILE) || defined(PROTO)
19301/*
19302 * Called when starting to read a function line.
19303 * "sourcing_lnum" must be correct!
19304 * When skipping lines it may not actually be executed, but we won't find out
19305 * until later and we need to store the time now.
19306 */
19307 void
19308func_line_start(cookie)
19309 void *cookie;
19310{
19311 funccall_T *fcp = (funccall_T *)cookie;
19312 ufunc_T *fp = fcp->func;
19313
19314 if (fp->uf_profiling && sourcing_lnum >= 1
19315 && sourcing_lnum <= fp->uf_lines.ga_len)
19316 {
19317 fp->uf_tml_idx = sourcing_lnum - 1;
19318 fp->uf_tml_execed = FALSE;
19319 profile_start(&fp->uf_tml_start);
19320 profile_zero(&fp->uf_tml_children);
19321 profile_get_wait(&fp->uf_tml_wait);
19322 }
19323}
19324
19325/*
19326 * Called when actually executing a function line.
19327 */
19328 void
19329func_line_exec(cookie)
19330 void *cookie;
19331{
19332 funccall_T *fcp = (funccall_T *)cookie;
19333 ufunc_T *fp = fcp->func;
19334
19335 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19336 fp->uf_tml_execed = TRUE;
19337}
19338
19339/*
19340 * Called when done with a function line.
19341 */
19342 void
19343func_line_end(cookie)
19344 void *cookie;
19345{
19346 funccall_T *fcp = (funccall_T *)cookie;
19347 ufunc_T *fp = fcp->func;
19348
19349 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19350 {
19351 if (fp->uf_tml_execed)
19352 {
19353 ++fp->uf_tml_count[fp->uf_tml_idx];
19354 profile_end(&fp->uf_tml_start);
19355 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
19356 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
19357 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
19358 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
19359 }
19360 fp->uf_tml_idx = -1;
19361 }
19362}
19363#endif
19364
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365/*
19366 * Return TRUE if the currently active function should be ended, because a
19367 * return was encountered or an error occured. Used inside a ":while".
19368 */
19369 int
19370func_has_ended(cookie)
19371 void *cookie;
19372{
Bram Moolenaar33570922005-01-25 22:26:29 +000019373 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374
19375 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19376 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019377 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378 || fcp->returned);
19379}
19380
19381/*
19382 * return TRUE if cookie indicates a function which "abort"s on errors.
19383 */
19384 int
19385func_has_abort(cookie)
19386 void *cookie;
19387{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019388 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389}
19390
19391#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19392typedef enum
19393{
19394 VAR_FLAVOUR_DEFAULT,
19395 VAR_FLAVOUR_SESSION,
19396 VAR_FLAVOUR_VIMINFO
19397} var_flavour_T;
19398
19399static var_flavour_T var_flavour __ARGS((char_u *varname));
19400
19401 static var_flavour_T
19402var_flavour(varname)
19403 char_u *varname;
19404{
19405 char_u *p = varname;
19406
19407 if (ASCII_ISUPPER(*p))
19408 {
19409 while (*(++p))
19410 if (ASCII_ISLOWER(*p))
19411 return VAR_FLAVOUR_SESSION;
19412 return VAR_FLAVOUR_VIMINFO;
19413 }
19414 else
19415 return VAR_FLAVOUR_DEFAULT;
19416}
19417#endif
19418
19419#if defined(FEAT_VIMINFO) || defined(PROTO)
19420/*
19421 * Restore global vars that start with a capital from the viminfo file
19422 */
19423 int
19424read_viminfo_varlist(virp, writing)
19425 vir_T *virp;
19426 int writing;
19427{
19428 char_u *tab;
19429 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019430 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431
19432 if (!writing && (find_viminfo_parameter('!') != NULL))
19433 {
19434 tab = vim_strchr(virp->vir_line + 1, '\t');
19435 if (tab != NULL)
19436 {
19437 *tab++ = '\0'; /* isolate the variable name */
19438 if (*tab == 'S') /* string var */
19439 is_string = TRUE;
19440
19441 tab = vim_strchr(tab, '\t');
19442 if (tab != NULL)
19443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019444 if (is_string)
19445 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019446 tv.v_type = VAR_STRING;
19447 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 }
19450 else
19451 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019452 tv.v_type = VAR_NUMBER;
19453 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019455 set_var(virp->vir_line + 1, &tv, FALSE);
19456 if (is_string)
19457 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 }
19459 }
19460 }
19461
19462 return viminfo_readline(virp);
19463}
19464
19465/*
19466 * Write global vars that start with a capital to the viminfo file
19467 */
19468 void
19469write_viminfo_varlist(fp)
19470 FILE *fp;
19471{
Bram Moolenaar33570922005-01-25 22:26:29 +000019472 hashitem_T *hi;
19473 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019474 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019475 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019476 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019477 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019478 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479
19480 if (find_viminfo_parameter('!') == NULL)
19481 return;
19482
19483 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019484
Bram Moolenaar33570922005-01-25 22:26:29 +000019485 todo = globvarht.ht_used;
19486 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019488 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019490 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019491 this_var = HI2DI(hi);
19492 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019493 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019494 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019495 {
19496 case VAR_STRING: s = "STR"; break;
19497 case VAR_NUMBER: s = "NUM"; break;
19498 default: continue;
19499 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019500 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019501 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019502 if (p != NULL)
19503 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019504 vim_free(tofree);
19505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506 }
19507 }
19508}
19509#endif
19510
19511#if defined(FEAT_SESSION) || defined(PROTO)
19512 int
19513store_session_globals(fd)
19514 FILE *fd;
19515{
Bram Moolenaar33570922005-01-25 22:26:29 +000019516 hashitem_T *hi;
19517 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019518 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 char_u *p, *t;
19520
Bram Moolenaar33570922005-01-25 22:26:29 +000019521 todo = globvarht.ht_used;
19522 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019524 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019526 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019527 this_var = HI2DI(hi);
19528 if ((this_var->di_tv.v_type == VAR_NUMBER
19529 || this_var->di_tv.v_type == VAR_STRING)
19530 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019531 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019532 /* Escape special characters with a backslash. Turn a LF and
19533 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019534 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019535 (char_u *)"\\\"\n\r");
19536 if (p == NULL) /* out of memory */
19537 break;
19538 for (t = p; *t != NUL; ++t)
19539 if (*t == '\n')
19540 *t = 'n';
19541 else if (*t == '\r')
19542 *t = 'r';
19543 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019544 this_var->di_key,
19545 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19546 : ' ',
19547 p,
19548 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19549 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019550 || put_eol(fd) == FAIL)
19551 {
19552 vim_free(p);
19553 return FAIL;
19554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 vim_free(p);
19556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557 }
19558 }
19559 return OK;
19560}
19561#endif
19562
Bram Moolenaar661b1822005-07-28 22:36:45 +000019563/*
19564 * Display script name where an item was last set.
19565 * Should only be invoked when 'verbose' is non-zero.
19566 */
19567 void
19568last_set_msg(scriptID)
19569 scid_T scriptID;
19570{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019571 char_u *p;
19572
Bram Moolenaar661b1822005-07-28 22:36:45 +000019573 if (scriptID != 0)
19574 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019575 p = home_replace_save(NULL, get_scriptname(scriptID));
19576 if (p != NULL)
19577 {
19578 verbose_enter();
19579 MSG_PUTS(_("\n\tLast set from "));
19580 MSG_PUTS(p);
19581 vim_free(p);
19582 verbose_leave();
19583 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019584 }
19585}
19586
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587#endif /* FEAT_EVAL */
19588
19589#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19590
19591
19592#ifdef WIN3264
19593/*
19594 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19595 */
19596static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19597static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19598static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19599
19600/*
19601 * Get the short pathname of a file.
19602 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19603 */
19604 static int
19605get_short_pathname(fnamep, bufp, fnamelen)
19606 char_u **fnamep;
19607 char_u **bufp;
19608 int *fnamelen;
19609{
19610 int l,len;
19611 char_u *newbuf;
19612
19613 len = *fnamelen;
19614
19615 l = GetShortPathName(*fnamep, *fnamep, len);
19616 if (l > len - 1)
19617 {
19618 /* If that doesn't work (not enough space), then save the string
19619 * and try again with a new buffer big enough
19620 */
19621 newbuf = vim_strnsave(*fnamep, l);
19622 if (newbuf == NULL)
19623 return 0;
19624
19625 vim_free(*bufp);
19626 *fnamep = *bufp = newbuf;
19627
19628 l = GetShortPathName(*fnamep,*fnamep,l+1);
19629
19630 /* Really should always succeed, as the buffer is big enough */
19631 }
19632
19633 *fnamelen = l;
19634 return 1;
19635}
19636
19637/*
19638 * Create a short path name. Returns the length of the buffer it needs.
19639 * Doesn't copy over the end of the buffer passed in.
19640 */
19641 static int
19642shortpath_for_invalid_fname(fname, bufp, fnamelen)
19643 char_u **fname;
19644 char_u **bufp;
19645 int *fnamelen;
19646{
19647 char_u *s, *p, *pbuf2, *pbuf3;
19648 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019649 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650
19651 /* Make a copy */
19652 len2 = *fnamelen;
19653 pbuf2 = vim_strnsave(*fname, len2);
19654 pbuf3 = NULL;
19655
19656 s = pbuf2 + len2 - 1; /* Find the end */
19657 slen = 1;
19658 plen = len2;
19659
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019660 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661 {
19662 --s;
19663 ++slen;
19664 --plen;
19665 }
19666
19667 do
19668 {
19669 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019670 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 {
19672 --s;
19673 ++slen;
19674 --plen;
19675 }
19676 if (s <= pbuf2)
19677 break;
19678
19679 /* Remeber the character that is about to be blatted */
19680 ch = *s;
19681 *s = 0; /* get_short_pathname requires a null-terminated string */
19682
19683 /* Try it in situ */
19684 p = pbuf2;
19685 if (!get_short_pathname(&p, &pbuf3, &plen))
19686 {
19687 vim_free(pbuf2);
19688 return -1;
19689 }
19690 *s = ch; /* Preserve the string */
19691 } while (plen == 0);
19692
19693 if (plen > 0)
19694 {
19695 /* Remeber the length of the new string. */
19696 *fnamelen = len = plen + slen;
19697 vim_free(*bufp);
19698 if (len > len2)
19699 {
19700 /* If there's not enough space in the currently allocated string,
19701 * then copy it to a buffer big enough.
19702 */
19703 *fname= *bufp = vim_strnsave(p, len);
19704 if (*fname == NULL)
19705 return -1;
19706 }
19707 else
19708 {
19709 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19710 *fname = *bufp = pbuf2;
19711 if (p != pbuf2)
19712 strncpy(*fname, p, plen);
19713 pbuf2 = NULL;
19714 }
19715 /* Concat the next bit */
19716 strncpy(*fname + plen, s, slen);
19717 (*fname)[len] = '\0';
19718 }
19719 vim_free(pbuf3);
19720 vim_free(pbuf2);
19721 return 0;
19722}
19723
19724/*
19725 * Get a pathname for a partial path.
19726 */
19727 static int
19728shortpath_for_partial(fnamep, bufp, fnamelen)
19729 char_u **fnamep;
19730 char_u **bufp;
19731 int *fnamelen;
19732{
19733 int sepcount, len, tflen;
19734 char_u *p;
19735 char_u *pbuf, *tfname;
19736 int hasTilde;
19737
19738 /* Count up the path seperators from the RHS.. so we know which part
19739 * of the path to return.
19740 */
19741 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019742 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743 if (vim_ispathsep(*p))
19744 ++sepcount;
19745
19746 /* Need full path first (use expand_env() to remove a "~/") */
19747 hasTilde = (**fnamep == '~');
19748 if (hasTilde)
19749 pbuf = tfname = expand_env_save(*fnamep);
19750 else
19751 pbuf = tfname = FullName_save(*fnamep, FALSE);
19752
19753 len = tflen = STRLEN(tfname);
19754
19755 if (!get_short_pathname(&tfname, &pbuf, &len))
19756 return -1;
19757
19758 if (len == 0)
19759 {
19760 /* Don't have a valid filename, so shorten the rest of the
19761 * path if we can. This CAN give us invalid 8.3 filenames, but
19762 * there's not a lot of point in guessing what it might be.
19763 */
19764 len = tflen;
19765 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19766 return -1;
19767 }
19768
19769 /* Count the paths backward to find the beginning of the desired string. */
19770 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019771 {
19772#ifdef FEAT_MBYTE
19773 if (has_mbyte)
19774 p -= mb_head_off(tfname, p);
19775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776 if (vim_ispathsep(*p))
19777 {
19778 if (sepcount == 0 || (hasTilde && sepcount == 1))
19779 break;
19780 else
19781 sepcount --;
19782 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784 if (hasTilde)
19785 {
19786 --p;
19787 if (p >= tfname)
19788 *p = '~';
19789 else
19790 return -1;
19791 }
19792 else
19793 ++p;
19794
19795 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19796 vim_free(*bufp);
19797 *fnamelen = (int)STRLEN(p);
19798 *bufp = pbuf;
19799 *fnamep = p;
19800
19801 return 0;
19802}
19803#endif /* WIN3264 */
19804
19805/*
19806 * Adjust a filename, according to a string of modifiers.
19807 * *fnamep must be NUL terminated when called. When returning, the length is
19808 * determined by *fnamelen.
19809 * Returns valid flags.
19810 * When there is an error, *fnamep is set to NULL.
19811 */
19812 int
19813modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19814 char_u *src; /* string with modifiers */
19815 int *usedlen; /* characters after src that are used */
19816 char_u **fnamep; /* file name so far */
19817 char_u **bufp; /* buffer for allocated file name or NULL */
19818 int *fnamelen; /* length of fnamep */
19819{
19820 int valid = 0;
19821 char_u *tail;
19822 char_u *s, *p, *pbuf;
19823 char_u dirname[MAXPATHL];
19824 int c;
19825 int has_fullname = 0;
19826#ifdef WIN3264
19827 int has_shortname = 0;
19828#endif
19829
19830repeat:
19831 /* ":p" - full path/file_name */
19832 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19833 {
19834 has_fullname = 1;
19835
19836 valid |= VALID_PATH;
19837 *usedlen += 2;
19838
19839 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19840 if ((*fnamep)[0] == '~'
19841#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19842 && ((*fnamep)[1] == '/'
19843# ifdef BACKSLASH_IN_FILENAME
19844 || (*fnamep)[1] == '\\'
19845# endif
19846 || (*fnamep)[1] == NUL)
19847
19848#endif
19849 )
19850 {
19851 *fnamep = expand_env_save(*fnamep);
19852 vim_free(*bufp); /* free any allocated file name */
19853 *bufp = *fnamep;
19854 if (*fnamep == NULL)
19855 return -1;
19856 }
19857
19858 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019859 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860 {
19861 if (vim_ispathsep(*p)
19862 && p[1] == '.'
19863 && (p[2] == NUL
19864 || vim_ispathsep(p[2])
19865 || (p[2] == '.'
19866 && (p[3] == NUL || vim_ispathsep(p[3])))))
19867 break;
19868 }
19869
19870 /* FullName_save() is slow, don't use it when not needed. */
19871 if (*p != NUL || !vim_isAbsName(*fnamep))
19872 {
19873 *fnamep = FullName_save(*fnamep, *p != NUL);
19874 vim_free(*bufp); /* free any allocated file name */
19875 *bufp = *fnamep;
19876 if (*fnamep == NULL)
19877 return -1;
19878 }
19879
19880 /* Append a path separator to a directory. */
19881 if (mch_isdir(*fnamep))
19882 {
19883 /* Make room for one or two extra characters. */
19884 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19885 vim_free(*bufp); /* free any allocated file name */
19886 *bufp = *fnamep;
19887 if (*fnamep == NULL)
19888 return -1;
19889 add_pathsep(*fnamep);
19890 }
19891 }
19892
19893 /* ":." - path relative to the current directory */
19894 /* ":~" - path relative to the home directory */
19895 /* ":8" - shortname path - postponed till after */
19896 while (src[*usedlen] == ':'
19897 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19898 {
19899 *usedlen += 2;
19900 if (c == '8')
19901 {
19902#ifdef WIN3264
19903 has_shortname = 1; /* Postpone this. */
19904#endif
19905 continue;
19906 }
19907 pbuf = NULL;
19908 /* Need full path first (use expand_env() to remove a "~/") */
19909 if (!has_fullname)
19910 {
19911 if (c == '.' && **fnamep == '~')
19912 p = pbuf = expand_env_save(*fnamep);
19913 else
19914 p = pbuf = FullName_save(*fnamep, FALSE);
19915 }
19916 else
19917 p = *fnamep;
19918
19919 has_fullname = 0;
19920
19921 if (p != NULL)
19922 {
19923 if (c == '.')
19924 {
19925 mch_dirname(dirname, MAXPATHL);
19926 s = shorten_fname(p, dirname);
19927 if (s != NULL)
19928 {
19929 *fnamep = s;
19930 if (pbuf != NULL)
19931 {
19932 vim_free(*bufp); /* free any allocated file name */
19933 *bufp = pbuf;
19934 pbuf = NULL;
19935 }
19936 }
19937 }
19938 else
19939 {
19940 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19941 /* Only replace it when it starts with '~' */
19942 if (*dirname == '~')
19943 {
19944 s = vim_strsave(dirname);
19945 if (s != NULL)
19946 {
19947 *fnamep = s;
19948 vim_free(*bufp);
19949 *bufp = s;
19950 }
19951 }
19952 }
19953 vim_free(pbuf);
19954 }
19955 }
19956
19957 tail = gettail(*fnamep);
19958 *fnamelen = (int)STRLEN(*fnamep);
19959
19960 /* ":h" - head, remove "/file_name", can be repeated */
19961 /* Don't remove the first "/" or "c:\" */
19962 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19963 {
19964 valid |= VALID_HEAD;
19965 *usedlen += 2;
19966 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019967 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 --tail;
19969 *fnamelen = (int)(tail - *fnamep);
19970#ifdef VMS
19971 if (*fnamelen > 0)
19972 *fnamelen += 1; /* the path separator is part of the path */
19973#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019974 while (tail > s && !after_pathsep(s, tail))
19975 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 }
19977
19978 /* ":8" - shortname */
19979 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19980 {
19981 *usedlen += 2;
19982#ifdef WIN3264
19983 has_shortname = 1;
19984#endif
19985 }
19986
19987#ifdef WIN3264
19988 /* Check shortname after we have done 'heads' and before we do 'tails'
19989 */
19990 if (has_shortname)
19991 {
19992 pbuf = NULL;
19993 /* Copy the string if it is shortened by :h */
19994 if (*fnamelen < (int)STRLEN(*fnamep))
19995 {
19996 p = vim_strnsave(*fnamep, *fnamelen);
19997 if (p == 0)
19998 return -1;
19999 vim_free(*bufp);
20000 *bufp = *fnamep = p;
20001 }
20002
20003 /* Split into two implementations - makes it easier. First is where
20004 * there isn't a full name already, second is where there is.
20005 */
20006 if (!has_fullname && !vim_isAbsName(*fnamep))
20007 {
20008 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20009 return -1;
20010 }
20011 else
20012 {
20013 int l;
20014
20015 /* Simple case, already have the full-name
20016 * Nearly always shorter, so try first time. */
20017 l = *fnamelen;
20018 if (!get_short_pathname(fnamep, bufp, &l))
20019 return -1;
20020
20021 if (l == 0)
20022 {
20023 /* Couldn't find the filename.. search the paths.
20024 */
20025 l = *fnamelen;
20026 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20027 return -1;
20028 }
20029 *fnamelen = l;
20030 }
20031 }
20032#endif /* WIN3264 */
20033
20034 /* ":t" - tail, just the basename */
20035 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20036 {
20037 *usedlen += 2;
20038 *fnamelen -= (int)(tail - *fnamep);
20039 *fnamep = tail;
20040 }
20041
20042 /* ":e" - extension, can be repeated */
20043 /* ":r" - root, without extension, can be repeated */
20044 while (src[*usedlen] == ':'
20045 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20046 {
20047 /* find a '.' in the tail:
20048 * - for second :e: before the current fname
20049 * - otherwise: The last '.'
20050 */
20051 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20052 s = *fnamep - 2;
20053 else
20054 s = *fnamep + *fnamelen - 1;
20055 for ( ; s > tail; --s)
20056 if (s[0] == '.')
20057 break;
20058 if (src[*usedlen + 1] == 'e') /* :e */
20059 {
20060 if (s > tail)
20061 {
20062 *fnamelen += (int)(*fnamep - (s + 1));
20063 *fnamep = s + 1;
20064#ifdef VMS
20065 /* cut version from the extension */
20066 s = *fnamep + *fnamelen - 1;
20067 for ( ; s > *fnamep; --s)
20068 if (s[0] == ';')
20069 break;
20070 if (s > *fnamep)
20071 *fnamelen = s - *fnamep;
20072#endif
20073 }
20074 else if (*fnamep <= tail)
20075 *fnamelen = 0;
20076 }
20077 else /* :r */
20078 {
20079 if (s > tail) /* remove one extension */
20080 *fnamelen = (int)(s - *fnamep);
20081 }
20082 *usedlen += 2;
20083 }
20084
20085 /* ":s?pat?foo?" - substitute */
20086 /* ":gs?pat?foo?" - global substitute */
20087 if (src[*usedlen] == ':'
20088 && (src[*usedlen + 1] == 's'
20089 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20090 {
20091 char_u *str;
20092 char_u *pat;
20093 char_u *sub;
20094 int sep;
20095 char_u *flags;
20096 int didit = FALSE;
20097
20098 flags = (char_u *)"";
20099 s = src + *usedlen + 2;
20100 if (src[*usedlen + 1] == 'g')
20101 {
20102 flags = (char_u *)"g";
20103 ++s;
20104 }
20105
20106 sep = *s++;
20107 if (sep)
20108 {
20109 /* find end of pattern */
20110 p = vim_strchr(s, sep);
20111 if (p != NULL)
20112 {
20113 pat = vim_strnsave(s, (int)(p - s));
20114 if (pat != NULL)
20115 {
20116 s = p + 1;
20117 /* find end of substitution */
20118 p = vim_strchr(s, sep);
20119 if (p != NULL)
20120 {
20121 sub = vim_strnsave(s, (int)(p - s));
20122 str = vim_strnsave(*fnamep, *fnamelen);
20123 if (sub != NULL && str != NULL)
20124 {
20125 *usedlen = (int)(p + 1 - src);
20126 s = do_string_sub(str, pat, sub, flags);
20127 if (s != NULL)
20128 {
20129 *fnamep = s;
20130 *fnamelen = (int)STRLEN(s);
20131 vim_free(*bufp);
20132 *bufp = s;
20133 didit = TRUE;
20134 }
20135 }
20136 vim_free(sub);
20137 vim_free(str);
20138 }
20139 vim_free(pat);
20140 }
20141 }
20142 /* after using ":s", repeat all the modifiers */
20143 if (didit)
20144 goto repeat;
20145 }
20146 }
20147
20148 return valid;
20149}
20150
20151/*
20152 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20153 * "flags" can be "g" to do a global substitute.
20154 * Returns an allocated string, NULL for error.
20155 */
20156 char_u *
20157do_string_sub(str, pat, sub, flags)
20158 char_u *str;
20159 char_u *pat;
20160 char_u *sub;
20161 char_u *flags;
20162{
20163 int sublen;
20164 regmatch_T regmatch;
20165 int i;
20166 int do_all;
20167 char_u *tail;
20168 garray_T ga;
20169 char_u *ret;
20170 char_u *save_cpo;
20171
20172 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20173 save_cpo = p_cpo;
20174 p_cpo = (char_u *)"";
20175
20176 ga_init2(&ga, 1, 200);
20177
20178 do_all = (flags[0] == 'g');
20179
20180 regmatch.rm_ic = p_ic;
20181 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20182 if (regmatch.regprog != NULL)
20183 {
20184 tail = str;
20185 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20186 {
20187 /*
20188 * Get some space for a temporary buffer to do the substitution
20189 * into. It will contain:
20190 * - The text up to where the match is.
20191 * - The substituted text.
20192 * - The text after the match.
20193 */
20194 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20195 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20196 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20197 {
20198 ga_clear(&ga);
20199 break;
20200 }
20201
20202 /* copy the text up to where the match is */
20203 i = (int)(regmatch.startp[0] - tail);
20204 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20205 /* add the substituted text */
20206 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20207 + ga.ga_len + i, TRUE, TRUE, FALSE);
20208 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209 /* avoid getting stuck on a match with an empty string */
20210 if (tail == regmatch.endp[0])
20211 {
20212 if (*tail == NUL)
20213 break;
20214 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20215 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216 }
20217 else
20218 {
20219 tail = regmatch.endp[0];
20220 if (*tail == NUL)
20221 break;
20222 }
20223 if (!do_all)
20224 break;
20225 }
20226
20227 if (ga.ga_data != NULL)
20228 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20229
20230 vim_free(regmatch.regprog);
20231 }
20232
20233 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20234 ga_clear(&ga);
20235 p_cpo = save_cpo;
20236
20237 return ret;
20238}
20239
20240#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */