blob: fef09d02f7dd32f0e6a42f991ba9e624523be483 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000344};
345
346/* shorthand */
347#define vv_type vv_di.di_tv.v_type
348#define vv_nr vv_di.di_tv.vval.v_number
349#define vv_str vv_di.di_tv.vval.v_string
350#define vv_tv vv_di.di_tv
351
352/*
353 * The v: variables are stored in dictionary "vimvardict".
354 * "vimvars_var" is the variable that is used for the "l:" scope.
355 */
356static dict_T vimvardict;
357static dictitem_T vimvars_var;
358#define vimvarht vimvardict.dv_hashtab
359
Bram Moolenaara40058a2005-07-11 22:42:07 +0000360static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
361static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
362#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
363static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
364#endif
365static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
366static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
367static char_u *skip_var_one __ARGS((char_u *arg));
368static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
369static void list_glob_vars __ARGS((void));
370static void list_buf_vars __ARGS((void));
371static void list_win_vars __ARGS((void));
372static void list_vim_vars __ARGS((void));
373static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
374static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
375static int check_changedtick __ARGS((char_u *arg));
376static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
377static void clear_lval __ARGS((lval_T *lp));
378static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
379static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
380static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
381static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
382static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
383static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
384static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
385static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
386static void item_lock __ARGS((typval_T *tv, int deep, int lock));
387static int tv_islocked __ARGS((typval_T *tv));
388
Bram Moolenaar33570922005-01-25 22:26:29 +0000389static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
390static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000398static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000403static listitem_T *listitem_alloc __ARGS((void));
404static void listitem_free __ARGS((listitem_T *item));
405static void listitem_remove __ARGS((list_T *l, listitem_T *item));
406static long list_len __ARGS((list_T *l));
407static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
408static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
409static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static listitem_T *list_find __ARGS((list_T *l, long n));
411static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static void list_append __ARGS((list_T *l, listitem_T *item));
413static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000414static int list_append_string __ARGS((list_T *l, char_u *str, int len));
415static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
417static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
418static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000419static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000421static char_u *list2string __ARGS((typval_T *tv, int copyID));
422static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000423static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
424static void set_ref_in_list __ARGS((list_T *l, int copyID));
425static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static void dict_unref __ARGS((dict_T *d));
427static void dict_free __ARGS((dict_T *d));
428static dictitem_T *dictitem_alloc __ARGS((char_u *key));
429static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
430static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
431static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000432static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static int dict_add __ARGS((dict_T *d, dictitem_T *item));
434static long dict_len __ARGS((dict_T *d));
435static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
439static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static char_u *string_quote __ARGS((char_u *str, int function));
441static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
442static int find_internal_func __ARGS((char_u *name));
443static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
444static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
445static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447
448static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000467#if defined(FEAT_INS_EXPAND)
468static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
476static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000502static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000504static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000510static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000518static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000541static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000547static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000564static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000568#ifdef vim_mkdir
569static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
570#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000571static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000575static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000576static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000578static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000590static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000592static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000599static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000600static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000605static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000606static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
609#ifdef HAVE_STRFTIME
610static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
611#endif
612static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000624static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000625static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000626static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000627static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000628static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000630static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000645static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000646
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
648static int get_env_len __ARGS((char_u **arg));
649static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000650static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000651static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
652#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
653#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
654 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000655static 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 +0000656static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000657static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
659static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static typval_T *alloc_tv __ARGS((void));
661static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void init_tv __ARGS((typval_T *varp));
663static long get_tv_number __ARGS((typval_T *varp));
664static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000665static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static char_u *get_tv_string __ARGS((typval_T *varp));
667static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000668static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000670static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
672static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
673static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
674static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
675static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
676static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
677static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000678static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000680static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
682static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
683static int eval_fname_script __ARGS((char_u *p));
684static int eval_fname_sid __ARGS((char_u *p));
685static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static ufunc_T *find_func __ARGS((char_u *name));
687static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000688static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000689#ifdef FEAT_PROFILE
690static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000691static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
692static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
693static int
694# ifdef __BORLANDC__
695 _RTLENTRYF
696# endif
697 prof_total_cmp __ARGS((const void *s1, const void *s2));
698static int
699# ifdef __BORLANDC__
700 _RTLENTRYF
701# endif
702 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000703#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000704static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000705static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000706static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707static void func_free __ARGS((ufunc_T *fp));
708static void func_unref __ARGS((char_u *name));
709static void func_ref __ARGS((char_u *name));
710static 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));
711static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000712static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000713static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
714static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000716/* Character used as separated in autoload function/variable names. */
717#define AUTOLOAD_CHAR '#'
718
Bram Moolenaar33570922005-01-25 22:26:29 +0000719/*
720 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000721 */
722 void
723eval_init()
724{
Bram Moolenaar33570922005-01-25 22:26:29 +0000725 int i;
726 struct vimvar *p;
727
728 init_var_dict(&globvardict, &globvars_var);
729 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000730 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000731 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000732
733 for (i = 0; i < VV_LEN; ++i)
734 {
735 p = &vimvars[i];
736 STRCPY(p->vv_di.di_key, p->vv_name);
737 if (p->vv_flags & VV_RO)
738 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
739 else if (p->vv_flags & VV_RO_SBX)
740 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
741 else
742 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000743
744 /* add to v: scope dict, unless the value is not always available */
745 if (p->vv_type != VAR_UNKNOWN)
746 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000747 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000748 /* add to compat scope dict */
749 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000750 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000751}
752
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000753#if defined(EXITFREE) || defined(PROTO)
754 void
755eval_clear()
756{
757 int i;
758 struct vimvar *p;
759
760 for (i = 0; i < VV_LEN; ++i)
761 {
762 p = &vimvars[i];
763 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000764 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000765 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000766 p->vv_di.di_tv.vval.v_string = NULL;
767 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000768 }
769 hash_clear(&vimvarht);
770 hash_clear(&compat_hashtab);
771
772 /* script-local variables */
773 for (i = 1; i <= ga_scripts.ga_len; ++i)
774 vars_clear(&SCRIPT_VARS(i));
775 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000776 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000777
778 /* global variables */
779 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000780
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000781 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000782 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000783 hash_clear(&func_hashtab);
784
785 /* unreferenced lists and dicts */
786 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000787}
788#endif
789
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 * Return the name of the executed function.
792 */
793 char_u *
794func_name(cookie)
795 void *cookie;
796{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000797 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798}
799
800/*
801 * Return the address holding the next breakpoint line for a funccall cookie.
802 */
803 linenr_T *
804func_breakpoint(cookie)
805 void *cookie;
806{
Bram Moolenaar33570922005-01-25 22:26:29 +0000807 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808}
809
810/*
811 * Return the address holding the debug tick for a funccall cookie.
812 */
813 int *
814func_dbg_tick(cookie)
815 void *cookie;
816{
Bram Moolenaar33570922005-01-25 22:26:29 +0000817 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818}
819
820/*
821 * Return the nesting level for a funccall cookie.
822 */
823 int
824func_level(cookie)
825 void *cookie;
826{
Bram Moolenaar33570922005-01-25 22:26:29 +0000827 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828}
829
830/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000831funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833/*
834 * Return TRUE when a function was ended by a ":return" command.
835 */
836 int
837current_func_returned()
838{
839 return current_funccal->returned;
840}
841
842
843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 * Set an internal variable to a string value. Creates the variable if it does
845 * not already exist.
846 */
847 void
848set_internal_string_var(name, value)
849 char_u *name;
850 char_u *value;
851{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000852 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000853 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
855 val = vim_strsave(value);
856 if (val != NULL)
857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000858 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000859 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000861 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000862 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 }
864 }
865}
866
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000867static lval_T *redir_lval = NULL;
868static char_u *redir_endp = NULL;
869static char_u *redir_varname = NULL;
870
871/*
872 * Start recording command output to a variable
873 * Returns OK if successfully completed the setup. FAIL otherwise.
874 */
875 int
876var_redir_start(name, append)
877 char_u *name;
878 int append; /* append to an existing variable */
879{
880 int save_emsg;
881 int err;
882 typval_T tv;
883
884 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000885 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000886 {
887 EMSG(_(e_invarg));
888 return FAIL;
889 }
890
891 redir_varname = vim_strsave(name);
892 if (redir_varname == NULL)
893 return FAIL;
894
895 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
896 if (redir_lval == NULL)
897 {
898 var_redir_stop();
899 return FAIL;
900 }
901
902 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000903 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
904 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000905 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
906 {
907 if (redir_endp != NULL && *redir_endp != NUL)
908 /* Trailing characters are present after the variable name */
909 EMSG(_(e_trailing));
910 else
911 EMSG(_(e_invarg));
912 var_redir_stop();
913 return FAIL;
914 }
915
916 /* check if we can write to the variable: set it to or append an empty
917 * string */
918 save_emsg = did_emsg;
919 did_emsg = FALSE;
920 tv.v_type = VAR_STRING;
921 tv.vval.v_string = (char_u *)"";
922 if (append)
923 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
924 else
925 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
926 err = did_emsg;
927 did_emsg += save_emsg;
928 if (err)
929 {
930 var_redir_stop();
931 return FAIL;
932 }
933 if (redir_lval->ll_newkey != NULL)
934 {
935 /* Dictionary item was created, don't do it again. */
936 vim_free(redir_lval->ll_newkey);
937 redir_lval->ll_newkey = NULL;
938 }
939
940 return OK;
941}
942
943/*
944 * Append "value[len]" to the variable set by var_redir_start().
945 */
946 void
947var_redir_str(value, len)
948 char_u *value;
949 int len;
950{
951 char_u *val;
952 typval_T tv;
953 int save_emsg;
954 int err;
955
956 if (redir_lval == NULL)
957 return;
958
959 if (len == -1)
960 /* Append the entire string */
961 val = vim_strsave(value);
962 else
963 /* Append only the specified number of characters */
964 val = vim_strnsave(value, len);
965 if (val == NULL)
966 return;
967
968 tv.v_type = VAR_STRING;
969 tv.vval.v_string = val;
970
971 save_emsg = did_emsg;
972 did_emsg = FALSE;
973 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
974 err = did_emsg;
975 did_emsg += save_emsg;
976 if (err)
977 var_redir_stop();
978
979 vim_free(tv.vval.v_string);
980}
981
982/*
983 * Stop redirecting command output to a variable.
984 */
985 void
986var_redir_stop()
987{
988 if (redir_lval != NULL)
989 {
990 clear_lval(redir_lval);
991 vim_free(redir_lval);
992 redir_lval = NULL;
993 }
994 vim_free(redir_varname);
995 redir_varname = NULL;
996}
997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998# if defined(FEAT_MBYTE) || defined(PROTO)
999 int
1000eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1001 char_u *enc_from;
1002 char_u *enc_to;
1003 char_u *fname_from;
1004 char_u *fname_to;
1005{
1006 int err = FALSE;
1007
1008 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1009 set_vim_var_string(VV_CC_TO, enc_to, -1);
1010 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1011 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1012 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1013 err = TRUE;
1014 set_vim_var_string(VV_CC_FROM, NULL, -1);
1015 set_vim_var_string(VV_CC_TO, NULL, -1);
1016 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1017 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1018
1019 if (err)
1020 return FAIL;
1021 return OK;
1022}
1023# endif
1024
1025# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1026 int
1027eval_printexpr(fname, args)
1028 char_u *fname;
1029 char_u *args;
1030{
1031 int err = FALSE;
1032
1033 set_vim_var_string(VV_FNAME_IN, fname, -1);
1034 set_vim_var_string(VV_CMDARG, args, -1);
1035 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1036 err = TRUE;
1037 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1038 set_vim_var_string(VV_CMDARG, NULL, -1);
1039
1040 if (err)
1041 {
1042 mch_remove(fname);
1043 return FAIL;
1044 }
1045 return OK;
1046}
1047# endif
1048
1049# if defined(FEAT_DIFF) || defined(PROTO)
1050 void
1051eval_diff(origfile, newfile, outfile)
1052 char_u *origfile;
1053 char_u *newfile;
1054 char_u *outfile;
1055{
1056 int err = FALSE;
1057
1058 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1059 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1060 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1061 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1062 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1063 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1064 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1065}
1066
1067 void
1068eval_patch(origfile, difffile, outfile)
1069 char_u *origfile;
1070 char_u *difffile;
1071 char_u *outfile;
1072{
1073 int err;
1074
1075 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1076 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1077 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1078 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1079 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1080 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1081 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1082}
1083# endif
1084
1085/*
1086 * Top level evaluation function, returning a boolean.
1087 * Sets "error" to TRUE if there was an error.
1088 * Return TRUE or FALSE.
1089 */
1090 int
1091eval_to_bool(arg, error, nextcmd, skip)
1092 char_u *arg;
1093 int *error;
1094 char_u **nextcmd;
1095 int skip; /* only parse, don't execute */
1096{
Bram Moolenaar33570922005-01-25 22:26:29 +00001097 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 int retval = FALSE;
1099
1100 if (skip)
1101 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001102 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 else
1105 {
1106 *error = FALSE;
1107 if (!skip)
1108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001109 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001110 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
1112 }
1113 if (skip)
1114 --emsg_skip;
1115
1116 return retval;
1117}
1118
1119/*
1120 * Top level evaluation function, returning a string. If "skip" is TRUE,
1121 * only parsing to "nextcmd" is done, without reporting errors. Return
1122 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1123 */
1124 char_u *
1125eval_to_string_skip(arg, nextcmd, skip)
1126 char_u *arg;
1127 char_u **nextcmd;
1128 int skip; /* only parse, don't execute */
1129{
Bram Moolenaar33570922005-01-25 22:26:29 +00001130 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 char_u *retval;
1132
1133 if (skip)
1134 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 retval = NULL;
1137 else
1138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001139 retval = vim_strsave(get_tv_string(&tv));
1140 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 }
1142 if (skip)
1143 --emsg_skip;
1144
1145 return retval;
1146}
1147
1148/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001149 * Skip over an expression at "*pp".
1150 * Return FAIL for an error, OK otherwise.
1151 */
1152 int
1153skip_expr(pp)
1154 char_u **pp;
1155{
Bram Moolenaar33570922005-01-25 22:26:29 +00001156 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001157
1158 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001159 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001160}
1161
1162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 * Top level evaluation function, returning a string.
1164 * Return pointer to allocated memory, or NULL for failure.
1165 */
1166 char_u *
1167eval_to_string(arg, nextcmd)
1168 char_u *arg;
1169 char_u **nextcmd;
1170{
Bram Moolenaar33570922005-01-25 22:26:29 +00001171 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 char_u *retval;
1173
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001174 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 retval = NULL;
1176 else
1177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001178 retval = vim_strsave(get_tv_string(&tv));
1179 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 }
1181
1182 return retval;
1183}
1184
1185/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001186 * Call eval_to_string() without using current local variables and using
1187 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 */
1189 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001190eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 char_u *arg;
1192 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001193 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194{
1195 char_u *retval;
1196 void *save_funccalp;
1197
1198 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001199 if (use_sandbox)
1200 ++sandbox;
1201 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 retval = eval_to_string(arg, nextcmd);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001203 if (use_sandbox)
1204 --sandbox;
1205 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 restore_funccal(save_funccalp);
1207 return retval;
1208}
1209
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210/*
1211 * Top level evaluation function, returning a number.
1212 * Evaluates "expr" silently.
1213 * Returns -1 for an error.
1214 */
1215 int
1216eval_to_number(expr)
1217 char_u *expr;
1218{
Bram Moolenaar33570922005-01-25 22:26:29 +00001219 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001221 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
1223 ++emsg_off;
1224
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 retval = -1;
1227 else
1228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001229 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001230 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 }
1232 --emsg_off;
1233
1234 return retval;
1235}
1236
Bram Moolenaara40058a2005-07-11 22:42:07 +00001237/*
1238 * Prepare v: variable "idx" to be used.
1239 * Save the current typeval in "save_tv".
1240 * When not used yet add the variable to the v: hashtable.
1241 */
1242 static void
1243prepare_vimvar(idx, save_tv)
1244 int idx;
1245 typval_T *save_tv;
1246{
1247 *save_tv = vimvars[idx].vv_tv;
1248 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1249 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1250}
1251
1252/*
1253 * Restore v: variable "idx" to typeval "save_tv".
1254 * When no longer defined, remove the variable from the v: hashtable.
1255 */
1256 static void
1257restore_vimvar(idx, save_tv)
1258 int idx;
1259 typval_T *save_tv;
1260{
1261 hashitem_T *hi;
1262
1263 clear_tv(&vimvars[idx].vv_tv);
1264 vimvars[idx].vv_tv = *save_tv;
1265 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1266 {
1267 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1268 if (HASHITEM_EMPTY(hi))
1269 EMSG2(_(e_intern2), "restore_vimvar()");
1270 else
1271 hash_remove(&vimvarht, hi);
1272 }
1273}
1274
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001275#if defined(FEAT_SYN_HL) || defined(PROTO)
1276/*
1277 * Evaluate an expression to a list with suggestions.
1278 * For the "expr:" part of 'spellsuggest'.
1279 */
1280 list_T *
1281eval_spell_expr(badword, expr)
1282 char_u *badword;
1283 char_u *expr;
1284{
1285 typval_T save_val;
1286 typval_T rettv;
1287 list_T *list = NULL;
1288 char_u *p = skipwhite(expr);
1289
1290 /* Set "v:val" to the bad word. */
1291 prepare_vimvar(VV_VAL, &save_val);
1292 vimvars[VV_VAL].vv_type = VAR_STRING;
1293 vimvars[VV_VAL].vv_str = badword;
1294 if (p_verbose == 0)
1295 ++emsg_off;
1296
1297 if (eval1(&p, &rettv, TRUE) == OK)
1298 {
1299 if (rettv.v_type != VAR_LIST)
1300 clear_tv(&rettv);
1301 else
1302 list = rettv.vval.v_list;
1303 }
1304
1305 if (p_verbose == 0)
1306 --emsg_off;
1307 vimvars[VV_VAL].vv_str = NULL;
1308 restore_vimvar(VV_VAL, &save_val);
1309
1310 return list;
1311}
1312
1313/*
1314 * "list" is supposed to contain two items: a word and a number. Return the
1315 * word in "pp" and the number as the return value.
1316 * Return -1 if anything isn't right.
1317 * Used to get the good word and score from the eval_spell_expr() result.
1318 */
1319 int
1320get_spellword(list, pp)
1321 list_T *list;
1322 char_u **pp;
1323{
1324 listitem_T *li;
1325
1326 li = list->lv_first;
1327 if (li == NULL)
1328 return -1;
1329 *pp = get_tv_string(&li->li_tv);
1330
1331 li = li->li_next;
1332 if (li == NULL)
1333 return -1;
1334 return get_tv_number(&li->li_tv);
1335}
1336#endif
1337
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001338/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001339 * Top level evaluation function.
1340 * Returns an allocated typval_T with the result.
1341 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001342 */
1343 typval_T *
1344eval_expr(arg, nextcmd)
1345 char_u *arg;
1346 char_u **nextcmd;
1347{
1348 typval_T *tv;
1349
1350 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001351 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001352 {
1353 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001354 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001355 }
1356
1357 return tv;
1358}
1359
1360
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1362/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001363 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001365 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001367 static int
1368call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 char_u *func;
1370 int argc;
1371 char_u **argv;
1372 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
Bram Moolenaar33570922005-01-25 22:26:29 +00001375 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 long n;
1377 int len;
1378 int i;
1379 int doesrange;
1380 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001381 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
Bram Moolenaar33570922005-01-25 22:26:29 +00001383 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001385 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
1387 for (i = 0; i < argc; i++)
1388 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001389 /* Pass a NULL or empty argument as an empty string */
1390 if (argv[i] == NULL || *argv[i] == NUL)
1391 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001392 argvars[i].v_type = VAR_STRING;
1393 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001394 continue;
1395 }
1396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 /* Recognize a number argument, the others must be strings. */
1398 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1399 if (len != 0 && len == (int)STRLEN(argv[i]))
1400 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001401 argvars[i].v_type = VAR_NUMBER;
1402 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
1404 else
1405 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001406 argvars[i].v_type = VAR_STRING;
1407 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409 }
1410
1411 if (safe)
1412 {
1413 save_funccalp = save_funccal();
1414 ++sandbox;
1415 }
1416
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001417 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1418 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001420 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 if (safe)
1422 {
1423 --sandbox;
1424 restore_funccal(save_funccalp);
1425 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001426 vim_free(argvars);
1427
1428 if (ret == FAIL)
1429 clear_tv(rettv);
1430
1431 return ret;
1432}
1433
1434/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001435 * Call vimL function "func" and return the result as a string.
1436 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001437 * Uses argv[argc] for the function arguments.
1438 */
1439 void *
1440call_func_retstr(func, argc, argv, safe)
1441 char_u *func;
1442 int argc;
1443 char_u **argv;
1444 int safe; /* use the sandbox */
1445{
1446 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001447 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001448
1449 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1450 return NULL;
1451
1452 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001453 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 return retval;
1455}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001456
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001457#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001458/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001459 * Call vimL function "func" and return the result as a number.
1460 * Returns -1 when calling the function fails.
1461 * Uses argv[argc] for the function arguments.
1462 */
1463 long
1464call_func_retnr(func, argc, argv, safe)
1465 char_u *func;
1466 int argc;
1467 char_u **argv;
1468 int safe; /* use the sandbox */
1469{
1470 typval_T rettv;
1471 long retval;
1472
1473 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1474 return -1;
1475
1476 retval = get_tv_number_chk(&rettv, NULL);
1477 clear_tv(&rettv);
1478 return retval;
1479}
1480#endif
1481
1482/*
1483 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001484 * Uses argv[argc] for the function arguments.
1485 */
1486 void *
1487call_func_retlist(func, argc, argv, safe)
1488 char_u *func;
1489 int argc;
1490 char_u **argv;
1491 int safe; /* use the sandbox */
1492{
1493 typval_T rettv;
1494
1495 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1496 return NULL;
1497
1498 if (rettv.v_type != VAR_LIST)
1499 {
1500 clear_tv(&rettv);
1501 return NULL;
1502 }
1503
1504 return rettv.vval.v_list;
1505}
1506
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507#endif
1508
1509/*
1510 * Save the current function call pointer, and set it to NULL.
1511 * Used when executing autocommands and for ":source".
1512 */
1513 void *
1514save_funccal()
1515{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001516 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 current_funccal = NULL;
1519 return (void *)fc;
1520}
1521
1522 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001523restore_funccal(vfc)
1524 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001526 funccall_T *fc = (funccall_T *)vfc;
1527
1528 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529}
1530
Bram Moolenaar05159a02005-02-26 23:04:13 +00001531#if defined(FEAT_PROFILE) || defined(PROTO)
1532/*
1533 * Prepare profiling for entering a child or something else that is not
1534 * counted for the script/function itself.
1535 * Should always be called in pair with prof_child_exit().
1536 */
1537 void
1538prof_child_enter(tm)
1539 proftime_T *tm; /* place to store waittime */
1540{
1541 funccall_T *fc = current_funccal;
1542
1543 if (fc != NULL && fc->func->uf_profiling)
1544 profile_start(&fc->prof_child);
1545 script_prof_save(tm);
1546}
1547
1548/*
1549 * Take care of time spent in a child.
1550 * Should always be called after prof_child_enter().
1551 */
1552 void
1553prof_child_exit(tm)
1554 proftime_T *tm; /* where waittime was stored */
1555{
1556 funccall_T *fc = current_funccal;
1557
1558 if (fc != NULL && fc->func->uf_profiling)
1559 {
1560 profile_end(&fc->prof_child);
1561 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1562 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1563 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1564 }
1565 script_prof_restore(tm);
1566}
1567#endif
1568
1569
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570#ifdef FEAT_FOLDING
1571/*
1572 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1573 * it in "*cp". Doesn't give error messages.
1574 */
1575 int
1576eval_foldexpr(arg, cp)
1577 char_u *arg;
1578 int *cp;
1579{
Bram Moolenaar33570922005-01-25 22:26:29 +00001580 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 int retval;
1582 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001583 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1584 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
1586 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001587 if (use_sandbox)
1588 ++sandbox;
1589 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001591 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 retval = 0;
1593 else
1594 {
1595 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001596 if (tv.v_type == VAR_NUMBER)
1597 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001598 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 retval = 0;
1600 else
1601 {
1602 /* If the result is a string, check if there is a non-digit before
1603 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001604 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 if (!VIM_ISDIGIT(*s) && *s != '-')
1606 *cp = *s++;
1607 retval = atol((char *)s);
1608 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001612 if (use_sandbox)
1613 --sandbox;
1614 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616 return retval;
1617}
1618#endif
1619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001621 * ":let" list all variable values
1622 * ":let var1 var2" list variable values
1623 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001624 * ":let var += expr" assignment command.
1625 * ":let var -= expr" assignment command.
1626 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001627 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 */
1629 void
1630ex_let(eap)
1631 exarg_T *eap;
1632{
1633 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001634 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001635 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001637 int var_count = 0;
1638 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001639 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001641 expr = skip_var_list(arg, &var_count, &semicolon);
1642 if (expr == NULL)
1643 return;
1644 expr = vim_strchr(expr, '=');
1645 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001647 /*
1648 * ":let" without "=": list variables
1649 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650 if (*arg == '[')
1651 EMSG(_(e_invarg));
1652 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001653 /* ":let var1 var2" */
1654 arg = list_arg_vars(eap, arg);
1655 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001658 list_glob_vars();
1659 list_buf_vars();
1660 list_win_vars();
1661 list_vim_vars();
1662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 eap->nextcmd = check_nextcmd(arg);
1664 }
1665 else
1666 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001667 op[0] = '=';
1668 op[1] = NUL;
1669 if (expr > arg)
1670 {
1671 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1672 op[0] = expr[-1]; /* +=, -= or .= */
1673 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (eap->skip)
1677 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001678 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (eap->skip)
1680 {
1681 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 --emsg_skip;
1684 }
1685 else if (i != FAIL)
1686 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 }
1691 }
1692}
1693
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694/*
1695 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1696 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001697 * When "nextchars" is not NULL it points to a string with characters that
1698 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1699 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001700 * Returns OK or FAIL;
1701 */
1702 static int
1703ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1704 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001705 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 int copy; /* copy values from "tv", don't move */
1707 int semicolon; /* from skip_var_list() */
1708 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001709 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710{
1711 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001712 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001714 listitem_T *item;
1715 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716
1717 if (*arg != '[')
1718 {
1719 /*
1720 * ":let var = expr" or ":for var in list"
1721 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001722 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723 return FAIL;
1724 return OK;
1725 }
1726
1727 /*
1728 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1729 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001730 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001731 {
1732 EMSG(_(e_listreq));
1733 return FAIL;
1734 }
1735
1736 i = list_len(l);
1737 if (semicolon == 0 && var_count < i)
1738 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001739 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001740 return FAIL;
1741 }
1742 if (var_count - semicolon > i)
1743 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001744 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745 return FAIL;
1746 }
1747
1748 item = l->lv_first;
1749 while (*arg != ']')
1750 {
1751 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001752 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001753 item = item->li_next;
1754 if (arg == NULL)
1755 return FAIL;
1756
1757 arg = skipwhite(arg);
1758 if (*arg == ';')
1759 {
1760 /* Put the rest of the list (may be empty) in the var after ';'.
1761 * Create a new list for this. */
1762 l = list_alloc();
1763 if (l == NULL)
1764 return FAIL;
1765 while (item != NULL)
1766 {
1767 list_append_tv(l, &item->li_tv);
1768 item = item->li_next;
1769 }
1770
1771 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001772 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001773 ltv.vval.v_list = l;
1774 l->lv_refcount = 1;
1775
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001776 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1777 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778 clear_tv(&ltv);
1779 if (arg == NULL)
1780 return FAIL;
1781 break;
1782 }
1783 else if (*arg != ',' && *arg != ']')
1784 {
1785 EMSG2(_(e_intern2), "ex_let_vars()");
1786 return FAIL;
1787 }
1788 }
1789
1790 return OK;
1791}
1792
1793/*
1794 * Skip over assignable variable "var" or list of variables "[var, var]".
1795 * Used for ":let varvar = expr" and ":for varvar in expr".
1796 * For "[var, var]" increment "*var_count" for each variable.
1797 * for "[var, var; var]" set "semicolon".
1798 * Return NULL for an error.
1799 */
1800 static char_u *
1801skip_var_list(arg, var_count, semicolon)
1802 char_u *arg;
1803 int *var_count;
1804 int *semicolon;
1805{
1806 char_u *p, *s;
1807
1808 if (*arg == '[')
1809 {
1810 /* "[var, var]": find the matching ']'. */
1811 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001812 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 {
1814 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1815 s = skip_var_one(p);
1816 if (s == p)
1817 {
1818 EMSG2(_(e_invarg2), p);
1819 return NULL;
1820 }
1821 ++*var_count;
1822
1823 p = skipwhite(s);
1824 if (*p == ']')
1825 break;
1826 else if (*p == ';')
1827 {
1828 if (*semicolon == 1)
1829 {
1830 EMSG(_("Double ; in list of variables"));
1831 return NULL;
1832 }
1833 *semicolon = 1;
1834 }
1835 else if (*p != ',')
1836 {
1837 EMSG2(_(e_invarg2), p);
1838 return NULL;
1839 }
1840 }
1841 return p + 1;
1842 }
1843 else
1844 return skip_var_one(arg);
1845}
1846
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001847/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001848 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1849 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001850 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 static char_u *
1852skip_var_one(arg)
1853 char_u *arg;
1854{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001855 if (*arg == '@' && arg[1] != NUL)
1856 return arg + 2;
1857 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1858 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859}
1860
Bram Moolenaara7043832005-01-21 11:56:39 +00001861/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001862 * List variables for hashtab "ht" with prefix "prefix".
1863 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001864 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001866list_hashtable_vars(ht, prefix, empty)
1867 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001868 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001869 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001870{
Bram Moolenaar33570922005-01-25 22:26:29 +00001871 hashitem_T *hi;
1872 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001873 int todo;
1874
1875 todo = ht->ht_used;
1876 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1877 {
1878 if (!HASHITEM_EMPTY(hi))
1879 {
1880 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001881 di = HI2DI(hi);
1882 if (empty || di->di_tv.v_type != VAR_STRING
1883 || di->di_tv.vval.v_string != NULL)
1884 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001885 }
1886 }
1887}
1888
1889/*
1890 * List global variables.
1891 */
1892 static void
1893list_glob_vars()
1894{
Bram Moolenaar33570922005-01-25 22:26:29 +00001895 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001896}
1897
1898/*
1899 * List buffer variables.
1900 */
1901 static void
1902list_buf_vars()
1903{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001904 char_u numbuf[NUMBUFLEN];
1905
Bram Moolenaar33570922005-01-25 22:26:29 +00001906 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001907
1908 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1909 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001910}
1911
1912/*
1913 * List window variables.
1914 */
1915 static void
1916list_win_vars()
1917{
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001919}
1920
1921/*
1922 * List Vim variables.
1923 */
1924 static void
1925list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926{
Bram Moolenaar33570922005-01-25 22:26:29 +00001927 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928}
1929
1930/*
1931 * List variables in "arg".
1932 */
1933 static char_u *
1934list_arg_vars(eap, arg)
1935 exarg_T *eap;
1936 char_u *arg;
1937{
1938 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001939 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001941 char_u *name_start;
1942 char_u *arg_subsc;
1943 char_u *tofree;
1944 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945
1946 while (!ends_excmd(*arg) && !got_int)
1947 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001948 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001949 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001950 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001951 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1952 {
1953 emsg_severe = TRUE;
1954 EMSG(_(e_trailing));
1955 break;
1956 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001958 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001960 /* get_name_len() takes care of expanding curly braces */
1961 name_start = name = arg;
1962 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1963 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001964 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001965 /* This is mainly to keep test 49 working: when expanding
1966 * curly braces fails overrule the exception error message. */
1967 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001968 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001969 emsg_severe = TRUE;
1970 EMSG2(_(e_invarg2), arg);
1971 break;
1972 }
1973 error = TRUE;
1974 }
1975 else
1976 {
1977 if (tofree != NULL)
1978 name = tofree;
1979 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001980 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 else
1982 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001983 /* handle d.key, l[idx], f(expr) */
1984 arg_subsc = arg;
1985 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001986 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001987 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001988 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001989 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001990 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001991 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001992 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001993 case 'g': list_glob_vars(); break;
1994 case 'b': list_buf_vars(); break;
1995 case 'w': list_win_vars(); break;
1996 case 'v': list_vim_vars(); break;
1997 default:
1998 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001999 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002000 }
2001 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002002 {
2003 char_u numbuf[NUMBUFLEN];
2004 char_u *tf;
2005 int c;
2006 char_u *s;
2007
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002008 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002009 c = *arg;
2010 *arg = NUL;
2011 list_one_var_a((char_u *)"",
2012 arg == arg_subsc ? name : name_start,
2013 tv.v_type, s == NULL ? (char_u *)"" : s);
2014 *arg = c;
2015 vim_free(tf);
2016 }
2017 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019 }
2020 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002021
2022 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002023 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002024
2025 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026 }
2027
2028 return arg;
2029}
2030
2031/*
2032 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2033 * Returns a pointer to the char just after the var name.
2034 * Returns NULL if there is an error.
2035 */
2036 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002037ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002038 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002039 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002042 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002043{
2044 int c1;
2045 char_u *name;
2046 char_u *p;
2047 char_u *arg_end = NULL;
2048 int len;
2049 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002050 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051
2052 /*
2053 * ":let $VAR = expr": Set environment variable.
2054 */
2055 if (*arg == '$')
2056 {
2057 /* Find the end of the name. */
2058 ++arg;
2059 name = arg;
2060 len = get_env_len(&arg);
2061 if (len == 0)
2062 EMSG2(_(e_invarg2), name - 1);
2063 else
2064 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002065 if (op != NULL && (*op == '+' || *op == '-'))
2066 EMSG2(_(e_letwrong), op);
2067 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002068 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002069 EMSG(_(e_letunexp));
2070 else
2071 {
2072 c1 = name[len];
2073 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002074 p = get_tv_string_chk(tv);
2075 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002076 {
2077 int mustfree = FALSE;
2078 char_u *s = vim_getenv(name, &mustfree);
2079
2080 if (s != NULL)
2081 {
2082 p = tofree = concat_str(s, p);
2083 if (mustfree)
2084 vim_free(s);
2085 }
2086 }
2087 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002088 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002090 if (STRICMP(name, "HOME") == 0)
2091 init_homedir();
2092 else if (didset_vim && STRICMP(name, "VIM") == 0)
2093 didset_vim = FALSE;
2094 else if (didset_vimruntime
2095 && STRICMP(name, "VIMRUNTIME") == 0)
2096 didset_vimruntime = FALSE;
2097 arg_end = arg;
2098 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002099 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002100 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002101 }
2102 }
2103 }
2104
2105 /*
2106 * ":let &option = expr": Set option value.
2107 * ":let &l:option = expr": Set local option value.
2108 * ":let &g:option = expr": Set global option value.
2109 */
2110 else if (*arg == '&')
2111 {
2112 /* Find the end of the name. */
2113 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002114 if (p == NULL || (endchars != NULL
2115 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002116 EMSG(_(e_letunexp));
2117 else
2118 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002119 long n;
2120 int opt_type;
2121 long numval;
2122 char_u *stringval = NULL;
2123 char_u *s;
2124
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125 c1 = *p;
2126 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002127
2128 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002129 s = get_tv_string_chk(tv); /* != NULL if number or string */
2130 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002131 {
2132 opt_type = get_option_value(arg, &numval,
2133 &stringval, opt_flags);
2134 if ((opt_type == 1 && *op == '.')
2135 || (opt_type == 0 && *op != '.'))
2136 EMSG2(_(e_letwrong), op);
2137 else
2138 {
2139 if (opt_type == 1) /* number */
2140 {
2141 if (*op == '+')
2142 n = numval + n;
2143 else
2144 n = numval - n;
2145 }
2146 else if (opt_type == 0 && stringval != NULL) /* string */
2147 {
2148 s = concat_str(stringval, s);
2149 vim_free(stringval);
2150 stringval = s;
2151 }
2152 }
2153 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002154 if (s != NULL)
2155 {
2156 set_option_value(arg, n, s, opt_flags);
2157 arg_end = p;
2158 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002160 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 }
2162 }
2163
2164 /*
2165 * ":let @r = expr": Set register contents.
2166 */
2167 else if (*arg == '@')
2168 {
2169 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002170 if (op != NULL && (*op == '+' || *op == '-'))
2171 EMSG2(_(e_letwrong), op);
2172 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002173 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174 EMSG(_(e_letunexp));
2175 else
2176 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002177 char_u *tofree = NULL;
2178 char_u *s;
2179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002180 p = get_tv_string_chk(tv);
2181 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002182 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002183 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002184 if (s != NULL)
2185 {
2186 p = tofree = concat_str(s, p);
2187 vim_free(s);
2188 }
2189 }
2190 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002191 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002192 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002193 arg_end = arg + 1;
2194 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002195 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 }
2197 }
2198
2199 /*
2200 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002203 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002205 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002207 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002208 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002210 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2211 EMSG(_(e_letunexp));
2212 else
2213 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002214 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 arg_end = p;
2216 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 }
2220
2221 else
2222 EMSG2(_(e_invarg2), arg);
2223
2224 return arg_end;
2225}
2226
2227/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002228 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2229 */
2230 static int
2231check_changedtick(arg)
2232 char_u *arg;
2233{
2234 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2235 {
2236 EMSG2(_(e_readonlyvar), arg);
2237 return TRUE;
2238 }
2239 return FALSE;
2240}
2241
2242/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002243 * Get an lval: variable, Dict item or List item that can be assigned a value
2244 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2245 * "name.key", "name.key[expr]" etc.
2246 * Indexing only works if "name" is an existing List or Dictionary.
2247 * "name" points to the start of the name.
2248 * If "rettv" is not NULL it points to the value to be assigned.
2249 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2250 * wrong; must end in space or cmd separator.
2251 *
2252 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 */
2256 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002257get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002259 typval_T *rettv;
2260 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 int unlet;
2262 int skip;
2263 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002264 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002267 char_u *expr_start, *expr_end;
2268 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002269 dictitem_T *v;
2270 typval_T var1;
2271 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002272 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002273 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002274 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002275 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002276 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002279 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280
2281 if (skip)
2282 {
2283 /* When skipping just find the end of the name. */
2284 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002285 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286 }
2287
2288 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002289 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 if (expr_start != NULL)
2291 {
2292 /* Don't expand the name when we already know there is an error. */
2293 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2294 && *p != '[' && *p != '.')
2295 {
2296 EMSG(_(e_trailing));
2297 return NULL;
2298 }
2299
2300 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2301 if (lp->ll_exp_name == NULL)
2302 {
2303 /* Report an invalid expression in braces, unless the
2304 * expression evaluation has been cancelled due to an
2305 * aborting error, an interrupt, or an exception. */
2306 if (!aborting() && !quiet)
2307 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002308 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002309 EMSG2(_(e_invarg2), name);
2310 return NULL;
2311 }
2312 }
2313 lp->ll_name = lp->ll_exp_name;
2314 }
2315 else
2316 lp->ll_name = name;
2317
2318 /* Without [idx] or .key we are done. */
2319 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2320 return p;
2321
2322 cc = *p;
2323 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002324 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 if (v == NULL && !quiet)
2326 EMSG2(_(e_undefvar), lp->ll_name);
2327 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 if (v == NULL)
2329 return NULL;
2330
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 /*
2332 * Loop until no more [idx] or .key is following.
2333 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002334 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002335 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2338 && !(lp->ll_tv->v_type == VAR_DICT
2339 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002341 if (!quiet)
2342 EMSG(_("E689: Can only index a List or Dictionary"));
2343 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002345 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 if (!quiet)
2348 EMSG(_("E708: [:] must come last"));
2349 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002351
Bram Moolenaar8c711452005-01-14 21:53:12 +00002352 len = -1;
2353 if (*p == '.')
2354 {
2355 key = p + 1;
2356 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2357 ;
2358 if (len == 0)
2359 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002360 if (!quiet)
2361 EMSG(_(e_emptykey));
2362 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002363 }
2364 p = key + len;
2365 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002366 else
2367 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002368 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002369 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002370 if (*p == ':')
2371 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002372 else
2373 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002374 empty1 = FALSE;
2375 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002377 if (get_tv_string_chk(&var1) == NULL)
2378 {
2379 /* not a number or string */
2380 clear_tv(&var1);
2381 return NULL;
2382 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002383 }
2384
2385 /* Optionally get the second index [ :expr]. */
2386 if (*p == ':')
2387 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002389 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002392 if (!empty1)
2393 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002395 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 if (rettv != NULL && (rettv->v_type != VAR_LIST
2397 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002398 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 if (!quiet)
2400 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002401 if (!empty1)
2402 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002404 }
2405 p = skipwhite(p + 1);
2406 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002408 else
2409 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002411 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2412 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002413 if (!empty1)
2414 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 if (get_tv_string_chk(&var2) == NULL)
2418 {
2419 /* not a number or string */
2420 if (!empty1)
2421 clear_tv(&var1);
2422 clear_tv(&var2);
2423 return NULL;
2424 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002425 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002426 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002427 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002428 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002430
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002432 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 if (!quiet)
2434 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002435 if (!empty1)
2436 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 }
2441
2442 /* Skip to past ']'. */
2443 ++p;
2444 }
2445
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 {
2448 if (len == -1)
2449 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 if (*key == NUL)
2453 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 if (!quiet)
2455 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 }
2459 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 lp->ll_list = NULL;
2461 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002462 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002464 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002465 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002467 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002470 if (len == -1)
2471 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 }
2474 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002476 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002478 if (len == -1)
2479 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 p = NULL;
2482 break;
2483 }
2484 if (len == -1)
2485 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 }
2488 else
2489 {
2490 /*
2491 * Get the number and item for the only or first index of the List.
2492 */
2493 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 else
2496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002497 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 clear_tv(&var1);
2499 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 lp->ll_list = lp->ll_tv->vval.v_list;
2502 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2503 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 if (!quiet)
2506 EMSGN(_(e_listidx), lp->ll_n1);
2507 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002508 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 }
2511
2512 /*
2513 * May need to find the item or absolute index for the second
2514 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 * When no index given: "lp->ll_empty2" is TRUE.
2516 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002517 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002520 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 if (ni == NULL)
2526 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 if (!quiet)
2528 EMSGN(_(e_listidx), lp->ll_n2);
2529 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 }
2533
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2535 if (lp->ll_n1 < 0)
2536 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2537 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002538 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 if (!quiet)
2540 EMSGN(_(e_listidx), lp->ll_n2);
2541 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002542 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002543 }
2544
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002546 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 }
2548
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 return p;
2550}
2551
2552/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002553 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 */
2555 static void
2556clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002557 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558{
2559 vim_free(lp->ll_exp_name);
2560 vim_free(lp->ll_newkey);
2561}
2562
2563/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002564 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002566 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 */
2568 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002569set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002570 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002574 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575{
2576 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 listitem_T *ri;
2578 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579
2580 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 cc = *endp;
2585 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002586 if (op != NULL && *op != '=')
2587 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002589
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002590 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002591 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2592 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002593 {
2594 if (tv_op(&tv, rettv, op) == OK)
2595 set_var(lp->ll_name, &tv, FALSE);
2596 clear_tv(&tv);
2597 }
2598 }
2599 else
2600 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002604 else if (tv_check_lock(lp->ll_newkey == NULL
2605 ? lp->ll_tv->v_lock
2606 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2607 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 else if (lp->ll_range)
2609 {
2610 /*
2611 * Assign the List values to the list items.
2612 */
2613 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002614 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002615 if (op != NULL && *op != '=')
2616 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2617 else
2618 {
2619 clear_tv(&lp->ll_li->li_tv);
2620 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2621 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 ri = ri->li_next;
2623 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2624 break;
2625 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002628 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 ri = NULL;
2631 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002633 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 lp->ll_li = lp->ll_li->li_next;
2635 ++lp->ll_n1;
2636 }
2637 if (ri != NULL)
2638 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002639 else if (lp->ll_empty2
2640 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 : lp->ll_n1 != lp->ll_n2)
2642 EMSG(_("E711: List value has not enough items"));
2643 }
2644 else
2645 {
2646 /*
2647 * Assign to a List or Dictionary item.
2648 */
2649 if (lp->ll_newkey != NULL)
2650 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002651 if (op != NULL && *op != '=')
2652 {
2653 EMSG2(_(e_letwrong), op);
2654 return;
2655 }
2656
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002658 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (di == NULL)
2660 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002661 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2662 {
2663 vim_free(di);
2664 return;
2665 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002667 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002668 else if (op != NULL && *op != '=')
2669 {
2670 tv_op(lp->ll_tv, rettv, op);
2671 return;
2672 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002673 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 /*
2677 * Assign the value to the variable or list item.
2678 */
2679 if (copy)
2680 copy_tv(rettv, lp->ll_tv);
2681 else
2682 {
2683 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002684 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002686 }
2687 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002688}
2689
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002690/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002691 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2692 * Returns OK or FAIL.
2693 */
2694 static int
2695tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002696 typval_T *tv1;
2697 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002698 char_u *op;
2699{
2700 long n;
2701 char_u numbuf[NUMBUFLEN];
2702 char_u *s;
2703
2704 /* Can't do anything with a Funcref or a Dict on the right. */
2705 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2706 {
2707 switch (tv1->v_type)
2708 {
2709 case VAR_DICT:
2710 case VAR_FUNC:
2711 break;
2712
2713 case VAR_LIST:
2714 if (*op != '+' || tv2->v_type != VAR_LIST)
2715 break;
2716 /* List += List */
2717 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2718 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2719 return OK;
2720
2721 case VAR_NUMBER:
2722 case VAR_STRING:
2723 if (tv2->v_type == VAR_LIST)
2724 break;
2725 if (*op == '+' || *op == '-')
2726 {
2727 /* nr += nr or nr -= nr*/
2728 n = get_tv_number(tv1);
2729 if (*op == '+')
2730 n += get_tv_number(tv2);
2731 else
2732 n -= get_tv_number(tv2);
2733 clear_tv(tv1);
2734 tv1->v_type = VAR_NUMBER;
2735 tv1->vval.v_number = n;
2736 }
2737 else
2738 {
2739 /* str .= str */
2740 s = get_tv_string(tv1);
2741 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2742 clear_tv(tv1);
2743 tv1->v_type = VAR_STRING;
2744 tv1->vval.v_string = s;
2745 }
2746 return OK;
2747 }
2748 }
2749
2750 EMSG2(_(e_letwrong), op);
2751 return FAIL;
2752}
2753
2754/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002755 * Add a watcher to a list.
2756 */
2757 static void
2758list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002759 list_T *l;
2760 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002761{
2762 lw->lw_next = l->lv_watch;
2763 l->lv_watch = lw;
2764}
2765
2766/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002767 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002768 * No warning when it isn't found...
2769 */
2770 static void
2771list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002772 list_T *l;
2773 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002774{
Bram Moolenaar33570922005-01-25 22:26:29 +00002775 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002776
2777 lwp = &l->lv_watch;
2778 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2779 {
2780 if (lw == lwrem)
2781 {
2782 *lwp = lw->lw_next;
2783 break;
2784 }
2785 lwp = &lw->lw_next;
2786 }
2787}
2788
2789/*
2790 * Just before removing an item from a list: advance watchers to the next
2791 * item.
2792 */
2793 static void
2794list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002795 list_T *l;
2796 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002797{
Bram Moolenaar33570922005-01-25 22:26:29 +00002798 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002799
2800 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2801 if (lw->lw_item == item)
2802 lw->lw_item = item->li_next;
2803}
2804
2805/*
2806 * Evaluate the expression used in a ":for var in expr" command.
2807 * "arg" points to "var".
2808 * Set "*errp" to TRUE for an error, FALSE otherwise;
2809 * Return a pointer that holds the info. Null when there is an error.
2810 */
2811 void *
2812eval_for_line(arg, errp, nextcmdp, skip)
2813 char_u *arg;
2814 int *errp;
2815 char_u **nextcmdp;
2816 int skip;
2817{
Bram Moolenaar33570922005-01-25 22:26:29 +00002818 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002819 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002820 typval_T tv;
2821 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002822
2823 *errp = TRUE; /* default: there is an error */
2824
Bram Moolenaar33570922005-01-25 22:26:29 +00002825 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002826 if (fi == NULL)
2827 return NULL;
2828
2829 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2830 if (expr == NULL)
2831 return fi;
2832
2833 expr = skipwhite(expr);
2834 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2835 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002836 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002837 return fi;
2838 }
2839
2840 if (skip)
2841 ++emsg_skip;
2842 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2843 {
2844 *errp = FALSE;
2845 if (!skip)
2846 {
2847 l = tv.vval.v_list;
2848 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002849 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002850 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002851 clear_tv(&tv);
2852 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002853 else
2854 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002855 /* No need to increment the refcount, it's already set for the
2856 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002857 fi->fi_list = l;
2858 list_add_watch(l, &fi->fi_lw);
2859 fi->fi_lw.lw_item = l->lv_first;
2860 }
2861 }
2862 }
2863 if (skip)
2864 --emsg_skip;
2865
2866 return fi;
2867}
2868
2869/*
2870 * Use the first item in a ":for" list. Advance to the next.
2871 * Assign the values to the variable (list). "arg" points to the first one.
2872 * Return TRUE when a valid item was found, FALSE when at end of list or
2873 * something wrong.
2874 */
2875 int
2876next_for_item(fi_void, arg)
2877 void *fi_void;
2878 char_u *arg;
2879{
Bram Moolenaar33570922005-01-25 22:26:29 +00002880 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002881 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883
2884 item = fi->fi_lw.lw_item;
2885 if (item == NULL)
2886 result = FALSE;
2887 else
2888 {
2889 fi->fi_lw.lw_item = item->li_next;
2890 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2891 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2892 }
2893 return result;
2894}
2895
2896/*
2897 * Free the structure used to store info used by ":for".
2898 */
2899 void
2900free_for_info(fi_void)
2901 void *fi_void;
2902{
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002904
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002905 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002906 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002907 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002908 list_unref(fi->fi_list);
2909 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002910 vim_free(fi);
2911}
2912
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2914
2915 void
2916set_context_for_expression(xp, arg, cmdidx)
2917 expand_T *xp;
2918 char_u *arg;
2919 cmdidx_T cmdidx;
2920{
2921 int got_eq = FALSE;
2922 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002923 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002925 if (cmdidx == CMD_let)
2926 {
2927 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002928 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002929 {
2930 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002931 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002932 {
2933 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002934 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002935 if (vim_iswhite(*p))
2936 break;
2937 }
2938 return;
2939 }
2940 }
2941 else
2942 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2943 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 while ((xp->xp_pattern = vim_strpbrk(arg,
2945 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2946 {
2947 c = *xp->xp_pattern;
2948 if (c == '&')
2949 {
2950 c = xp->xp_pattern[1];
2951 if (c == '&')
2952 {
2953 ++xp->xp_pattern;
2954 xp->xp_context = cmdidx != CMD_let || got_eq
2955 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2956 }
2957 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002960 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2961 xp->xp_pattern += 2;
2962
2963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 }
2965 else if (c == '$')
2966 {
2967 /* environment variable */
2968 xp->xp_context = EXPAND_ENV_VARS;
2969 }
2970 else if (c == '=')
2971 {
2972 got_eq = TRUE;
2973 xp->xp_context = EXPAND_EXPRESSION;
2974 }
2975 else if (c == '<'
2976 && xp->xp_context == EXPAND_FUNCTIONS
2977 && vim_strchr(xp->xp_pattern, '(') == NULL)
2978 {
2979 /* Function name can start with "<SNR>" */
2980 break;
2981 }
2982 else if (cmdidx != CMD_let || got_eq)
2983 {
2984 if (c == '"') /* string */
2985 {
2986 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2987 if (c == '\\' && xp->xp_pattern[1] != NUL)
2988 ++xp->xp_pattern;
2989 xp->xp_context = EXPAND_NOTHING;
2990 }
2991 else if (c == '\'') /* literal string */
2992 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002993 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2995 /* skip */ ;
2996 xp->xp_context = EXPAND_NOTHING;
2997 }
2998 else if (c == '|')
2999 {
3000 if (xp->xp_pattern[1] == '|')
3001 {
3002 ++xp->xp_pattern;
3003 xp->xp_context = EXPAND_EXPRESSION;
3004 }
3005 else
3006 xp->xp_context = EXPAND_COMMANDS;
3007 }
3008 else
3009 xp->xp_context = EXPAND_EXPRESSION;
3010 }
3011 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003012 /* Doesn't look like something valid, expand as an expression
3013 * anyway. */
3014 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 arg = xp->xp_pattern;
3016 if (*arg != NUL)
3017 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3018 /* skip */ ;
3019 }
3020 xp->xp_pattern = arg;
3021}
3022
3023#endif /* FEAT_CMDL_COMPL */
3024
3025/*
3026 * ":1,25call func(arg1, arg2)" function call.
3027 */
3028 void
3029ex_call(eap)
3030 exarg_T *eap;
3031{
3032 char_u *arg = eap->arg;
3033 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003035 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003037 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 linenr_T lnum;
3039 int doesrange;
3040 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003041 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003043 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3044 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003045 if (tofree == NULL)
3046 return;
3047
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003048 /* Increase refcount on dictionary, it could get deleted when evaluating
3049 * the arguments. */
3050 if (fudi.fd_dict != NULL)
3051 ++fudi.fd_dict->dv_refcount;
3052
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3054 len = STRLEN(tofree);
3055 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056
Bram Moolenaar532c7802005-01-27 14:44:31 +00003057 /* Skip white space to allow ":call func ()". Not good, but required for
3058 * backward compatibility. */
3059 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003060 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
3062 if (*startarg != '(')
3063 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003064 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 goto end;
3066 }
3067
3068 /*
3069 * When skipping, evaluate the function once, to find the end of the
3070 * arguments.
3071 * When the function takes a range, this is discovered after the first
3072 * call, and the loop is broken.
3073 */
3074 if (eap->skip)
3075 {
3076 ++emsg_skip;
3077 lnum = eap->line2; /* do it once, also with an invalid range */
3078 }
3079 else
3080 lnum = eap->line1;
3081 for ( ; lnum <= eap->line2; ++lnum)
3082 {
3083 if (!eap->skip && eap->addr_count > 0)
3084 {
3085 curwin->w_cursor.lnum = lnum;
3086 curwin->w_cursor.col = 0;
3087 }
3088 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003090 eap->line1, eap->line2, &doesrange,
3091 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 {
3093 failed = TRUE;
3094 break;
3095 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003096 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 if (doesrange || eap->skip)
3098 break;
3099 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003100 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003101 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003102 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 if (aborting())
3104 break;
3105 }
3106 if (eap->skip)
3107 --emsg_skip;
3108
3109 if (!failed)
3110 {
3111 /* Check for trailing illegal characters and a following command. */
3112 if (!ends_excmd(*arg))
3113 {
3114 emsg_severe = TRUE;
3115 EMSG(_(e_trailing));
3116 }
3117 else
3118 eap->nextcmd = check_nextcmd(arg);
3119 }
3120
3121end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003122 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003123 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124}
3125
3126/*
3127 * ":unlet[!] var1 ... " command.
3128 */
3129 void
3130ex_unlet(eap)
3131 exarg_T *eap;
3132{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003133 ex_unletlock(eap, eap->arg, 0);
3134}
3135
3136/*
3137 * ":lockvar" and ":unlockvar" commands
3138 */
3139 void
3140ex_lockvar(eap)
3141 exarg_T *eap;
3142{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003144 int deep = 2;
3145
3146 if (eap->forceit)
3147 deep = -1;
3148 else if (vim_isdigit(*arg))
3149 {
3150 deep = getdigits(&arg);
3151 arg = skipwhite(arg);
3152 }
3153
3154 ex_unletlock(eap, arg, deep);
3155}
3156
3157/*
3158 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3159 */
3160 static void
3161ex_unletlock(eap, argstart, deep)
3162 exarg_T *eap;
3163 char_u *argstart;
3164 int deep;
3165{
3166 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003169 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 do
3172 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003173 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003174 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3175 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003176 if (lv.ll_name == NULL)
3177 error = TRUE; /* error but continue parsing */
3178 if (name_end == NULL || (!vim_iswhite(*name_end)
3179 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003181 if (name_end != NULL)
3182 {
3183 emsg_severe = TRUE;
3184 EMSG(_(e_trailing));
3185 }
3186 if (!(eap->skip || error))
3187 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 break;
3189 }
3190
3191 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003192 {
3193 if (eap->cmdidx == CMD_unlet)
3194 {
3195 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3196 error = TRUE;
3197 }
3198 else
3199 {
3200 if (do_lock_var(&lv, name_end, deep,
3201 eap->cmdidx == CMD_lockvar) == FAIL)
3202 error = TRUE;
3203 }
3204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003206 if (!eap->skip)
3207 clear_lval(&lv);
3208
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 arg = skipwhite(name_end);
3210 } while (!ends_excmd(*arg));
3211
3212 eap->nextcmd = check_nextcmd(arg);
3213}
3214
Bram Moolenaar8c711452005-01-14 21:53:12 +00003215 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003216do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003218 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003219 int forceit;
3220{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003221 int ret = OK;
3222 int cc;
3223
3224 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003225 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003226 cc = *name_end;
3227 *name_end = NUL;
3228
3229 /* Normal name or expanded name. */
3230 if (check_changedtick(lp->ll_name))
3231 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003232 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003233 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003234 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003235 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003236 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3237 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003238 else if (lp->ll_range)
3239 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003241
3242 /* Delete a range of List items. */
3243 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3244 {
3245 li = lp->ll_li->li_next;
3246 listitem_remove(lp->ll_list, lp->ll_li);
3247 lp->ll_li = li;
3248 ++lp->ll_n1;
3249 }
3250 }
3251 else
3252 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003253 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003254 /* unlet a List item. */
3255 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003256 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003257 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003258 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003259 }
3260
3261 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003262}
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264/*
3265 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003266 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 */
3268 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003269do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003271 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
Bram Moolenaar33570922005-01-25 22:26:29 +00003273 hashtab_T *ht;
3274 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003275 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 ht = find_var_ht(name, &varname);
3278 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003280 hi = hash_find(ht, varname);
3281 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003282 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003283 if (var_check_ro(HI2DI(hi)->di_flags, name))
3284 return FAIL;
3285 delete_var(ht, hi);
3286 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003289 if (forceit)
3290 return OK;
3291 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 return FAIL;
3293}
3294
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003295/*
3296 * Lock or unlock variable indicated by "lp".
3297 * "deep" is the levels to go (-1 for unlimited);
3298 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3299 */
3300 static int
3301do_lock_var(lp, name_end, deep, lock)
3302 lval_T *lp;
3303 char_u *name_end;
3304 int deep;
3305 int lock;
3306{
3307 int ret = OK;
3308 int cc;
3309 dictitem_T *di;
3310
3311 if (deep == 0) /* nothing to do */
3312 return OK;
3313
3314 if (lp->ll_tv == NULL)
3315 {
3316 cc = *name_end;
3317 *name_end = NUL;
3318
3319 /* Normal name or expanded name. */
3320 if (check_changedtick(lp->ll_name))
3321 ret = FAIL;
3322 else
3323 {
3324 di = find_var(lp->ll_name, NULL);
3325 if (di == NULL)
3326 ret = FAIL;
3327 else
3328 {
3329 if (lock)
3330 di->di_flags |= DI_FLAGS_LOCK;
3331 else
3332 di->di_flags &= ~DI_FLAGS_LOCK;
3333 item_lock(&di->di_tv, deep, lock);
3334 }
3335 }
3336 *name_end = cc;
3337 }
3338 else if (lp->ll_range)
3339 {
3340 listitem_T *li = lp->ll_li;
3341
3342 /* (un)lock a range of List items. */
3343 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3344 {
3345 item_lock(&li->li_tv, deep, lock);
3346 li = li->li_next;
3347 ++lp->ll_n1;
3348 }
3349 }
3350 else if (lp->ll_list != NULL)
3351 /* (un)lock a List item. */
3352 item_lock(&lp->ll_li->li_tv, deep, lock);
3353 else
3354 /* un(lock) a Dictionary item. */
3355 item_lock(&lp->ll_di->di_tv, deep, lock);
3356
3357 return ret;
3358}
3359
3360/*
3361 * Lock or unlock an item. "deep" is nr of levels to go.
3362 */
3363 static void
3364item_lock(tv, deep, lock)
3365 typval_T *tv;
3366 int deep;
3367 int lock;
3368{
3369 static int recurse = 0;
3370 list_T *l;
3371 listitem_T *li;
3372 dict_T *d;
3373 hashitem_T *hi;
3374 int todo;
3375
3376 if (recurse >= DICT_MAXNEST)
3377 {
3378 EMSG(_("E743: variable nested too deep for (un)lock"));
3379 return;
3380 }
3381 if (deep == 0)
3382 return;
3383 ++recurse;
3384
3385 /* lock/unlock the item itself */
3386 if (lock)
3387 tv->v_lock |= VAR_LOCKED;
3388 else
3389 tv->v_lock &= ~VAR_LOCKED;
3390
3391 switch (tv->v_type)
3392 {
3393 case VAR_LIST:
3394 if ((l = tv->vval.v_list) != NULL)
3395 {
3396 if (lock)
3397 l->lv_lock |= VAR_LOCKED;
3398 else
3399 l->lv_lock &= ~VAR_LOCKED;
3400 if (deep < 0 || deep > 1)
3401 /* recursive: lock/unlock the items the List contains */
3402 for (li = l->lv_first; li != NULL; li = li->li_next)
3403 item_lock(&li->li_tv, deep - 1, lock);
3404 }
3405 break;
3406 case VAR_DICT:
3407 if ((d = tv->vval.v_dict) != NULL)
3408 {
3409 if (lock)
3410 d->dv_lock |= VAR_LOCKED;
3411 else
3412 d->dv_lock &= ~VAR_LOCKED;
3413 if (deep < 0 || deep > 1)
3414 {
3415 /* recursive: lock/unlock the items the List contains */
3416 todo = d->dv_hashtab.ht_used;
3417 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3418 {
3419 if (!HASHITEM_EMPTY(hi))
3420 {
3421 --todo;
3422 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3423 }
3424 }
3425 }
3426 }
3427 }
3428 --recurse;
3429}
3430
Bram Moolenaara40058a2005-07-11 22:42:07 +00003431/*
3432 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3433 * it refers to a List or Dictionary that is locked.
3434 */
3435 static int
3436tv_islocked(tv)
3437 typval_T *tv;
3438{
3439 return (tv->v_lock & VAR_LOCKED)
3440 || (tv->v_type == VAR_LIST
3441 && tv->vval.v_list != NULL
3442 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3443 || (tv->v_type == VAR_DICT
3444 && tv->vval.v_dict != NULL
3445 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3446}
3447
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3449/*
3450 * Delete all "menutrans_" variables.
3451 */
3452 void
3453del_menutrans_vars()
3454{
Bram Moolenaar33570922005-01-25 22:26:29 +00003455 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003456 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
Bram Moolenaar33570922005-01-25 22:26:29 +00003458 hash_lock(&globvarht);
3459 todo = globvarht.ht_used;
3460 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003461 {
3462 if (!HASHITEM_EMPTY(hi))
3463 {
3464 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003465 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3466 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003467 }
3468 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003469 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470}
3471#endif
3472
3473#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3474
3475/*
3476 * Local string buffer for the next two functions to store a variable name
3477 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3478 * get_user_var_name().
3479 */
3480
3481static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3482
3483static char_u *varnamebuf = NULL;
3484static int varnamebuflen = 0;
3485
3486/*
3487 * Function to concatenate a prefix and a variable name.
3488 */
3489 static char_u *
3490cat_prefix_varname(prefix, name)
3491 int prefix;
3492 char_u *name;
3493{
3494 int len;
3495
3496 len = (int)STRLEN(name) + 3;
3497 if (len > varnamebuflen)
3498 {
3499 vim_free(varnamebuf);
3500 len += 10; /* some additional space */
3501 varnamebuf = alloc(len);
3502 if (varnamebuf == NULL)
3503 {
3504 varnamebuflen = 0;
3505 return NULL;
3506 }
3507 varnamebuflen = len;
3508 }
3509 *varnamebuf = prefix;
3510 varnamebuf[1] = ':';
3511 STRCPY(varnamebuf + 2, name);
3512 return varnamebuf;
3513}
3514
3515/*
3516 * Function given to ExpandGeneric() to obtain the list of user defined
3517 * (global/buffer/window/built-in) variable names.
3518 */
3519/*ARGSUSED*/
3520 char_u *
3521get_user_var_name(xp, idx)
3522 expand_T *xp;
3523 int idx;
3524{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003525 static long_u gdone;
3526 static long_u bdone;
3527 static long_u wdone;
3528 static int vidx;
3529 static hashitem_T *hi;
3530 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
3532 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003533 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003534
3535 /* Global variables */
3536 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003538 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003539 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003540 else
3541 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003542 while (HASHITEM_EMPTY(hi))
3543 ++hi;
3544 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3545 return cat_prefix_varname('g', hi->hi_key);
3546 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003548
3549 /* b: variables */
3550 ht = &curbuf->b_vars.dv_hashtab;
3551 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003553 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003554 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003555 else
3556 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003557 while (HASHITEM_EMPTY(hi))
3558 ++hi;
3559 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003561 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003563 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 return (char_u *)"b:changedtick";
3565 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003566
3567 /* w: variables */
3568 ht = &curwin->w_vars.dv_hashtab;
3569 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003571 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003572 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003573 else
3574 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003575 while (HASHITEM_EMPTY(hi))
3576 ++hi;
3577 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003579
3580 /* v: variables */
3581 if (vidx < VV_LEN)
3582 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
3584 vim_free(varnamebuf);
3585 varnamebuf = NULL;
3586 varnamebuflen = 0;
3587 return NULL;
3588}
3589
3590#endif /* FEAT_CMDL_COMPL */
3591
3592/*
3593 * types for expressions.
3594 */
3595typedef enum
3596{
3597 TYPE_UNKNOWN = 0
3598 , TYPE_EQUAL /* == */
3599 , TYPE_NEQUAL /* != */
3600 , TYPE_GREATER /* > */
3601 , TYPE_GEQUAL /* >= */
3602 , TYPE_SMALLER /* < */
3603 , TYPE_SEQUAL /* <= */
3604 , TYPE_MATCH /* =~ */
3605 , TYPE_NOMATCH /* !~ */
3606} exptype_T;
3607
3608/*
3609 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003610 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3612 */
3613
3614/*
3615 * Handle zero level expression.
3616 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003617 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003618 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 * Return OK or FAIL.
3620 */
3621 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003622eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 char_u **nextcmd;
3626 int evaluate;
3627{
3628 int ret;
3629 char_u *p;
3630
3631 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003632 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 if (ret == FAIL || !ends_excmd(*p))
3634 {
3635 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003636 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 /*
3638 * Report the invalid expression unless the expression evaluation has
3639 * been cancelled due to an aborting error, an interrupt, or an
3640 * exception.
3641 */
3642 if (!aborting())
3643 EMSG2(_(e_invexpr2), arg);
3644 ret = FAIL;
3645 }
3646 if (nextcmd != NULL)
3647 *nextcmd = check_nextcmd(p);
3648
3649 return ret;
3650}
3651
3652/*
3653 * Handle top level expression:
3654 * expr1 ? expr0 : expr0
3655 *
3656 * "arg" must point to the first non-white of the expression.
3657 * "arg" is advanced to the next non-white after the recognized expression.
3658 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003659 * Note: "rettv.v_lock" is not set.
3660 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 * Return OK or FAIL.
3662 */
3663 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003664eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 int evaluate;
3668{
3669 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003670 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
3672 /*
3673 * Get the first variable.
3674 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003675 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 return FAIL;
3677
3678 if ((*arg)[0] == '?')
3679 {
3680 result = FALSE;
3681 if (evaluate)
3682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003683 int error = FALSE;
3684
3685 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003687 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003688 if (error)
3689 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691
3692 /*
3693 * Get the second variable.
3694 */
3695 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003696 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 return FAIL;
3698
3699 /*
3700 * Check for the ":".
3701 */
3702 if ((*arg)[0] != ':')
3703 {
3704 EMSG(_("E109: Missing ':' after '?'"));
3705 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003706 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 return FAIL;
3708 }
3709
3710 /*
3711 * Get the third variable.
3712 */
3713 *arg = skipwhite(*arg + 1);
3714 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3715 {
3716 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003717 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 return FAIL;
3719 }
3720 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003721 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 }
3723
3724 return OK;
3725}
3726
3727/*
3728 * Handle first level expression:
3729 * expr2 || expr2 || expr2 logical OR
3730 *
3731 * "arg" must point to the first non-white of the expression.
3732 * "arg" is advanced to the next non-white after the recognized expression.
3733 *
3734 * Return OK or FAIL.
3735 */
3736 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003737eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 int evaluate;
3741{
Bram Moolenaar33570922005-01-25 22:26:29 +00003742 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 long result;
3744 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003745 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746
3747 /*
3748 * Get the first variable.
3749 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003750 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 return FAIL;
3752
3753 /*
3754 * Repeat until there is no following "||".
3755 */
3756 first = TRUE;
3757 result = FALSE;
3758 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3759 {
3760 if (evaluate && first)
3761 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003762 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003764 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003765 if (error)
3766 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 first = FALSE;
3768 }
3769
3770 /*
3771 * Get the second variable.
3772 */
3773 *arg = skipwhite(*arg + 2);
3774 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3775 return FAIL;
3776
3777 /*
3778 * Compute the result.
3779 */
3780 if (evaluate && !result)
3781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003782 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003784 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003785 if (error)
3786 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
3788 if (evaluate)
3789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003790 rettv->v_type = VAR_NUMBER;
3791 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793 }
3794
3795 return OK;
3796}
3797
3798/*
3799 * Handle second level expression:
3800 * expr3 && expr3 && expr3 logical AND
3801 *
3802 * "arg" must point to the first non-white of the expression.
3803 * "arg" is advanced to the next non-white after the recognized expression.
3804 *
3805 * Return OK or FAIL.
3806 */
3807 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003808eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 int evaluate;
3812{
Bram Moolenaar33570922005-01-25 22:26:29 +00003813 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 long result;
3815 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003816 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817
3818 /*
3819 * Get the first variable.
3820 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003821 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 return FAIL;
3823
3824 /*
3825 * Repeat until there is no following "&&".
3826 */
3827 first = TRUE;
3828 result = TRUE;
3829 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3830 {
3831 if (evaluate && first)
3832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003833 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003835 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003836 if (error)
3837 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 first = FALSE;
3839 }
3840
3841 /*
3842 * Get the second variable.
3843 */
3844 *arg = skipwhite(*arg + 2);
3845 if (eval4(arg, &var2, evaluate && result) == FAIL)
3846 return FAIL;
3847
3848 /*
3849 * Compute the result.
3850 */
3851 if (evaluate && result)
3852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003853 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003855 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003856 if (error)
3857 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 }
3859 if (evaluate)
3860 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003861 rettv->v_type = VAR_NUMBER;
3862 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
3864 }
3865
3866 return OK;
3867}
3868
3869/*
3870 * Handle third level expression:
3871 * var1 == var2
3872 * var1 =~ var2
3873 * var1 != var2
3874 * var1 !~ var2
3875 * var1 > var2
3876 * var1 >= var2
3877 * var1 < var2
3878 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003879 * var1 is var2
3880 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 *
3882 * "arg" must point to the first non-white of the expression.
3883 * "arg" is advanced to the next non-white after the recognized expression.
3884 *
3885 * Return OK or FAIL.
3886 */
3887 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003888eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 int evaluate;
3892{
Bram Moolenaar33570922005-01-25 22:26:29 +00003893 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 char_u *p;
3895 int i;
3896 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003897 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 int len = 2;
3899 long n1, n2;
3900 char_u *s1, *s2;
3901 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3902 regmatch_T regmatch;
3903 int ic;
3904 char_u *save_cpo;
3905
3906 /*
3907 * Get the first variable.
3908 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003909 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 return FAIL;
3911
3912 p = *arg;
3913 switch (p[0])
3914 {
3915 case '=': if (p[1] == '=')
3916 type = TYPE_EQUAL;
3917 else if (p[1] == '~')
3918 type = TYPE_MATCH;
3919 break;
3920 case '!': if (p[1] == '=')
3921 type = TYPE_NEQUAL;
3922 else if (p[1] == '~')
3923 type = TYPE_NOMATCH;
3924 break;
3925 case '>': if (p[1] != '=')
3926 {
3927 type = TYPE_GREATER;
3928 len = 1;
3929 }
3930 else
3931 type = TYPE_GEQUAL;
3932 break;
3933 case '<': if (p[1] != '=')
3934 {
3935 type = TYPE_SMALLER;
3936 len = 1;
3937 }
3938 else
3939 type = TYPE_SEQUAL;
3940 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003941 case 'i': if (p[1] == 's')
3942 {
3943 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3944 len = 5;
3945 if (!vim_isIDc(p[len]))
3946 {
3947 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3948 type_is = TRUE;
3949 }
3950 }
3951 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
3953
3954 /*
3955 * If there is a comparitive operator, use it.
3956 */
3957 if (type != TYPE_UNKNOWN)
3958 {
3959 /* extra question mark appended: ignore case */
3960 if (p[len] == '?')
3961 {
3962 ic = TRUE;
3963 ++len;
3964 }
3965 /* extra '#' appended: match case */
3966 else if (p[len] == '#')
3967 {
3968 ic = FALSE;
3969 ++len;
3970 }
3971 /* nothing appened: use 'ignorecase' */
3972 else
3973 ic = p_ic;
3974
3975 /*
3976 * Get the second variable.
3977 */
3978 *arg = skipwhite(p + len);
3979 if (eval5(arg, &var2, evaluate) == FAIL)
3980 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003981 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 return FAIL;
3983 }
3984
3985 if (evaluate)
3986 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003987 if (type_is && rettv->v_type != var2.v_type)
3988 {
3989 /* For "is" a different type always means FALSE, for "notis"
3990 * it means TRUE. */
3991 n1 = (type == TYPE_NEQUAL);
3992 }
3993 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3994 {
3995 if (type_is)
3996 {
3997 n1 = (rettv->v_type == var2.v_type
3998 && rettv->vval.v_list == var2.vval.v_list);
3999 if (type == TYPE_NEQUAL)
4000 n1 = !n1;
4001 }
4002 else if (rettv->v_type != var2.v_type
4003 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4004 {
4005 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004006 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004007 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004008 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004009 clear_tv(rettv);
4010 clear_tv(&var2);
4011 return FAIL;
4012 }
4013 else
4014 {
4015 /* Compare two Lists for being equal or unequal. */
4016 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4017 if (type == TYPE_NEQUAL)
4018 n1 = !n1;
4019 }
4020 }
4021
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004022 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4023 {
4024 if (type_is)
4025 {
4026 n1 = (rettv->v_type == var2.v_type
4027 && rettv->vval.v_dict == var2.vval.v_dict);
4028 if (type == TYPE_NEQUAL)
4029 n1 = !n1;
4030 }
4031 else if (rettv->v_type != var2.v_type
4032 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4033 {
4034 if (rettv->v_type != var2.v_type)
4035 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4036 else
4037 EMSG(_("E736: Invalid operation for Dictionary"));
4038 clear_tv(rettv);
4039 clear_tv(&var2);
4040 return FAIL;
4041 }
4042 else
4043 {
4044 /* Compare two Dictionaries for being equal or unequal. */
4045 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4046 if (type == TYPE_NEQUAL)
4047 n1 = !n1;
4048 }
4049 }
4050
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004051 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4052 {
4053 if (rettv->v_type != var2.v_type
4054 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4055 {
4056 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004057 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004058 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004059 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004060 clear_tv(rettv);
4061 clear_tv(&var2);
4062 return FAIL;
4063 }
4064 else
4065 {
4066 /* Compare two Funcrefs for being equal or unequal. */
4067 if (rettv->vval.v_string == NULL
4068 || var2.vval.v_string == NULL)
4069 n1 = FALSE;
4070 else
4071 n1 = STRCMP(rettv->vval.v_string,
4072 var2.vval.v_string) == 0;
4073 if (type == TYPE_NEQUAL)
4074 n1 = !n1;
4075 }
4076 }
4077
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 /*
4079 * If one of the two variables is a number, compare as a number.
4080 * When using "=~" or "!~", always compare as string.
4081 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004082 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4084 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004085 n1 = get_tv_number(rettv);
4086 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 switch (type)
4088 {
4089 case TYPE_EQUAL: n1 = (n1 == n2); break;
4090 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4091 case TYPE_GREATER: n1 = (n1 > n2); break;
4092 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4093 case TYPE_SMALLER: n1 = (n1 < n2); break;
4094 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4095 case TYPE_UNKNOWN:
4096 case TYPE_MATCH:
4097 case TYPE_NOMATCH: break; /* avoid gcc warning */
4098 }
4099 }
4100 else
4101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004102 s1 = get_tv_string_buf(rettv, buf1);
4103 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4105 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4106 else
4107 i = 0;
4108 n1 = FALSE;
4109 switch (type)
4110 {
4111 case TYPE_EQUAL: n1 = (i == 0); break;
4112 case TYPE_NEQUAL: n1 = (i != 0); break;
4113 case TYPE_GREATER: n1 = (i > 0); break;
4114 case TYPE_GEQUAL: n1 = (i >= 0); break;
4115 case TYPE_SMALLER: n1 = (i < 0); break;
4116 case TYPE_SEQUAL: n1 = (i <= 0); break;
4117
4118 case TYPE_MATCH:
4119 case TYPE_NOMATCH:
4120 /* avoid 'l' flag in 'cpoptions' */
4121 save_cpo = p_cpo;
4122 p_cpo = (char_u *)"";
4123 regmatch.regprog = vim_regcomp(s2,
4124 RE_MAGIC + RE_STRING);
4125 regmatch.rm_ic = ic;
4126 if (regmatch.regprog != NULL)
4127 {
4128 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4129 vim_free(regmatch.regprog);
4130 if (type == TYPE_NOMATCH)
4131 n1 = !n1;
4132 }
4133 p_cpo = save_cpo;
4134 break;
4135
4136 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4137 }
4138 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139 clear_tv(rettv);
4140 clear_tv(&var2);
4141 rettv->v_type = VAR_NUMBER;
4142 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144 }
4145
4146 return OK;
4147}
4148
4149/*
4150 * Handle fourth level expression:
4151 * + number addition
4152 * - number subtraction
4153 * . string concatenation
4154 *
4155 * "arg" must point to the first non-white of the expression.
4156 * "arg" is advanced to the next non-white after the recognized expression.
4157 *
4158 * Return OK or FAIL.
4159 */
4160 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004161eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 int evaluate;
4165{
Bram Moolenaar33570922005-01-25 22:26:29 +00004166 typval_T var2;
4167 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 int op;
4169 long n1, n2;
4170 char_u *s1, *s2;
4171 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4172 char_u *p;
4173
4174 /*
4175 * Get the first variable.
4176 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 return FAIL;
4179
4180 /*
4181 * Repeat computing, until no '+', '-' or '.' is following.
4182 */
4183 for (;;)
4184 {
4185 op = **arg;
4186 if (op != '+' && op != '-' && op != '.')
4187 break;
4188
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004189 if (op != '+' || rettv->v_type != VAR_LIST)
4190 {
4191 /* For "list + ...", an illegal use of the first operand as
4192 * a number cannot be determined before evaluating the 2nd
4193 * operand: if this is also a list, all is ok.
4194 * For "something . ...", "something - ..." or "non-list + ...",
4195 * we know that the first operand needs to be a string or number
4196 * without evaluating the 2nd operand. So check before to avoid
4197 * side effects after an error. */
4198 if (evaluate && get_tv_string_chk(rettv) == NULL)
4199 {
4200 clear_tv(rettv);
4201 return FAIL;
4202 }
4203 }
4204
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 /*
4206 * Get the second variable.
4207 */
4208 *arg = skipwhite(*arg + 1);
4209 if (eval6(arg, &var2, evaluate) == FAIL)
4210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213 }
4214
4215 if (evaluate)
4216 {
4217 /*
4218 * Compute the result.
4219 */
4220 if (op == '.')
4221 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004222 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4223 s2 = get_tv_string_buf_chk(&var2, buf2);
4224 if (s2 == NULL) /* type error ? */
4225 {
4226 clear_tv(rettv);
4227 clear_tv(&var2);
4228 return FAIL;
4229 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004230 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231 clear_tv(rettv);
4232 rettv->v_type = VAR_STRING;
4233 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004235 else if (op == '+' && rettv->v_type == VAR_LIST
4236 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004237 {
4238 /* concatenate Lists */
4239 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4240 &var3) == FAIL)
4241 {
4242 clear_tv(rettv);
4243 clear_tv(&var2);
4244 return FAIL;
4245 }
4246 clear_tv(rettv);
4247 *rettv = var3;
4248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 else
4250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004251 int error = FALSE;
4252
4253 n1 = get_tv_number_chk(rettv, &error);
4254 if (error)
4255 {
4256 /* This can only happen for "list + non-list".
4257 * For "non-list + ..." or "something - ...", we returned
4258 * before evaluating the 2nd operand. */
4259 clear_tv(rettv);
4260 return FAIL;
4261 }
4262 n2 = get_tv_number_chk(&var2, &error);
4263 if (error)
4264 {
4265 clear_tv(rettv);
4266 clear_tv(&var2);
4267 return FAIL;
4268 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004269 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 if (op == '+')
4271 n1 = n1 + n2;
4272 else
4273 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 rettv->v_type = VAR_NUMBER;
4275 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 }
4280 return OK;
4281}
4282
4283/*
4284 * Handle fifth level expression:
4285 * * number multiplication
4286 * / number division
4287 * % number modulo
4288 *
4289 * "arg" must point to the first non-white of the expression.
4290 * "arg" is advanced to the next non-white after the recognized expression.
4291 *
4292 * Return OK or FAIL.
4293 */
4294 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 int evaluate;
4299{
Bram Moolenaar33570922005-01-25 22:26:29 +00004300 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 int op;
4302 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004303 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304
4305 /*
4306 * Get the first variable.
4307 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 return FAIL;
4310
4311 /*
4312 * Repeat computing, until no '*', '/' or '%' is following.
4313 */
4314 for (;;)
4315 {
4316 op = **arg;
4317 if (op != '*' && op != '/' && op != '%')
4318 break;
4319
4320 if (evaluate)
4321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004322 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324 if (error)
4325 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 }
4327 else
4328 n1 = 0;
4329
4330 /*
4331 * Get the second variable.
4332 */
4333 *arg = skipwhite(*arg + 1);
4334 if (eval7(arg, &var2, evaluate) == FAIL)
4335 return FAIL;
4336
4337 if (evaluate)
4338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (error)
4342 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343
4344 /*
4345 * Compute the result.
4346 */
4347 if (op == '*')
4348 n1 = n1 * n2;
4349 else if (op == '/')
4350 {
4351 if (n2 == 0) /* give an error message? */
4352 n1 = 0x7fffffffL;
4353 else
4354 n1 = n1 / n2;
4355 }
4356 else
4357 {
4358 if (n2 == 0) /* give an error message? */
4359 n1 = 0;
4360 else
4361 n1 = n1 % n2;
4362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004363 rettv->v_type = VAR_NUMBER;
4364 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 }
4367
4368 return OK;
4369}
4370
4371/*
4372 * Handle sixth level expression:
4373 * number number constant
4374 * "string" string contstant
4375 * 'string' literal string contstant
4376 * &option-name option value
4377 * @r register contents
4378 * identifier variable value
4379 * function() function call
4380 * $VAR environment variable
4381 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004382 * [expr, expr] List
4383 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 *
4385 * Also handle:
4386 * ! in front logical NOT
4387 * - in front unary minus
4388 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004389 * trailing [] subscript in String or List
4390 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 *
4392 * "arg" must point to the first non-white of the expression.
4393 * "arg" is advanced to the next non-white after the recognized expression.
4394 *
4395 * Return OK or FAIL.
4396 */
4397 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 int evaluate;
4402{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 long n;
4404 int len;
4405 char_u *s;
4406 int val;
4407 char_u *start_leader, *end_leader;
4408 int ret = OK;
4409 char_u *alias;
4410
4411 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004413 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004415 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416
4417 /*
4418 * Skip '!' and '-' characters. They are handled later.
4419 */
4420 start_leader = *arg;
4421 while (**arg == '!' || **arg == '-' || **arg == '+')
4422 *arg = skipwhite(*arg + 1);
4423 end_leader = *arg;
4424
4425 switch (**arg)
4426 {
4427 /*
4428 * Number constant.
4429 */
4430 case '0':
4431 case '1':
4432 case '2':
4433 case '3':
4434 case '4':
4435 case '5':
4436 case '6':
4437 case '7':
4438 case '8':
4439 case '9':
4440 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4441 *arg += len;
4442 if (evaluate)
4443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004444 rettv->v_type = VAR_NUMBER;
4445 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447 break;
4448
4449 /*
4450 * String constant: "string".
4451 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004452 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 break;
4454
4455 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004456 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004458 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004459 break;
4460
4461 /*
4462 * List: [expr, expr]
4463 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004464 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 break;
4466
4467 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004468 * Dictionary: {key: val, key: val}
4469 */
4470 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4471 break;
4472
4473 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004474 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004476 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 break;
4478
4479 /*
4480 * Environment variable: $VAR.
4481 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004482 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 break;
4484
4485 /*
4486 * Register contents: @r.
4487 */
4488 case '@': ++*arg;
4489 if (evaluate)
4490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004491 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004492 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494 if (**arg != NUL)
4495 ++*arg;
4496 break;
4497
4498 /*
4499 * nested expression: (expression).
4500 */
4501 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004502 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 if (**arg == ')')
4504 ++*arg;
4505 else if (ret == OK)
4506 {
4507 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004508 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 ret = FAIL;
4510 }
4511 break;
4512
Bram Moolenaar8c711452005-01-14 21:53:12 +00004513 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 break;
4515 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004516
4517 if (ret == NOTDONE)
4518 {
4519 /*
4520 * Must be a variable or function name.
4521 * Can also be a curly-braces kind of name: {expr}.
4522 */
4523 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004524 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004525 if (alias != NULL)
4526 s = alias;
4527
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004528 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004529 ret = FAIL;
4530 else
4531 {
4532 if (**arg == '(') /* recursive! */
4533 {
4534 /* If "s" is the name of a variable of type VAR_FUNC
4535 * use its contents. */
4536 s = deref_func_name(s, &len);
4537
4538 /* Invoke the function. */
4539 ret = get_func_tv(s, len, rettv, arg,
4540 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004541 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004542 /* Stop the expression evaluation when immediately
4543 * aborting on error, or when an interrupt occurred or
4544 * an exception was thrown but not caught. */
4545 if (aborting())
4546 {
4547 if (ret == OK)
4548 clear_tv(rettv);
4549 ret = FAIL;
4550 }
4551 }
4552 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004553 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004554 else
4555 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004556 }
4557
4558 if (alias != NULL)
4559 vim_free(alias);
4560 }
4561
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 *arg = skipwhite(*arg);
4563
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004564 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4565 * expr(expr). */
4566 if (ret == OK)
4567 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
4569 /*
4570 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4571 */
4572 if (ret == OK && evaluate && end_leader > start_leader)
4573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004574 int error = FALSE;
4575
4576 val = get_tv_number_chk(rettv, &error);
4577 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004579 clear_tv(rettv);
4580 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004582 else
4583 {
4584 while (end_leader > start_leader)
4585 {
4586 --end_leader;
4587 if (*end_leader == '!')
4588 val = !val;
4589 else if (*end_leader == '-')
4590 val = -val;
4591 }
4592 clear_tv(rettv);
4593 rettv->v_type = VAR_NUMBER;
4594 rettv->vval.v_number = val;
4595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
4597
4598 return ret;
4599}
4600
4601/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004602 * Evaluate an "[expr]" or "[expr:expr]" index.
4603 * "*arg" points to the '['.
4604 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4605 */
4606 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004607eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004608 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004609 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004610 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004611 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004612{
4613 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004614 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004616 long len = -1;
4617 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004618 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004619 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004621 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004623 if (verbose)
4624 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004625 return FAIL;
4626 }
4627
Bram Moolenaar8c711452005-01-14 21:53:12 +00004628 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004630 /*
4631 * dict.name
4632 */
4633 key = *arg + 1;
4634 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4635 ;
4636 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004637 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004638 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004639 }
4640 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004641 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004642 /*
4643 * something[idx]
4644 *
4645 * Get the (first) variable from inside the [].
4646 */
4647 *arg = skipwhite(*arg + 1);
4648 if (**arg == ':')
4649 empty1 = TRUE;
4650 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4651 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004652 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4653 {
4654 /* not a number or string */
4655 clear_tv(&var1);
4656 return FAIL;
4657 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004658
4659 /*
4660 * Get the second variable from inside the [:].
4661 */
4662 if (**arg == ':')
4663 {
4664 range = TRUE;
4665 *arg = skipwhite(*arg + 1);
4666 if (**arg == ']')
4667 empty2 = TRUE;
4668 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004670 if (!empty1)
4671 clear_tv(&var1);
4672 return FAIL;
4673 }
4674 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4675 {
4676 /* not a number or string */
4677 if (!empty1)
4678 clear_tv(&var1);
4679 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004680 return FAIL;
4681 }
4682 }
4683
4684 /* Check for the ']'. */
4685 if (**arg != ']')
4686 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004687 if (verbose)
4688 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689 clear_tv(&var1);
4690 if (range)
4691 clear_tv(&var2);
4692 return FAIL;
4693 }
4694 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 }
4696
4697 if (evaluate)
4698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004699 n1 = 0;
4700 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004702 n1 = get_tv_number(&var1);
4703 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004704 }
4705 if (range)
4706 {
4707 if (empty2)
4708 n2 = -1;
4709 else
4710 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004711 n2 = get_tv_number(&var2);
4712 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004713 }
4714 }
4715
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004716 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004717 {
4718 case VAR_NUMBER:
4719 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004720 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 len = (long)STRLEN(s);
4722 if (range)
4723 {
4724 /* The resulting variable is a substring. If the indexes
4725 * are out of range the result is empty. */
4726 if (n1 < 0)
4727 {
4728 n1 = len + n1;
4729 if (n1 < 0)
4730 n1 = 0;
4731 }
4732 if (n2 < 0)
4733 n2 = len + n2;
4734 else if (n2 >= len)
4735 n2 = len;
4736 if (n1 >= len || n2 < 0 || n1 > n2)
4737 s = NULL;
4738 else
4739 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4740 }
4741 else
4742 {
4743 /* The resulting variable is a string of a single
4744 * character. If the index is too big or negative the
4745 * result is empty. */
4746 if (n1 >= len || n1 < 0)
4747 s = NULL;
4748 else
4749 s = vim_strnsave(s + n1, 1);
4750 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004751 clear_tv(rettv);
4752 rettv->v_type = VAR_STRING;
4753 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754 break;
4755
4756 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004757 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004758 if (n1 < 0)
4759 n1 = len + n1;
4760 if (!empty1 && (n1 < 0 || n1 >= len))
4761 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004762 if (verbose)
4763 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764 return FAIL;
4765 }
4766 if (range)
4767 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004768 list_T *l;
4769 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770
4771 if (n2 < 0)
4772 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004773 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004774 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004775 if (verbose)
4776 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004777 return FAIL;
4778 }
4779 l = list_alloc();
4780 if (l == NULL)
4781 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004782 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783 n1 <= n2; ++n1)
4784 {
4785 if (list_append_tv(l, &item->li_tv) == FAIL)
4786 {
4787 list_free(l);
4788 return FAIL;
4789 }
4790 item = item->li_next;
4791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004792 clear_tv(rettv);
4793 rettv->v_type = VAR_LIST;
4794 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004795 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004796 }
4797 else
4798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004799 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004800 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004801 clear_tv(rettv);
4802 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004803 }
4804 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805
4806 case VAR_DICT:
4807 if (range)
4808 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004809 if (verbose)
4810 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004811 if (len == -1)
4812 clear_tv(&var1);
4813 return FAIL;
4814 }
4815 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004816 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004817
4818 if (len == -1)
4819 {
4820 key = get_tv_string(&var1);
4821 if (*key == NUL)
4822 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004823 if (verbose)
4824 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004825 clear_tv(&var1);
4826 return FAIL;
4827 }
4828 }
4829
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004830 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004831
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004832 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004833 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004834 if (len == -1)
4835 clear_tv(&var1);
4836 if (item == NULL)
4837 return FAIL;
4838
4839 copy_tv(&item->di_tv, &var1);
4840 clear_tv(rettv);
4841 *rettv = var1;
4842 }
4843 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004844 }
4845 }
4846
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004847 return OK;
4848}
4849
4850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 * Get an option value.
4852 * "arg" points to the '&' or '+' before the option name.
4853 * "arg" is advanced to character after the option name.
4854 * Return OK or FAIL.
4855 */
4856 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004857get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004859 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 int evaluate;
4861{
4862 char_u *option_end;
4863 long numval;
4864 char_u *stringval;
4865 int opt_type;
4866 int c;
4867 int working = (**arg == '+'); /* has("+option") */
4868 int ret = OK;
4869 int opt_flags;
4870
4871 /*
4872 * Isolate the option name and find its value.
4873 */
4874 option_end = find_option_end(arg, &opt_flags);
4875 if (option_end == NULL)
4876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004877 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 EMSG2(_("E112: Option name missing: %s"), *arg);
4879 return FAIL;
4880 }
4881
4882 if (!evaluate)
4883 {
4884 *arg = option_end;
4885 return OK;
4886 }
4887
4888 c = *option_end;
4889 *option_end = NUL;
4890 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004891 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892
4893 if (opt_type == -3) /* invalid name */
4894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 EMSG2(_("E113: Unknown option: %s"), *arg);
4897 ret = FAIL;
4898 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004899 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 {
4901 if (opt_type == -2) /* hidden string option */
4902 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004903 rettv->v_type = VAR_STRING;
4904 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906 else if (opt_type == -1) /* hidden number option */
4907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004908 rettv->v_type = VAR_NUMBER;
4909 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
4911 else if (opt_type == 1) /* number option */
4912 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 rettv->v_type = VAR_NUMBER;
4914 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
4916 else /* string option */
4917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004918 rettv->v_type = VAR_STRING;
4919 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 }
4921 }
4922 else if (working && (opt_type == -2 || opt_type == -1))
4923 ret = FAIL;
4924
4925 *option_end = c; /* put back for error messages */
4926 *arg = option_end;
4927
4928 return ret;
4929}
4930
4931/*
4932 * Allocate a variable for a string constant.
4933 * Return OK or FAIL.
4934 */
4935 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004936get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 int evaluate;
4940{
4941 char_u *p;
4942 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 int extra = 0;
4944
4945 /*
4946 * Find the end of the string, skipping backslashed characters.
4947 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004948 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 {
4950 if (*p == '\\' && p[1] != NUL)
4951 {
4952 ++p;
4953 /* A "\<x>" form occupies at least 4 characters, and produces up
4954 * to 6 characters: reserve space for 2 extra */
4955 if (*p == '<')
4956 extra += 2;
4957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 }
4959
4960 if (*p != '"')
4961 {
4962 EMSG2(_("E114: Missing quote: %s"), *arg);
4963 return FAIL;
4964 }
4965
4966 /* If only parsing, set *arg and return here */
4967 if (!evaluate)
4968 {
4969 *arg = p + 1;
4970 return OK;
4971 }
4972
4973 /*
4974 * Copy the string into allocated memory, handling backslashed
4975 * characters.
4976 */
4977 name = alloc((unsigned)(p - *arg + extra));
4978 if (name == NULL)
4979 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980 rettv->v_type = VAR_STRING;
4981 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 {
4985 if (*p == '\\')
4986 {
4987 switch (*++p)
4988 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989 case 'b': *name++ = BS; ++p; break;
4990 case 'e': *name++ = ESC; ++p; break;
4991 case 'f': *name++ = FF; ++p; break;
4992 case 'n': *name++ = NL; ++p; break;
4993 case 'r': *name++ = CAR; ++p; break;
4994 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
4996 case 'X': /* hex: "\x1", "\x12" */
4997 case 'x':
4998 case 'u': /* Unicode: "\u0023" */
4999 case 'U':
5000 if (vim_isxdigit(p[1]))
5001 {
5002 int n, nr;
5003 int c = toupper(*p);
5004
5005 if (c == 'X')
5006 n = 2;
5007 else
5008 n = 4;
5009 nr = 0;
5010 while (--n >= 0 && vim_isxdigit(p[1]))
5011 {
5012 ++p;
5013 nr = (nr << 4) + hex2nr(*p);
5014 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005015 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016#ifdef FEAT_MBYTE
5017 /* For "\u" store the number according to
5018 * 'encoding'. */
5019 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 else
5022#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005023 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 break;
5026
5027 /* octal: "\1", "\12", "\123" */
5028 case '0':
5029 case '1':
5030 case '2':
5031 case '3':
5032 case '4':
5033 case '5':
5034 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 case '7': *name = *p++ - '0';
5036 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 *name = (*name << 3) + *p++ - '0';
5039 if (*p >= '0' && *p <= '7')
5040 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 break;
5044
5045 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (extra != 0)
5048 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005049 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 break;
5051 }
5052 /* FALLTHROUGH */
5053
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 break;
5056 }
5057 }
5058 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 *arg = p + 1;
5064
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 return OK;
5066}
5067
5068/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 * Return OK or FAIL.
5071 */
5072 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 int evaluate;
5077{
5078 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005079 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005080 int reduce = 0;
5081
5082 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005084 */
5085 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5086 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005088 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005089 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005090 break;
5091 ++reduce;
5092 ++p;
5093 }
5094 }
5095
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005097 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099 return FAIL;
5100 }
5101
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005103 if (!evaluate)
5104 {
5105 *arg = p + 1;
5106 return OK;
5107 }
5108
5109 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005110 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005111 */
5112 str = alloc((unsigned)((p - *arg) - reduce));
5113 if (str == NULL)
5114 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005115 rettv->v_type = VAR_STRING;
5116 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005117
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005119 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005121 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005122 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005123 break;
5124 ++p;
5125 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005126 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005127 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005128 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005129 *arg = p + 1;
5130
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005131 return OK;
5132}
5133
5134/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005135 * Allocate a variable for a List and fill it from "*arg".
5136 * Return OK or FAIL.
5137 */
5138 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005139get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005140 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005141 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005142 int evaluate;
5143{
Bram Moolenaar33570922005-01-25 22:26:29 +00005144 list_T *l = NULL;
5145 typval_T tv;
5146 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147
5148 if (evaluate)
5149 {
5150 l = list_alloc();
5151 if (l == NULL)
5152 return FAIL;
5153 }
5154
5155 *arg = skipwhite(*arg + 1);
5156 while (**arg != ']' && **arg != NUL)
5157 {
5158 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5159 goto failret;
5160 if (evaluate)
5161 {
5162 item = listitem_alloc();
5163 if (item != NULL)
5164 {
5165 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005166 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005167 list_append(l, item);
5168 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005169 else
5170 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005171 }
5172
5173 if (**arg == ']')
5174 break;
5175 if (**arg != ',')
5176 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005178 goto failret;
5179 }
5180 *arg = skipwhite(*arg + 1);
5181 }
5182
5183 if (**arg != ']')
5184 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005186failret:
5187 if (evaluate)
5188 list_free(l);
5189 return FAIL;
5190 }
5191
5192 *arg = skipwhite(*arg + 1);
5193 if (evaluate)
5194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005195 rettv->v_type = VAR_LIST;
5196 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005197 ++l->lv_refcount;
5198 }
5199
5200 return OK;
5201}
5202
5203/*
5204 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005205 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005206 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005207 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005208list_alloc()
5209{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005210 list_T *l;
5211
5212 l = (list_T *)alloc_clear(sizeof(list_T));
5213 if (l != NULL)
5214 {
5215 /* Prepend the list to the list of lists for garbage collection. */
5216 if (first_list != NULL)
5217 first_list->lv_used_prev = l;
5218 l->lv_used_prev = NULL;
5219 l->lv_used_next = first_list;
5220 first_list = l;
5221 }
5222 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005223}
5224
5225/*
5226 * Unreference a list: decrement the reference count and free it when it
5227 * becomes zero.
5228 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005229 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005231 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005233 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5234 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005235}
5236
5237/*
5238 * Free a list, including all items it points to.
5239 * Ignores the reference count.
5240 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005241 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005243 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244{
Bram Moolenaar33570922005-01-25 22:26:29 +00005245 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246
Bram Moolenaard9fba312005-06-26 22:34:35 +00005247 /* Avoid that recursive reference to the list frees us again. */
5248 l->lv_refcount = DEL_REFCOUNT;
5249
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005250 /* Remove the list from the list of lists for garbage collection. */
5251 if (l->lv_used_prev == NULL)
5252 first_list = l->lv_used_next;
5253 else
5254 l->lv_used_prev->lv_used_next = l->lv_used_next;
5255 if (l->lv_used_next != NULL)
5256 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5257
Bram Moolenaard9fba312005-06-26 22:34:35 +00005258 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005260 /* Remove the item before deleting it. */
5261 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 listitem_free(item);
5263 }
5264 vim_free(l);
5265}
5266
5267/*
5268 * Allocate a list item.
5269 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005270 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271listitem_alloc()
5272{
Bram Moolenaar33570922005-01-25 22:26:29 +00005273 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274}
5275
5276/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005277 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 */
5279 static void
5280listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005281 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005283 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284 vim_free(item);
5285}
5286
5287/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005288 * Remove a list item from a List and free it. Also clears the value.
5289 */
5290 static void
5291listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005292 list_T *l;
5293 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005294{
5295 list_remove(l, item, item);
5296 listitem_free(item);
5297}
5298
5299/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 * Get the number of items in a list.
5301 */
5302 static long
5303list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005304 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 if (l == NULL)
5307 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005308 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309}
5310
5311/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005312 * Return TRUE when two lists have exactly the same values.
5313 */
5314 static int
5315list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005316 list_T *l1;
5317 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005318 int ic; /* ignore case for strings */
5319{
Bram Moolenaar33570922005-01-25 22:26:29 +00005320 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005321
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005322 if (list_len(l1) != list_len(l2))
5323 return FALSE;
5324
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005325 for (item1 = l1->lv_first, item2 = l2->lv_first;
5326 item1 != NULL && item2 != NULL;
5327 item1 = item1->li_next, item2 = item2->li_next)
5328 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5329 return FALSE;
5330 return item1 == NULL && item2 == NULL;
5331}
5332
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005333#if defined(FEAT_PYTHON) || defined(PROTO)
5334/*
5335 * Return the dictitem that an entry in a hashtable points to.
5336 */
5337 dictitem_T *
5338dict_lookup(hi)
5339 hashitem_T *hi;
5340{
5341 return HI2DI(hi);
5342}
5343#endif
5344
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005345/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005346 * Return TRUE when two dictionaries have exactly the same key/values.
5347 */
5348 static int
5349dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005350 dict_T *d1;
5351 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005352 int ic; /* ignore case for strings */
5353{
Bram Moolenaar33570922005-01-25 22:26:29 +00005354 hashitem_T *hi;
5355 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005356 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005357
5358 if (dict_len(d1) != dict_len(d2))
5359 return FALSE;
5360
Bram Moolenaar33570922005-01-25 22:26:29 +00005361 todo = d1->dv_hashtab.ht_used;
5362 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005363 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005364 if (!HASHITEM_EMPTY(hi))
5365 {
5366 item2 = dict_find(d2, hi->hi_key, -1);
5367 if (item2 == NULL)
5368 return FALSE;
5369 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5370 return FALSE;
5371 --todo;
5372 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005373 }
5374 return TRUE;
5375}
5376
5377/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005378 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005379 * Compares the items just like "==" would compare them, but strings and
5380 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005381 */
5382 static int
5383tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005384 typval_T *tv1;
5385 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005386 int ic; /* ignore case */
5387{
5388 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005389 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005390
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005391 if (tv1->v_type != tv2->v_type)
5392 return FALSE;
5393
5394 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005395 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005396 case VAR_LIST:
5397 /* recursive! */
5398 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5399
5400 case VAR_DICT:
5401 /* recursive! */
5402 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5403
5404 case VAR_FUNC:
5405 return (tv1->vval.v_string != NULL
5406 && tv2->vval.v_string != NULL
5407 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5408
5409 case VAR_NUMBER:
5410 return tv1->vval.v_number == tv2->vval.v_number;
5411
5412 case VAR_STRING:
5413 s1 = get_tv_string_buf(tv1, buf1);
5414 s2 = get_tv_string_buf(tv2, buf2);
5415 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005416 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005417
5418 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005419 return TRUE;
5420}
5421
5422/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423 * Locate item with index "n" in list "l" and return it.
5424 * A negative index is counted from the end; -1 is the last item.
5425 * Returns NULL when "n" is out of range.
5426 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005427 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005429 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 long n;
5431{
Bram Moolenaar33570922005-01-25 22:26:29 +00005432 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 long idx;
5434
5435 if (l == NULL)
5436 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005437
5438 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005440 n = l->lv_len + n;
5441
5442 /* Check for index out of range. */
5443 if (n < 0 || n >= l->lv_len)
5444 return NULL;
5445
5446 /* When there is a cached index may start search from there. */
5447 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005449 if (n < l->lv_idx / 2)
5450 {
5451 /* closest to the start of the list */
5452 item = l->lv_first;
5453 idx = 0;
5454 }
5455 else if (n > (l->lv_idx + l->lv_len) / 2)
5456 {
5457 /* closest to the end of the list */
5458 item = l->lv_last;
5459 idx = l->lv_len - 1;
5460 }
5461 else
5462 {
5463 /* closest to the cached index */
5464 item = l->lv_idx_item;
5465 idx = l->lv_idx;
5466 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 }
5468 else
5469 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005470 if (n < l->lv_len / 2)
5471 {
5472 /* closest to the start of the list */
5473 item = l->lv_first;
5474 idx = 0;
5475 }
5476 else
5477 {
5478 /* closest to the end of the list */
5479 item = l->lv_last;
5480 idx = l->lv_len - 1;
5481 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005483
5484 while (n > idx)
5485 {
5486 /* search forward */
5487 item = item->li_next;
5488 ++idx;
5489 }
5490 while (n < idx)
5491 {
5492 /* search backward */
5493 item = item->li_prev;
5494 --idx;
5495 }
5496
5497 /* cache the used index */
5498 l->lv_idx = idx;
5499 l->lv_idx_item = item;
5500
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 return item;
5502}
5503
5504/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005505 * Locate "item" list "l" and return its index.
5506 * Returns -1 when "item" is not in the list.
5507 */
5508 static long
5509list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005510 list_T *l;
5511 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005512{
5513 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005514 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005515
5516 if (l == NULL)
5517 return -1;
5518 idx = 0;
5519 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5520 ++idx;
5521 if (li == NULL)
5522 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005523 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005524}
5525
5526/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 * Append item "item" to the end of list "l".
5528 */
5529 static void
5530list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005531 list_T *l;
5532 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533{
5534 if (l->lv_last == NULL)
5535 {
5536 /* empty list */
5537 l->lv_first = item;
5538 l->lv_last = item;
5539 item->li_prev = NULL;
5540 }
5541 else
5542 {
5543 l->lv_last->li_next = item;
5544 item->li_prev = l->lv_last;
5545 l->lv_last = item;
5546 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005547 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 item->li_next = NULL;
5549}
5550
5551/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005552 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005553 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 */
5555 static int
5556list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005557 list_T *l;
5558 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005560 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561
Bram Moolenaar05159a02005-02-26 23:04:13 +00005562 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005564 copy_tv(tv, &li->li_tv);
5565 list_append(l, li);
5566 return OK;
5567}
5568
5569/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005570 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005571 * Return FAIL when out of memory.
5572 */
5573 int
5574list_append_dict(list, dict)
5575 list_T *list;
5576 dict_T *dict;
5577{
5578 listitem_T *li = listitem_alloc();
5579
5580 if (li == NULL)
5581 return FAIL;
5582 li->li_tv.v_type = VAR_DICT;
5583 li->li_tv.v_lock = 0;
5584 li->li_tv.vval.v_dict = dict;
5585 list_append(list, li);
5586 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005587 return OK;
5588}
5589
5590/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005591 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005592 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005593 * Returns FAIL when out of memory.
5594 */
5595 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005596list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005597 list_T *l;
5598 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005599 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005600{
5601 listitem_T *li = listitem_alloc();
5602
5603 if (li == NULL)
5604 return FAIL;
5605 list_append(l, li);
5606 li->li_tv.v_type = VAR_STRING;
5607 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005608 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5609 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005610 return FAIL;
5611 return OK;
5612}
5613
5614/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005615 * Append "n" to list "l".
5616 * Returns FAIL when out of memory.
5617 */
5618 static int
5619list_append_number(l, n)
5620 list_T *l;
5621 varnumber_T n;
5622{
5623 listitem_T *li;
5624
5625 li = listitem_alloc();
5626 if (li == NULL)
5627 return FAIL;
5628 li->li_tv.v_type = VAR_NUMBER;
5629 li->li_tv.v_lock = 0;
5630 li->li_tv.vval.v_number = n;
5631 list_append(l, li);
5632 return OK;
5633}
5634
5635/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005636 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005637 * If "item" is NULL append at the end.
5638 * Return FAIL when out of memory.
5639 */
5640 static int
5641list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005642 list_T *l;
5643 typval_T *tv;
5644 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005645{
Bram Moolenaar33570922005-01-25 22:26:29 +00005646 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005647
5648 if (ni == NULL)
5649 return FAIL;
5650 copy_tv(tv, &ni->li_tv);
5651 if (item == NULL)
5652 /* Append new item at end of list. */
5653 list_append(l, ni);
5654 else
5655 {
5656 /* Insert new item before existing item. */
5657 ni->li_prev = item->li_prev;
5658 ni->li_next = item;
5659 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005660 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005661 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005662 ++l->lv_idx;
5663 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005664 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005665 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005666 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005667 l->lv_idx_item = NULL;
5668 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005669 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005670 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005671 }
5672 return OK;
5673}
5674
5675/*
5676 * Extend "l1" with "l2".
5677 * If "bef" is NULL append at the end, otherwise insert before this item.
5678 * Returns FAIL when out of memory.
5679 */
5680 static int
5681list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005682 list_T *l1;
5683 list_T *l2;
5684 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005685{
Bram Moolenaar33570922005-01-25 22:26:29 +00005686 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005687
5688 for (item = l2->lv_first; item != NULL; item = item->li_next)
5689 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5690 return FAIL;
5691 return OK;
5692}
5693
5694/*
5695 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5696 * Return FAIL when out of memory.
5697 */
5698 static int
5699list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005700 list_T *l1;
5701 list_T *l2;
5702 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005703{
Bram Moolenaar33570922005-01-25 22:26:29 +00005704 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005705
5706 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005707 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005708 if (l == NULL)
5709 return FAIL;
5710 tv->v_type = VAR_LIST;
5711 tv->vval.v_list = l;
5712
5713 /* append all items from the second list */
5714 return list_extend(l, l2, NULL);
5715}
5716
5717/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005718 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005720 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721 * Returns NULL when out of memory.
5722 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005723 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005724list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005725 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005727 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728{
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 list_T *copy;
5730 listitem_T *item;
5731 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732
5733 if (orig == NULL)
5734 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005735
5736 copy = list_alloc();
5737 if (copy != NULL)
5738 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005739 if (copyID != 0)
5740 {
5741 /* Do this before adding the items, because one of the items may
5742 * refer back to this list. */
5743 orig->lv_copyID = copyID;
5744 orig->lv_copylist = copy;
5745 }
5746 for (item = orig->lv_first; item != NULL && !got_int;
5747 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005748 {
5749 ni = listitem_alloc();
5750 if (ni == NULL)
5751 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005752 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005753 {
5754 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5755 {
5756 vim_free(ni);
5757 break;
5758 }
5759 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005761 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005762 list_append(copy, ni);
5763 }
5764 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005765 if (item != NULL)
5766 {
5767 list_unref(copy);
5768 copy = NULL;
5769 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005770 }
5771
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005772 return copy;
5773}
5774
5775/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005776 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005777 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005779 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005780list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005781 list_T *l;
5782 listitem_T *item;
5783 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005784{
Bram Moolenaar33570922005-01-25 22:26:29 +00005785 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005786
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005787 /* notify watchers */
5788 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005789 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005790 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005791 list_fix_watch(l, ip);
5792 if (ip == item2)
5793 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005794 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005795
5796 if (item2->li_next == NULL)
5797 l->lv_last = item->li_prev;
5798 else
5799 item2->li_next->li_prev = item->li_prev;
5800 if (item->li_prev == NULL)
5801 l->lv_first = item2->li_next;
5802 else
5803 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005804 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805}
5806
5807/*
5808 * Return an allocated string with the string representation of a list.
5809 * May return NULL.
5810 */
5811 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005812list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005813 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005814 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005815{
5816 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817
5818 if (tv->vval.v_list == NULL)
5819 return NULL;
5820 ga_init2(&ga, (int)sizeof(char), 80);
5821 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005822 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005823 {
5824 vim_free(ga.ga_data);
5825 return NULL;
5826 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005827 ga_append(&ga, ']');
5828 ga_append(&ga, NUL);
5829 return (char_u *)ga.ga_data;
5830}
5831
5832/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 * Join list "l" into a string in "*gap", using separator "sep".
5834 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005835 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005836 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005837 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005838list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005840 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005841 char_u *sep;
5842 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005843 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844{
5845 int first = TRUE;
5846 char_u *tofree;
5847 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005848 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 char_u *s;
5850
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005851 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 {
5853 if (first)
5854 first = FALSE;
5855 else
5856 ga_concat(gap, sep);
5857
5858 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005859 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005861 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 if (s != NULL)
5863 ga_concat(gap, s);
5864 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005865 if (s == NULL)
5866 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005868 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869}
5870
5871/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005872 * Garbage collection for lists and dictionaries.
5873 *
5874 * We use reference counts to be able to free most items right away when they
5875 * are no longer used. But for composite items it's possible that it becomes
5876 * unused while the reference count is > 0: When there is a recursive
5877 * reference. Example:
5878 * :let l = [1, 2, 3]
5879 * :let d = {9: l}
5880 * :let l[1] = d
5881 *
5882 * Since this is quite unusual we handle this with garbage collection: every
5883 * once in a while find out which lists and dicts are not referenced from any
5884 * variable.
5885 *
5886 * Here is a good reference text about garbage collection (refers to Python
5887 * but it applies to all reference-counting mechanisms):
5888 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005889 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005890
5891/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005892 * Do garbage collection for lists and dicts.
5893 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005894 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005895 int
5896garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005897{
5898 dict_T *dd;
5899 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005900 int copyID = ++current_copyID;
5901 buf_T *buf;
5902 win_T *wp;
5903 int i;
5904 funccall_T *fc;
5905 int did_free = FALSE;
5906
5907 /*
5908 * 1. Go through all accessible variables and mark all lists and dicts
5909 * with copyID.
5910 */
5911 /* script-local variables */
5912 for (i = 1; i <= ga_scripts.ga_len; ++i)
5913 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5914
5915 /* buffer-local variables */
5916 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5917 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5918
5919 /* window-local variables */
5920 FOR_ALL_WINDOWS(wp)
5921 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5922
5923 /* global variables */
5924 set_ref_in_ht(&globvarht, copyID);
5925
5926 /* function-local variables */
5927 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5928 {
5929 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5930 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5931 }
5932
5933 /*
5934 * 2. Go through the list of dicts and free items without the copyID.
5935 */
5936 for (dd = first_dict; dd != NULL; )
5937 if (dd->dv_copyID != copyID)
5938 {
5939 dict_free(dd);
5940 did_free = TRUE;
5941
5942 /* restart, next dict may also have been freed */
5943 dd = first_dict;
5944 }
5945 else
5946 dd = dd->dv_used_next;
5947
5948 /*
5949 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005950 * But don't free a list that has a watcher (used in a for loop), these
5951 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005952 */
5953 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005954 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005955 {
5956 list_free(ll);
5957 did_free = TRUE;
5958
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005959 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005960 ll = first_list;
5961 }
5962 else
5963 ll = ll->lv_used_next;
5964
5965 return did_free;
5966}
5967
5968/*
5969 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5970 */
5971 static void
5972set_ref_in_ht(ht, copyID)
5973 hashtab_T *ht;
5974 int copyID;
5975{
5976 int todo;
5977 hashitem_T *hi;
5978
5979 todo = ht->ht_used;
5980 for (hi = ht->ht_array; todo > 0; ++hi)
5981 if (!HASHITEM_EMPTY(hi))
5982 {
5983 --todo;
5984 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5985 }
5986}
5987
5988/*
5989 * Mark all lists and dicts referenced through list "l" with "copyID".
5990 */
5991 static void
5992set_ref_in_list(l, copyID)
5993 list_T *l;
5994 int copyID;
5995{
5996 listitem_T *li;
5997
5998 for (li = l->lv_first; li != NULL; li = li->li_next)
5999 set_ref_in_item(&li->li_tv, copyID);
6000}
6001
6002/*
6003 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6004 */
6005 static void
6006set_ref_in_item(tv, copyID)
6007 typval_T *tv;
6008 int copyID;
6009{
6010 dict_T *dd;
6011 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006012
6013 switch (tv->v_type)
6014 {
6015 case VAR_DICT:
6016 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006017 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006018 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006019 /* Didn't see this dict yet. */
6020 dd->dv_copyID = copyID;
6021 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006022 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006023 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006024
6025 case VAR_LIST:
6026 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006027 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006028 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006029 /* Didn't see this list yet. */
6030 ll->lv_copyID = copyID;
6031 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006032 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006033 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006034 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006035 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006036}
6037
6038/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006039 * Allocate an empty header for a dictionary.
6040 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006041 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006042dict_alloc()
6043{
Bram Moolenaar33570922005-01-25 22:26:29 +00006044 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006045
Bram Moolenaar33570922005-01-25 22:26:29 +00006046 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006047 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006048 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006049 /* Add the list to the hashtable for garbage collection. */
6050 if (first_dict != NULL)
6051 first_dict->dv_used_prev = d;
6052 d->dv_used_next = first_dict;
6053 d->dv_used_prev = NULL;
6054
Bram Moolenaar33570922005-01-25 22:26:29 +00006055 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006056 d->dv_lock = 0;
6057 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006058 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006059 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006060 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006061}
6062
6063/*
6064 * Unreference a Dictionary: decrement the reference count and free it when it
6065 * becomes zero.
6066 */
6067 static void
6068dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006070{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006071 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6072 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006073}
6074
6075/*
6076 * Free a Dictionary, including all items it contains.
6077 * Ignores the reference count.
6078 */
6079 static void
6080dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006082{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006083 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006084 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006085 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006086
Bram Moolenaard9fba312005-06-26 22:34:35 +00006087 /* Avoid that recursive reference to the dict frees us again. */
6088 d->dv_refcount = DEL_REFCOUNT;
6089
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006090 /* Remove the dict from the list of dicts for garbage collection. */
6091 if (d->dv_used_prev == NULL)
6092 first_dict = d->dv_used_next;
6093 else
6094 d->dv_used_prev->dv_used_next = d->dv_used_next;
6095 if (d->dv_used_next != NULL)
6096 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6097
6098 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006099 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006100 todo = d->dv_hashtab.ht_used;
6101 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006102 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006103 if (!HASHITEM_EMPTY(hi))
6104 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006105 /* Remove the item before deleting it, just in case there is
6106 * something recursive causing trouble. */
6107 di = HI2DI(hi);
6108 hash_remove(&d->dv_hashtab, hi);
6109 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006110 --todo;
6111 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006112 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006113 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006114 vim_free(d);
6115}
6116
6117/*
6118 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006119 * The "key" is copied to the new item.
6120 * Note that the value of the item "di_tv" still needs to be initialized!
6121 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006122 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006123 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006124dictitem_alloc(key)
6125 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006126{
Bram Moolenaar33570922005-01-25 22:26:29 +00006127 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006128
Bram Moolenaar33570922005-01-25 22:26:29 +00006129 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006130 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006131 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006132 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006133 di->di_flags = 0;
6134 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006135 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006136}
6137
6138/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006139 * Make a copy of a Dictionary item.
6140 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006142dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006144{
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006146
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006148 if (di != NULL)
6149 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006150 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006151 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006152 copy_tv(&org->di_tv, &di->di_tv);
6153 }
6154 return di;
6155}
6156
6157/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006158 * Remove item "item" from Dictionary "dict" and free it.
6159 */
6160 static void
6161dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006162 dict_T *dict;
6163 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006164{
Bram Moolenaar33570922005-01-25 22:26:29 +00006165 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006166
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006168 if (HASHITEM_EMPTY(hi))
6169 EMSG2(_(e_intern2), "dictitem_remove()");
6170 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006171 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006172 dictitem_free(item);
6173}
6174
6175/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006176 * Free a dict item. Also clears the value.
6177 */
6178 static void
6179dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006180 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006181{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006182 clear_tv(&item->di_tv);
6183 vim_free(item);
6184}
6185
6186/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006187 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6188 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006189 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006190 * Returns NULL when out of memory.
6191 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006193dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006194 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006195 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006196 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006197{
Bram Moolenaar33570922005-01-25 22:26:29 +00006198 dict_T *copy;
6199 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006200 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006201 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006202
6203 if (orig == NULL)
6204 return NULL;
6205
6206 copy = dict_alloc();
6207 if (copy != NULL)
6208 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006209 if (copyID != 0)
6210 {
6211 orig->dv_copyID = copyID;
6212 orig->dv_copydict = copy;
6213 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006214 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006215 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006216 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006217 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006218 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006219 --todo;
6220
6221 di = dictitem_alloc(hi->hi_key);
6222 if (di == NULL)
6223 break;
6224 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006225 {
6226 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6227 copyID) == FAIL)
6228 {
6229 vim_free(di);
6230 break;
6231 }
6232 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006233 else
6234 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6235 if (dict_add(copy, di) == FAIL)
6236 {
6237 dictitem_free(di);
6238 break;
6239 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006240 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006241 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006242
Bram Moolenaare9a41262005-01-15 22:18:47 +00006243 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006244 if (todo > 0)
6245 {
6246 dict_unref(copy);
6247 copy = NULL;
6248 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006249 }
6250
6251 return copy;
6252}
6253
6254/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006255 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006256 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006257 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006258 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006259dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 dict_T *d;
6261 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006262{
Bram Moolenaar33570922005-01-25 22:26:29 +00006263 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006264}
6265
Bram Moolenaar8c711452005-01-14 21:53:12 +00006266/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006267 * Add a number or string entry to dictionary "d".
6268 * When "str" is NULL use number "nr", otherwise use "str".
6269 * Returns FAIL when out of memory and when key already exists.
6270 */
6271 int
6272dict_add_nr_str(d, key, nr, str)
6273 dict_T *d;
6274 char *key;
6275 long nr;
6276 char_u *str;
6277{
6278 dictitem_T *item;
6279
6280 item = dictitem_alloc((char_u *)key);
6281 if (item == NULL)
6282 return FAIL;
6283 item->di_tv.v_lock = 0;
6284 if (str == NULL)
6285 {
6286 item->di_tv.v_type = VAR_NUMBER;
6287 item->di_tv.vval.v_number = nr;
6288 }
6289 else
6290 {
6291 item->di_tv.v_type = VAR_STRING;
6292 item->di_tv.vval.v_string = vim_strsave(str);
6293 }
6294 if (dict_add(d, item) == FAIL)
6295 {
6296 dictitem_free(item);
6297 return FAIL;
6298 }
6299 return OK;
6300}
6301
6302/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006303 * Get the number of items in a Dictionary.
6304 */
6305 static long
6306dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006307 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006308{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006309 if (d == NULL)
6310 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006312}
6313
6314/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006315 * Find item "key[len]" in Dictionary "d".
6316 * If "len" is negative use strlen(key).
6317 * Returns NULL when not found.
6318 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006319 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006320dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006321 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006322 char_u *key;
6323 int len;
6324{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006325#define AKEYLEN 200
6326 char_u buf[AKEYLEN];
6327 char_u *akey;
6328 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006329 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006330
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006331 if (len < 0)
6332 akey = key;
6333 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006334 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006335 tofree = akey = vim_strnsave(key, len);
6336 if (akey == NULL)
6337 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006338 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006339 else
6340 {
6341 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006342 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006343 akey = buf;
6344 }
6345
Bram Moolenaar33570922005-01-25 22:26:29 +00006346 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006347 vim_free(tofree);
6348 if (HASHITEM_EMPTY(hi))
6349 return NULL;
6350 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006351}
6352
6353/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006354 * Get a string item from a dictionary.
6355 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006356 * Returns NULL if the entry doesn't exist or out of memory.
6357 */
6358 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006359get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006360 dict_T *d;
6361 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006362 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006363{
6364 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006365 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006366
6367 di = dict_find(d, key, -1);
6368 if (di == NULL)
6369 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006370 s = get_tv_string(&di->di_tv);
6371 if (save && s != NULL)
6372 s = vim_strsave(s);
6373 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006374}
6375
6376/*
6377 * Get a number item from a dictionary.
6378 * Returns 0 if the entry doesn't exist or out of memory.
6379 */
6380 long
6381get_dict_number(d, key)
6382 dict_T *d;
6383 char_u *key;
6384{
6385 dictitem_T *di;
6386
6387 di = dict_find(d, key, -1);
6388 if (di == NULL)
6389 return 0;
6390 return get_tv_number(&di->di_tv);
6391}
6392
6393/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006394 * Return an allocated string with the string representation of a Dictionary.
6395 * May return NULL.
6396 */
6397 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006398dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006399 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006400 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006401{
6402 garray_T ga;
6403 int first = TRUE;
6404 char_u *tofree;
6405 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006407 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006409 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006410
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006411 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006412 return NULL;
6413 ga_init2(&ga, (int)sizeof(char), 80);
6414 ga_append(&ga, '{');
6415
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006417 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006418 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006419 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006420 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006421 --todo;
6422
6423 if (first)
6424 first = FALSE;
6425 else
6426 ga_concat(&ga, (char_u *)", ");
6427
6428 tofree = string_quote(hi->hi_key, FALSE);
6429 if (tofree != NULL)
6430 {
6431 ga_concat(&ga, tofree);
6432 vim_free(tofree);
6433 }
6434 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006435 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006436 if (s != NULL)
6437 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006438 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006439 if (s == NULL)
6440 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006441 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006442 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006443 if (todo > 0)
6444 {
6445 vim_free(ga.ga_data);
6446 return NULL;
6447 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006448
6449 ga_append(&ga, '}');
6450 ga_append(&ga, NUL);
6451 return (char_u *)ga.ga_data;
6452}
6453
6454/*
6455 * Allocate a variable for a Dictionary and fill it from "*arg".
6456 * Return OK or FAIL. Returns NOTDONE for {expr}.
6457 */
6458 static int
6459get_dict_tv(arg, rettv, evaluate)
6460 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006462 int evaluate;
6463{
Bram Moolenaar33570922005-01-25 22:26:29 +00006464 dict_T *d = NULL;
6465 typval_T tvkey;
6466 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006467 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006469 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006470 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006471
6472 /*
6473 * First check if it's not a curly-braces thing: {expr}.
6474 * Must do this without evaluating, otherwise a function may be called
6475 * twice. Unfortunately this means we need to call eval1() twice for the
6476 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006477 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006478 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006479 if (*start != '}')
6480 {
6481 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6482 return FAIL;
6483 if (*start == '}')
6484 return NOTDONE;
6485 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006486
6487 if (evaluate)
6488 {
6489 d = dict_alloc();
6490 if (d == NULL)
6491 return FAIL;
6492 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006493 tvkey.v_type = VAR_UNKNOWN;
6494 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006495
6496 *arg = skipwhite(*arg + 1);
6497 while (**arg != '}' && **arg != NUL)
6498 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006499 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006500 goto failret;
6501 if (**arg != ':')
6502 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006503 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006504 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006505 goto failret;
6506 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006507 key = get_tv_string_buf_chk(&tvkey, buf);
6508 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006510 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6511 if (key != NULL)
6512 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006513 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006514 goto failret;
6515 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006516
6517 *arg = skipwhite(*arg + 1);
6518 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6519 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006520 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006521 goto failret;
6522 }
6523 if (evaluate)
6524 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006525 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006526 if (item != NULL)
6527 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006528 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006529 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006530 clear_tv(&tv);
6531 goto failret;
6532 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006533 item = dictitem_alloc(key);
6534 clear_tv(&tvkey);
6535 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006536 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006537 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006538 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006539 if (dict_add(d, item) == FAIL)
6540 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006541 }
6542 }
6543
6544 if (**arg == '}')
6545 break;
6546 if (**arg != ',')
6547 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006548 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006549 goto failret;
6550 }
6551 *arg = skipwhite(*arg + 1);
6552 }
6553
6554 if (**arg != '}')
6555 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006556 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006557failret:
6558 if (evaluate)
6559 dict_free(d);
6560 return FAIL;
6561 }
6562
6563 *arg = skipwhite(*arg + 1);
6564 if (evaluate)
6565 {
6566 rettv->v_type = VAR_DICT;
6567 rettv->vval.v_dict = d;
6568 ++d->dv_refcount;
6569 }
6570
6571 return OK;
6572}
6573
Bram Moolenaar8c711452005-01-14 21:53:12 +00006574/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575 * Return a string with the string representation of a variable.
6576 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006577 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006578 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006579 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580 * May return NULL;
6581 */
6582 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006583echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006587 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006589 static int recurse = 0;
6590 char_u *r = NULL;
6591
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006593 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006594 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006595 *tofree = NULL;
6596 return NULL;
6597 }
6598 ++recurse;
6599
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600 switch (tv->v_type)
6601 {
6602 case VAR_FUNC:
6603 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006604 r = tv->vval.v_string;
6605 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006606
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006608 if (tv->vval.v_list == NULL)
6609 {
6610 *tofree = NULL;
6611 r = NULL;
6612 }
6613 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6614 {
6615 *tofree = NULL;
6616 r = (char_u *)"[...]";
6617 }
6618 else
6619 {
6620 tv->vval.v_list->lv_copyID = copyID;
6621 *tofree = list2string(tv, copyID);
6622 r = *tofree;
6623 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006624 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006625
Bram Moolenaar8c711452005-01-14 21:53:12 +00006626 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006627 if (tv->vval.v_dict == NULL)
6628 {
6629 *tofree = NULL;
6630 r = NULL;
6631 }
6632 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6633 {
6634 *tofree = NULL;
6635 r = (char_u *)"{...}";
6636 }
6637 else
6638 {
6639 tv->vval.v_dict->dv_copyID = copyID;
6640 *tofree = dict2string(tv, copyID);
6641 r = *tofree;
6642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006643 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006644
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006645 case VAR_STRING:
6646 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006647 *tofree = NULL;
6648 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006650
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006651 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006652 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006653 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006654 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006655
6656 --recurse;
6657 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006658}
6659
6660/*
6661 * Return a string with the string representation of a variable.
6662 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6663 * "numbuf" is used for a number.
6664 * Puts quotes around strings, so that they can be parsed back by eval().
6665 * May return NULL;
6666 */
6667 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006668tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006669 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006670 char_u **tofree;
6671 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006672 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006673{
6674 switch (tv->v_type)
6675 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006676 case VAR_FUNC:
6677 *tofree = string_quote(tv->vval.v_string, TRUE);
6678 return *tofree;
6679 case VAR_STRING:
6680 *tofree = string_quote(tv->vval.v_string, FALSE);
6681 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006682 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006683 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006684 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006685 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006686 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006687 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006688 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006689 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006690}
6691
6692/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006693 * Return string "str" in ' quotes, doubling ' characters.
6694 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006695 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006696 */
6697 static char_u *
6698string_quote(str, function)
6699 char_u *str;
6700 int function;
6701{
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703 char_u *p, *r, *s;
6704
Bram Moolenaar33570922005-01-25 22:26:29 +00006705 len = (function ? 13 : 3);
6706 if (str != NULL)
6707 {
6708 len += STRLEN(str);
6709 for (p = str; *p != NUL; mb_ptr_adv(p))
6710 if (*p == '\'')
6711 ++len;
6712 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006713 s = r = alloc(len);
6714 if (r != NULL)
6715 {
6716 if (function)
6717 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006718 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006719 r += 10;
6720 }
6721 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006722 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006723 if (str != NULL)
6724 for (p = str; *p != NUL; )
6725 {
6726 if (*p == '\'')
6727 *r++ = '\'';
6728 MB_COPY_CHAR(p, r);
6729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006730 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006731 if (function)
6732 *r++ = ')';
6733 *r++ = NUL;
6734 }
6735 return s;
6736}
6737
6738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 * Get the value of an environment variable.
6740 * "arg" is pointing to the '$'. It is advanced to after the name.
6741 * If the environment variable was not set, silently assume it is empty.
6742 * Always return OK.
6743 */
6744 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006745get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 int evaluate;
6749{
6750 char_u *string = NULL;
6751 int len;
6752 int cc;
6753 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006754 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
6756 ++*arg;
6757 name = *arg;
6758 len = get_env_len(arg);
6759 if (evaluate)
6760 {
6761 if (len != 0)
6762 {
6763 cc = name[len];
6764 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006765 /* first try vim_getenv(), fast for normal environment vars */
6766 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006768 {
6769 if (!mustfree)
6770 string = vim_strsave(string);
6771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 else
6773 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006774 if (mustfree)
6775 vim_free(string);
6776
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 /* next try expanding things like $VIM and ${HOME} */
6778 string = expand_env_save(name - 1);
6779 if (string != NULL && *string == '$')
6780 {
6781 vim_free(string);
6782 string = NULL;
6783 }
6784 }
6785 name[len] = cc;
6786 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006787 rettv->v_type = VAR_STRING;
6788 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 }
6790
6791 return OK;
6792}
6793
6794/*
6795 * Array with names and number of arguments of all internal functions
6796 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6797 */
6798static struct fst
6799{
6800 char *f_name; /* function name */
6801 char f_min_argc; /* minimal number of arguments */
6802 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006803 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006804 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805} functions[] =
6806{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006807 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {"append", 2, 2, f_append},
6809 {"argc", 0, 0, f_argc},
6810 {"argidx", 0, 0, f_argidx},
6811 {"argv", 1, 1, f_argv},
6812 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006813 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {"bufexists", 1, 1, f_bufexists},
6815 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6816 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6817 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6818 {"buflisted", 1, 1, f_buflisted},
6819 {"bufloaded", 1, 1, f_bufloaded},
6820 {"bufname", 1, 1, f_bufname},
6821 {"bufnr", 1, 1, f_bufnr},
6822 {"bufwinnr", 1, 1, f_bufwinnr},
6823 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006824 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006825 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 {"char2nr", 1, 1, f_char2nr},
6827 {"cindent", 1, 1, f_cindent},
6828 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006829#if defined(FEAT_INS_EXPAND)
6830 {"complete_add", 1, 1, f_complete_add},
6831 {"complete_check", 0, 0, f_complete_check},
6832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006834 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006835 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 {"cscope_connection",0,3, f_cscope_connection},
6837 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006838 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 {"delete", 1, 1, f_delete},
6840 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006841 {"diff_filler", 1, 1, f_diff_filler},
6842 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006843 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006845 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 {"eventhandler", 0, 0, f_eventhandler},
6847 {"executable", 1, 1, f_executable},
6848 {"exists", 1, 1, f_exists},
6849 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006850 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6852 {"filereadable", 1, 1, f_filereadable},
6853 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006854 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006855 {"finddir", 1, 3, f_finddir},
6856 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 {"fnamemodify", 2, 2, f_fnamemodify},
6858 {"foldclosed", 1, 1, f_foldclosed},
6859 {"foldclosedend", 1, 1, f_foldclosedend},
6860 {"foldlevel", 1, 1, f_foldlevel},
6861 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006862 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006864 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006866 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006867 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 {"getbufvar", 2, 2, f_getbufvar},
6869 {"getchar", 0, 1, f_getchar},
6870 {"getcharmod", 0, 0, f_getcharmod},
6871 {"getcmdline", 0, 0, f_getcmdline},
6872 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006873 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006875 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006876 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {"getfsize", 1, 1, f_getfsize},
6878 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006879 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006880 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006881 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006882 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006883 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {"getregtype", 0, 1, f_getregtype},
6885 {"getwinposx", 0, 0, f_getwinposx},
6886 {"getwinposy", 0, 0, f_getwinposy},
6887 {"getwinvar", 2, 2, f_getwinvar},
6888 {"glob", 1, 1, f_glob},
6889 {"globpath", 2, 2, f_globpath},
6890 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006891 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 {"hasmapto", 1, 2, f_hasmapto},
6893 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6894 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6895 {"histadd", 2, 2, f_histadd},
6896 {"histdel", 1, 2, f_histdel},
6897 {"histget", 1, 2, f_histget},
6898 {"histnr", 1, 1, f_histnr},
6899 {"hlID", 1, 1, f_hlID},
6900 {"hlexists", 1, 1, f_hlexists},
6901 {"hostname", 0, 0, f_hostname},
6902 {"iconv", 3, 3, f_iconv},
6903 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006904 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006905 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006907 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {"inputrestore", 0, 0, f_inputrestore},
6909 {"inputsave", 0, 0, f_inputsave},
6910 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006911 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006913 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006914 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006915 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006916 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006918 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 {"libcall", 3, 3, f_libcall},
6920 {"libcallnr", 3, 3, f_libcallnr},
6921 {"line", 1, 1, f_line},
6922 {"line2byte", 1, 1, f_line2byte},
6923 {"lispindent", 1, 1, f_lispindent},
6924 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006925 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 {"maparg", 1, 2, f_maparg},
6927 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006928 {"match", 2, 4, f_match},
6929 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006930 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006931 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006932 {"max", 1, 1, f_max},
6933 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006934#ifdef vim_mkdir
6935 {"mkdir", 1, 3, f_mkdir},
6936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 {"mode", 0, 0, f_mode},
6938 {"nextnonblank", 1, 1, f_nextnonblank},
6939 {"nr2char", 1, 1, f_nr2char},
6940 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006941 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006942 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006943 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006944 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {"remote_expr", 2, 3, f_remote_expr},
6946 {"remote_foreground", 1, 1, f_remote_foreground},
6947 {"remote_peek", 1, 2, f_remote_peek},
6948 {"remote_read", 1, 1, f_remote_read},
6949 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006950 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006952 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006954 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006956 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {"searchpair", 3, 5, f_searchpair},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006958 {"searchpairpos", 3, 5, f_searchpairpos},
6959 {"searchpos", 1, 2, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 {"server2client", 2, 2, f_server2client},
6961 {"serverlist", 0, 0, f_serverlist},
6962 {"setbufvar", 3, 3, f_setbufvar},
6963 {"setcmdpos", 1, 1, f_setcmdpos},
6964 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006965 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006966 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 {"setreg", 2, 3, f_setreg},
6968 {"setwinvar", 3, 3, f_setwinvar},
6969 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006970 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006971 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006972 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006973 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006974 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975#ifdef HAVE_STRFTIME
6976 {"strftime", 1, 2, f_strftime},
6977#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006978 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006979 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 {"strlen", 1, 1, f_strlen},
6981 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006982 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {"strtrans", 1, 1, f_strtrans},
6984 {"submatch", 1, 1, f_submatch},
6985 {"substitute", 4, 4, f_substitute},
6986 {"synID", 3, 3, f_synID},
6987 {"synIDattr", 2, 3, f_synIDattr},
6988 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006989 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006990 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006991 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006992 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006993 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006994 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006996 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {"tolower", 1, 1, f_tolower},
6998 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006999 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007001 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {"virtcol", 1, 1, f_virtcol},
7003 {"visualmode", 0, 1, f_visualmode},
7004 {"winbufnr", 1, 1, f_winbufnr},
7005 {"wincol", 0, 0, f_wincol},
7006 {"winheight", 1, 1, f_winheight},
7007 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007008 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 {"winrestcmd", 0, 0, f_winrestcmd},
7010 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007011 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012};
7013
7014#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7015
7016/*
7017 * Function given to ExpandGeneric() to obtain the list of internal
7018 * or user defined function names.
7019 */
7020 char_u *
7021get_function_name(xp, idx)
7022 expand_T *xp;
7023 int idx;
7024{
7025 static int intidx = -1;
7026 char_u *name;
7027
7028 if (idx == 0)
7029 intidx = -1;
7030 if (intidx < 0)
7031 {
7032 name = get_user_func_name(xp, idx);
7033 if (name != NULL)
7034 return name;
7035 }
7036 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7037 {
7038 STRCPY(IObuff, functions[intidx].f_name);
7039 STRCAT(IObuff, "(");
7040 if (functions[intidx].f_max_argc == 0)
7041 STRCAT(IObuff, ")");
7042 return IObuff;
7043 }
7044
7045 return NULL;
7046}
7047
7048/*
7049 * Function given to ExpandGeneric() to obtain the list of internal or
7050 * user defined variable or function names.
7051 */
7052/*ARGSUSED*/
7053 char_u *
7054get_expr_name(xp, idx)
7055 expand_T *xp;
7056 int idx;
7057{
7058 static int intidx = -1;
7059 char_u *name;
7060
7061 if (idx == 0)
7062 intidx = -1;
7063 if (intidx < 0)
7064 {
7065 name = get_function_name(xp, idx);
7066 if (name != NULL)
7067 return name;
7068 }
7069 return get_user_var_name(xp, ++intidx);
7070}
7071
7072#endif /* FEAT_CMDL_COMPL */
7073
7074/*
7075 * Find internal function in table above.
7076 * Return index, or -1 if not found
7077 */
7078 static int
7079find_internal_func(name)
7080 char_u *name; /* name of the function */
7081{
7082 int first = 0;
7083 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7084 int cmp;
7085 int x;
7086
7087 /*
7088 * Find the function name in the table. Binary search.
7089 */
7090 while (first <= last)
7091 {
7092 x = first + ((unsigned)(last - first) >> 1);
7093 cmp = STRCMP(name, functions[x].f_name);
7094 if (cmp < 0)
7095 last = x - 1;
7096 else if (cmp > 0)
7097 first = x + 1;
7098 else
7099 return x;
7100 }
7101 return -1;
7102}
7103
7104/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007105 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7106 * name it contains, otherwise return "name".
7107 */
7108 static char_u *
7109deref_func_name(name, lenp)
7110 char_u *name;
7111 int *lenp;
7112{
Bram Moolenaar33570922005-01-25 22:26:29 +00007113 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007114 int cc;
7115
7116 cc = name[*lenp];
7117 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007118 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007119 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007121 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007123 {
7124 *lenp = 0;
7125 return (char_u *)""; /* just in case */
7126 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 *lenp = STRLEN(v->di_tv.vval.v_string);
7128 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007129 }
7130
7131 return name;
7132}
7133
7134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 * Allocate a variable for the result of a function.
7136 * Return OK or FAIL.
7137 */
7138 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007139get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7140 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 char_u *name; /* name of the function */
7142 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 char_u **arg; /* argument, pointing to the '(' */
7145 linenr_T firstline; /* first line of range */
7146 linenr_T lastline; /* last line of range */
7147 int *doesrange; /* return: function handled range */
7148 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007149 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150{
7151 char_u *argp;
7152 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007153 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 int argcount = 0; /* number of arguments found */
7155
7156 /*
7157 * Get the arguments.
7158 */
7159 argp = *arg;
7160 while (argcount < MAX_FUNC_ARGS)
7161 {
7162 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7163 if (*argp == ')' || *argp == ',' || *argp == NUL)
7164 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7166 {
7167 ret = FAIL;
7168 break;
7169 }
7170 ++argcount;
7171 if (*argp != ',')
7172 break;
7173 }
7174 if (*argp == ')')
7175 ++argp;
7176 else
7177 ret = FAIL;
7178
7179 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007180 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007181 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 {
7184 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007185 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007186 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007187 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189
7190 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007191 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
7193 *arg = skipwhite(argp);
7194 return ret;
7195}
7196
7197
7198/*
7199 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007200 * Return OK when the function can't be called, FAIL otherwise.
7201 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 */
7203 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007204call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007205 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 char_u *name; /* name of the function */
7207 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007208 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007210 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 linenr_T firstline; /* first line of range */
7212 linenr_T lastline; /* last line of range */
7213 int *doesrange; /* return: function handled range */
7214 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007215 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218#define ERROR_UNKNOWN 0
7219#define ERROR_TOOMANY 1
7220#define ERROR_TOOFEW 2
7221#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007222#define ERROR_DICT 4
7223#define ERROR_NONE 5
7224#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 int error = ERROR_NONE;
7226 int i;
7227 int llen;
7228 ufunc_T *fp;
7229 int cc;
7230#define FLEN_FIXED 40
7231 char_u fname_buf[FLEN_FIXED + 1];
7232 char_u *fname;
7233
7234 /*
7235 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7236 * Change <SNR>123_name() to K_SNR 123_name().
7237 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7238 */
7239 cc = name[len];
7240 name[len] = NUL;
7241 llen = eval_fname_script(name);
7242 if (llen > 0)
7243 {
7244 fname_buf[0] = K_SPECIAL;
7245 fname_buf[1] = KS_EXTRA;
7246 fname_buf[2] = (int)KE_SNR;
7247 i = 3;
7248 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7249 {
7250 if (current_SID <= 0)
7251 error = ERROR_SCRIPT;
7252 else
7253 {
7254 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7255 i = (int)STRLEN(fname_buf);
7256 }
7257 }
7258 if (i + STRLEN(name + llen) < FLEN_FIXED)
7259 {
7260 STRCPY(fname_buf + i, name + llen);
7261 fname = fname_buf;
7262 }
7263 else
7264 {
7265 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7266 if (fname == NULL)
7267 error = ERROR_OTHER;
7268 else
7269 {
7270 mch_memmove(fname, fname_buf, (size_t)i);
7271 STRCPY(fname + i, name + llen);
7272 }
7273 }
7274 }
7275 else
7276 fname = name;
7277
7278 *doesrange = FALSE;
7279
7280
7281 /* execute the function if no errors detected and executing */
7282 if (evaluate && error == ERROR_NONE)
7283 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007284 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 error = ERROR_UNKNOWN;
7286
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007287 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 {
7289 /*
7290 * User defined function.
7291 */
7292 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007293
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007295 /* Trigger FuncUndefined event, may load the function. */
7296 if (fp == NULL
7297 && apply_autocmds(EVENT_FUNCUNDEFINED,
7298 fname, fname, TRUE, NULL)
7299 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007301 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 fp = find_func(fname);
7303 }
7304#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007305 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007306 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007307 {
7308 /* loaded a package, search for the function again */
7309 fp = find_func(fname);
7310 }
7311
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 if (fp != NULL)
7313 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007314 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007316 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007318 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007320 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007321 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 else
7323 {
7324 /*
7325 * Call the user function.
7326 * Save and restore search patterns, script variables and
7327 * redo buffer.
7328 */
7329 save_search_patterns();
7330 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007331 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007332 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007333 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007334 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7335 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7336 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007337 /* Function was unreferenced while being used, free it
7338 * now. */
7339 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 restoreRedobuff();
7341 restore_search_patterns();
7342 error = ERROR_NONE;
7343 }
7344 }
7345 }
7346 else
7347 {
7348 /*
7349 * Find the function name in the table, call its implementation.
7350 */
7351 i = find_internal_func(fname);
7352 if (i >= 0)
7353 {
7354 if (argcount < functions[i].f_min_argc)
7355 error = ERROR_TOOFEW;
7356 else if (argcount > functions[i].f_max_argc)
7357 error = ERROR_TOOMANY;
7358 else
7359 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007360 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007361 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 error = ERROR_NONE;
7363 }
7364 }
7365 }
7366 /*
7367 * The function call (or "FuncUndefined" autocommand sequence) might
7368 * have been aborted by an error, an interrupt, or an explicitly thrown
7369 * exception that has not been caught so far. This situation can be
7370 * tested for by calling aborting(). For an error in an internal
7371 * function or for the "E132" error in call_user_func(), however, the
7372 * throw point at which the "force_abort" flag (temporarily reset by
7373 * emsg()) is normally updated has not been reached yet. We need to
7374 * update that flag first to make aborting() reliable.
7375 */
7376 update_force_abort();
7377 }
7378 if (error == ERROR_NONE)
7379 ret = OK;
7380
7381 /*
7382 * Report an error unless the argument evaluation or function call has been
7383 * cancelled due to an aborting error, an interrupt, or an exception.
7384 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385 if (!aborting())
7386 {
7387 switch (error)
7388 {
7389 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007390 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391 break;
7392 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007393 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 break;
7395 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007396 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007397 name);
7398 break;
7399 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007400 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401 name);
7402 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007403 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007404 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007405 name);
7406 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 }
7408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409
7410 name[len] = cc;
7411 if (fname != name && fname != fname_buf)
7412 vim_free(fname);
7413
7414 return ret;
7415}
7416
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007417/*
7418 * Give an error message with a function name. Handle <SNR> things.
7419 */
7420 static void
7421emsg_funcname(msg, name)
7422 char *msg;
7423 char_u *name;
7424{
7425 char_u *p;
7426
7427 if (*name == K_SPECIAL)
7428 p = concat_str((char_u *)"<SNR>", name + 3);
7429 else
7430 p = name;
7431 EMSG2(_(msg), p);
7432 if (p != name)
7433 vim_free(p);
7434}
7435
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436/*********************************************
7437 * Implementation of the built-in functions
7438 */
7439
7440/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007441 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 */
7443 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007444f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007445 typval_T *argvars;
7446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447{
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007450 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007451 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007453 if ((l = argvars[0].vval.v_list) != NULL
7454 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7455 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007456 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007457 }
7458 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007459 EMSG(_(e_listreq));
7460}
7461
7462/*
7463 * "append(lnum, string/list)" function
7464 */
7465 static void
7466f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007467 typval_T *argvars;
7468 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007469{
7470 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007471 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007472 list_T *l = NULL;
7473 listitem_T *li = NULL;
7474 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007475 long added = 0;
7476
Bram Moolenaar0d660222005-01-07 21:51:51 +00007477 lnum = get_tv_lnum(argvars);
7478 if (lnum >= 0
7479 && lnum <= curbuf->b_ml.ml_line_count
7480 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007481 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007482 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007483 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007484 l = argvars[1].vval.v_list;
7485 if (l == NULL)
7486 return;
7487 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007488 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007489 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007490 for (;;)
7491 {
7492 if (l == NULL)
7493 tv = &argvars[1]; /* append a string */
7494 else if (li == NULL)
7495 break; /* end of list */
7496 else
7497 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007498 line = get_tv_string_chk(tv);
7499 if (line == NULL) /* type error */
7500 {
7501 rettv->vval.v_number = 1; /* Failed */
7502 break;
7503 }
7504 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007505 ++added;
7506 if (l == NULL)
7507 break;
7508 li = li->li_next;
7509 }
7510
7511 appended_lines_mark(lnum, added);
7512 if (curwin->w_cursor.lnum > lnum)
7513 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007515 else
7516 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517}
7518
7519/*
7520 * "argc()" function
7521 */
7522/* ARGSUSED */
7523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007524f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007525 typval_T *argvars;
7526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007528 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529}
7530
7531/*
7532 * "argidx()" function
7533 */
7534/* ARGSUSED */
7535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007536f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007537 typval_T *argvars;
7538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007540 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541}
7542
7543/*
7544 * "argv(nr)" function
7545 */
7546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007547f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 typval_T *argvars;
7549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550{
7551 int idx;
7552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007553 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007555 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007557 rettv->vval.v_string = NULL;
7558 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559}
7560
7561/*
7562 * "browse(save, title, initdir, default)" function
7563 */
7564/* ARGSUSED */
7565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007566f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 typval_T *argvars;
7568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569{
7570#ifdef FEAT_BROWSE
7571 int save;
7572 char_u *title;
7573 char_u *initdir;
7574 char_u *defname;
7575 char_u buf[NUMBUFLEN];
7576 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007577 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007579 save = get_tv_number_chk(&argvars[0], &error);
7580 title = get_tv_string_chk(&argvars[1]);
7581 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7582 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007584 if (error || title == NULL || initdir == NULL || defname == NULL)
7585 rettv->vval.v_string = NULL;
7586 else
7587 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007588 do_browse(save ? BROWSE_SAVE : 0,
7589 title, defname, NULL, initdir, NULL, curbuf);
7590#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007591 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007592#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007593 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007594}
7595
7596/*
7597 * "browsedir(title, initdir)" function
7598 */
7599/* ARGSUSED */
7600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007601f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007602 typval_T *argvars;
7603 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007604{
7605#ifdef FEAT_BROWSE
7606 char_u *title;
7607 char_u *initdir;
7608 char_u buf[NUMBUFLEN];
7609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007610 title = get_tv_string_chk(&argvars[0]);
7611 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007613 if (title == NULL || initdir == NULL)
7614 rettv->vval.v_string = NULL;
7615 else
7616 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007617 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007619 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007621 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622}
7623
Bram Moolenaar33570922005-01-25 22:26:29 +00007624static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007625
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626/*
7627 * Find a buffer by number or exact name.
7628 */
7629 static buf_T *
7630find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007631 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632{
7633 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007635 if (avar->v_type == VAR_NUMBER)
7636 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007637 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007639 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007640 if (buf == NULL)
7641 {
7642 /* No full path name match, try a match with a URL or a "nofile"
7643 * buffer, these don't use the full path. */
7644 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7645 if (buf->b_fname != NULL
7646 && (path_with_url(buf->b_fname)
7647#ifdef FEAT_QUICKFIX
7648 || bt_nofile(buf)
7649#endif
7650 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007651 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007652 break;
7653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
7655 return buf;
7656}
7657
7658/*
7659 * "bufexists(expr)" function
7660 */
7661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007662f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007663 typval_T *argvars;
7664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007666 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667}
7668
7669/*
7670 * "buflisted(expr)" function
7671 */
7672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007674 typval_T *argvars;
7675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676{
7677 buf_T *buf;
7678
7679 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007680 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681}
7682
7683/*
7684 * "bufloaded(expr)" function
7685 */
7686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007688 typval_T *argvars;
7689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690{
7691 buf_T *buf;
7692
7693 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007694 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695}
7696
Bram Moolenaar33570922005-01-25 22:26:29 +00007697static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007698
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699/*
7700 * Get buffer by number or pattern.
7701 */
7702 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007703get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007704 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007706 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 int save_magic;
7708 char_u *save_cpo;
7709 buf_T *buf;
7710
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007711 if (tv->v_type == VAR_NUMBER)
7712 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007713 if (tv->v_type != VAR_STRING)
7714 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 if (name == NULL || *name == NUL)
7716 return curbuf;
7717 if (name[0] == '$' && name[1] == NUL)
7718 return lastbuf;
7719
7720 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7721 save_magic = p_magic;
7722 p_magic = TRUE;
7723 save_cpo = p_cpo;
7724 p_cpo = (char_u *)"";
7725
7726 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7727 TRUE, FALSE));
7728
7729 p_magic = save_magic;
7730 p_cpo = save_cpo;
7731
7732 /* If not found, try expanding the name, like done for bufexists(). */
7733 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007734 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735
7736 return buf;
7737}
7738
7739/*
7740 * "bufname(expr)" function
7741 */
7742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007743f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007744 typval_T *argvars;
7745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
7747 buf_T *buf;
7748
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007749 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007751 buf = get_buf_tv(&argvars[0]);
7752 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007754 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007756 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 --emsg_off;
7758}
7759
7760/*
7761 * "bufnr(expr)" function
7762 */
7763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007764f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007765 typval_T *argvars;
7766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767{
7768 buf_T *buf;
7769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007770 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007772 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 --emsg_off;
7778}
7779
7780/*
7781 * "bufwinnr(nr)" function
7782 */
7783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007784f_bufwinnr(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#ifdef FEAT_WINDOWS
7789 win_T *wp;
7790 int winnr = 0;
7791#endif
7792 buf_T *buf;
7793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007794 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007796 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797#ifdef FEAT_WINDOWS
7798 for (wp = firstwin; wp; wp = wp->w_next)
7799 {
7800 ++winnr;
7801 if (wp->w_buffer == buf)
7802 break;
7803 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007806 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807#endif
7808 --emsg_off;
7809}
7810
7811/*
7812 * "byte2line(byte)" function
7813 */
7814/*ARGSUSED*/
7815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007816f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007817 typval_T *argvars;
7818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007821 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822#else
7823 long boff = 0;
7824
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007825 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007827 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007829 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 (linenr_T)0, &boff);
7831#endif
7832}
7833
7834/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007835 * "byteidx()" function
7836 */
7837/*ARGSUSED*/
7838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007839f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007840 typval_T *argvars;
7841 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007842{
7843#ifdef FEAT_MBYTE
7844 char_u *t;
7845#endif
7846 char_u *str;
7847 long idx;
7848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007849 str = get_tv_string_chk(&argvars[0]);
7850 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007851 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007852 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007853 return;
7854
7855#ifdef FEAT_MBYTE
7856 t = str;
7857 for ( ; idx > 0; idx--)
7858 {
7859 if (*t == NUL) /* EOL reached */
7860 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007861 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007862 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007863 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007864#else
7865 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007867#endif
7868}
7869
7870/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007871 * "call(func, arglist)" function
7872 */
7873 static void
7874f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 typval_T *argvars;
7876 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007877{
7878 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007879 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007880 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007881 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007882 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007883 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007884
7885 rettv->vval.v_number = 0;
7886 if (argvars[1].v_type != VAR_LIST)
7887 {
7888 EMSG(_(e_listreq));
7889 return;
7890 }
7891 if (argvars[1].vval.v_list == NULL)
7892 return;
7893
7894 if (argvars[0].v_type == VAR_FUNC)
7895 func = argvars[0].vval.v_string;
7896 else
7897 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007898 if (*func == NUL)
7899 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007900
Bram Moolenaare9a41262005-01-15 22:18:47 +00007901 if (argvars[2].v_type != VAR_UNKNOWN)
7902 {
7903 if (argvars[2].v_type != VAR_DICT)
7904 {
7905 EMSG(_(e_dictreq));
7906 return;
7907 }
7908 selfdict = argvars[2].vval.v_dict;
7909 }
7910
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007911 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7912 item = item->li_next)
7913 {
7914 if (argc == MAX_FUNC_ARGS)
7915 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007916 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007917 break;
7918 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007919 /* Make a copy of each argument. This is needed to be able to set
7920 * v_lock to VAR_FIXED in the copy without changing the original list.
7921 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007922 copy_tv(&item->li_tv, &argv[argc++]);
7923 }
7924
7925 if (item == NULL)
7926 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007927 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7928 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007929
7930 /* Free the arguments. */
7931 while (argc > 0)
7932 clear_tv(&argv[--argc]);
7933}
7934
7935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 * "char2nr(string)" function
7937 */
7938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007939f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007940 typval_T *argvars;
7941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
7943#ifdef FEAT_MBYTE
7944 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007945 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 else
7947#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007948 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949}
7950
7951/*
7952 * "cindent(lnum)" function
7953 */
7954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007955f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 typval_T *argvars;
7957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958{
7959#ifdef FEAT_CINDENT
7960 pos_T pos;
7961 linenr_T lnum;
7962
7963 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007964 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7966 {
7967 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 curwin->w_cursor = pos;
7970 }
7971 else
7972#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007973 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974}
7975
7976/*
7977 * "col(string)" function
7978 */
7979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007980f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007981 typval_T *argvars;
7982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983{
7984 colnr_T col = 0;
7985 pos_T *fp;
7986
7987 fp = var2fpos(&argvars[0], FALSE);
7988 if (fp != NULL)
7989 {
7990 if (fp->col == MAXCOL)
7991 {
7992 /* '> can be MAXCOL, get the length of the line then */
7993 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7994 col = STRLEN(ml_get(fp->lnum)) + 1;
7995 else
7996 col = MAXCOL;
7997 }
7998 else
7999 {
8000 col = fp->col + 1;
8001#ifdef FEAT_VIRTUALEDIT
8002 /* col(".") when the cursor is on the NUL at the end of the line
8003 * because of "coladd" can be seen as an extra column. */
8004 if (virtual_active() && fp == &curwin->w_cursor)
8005 {
8006 char_u *p = ml_get_cursor();
8007
8008 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8009 curwin->w_virtcol - curwin->w_cursor.coladd))
8010 {
8011# ifdef FEAT_MBYTE
8012 int l;
8013
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008014 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 col += l;
8016# else
8017 if (*p != NUL && p[1] == NUL)
8018 ++col;
8019# endif
8020 }
8021 }
8022#endif
8023 }
8024 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008025 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026}
8027
Bram Moolenaar572cb562005-08-05 21:35:02 +00008028#if defined(FEAT_INS_EXPAND)
8029/*
8030 * "complete_add()" function
8031 */
8032/*ARGSUSED*/
8033 static void
8034f_complete_add(argvars, rettv)
8035 typval_T *argvars;
8036 typval_T *rettv;
8037{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008038 char_u *word;
8039 char_u *extra = NULL;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008040 int icase = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008041
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008042 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8043 {
8044 word = get_dict_string(argvars[0].vval.v_dict,
8045 (char_u *)"word", FALSE);
8046 extra = get_dict_string(argvars[0].vval.v_dict,
8047 (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008048 icase = get_dict_number(argvars[0].vval.v_dict, (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008049 }
8050 else
8051 word = get_tv_string_chk(&argvars[0]);
8052 if (word != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008053 rettv->vval.v_number = ins_compl_add(word, -1, icase,
8054 NULL, extra, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008055}
8056
8057/*
8058 * "complete_check()" function
8059 */
8060/*ARGSUSED*/
8061 static void
8062f_complete_check(argvars, rettv)
8063 typval_T *argvars;
8064 typval_T *rettv;
8065{
8066 int saved = RedrawingDisabled;
8067
8068 RedrawingDisabled = 0;
8069 ins_compl_check_keys(0);
8070 rettv->vval.v_number = compl_interrupted;
8071 RedrawingDisabled = saved;
8072}
8073#endif
8074
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075/*
8076 * "confirm(message, buttons[, default [, type]])" function
8077 */
8078/*ARGSUSED*/
8079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008080f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008081 typval_T *argvars;
8082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
8084#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8085 char_u *message;
8086 char_u *buttons = NULL;
8087 char_u buf[NUMBUFLEN];
8088 char_u buf2[NUMBUFLEN];
8089 int def = 1;
8090 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008091 char_u *typestr;
8092 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008094 message = get_tv_string_chk(&argvars[0]);
8095 if (message == NULL)
8096 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008097 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008099 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8100 if (buttons == NULL)
8101 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008102 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008104 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008105 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008107 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8108 if (typestr == NULL)
8109 error = TRUE;
8110 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008112 switch (TOUPPER_ASC(*typestr))
8113 {
8114 case 'E': type = VIM_ERROR; break;
8115 case 'Q': type = VIM_QUESTION; break;
8116 case 'I': type = VIM_INFO; break;
8117 case 'W': type = VIM_WARNING; break;
8118 case 'G': type = VIM_GENERIC; break;
8119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 }
8121 }
8122 }
8123 }
8124
8125 if (buttons == NULL || *buttons == NUL)
8126 buttons = (char_u *)_("&Ok");
8127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008128 if (error)
8129 rettv->vval.v_number = 0;
8130 else
8131 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 def, NULL);
8133#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008134 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135#endif
8136}
8137
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138/*
8139 * "copy()" function
8140 */
8141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008142f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008143 typval_T *argvars;
8144 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008145{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008146 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008147}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148
8149/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008150 * "count()" function
8151 */
8152 static void
8153f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008154 typval_T *argvars;
8155 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008156{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008157 long n = 0;
8158 int ic = FALSE;
8159
Bram Moolenaare9a41262005-01-15 22:18:47 +00008160 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008161 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008162 listitem_T *li;
8163 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008164 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008165
Bram Moolenaare9a41262005-01-15 22:18:47 +00008166 if ((l = argvars[0].vval.v_list) != NULL)
8167 {
8168 li = l->lv_first;
8169 if (argvars[2].v_type != VAR_UNKNOWN)
8170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008171 int error = FALSE;
8172
8173 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008174 if (argvars[3].v_type != VAR_UNKNOWN)
8175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008176 idx = get_tv_number_chk(&argvars[3], &error);
8177 if (!error)
8178 {
8179 li = list_find(l, idx);
8180 if (li == NULL)
8181 EMSGN(_(e_listidx), idx);
8182 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008183 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008184 if (error)
8185 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008186 }
8187
8188 for ( ; li != NULL; li = li->li_next)
8189 if (tv_equal(&li->li_tv, &argvars[1], ic))
8190 ++n;
8191 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008192 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008193 else if (argvars[0].v_type == VAR_DICT)
8194 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008195 int todo;
8196 dict_T *d;
8197 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008198
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008199 if ((d = argvars[0].vval.v_dict) != NULL)
8200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008201 int error = FALSE;
8202
Bram Moolenaare9a41262005-01-15 22:18:47 +00008203 if (argvars[2].v_type != VAR_UNKNOWN)
8204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008205 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008206 if (argvars[3].v_type != VAR_UNKNOWN)
8207 EMSG(_(e_invarg));
8208 }
8209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008210 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008211 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008212 {
8213 if (!HASHITEM_EMPTY(hi))
8214 {
8215 --todo;
8216 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8217 ++n;
8218 }
8219 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008220 }
8221 }
8222 else
8223 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008224 rettv->vval.v_number = n;
8225}
8226
8227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8229 *
8230 * Checks the existence of a cscope connection.
8231 */
8232/*ARGSUSED*/
8233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008234f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008235 typval_T *argvars;
8236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237{
8238#ifdef FEAT_CSCOPE
8239 int num = 0;
8240 char_u *dbpath = NULL;
8241 char_u *prepend = NULL;
8242 char_u buf[NUMBUFLEN];
8243
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008244 if (argvars[0].v_type != VAR_UNKNOWN
8245 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008247 num = (int)get_tv_number(&argvars[0]);
8248 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008249 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008250 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 }
8252
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008253 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008255 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256#endif
8257}
8258
8259/*
8260 * "cursor(lnum, col)" function
8261 *
8262 * Moves the cursor to the specified line and column
8263 */
8264/*ARGSUSED*/
8265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008266f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008267 typval_T *argvars;
8268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269{
8270 long line, col;
8271
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008272 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008273 col = get_tv_number_chk(&argvars[1], NULL);
8274 if (line < 0 || col < 0)
8275 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 if (line > 0)
8277 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 if (col > 0)
8279 curwin->w_cursor.col = col - 1;
8280#ifdef FEAT_VIRTUALEDIT
8281 curwin->w_cursor.coladd = 0;
8282#endif
8283
8284 /* Make sure the cursor is in a valid position. */
8285 check_cursor();
8286#ifdef FEAT_MBYTE
8287 /* Correct cursor for multi-byte character. */
8288 if (has_mbyte)
8289 mb_adjust_cursor();
8290#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008291
8292 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293}
8294
8295/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008296 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 */
8298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008299f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008300 typval_T *argvars;
8301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008303 int noref = 0;
8304
8305 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008306 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008307 if (noref < 0 || noref > 1)
8308 EMSG(_(e_invarg));
8309 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008310 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311}
8312
8313/*
8314 * "delete()" function
8315 */
8316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008317f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008318 typval_T *argvars;
8319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
8321 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008322 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008324 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325}
8326
8327/*
8328 * "did_filetype()" function
8329 */
8330/*ARGSUSED*/
8331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008332f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008333 typval_T *argvars;
8334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
8336#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008337 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008339 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340#endif
8341}
8342
8343/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008344 * "diff_filler()" function
8345 */
8346/*ARGSUSED*/
8347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008348f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 typval_T *argvars;
8350 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008351{
8352#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008353 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008354#endif
8355}
8356
8357/*
8358 * "diff_hlID()" function
8359 */
8360/*ARGSUSED*/
8361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008362f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008363 typval_T *argvars;
8364 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008365{
8366#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008367 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008368 static linenr_T prev_lnum = 0;
8369 static int changedtick = 0;
8370 static int fnum = 0;
8371 static int change_start = 0;
8372 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008373 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008374 int filler_lines;
8375 int col;
8376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008377 if (lnum < 0) /* ignore type error in {lnum} arg */
8378 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008379 if (lnum != prev_lnum
8380 || changedtick != curbuf->b_changedtick
8381 || fnum != curbuf->b_fnum)
8382 {
8383 /* New line, buffer, change: need to get the values. */
8384 filler_lines = diff_check(curwin, lnum);
8385 if (filler_lines < 0)
8386 {
8387 if (filler_lines == -1)
8388 {
8389 change_start = MAXCOL;
8390 change_end = -1;
8391 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8392 hlID = HLF_ADD; /* added line */
8393 else
8394 hlID = HLF_CHD; /* changed line */
8395 }
8396 else
8397 hlID = HLF_ADD; /* added line */
8398 }
8399 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008400 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008401 prev_lnum = lnum;
8402 changedtick = curbuf->b_changedtick;
8403 fnum = curbuf->b_fnum;
8404 }
8405
8406 if (hlID == HLF_CHD || hlID == HLF_TXD)
8407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008408 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008409 if (col >= change_start && col <= change_end)
8410 hlID = HLF_TXD; /* changed text */
8411 else
8412 hlID = HLF_CHD; /* changed line */
8413 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008414 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008415#endif
8416}
8417
8418/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008419 * "empty({expr})" function
8420 */
8421 static void
8422f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008423 typval_T *argvars;
8424 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008425{
8426 int n;
8427
8428 switch (argvars[0].v_type)
8429 {
8430 case VAR_STRING:
8431 case VAR_FUNC:
8432 n = argvars[0].vval.v_string == NULL
8433 || *argvars[0].vval.v_string == NUL;
8434 break;
8435 case VAR_NUMBER:
8436 n = argvars[0].vval.v_number == 0;
8437 break;
8438 case VAR_LIST:
8439 n = argvars[0].vval.v_list == NULL
8440 || argvars[0].vval.v_list->lv_first == NULL;
8441 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008442 case VAR_DICT:
8443 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008444 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008445 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008446 default:
8447 EMSG2(_(e_intern2), "f_empty()");
8448 n = 0;
8449 }
8450
8451 rettv->vval.v_number = n;
8452}
8453
8454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 * "escape({string}, {chars})" function
8456 */
8457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008458f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008459 typval_T *argvars;
8460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461{
8462 char_u buf[NUMBUFLEN];
8463
Bram Moolenaar758711c2005-02-02 23:11:38 +00008464 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8465 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008466 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467}
8468
8469/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008470 * "eval()" function
8471 */
8472/*ARGSUSED*/
8473 static void
8474f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008475 typval_T *argvars;
8476 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008477{
8478 char_u *s;
8479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008480 s = get_tv_string_chk(&argvars[0]);
8481 if (s != NULL)
8482 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008484 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8485 {
8486 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008487 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008488 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008489 else if (*s != NUL)
8490 EMSG(_(e_trailing));
8491}
8492
8493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 * "eventhandler()" function
8495 */
8496/*ARGSUSED*/
8497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008499 typval_T *argvars;
8500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008502 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503}
8504
8505/*
8506 * "executable()" function
8507 */
8508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008509f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008510 typval_T *argvars;
8511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008513 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514}
8515
8516/*
8517 * "exists()" function
8518 */
8519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008520f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008521 typval_T *argvars;
8522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523{
8524 char_u *p;
8525 char_u *name;
8526 int n = FALSE;
8527 int len = 0;
8528
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008529 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 if (*p == '$') /* environment variable */
8531 {
8532 /* first try "normal" environment variables (fast) */
8533 if (mch_getenv(p + 1) != NULL)
8534 n = TRUE;
8535 else
8536 {
8537 /* try expanding things like $VIM and ${HOME} */
8538 p = expand_env_save(p);
8539 if (p != NULL && *p != '$')
8540 n = TRUE;
8541 vim_free(p);
8542 }
8543 }
8544 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008545 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 else if (*p == '*') /* internal or user defined function */
8547 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008548 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 }
8550 else if (*p == ':')
8551 {
8552 n = cmd_exists(p + 1);
8553 }
8554 else if (*p == '#')
8555 {
8556#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008557 if (p[1] == '#')
8558 n = autocmd_supported(p + 2);
8559 else
8560 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561#endif
8562 }
8563 else /* internal variable */
8564 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008565 char_u *tofree;
8566 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008568 /* get_name_len() takes care of expanding curly braces */
8569 name = p;
8570 len = get_name_len(&p, &tofree, TRUE, FALSE);
8571 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008573 if (tofree != NULL)
8574 name = tofree;
8575 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8576 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008578 /* handle d.key, l[idx], f(expr) */
8579 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8580 if (n)
8581 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 }
8583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008585 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 }
8587
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589}
8590
8591/*
8592 * "expand()" function
8593 */
8594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008595f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008596 typval_T *argvars;
8597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598{
8599 char_u *s;
8600 int len;
8601 char_u *errormsg;
8602 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8603 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008604 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008606 rettv->v_type = VAR_STRING;
8607 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 if (*s == '%' || *s == '#' || *s == '<')
8609 {
8610 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008611 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 --emsg_off;
8613 }
8614 else
8615 {
8616 /* When the optional second argument is non-zero, don't remove matches
8617 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008618 if (argvars[1].v_type != VAR_UNKNOWN
8619 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008621 if (!error)
8622 {
8623 ExpandInit(&xpc);
8624 xpc.xp_context = EXPAND_FILES;
8625 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8626 ExpandCleanup(&xpc);
8627 }
8628 else
8629 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 }
8631}
8632
8633/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008634 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008635 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008636 */
8637 static void
8638f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008639 typval_T *argvars;
8640 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008641{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008642 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008643 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008644 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008645 list_T *l1, *l2;
8646 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008647 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008648 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008649
Bram Moolenaare9a41262005-01-15 22:18:47 +00008650 l1 = argvars[0].vval.v_list;
8651 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008652 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8653 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008654 {
8655 if (argvars[2].v_type != VAR_UNKNOWN)
8656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008657 before = get_tv_number_chk(&argvars[2], &error);
8658 if (error)
8659 return; /* type error; errmsg already given */
8660
Bram Moolenaar758711c2005-02-02 23:11:38 +00008661 if (before == l1->lv_len)
8662 item = NULL;
8663 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008664 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008665 item = list_find(l1, before);
8666 if (item == NULL)
8667 {
8668 EMSGN(_(e_listidx), before);
8669 return;
8670 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008671 }
8672 }
8673 else
8674 item = NULL;
8675 list_extend(l1, l2, item);
8676
Bram Moolenaare9a41262005-01-15 22:18:47 +00008677 copy_tv(&argvars[0], rettv);
8678 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008679 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008680 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8681 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008682 dict_T *d1, *d2;
8683 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008684 char_u *action;
8685 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008686 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008687 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008688
8689 d1 = argvars[0].vval.v_dict;
8690 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008691 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8692 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008693 {
8694 /* Check the third argument. */
8695 if (argvars[2].v_type != VAR_UNKNOWN)
8696 {
8697 static char *(av[]) = {"keep", "force", "error"};
8698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008699 action = get_tv_string_chk(&argvars[2]);
8700 if (action == NULL)
8701 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008702 for (i = 0; i < 3; ++i)
8703 if (STRCMP(action, av[i]) == 0)
8704 break;
8705 if (i == 3)
8706 {
8707 EMSGN(_(e_invarg2), action);
8708 return;
8709 }
8710 }
8711 else
8712 action = (char_u *)"force";
8713
8714 /* Go over all entries in the second dict and add them to the
8715 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008716 todo = d2->dv_hashtab.ht_used;
8717 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008718 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008719 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008720 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008721 --todo;
8722 di1 = dict_find(d1, hi2->hi_key, -1);
8723 if (di1 == NULL)
8724 {
8725 di1 = dictitem_copy(HI2DI(hi2));
8726 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8727 dictitem_free(di1);
8728 }
8729 else if (*action == 'e')
8730 {
8731 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8732 break;
8733 }
8734 else if (*action == 'f')
8735 {
8736 clear_tv(&di1->di_tv);
8737 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8738 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008739 }
8740 }
8741
Bram Moolenaare9a41262005-01-15 22:18:47 +00008742 copy_tv(&argvars[0], rettv);
8743 }
8744 }
8745 else
8746 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008747}
8748
8749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 * "filereadable()" function
8751 */
8752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008754 typval_T *argvars;
8755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756{
8757 FILE *fd;
8758 char_u *p;
8759 int n;
8760
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008761 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8763 {
8764 n = TRUE;
8765 fclose(fd);
8766 }
8767 else
8768 n = FALSE;
8769
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771}
8772
8773/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008774 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 * rights to write into.
8776 */
8777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008779 typval_T *argvars;
8780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008782 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783}
8784
Bram Moolenaar33570922005-01-25 22:26:29 +00008785static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008786
8787 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008788findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008789 typval_T *argvars;
8790 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008791 int dir;
8792{
8793#ifdef FEAT_SEARCHPATH
8794 char_u *fname;
8795 char_u *fresult = NULL;
8796 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8797 char_u *p;
8798 char_u pathbuf[NUMBUFLEN];
8799 int count = 1;
8800 int first = TRUE;
8801
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008803
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008804 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008806 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8807 if (p == NULL)
8808 count = -1; /* error */
8809 else
8810 {
8811 if (*p != NUL)
8812 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008814 if (argvars[2].v_type != VAR_UNKNOWN)
8815 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8816 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008817 }
8818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008819 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008821 do
8822 {
8823 vim_free(fresult);
8824 fresult = find_file_in_path_option(first ? fname : NULL,
8825 first ? (int)STRLEN(fname) : 0,
8826 0, first, path, dir, NULL);
8827 first = FALSE;
8828 } while (--count > 0 && fresult != NULL);
8829 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008830
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008832#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008833 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008834#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008835 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008836}
8837
Bram Moolenaar33570922005-01-25 22:26:29 +00008838static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8839static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008840
8841/*
8842 * Implementation of map() and filter().
8843 */
8844 static void
8845filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008846 typval_T *argvars;
8847 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008848 int map;
8849{
8850 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008851 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008852 listitem_T *li, *nli;
8853 list_T *l = NULL;
8854 dictitem_T *di;
8855 hashtab_T *ht;
8856 hashitem_T *hi;
8857 dict_T *d = NULL;
8858 typval_T save_val;
8859 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008860 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008861 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008862 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar280f1262006-01-30 00:14:18 +00008863 int save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008864
8865 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008866 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008867 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008868 if ((l = argvars[0].vval.v_list) == NULL
8869 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008870 return;
8871 }
8872 else if (argvars[0].v_type == VAR_DICT)
8873 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008874 if ((d = argvars[0].vval.v_dict) == NULL
8875 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008876 return;
8877 }
8878 else
8879 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008880 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008881 return;
8882 }
8883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008884 expr = get_tv_string_buf_chk(&argvars[1], buf);
8885 /* On type errors, the preceding call has already displayed an error
8886 * message. Avoid a misleading error message for an empty string that
8887 * was not passed as argument. */
8888 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008890 prepare_vimvar(VV_VAL, &save_val);
8891 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008892
Bram Moolenaar280f1262006-01-30 00:14:18 +00008893 /* We reset "called_emsg" to be able to detect whether an error
8894 * occurred during evaluation of the expression. "did_emsg" can't be
8895 * used, because it is reset when calling a function. */
8896 save_called_emsg = called_emsg;
8897 called_emsg = FALSE;
8898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008899 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008900 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008901 prepare_vimvar(VV_KEY, &save_key);
8902 vimvars[VV_KEY].vv_type = VAR_STRING;
8903
8904 ht = &d->dv_hashtab;
8905 hash_lock(ht);
8906 todo = ht->ht_used;
8907 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008908 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008909 if (!HASHITEM_EMPTY(hi))
8910 {
8911 --todo;
8912 di = HI2DI(hi);
8913 if (tv_check_lock(di->di_tv.v_lock, msg))
8914 break;
8915 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008916 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
8917 || called_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008918 break;
8919 if (!map && rem)
8920 dictitem_remove(d, di);
8921 clear_tv(&vimvars[VV_KEY].vv_tv);
8922 }
8923 }
8924 hash_unlock(ht);
8925
8926 restore_vimvar(VV_KEY, &save_key);
8927 }
8928 else
8929 {
8930 for (li = l->lv_first; li != NULL; li = nli)
8931 {
8932 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008933 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008935 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
8936 || called_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008937 break;
8938 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008939 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008940 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008941 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008943 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008944
8945 called_emsg |= save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008946 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008947
8948 copy_tv(&argvars[0], rettv);
8949}
8950
8951 static int
8952filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008953 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008954 char_u *expr;
8955 int map;
8956 int *remp;
8957{
Bram Moolenaar33570922005-01-25 22:26:29 +00008958 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008959 char_u *s;
8960
Bram Moolenaar33570922005-01-25 22:26:29 +00008961 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008962 s = expr;
8963 if (eval1(&s, &rettv, TRUE) == FAIL)
8964 return FAIL;
8965 if (*s != NUL) /* check for trailing chars after expr */
8966 {
8967 EMSG2(_(e_invexpr2), s);
8968 return FAIL;
8969 }
8970 if (map)
8971 {
8972 /* map(): replace the list item value */
8973 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008974 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008975 *tv = rettv;
8976 }
8977 else
8978 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008979 int error = FALSE;
8980
Bram Moolenaare9a41262005-01-15 22:18:47 +00008981 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008982 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008983 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008984 /* On type error, nothing has been removed; return FAIL to stop the
8985 * loop. The error message was given by get_tv_number_chk(). */
8986 if (error)
8987 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008988 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008989 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008990 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008991}
8992
8993/*
8994 * "filter()" function
8995 */
8996 static void
8997f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008998 typval_T *argvars;
8999 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009000{
9001 filter_map(argvars, rettv, FALSE);
9002}
9003
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009004/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009005 * "finddir({fname}[, {path}[, {count}]])" function
9006 */
9007 static void
9008f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009009 typval_T *argvars;
9010 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009011{
9012 findfilendir(argvars, rettv, TRUE);
9013}
9014
9015/*
9016 * "findfile({fname}[, {path}[, {count}]])" function
9017 */
9018 static void
9019f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009020 typval_T *argvars;
9021 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009022{
9023 findfilendir(argvars, rettv, FALSE);
9024}
9025
9026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 * "fnamemodify({fname}, {mods})" function
9028 */
9029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009030f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009031 typval_T *argvars;
9032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033{
9034 char_u *fname;
9035 char_u *mods;
9036 int usedlen = 0;
9037 int len;
9038 char_u *fbuf = NULL;
9039 char_u buf[NUMBUFLEN];
9040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009041 fname = get_tv_string_chk(&argvars[0]);
9042 mods = get_tv_string_buf_chk(&argvars[1], buf);
9043 if (fname == NULL || mods == NULL)
9044 fname = NULL;
9045 else
9046 {
9047 len = (int)STRLEN(fname);
9048 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009051 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009053 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009055 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 vim_free(fbuf);
9057}
9058
Bram Moolenaar33570922005-01-25 22:26:29 +00009059static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060
9061/*
9062 * "foldclosed()" function
9063 */
9064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009065foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009066 typval_T *argvars;
9067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 int end;
9069{
9070#ifdef FEAT_FOLDING
9071 linenr_T lnum;
9072 linenr_T first, last;
9073
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009074 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9076 {
9077 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9078 {
9079 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 return;
9084 }
9085 }
9086#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088}
9089
9090/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009091 * "foldclosed()" function
9092 */
9093 static void
9094f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009095 typval_T *argvars;
9096 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009097{
9098 foldclosed_both(argvars, rettv, FALSE);
9099}
9100
9101/*
9102 * "foldclosedend()" function
9103 */
9104 static void
9105f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009106 typval_T *argvars;
9107 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009108{
9109 foldclosed_both(argvars, rettv, TRUE);
9110}
9111
9112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 * "foldlevel()" function
9114 */
9115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009116f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009117 typval_T *argvars;
9118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119{
9120#ifdef FEAT_FOLDING
9121 linenr_T lnum;
9122
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009125 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 else
9127#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009128 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129}
9130
9131/*
9132 * "foldtext()" function
9133 */
9134/*ARGSUSED*/
9135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009136f_foldtext(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 char_u *s;
9143 char_u *r;
9144 int len;
9145 char *txt;
9146#endif
9147
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009148 rettv->v_type = VAR_STRING;
9149 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009151 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9152 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9153 <= curbuf->b_ml.ml_line_count
9154 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 {
9156 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009157 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9158 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 {
9160 if (!linewhite(lnum))
9161 break;
9162 ++lnum;
9163 }
9164
9165 /* Find interesting text in this line. */
9166 s = skipwhite(ml_get(lnum));
9167 /* skip C comment-start */
9168 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009171 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009172 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009173 {
9174 s = skipwhite(ml_get(lnum + 1));
9175 if (*s == '*')
9176 s = skipwhite(s + 1);
9177 }
9178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 txt = _("+-%s%3ld lines: ");
9180 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009181 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 + 20 /* for %3ld */
9183 + STRLEN(s))); /* concatenated */
9184 if (r != NULL)
9185 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009186 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9187 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9188 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 len = (int)STRLEN(r);
9190 STRCAT(r, s);
9191 /* remove 'foldmarker' and 'commentstring' */
9192 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009193 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 }
9195 }
9196#endif
9197}
9198
9199/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009200 * "foldtextresult(lnum)" function
9201 */
9202/*ARGSUSED*/
9203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009205 typval_T *argvars;
9206 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009207{
9208#ifdef FEAT_FOLDING
9209 linenr_T lnum;
9210 char_u *text;
9211 char_u buf[51];
9212 foldinfo_T foldinfo;
9213 int fold_count;
9214#endif
9215
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 rettv->v_type = VAR_STRING;
9217 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009218#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009220 /* treat illegal types and illegal string values for {lnum} the same */
9221 if (lnum < 0)
9222 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009223 fold_count = foldedCount(curwin, lnum, &foldinfo);
9224 if (fold_count > 0)
9225 {
9226 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9227 &foldinfo, buf);
9228 if (text == buf)
9229 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009230 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009231 }
9232#endif
9233}
9234
9235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 * "foreground()" function
9237 */
9238/*ARGSUSED*/
9239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009240f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009241 typval_T *argvars;
9242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009244 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245#ifdef FEAT_GUI
9246 if (gui.in_use)
9247 gui_mch_set_foreground();
9248#else
9249# ifdef WIN32
9250 win32_set_foreground();
9251# endif
9252#endif
9253}
9254
9255/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009256 * "function()" function
9257 */
9258/*ARGSUSED*/
9259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009260f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009261 typval_T *argvars;
9262 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009263{
9264 char_u *s;
9265
Bram Moolenaara7043832005-01-21 11:56:39 +00009266 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009267 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009268 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009269 EMSG2(_(e_invarg2), s);
9270 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009271 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009272 else
9273 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009274 rettv->vval.v_string = vim_strsave(s);
9275 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009276 }
9277}
9278
9279/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009280 * "garbagecollect()" function
9281 */
9282/*ARGSUSED*/
9283 static void
9284f_garbagecollect(argvars, rettv)
9285 typval_T *argvars;
9286 typval_T *rettv;
9287{
9288 garbage_collect();
9289}
9290
9291/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009292 * "get()" function
9293 */
9294 static void
9295f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009296 typval_T *argvars;
9297 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009298{
Bram Moolenaar33570922005-01-25 22:26:29 +00009299 listitem_T *li;
9300 list_T *l;
9301 dictitem_T *di;
9302 dict_T *d;
9303 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009304
Bram Moolenaare9a41262005-01-15 22:18:47 +00009305 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009306 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009307 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009309 int error = FALSE;
9310
9311 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9312 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009313 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009314 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009315 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009316 else if (argvars[0].v_type == VAR_DICT)
9317 {
9318 if ((d = argvars[0].vval.v_dict) != NULL)
9319 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009320 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009321 if (di != NULL)
9322 tv = &di->di_tv;
9323 }
9324 }
9325 else
9326 EMSG2(_(e_listdictarg), "get()");
9327
9328 if (tv == NULL)
9329 {
9330 if (argvars[2].v_type == VAR_UNKNOWN)
9331 rettv->vval.v_number = 0;
9332 else
9333 copy_tv(&argvars[2], rettv);
9334 }
9335 else
9336 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009337}
9338
Bram Moolenaar342337a2005-07-21 21:11:17 +00009339static 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 +00009340
9341/*
9342 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009343 * Return a range (from start to end) of lines in rettv from the specified
9344 * buffer.
9345 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009346 */
9347 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009348get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009349 buf_T *buf;
9350 linenr_T start;
9351 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009352 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009353 typval_T *rettv;
9354{
9355 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009356 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009357
Bram Moolenaar342337a2005-07-21 21:11:17 +00009358 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009359 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009360 l = list_alloc();
9361 if (l == NULL)
9362 return;
9363
9364 rettv->vval.v_list = l;
9365 rettv->v_type = VAR_LIST;
9366 ++l->lv_refcount;
9367 }
9368 else
9369 rettv->vval.v_number = 0;
9370
9371 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9372 return;
9373
9374 if (!retlist)
9375 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009376 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9377 p = ml_get_buf(buf, start, FALSE);
9378 else
9379 p = (char_u *)"";
9380
9381 rettv->v_type = VAR_STRING;
9382 rettv->vval.v_string = vim_strsave(p);
9383 }
9384 else
9385 {
9386 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009387 return;
9388
9389 if (start < 1)
9390 start = 1;
9391 if (end > buf->b_ml.ml_line_count)
9392 end = buf->b_ml.ml_line_count;
9393 while (start <= end)
Bram Moolenaar4463f292005-09-25 22:20:24 +00009394 if (list_append_string(l, ml_get_buf(buf, start++, FALSE), -1)
9395 == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009396 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009397 }
9398}
9399
9400/*
9401 * "getbufline()" function
9402 */
9403 static void
9404f_getbufline(argvars, rettv)
9405 typval_T *argvars;
9406 typval_T *rettv;
9407{
9408 linenr_T lnum;
9409 linenr_T end;
9410 buf_T *buf;
9411
9412 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9413 ++emsg_off;
9414 buf = get_buf_tv(&argvars[0]);
9415 --emsg_off;
9416
Bram Moolenaar661b1822005-07-28 22:36:45 +00009417 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009418 if (argvars[2].v_type == VAR_UNKNOWN)
9419 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009420 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009421 end = get_tv_lnum_buf(&argvars[2], buf);
9422
Bram Moolenaar342337a2005-07-21 21:11:17 +00009423 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009424}
9425
Bram Moolenaar0d660222005-01-07 21:51:51 +00009426/*
9427 * "getbufvar()" function
9428 */
9429 static void
9430f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009431 typval_T *argvars;
9432 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009433{
9434 buf_T *buf;
9435 buf_T *save_curbuf;
9436 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009437 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009439 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9440 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009441 ++emsg_off;
9442 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009443
9444 rettv->v_type = VAR_STRING;
9445 rettv->vval.v_string = NULL;
9446
9447 if (buf != NULL && varname != NULL)
9448 {
9449 if (*varname == '&') /* buffer-local-option */
9450 {
9451 /* set curbuf to be our buf, temporarily */
9452 save_curbuf = curbuf;
9453 curbuf = buf;
9454
9455 get_option_tv(&varname, rettv, TRUE);
9456
9457 /* restore previous notion of curbuf */
9458 curbuf = save_curbuf;
9459 }
9460 else
9461 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009462 if (*varname == NUL)
9463 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9464 * scope prefix before the NUL byte is required by
9465 * find_var_in_ht(). */
9466 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009467 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009468 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009469 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009470 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009471 }
9472 }
9473
9474 --emsg_off;
9475}
9476
9477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 * "getchar()" function
9479 */
9480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009481f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009482 typval_T *argvars;
9483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484{
9485 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009486 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487
9488 ++no_mapping;
9489 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009490 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 /* getchar(): blocking wait. */
9492 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009493 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 /* getchar(1): only check if char avail */
9495 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009496 else if (error || vpeekc() == NUL)
9497 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 n = 0;
9499 else
9500 /* getchar(0) and char avail: return char */
9501 n = safe_vgetc();
9502 --no_mapping;
9503 --allow_keys;
9504
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009505 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 if (IS_SPECIAL(n) || mod_mask != 0)
9507 {
9508 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9509 int i = 0;
9510
9511 /* Turn a special key into three bytes, plus modifier. */
9512 if (mod_mask != 0)
9513 {
9514 temp[i++] = K_SPECIAL;
9515 temp[i++] = KS_MODIFIER;
9516 temp[i++] = mod_mask;
9517 }
9518 if (IS_SPECIAL(n))
9519 {
9520 temp[i++] = K_SPECIAL;
9521 temp[i++] = K_SECOND(n);
9522 temp[i++] = K_THIRD(n);
9523 }
9524#ifdef FEAT_MBYTE
9525 else if (has_mbyte)
9526 i += (*mb_char2bytes)(n, temp + i);
9527#endif
9528 else
9529 temp[i++] = n;
9530 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009531 rettv->v_type = VAR_STRING;
9532 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 }
9534}
9535
9536/*
9537 * "getcharmod()" function
9538 */
9539/*ARGSUSED*/
9540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009542 typval_T *argvars;
9543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546}
9547
9548/*
9549 * "getcmdline()" function
9550 */
9551/*ARGSUSED*/
9552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009553f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009554 typval_T *argvars;
9555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->v_type = VAR_STRING;
9558 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559}
9560
9561/*
9562 * "getcmdpos()" function
9563 */
9564/*ARGSUSED*/
9565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009567 typval_T *argvars;
9568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009570 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571}
9572
9573/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009574 * "getcmdtype()" function
9575 */
9576/*ARGSUSED*/
9577 static void
9578f_getcmdtype(argvars, rettv)
9579 typval_T *argvars;
9580 typval_T *rettv;
9581{
9582 rettv->v_type = VAR_STRING;
9583 rettv->vval.v_string = alloc(2);
9584 if (rettv->vval.v_string != NULL)
9585 {
9586 rettv->vval.v_string[0] = get_cmdline_type();
9587 rettv->vval.v_string[1] = NUL;
9588 }
9589}
9590
9591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 * "getcwd()" function
9593 */
9594/*ARGSUSED*/
9595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009597 typval_T *argvars;
9598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599{
9600 char_u cwd[MAXPATHL];
9601
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 else
9606 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009607 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009609 if (rettv->vval.v_string != NULL)
9610 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611#endif
9612 }
9613}
9614
9615/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009616 * "getfontname()" function
9617 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009618/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009621 typval_T *argvars;
9622 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009623{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624 rettv->v_type = VAR_STRING;
9625 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009626#ifdef FEAT_GUI
9627 if (gui.in_use)
9628 {
9629 GuiFont font;
9630 char_u *name = NULL;
9631
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009632 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009633 {
9634 /* Get the "Normal" font. Either the name saved by
9635 * hl_set_font_name() or from the font ID. */
9636 font = gui.norm_font;
9637 name = hl_get_font_name();
9638 }
9639 else
9640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009642 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9643 return;
9644 font = gui_mch_get_font(name, FALSE);
9645 if (font == NOFONT)
9646 return; /* Invalid font name, return empty string. */
9647 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009649 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009650 gui_mch_free_font(font);
9651 }
9652#endif
9653}
9654
9655/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009656 * "getfperm({fname})" function
9657 */
9658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009660 typval_T *argvars;
9661 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009662{
9663 char_u *fname;
9664 struct stat st;
9665 char_u *perm = NULL;
9666 char_u flags[] = "rwx";
9667 int i;
9668
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009670
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009672 if (mch_stat((char *)fname, &st) >= 0)
9673 {
9674 perm = vim_strsave((char_u *)"---------");
9675 if (perm != NULL)
9676 {
9677 for (i = 0; i < 9; i++)
9678 {
9679 if (st.st_mode & (1 << (8 - i)))
9680 perm[i] = flags[i % 3];
9681 }
9682 }
9683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009685}
9686
9687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 * "getfsize({fname})" function
9689 */
9690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009692 typval_T *argvars;
9693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694{
9695 char_u *fname;
9696 struct stat st;
9697
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009698 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701
9702 if (mch_stat((char *)fname, &st) >= 0)
9703 {
9704 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 }
9709 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009710 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711}
9712
9713/*
9714 * "getftime({fname})" function
9715 */
9716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009717f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009718 typval_T *argvars;
9719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
9721 char_u *fname;
9722 struct stat st;
9723
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725
9726 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730}
9731
9732/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009733 * "getftype({fname})" function
9734 */
9735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009737 typval_T *argvars;
9738 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009739{
9740 char_u *fname;
9741 struct stat st;
9742 char_u *type = NULL;
9743 char *t;
9744
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009746
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009747 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009748 if (mch_lstat((char *)fname, &st) >= 0)
9749 {
9750#ifdef S_ISREG
9751 if (S_ISREG(st.st_mode))
9752 t = "file";
9753 else if (S_ISDIR(st.st_mode))
9754 t = "dir";
9755# ifdef S_ISLNK
9756 else if (S_ISLNK(st.st_mode))
9757 t = "link";
9758# endif
9759# ifdef S_ISBLK
9760 else if (S_ISBLK(st.st_mode))
9761 t = "bdev";
9762# endif
9763# ifdef S_ISCHR
9764 else if (S_ISCHR(st.st_mode))
9765 t = "cdev";
9766# endif
9767# ifdef S_ISFIFO
9768 else if (S_ISFIFO(st.st_mode))
9769 t = "fifo";
9770# endif
9771# ifdef S_ISSOCK
9772 else if (S_ISSOCK(st.st_mode))
9773 t = "fifo";
9774# endif
9775 else
9776 t = "other";
9777#else
9778# ifdef S_IFMT
9779 switch (st.st_mode & S_IFMT)
9780 {
9781 case S_IFREG: t = "file"; break;
9782 case S_IFDIR: t = "dir"; break;
9783# ifdef S_IFLNK
9784 case S_IFLNK: t = "link"; break;
9785# endif
9786# ifdef S_IFBLK
9787 case S_IFBLK: t = "bdev"; break;
9788# endif
9789# ifdef S_IFCHR
9790 case S_IFCHR: t = "cdev"; break;
9791# endif
9792# ifdef S_IFIFO
9793 case S_IFIFO: t = "fifo"; break;
9794# endif
9795# ifdef S_IFSOCK
9796 case S_IFSOCK: t = "socket"; break;
9797# endif
9798 default: t = "other";
9799 }
9800# else
9801 if (mch_isdir(fname))
9802 t = "dir";
9803 else
9804 t = "file";
9805# endif
9806#endif
9807 type = vim_strsave((char_u *)t);
9808 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009810}
9811
9812/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009813 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009814 */
9815 static void
9816f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009817 typval_T *argvars;
9818 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009819{
9820 linenr_T lnum;
9821 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009822 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009823
9824 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009825 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009826 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009827 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009828 retlist = FALSE;
9829 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009830 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009831 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009832 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009833 retlist = TRUE;
9834 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009835
Bram Moolenaar342337a2005-07-21 21:11:17 +00009836 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009837}
9838
9839/*
Bram Moolenaar280f1262006-01-30 00:14:18 +00009840 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +00009841 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00009842/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +00009843 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +00009844f_getqflist(argvars, rettv)
9845 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009846 typval_T *rettv;
9847{
9848#ifdef FEAT_QUICKFIX
9849 list_T *l;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009850 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009851#endif
9852
9853 rettv->vval.v_number = FALSE;
9854#ifdef FEAT_QUICKFIX
9855 l = list_alloc();
9856 if (l != NULL)
9857 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009858 rettv->vval.v_list = l;
9859 rettv->v_type = VAR_LIST;
9860 ++l->lv_refcount;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009861 wp = NULL;
9862 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
9863 {
9864 wp = find_win_by_nr(&argvars[0]);
9865 if (wp == NULL)
9866 return;
9867 }
9868
Bram Moolenaar17c7c012006-01-26 22:25:15 +00009869 (void)get_errorlist(wp, l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009870 }
9871#endif
9872}
9873
9874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 * "getreg()" function
9876 */
9877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009878f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009879 typval_T *argvars;
9880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881{
9882 char_u *strregname;
9883 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009884 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009887 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009889 strregname = get_tv_string_chk(&argvars[0]);
9890 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009891 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009892 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009895 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 regname = (strregname == NULL ? '"' : *strregname);
9897 if (regname == 0)
9898 regname = '"';
9899
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009900 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009901 rettv->vval.v_string = error ? NULL :
9902 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903}
9904
9905/*
9906 * "getregtype()" function
9907 */
9908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009909f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009910 typval_T *argvars;
9911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912{
9913 char_u *strregname;
9914 int regname;
9915 char_u buf[NUMBUFLEN + 2];
9916 long reglen = 0;
9917
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009918 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009919 {
9920 strregname = get_tv_string_chk(&argvars[0]);
9921 if (strregname == NULL) /* type error; errmsg already given */
9922 {
9923 rettv->v_type = VAR_STRING;
9924 rettv->vval.v_string = NULL;
9925 return;
9926 }
9927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 else
9929 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009930 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931
9932 regname = (strregname == NULL ? '"' : *strregname);
9933 if (regname == 0)
9934 regname = '"';
9935
9936 buf[0] = NUL;
9937 buf[1] = NUL;
9938 switch (get_reg_type(regname, &reglen))
9939 {
9940 case MLINE: buf[0] = 'V'; break;
9941 case MCHAR: buf[0] = 'v'; break;
9942#ifdef FEAT_VISUAL
9943 case MBLOCK:
9944 buf[0] = Ctrl_V;
9945 sprintf((char *)buf + 1, "%ld", reglen + 1);
9946 break;
9947#endif
9948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009949 rettv->v_type = VAR_STRING;
9950 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951}
9952
9953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 * "getwinposx()" function
9955 */
9956/*ARGSUSED*/
9957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009958f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009959 typval_T *argvars;
9960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009962 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963#ifdef FEAT_GUI
9964 if (gui.in_use)
9965 {
9966 int x, y;
9967
9968 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009969 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 }
9971#endif
9972}
9973
9974/*
9975 * "getwinposy()" function
9976 */
9977/*ARGSUSED*/
9978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009980 typval_T *argvars;
9981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984#ifdef FEAT_GUI
9985 if (gui.in_use)
9986 {
9987 int x, y;
9988
9989 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 }
9992#endif
9993}
9994
Bram Moolenaara40058a2005-07-11 22:42:07 +00009995 static win_T *
9996find_win_by_nr(vp)
9997 typval_T *vp;
9998{
9999#ifdef FEAT_WINDOWS
10000 win_T *wp;
10001#endif
10002 int nr;
10003
10004 nr = get_tv_number_chk(vp, NULL);
10005
10006#ifdef FEAT_WINDOWS
10007 if (nr < 0)
10008 return NULL;
10009 if (nr == 0)
10010 return curwin;
10011
10012 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10013 if (--nr <= 0)
10014 break;
10015 return wp;
10016#else
10017 if (nr == 0 || nr == 1)
10018 return curwin;
10019 return NULL;
10020#endif
10021}
10022
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023/*
10024 * "getwinvar()" function
10025 */
10026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010028 typval_T *argvars;
10029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030{
10031 win_T *win, *oldcurwin;
10032 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010033 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010036 varname = get_tv_string_chk(&argvars[1]);
10037 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039 rettv->v_type = VAR_STRING;
10040 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041
10042 if (win != NULL && varname != NULL)
10043 {
10044 if (*varname == '&') /* window-local-option */
10045 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010046 /* Set curwin to be our win, temporarily. Also set curbuf, so
10047 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 oldcurwin = curwin;
10049 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010050 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053
10054 /* restore previous notion of curwin */
10055 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010056 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 }
10058 else
10059 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010060 if (*varname == NUL)
10061 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10062 * scope prefix before the NUL byte is required by
10063 * find_var_in_ht(). */
10064 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010066 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010068 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 }
10070 }
10071
10072 --emsg_off;
10073}
10074
10075/*
10076 * "glob()" function
10077 */
10078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010080 typval_T *argvars;
10081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082{
10083 expand_T xpc;
10084
10085 ExpandInit(&xpc);
10086 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087 rettv->v_type = VAR_STRING;
10088 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10090 ExpandCleanup(&xpc);
10091}
10092
10093/*
10094 * "globpath()" function
10095 */
10096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010097f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010098 typval_T *argvars;
10099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100{
10101 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010105 if (file == NULL)
10106 rettv->vval.v_string = NULL;
10107 else
10108 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109}
10110
10111/*
10112 * "has()" function
10113 */
10114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010116 typval_T *argvars;
10117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118{
10119 int i;
10120 char_u *name;
10121 int n = FALSE;
10122 static char *(has_list[]) =
10123 {
10124#ifdef AMIGA
10125 "amiga",
10126# ifdef FEAT_ARP
10127 "arp",
10128# endif
10129#endif
10130#ifdef __BEOS__
10131 "beos",
10132#endif
10133#ifdef MSDOS
10134# ifdef DJGPP
10135 "dos32",
10136# else
10137 "dos16",
10138# endif
10139#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010140#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 "mac",
10142#endif
10143#if defined(MACOS_X_UNIX)
10144 "macunix",
10145#endif
10146#ifdef OS2
10147 "os2",
10148#endif
10149#ifdef __QNX__
10150 "qnx",
10151#endif
10152#ifdef RISCOS
10153 "riscos",
10154#endif
10155#ifdef UNIX
10156 "unix",
10157#endif
10158#ifdef VMS
10159 "vms",
10160#endif
10161#ifdef WIN16
10162 "win16",
10163#endif
10164#ifdef WIN32
10165 "win32",
10166#endif
10167#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10168 "win32unix",
10169#endif
10170#ifdef WIN64
10171 "win64",
10172#endif
10173#ifdef EBCDIC
10174 "ebcdic",
10175#endif
10176#ifndef CASE_INSENSITIVE_FILENAME
10177 "fname_case",
10178#endif
10179#ifdef FEAT_ARABIC
10180 "arabic",
10181#endif
10182#ifdef FEAT_AUTOCMD
10183 "autocmd",
10184#endif
10185#ifdef FEAT_BEVAL
10186 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010187# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10188 "balloon_multiline",
10189# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190#endif
10191#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10192 "builtin_terms",
10193# ifdef ALL_BUILTIN_TCAPS
10194 "all_builtin_terms",
10195# endif
10196#endif
10197#ifdef FEAT_BYTEOFF
10198 "byte_offset",
10199#endif
10200#ifdef FEAT_CINDENT
10201 "cindent",
10202#endif
10203#ifdef FEAT_CLIENTSERVER
10204 "clientserver",
10205#endif
10206#ifdef FEAT_CLIPBOARD
10207 "clipboard",
10208#endif
10209#ifdef FEAT_CMDL_COMPL
10210 "cmdline_compl",
10211#endif
10212#ifdef FEAT_CMDHIST
10213 "cmdline_hist",
10214#endif
10215#ifdef FEAT_COMMENTS
10216 "comments",
10217#endif
10218#ifdef FEAT_CRYPT
10219 "cryptv",
10220#endif
10221#ifdef FEAT_CSCOPE
10222 "cscope",
10223#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010224#ifdef CURSOR_SHAPE
10225 "cursorshape",
10226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227#ifdef DEBUG
10228 "debug",
10229#endif
10230#ifdef FEAT_CON_DIALOG
10231 "dialog_con",
10232#endif
10233#ifdef FEAT_GUI_DIALOG
10234 "dialog_gui",
10235#endif
10236#ifdef FEAT_DIFF
10237 "diff",
10238#endif
10239#ifdef FEAT_DIGRAPHS
10240 "digraphs",
10241#endif
10242#ifdef FEAT_DND
10243 "dnd",
10244#endif
10245#ifdef FEAT_EMACS_TAGS
10246 "emacs_tags",
10247#endif
10248 "eval", /* always present, of course! */
10249#ifdef FEAT_EX_EXTRA
10250 "ex_extra",
10251#endif
10252#ifdef FEAT_SEARCH_EXTRA
10253 "extra_search",
10254#endif
10255#ifdef FEAT_FKMAP
10256 "farsi",
10257#endif
10258#ifdef FEAT_SEARCHPATH
10259 "file_in_path",
10260#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010261#if defined(UNIX) && !defined(USE_SYSTEM)
10262 "filterpipe",
10263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264#ifdef FEAT_FIND_ID
10265 "find_in_path",
10266#endif
10267#ifdef FEAT_FOLDING
10268 "folding",
10269#endif
10270#ifdef FEAT_FOOTER
10271 "footer",
10272#endif
10273#if !defined(USE_SYSTEM) && defined(UNIX)
10274 "fork",
10275#endif
10276#ifdef FEAT_GETTEXT
10277 "gettext",
10278#endif
10279#ifdef FEAT_GUI
10280 "gui",
10281#endif
10282#ifdef FEAT_GUI_ATHENA
10283# ifdef FEAT_GUI_NEXTAW
10284 "gui_neXtaw",
10285# else
10286 "gui_athena",
10287# endif
10288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289#ifdef FEAT_GUI_GTK
10290 "gui_gtk",
10291# ifdef HAVE_GTK2
10292 "gui_gtk2",
10293# endif
10294#endif
10295#ifdef FEAT_GUI_MAC
10296 "gui_mac",
10297#endif
10298#ifdef FEAT_GUI_MOTIF
10299 "gui_motif",
10300#endif
10301#ifdef FEAT_GUI_PHOTON
10302 "gui_photon",
10303#endif
10304#ifdef FEAT_GUI_W16
10305 "gui_win16",
10306#endif
10307#ifdef FEAT_GUI_W32
10308 "gui_win32",
10309#endif
10310#ifdef FEAT_HANGULIN
10311 "hangul_input",
10312#endif
10313#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10314 "iconv",
10315#endif
10316#ifdef FEAT_INS_EXPAND
10317 "insert_expand",
10318#endif
10319#ifdef FEAT_JUMPLIST
10320 "jumplist",
10321#endif
10322#ifdef FEAT_KEYMAP
10323 "keymap",
10324#endif
10325#ifdef FEAT_LANGMAP
10326 "langmap",
10327#endif
10328#ifdef FEAT_LIBCALL
10329 "libcall",
10330#endif
10331#ifdef FEAT_LINEBREAK
10332 "linebreak",
10333#endif
10334#ifdef FEAT_LISP
10335 "lispindent",
10336#endif
10337#ifdef FEAT_LISTCMDS
10338 "listcmds",
10339#endif
10340#ifdef FEAT_LOCALMAP
10341 "localmap",
10342#endif
10343#ifdef FEAT_MENU
10344 "menu",
10345#endif
10346#ifdef FEAT_SESSION
10347 "mksession",
10348#endif
10349#ifdef FEAT_MODIFY_FNAME
10350 "modify_fname",
10351#endif
10352#ifdef FEAT_MOUSE
10353 "mouse",
10354#endif
10355#ifdef FEAT_MOUSESHAPE
10356 "mouseshape",
10357#endif
10358#if defined(UNIX) || defined(VMS)
10359# ifdef FEAT_MOUSE_DEC
10360 "mouse_dec",
10361# endif
10362# ifdef FEAT_MOUSE_GPM
10363 "mouse_gpm",
10364# endif
10365# ifdef FEAT_MOUSE_JSB
10366 "mouse_jsbterm",
10367# endif
10368# ifdef FEAT_MOUSE_NET
10369 "mouse_netterm",
10370# endif
10371# ifdef FEAT_MOUSE_PTERM
10372 "mouse_pterm",
10373# endif
10374# ifdef FEAT_MOUSE_XTERM
10375 "mouse_xterm",
10376# endif
10377#endif
10378#ifdef FEAT_MBYTE
10379 "multi_byte",
10380#endif
10381#ifdef FEAT_MBYTE_IME
10382 "multi_byte_ime",
10383#endif
10384#ifdef FEAT_MULTI_LANG
10385 "multi_lang",
10386#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010387#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010388#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010389 "mzscheme",
10390#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392#ifdef FEAT_OLE
10393 "ole",
10394#endif
10395#ifdef FEAT_OSFILETYPE
10396 "osfiletype",
10397#endif
10398#ifdef FEAT_PATH_EXTRA
10399 "path_extra",
10400#endif
10401#ifdef FEAT_PERL
10402#ifndef DYNAMIC_PERL
10403 "perl",
10404#endif
10405#endif
10406#ifdef FEAT_PYTHON
10407#ifndef DYNAMIC_PYTHON
10408 "python",
10409#endif
10410#endif
10411#ifdef FEAT_POSTSCRIPT
10412 "postscript",
10413#endif
10414#ifdef FEAT_PRINTER
10415 "printer",
10416#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010417#ifdef FEAT_PROFILE
10418 "profile",
10419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420#ifdef FEAT_QUICKFIX
10421 "quickfix",
10422#endif
10423#ifdef FEAT_RIGHTLEFT
10424 "rightleft",
10425#endif
10426#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10427 "ruby",
10428#endif
10429#ifdef FEAT_SCROLLBIND
10430 "scrollbind",
10431#endif
10432#ifdef FEAT_CMDL_INFO
10433 "showcmd",
10434 "cmdline_info",
10435#endif
10436#ifdef FEAT_SIGNS
10437 "signs",
10438#endif
10439#ifdef FEAT_SMARTINDENT
10440 "smartindent",
10441#endif
10442#ifdef FEAT_SNIFF
10443 "sniff",
10444#endif
10445#ifdef FEAT_STL_OPT
10446 "statusline",
10447#endif
10448#ifdef FEAT_SUN_WORKSHOP
10449 "sun_workshop",
10450#endif
10451#ifdef FEAT_NETBEANS_INTG
10452 "netbeans_intg",
10453#endif
10454#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010455 "spell",
10456#endif
10457#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 "syntax",
10459#endif
10460#if defined(USE_SYSTEM) || !defined(UNIX)
10461 "system",
10462#endif
10463#ifdef FEAT_TAG_BINS
10464 "tag_binary",
10465#endif
10466#ifdef FEAT_TAG_OLDSTATIC
10467 "tag_old_static",
10468#endif
10469#ifdef FEAT_TAG_ANYWHITE
10470 "tag_any_white",
10471#endif
10472#ifdef FEAT_TCL
10473# ifndef DYNAMIC_TCL
10474 "tcl",
10475# endif
10476#endif
10477#ifdef TERMINFO
10478 "terminfo",
10479#endif
10480#ifdef FEAT_TERMRESPONSE
10481 "termresponse",
10482#endif
10483#ifdef FEAT_TEXTOBJ
10484 "textobjects",
10485#endif
10486#ifdef HAVE_TGETENT
10487 "tgetent",
10488#endif
10489#ifdef FEAT_TITLE
10490 "title",
10491#endif
10492#ifdef FEAT_TOOLBAR
10493 "toolbar",
10494#endif
10495#ifdef FEAT_USR_CMDS
10496 "user-commands", /* was accidentally included in 5.4 */
10497 "user_commands",
10498#endif
10499#ifdef FEAT_VIMINFO
10500 "viminfo",
10501#endif
10502#ifdef FEAT_VERTSPLIT
10503 "vertsplit",
10504#endif
10505#ifdef FEAT_VIRTUALEDIT
10506 "virtualedit",
10507#endif
10508#ifdef FEAT_VISUAL
10509 "visual",
10510#endif
10511#ifdef FEAT_VISUALEXTRA
10512 "visualextra",
10513#endif
10514#ifdef FEAT_VREPLACE
10515 "vreplace",
10516#endif
10517#ifdef FEAT_WILDIGN
10518 "wildignore",
10519#endif
10520#ifdef FEAT_WILDMENU
10521 "wildmenu",
10522#endif
10523#ifdef FEAT_WINDOWS
10524 "windows",
10525#endif
10526#ifdef FEAT_WAK
10527 "winaltkeys",
10528#endif
10529#ifdef FEAT_WRITEBACKUP
10530 "writebackup",
10531#endif
10532#ifdef FEAT_XIM
10533 "xim",
10534#endif
10535#ifdef FEAT_XFONTSET
10536 "xfontset",
10537#endif
10538#ifdef USE_XSMP
10539 "xsmp",
10540#endif
10541#ifdef USE_XSMP_INTERACT
10542 "xsmp_interact",
10543#endif
10544#ifdef FEAT_XCLIPBOARD
10545 "xterm_clipboard",
10546#endif
10547#ifdef FEAT_XTERM_SAVE
10548 "xterm_save",
10549#endif
10550#if defined(UNIX) && defined(FEAT_X11)
10551 "X11",
10552#endif
10553 NULL
10554 };
10555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 for (i = 0; has_list[i] != NULL; ++i)
10558 if (STRICMP(name, has_list[i]) == 0)
10559 {
10560 n = TRUE;
10561 break;
10562 }
10563
10564 if (n == FALSE)
10565 {
10566 if (STRNICMP(name, "patch", 5) == 0)
10567 n = has_patch(atoi((char *)name + 5));
10568 else if (STRICMP(name, "vim_starting") == 0)
10569 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010570#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10571 else if (STRICMP(name, "balloon_multiline") == 0)
10572 n = multiline_balloon_available();
10573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574#ifdef DYNAMIC_TCL
10575 else if (STRICMP(name, "tcl") == 0)
10576 n = tcl_enabled(FALSE);
10577#endif
10578#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10579 else if (STRICMP(name, "iconv") == 0)
10580 n = iconv_enabled(FALSE);
10581#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010582#ifdef DYNAMIC_MZSCHEME
10583 else if (STRICMP(name, "mzscheme") == 0)
10584 n = mzscheme_enabled(FALSE);
10585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586#ifdef DYNAMIC_RUBY
10587 else if (STRICMP(name, "ruby") == 0)
10588 n = ruby_enabled(FALSE);
10589#endif
10590#ifdef DYNAMIC_PYTHON
10591 else if (STRICMP(name, "python") == 0)
10592 n = python_enabled(FALSE);
10593#endif
10594#ifdef DYNAMIC_PERL
10595 else if (STRICMP(name, "perl") == 0)
10596 n = perl_enabled(FALSE);
10597#endif
10598#ifdef FEAT_GUI
10599 else if (STRICMP(name, "gui_running") == 0)
10600 n = (gui.in_use || gui.starting);
10601# ifdef FEAT_GUI_W32
10602 else if (STRICMP(name, "gui_win32s") == 0)
10603 n = gui_is_win32s();
10604# endif
10605# ifdef FEAT_BROWSE
10606 else if (STRICMP(name, "browse") == 0)
10607 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10608# endif
10609#endif
10610#ifdef FEAT_SYN_HL
10611 else if (STRICMP(name, "syntax_items") == 0)
10612 n = syntax_present(curbuf);
10613#endif
10614#if defined(WIN3264)
10615 else if (STRICMP(name, "win95") == 0)
10616 n = mch_windows95();
10617#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010618#ifdef FEAT_NETBEANS_INTG
10619 else if (STRICMP(name, "netbeans_enabled") == 0)
10620 n = usingNetbeans;
10621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 }
10623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010624 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625}
10626
10627/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010628 * "has_key()" function
10629 */
10630 static void
10631f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010632 typval_T *argvars;
10633 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010634{
10635 rettv->vval.v_number = 0;
10636 if (argvars[0].v_type != VAR_DICT)
10637 {
10638 EMSG(_(e_dictreq));
10639 return;
10640 }
10641 if (argvars[0].vval.v_dict == NULL)
10642 return;
10643
10644 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010645 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010646}
10647
10648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 * "hasmapto()" function
10650 */
10651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010652f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010653 typval_T *argvars;
10654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655{
10656 char_u *name;
10657 char_u *mode;
10658 char_u buf[NUMBUFLEN];
10659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010660 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010661 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 mode = (char_u *)"nvo";
10663 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010664 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665
10666 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010667 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010669 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670}
10671
10672/*
10673 * "histadd()" function
10674 */
10675/*ARGSUSED*/
10676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010677f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010678 typval_T *argvars;
10679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680{
10681#ifdef FEAT_CMDHIST
10682 int histype;
10683 char_u *str;
10684 char_u buf[NUMBUFLEN];
10685#endif
10686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010687 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 if (check_restricted() || check_secure())
10689 return;
10690#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010691 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10692 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 if (histype >= 0)
10694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 if (*str != NUL)
10697 {
10698 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010699 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 return;
10701 }
10702 }
10703#endif
10704}
10705
10706/*
10707 * "histdel()" function
10708 */
10709/*ARGSUSED*/
10710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010711f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010712 typval_T *argvars;
10713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714{
10715#ifdef FEAT_CMDHIST
10716 int n;
10717 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010718 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010720 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10721 if (str == NULL)
10722 n = 0;
10723 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010725 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010726 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010728 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010729 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 else
10731 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010732 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733 get_tv_string_buf(&argvars[1], buf));
10734 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010736 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737#endif
10738}
10739
10740/*
10741 * "histget()" function
10742 */
10743/*ARGSUSED*/
10744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010745f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010746 typval_T *argvars;
10747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748{
10749#ifdef FEAT_CMDHIST
10750 int type;
10751 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010752 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010754 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10755 if (str == NULL)
10756 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010758 {
10759 type = get_histtype(str);
10760 if (argvars[1].v_type == VAR_UNKNOWN)
10761 idx = get_history_idx(type);
10762 else
10763 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10764 /* -1 on type error */
10765 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010768 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771}
10772
10773/*
10774 * "histnr()" function
10775 */
10776/*ARGSUSED*/
10777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010778f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010779 typval_T *argvars;
10780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781{
10782 int i;
10783
10784#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010785 char_u *history = get_tv_string_chk(&argvars[0]);
10786
10787 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 if (i >= HIST_CMD && i < HIST_COUNT)
10789 i = get_history_idx(i);
10790 else
10791#endif
10792 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010793 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794}
10795
10796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 * "highlightID(name)" function
10798 */
10799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010800f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010801 typval_T *argvars;
10802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010804 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805}
10806
10807/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010808 * "highlight_exists()" function
10809 */
10810 static void
10811f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010812 typval_T *argvars;
10813 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010814{
10815 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10816}
10817
10818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 * "hostname()" function
10820 */
10821/*ARGSUSED*/
10822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010823f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010824 typval_T *argvars;
10825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826{
10827 char_u hostname[256];
10828
10829 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010830 rettv->v_type = VAR_STRING;
10831 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832}
10833
10834/*
10835 * iconv() function
10836 */
10837/*ARGSUSED*/
10838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010839f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010840 typval_T *argvars;
10841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842{
10843#ifdef FEAT_MBYTE
10844 char_u buf1[NUMBUFLEN];
10845 char_u buf2[NUMBUFLEN];
10846 char_u *from, *to, *str;
10847 vimconv_T vimconv;
10848#endif
10849
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010850 rettv->v_type = VAR_STRING;
10851 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852
10853#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010854 str = get_tv_string(&argvars[0]);
10855 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10856 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 vimconv.vc_type = CONV_NONE;
10858 convert_setup(&vimconv, from, to);
10859
10860 /* If the encodings are equal, no conversion needed. */
10861 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010862 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010864 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865
10866 convert_setup(&vimconv, NULL, NULL);
10867 vim_free(from);
10868 vim_free(to);
10869#endif
10870}
10871
10872/*
10873 * "indent()" function
10874 */
10875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010876f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010877 typval_T *argvars;
10878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879{
10880 linenr_T lnum;
10881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010882 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010886 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887}
10888
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010889/*
10890 * "index()" function
10891 */
10892 static void
10893f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010894 typval_T *argvars;
10895 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010896{
Bram Moolenaar33570922005-01-25 22:26:29 +000010897 list_T *l;
10898 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010899 long idx = 0;
10900 int ic = FALSE;
10901
10902 rettv->vval.v_number = -1;
10903 if (argvars[0].v_type != VAR_LIST)
10904 {
10905 EMSG(_(e_listreq));
10906 return;
10907 }
10908 l = argvars[0].vval.v_list;
10909 if (l != NULL)
10910 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010911 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010912 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010913 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010914 int error = FALSE;
10915
Bram Moolenaar758711c2005-02-02 23:11:38 +000010916 /* Start at specified item. Use the cached index that list_find()
10917 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010918 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010919 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010920 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010921 ic = get_tv_number_chk(&argvars[3], &error);
10922 if (error)
10923 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010924 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010925
Bram Moolenaar758711c2005-02-02 23:11:38 +000010926 for ( ; item != NULL; item = item->li_next, ++idx)
10927 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010928 {
10929 rettv->vval.v_number = idx;
10930 break;
10931 }
10932 }
10933}
10934
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935static int inputsecret_flag = 0;
10936
10937/*
10938 * "input()" function
10939 * Also handles inputsecret() when inputsecret is set.
10940 */
10941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010942f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010943 typval_T *argvars;
10944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010946 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 char_u *p = NULL;
10948 int c;
10949 char_u buf[NUMBUFLEN];
10950 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010951 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010952 int xp_type = EXPAND_NOTHING;
10953 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010955 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956
10957#ifdef NO_CONSOLE_INPUT
10958 /* While starting up, there is no place to enter text. */
10959 if (no_console_input())
10960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010961 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 return;
10963 }
10964#endif
10965
10966 cmd_silent = FALSE; /* Want to see the prompt. */
10967 if (prompt != NULL)
10968 {
10969 /* Only the part of the message after the last NL is considered as
10970 * prompt for the command line */
10971 p = vim_strrchr(prompt, '\n');
10972 if (p == NULL)
10973 p = prompt;
10974 else
10975 {
10976 ++p;
10977 c = *p;
10978 *p = NUL;
10979 msg_start();
10980 msg_clr_eos();
10981 msg_puts_attr(prompt, echo_attr);
10982 msg_didout = FALSE;
10983 msg_starthere();
10984 *p = c;
10985 }
10986 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010988 if (argvars[1].v_type != VAR_UNKNOWN)
10989 {
10990 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10991 if (defstr != NULL)
10992 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993
Bram Moolenaar4463f292005-09-25 22:20:24 +000010994 if (argvars[2].v_type != VAR_UNKNOWN)
10995 {
10996 char_u *xp_name;
10997 int xp_namelen;
10998 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010999
Bram Moolenaar4463f292005-09-25 22:20:24 +000011000 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011001
Bram Moolenaar4463f292005-09-25 22:20:24 +000011002 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11003 if (xp_name == NULL)
11004 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011005
Bram Moolenaar4463f292005-09-25 22:20:24 +000011006 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011007
Bram Moolenaar4463f292005-09-25 22:20:24 +000011008 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11009 &xp_arg) == FAIL)
11010 return;
11011 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011012 }
11013
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011014 if (defstr != NULL)
11015 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011016 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11017 xp_type, xp_arg);
11018
11019 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011021 /* since the user typed this, no need to wait for return */
11022 need_wait_return = FALSE;
11023 msg_didout = FALSE;
11024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 cmd_silent = cmd_silent_save;
11026}
11027
11028/*
11029 * "inputdialog()" function
11030 */
11031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011032f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011033 typval_T *argvars;
11034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035{
11036#if defined(FEAT_GUI_TEXTDIALOG)
11037 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11038 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11039 {
11040 char_u *message;
11041 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011042 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011044 message = get_tv_string_chk(&argvars[0]);
11045 if (argvars[1].v_type != VAR_UNKNOWN
11046 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011047 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 else
11049 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011050 if (message != NULL && defstr != NULL
11051 && do_dialog(VIM_QUESTION, NULL, message,
11052 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011053 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 else
11055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011056 if (message != NULL && defstr != NULL
11057 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011058 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 rettv->vval.v_string = vim_strsave(
11060 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 }
11066 else
11067#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011068 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069}
11070
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011071/*
11072 * "inputlist()" function
11073 */
11074 static void
11075f_inputlist(argvars, rettv)
11076 typval_T *argvars;
11077 typval_T *rettv;
11078{
11079 listitem_T *li;
11080 int selected;
11081 int mouse_used;
11082
11083 rettv->vval.v_number = 0;
11084#ifdef NO_CONSOLE_INPUT
11085 /* While starting up, there is no place to enter text. */
11086 if (no_console_input())
11087 return;
11088#endif
11089 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11090 {
11091 EMSG2(_(e_listarg), "inputlist()");
11092 return;
11093 }
11094
11095 msg_start();
11096 lines_left = Rows; /* avoid more prompt */
11097 msg_scroll = TRUE;
11098 msg_clr_eos();
11099
11100 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11101 {
11102 msg_puts(get_tv_string(&li->li_tv));
11103 msg_putchar('\n');
11104 }
11105
11106 /* Ask for choice. */
11107 selected = prompt_for_number(&mouse_used);
11108 if (mouse_used)
11109 selected -= lines_left;
11110
11111 rettv->vval.v_number = selected;
11112}
11113
11114
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11116
11117/*
11118 * "inputrestore()" function
11119 */
11120/*ARGSUSED*/
11121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011122f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011123 typval_T *argvars;
11124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125{
11126 if (ga_userinput.ga_len > 0)
11127 {
11128 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11130 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011131 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 }
11133 else if (p_verbose > 1)
11134 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011135 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011136 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 }
11138}
11139
11140/*
11141 * "inputsave()" function
11142 */
11143/*ARGSUSED*/
11144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011145f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011146 typval_T *argvars;
11147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148{
11149 /* Add an entry to the stack of typehead storage. */
11150 if (ga_grow(&ga_userinput, 1) == OK)
11151 {
11152 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11153 + ga_userinput.ga_len);
11154 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 }
11157 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159}
11160
11161/*
11162 * "inputsecret()" function
11163 */
11164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011165f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011166 typval_T *argvars;
11167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168{
11169 ++cmdline_star;
11170 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 --cmdline_star;
11173 --inputsecret_flag;
11174}
11175
11176/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011177 * "insert()" function
11178 */
11179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011180f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011181 typval_T *argvars;
11182 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011183{
11184 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011185 listitem_T *item;
11186 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011188
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011189 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011190 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011191 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011192 else if ((l = argvars[0].vval.v_list) != NULL
11193 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011194 {
11195 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011196 before = get_tv_number_chk(&argvars[2], &error);
11197 if (error)
11198 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011199
Bram Moolenaar758711c2005-02-02 23:11:38 +000011200 if (before == l->lv_len)
11201 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011202 else
11203 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011204 item = list_find(l, before);
11205 if (item == NULL)
11206 {
11207 EMSGN(_(e_listidx), before);
11208 l = NULL;
11209 }
11210 }
11211 if (l != NULL)
11212 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011213 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011214 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011215 }
11216 }
11217}
11218
11219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 * "isdirectory()" function
11221 */
11222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011223f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011224 typval_T *argvars;
11225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011227 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228}
11229
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011230/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011231 * "islocked()" function
11232 */
11233 static void
11234f_islocked(argvars, rettv)
11235 typval_T *argvars;
11236 typval_T *rettv;
11237{
11238 lval_T lv;
11239 char_u *end;
11240 dictitem_T *di;
11241
11242 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011243 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11244 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011245 if (end != NULL && lv.ll_name != NULL)
11246 {
11247 if (*end != NUL)
11248 EMSG(_(e_trailing));
11249 else
11250 {
11251 if (lv.ll_tv == NULL)
11252 {
11253 if (check_changedtick(lv.ll_name))
11254 rettv->vval.v_number = 1; /* always locked */
11255 else
11256 {
11257 di = find_var(lv.ll_name, NULL);
11258 if (di != NULL)
11259 {
11260 /* Consider a variable locked when:
11261 * 1. the variable itself is locked
11262 * 2. the value of the variable is locked.
11263 * 3. the List or Dict value is locked.
11264 */
11265 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11266 || tv_islocked(&di->di_tv));
11267 }
11268 }
11269 }
11270 else if (lv.ll_range)
11271 EMSG(_("E745: Range not allowed"));
11272 else if (lv.ll_newkey != NULL)
11273 EMSG2(_(e_dictkey), lv.ll_newkey);
11274 else if (lv.ll_list != NULL)
11275 /* List item. */
11276 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11277 else
11278 /* Dictionary item. */
11279 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11280 }
11281 }
11282
11283 clear_lval(&lv);
11284}
11285
Bram Moolenaar33570922005-01-25 22:26:29 +000011286static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011287
11288/*
11289 * Turn a dict into a list:
11290 * "what" == 0: list of keys
11291 * "what" == 1: list of values
11292 * "what" == 2: list of items
11293 */
11294 static void
11295dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 typval_T *argvars;
11297 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011298 int what;
11299{
Bram Moolenaar33570922005-01-25 22:26:29 +000011300 list_T *l;
11301 list_T *l2;
11302 dictitem_T *di;
11303 hashitem_T *hi;
11304 listitem_T *li;
11305 listitem_T *li2;
11306 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011307 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011308
11309 rettv->vval.v_number = 0;
11310 if (argvars[0].v_type != VAR_DICT)
11311 {
11312 EMSG(_(e_dictreq));
11313 return;
11314 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011315 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011316 return;
11317
11318 l = list_alloc();
11319 if (l == NULL)
11320 return;
11321 rettv->v_type = VAR_LIST;
11322 rettv->vval.v_list = l;
11323 ++l->lv_refcount;
11324
Bram Moolenaar33570922005-01-25 22:26:29 +000011325 todo = d->dv_hashtab.ht_used;
11326 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011327 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011328 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011329 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011330 --todo;
11331 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011332
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011333 li = listitem_alloc();
11334 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011335 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011336 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011337
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011338 if (what == 0)
11339 {
11340 /* keys() */
11341 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011342 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011343 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11344 }
11345 else if (what == 1)
11346 {
11347 /* values() */
11348 copy_tv(&di->di_tv, &li->li_tv);
11349 }
11350 else
11351 {
11352 /* items() */
11353 l2 = list_alloc();
11354 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011355 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011356 li->li_tv.vval.v_list = l2;
11357 if (l2 == NULL)
11358 break;
11359 ++l2->lv_refcount;
11360
11361 li2 = listitem_alloc();
11362 if (li2 == NULL)
11363 break;
11364 list_append(l2, li2);
11365 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011366 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011367 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11368
11369 li2 = listitem_alloc();
11370 if (li2 == NULL)
11371 break;
11372 list_append(l2, li2);
11373 copy_tv(&di->di_tv, &li2->li_tv);
11374 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011375 }
11376 }
11377}
11378
11379/*
11380 * "items(dict)" function
11381 */
11382 static void
11383f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011384 typval_T *argvars;
11385 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011386{
11387 dict_list(argvars, rettv, 2);
11388}
11389
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011391 * "join()" function
11392 */
11393 static void
11394f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011395 typval_T *argvars;
11396 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011397{
11398 garray_T ga;
11399 char_u *sep;
11400
11401 rettv->vval.v_number = 0;
11402 if (argvars[0].v_type != VAR_LIST)
11403 {
11404 EMSG(_(e_listreq));
11405 return;
11406 }
11407 if (argvars[0].vval.v_list == NULL)
11408 return;
11409 if (argvars[1].v_type == VAR_UNKNOWN)
11410 sep = (char_u *)" ";
11411 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011412 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011413
11414 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011415
11416 if (sep != NULL)
11417 {
11418 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011419 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011420 ga_append(&ga, NUL);
11421 rettv->vval.v_string = (char_u *)ga.ga_data;
11422 }
11423 else
11424 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011425}
11426
11427/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011428 * "keys()" function
11429 */
11430 static void
11431f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011432 typval_T *argvars;
11433 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011434{
11435 dict_list(argvars, rettv, 0);
11436}
11437
11438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 * "last_buffer_nr()" function.
11440 */
11441/*ARGSUSED*/
11442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011444 typval_T *argvars;
11445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446{
11447 int n = 0;
11448 buf_T *buf;
11449
11450 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11451 if (n < buf->b_fnum)
11452 n = buf->b_fnum;
11453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011455}
11456
11457/*
11458 * "len()" function
11459 */
11460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011462 typval_T *argvars;
11463 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011464{
11465 switch (argvars[0].v_type)
11466 {
11467 case VAR_STRING:
11468 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469 rettv->vval.v_number = (varnumber_T)STRLEN(
11470 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011471 break;
11472 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011474 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011475 case VAR_DICT:
11476 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11477 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011478 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011479 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011480 break;
11481 }
11482}
11483
Bram Moolenaar33570922005-01-25 22:26:29 +000011484static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011485
11486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011488 typval_T *argvars;
11489 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011490 int type;
11491{
11492#ifdef FEAT_LIBCALL
11493 char_u *string_in;
11494 char_u **string_result;
11495 int nr_result;
11496#endif
11497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011499 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011501 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011503
11504 if (check_restricted() || check_secure())
11505 return;
11506
11507#ifdef FEAT_LIBCALL
11508 /* The first two args must be strings, otherwise its meaningless */
11509 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11510 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011511 string_in = NULL;
11512 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011513 string_in = argvars[2].vval.v_string;
11514 if (type == VAR_NUMBER)
11515 string_result = NULL;
11516 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011518 if (mch_libcall(argvars[0].vval.v_string,
11519 argvars[1].vval.v_string,
11520 string_in,
11521 argvars[2].vval.v_number,
11522 string_result,
11523 &nr_result) == OK
11524 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011525 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011526 }
11527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528}
11529
11530/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011531 * "libcall()" function
11532 */
11533 static void
11534f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011535 typval_T *argvars;
11536 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011537{
11538 libcall_common(argvars, rettv, VAR_STRING);
11539}
11540
11541/*
11542 * "libcallnr()" function
11543 */
11544 static void
11545f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011546 typval_T *argvars;
11547 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011548{
11549 libcall_common(argvars, rettv, VAR_NUMBER);
11550}
11551
11552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 * "line(string)" function
11554 */
11555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011556f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011557 typval_T *argvars;
11558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559{
11560 linenr_T lnum = 0;
11561 pos_T *fp;
11562
11563 fp = var2fpos(&argvars[0], TRUE);
11564 if (fp != NULL)
11565 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567}
11568
11569/*
11570 * "line2byte(lnum)" function
11571 */
11572/*ARGSUSED*/
11573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011574f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011575 typval_T *argvars;
11576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577{
11578#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580#else
11581 linenr_T lnum;
11582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011583 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11588 if (rettv->vval.v_number >= 0)
11589 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590#endif
11591}
11592
11593/*
11594 * "lispindent(lnum)" function
11595 */
11596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011597f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011598 typval_T *argvars;
11599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600{
11601#ifdef FEAT_LISP
11602 pos_T pos;
11603 linenr_T lnum;
11604
11605 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011606 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11608 {
11609 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011610 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611 curwin->w_cursor = pos;
11612 }
11613 else
11614#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011615 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616}
11617
11618/*
11619 * "localtime()" function
11620 */
11621/*ARGSUSED*/
11622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011623f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011624 typval_T *argvars;
11625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628}
11629
Bram Moolenaar33570922005-01-25 22:26:29 +000011630static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631
11632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011633get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011634 typval_T *argvars;
11635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636 int exact;
11637{
11638 char_u *keys;
11639 char_u *which;
11640 char_u buf[NUMBUFLEN];
11641 char_u *keys_buf = NULL;
11642 char_u *rhs;
11643 int mode;
11644 garray_T ga;
11645
11646 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647 rettv->v_type = VAR_STRING;
11648 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011650 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651 if (*keys == NUL)
11652 return;
11653
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011654 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011655 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 else
11657 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011658 if (which == NULL)
11659 return;
11660
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661 mode = get_map_mode(&which, 0);
11662
11663 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011664 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665 vim_free(keys_buf);
11666 if (rhs != NULL)
11667 {
11668 ga_init(&ga);
11669 ga.ga_itemsize = 1;
11670 ga.ga_growsize = 40;
11671
11672 while (*rhs != NUL)
11673 ga_concat(&ga, str2special(&rhs, FALSE));
11674
11675 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011676 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 }
11678}
11679
11680/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011681 * "map()" function
11682 */
11683 static void
11684f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011685 typval_T *argvars;
11686 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011687{
11688 filter_map(argvars, rettv, TRUE);
11689}
11690
11691/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011692 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693 */
11694 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011695f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011696 typval_T *argvars;
11697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011699 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700}
11701
11702/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011703 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704 */
11705 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011706f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011707 typval_T *argvars;
11708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011710 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711}
11712
Bram Moolenaar33570922005-01-25 22:26:29 +000011713static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714
11715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011716find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011717 typval_T *argvars;
11718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 int type;
11720{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011721 char_u *str = NULL;
11722 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723 char_u *pat;
11724 regmatch_T regmatch;
11725 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011726 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 char_u *save_cpo;
11728 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011729 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011730 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011731 list_T *l = NULL;
11732 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011733 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011734 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735
11736 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11737 save_cpo = p_cpo;
11738 p_cpo = (char_u *)"";
11739
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011740 rettv->vval.v_number = -1;
11741 if (type == 3)
11742 {
11743 /* return empty list when there are no matches */
11744 if ((rettv->vval.v_list = list_alloc()) == NULL)
11745 goto theend;
11746 rettv->v_type = VAR_LIST;
11747 ++rettv->vval.v_list->lv_refcount;
11748 }
11749 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011751 rettv->v_type = VAR_STRING;
11752 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011755 if (argvars[0].v_type == VAR_LIST)
11756 {
11757 if ((l = argvars[0].vval.v_list) == NULL)
11758 goto theend;
11759 li = l->lv_first;
11760 }
11761 else
11762 expr = str = get_tv_string(&argvars[0]);
11763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011764 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11765 if (pat == NULL)
11766 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011767
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011768 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011770 int error = FALSE;
11771
11772 start = get_tv_number_chk(&argvars[2], &error);
11773 if (error)
11774 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011775 if (l != NULL)
11776 {
11777 li = list_find(l, start);
11778 if (li == NULL)
11779 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011780 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011781 }
11782 else
11783 {
11784 if (start < 0)
11785 start = 0;
11786 if (start > (long)STRLEN(str))
11787 goto theend;
11788 str += start;
11789 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011790
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011791 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011792 nth = get_tv_number_chk(&argvars[3], &error);
11793 if (error)
11794 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 }
11796
11797 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11798 if (regmatch.regprog != NULL)
11799 {
11800 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011801
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011802 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011803 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011804 if (l != NULL)
11805 {
11806 if (li == NULL)
11807 {
11808 match = FALSE;
11809 break;
11810 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011811 vim_free(tofree);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011812 str = echo_string(&li->li_tv, &tofree, strbuf,0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011813 if (str == NULL)
11814 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011815 }
11816
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011817 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011818
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011819 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011820 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011821 if (l == NULL && !match)
11822 break;
11823
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011824 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011825 if (l != NULL)
11826 {
11827 li = li->li_next;
11828 ++idx;
11829 }
11830 else
11831 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011832#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011833 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011834#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011835 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011836#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011837 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011838 }
11839
11840 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011842 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011843 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011844 int i;
11845
11846 /* return list with matched string and submatches */
11847 for (i = 0; i < NSUBEXP; ++i)
11848 {
11849 if (regmatch.endp[i] == NULL)
11850 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011851 if (list_append_string(rettv->vval.v_list,
11852 regmatch.startp[i],
11853 (int)(regmatch.endp[i] - regmatch.startp[i]))
11854 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011855 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011856 }
11857 }
11858 else if (type == 2)
11859 {
11860 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011861 if (l != NULL)
11862 copy_tv(&li->li_tv, rettv);
11863 else
11864 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011866 }
11867 else if (l != NULL)
11868 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869 else
11870 {
11871 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011872 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 (varnumber_T)(regmatch.startp[0] - str);
11874 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011875 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011877 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878 }
11879 }
11880 vim_free(regmatch.regprog);
11881 }
11882
11883theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011884 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885 p_cpo = save_cpo;
11886}
11887
11888/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011889 * "match()" function
11890 */
11891 static void
11892f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011893 typval_T *argvars;
11894 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011895{
11896 find_some_match(argvars, rettv, 1);
11897}
11898
11899/*
11900 * "matchend()" function
11901 */
11902 static void
11903f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011904 typval_T *argvars;
11905 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011906{
11907 find_some_match(argvars, rettv, 0);
11908}
11909
11910/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011911 * "matchlist()" function
11912 */
11913 static void
11914f_matchlist(argvars, rettv)
11915 typval_T *argvars;
11916 typval_T *rettv;
11917{
11918 find_some_match(argvars, rettv, 3);
11919}
11920
11921/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011922 * "matchstr()" function
11923 */
11924 static void
11925f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011926 typval_T *argvars;
11927 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011928{
11929 find_some_match(argvars, rettv, 2);
11930}
11931
Bram Moolenaar33570922005-01-25 22:26:29 +000011932static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011933
11934 static void
11935max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011936 typval_T *argvars;
11937 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011938 int domax;
11939{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011940 long n = 0;
11941 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011942 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011943
11944 if (argvars[0].v_type == VAR_LIST)
11945 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011946 list_T *l;
11947 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011948
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011949 l = argvars[0].vval.v_list;
11950 if (l != NULL)
11951 {
11952 li = l->lv_first;
11953 if (li != NULL)
11954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011955 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011956 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011957 {
11958 li = li->li_next;
11959 if (li == NULL)
11960 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011961 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011962 if (domax ? i > n : i < n)
11963 n = i;
11964 }
11965 }
11966 }
11967 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011968 else if (argvars[0].v_type == VAR_DICT)
11969 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011970 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011971 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011972 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011973 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011974
11975 d = argvars[0].vval.v_dict;
11976 if (d != NULL)
11977 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011978 todo = d->dv_hashtab.ht_used;
11979 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011980 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011981 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011982 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011983 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011984 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011985 if (first)
11986 {
11987 n = i;
11988 first = FALSE;
11989 }
11990 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011991 n = i;
11992 }
11993 }
11994 }
11995 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011996 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011997 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011998 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011999}
12000
12001/*
12002 * "max()" function
12003 */
12004 static void
12005f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012006 typval_T *argvars;
12007 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012008{
12009 max_min(argvars, rettv, TRUE);
12010}
12011
12012/*
12013 * "min()" function
12014 */
12015 static void
12016f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012017 typval_T *argvars;
12018 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012019{
12020 max_min(argvars, rettv, FALSE);
12021}
12022
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012023static int mkdir_recurse __ARGS((char_u *dir, int prot));
12024
12025/*
12026 * Create the directory in which "dir" is located, and higher levels when
12027 * needed.
12028 */
12029 static int
12030mkdir_recurse(dir, prot)
12031 char_u *dir;
12032 int prot;
12033{
12034 char_u *p;
12035 char_u *updir;
12036 int r = FAIL;
12037
12038 /* Get end of directory name in "dir".
12039 * We're done when it's "/" or "c:/". */
12040 p = gettail_sep(dir);
12041 if (p <= get_past_head(dir))
12042 return OK;
12043
12044 /* If the directory exists we're done. Otherwise: create it.*/
12045 updir = vim_strnsave(dir, (int)(p - dir));
12046 if (updir == NULL)
12047 return FAIL;
12048 if (mch_isdir(updir))
12049 r = OK;
12050 else if (mkdir_recurse(updir, prot) == OK)
12051 r = vim_mkdir_emsg(updir, prot);
12052 vim_free(updir);
12053 return r;
12054}
12055
12056#ifdef vim_mkdir
12057/*
12058 * "mkdir()" function
12059 */
12060 static void
12061f_mkdir(argvars, rettv)
12062 typval_T *argvars;
12063 typval_T *rettv;
12064{
12065 char_u *dir;
12066 char_u buf[NUMBUFLEN];
12067 int prot = 0755;
12068
12069 rettv->vval.v_number = FAIL;
12070 if (check_restricted() || check_secure())
12071 return;
12072
12073 dir = get_tv_string_buf(&argvars[0], buf);
12074 if (argvars[1].v_type != VAR_UNKNOWN)
12075 {
12076 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012077 prot = get_tv_number_chk(&argvars[2], NULL);
12078 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012079 mkdir_recurse(dir, prot);
12080 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012081 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012082}
12083#endif
12084
Bram Moolenaar0d660222005-01-07 21:51:51 +000012085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 * "mode()" function
12087 */
12088/*ARGSUSED*/
12089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012091 typval_T *argvars;
12092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093{
12094 char_u buf[2];
12095
12096#ifdef FEAT_VISUAL
12097 if (VIsual_active)
12098 {
12099 if (VIsual_select)
12100 buf[0] = VIsual_mode + 's' - 'v';
12101 else
12102 buf[0] = VIsual_mode;
12103 }
12104 else
12105#endif
12106 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12107 buf[0] = 'r';
12108 else if (State & INSERT)
12109 {
12110 if (State & REPLACE_FLAG)
12111 buf[0] = 'R';
12112 else
12113 buf[0] = 'i';
12114 }
12115 else if (State & CMDLINE)
12116 buf[0] = 'c';
12117 else
12118 buf[0] = 'n';
12119
12120 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121 rettv->vval.v_string = vim_strsave(buf);
12122 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123}
12124
12125/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012126 * "nextnonblank()" function
12127 */
12128 static void
12129f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012130 typval_T *argvars;
12131 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012132{
12133 linenr_T lnum;
12134
12135 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012137 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012138 {
12139 lnum = 0;
12140 break;
12141 }
12142 if (*skipwhite(ml_get(lnum)) != NUL)
12143 break;
12144 }
12145 rettv->vval.v_number = lnum;
12146}
12147
12148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149 * "nr2char()" function
12150 */
12151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012152f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012153 typval_T *argvars;
12154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155{
12156 char_u buf[NUMBUFLEN];
12157
12158#ifdef FEAT_MBYTE
12159 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 else
12162#endif
12163 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012164 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165 buf[1] = NUL;
12166 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012167 rettv->v_type = VAR_STRING;
12168 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012169}
12170
12171/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012172 * "prevnonblank()" function
12173 */
12174 static void
12175f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012176 typval_T *argvars;
12177 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012178{
12179 linenr_T lnum;
12180
12181 lnum = get_tv_lnum(argvars);
12182 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12183 lnum = 0;
12184 else
12185 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12186 --lnum;
12187 rettv->vval.v_number = lnum;
12188}
12189
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012190#ifdef HAVE_STDARG_H
12191/* This dummy va_list is here because:
12192 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12193 * - locally in the function results in a "used before set" warning
12194 * - using va_start() to initialize it gives "function with fixed args" error */
12195static va_list ap;
12196#endif
12197
Bram Moolenaar8c711452005-01-14 21:53:12 +000012198/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012199 * "printf()" function
12200 */
12201 static void
12202f_printf(argvars, rettv)
12203 typval_T *argvars;
12204 typval_T *rettv;
12205{
12206 rettv->v_type = VAR_STRING;
12207 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012208#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012209 {
12210 char_u buf[NUMBUFLEN];
12211 int len;
12212 char_u *s;
12213 int saved_did_emsg = did_emsg;
12214 char *fmt;
12215
12216 /* Get the required length, allocate the buffer and do it for real. */
12217 did_emsg = FALSE;
12218 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012219 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012220 if (!did_emsg)
12221 {
12222 s = alloc(len + 1);
12223 if (s != NULL)
12224 {
12225 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012226 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012227 }
12228 }
12229 did_emsg |= saved_did_emsg;
12230 }
12231#endif
12232}
12233
12234/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012235 * "pumvisible()" function
12236 */
12237/*ARGSUSED*/
12238 static void
12239f_pumvisible(argvars, rettv)
12240 typval_T *argvars;
12241 typval_T *rettv;
12242{
12243 rettv->vval.v_number = 0;
12244#ifdef FEAT_INS_EXPAND
12245 if (pum_visible())
12246 rettv->vval.v_number = 1;
12247#endif
12248}
12249
12250/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012251 * "range()" function
12252 */
12253 static void
12254f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012255 typval_T *argvars;
12256 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012257{
12258 long start;
12259 long end;
12260 long stride = 1;
12261 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012262 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012263 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012264
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012265 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012266 if (argvars[1].v_type == VAR_UNKNOWN)
12267 {
12268 end = start - 1;
12269 start = 0;
12270 }
12271 else
12272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012273 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012274 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012275 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012276 }
12277
12278 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012279 if (error)
12280 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012281 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012282 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012283 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012284 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012285 else
12286 {
12287 l = list_alloc();
12288 if (l != NULL)
12289 {
12290 rettv->v_type = VAR_LIST;
12291 rettv->vval.v_list = l;
12292 ++l->lv_refcount;
12293
12294 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012295 if (list_append_number(l, (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012296 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012297 }
12298 }
12299}
12300
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012301/*
12302 * "readfile()" function
12303 */
12304 static void
12305f_readfile(argvars, rettv)
12306 typval_T *argvars;
12307 typval_T *rettv;
12308{
12309 int binary = FALSE;
12310 char_u *fname;
12311 FILE *fd;
12312 list_T *l;
12313 listitem_T *li;
12314#define FREAD_SIZE 200 /* optimized for text lines */
12315 char_u buf[FREAD_SIZE];
12316 int readlen; /* size of last fread() */
12317 int buflen; /* nr of valid chars in buf[] */
12318 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12319 int tolist; /* first byte in buf[] still to be put in list */
12320 int chop; /* how many CR to chop off */
12321 char_u *prev = NULL; /* previously read bytes, if any */
12322 int prevlen = 0; /* length of "prev" if not NULL */
12323 char_u *s;
12324 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012325 long maxline = MAXLNUM;
12326 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012327
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012328 if (argvars[1].v_type != VAR_UNKNOWN)
12329 {
12330 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12331 binary = TRUE;
12332 if (argvars[2].v_type != VAR_UNKNOWN)
12333 maxline = get_tv_number(&argvars[2]);
12334 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012335
12336 l = list_alloc();
12337 if (l == NULL)
12338 return;
12339 rettv->v_type = VAR_LIST;
12340 rettv->vval.v_list = l;
12341 l->lv_refcount = 1;
12342
12343 /* Always open the file in binary mode, library functions have a mind of
12344 * their own about CR-LF conversion. */
12345 fname = get_tv_string(&argvars[0]);
12346 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12347 {
12348 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12349 return;
12350 }
12351
12352 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012353 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012354 {
12355 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12356 buflen = filtd + readlen;
12357 tolist = 0;
12358 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12359 {
12360 if (buf[filtd] == '\n' || readlen <= 0)
12361 {
12362 /* Only when in binary mode add an empty list item when the
12363 * last line ends in a '\n'. */
12364 if (!binary && readlen == 0 && filtd == 0)
12365 break;
12366
12367 /* Found end-of-line or end-of-file: add a text line to the
12368 * list. */
12369 chop = 0;
12370 if (!binary)
12371 while (filtd - chop - 1 >= tolist
12372 && buf[filtd - chop - 1] == '\r')
12373 ++chop;
12374 len = filtd - tolist - chop;
12375 if (prev == NULL)
12376 s = vim_strnsave(buf + tolist, len);
12377 else
12378 {
12379 s = alloc((unsigned)(prevlen + len + 1));
12380 if (s != NULL)
12381 {
12382 mch_memmove(s, prev, prevlen);
12383 vim_free(prev);
12384 prev = NULL;
12385 mch_memmove(s + prevlen, buf + tolist, len);
12386 s[prevlen + len] = NUL;
12387 }
12388 }
12389 tolist = filtd + 1;
12390
12391 li = listitem_alloc();
12392 if (li == NULL)
12393 {
12394 vim_free(s);
12395 break;
12396 }
12397 li->li_tv.v_type = VAR_STRING;
12398 li->li_tv.v_lock = 0;
12399 li->li_tv.vval.v_string = s;
12400 list_append(l, li);
12401
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012402 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012403 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012404 if (readlen <= 0)
12405 break;
12406 }
12407 else if (buf[filtd] == NUL)
12408 buf[filtd] = '\n';
12409 }
12410 if (readlen <= 0)
12411 break;
12412
12413 if (tolist == 0)
12414 {
12415 /* "buf" is full, need to move text to an allocated buffer */
12416 if (prev == NULL)
12417 {
12418 prev = vim_strnsave(buf, buflen);
12419 prevlen = buflen;
12420 }
12421 else
12422 {
12423 s = alloc((unsigned)(prevlen + buflen));
12424 if (s != NULL)
12425 {
12426 mch_memmove(s, prev, prevlen);
12427 mch_memmove(s + prevlen, buf, buflen);
12428 vim_free(prev);
12429 prev = s;
12430 prevlen += buflen;
12431 }
12432 }
12433 filtd = 0;
12434 }
12435 else
12436 {
12437 mch_memmove(buf, buf + tolist, buflen - tolist);
12438 filtd -= tolist;
12439 }
12440 }
12441
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012442 /*
12443 * For a negative line count use only the lines at the end of the file,
12444 * free the rest.
12445 */
12446 if (maxline < 0)
12447 while (cnt > -maxline)
12448 {
12449 listitem_remove(l, l->lv_first);
12450 --cnt;
12451 }
12452
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012453 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012454 fclose(fd);
12455}
12456
12457
Bram Moolenaar0d660222005-01-07 21:51:51 +000012458#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12459static void make_connection __ARGS((void));
12460static int check_connection __ARGS((void));
12461
12462 static void
12463make_connection()
12464{
12465 if (X_DISPLAY == NULL
12466# ifdef FEAT_GUI
12467 && !gui.in_use
12468# endif
12469 )
12470 {
12471 x_force_connect = TRUE;
12472 setup_term_clip();
12473 x_force_connect = FALSE;
12474 }
12475}
12476
12477 static int
12478check_connection()
12479{
12480 make_connection();
12481 if (X_DISPLAY == NULL)
12482 {
12483 EMSG(_("E240: No connection to Vim server"));
12484 return FAIL;
12485 }
12486 return OK;
12487}
12488#endif
12489
12490#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012491static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012492
12493 static void
12494remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012495 typval_T *argvars;
12496 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012497 int expr;
12498{
12499 char_u *server_name;
12500 char_u *keys;
12501 char_u *r = NULL;
12502 char_u buf[NUMBUFLEN];
12503# ifdef WIN32
12504 HWND w;
12505# else
12506 Window w;
12507# endif
12508
12509 if (check_restricted() || check_secure())
12510 return;
12511
12512# ifdef FEAT_X11
12513 if (check_connection() == FAIL)
12514 return;
12515# endif
12516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012517 server_name = get_tv_string_chk(&argvars[0]);
12518 if (server_name == NULL)
12519 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012520 keys = get_tv_string_buf(&argvars[1], buf);
12521# ifdef WIN32
12522 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12523# else
12524 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12525 < 0)
12526# endif
12527 {
12528 if (r != NULL)
12529 EMSG(r); /* sending worked but evaluation failed */
12530 else
12531 EMSG2(_("E241: Unable to send to %s"), server_name);
12532 return;
12533 }
12534
12535 rettv->vval.v_string = r;
12536
12537 if (argvars[2].v_type != VAR_UNKNOWN)
12538 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012539 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012540 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012541 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012542
12543 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012544 v.di_tv.v_type = VAR_STRING;
12545 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012546 idvar = get_tv_string_chk(&argvars[2]);
12547 if (idvar != NULL)
12548 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012549 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012550 }
12551}
12552#endif
12553
12554/*
12555 * "remote_expr()" function
12556 */
12557/*ARGSUSED*/
12558 static void
12559f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012560 typval_T *argvars;
12561 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012562{
12563 rettv->v_type = VAR_STRING;
12564 rettv->vval.v_string = NULL;
12565#ifdef FEAT_CLIENTSERVER
12566 remote_common(argvars, rettv, TRUE);
12567#endif
12568}
12569
12570/*
12571 * "remote_foreground()" function
12572 */
12573/*ARGSUSED*/
12574 static void
12575f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012576 typval_T *argvars;
12577 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012578{
12579 rettv->vval.v_number = 0;
12580#ifdef FEAT_CLIENTSERVER
12581# ifdef WIN32
12582 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012583 {
12584 char_u *server_name = get_tv_string_chk(&argvars[0]);
12585
12586 if (server_name != NULL)
12587 serverForeground(server_name);
12588 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012589# else
12590 /* Send a foreground() expression to the server. */
12591 argvars[1].v_type = VAR_STRING;
12592 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12593 argvars[2].v_type = VAR_UNKNOWN;
12594 remote_common(argvars, rettv, TRUE);
12595 vim_free(argvars[1].vval.v_string);
12596# endif
12597#endif
12598}
12599
12600/*ARGSUSED*/
12601 static void
12602f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012603 typval_T *argvars;
12604 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012605{
12606#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012607 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012608 char_u *s = NULL;
12609# ifdef WIN32
12610 int n = 0;
12611# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012612 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012613
12614 if (check_restricted() || check_secure())
12615 {
12616 rettv->vval.v_number = -1;
12617 return;
12618 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012619 serverid = get_tv_string_chk(&argvars[0]);
12620 if (serverid == NULL)
12621 {
12622 rettv->vval.v_number = -1;
12623 return; /* type error; errmsg already given */
12624 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012625# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012626 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012627 if (n == 0)
12628 rettv->vval.v_number = -1;
12629 else
12630 {
12631 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12632 rettv->vval.v_number = (s != NULL);
12633 }
12634# else
12635 rettv->vval.v_number = 0;
12636 if (check_connection() == FAIL)
12637 return;
12638
12639 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012640 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012641# endif
12642
12643 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12644 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012645 char_u *retvar;
12646
Bram Moolenaar33570922005-01-25 22:26:29 +000012647 v.di_tv.v_type = VAR_STRING;
12648 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012649 retvar = get_tv_string_chk(&argvars[1]);
12650 if (retvar != NULL)
12651 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012652 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012653 }
12654#else
12655 rettv->vval.v_number = -1;
12656#endif
12657}
12658
12659/*ARGSUSED*/
12660 static void
12661f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012662 typval_T *argvars;
12663 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012664{
12665 char_u *r = NULL;
12666
12667#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012668 char_u *serverid = get_tv_string_chk(&argvars[0]);
12669
12670 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012671 {
12672# ifdef WIN32
12673 /* The server's HWND is encoded in the 'id' parameter */
12674 int n = 0;
12675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012676 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012677 if (n != 0)
12678 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12679 if (r == NULL)
12680# else
12681 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012682 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012683# endif
12684 EMSG(_("E277: Unable to read a server reply"));
12685 }
12686#endif
12687 rettv->v_type = VAR_STRING;
12688 rettv->vval.v_string = r;
12689}
12690
12691/*
12692 * "remote_send()" function
12693 */
12694/*ARGSUSED*/
12695 static void
12696f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012697 typval_T *argvars;
12698 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012699{
12700 rettv->v_type = VAR_STRING;
12701 rettv->vval.v_string = NULL;
12702#ifdef FEAT_CLIENTSERVER
12703 remote_common(argvars, rettv, FALSE);
12704#endif
12705}
12706
12707/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012708 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012709 */
12710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012712 typval_T *argvars;
12713 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012714{
Bram Moolenaar33570922005-01-25 22:26:29 +000012715 list_T *l;
12716 listitem_T *item, *item2;
12717 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012718 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012719 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012720 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012721 dict_T *d;
12722 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012723
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012724 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012725 if (argvars[0].v_type == VAR_DICT)
12726 {
12727 if (argvars[2].v_type != VAR_UNKNOWN)
12728 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012729 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012730 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012731 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012732 key = get_tv_string_chk(&argvars[1]);
12733 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012735 di = dict_find(d, key, -1);
12736 if (di == NULL)
12737 EMSG2(_(e_dictkey), key);
12738 else
12739 {
12740 *rettv = di->di_tv;
12741 init_tv(&di->di_tv);
12742 dictitem_remove(d, di);
12743 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012744 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012745 }
12746 }
12747 else if (argvars[0].v_type != VAR_LIST)
12748 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012749 else if ((l = argvars[0].vval.v_list) != NULL
12750 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012752 int error = FALSE;
12753
12754 idx = get_tv_number_chk(&argvars[1], &error);
12755 if (error)
12756 ; /* type error: do nothing, errmsg already given */
12757 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012758 EMSGN(_(e_listidx), idx);
12759 else
12760 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012761 if (argvars[2].v_type == VAR_UNKNOWN)
12762 {
12763 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012764 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012765 *rettv = item->li_tv;
12766 vim_free(item);
12767 }
12768 else
12769 {
12770 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012771 end = get_tv_number_chk(&argvars[2], &error);
12772 if (error)
12773 ; /* type error: do nothing */
12774 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012775 EMSGN(_(e_listidx), end);
12776 else
12777 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012778 int cnt = 0;
12779
12780 for (li = item; li != NULL; li = li->li_next)
12781 {
12782 ++cnt;
12783 if (li == item2)
12784 break;
12785 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012786 if (li == NULL) /* didn't find "item2" after "item" */
12787 EMSG(_(e_invrange));
12788 else
12789 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012790 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012791 l = list_alloc();
12792 if (l != NULL)
12793 {
12794 rettv->v_type = VAR_LIST;
12795 rettv->vval.v_list = l;
12796 l->lv_first = item;
12797 l->lv_last = item2;
12798 l->lv_refcount = 1;
12799 item->li_prev = NULL;
12800 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012801 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012802 }
12803 }
12804 }
12805 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012806 }
12807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808}
12809
12810/*
12811 * "rename({from}, {to})" function
12812 */
12813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012815 typval_T *argvars;
12816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817{
12818 char_u buf[NUMBUFLEN];
12819
12820 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012821 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12824 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825}
12826
12827/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012828 * "repeat()" function
12829 */
12830/*ARGSUSED*/
12831 static void
12832f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012833 typval_T *argvars;
12834 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012835{
12836 char_u *p;
12837 int n;
12838 int slen;
12839 int len;
12840 char_u *r;
12841 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012842 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012843
12844 n = get_tv_number(&argvars[1]);
12845 if (argvars[0].v_type == VAR_LIST)
12846 {
12847 l = list_alloc();
12848 if (l != NULL && argvars[0].vval.v_list != NULL)
12849 {
12850 l->lv_refcount = 1;
12851 while (n-- > 0)
12852 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12853 break;
12854 }
12855 rettv->v_type = VAR_LIST;
12856 rettv->vval.v_list = l;
12857 }
12858 else
12859 {
12860 p = get_tv_string(&argvars[0]);
12861 rettv->v_type = VAR_STRING;
12862 rettv->vval.v_string = NULL;
12863
12864 slen = (int)STRLEN(p);
12865 len = slen * n;
12866 if (len <= 0)
12867 return;
12868
12869 r = alloc(len + 1);
12870 if (r != NULL)
12871 {
12872 for (i = 0; i < n; i++)
12873 mch_memmove(r + i * slen, p, (size_t)slen);
12874 r[len] = NUL;
12875 }
12876
12877 rettv->vval.v_string = r;
12878 }
12879}
12880
12881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882 * "resolve()" function
12883 */
12884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012886 typval_T *argvars;
12887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888{
12889 char_u *p;
12890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012891 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892#ifdef FEAT_SHORTCUT
12893 {
12894 char_u *v = NULL;
12895
12896 v = mch_resolve_shortcut(p);
12897 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901 }
12902#else
12903# ifdef HAVE_READLINK
12904 {
12905 char_u buf[MAXPATHL + 1];
12906 char_u *cpy;
12907 int len;
12908 char_u *remain = NULL;
12909 char_u *q;
12910 int is_relative_to_current = FALSE;
12911 int has_trailing_pathsep = FALSE;
12912 int limit = 100;
12913
12914 p = vim_strsave(p);
12915
12916 if (p[0] == '.' && (vim_ispathsep(p[1])
12917 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12918 is_relative_to_current = TRUE;
12919
12920 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012921 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922 has_trailing_pathsep = TRUE;
12923
12924 q = getnextcomp(p);
12925 if (*q != NUL)
12926 {
12927 /* Separate the first path component in "p", and keep the
12928 * remainder (beginning with the path separator). */
12929 remain = vim_strsave(q - 1);
12930 q[-1] = NUL;
12931 }
12932
12933 for (;;)
12934 {
12935 for (;;)
12936 {
12937 len = readlink((char *)p, (char *)buf, MAXPATHL);
12938 if (len <= 0)
12939 break;
12940 buf[len] = NUL;
12941
12942 if (limit-- == 0)
12943 {
12944 vim_free(p);
12945 vim_free(remain);
12946 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012947 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948 goto fail;
12949 }
12950
12951 /* Ensure that the result will have a trailing path separator
12952 * if the argument has one. */
12953 if (remain == NULL && has_trailing_pathsep)
12954 add_pathsep(buf);
12955
12956 /* Separate the first path component in the link value and
12957 * concatenate the remainders. */
12958 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12959 if (*q != NUL)
12960 {
12961 if (remain == NULL)
12962 remain = vim_strsave(q - 1);
12963 else
12964 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000012965 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966 if (cpy != NULL)
12967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968 vim_free(remain);
12969 remain = cpy;
12970 }
12971 }
12972 q[-1] = NUL;
12973 }
12974
12975 q = gettail(p);
12976 if (q > p && *q == NUL)
12977 {
12978 /* Ignore trailing path separator. */
12979 q[-1] = NUL;
12980 q = gettail(p);
12981 }
12982 if (q > p && !mch_isFullName(buf))
12983 {
12984 /* symlink is relative to directory of argument */
12985 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12986 if (cpy != NULL)
12987 {
12988 STRCPY(cpy, p);
12989 STRCPY(gettail(cpy), buf);
12990 vim_free(p);
12991 p = cpy;
12992 }
12993 }
12994 else
12995 {
12996 vim_free(p);
12997 p = vim_strsave(buf);
12998 }
12999 }
13000
13001 if (remain == NULL)
13002 break;
13003
13004 /* Append the first path component of "remain" to "p". */
13005 q = getnextcomp(remain + 1);
13006 len = q - remain - (*q != NUL);
13007 cpy = vim_strnsave(p, STRLEN(p) + len);
13008 if (cpy != NULL)
13009 {
13010 STRNCAT(cpy, remain, len);
13011 vim_free(p);
13012 p = cpy;
13013 }
13014 /* Shorten "remain". */
13015 if (*q != NUL)
13016 STRCPY(remain, q - 1);
13017 else
13018 {
13019 vim_free(remain);
13020 remain = NULL;
13021 }
13022 }
13023
13024 /* If the result is a relative path name, make it explicitly relative to
13025 * the current directory if and only if the argument had this form. */
13026 if (!vim_ispathsep(*p))
13027 {
13028 if (is_relative_to_current
13029 && *p != NUL
13030 && !(p[0] == '.'
13031 && (p[1] == NUL
13032 || vim_ispathsep(p[1])
13033 || (p[1] == '.'
13034 && (p[2] == NUL
13035 || vim_ispathsep(p[2]))))))
13036 {
13037 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013038 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039 if (cpy != NULL)
13040 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041 vim_free(p);
13042 p = cpy;
13043 }
13044 }
13045 else if (!is_relative_to_current)
13046 {
13047 /* Strip leading "./". */
13048 q = p;
13049 while (q[0] == '.' && vim_ispathsep(q[1]))
13050 q += 2;
13051 if (q > p)
13052 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13053 }
13054 }
13055
13056 /* Ensure that the result will have no trailing path separator
13057 * if the argument had none. But keep "/" or "//". */
13058 if (!has_trailing_pathsep)
13059 {
13060 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013061 if (after_pathsep(p, q))
13062 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063 }
13064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013065 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066 }
13067# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013068 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069# endif
13070#endif
13071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013072 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073
13074#ifdef HAVE_READLINK
13075fail:
13076#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013077 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078}
13079
13080/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013081 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082 */
13083 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013084f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013085 typval_T *argvars;
13086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087{
Bram Moolenaar33570922005-01-25 22:26:29 +000013088 list_T *l;
13089 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090
Bram Moolenaar0d660222005-01-07 21:51:51 +000013091 rettv->vval.v_number = 0;
13092 if (argvars[0].v_type != VAR_LIST)
13093 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013094 else if ((l = argvars[0].vval.v_list) != NULL
13095 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013096 {
13097 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013098 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013099 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013100 while (li != NULL)
13101 {
13102 ni = li->li_prev;
13103 list_append(l, li);
13104 li = ni;
13105 }
13106 rettv->vval.v_list = l;
13107 rettv->v_type = VAR_LIST;
13108 ++l->lv_refcount;
13109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110}
13111
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013112#define SP_NOMOVE 1 /* don't move cursor */
13113#define SP_REPEAT 2 /* repeat to find outer pair */
13114#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000013115#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013116
Bram Moolenaar33570922005-01-25 22:26:29 +000013117static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013118
13119/*
13120 * Get flags for a search function.
13121 * Possibly sets "p_ws".
13122 * Returns BACKWARD, FORWARD or zero (for an error).
13123 */
13124 static int
13125get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013126 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013127 int *flagsp;
13128{
13129 int dir = FORWARD;
13130 char_u *flags;
13131 char_u nbuf[NUMBUFLEN];
13132 int mask;
13133
13134 if (varp->v_type != VAR_UNKNOWN)
13135 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013136 flags = get_tv_string_buf_chk(varp, nbuf);
13137 if (flags == NULL)
13138 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013139 while (*flags != NUL)
13140 {
13141 switch (*flags)
13142 {
13143 case 'b': dir = BACKWARD; break;
13144 case 'w': p_ws = TRUE; break;
13145 case 'W': p_ws = FALSE; break;
13146 default: mask = 0;
13147 if (flagsp != NULL)
13148 switch (*flags)
13149 {
13150 case 'n': mask = SP_NOMOVE; break;
13151 case 'r': mask = SP_REPEAT; break;
13152 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013153 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013154 }
13155 if (mask == 0)
13156 {
13157 EMSG2(_(e_invarg2), flags);
13158 dir = 0;
13159 }
13160 else
13161 *flagsp |= mask;
13162 }
13163 if (dir == 0)
13164 break;
13165 ++flags;
13166 }
13167 }
13168 return dir;
13169}
13170
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013172 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013174 static int
13175search_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013176 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013177 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178{
13179 char_u *pat;
13180 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013181 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182 int save_p_ws = p_ws;
13183 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013184 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013185 int retval = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013188 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13189 if (dir == 0)
13190 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013191 /*
13192 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13193 * Check to make sure only those flags are set.
13194 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13195 * flags cannot be set. Check for that condition also.
13196 */
13197 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13198 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013200 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013201 goto theend;
13202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013204 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13206 SEARCH_KEEP, RE_SEARCH) != FAIL)
13207 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013208 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013209 if (flags & SP_SETPCMARK)
13210 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013212 if (match_pos != NULL)
13213 {
13214 /* Store the match cursor position */
13215 match_pos->lnum = pos.lnum;
13216 match_pos->col = pos.col + 1;
13217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218 /* "/$" will put the cursor after the end of the line, may need to
13219 * correct that here */
13220 check_cursor();
13221 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013222
13223 /* If 'n' flag is used: restore cursor position. */
13224 if (flags & SP_NOMOVE)
13225 curwin->w_cursor = save_cursor;
13226theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013228
13229 return retval;
13230}
13231
13232/*
13233 * "search()" function
13234 */
13235 static void
13236f_search(argvars, rettv)
13237 typval_T *argvars;
13238 typval_T *rettv;
13239{
13240 rettv->vval.v_number = search_cmn(argvars, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241}
13242
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013244 * "searchdecl()" function
13245 */
13246 static void
13247f_searchdecl(argvars, rettv)
13248 typval_T *argvars;
13249 typval_T *rettv;
13250{
13251 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013252 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013253 int error = FALSE;
13254 char_u *name;
13255
13256 rettv->vval.v_number = 1; /* default: FAIL */
13257
13258 name = get_tv_string_chk(&argvars[0]);
13259 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013260 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013261 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013262 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13263 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13264 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013265 if (!error && name != NULL)
13266 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013267 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013268}
13269
13270/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013271 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013273 static int
13274searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013275 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013276 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277{
13278 char_u *spat, *mpat, *epat;
13279 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 int dir;
13282 int flags = 0;
13283 char_u nbuf1[NUMBUFLEN];
13284 char_u nbuf2[NUMBUFLEN];
13285 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013286 int retval = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013289 spat = get_tv_string_chk(&argvars[0]);
13290 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13291 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13292 if (spat == NULL || mpat == NULL || epat == NULL)
13293 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 /* Handle the optional fourth argument: flags */
13296 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013297 if (dir == 0)
13298 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013299 /*
13300 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13301 */
13302 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13303 {
13304 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13305 goto theend;
13306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013308 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013309 if (argvars[3].v_type == VAR_UNKNOWN
13310 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311 skip = (char_u *)"";
13312 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013313 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13314 if (skip == NULL)
13315 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013317 retval = do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013318
13319theend:
13320 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013321
13322 return retval;
13323}
13324
13325/*
13326 * "searchpair()" function
13327 */
13328 static void
13329f_searchpair(argvars, rettv)
13330 typval_T *argvars;
13331 typval_T *rettv;
13332{
13333 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13334}
13335
13336/*
13337 * "searchpairpos()" function
13338 */
13339 static void
13340f_searchpairpos(argvars, rettv)
13341 typval_T *argvars;
13342 typval_T *rettv;
13343{
13344 list_T *l;
13345 pos_T match_pos;
13346 int lnum = 0;
13347 int col = 0;
13348
13349 rettv->vval.v_number = 0;
13350
13351 l = list_alloc();
13352 if (l == NULL)
13353 return;
13354 rettv->v_type = VAR_LIST;
13355 rettv->vval.v_list = l;
13356 ++l->lv_refcount;
13357
13358 if (searchpair_cmn(argvars, &match_pos) > 0)
13359 {
13360 lnum = match_pos.lnum;
13361 col = match_pos.col;
13362 }
13363
13364 list_append_number(l, (varnumber_T)lnum);
13365 list_append_number(l, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013366}
13367
13368/*
13369 * Search for a start/middle/end thing.
13370 * Used by searchpair(), see its documentation for the details.
13371 * Returns 0 or -1 for no match,
13372 */
13373 long
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013374do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013375 char_u *spat; /* start pattern */
13376 char_u *mpat; /* middle pattern */
13377 char_u *epat; /* end pattern */
13378 int dir; /* BACKWARD or FORWARD */
13379 char_u *skip; /* skip expression */
13380 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013381 pos_T *match_pos;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013382{
13383 char_u *save_cpo;
13384 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13385 long retval = 0;
13386 pos_T pos;
13387 pos_T firstpos;
13388 pos_T foundpos;
13389 pos_T save_cursor;
13390 pos_T save_pos;
13391 int n;
13392 int r;
13393 int nest = 1;
13394 int err;
13395
13396 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13397 save_cpo = p_cpo;
13398 p_cpo = (char_u *)"";
13399
13400 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13401 * start/middle/end (pat3, for the top pair). */
13402 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13403 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13404 if (pat2 == NULL || pat3 == NULL)
13405 goto theend;
13406 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13407 if (*mpat == NUL)
13408 STRCPY(pat3, pat2);
13409 else
13410 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13411 spat, epat, mpat);
13412
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 save_cursor = curwin->w_cursor;
13414 pos = curwin->w_cursor;
13415 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013416 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 pat = pat3;
13418 for (;;)
13419 {
13420 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13421 SEARCH_KEEP, RE_SEARCH);
13422 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13423 /* didn't find it or found the first match again: FAIL */
13424 break;
13425
13426 if (firstpos.lnum == 0)
13427 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013428 if (equalpos(pos, foundpos))
13429 {
13430 /* Found the same position again. Can happen with a pattern that
13431 * has "\zs" at the end and searching backwards. Advance one
13432 * character and try again. */
13433 if (dir == BACKWARD)
13434 decl(&pos);
13435 else
13436 incl(&pos);
13437 }
13438 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439
13440 /* If the skip pattern matches, ignore this match. */
13441 if (*skip != NUL)
13442 {
13443 save_pos = curwin->w_cursor;
13444 curwin->w_cursor = pos;
13445 r = eval_to_bool(skip, &err, NULL, FALSE);
13446 curwin->w_cursor = save_pos;
13447 if (err)
13448 {
13449 /* Evaluating {skip} caused an error, break here. */
13450 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013451 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 break;
13453 }
13454 if (r)
13455 continue;
13456 }
13457
13458 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13459 {
13460 /* Found end when searching backwards or start when searching
13461 * forward: nested pair. */
13462 ++nest;
13463 pat = pat2; /* nested, don't search for middle */
13464 }
13465 else
13466 {
13467 /* Found end when searching forward or start when searching
13468 * backward: end of (nested) pair; or found middle in outer pair. */
13469 if (--nest == 1)
13470 pat = pat3; /* outer level, search for middle */
13471 }
13472
13473 if (nest == 0)
13474 {
13475 /* Found the match: return matchcount or line number. */
13476 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013477 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013479 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013480 if (flags & SP_SETPCMARK)
13481 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482 curwin->w_cursor = pos;
13483 if (!(flags & SP_REPEAT))
13484 break;
13485 nest = 1; /* search for next unmatched */
13486 }
13487 }
13488
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013489 if (match_pos != NULL)
13490 {
13491 /* Store the match cursor position */
13492 match_pos->lnum = curwin->w_cursor.lnum;
13493 match_pos->col = curwin->w_cursor.col + 1;
13494 }
13495
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013497 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 curwin->w_cursor = save_cursor;
13499
13500theend:
13501 vim_free(pat2);
13502 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013504
13505 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506}
13507
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013508/*
13509 * "searchpos()" function
13510 */
13511 static void
13512f_searchpos(argvars, rettv)
13513 typval_T *argvars;
13514 typval_T *rettv;
13515{
13516 list_T *l;
13517 pos_T match_pos;
13518 int lnum = 0;
13519 int col = 0;
13520
13521 rettv->vval.v_number = 0;
13522
13523 l = list_alloc();
13524 if (l == NULL)
13525 return;
13526 rettv->v_type = VAR_LIST;
13527 rettv->vval.v_list = l;
13528 ++l->lv_refcount;
13529
13530 if (search_cmn(argvars, &match_pos) > 0)
13531 {
13532 lnum = match_pos.lnum;
13533 col = match_pos.col;
13534 }
13535
13536 list_append_number(l, (varnumber_T)lnum);
13537 list_append_number(l, (varnumber_T)col);
13538
13539}
13540
13541
Bram Moolenaar0d660222005-01-07 21:51:51 +000013542/*ARGSUSED*/
13543 static void
13544f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013545 typval_T *argvars;
13546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013548#ifdef FEAT_CLIENTSERVER
13549 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013550 char_u *server = get_tv_string_chk(&argvars[0]);
13551 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552
Bram Moolenaar0d660222005-01-07 21:51:51 +000013553 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013554 if (server == NULL || reply == NULL)
13555 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013556 if (check_restricted() || check_secure())
13557 return;
13558# ifdef FEAT_X11
13559 if (check_connection() == FAIL)
13560 return;
13561# endif
13562
13563 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013565 EMSG(_("E258: Unable to send to client"));
13566 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013568 rettv->vval.v_number = 0;
13569#else
13570 rettv->vval.v_number = -1;
13571#endif
13572}
13573
13574/*ARGSUSED*/
13575 static void
13576f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013577 typval_T *argvars;
13578 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013579{
13580 char_u *r = NULL;
13581
13582#ifdef FEAT_CLIENTSERVER
13583# ifdef WIN32
13584 r = serverGetVimNames();
13585# else
13586 make_connection();
13587 if (X_DISPLAY != NULL)
13588 r = serverGetVimNames(X_DISPLAY);
13589# endif
13590#endif
13591 rettv->v_type = VAR_STRING;
13592 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593}
13594
13595/*
13596 * "setbufvar()" function
13597 */
13598/*ARGSUSED*/
13599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013600f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013601 typval_T *argvars;
13602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603{
13604 buf_T *buf;
13605#ifdef FEAT_AUTOCMD
13606 aco_save_T aco;
13607#else
13608 buf_T *save_curbuf;
13609#endif
13610 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013611 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 char_u nbuf[NUMBUFLEN];
13613
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013614 rettv->vval.v_number = 0;
13615
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 if (check_restricted() || check_secure())
13617 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013618 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13619 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 varp = &argvars[2];
13622
13623 if (buf != NULL && varname != NULL && varp != NULL)
13624 {
13625 /* set curbuf to be our buf, temporarily */
13626#ifdef FEAT_AUTOCMD
13627 aucmd_prepbuf(&aco, buf);
13628#else
13629 save_curbuf = curbuf;
13630 curbuf = buf;
13631#endif
13632
13633 if (*varname == '&')
13634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013635 long numval;
13636 char_u *strval;
13637 int error = FALSE;
13638
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013640 numval = get_tv_number_chk(varp, &error);
13641 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013642 if (!error && strval != NULL)
13643 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644 }
13645 else
13646 {
13647 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13648 if (bufvarname != NULL)
13649 {
13650 STRCPY(bufvarname, "b:");
13651 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013652 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653 vim_free(bufvarname);
13654 }
13655 }
13656
13657 /* reset notion of buffer */
13658#ifdef FEAT_AUTOCMD
13659 aucmd_restbuf(&aco);
13660#else
13661 curbuf = save_curbuf;
13662#endif
13663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664}
13665
13666/*
13667 * "setcmdpos()" function
13668 */
13669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013671 typval_T *argvars;
13672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013674 int pos = (int)get_tv_number(&argvars[0]) - 1;
13675
13676 if (pos >= 0)
13677 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678}
13679
13680/*
13681 * "setline()" function
13682 */
13683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013684f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013685 typval_T *argvars;
13686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687{
13688 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013689 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013690 list_T *l = NULL;
13691 listitem_T *li = NULL;
13692 long added = 0;
13693 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013695 lnum = get_tv_lnum(&argvars[0]);
13696 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013698 l = argvars[1].vval.v_list;
13699 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013701 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013702 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013703
13704 rettv->vval.v_number = 0; /* OK */
13705 for (;;)
13706 {
13707 if (l != NULL)
13708 {
13709 /* list argument, get next string */
13710 if (li == NULL)
13711 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013712 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013713 li = li->li_next;
13714 }
13715
13716 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013717 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013718 break;
13719 if (lnum <= curbuf->b_ml.ml_line_count)
13720 {
13721 /* existing line, replace it */
13722 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13723 {
13724 changed_bytes(lnum, 0);
13725 check_cursor_col();
13726 rettv->vval.v_number = 0; /* OK */
13727 }
13728 }
13729 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13730 {
13731 /* lnum is one past the last line, append the line */
13732 ++added;
13733 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13734 rettv->vval.v_number = 0; /* OK */
13735 }
13736
13737 if (l == NULL) /* only one string argument */
13738 break;
13739 ++lnum;
13740 }
13741
13742 if (added > 0)
13743 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744}
13745
13746/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013747 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013748 */
13749/*ARGSUSED*/
13750 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013751set_qf_ll_list(wp, list_arg, action_arg, rettv)
13752 win_T *wp;
13753 typval_T *list_arg;
13754 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013755 typval_T *rettv;
13756{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013757#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013758 char_u *act;
13759 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013760#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013761
Bram Moolenaar2641f772005-03-25 21:58:17 +000013762 rettv->vval.v_number = -1;
13763
13764#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013765 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013766 EMSG(_(e_listreq));
13767 else
13768 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013769 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013770
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013771 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013772 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013773 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013774 if (act == NULL)
13775 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013776 if (*act == 'a' || *act == 'r')
13777 action = *act;
13778 }
13779
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013780 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013781 rettv->vval.v_number = 0;
13782 }
13783#endif
13784}
13785
13786/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013787 * "setloclist()" function
13788 */
13789/*ARGSUSED*/
13790 static void
13791f_setloclist(argvars, rettv)
13792 typval_T *argvars;
13793 typval_T *rettv;
13794{
13795 win_T *win;
13796
13797 rettv->vval.v_number = -1;
13798
13799 win = find_win_by_nr(&argvars[0]);
13800 if (win != NULL)
13801 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13802}
13803
13804/*
13805 * "setqflist()" function
13806 */
13807/*ARGSUSED*/
13808 static void
13809f_setqflist(argvars, rettv)
13810 typval_T *argvars;
13811 typval_T *rettv;
13812{
13813 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
13814}
13815
13816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 * "setreg()" function
13818 */
13819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013820f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013821 typval_T *argvars;
13822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823{
13824 int regname;
13825 char_u *strregname;
13826 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013827 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 int append;
13829 char_u yank_type;
13830 long block_len;
13831
13832 block_len = -1;
13833 yank_type = MAUTO;
13834 append = FALSE;
13835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013836 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013837 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013839 if (strregname == NULL)
13840 return; /* type error; errmsg already given */
13841 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 if (regname == 0 || regname == '@')
13843 regname = '"';
13844 else if (regname == '=')
13845 return;
13846
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013847 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013849 stropt = get_tv_string_chk(&argvars[2]);
13850 if (stropt == NULL)
13851 return; /* type error */
13852 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 switch (*stropt)
13854 {
13855 case 'a': case 'A': /* append */
13856 append = TRUE;
13857 break;
13858 case 'v': case 'c': /* character-wise selection */
13859 yank_type = MCHAR;
13860 break;
13861 case 'V': case 'l': /* line-wise selection */
13862 yank_type = MLINE;
13863 break;
13864#ifdef FEAT_VISUAL
13865 case 'b': case Ctrl_V: /* block-wise selection */
13866 yank_type = MBLOCK;
13867 if (VIM_ISDIGIT(stropt[1]))
13868 {
13869 ++stropt;
13870 block_len = getdigits(&stropt) - 1;
13871 --stropt;
13872 }
13873 break;
13874#endif
13875 }
13876 }
13877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013878 strval = get_tv_string_chk(&argvars[1]);
13879 if (strval != NULL)
13880 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013882 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883}
13884
13885
13886/*
13887 * "setwinvar(expr)" function
13888 */
13889/*ARGSUSED*/
13890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013891f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013892 typval_T *argvars;
13893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894{
13895 win_T *win;
13896#ifdef FEAT_WINDOWS
13897 win_T *save_curwin;
13898#endif
13899 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013900 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 char_u nbuf[NUMBUFLEN];
13902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013903 rettv->vval.v_number = 0;
13904
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 if (check_restricted() || check_secure())
13906 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013908 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 varp = &argvars[2];
13910
13911 if (win != NULL && varname != NULL && varp != NULL)
13912 {
13913#ifdef FEAT_WINDOWS
13914 /* set curwin to be our win, temporarily */
13915 save_curwin = curwin;
13916 curwin = win;
13917 curbuf = curwin->w_buffer;
13918#endif
13919
13920 if (*varname == '&')
13921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013922 long numval;
13923 char_u *strval;
13924 int error = FALSE;
13925
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013927 numval = get_tv_number_chk(varp, &error);
13928 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013929 if (!error && strval != NULL)
13930 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931 }
13932 else
13933 {
13934 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13935 if (winvarname != NULL)
13936 {
13937 STRCPY(winvarname, "w:");
13938 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013939 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940 vim_free(winvarname);
13941 }
13942 }
13943
13944#ifdef FEAT_WINDOWS
13945 /* Restore current window, if it's still valid (autocomands can make
13946 * it invalid). */
13947 if (win_valid(save_curwin))
13948 {
13949 curwin = save_curwin;
13950 curbuf = curwin->w_buffer;
13951 }
13952#endif
13953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954}
13955
13956/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013957 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 */
13959 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013960f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013961 typval_T *argvars;
13962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013964 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965
Bram Moolenaar0d660222005-01-07 21:51:51 +000013966 p = get_tv_string(&argvars[0]);
13967 rettv->vval.v_string = vim_strsave(p);
13968 simplify_filename(rettv->vval.v_string); /* simplify in place */
13969 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970}
13971
Bram Moolenaar0d660222005-01-07 21:51:51 +000013972static int
13973#ifdef __BORLANDC__
13974 _RTLENTRYF
13975#endif
13976 item_compare __ARGS((const void *s1, const void *s2));
13977static int
13978#ifdef __BORLANDC__
13979 _RTLENTRYF
13980#endif
13981 item_compare2 __ARGS((const void *s1, const void *s2));
13982
13983static int item_compare_ic;
13984static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013986#define ITEM_COMPARE_FAIL 999
13987
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013989 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013991 static int
13992#ifdef __BORLANDC__
13993_RTLENTRYF
13994#endif
13995item_compare(s1, s2)
13996 const void *s1;
13997 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013999 char_u *p1, *p2;
14000 char_u *tofree1, *tofree2;
14001 int res;
14002 char_u numbuf1[NUMBUFLEN];
14003 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014005 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14006 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014007 if (item_compare_ic)
14008 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014010 res = STRCMP(p1, p2);
14011 vim_free(tofree1);
14012 vim_free(tofree2);
14013 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014}
14015
14016 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014017#ifdef __BORLANDC__
14018_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014020item_compare2(s1, s2)
14021 const void *s1;
14022 const void *s2;
14023{
14024 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014025 typval_T rettv;
14026 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014027 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014029 /* shortcut after failure in previous call; compare all items equal */
14030 if (item_compare_func_err)
14031 return 0;
14032
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014033 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14034 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014035 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14036 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014037
14038 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14039 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014040 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014041 clear_tv(&argv[0]);
14042 clear_tv(&argv[1]);
14043
14044 if (res == FAIL)
14045 res = ITEM_COMPARE_FAIL;
14046 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014047 /* return value has wrong type */
14048 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14049 if (item_compare_func_err)
14050 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014051 clear_tv(&rettv);
14052 return res;
14053}
14054
14055/*
14056 * "sort({list})" function
14057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014059f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014060 typval_T *argvars;
14061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062{
Bram Moolenaar33570922005-01-25 22:26:29 +000014063 list_T *l;
14064 listitem_T *li;
14065 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014066 long len;
14067 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068
Bram Moolenaar0d660222005-01-07 21:51:51 +000014069 rettv->vval.v_number = 0;
14070 if (argvars[0].v_type != VAR_LIST)
14071 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072 else
14073 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014074 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014075 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014076 return;
14077 rettv->vval.v_list = l;
14078 rettv->v_type = VAR_LIST;
14079 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080
Bram Moolenaar0d660222005-01-07 21:51:51 +000014081 len = list_len(l);
14082 if (len <= 1)
14083 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084
Bram Moolenaar0d660222005-01-07 21:51:51 +000014085 item_compare_ic = FALSE;
14086 item_compare_func = NULL;
14087 if (argvars[1].v_type != VAR_UNKNOWN)
14088 {
14089 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014090 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014091 else
14092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014093 int error = FALSE;
14094
14095 i = get_tv_number_chk(&argvars[1], &error);
14096 if (error)
14097 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014098 if (i == 1)
14099 item_compare_ic = TRUE;
14100 else
14101 item_compare_func = get_tv_string(&argvars[1]);
14102 }
14103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104
Bram Moolenaar0d660222005-01-07 21:51:51 +000014105 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014106 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014107 if (ptrs == NULL)
14108 return;
14109 i = 0;
14110 for (li = l->lv_first; li != NULL; li = li->li_next)
14111 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014113 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014114 /* test the compare function */
14115 if (item_compare_func != NULL
14116 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14117 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014118 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014120 {
14121 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014122 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014123 item_compare_func == NULL ? item_compare : item_compare2);
14124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014125 if (!item_compare_func_err)
14126 {
14127 /* Clear the List and append the items in the sorted order. */
14128 l->lv_first = l->lv_last = NULL;
14129 l->lv_len = 0;
14130 for (i = 0; i < len; ++i)
14131 list_append(l, ptrs[i]);
14132 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014133 }
14134
14135 vim_free(ptrs);
14136 }
14137}
14138
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014139/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014140 * "soundfold({word})" function
14141 */
14142 static void
14143f_soundfold(argvars, rettv)
14144 typval_T *argvars;
14145 typval_T *rettv;
14146{
14147 char_u *s;
14148
14149 rettv->v_type = VAR_STRING;
14150 s = get_tv_string(&argvars[0]);
14151#ifdef FEAT_SYN_HL
14152 rettv->vval.v_string = eval_soundfold(s);
14153#else
14154 rettv->vval.v_string = vim_strsave(s);
14155#endif
14156}
14157
14158/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014159 * "spellbadword()" function
14160 */
14161/* ARGSUSED */
14162 static void
14163f_spellbadword(argvars, rettv)
14164 typval_T *argvars;
14165 typval_T *rettv;
14166{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014167 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014168 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014169 int len = 0;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014170 list_T *l;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014171
Bram Moolenaar4463f292005-09-25 22:20:24 +000014172 l = list_alloc();
14173 if (l == NULL)
14174 return;
14175 rettv->v_type = VAR_LIST;
14176 rettv->vval.v_list = l;
14177 ++l->lv_refcount;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014178
14179#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014180 if (argvars[0].v_type == VAR_UNKNOWN)
14181 {
14182 /* Find the start and length of the badly spelled word. */
14183 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14184 if (len != 0)
14185 word = ml_get_cursor();
14186 }
14187 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14188 {
14189 char_u *str = get_tv_string_chk(&argvars[0]);
14190 int capcol = -1;
14191
14192 if (str != NULL)
14193 {
14194 /* Check the argument for spelling. */
14195 while (*str != NUL)
14196 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014197 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014198 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014199 {
14200 word = str;
14201 break;
14202 }
14203 str += len;
14204 }
14205 }
14206 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014207#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014208
14209 list_append_string(l, word, len);
14210 list_append_string(l, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014211 attr == HLF_SPB ? "bad" :
14212 attr == HLF_SPR ? "rare" :
14213 attr == HLF_SPL ? "local" :
14214 attr == HLF_SPC ? "caps" :
14215 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014216}
14217
14218/*
14219 * "spellsuggest()" function
14220 */
14221 static void
14222f_spellsuggest(argvars, rettv)
14223 typval_T *argvars;
14224 typval_T *rettv;
14225{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014226 list_T *l;
14227#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014228 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014229 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014230 int maxcount;
14231 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014232 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014233 listitem_T *li;
14234 int need_capital = FALSE;
14235#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014236
14237 l = list_alloc();
14238 if (l == NULL)
14239 return;
14240 rettv->v_type = VAR_LIST;
14241 rettv->vval.v_list = l;
14242 ++l->lv_refcount;
14243
14244#ifdef FEAT_SYN_HL
14245 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14246 {
14247 str = get_tv_string(&argvars[0]);
14248 if (argvars[1].v_type != VAR_UNKNOWN)
14249 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014250 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014251 if (maxcount <= 0)
14252 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014253 if (argvars[2].v_type != VAR_UNKNOWN)
14254 {
14255 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14256 if (typeerr)
14257 return;
14258 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014259 }
14260 else
14261 maxcount = 25;
14262
Bram Moolenaar4770d092006-01-12 23:22:24 +000014263 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014264
14265 for (i = 0; i < ga.ga_len; ++i)
14266 {
14267 str = ((char_u **)ga.ga_data)[i];
14268
14269 li = listitem_alloc();
14270 if (li == NULL)
14271 vim_free(str);
14272 else
14273 {
14274 li->li_tv.v_type = VAR_STRING;
14275 li->li_tv.v_lock = 0;
14276 li->li_tv.vval.v_string = str;
14277 list_append(l, li);
14278 }
14279 }
14280 ga_clear(&ga);
14281 }
14282#endif
14283}
14284
Bram Moolenaar0d660222005-01-07 21:51:51 +000014285 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014286f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014287 typval_T *argvars;
14288 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014289{
14290 char_u *str;
14291 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014292 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014293 regmatch_T regmatch;
14294 char_u patbuf[NUMBUFLEN];
14295 char_u *save_cpo;
14296 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000014297 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014298 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014299 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014300 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014301
14302 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14303 save_cpo = p_cpo;
14304 p_cpo = (char_u *)"";
14305
14306 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014307 if (argvars[1].v_type != VAR_UNKNOWN)
14308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014309 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14310 if (pat == NULL)
14311 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014312 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014313 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014314 }
14315 if (pat == NULL || *pat == NUL)
14316 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014317
14318 l = list_alloc();
14319 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014321 rettv->v_type = VAR_LIST;
14322 rettv->vval.v_list = l;
14323 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014324 if (typeerr)
14325 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326
Bram Moolenaar0d660222005-01-07 21:51:51 +000014327 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14328 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014330 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014331 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014332 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014333 if (*str == NUL)
14334 match = FALSE; /* empty item at the end */
14335 else
14336 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014337 if (match)
14338 end = regmatch.startp[0];
14339 else
14340 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014341 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
14342 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014343 {
Bram Moolenaar4463f292005-09-25 22:20:24 +000014344 if (list_append_string(l, str, (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014345 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014346 }
14347 if (!match)
14348 break;
14349 /* Advance to just after the match. */
14350 if (regmatch.endp[0] > str)
14351 col = 0;
14352 else
14353 {
14354 /* Don't get stuck at the same match. */
14355#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014356 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014357#else
14358 col = 1;
14359#endif
14360 }
14361 str = regmatch.endp[0];
14362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363
Bram Moolenaar0d660222005-01-07 21:51:51 +000014364 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366
Bram Moolenaar0d660222005-01-07 21:51:51 +000014367 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368}
14369
14370#ifdef HAVE_STRFTIME
14371/*
14372 * "strftime({format}[, {time}])" function
14373 */
14374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014375f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014376 typval_T *argvars;
14377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378{
14379 char_u result_buf[256];
14380 struct tm *curtime;
14381 time_t seconds;
14382 char_u *p;
14383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014384 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014386 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014387 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 seconds = time(NULL);
14389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014390 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 curtime = localtime(&seconds);
14392 /* MSVC returns NULL for an invalid value of seconds. */
14393 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014394 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 else
14396 {
14397# ifdef FEAT_MBYTE
14398 vimconv_T conv;
14399 char_u *enc;
14400
14401 conv.vc_type = CONV_NONE;
14402 enc = enc_locale();
14403 convert_setup(&conv, p_enc, enc);
14404 if (conv.vc_type != CONV_NONE)
14405 p = string_convert(&conv, p, NULL);
14406# endif
14407 if (p != NULL)
14408 (void)strftime((char *)result_buf, sizeof(result_buf),
14409 (char *)p, curtime);
14410 else
14411 result_buf[0] = NUL;
14412
14413# ifdef FEAT_MBYTE
14414 if (conv.vc_type != CONV_NONE)
14415 vim_free(p);
14416 convert_setup(&conv, enc, p_enc);
14417 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014418 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419 else
14420# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014421 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422
14423# ifdef FEAT_MBYTE
14424 /* Release conversion descriptors */
14425 convert_setup(&conv, NULL, NULL);
14426 vim_free(enc);
14427# endif
14428 }
14429}
14430#endif
14431
14432/*
14433 * "stridx()" function
14434 */
14435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014436f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014437 typval_T *argvars;
14438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439{
14440 char_u buf[NUMBUFLEN];
14441 char_u *needle;
14442 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014443 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014445 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014447 needle = get_tv_string_chk(&argvars[1]);
14448 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014449 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014450 if (needle == NULL || haystack == NULL)
14451 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452
Bram Moolenaar33570922005-01-25 22:26:29 +000014453 if (argvars[2].v_type != VAR_UNKNOWN)
14454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014455 int error = FALSE;
14456
14457 start_idx = get_tv_number_chk(&argvars[2], &error);
14458 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014459 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014460 if (start_idx >= 0)
14461 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014462 }
14463
14464 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14465 if (pos != NULL)
14466 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467}
14468
14469/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014470 * "string()" function
14471 */
14472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014473f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014474 typval_T *argvars;
14475 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014476{
14477 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014478 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014480 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014481 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014482 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014483 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484}
14485
14486/*
14487 * "strlen()" function
14488 */
14489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014490f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014491 typval_T *argvars;
14492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014494 rettv->vval.v_number = (varnumber_T)(STRLEN(
14495 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496}
14497
14498/*
14499 * "strpart()" function
14500 */
14501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014502f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014503 typval_T *argvars;
14504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505{
14506 char_u *p;
14507 int n;
14508 int len;
14509 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014510 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014512 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 slen = (int)STRLEN(p);
14514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014515 n = get_tv_number_chk(&argvars[1], &error);
14516 if (error)
14517 len = 0;
14518 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014519 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520 else
14521 len = slen - n; /* default len: all bytes that are available. */
14522
14523 /*
14524 * Only return the overlap between the specified part and the actual
14525 * string.
14526 */
14527 if (n < 0)
14528 {
14529 len += n;
14530 n = 0;
14531 }
14532 else if (n > slen)
14533 n = slen;
14534 if (len < 0)
14535 len = 0;
14536 else if (n + len > slen)
14537 len = slen - n;
14538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014539 rettv->v_type = VAR_STRING;
14540 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541}
14542
14543/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014544 * "strridx()" function
14545 */
14546 static void
14547f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014548 typval_T *argvars;
14549 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014550{
14551 char_u buf[NUMBUFLEN];
14552 char_u *needle;
14553 char_u *haystack;
14554 char_u *rest;
14555 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014556 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014557
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014558 needle = get_tv_string_chk(&argvars[1]);
14559 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014560 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014561
14562 rettv->vval.v_number = -1;
14563 if (needle == NULL || haystack == NULL)
14564 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014565 if (argvars[2].v_type != VAR_UNKNOWN)
14566 {
14567 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014568 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014569 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014570 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014571 }
14572 else
14573 end_idx = haystack_len;
14574
Bram Moolenaar0d660222005-01-07 21:51:51 +000014575 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014576 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014577 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014578 lastmatch = haystack + end_idx;
14579 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014580 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014581 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014582 for (rest = haystack; *rest != '\0'; ++rest)
14583 {
14584 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014585 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014586 break;
14587 lastmatch = rest;
14588 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014589 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014590
14591 if (lastmatch == NULL)
14592 rettv->vval.v_number = -1;
14593 else
14594 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14595}
14596
14597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 * "strtrans()" function
14599 */
14600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014601f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014602 typval_T *argvars;
14603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014605 rettv->v_type = VAR_STRING;
14606 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607}
14608
14609/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014610 * "submatch()" function
14611 */
14612 static void
14613f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014614 typval_T *argvars;
14615 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014616{
14617 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014618 rettv->vval.v_string =
14619 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014620}
14621
14622/*
14623 * "substitute()" function
14624 */
14625 static void
14626f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014627 typval_T *argvars;
14628 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014629{
14630 char_u patbuf[NUMBUFLEN];
14631 char_u subbuf[NUMBUFLEN];
14632 char_u flagsbuf[NUMBUFLEN];
14633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014634 char_u *str = get_tv_string_chk(&argvars[0]);
14635 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14636 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14637 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14638
Bram Moolenaar0d660222005-01-07 21:51:51 +000014639 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014640 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14641 rettv->vval.v_string = NULL;
14642 else
14643 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014644}
14645
14646/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014647 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648 */
14649/*ARGSUSED*/
14650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014651f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014652 typval_T *argvars;
14653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654{
14655 int id = 0;
14656#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014657 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014658 long col;
14659 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014660 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014662 lnum = get_tv_lnum(argvars); /* -1 on type error */
14663 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14664 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014666 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014667 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014668 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669#endif
14670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014671 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672}
14673
14674/*
14675 * "synIDattr(id, what [, mode])" function
14676 */
14677/*ARGSUSED*/
14678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014679f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014680 typval_T *argvars;
14681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682{
14683 char_u *p = NULL;
14684#ifdef FEAT_SYN_HL
14685 int id;
14686 char_u *what;
14687 char_u *mode;
14688 char_u modebuf[NUMBUFLEN];
14689 int modec;
14690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014691 id = get_tv_number(&argvars[0]);
14692 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014693 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014695 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014696 modec = TOLOWER_ASC(mode[0]);
14697 if (modec != 't' && modec != 'c'
14698#ifdef FEAT_GUI
14699 && modec != 'g'
14700#endif
14701 )
14702 modec = 0; /* replace invalid with current */
14703 }
14704 else
14705 {
14706#ifdef FEAT_GUI
14707 if (gui.in_use)
14708 modec = 'g';
14709 else
14710#endif
14711 if (t_colors > 1)
14712 modec = 'c';
14713 else
14714 modec = 't';
14715 }
14716
14717
14718 switch (TOLOWER_ASC(what[0]))
14719 {
14720 case 'b':
14721 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14722 p = highlight_color(id, what, modec);
14723 else /* bold */
14724 p = highlight_has_attr(id, HL_BOLD, modec);
14725 break;
14726
14727 case 'f': /* fg[#] */
14728 p = highlight_color(id, what, modec);
14729 break;
14730
14731 case 'i':
14732 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14733 p = highlight_has_attr(id, HL_INVERSE, modec);
14734 else /* italic */
14735 p = highlight_has_attr(id, HL_ITALIC, modec);
14736 break;
14737
14738 case 'n': /* name */
14739 p = get_highlight_name(NULL, id - 1);
14740 break;
14741
14742 case 'r': /* reverse */
14743 p = highlight_has_attr(id, HL_INVERSE, modec);
14744 break;
14745
14746 case 's': /* standout */
14747 p = highlight_has_attr(id, HL_STANDOUT, modec);
14748 break;
14749
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014750 case 'u':
14751 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14752 /* underline */
14753 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14754 else
14755 /* undercurl */
14756 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 break;
14758 }
14759
14760 if (p != NULL)
14761 p = vim_strsave(p);
14762#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014763 rettv->v_type = VAR_STRING;
14764 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765}
14766
14767/*
14768 * "synIDtrans(id)" function
14769 */
14770/*ARGSUSED*/
14771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014772f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014773 typval_T *argvars;
14774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775{
14776 int id;
14777
14778#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014779 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014780
14781 if (id > 0)
14782 id = syn_get_final_id(id);
14783 else
14784#endif
14785 id = 0;
14786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014787 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788}
14789
14790/*
14791 * "system()" function
14792 */
14793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014794f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014795 typval_T *argvars;
14796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014798 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014799 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014800 char_u *infile = NULL;
14801 char_u buf[NUMBUFLEN];
14802 int err = FALSE;
14803 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014805 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014806 {
14807 /*
14808 * Write the string to a temp file, to be used for input of the shell
14809 * command.
14810 */
14811 if ((infile = vim_tempname('i')) == NULL)
14812 {
14813 EMSG(_(e_notmp));
14814 return;
14815 }
14816
14817 fd = mch_fopen((char *)infile, WRITEBIN);
14818 if (fd == NULL)
14819 {
14820 EMSG2(_(e_notopen), infile);
14821 goto done;
14822 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014823 p = get_tv_string_buf_chk(&argvars[1], buf);
14824 if (p == NULL)
14825 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014826 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14827 err = TRUE;
14828 if (fclose(fd) != 0)
14829 err = TRUE;
14830 if (err)
14831 {
14832 EMSG(_("E677: Error writing temp file"));
14833 goto done;
14834 }
14835 }
14836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014837 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014838
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839#ifdef USE_CR
14840 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014841 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842 {
14843 char_u *s;
14844
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014845 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014846 {
14847 if (*s == CAR)
14848 *s = NL;
14849 }
14850 }
14851#else
14852# ifdef USE_CRNL
14853 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014854 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855 {
14856 char_u *s, *d;
14857
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014858 d = res;
14859 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860 {
14861 if (s[0] == CAR && s[1] == NL)
14862 ++s;
14863 *d++ = *s;
14864 }
14865 *d = NUL;
14866 }
14867# endif
14868#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014869
14870done:
14871 if (infile != NULL)
14872 {
14873 mch_remove(infile);
14874 vim_free(infile);
14875 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014876 rettv->v_type = VAR_STRING;
14877 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878}
14879
14880/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014881 * "tabpagebuflist()" function
14882 */
14883/* ARGSUSED */
14884 static void
14885f_tabpagebuflist(argvars, rettv)
14886 typval_T *argvars;
14887 typval_T *rettv;
14888{
14889#ifndef FEAT_WINDOWS
14890 rettv->vval.v_number = 0;
14891#else
14892 tabpage_T *tp;
14893 win_T *wp = NULL;
14894 list_T *l;
14895
14896 if (argvars[0].v_type == VAR_UNKNOWN)
14897 wp = firstwin;
14898 else
14899 {
14900 tp = find_tabpage((int)get_tv_number(&argvars[0]));
14901 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000014902 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014903 }
14904 if (wp == NULL)
14905 rettv->vval.v_number = 0;
14906 else
14907 {
14908 l = list_alloc();
14909 if (l == NULL)
14910 rettv->vval.v_number = 0;
14911 else
14912 {
14913 rettv->vval.v_list = l;
14914 rettv->v_type = VAR_LIST;
14915 ++l->lv_refcount;
14916
14917 for (; wp != NULL; wp = wp->w_next)
14918 if (list_append_number(l, wp->w_buffer->b_fnum) == FAIL)
14919 break;
14920 }
14921 }
14922#endif
14923}
14924
14925
14926/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000014927 * "tabpagenr()" function
14928 */
14929/* ARGSUSED */
14930 static void
14931f_tabpagenr(argvars, rettv)
14932 typval_T *argvars;
14933 typval_T *rettv;
14934{
14935 int nr = 1;
14936#ifdef FEAT_WINDOWS
14937 tabpage_T *tp;
14938 char_u *arg;
14939
14940 if (argvars[0].v_type != VAR_UNKNOWN)
14941 {
14942 arg = get_tv_string_chk(&argvars[0]);
14943 nr = 0;
14944 if (arg != NULL)
14945 {
14946 if (STRCMP(arg, "$") == 0)
14947 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
14948 ++nr;
14949 else
14950 EMSG2(_(e_invexpr2), arg);
14951 }
14952 }
14953 else
14954 for (tp = first_tabpage; tp != curtab; tp = tp->tp_next)
14955 ++nr;
14956#endif
14957 rettv->vval.v_number = nr;
14958}
14959
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014960
14961#ifdef FEAT_WINDOWS
14962static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
14963
14964/*
14965 * Common code for tabpagewinnr() and winnr().
14966 */
14967 static int
14968get_winnr(tp, argvar)
14969 tabpage_T *tp;
14970 typval_T *argvar;
14971{
14972 win_T *twin;
14973 int nr = 1;
14974 win_T *wp;
14975 char_u *arg;
14976
14977 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14978 if (argvar->v_type != VAR_UNKNOWN)
14979 {
14980 arg = get_tv_string_chk(argvar);
14981 if (arg == NULL)
14982 nr = 0; /* type error; errmsg already given */
14983 else if (STRCMP(arg, "$") == 0)
14984 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14985 else if (STRCMP(arg, "#") == 0)
14986 {
14987 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14988 if (twin == NULL)
14989 nr = 0;
14990 }
14991 else
14992 {
14993 EMSG2(_(e_invexpr2), arg);
14994 nr = 0;
14995 }
14996 }
14997
14998 if (nr > 0)
14999 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15000 wp != twin; wp = wp->w_next)
15001 ++nr;
15002 return nr;
15003}
15004#endif
15005
15006/*
15007 * "tabpagewinnr()" function
15008 */
15009/* ARGSUSED */
15010 static void
15011f_tabpagewinnr(argvars, rettv)
15012 typval_T *argvars;
15013 typval_T *rettv;
15014{
15015 int nr = 1;
15016#ifdef FEAT_WINDOWS
15017 tabpage_T *tp;
15018
15019 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15020 if (tp == NULL)
15021 nr = 0;
15022 else
15023 nr = get_winnr(tp, &argvars[1]);
15024#endif
15025 rettv->vval.v_number = nr;
15026}
15027
15028
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015029/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015030 * "tagfiles()" function
15031 */
15032/*ARGSUSED*/
15033 static void
15034f_tagfiles(argvars, rettv)
15035 typval_T *argvars;
15036 typval_T *rettv;
15037{
15038 char_u fname[MAXPATHL + 1];
15039 list_T *l;
15040
15041 l = list_alloc();
15042 if (l == NULL)
15043 {
15044 rettv->vval.v_number = 0;
15045 return;
15046 }
15047 rettv->vval.v_list = l;
15048 rettv->v_type = VAR_LIST;
15049 ++l->lv_refcount;
15050
15051 get_tagfname(TRUE, NULL);
15052 for (;;)
15053 if (get_tagfname(FALSE, fname) == FAIL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015054 || list_append_string(l, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015055 break;
15056}
15057
15058/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015059 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015060 */
15061 static void
15062f_taglist(argvars, rettv)
15063 typval_T *argvars;
15064 typval_T *rettv;
15065{
15066 char_u *tag_pattern;
15067 list_T *l;
15068
15069 tag_pattern = get_tv_string(&argvars[0]);
15070
15071 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015072 if (*tag_pattern == NUL)
15073 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015074
15075 l = list_alloc();
15076 if (l != NULL)
15077 {
15078 if (get_tags(l, tag_pattern) != FAIL)
15079 {
15080 rettv->vval.v_list = l;
15081 rettv->v_type = VAR_LIST;
15082 ++l->lv_refcount;
15083 }
15084 else
15085 list_free(l);
15086 }
15087}
15088
15089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090 * "tempname()" function
15091 */
15092/*ARGSUSED*/
15093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015094f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015095 typval_T *argvars;
15096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097{
15098 static int x = 'A';
15099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015100 rettv->v_type = VAR_STRING;
15101 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015102
15103 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15104 * names. Skip 'I' and 'O', they are used for shell redirection. */
15105 do
15106 {
15107 if (x == 'Z')
15108 x = '0';
15109 else if (x == '9')
15110 x = 'A';
15111 else
15112 {
15113#ifdef EBCDIC
15114 if (x == 'I')
15115 x = 'J';
15116 else if (x == 'R')
15117 x = 'S';
15118 else
15119#endif
15120 ++x;
15121 }
15122 } while (x == 'I' || x == 'O');
15123}
15124
15125/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015126 * "test(list)" function: Just checking the walls...
15127 */
15128/*ARGSUSED*/
15129 static void
15130f_test(argvars, rettv)
15131 typval_T *argvars;
15132 typval_T *rettv;
15133{
15134 /* Used for unit testing. Change the code below to your liking. */
15135#if 0
15136 listitem_T *li;
15137 list_T *l;
15138 char_u *bad, *good;
15139
15140 if (argvars[0].v_type != VAR_LIST)
15141 return;
15142 l = argvars[0].vval.v_list;
15143 if (l == NULL)
15144 return;
15145 li = l->lv_first;
15146 if (li == NULL)
15147 return;
15148 bad = get_tv_string(&li->li_tv);
15149 li = li->li_next;
15150 if (li == NULL)
15151 return;
15152 good = get_tv_string(&li->li_tv);
15153 rettv->vval.v_number = test_edit_score(bad, good);
15154#endif
15155}
15156
15157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158 * "tolower(string)" function
15159 */
15160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015161f_tolower(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{
15165 char_u *p;
15166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015167 p = vim_strsave(get_tv_string(&argvars[0]));
15168 rettv->v_type = VAR_STRING;
15169 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170
15171 if (p != NULL)
15172 while (*p != NUL)
15173 {
15174#ifdef FEAT_MBYTE
15175 int l;
15176
15177 if (enc_utf8)
15178 {
15179 int c, lc;
15180
15181 c = utf_ptr2char(p);
15182 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015183 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184 /* TODO: reallocate string when byte count changes. */
15185 if (utf_char2len(lc) == l)
15186 utf_char2bytes(lc, p);
15187 p += l;
15188 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015189 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190 p += l; /* skip multi-byte character */
15191 else
15192#endif
15193 {
15194 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15195 ++p;
15196 }
15197 }
15198}
15199
15200/*
15201 * "toupper(string)" function
15202 */
15203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015204f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015205 typval_T *argvars;
15206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015208 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015209 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210}
15211
15212/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015213 * "tr(string, fromstr, tostr)" function
15214 */
15215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015216f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015217 typval_T *argvars;
15218 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015219{
15220 char_u *instr;
15221 char_u *fromstr;
15222 char_u *tostr;
15223 char_u *p;
15224#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015225 int inlen;
15226 int fromlen;
15227 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015228 int idx;
15229 char_u *cpstr;
15230 int cplen;
15231 int first = TRUE;
15232#endif
15233 char_u buf[NUMBUFLEN];
15234 char_u buf2[NUMBUFLEN];
15235 garray_T ga;
15236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015237 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015238 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15239 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015240
15241 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015242 rettv->v_type = VAR_STRING;
15243 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015244 if (fromstr == NULL || tostr == NULL)
15245 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015246 ga_init2(&ga, (int)sizeof(char), 80);
15247
15248#ifdef FEAT_MBYTE
15249 if (!has_mbyte)
15250#endif
15251 /* not multi-byte: fromstr and tostr must be the same length */
15252 if (STRLEN(fromstr) != STRLEN(tostr))
15253 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015254#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015255error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015256#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015257 EMSG2(_(e_invarg2), fromstr);
15258 ga_clear(&ga);
15259 return;
15260 }
15261
15262 /* fromstr and tostr have to contain the same number of chars */
15263 while (*instr != NUL)
15264 {
15265#ifdef FEAT_MBYTE
15266 if (has_mbyte)
15267 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015268 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015269 cpstr = instr;
15270 cplen = inlen;
15271 idx = 0;
15272 for (p = fromstr; *p != NUL; p += fromlen)
15273 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015274 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015275 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15276 {
15277 for (p = tostr; *p != NUL; p += tolen)
15278 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015279 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015280 if (idx-- == 0)
15281 {
15282 cplen = tolen;
15283 cpstr = p;
15284 break;
15285 }
15286 }
15287 if (*p == NUL) /* tostr is shorter than fromstr */
15288 goto error;
15289 break;
15290 }
15291 ++idx;
15292 }
15293
15294 if (first && cpstr == instr)
15295 {
15296 /* Check that fromstr and tostr have the same number of
15297 * (multi-byte) characters. Done only once when a character
15298 * of instr doesn't appear in fromstr. */
15299 first = FALSE;
15300 for (p = tostr; *p != NUL; p += tolen)
15301 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015302 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015303 --idx;
15304 }
15305 if (idx != 0)
15306 goto error;
15307 }
15308
15309 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015310 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015311 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015312
15313 instr += inlen;
15314 }
15315 else
15316#endif
15317 {
15318 /* When not using multi-byte chars we can do it faster. */
15319 p = vim_strchr(fromstr, *instr);
15320 if (p != NULL)
15321 ga_append(&ga, tostr[p - fromstr]);
15322 else
15323 ga_append(&ga, *instr);
15324 ++instr;
15325 }
15326 }
15327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015328 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015329}
15330
15331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332 * "type(expr)" function
15333 */
15334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015335f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015336 typval_T *argvars;
15337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015339 int n;
15340
15341 switch (argvars[0].v_type)
15342 {
15343 case VAR_NUMBER: n = 0; break;
15344 case VAR_STRING: n = 1; break;
15345 case VAR_FUNC: n = 2; break;
15346 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015347 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015348 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15349 }
15350 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351}
15352
15353/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015354 * "values(dict)" function
15355 */
15356 static void
15357f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015358 typval_T *argvars;
15359 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015360{
15361 dict_list(argvars, rettv, 1);
15362}
15363
15364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 * "virtcol(string)" function
15366 */
15367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015368f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015369 typval_T *argvars;
15370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371{
15372 colnr_T vcol = 0;
15373 pos_T *fp;
15374
15375 fp = var2fpos(&argvars[0], FALSE);
15376 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
15377 {
15378 getvvcol(curwin, fp, NULL, NULL, &vcol);
15379 ++vcol;
15380 }
15381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015382 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383}
15384
15385/*
15386 * "visualmode()" function
15387 */
15388/*ARGSUSED*/
15389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015391 typval_T *argvars;
15392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393{
15394#ifdef FEAT_VISUAL
15395 char_u str[2];
15396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398 str[0] = curbuf->b_visual_mode_eval;
15399 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401
15402 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015403 if ((argvars[0].v_type == VAR_NUMBER
15404 && argvars[0].vval.v_number != 0)
15405 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407 curbuf->b_visual_mode_eval = NUL;
15408#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410#endif
15411}
15412
15413/*
15414 * "winbufnr(nr)" function
15415 */
15416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015417f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015418 typval_T *argvars;
15419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420{
15421 win_T *wp;
15422
15423 wp = find_win_by_nr(&argvars[0]);
15424 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015425 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015427 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428}
15429
15430/*
15431 * "wincol()" function
15432 */
15433/*ARGSUSED*/
15434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015435f_wincol(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 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015440 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441}
15442
15443/*
15444 * "winheight(nr)" function
15445 */
15446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015448 typval_T *argvars;
15449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450{
15451 win_T *wp;
15452
15453 wp = find_win_by_nr(&argvars[0]);
15454 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015455 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015457 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458}
15459
15460/*
15461 * "winline()" function
15462 */
15463/*ARGSUSED*/
15464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015465f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015466 typval_T *argvars;
15467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468{
15469 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015470 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471}
15472
15473/*
15474 * "winnr()" function
15475 */
15476/* ARGSUSED */
15477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015479 typval_T *argvars;
15480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481{
15482 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015483
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015485 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015487 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015488}
15489
15490/*
15491 * "winrestcmd()" function
15492 */
15493/* ARGSUSED */
15494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015495f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015496 typval_T *argvars;
15497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498{
15499#ifdef FEAT_WINDOWS
15500 win_T *wp;
15501 int winnr = 1;
15502 garray_T ga;
15503 char_u buf[50];
15504
15505 ga_init2(&ga, (int)sizeof(char), 70);
15506 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15507 {
15508 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15509 ga_concat(&ga, buf);
15510# ifdef FEAT_VERTSPLIT
15511 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15512 ga_concat(&ga, buf);
15513# endif
15514 ++winnr;
15515 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015516 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015518 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015520 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015522 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523}
15524
15525/*
15526 * "winwidth(nr)" function
15527 */
15528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015529f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015530 typval_T *argvars;
15531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532{
15533 win_T *wp;
15534
15535 wp = find_win_by_nr(&argvars[0]);
15536 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015537 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538 else
15539#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015540 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015542 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543#endif
15544}
15545
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015547 * "writefile()" function
15548 */
15549 static void
15550f_writefile(argvars, rettv)
15551 typval_T *argvars;
15552 typval_T *rettv;
15553{
15554 int binary = FALSE;
15555 char_u *fname;
15556 FILE *fd;
15557 listitem_T *li;
15558 char_u *s;
15559 int ret = 0;
15560 int c;
15561
15562 if (argvars[0].v_type != VAR_LIST)
15563 {
15564 EMSG2(_(e_listarg), "writefile()");
15565 return;
15566 }
15567 if (argvars[0].vval.v_list == NULL)
15568 return;
15569
15570 if (argvars[2].v_type != VAR_UNKNOWN
15571 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15572 binary = TRUE;
15573
15574 /* Always open the file in binary mode, library functions have a mind of
15575 * their own about CR-LF conversion. */
15576 fname = get_tv_string(&argvars[1]);
15577 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15578 {
15579 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15580 ret = -1;
15581 }
15582 else
15583 {
15584 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15585 li = li->li_next)
15586 {
15587 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15588 {
15589 if (*s == '\n')
15590 c = putc(NUL, fd);
15591 else
15592 c = putc(*s, fd);
15593 if (c == EOF)
15594 {
15595 ret = -1;
15596 break;
15597 }
15598 }
15599 if (!binary || li->li_next != NULL)
15600 if (putc('\n', fd) == EOF)
15601 {
15602 ret = -1;
15603 break;
15604 }
15605 if (ret < 0)
15606 {
15607 EMSG(_(e_write));
15608 break;
15609 }
15610 }
15611 fclose(fd);
15612 }
15613
15614 rettv->vval.v_number = ret;
15615}
15616
15617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618 * Translate a String variable into a position.
15619 */
15620 static pos_T *
15621var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015622 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 int lnum; /* TRUE when $ is last line */
15624{
15625 char_u *name;
15626 static pos_T pos;
15627 pos_T *pp;
15628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015629 name = get_tv_string_chk(varp);
15630 if (name == NULL)
15631 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 if (name[0] == '.') /* cursor */
15633 return &curwin->w_cursor;
15634 if (name[0] == '\'') /* mark */
15635 {
15636 pp = getmark(name[1], FALSE);
15637 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15638 return NULL;
15639 return pp;
15640 }
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015641 if (name[0] == 'w' && lnum)
15642 {
15643 pos.col = 0;
15644 if (name[1] == '0') /* "w0": first visible line */
15645 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015646 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015647 pos.lnum = curwin->w_topline;
15648 return &pos;
15649 }
15650 else if (name[1] == '$') /* "w$": last visible line */
15651 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015652 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015653 pos.lnum = curwin->w_botline - 1;
15654 return &pos;
15655 }
15656 }
15657 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 {
15659 if (lnum)
15660 {
15661 pos.lnum = curbuf->b_ml.ml_line_count;
15662 pos.col = 0;
15663 }
15664 else
15665 {
15666 pos.lnum = curwin->w_cursor.lnum;
15667 pos.col = (colnr_T)STRLEN(ml_get_curline());
15668 }
15669 return &pos;
15670 }
15671 return NULL;
15672}
15673
15674/*
15675 * Get the length of an environment variable name.
15676 * Advance "arg" to the first character after the name.
15677 * Return 0 for error.
15678 */
15679 static int
15680get_env_len(arg)
15681 char_u **arg;
15682{
15683 char_u *p;
15684 int len;
15685
15686 for (p = *arg; vim_isIDc(*p); ++p)
15687 ;
15688 if (p == *arg) /* no name found */
15689 return 0;
15690
15691 len = (int)(p - *arg);
15692 *arg = p;
15693 return len;
15694}
15695
15696/*
15697 * Get the length of the name of a function or internal variable.
15698 * "arg" is advanced to the first non-white character after the name.
15699 * Return 0 if something is wrong.
15700 */
15701 static int
15702get_id_len(arg)
15703 char_u **arg;
15704{
15705 char_u *p;
15706 int len;
15707
15708 /* Find the end of the name. */
15709 for (p = *arg; eval_isnamec(*p); ++p)
15710 ;
15711 if (p == *arg) /* no name found */
15712 return 0;
15713
15714 len = (int)(p - *arg);
15715 *arg = skipwhite(p);
15716
15717 return len;
15718}
15719
15720/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015721 * Get the length of the name of a variable or function.
15722 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015724 * Return -1 if curly braces expansion failed.
15725 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726 * If the name contains 'magic' {}'s, expand them and return the
15727 * expanded name in an allocated string via 'alias' - caller must free.
15728 */
15729 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015730get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731 char_u **arg;
15732 char_u **alias;
15733 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015734 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735{
15736 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737 char_u *p;
15738 char_u *expr_start;
15739 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740
15741 *alias = NULL; /* default to no alias */
15742
15743 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15744 && (*arg)[2] == (int)KE_SNR)
15745 {
15746 /* hard coded <SNR>, already translated */
15747 *arg += 3;
15748 return get_id_len(arg) + 3;
15749 }
15750 len = eval_fname_script(*arg);
15751 if (len > 0)
15752 {
15753 /* literal "<SID>", "s:" or "<SNR>" */
15754 *arg += len;
15755 }
15756
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015758 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015760 p = find_name_end(*arg, &expr_start, &expr_end,
15761 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762 if (expr_start != NULL)
15763 {
15764 char_u *temp_string;
15765
15766 if (!evaluate)
15767 {
15768 len += (int)(p - *arg);
15769 *arg = skipwhite(p);
15770 return len;
15771 }
15772
15773 /*
15774 * Include any <SID> etc in the expanded string:
15775 * Thus the -len here.
15776 */
15777 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15778 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015779 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 *alias = temp_string;
15781 *arg = skipwhite(p);
15782 return (int)STRLEN(temp_string);
15783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784
15785 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015786 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787 EMSG2(_(e_invexpr2), *arg);
15788
15789 return len;
15790}
15791
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015792/*
15793 * Find the end of a variable or function name, taking care of magic braces.
15794 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15795 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015796 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015797 * Return a pointer to just after the name. Equal to "arg" if there is no
15798 * valid name.
15799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015801find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802 char_u *arg;
15803 char_u **expr_start;
15804 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015805 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015807 int mb_nest = 0;
15808 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 char_u *p;
15810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015811 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015813 *expr_start = NULL;
15814 *expr_end = NULL;
15815 }
15816
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015817 /* Quick check for valid starting character. */
15818 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15819 return arg;
15820
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015821 for (p = arg; *p != NUL
15822 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015823 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015824 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015825 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015826 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015827 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015828 if (*p == '\'')
15829 {
15830 /* skip over 'string' to avoid counting [ and ] inside it. */
15831 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15832 ;
15833 if (*p == NUL)
15834 break;
15835 }
15836 else if (*p == '"')
15837 {
15838 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15839 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15840 if (*p == '\\' && p[1] != NUL)
15841 ++p;
15842 if (*p == NUL)
15843 break;
15844 }
15845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015846 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015848 if (*p == '[')
15849 ++br_nest;
15850 else if (*p == ']')
15851 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015854 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015856 if (*p == '{')
15857 {
15858 mb_nest++;
15859 if (expr_start != NULL && *expr_start == NULL)
15860 *expr_start = p;
15861 }
15862 else if (*p == '}')
15863 {
15864 mb_nest--;
15865 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15866 *expr_end = p;
15867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 }
15870
15871 return p;
15872}
15873
15874/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015875 * Expands out the 'magic' {}'s in a variable/function name.
15876 * Note that this can call itself recursively, to deal with
15877 * constructs like foo{bar}{baz}{bam}
15878 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15879 * "in_start" ^
15880 * "expr_start" ^
15881 * "expr_end" ^
15882 * "in_end" ^
15883 *
15884 * Returns a new allocated string, which the caller must free.
15885 * Returns NULL for failure.
15886 */
15887 static char_u *
15888make_expanded_name(in_start, expr_start, expr_end, in_end)
15889 char_u *in_start;
15890 char_u *expr_start;
15891 char_u *expr_end;
15892 char_u *in_end;
15893{
15894 char_u c1;
15895 char_u *retval = NULL;
15896 char_u *temp_result;
15897 char_u *nextcmd = NULL;
15898
15899 if (expr_end == NULL || in_end == NULL)
15900 return NULL;
15901 *expr_start = NUL;
15902 *expr_end = NUL;
15903 c1 = *in_end;
15904 *in_end = NUL;
15905
15906 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15907 if (temp_result != NULL && nextcmd == NULL)
15908 {
15909 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15910 + (in_end - expr_end) + 1));
15911 if (retval != NULL)
15912 {
15913 STRCPY(retval, in_start);
15914 STRCAT(retval, temp_result);
15915 STRCAT(retval, expr_end + 1);
15916 }
15917 }
15918 vim_free(temp_result);
15919
15920 *in_end = c1; /* put char back for error messages */
15921 *expr_start = '{';
15922 *expr_end = '}';
15923
15924 if (retval != NULL)
15925 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015926 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015927 if (expr_start != NULL)
15928 {
15929 /* Further expansion! */
15930 temp_result = make_expanded_name(retval, expr_start,
15931 expr_end, temp_result);
15932 vim_free(retval);
15933 retval = temp_result;
15934 }
15935 }
15936
15937 return retval;
15938}
15939
15940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015942 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943 */
15944 static int
15945eval_isnamec(c)
15946 int c;
15947{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015948 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15949}
15950
15951/*
15952 * Return TRUE if character "c" can be used as the first character in a
15953 * variable or function name (excluding '{' and '}').
15954 */
15955 static int
15956eval_isnamec1(c)
15957 int c;
15958{
15959 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960}
15961
15962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015963 * Set number v: variable to "val".
15964 */
15965 void
15966set_vim_var_nr(idx, val)
15967 int idx;
15968 long val;
15969{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015970 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015971}
15972
15973/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015974 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015975 */
15976 long
15977get_vim_var_nr(idx)
15978 int idx;
15979{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015980 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015981}
15982
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015983#if defined(FEAT_AUTOCMD) || defined(PROTO)
15984/*
15985 * Get string v: variable value. Uses a static buffer, can only be used once.
15986 */
15987 char_u *
15988get_vim_var_str(idx)
15989 int idx;
15990{
15991 return get_tv_string(&vimvars[idx].vv_tv);
15992}
15993#endif
15994
Bram Moolenaar071d4272004-06-13 20:20:40 +000015995/*
15996 * Set v:count, v:count1 and v:prevcount.
15997 */
15998 void
15999set_vcount(count, count1)
16000 long count;
16001 long count1;
16002{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016003 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16004 vimvars[VV_COUNT].vv_nr = count;
16005 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006}
16007
16008/*
16009 * Set string v: variable to a copy of "val".
16010 */
16011 void
16012set_vim_var_string(idx, val, len)
16013 int idx;
16014 char_u *val;
16015 int len; /* length of "val" to use or -1 (whole string) */
16016{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016017 /* Need to do this (at least) once, since we can't initialize a union.
16018 * Will always be invoked when "v:progname" is set. */
16019 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16020
Bram Moolenaare9a41262005-01-15 22:18:47 +000016021 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016023 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016025 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016026 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016027 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028}
16029
16030/*
16031 * Set v:register if needed.
16032 */
16033 void
16034set_reg_var(c)
16035 int c;
16036{
16037 char_u regname;
16038
16039 if (c == 0 || c == ' ')
16040 regname = '"';
16041 else
16042 regname = c;
16043 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016044 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 set_vim_var_string(VV_REG, &regname, 1);
16046}
16047
16048/*
16049 * Get or set v:exception. If "oldval" == NULL, return the current value.
16050 * Otherwise, restore the value to "oldval" and return NULL.
16051 * Must always be called in pairs to save and restore v:exception! Does not
16052 * take care of memory allocations.
16053 */
16054 char_u *
16055v_exception(oldval)
16056 char_u *oldval;
16057{
16058 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016059 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060
Bram Moolenaare9a41262005-01-15 22:18:47 +000016061 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 return NULL;
16063}
16064
16065/*
16066 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16067 * Otherwise, restore the value to "oldval" and return NULL.
16068 * Must always be called in pairs to save and restore v:throwpoint! Does not
16069 * take care of memory allocations.
16070 */
16071 char_u *
16072v_throwpoint(oldval)
16073 char_u *oldval;
16074{
16075 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016076 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077
Bram Moolenaare9a41262005-01-15 22:18:47 +000016078 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079 return NULL;
16080}
16081
16082#if defined(FEAT_AUTOCMD) || defined(PROTO)
16083/*
16084 * Set v:cmdarg.
16085 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16086 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16087 * Must always be called in pairs!
16088 */
16089 char_u *
16090set_cmdarg(eap, oldarg)
16091 exarg_T *eap;
16092 char_u *oldarg;
16093{
16094 char_u *oldval;
16095 char_u *newval;
16096 unsigned len;
16097
Bram Moolenaare9a41262005-01-15 22:18:47 +000016098 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016099 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016101 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016102 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016103 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 }
16105
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016106 if (eap->force_bin == FORCE_BIN)
16107 len = 6;
16108 else if (eap->force_bin == FORCE_NOBIN)
16109 len = 8;
16110 else
16111 len = 0;
16112 if (eap->force_ff != 0)
16113 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16114# ifdef FEAT_MBYTE
16115 if (eap->force_enc != 0)
16116 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016117 if (eap->bad_char != 0)
16118 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016119# endif
16120
16121 newval = alloc(len + 1);
16122 if (newval == NULL)
16123 return NULL;
16124
16125 if (eap->force_bin == FORCE_BIN)
16126 sprintf((char *)newval, " ++bin");
16127 else if (eap->force_bin == FORCE_NOBIN)
16128 sprintf((char *)newval, " ++nobin");
16129 else
16130 *newval = NUL;
16131 if (eap->force_ff != 0)
16132 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16133 eap->cmd + eap->force_ff);
16134# ifdef FEAT_MBYTE
16135 if (eap->force_enc != 0)
16136 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16137 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016138 if (eap->bad_char != 0)
16139 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16140 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016141# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016142 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016143 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144}
16145#endif
16146
16147/*
16148 * Get the value of internal variable "name".
16149 * Return OK or FAIL.
16150 */
16151 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016152get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153 char_u *name;
16154 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016155 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016156 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157{
16158 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016159 typval_T *tv = NULL;
16160 typval_T atv;
16161 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163
16164 /* truncate the name, so that we can use strcmp() */
16165 cc = name[len];
16166 name[len] = NUL;
16167
16168 /*
16169 * Check for "b:changedtick".
16170 */
16171 if (STRCMP(name, "b:changedtick") == 0)
16172 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016173 atv.v_type = VAR_NUMBER;
16174 atv.vval.v_number = curbuf->b_changedtick;
16175 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 }
16177
16178 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179 * Check for user-defined variables.
16180 */
16181 else
16182 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016183 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016185 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 }
16187
Bram Moolenaare9a41262005-01-15 22:18:47 +000016188 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016190 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016191 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192 ret = FAIL;
16193 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016194 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016195 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196
16197 name[len] = cc;
16198
16199 return ret;
16200}
16201
16202/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016203 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16204 * Also handle function call with Funcref variable: func(expr)
16205 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16206 */
16207 static int
16208handle_subscript(arg, rettv, evaluate, verbose)
16209 char_u **arg;
16210 typval_T *rettv;
16211 int evaluate; /* do more than finding the end */
16212 int verbose; /* give error messages */
16213{
16214 int ret = OK;
16215 dict_T *selfdict = NULL;
16216 char_u *s;
16217 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016218 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016219
16220 while (ret == OK
16221 && (**arg == '['
16222 || (**arg == '.' && rettv->v_type == VAR_DICT)
16223 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16224 && !vim_iswhite(*(*arg - 1)))
16225 {
16226 if (**arg == '(')
16227 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016228 /* need to copy the funcref so that we can clear rettv */
16229 functv = *rettv;
16230 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016231
16232 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016233 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016234 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016235 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16236 &len, evaluate, selfdict);
16237
16238 /* Clear the funcref afterwards, so that deleting it while
16239 * evaluating the arguments is possible (see test55). */
16240 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016241
16242 /* Stop the expression evaluation when immediately aborting on
16243 * error, or when an interrupt occurred or an exception was thrown
16244 * but not caught. */
16245 if (aborting())
16246 {
16247 if (ret == OK)
16248 clear_tv(rettv);
16249 ret = FAIL;
16250 }
16251 dict_unref(selfdict);
16252 selfdict = NULL;
16253 }
16254 else /* **arg == '[' || **arg == '.' */
16255 {
16256 dict_unref(selfdict);
16257 if (rettv->v_type == VAR_DICT)
16258 {
16259 selfdict = rettv->vval.v_dict;
16260 if (selfdict != NULL)
16261 ++selfdict->dv_refcount;
16262 }
16263 else
16264 selfdict = NULL;
16265 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16266 {
16267 clear_tv(rettv);
16268 ret = FAIL;
16269 }
16270 }
16271 }
16272 dict_unref(selfdict);
16273 return ret;
16274}
16275
16276/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016277 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16278 * value).
16279 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016280 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016281alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016282{
Bram Moolenaar33570922005-01-25 22:26:29 +000016283 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016284}
16285
16286/*
16287 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288 * The string "s" must have been allocated, it is consumed.
16289 * Return NULL for out of memory, the variable otherwise.
16290 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016291 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016292alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293 char_u *s;
16294{
Bram Moolenaar33570922005-01-25 22:26:29 +000016295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016297 rettv = alloc_tv();
16298 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016300 rettv->v_type = VAR_STRING;
16301 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302 }
16303 else
16304 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016305 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306}
16307
16308/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016309 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016311 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016312free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016313 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314{
16315 if (varp != NULL)
16316 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016317 switch (varp->v_type)
16318 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016319 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016320 func_unref(varp->vval.v_string);
16321 /*FALLTHROUGH*/
16322 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016323 vim_free(varp->vval.v_string);
16324 break;
16325 case VAR_LIST:
16326 list_unref(varp->vval.v_list);
16327 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016328 case VAR_DICT:
16329 dict_unref(varp->vval.v_dict);
16330 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016331 case VAR_NUMBER:
16332 case VAR_UNKNOWN:
16333 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016334 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016335 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016336 break;
16337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338 vim_free(varp);
16339 }
16340}
16341
16342/*
16343 * Free the memory for a variable value and set the value to NULL or 0.
16344 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016345 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016346clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016347 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348{
16349 if (varp != NULL)
16350 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016351 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016353 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016354 func_unref(varp->vval.v_string);
16355 /*FALLTHROUGH*/
16356 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016357 vim_free(varp->vval.v_string);
16358 varp->vval.v_string = NULL;
16359 break;
16360 case VAR_LIST:
16361 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016362 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016363 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016364 case VAR_DICT:
16365 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016366 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016367 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016368 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016369 varp->vval.v_number = 0;
16370 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016371 case VAR_UNKNOWN:
16372 break;
16373 default:
16374 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016376 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377 }
16378}
16379
16380/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016381 * Set the value of a variable to NULL without freeing items.
16382 */
16383 static void
16384init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016385 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016386{
16387 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016388 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016389}
16390
16391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392 * Get the number value of a variable.
16393 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016394 * For incompatible types, return 0.
16395 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16396 * caller of incompatible types: it sets *denote to TRUE if "denote"
16397 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398 */
16399 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016400get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016401 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016403 int error = FALSE;
16404
16405 return get_tv_number_chk(varp, &error); /* return 0L on error */
16406}
16407
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016408 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016409get_tv_number_chk(varp, denote)
16410 typval_T *varp;
16411 int *denote;
16412{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016413 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016415 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016417 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016418 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016419 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016420 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016421 break;
16422 case VAR_STRING:
16423 if (varp->vval.v_string != NULL)
16424 vim_str2nr(varp->vval.v_string, NULL, NULL,
16425 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016426 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016427 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016428 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016429 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016430 case VAR_DICT:
16431 EMSG(_("E728: Using a Dictionary as a number"));
16432 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016433 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016434 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016435 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016437 if (denote == NULL) /* useful for values that must be unsigned */
16438 n = -1;
16439 else
16440 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016441 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442}
16443
16444/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016445 * Get the lnum from the first argument.
16446 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016447 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448 */
16449 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016450get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016451 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452{
Bram Moolenaar33570922005-01-25 22:26:29 +000016453 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 linenr_T lnum;
16455
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016456 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 if (lnum == 0) /* no valid number, try using line() */
16458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016459 rettv.v_type = VAR_NUMBER;
16460 f_line(argvars, &rettv);
16461 lnum = rettv.vval.v_number;
16462 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 }
16464 return lnum;
16465}
16466
16467/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016468 * Get the lnum from the first argument.
16469 * Also accepts "$", then "buf" is used.
16470 * Returns 0 on error.
16471 */
16472 static linenr_T
16473get_tv_lnum_buf(argvars, buf)
16474 typval_T *argvars;
16475 buf_T *buf;
16476{
16477 if (argvars[0].v_type == VAR_STRING
16478 && argvars[0].vval.v_string != NULL
16479 && argvars[0].vval.v_string[0] == '$'
16480 && buf != NULL)
16481 return buf->b_ml.ml_line_count;
16482 return get_tv_number_chk(&argvars[0], NULL);
16483}
16484
16485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486 * Get the string value of a variable.
16487 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016488 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16489 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490 * If the String variable has never been set, return an empty string.
16491 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016492 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16493 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 */
16495 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016496get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016497 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498{
16499 static char_u mybuf[NUMBUFLEN];
16500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016501 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502}
16503
16504 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016505get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016506 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016507 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016508{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016509 char_u *res = get_tv_string_buf_chk(varp, buf);
16510
16511 return res != NULL ? res : (char_u *)"";
16512}
16513
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016514 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016515get_tv_string_chk(varp)
16516 typval_T *varp;
16517{
16518 static char_u mybuf[NUMBUFLEN];
16519
16520 return get_tv_string_buf_chk(varp, mybuf);
16521}
16522
16523 static char_u *
16524get_tv_string_buf_chk(varp, buf)
16525 typval_T *varp;
16526 char_u *buf;
16527{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016528 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016530 case VAR_NUMBER:
16531 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16532 return buf;
16533 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016534 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016535 break;
16536 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016537 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016538 break;
16539 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016540 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016541 break;
16542 case VAR_STRING:
16543 if (varp->vval.v_string != NULL)
16544 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016545 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016546 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016547 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016548 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016550 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551}
16552
16553/*
16554 * Find variable "name" in the list of variables.
16555 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016556 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016557 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016558 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016560 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016561find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016563 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016566 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567
Bram Moolenaara7043832005-01-21 11:56:39 +000016568 ht = find_var_ht(name, &varname);
16569 if (htp != NULL)
16570 *htp = ht;
16571 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016573 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574}
16575
16576/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016577 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016578 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016579 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016580 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016581find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016582 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016583 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016584 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016585{
Bram Moolenaar33570922005-01-25 22:26:29 +000016586 hashitem_T *hi;
16587
16588 if (*varname == NUL)
16589 {
16590 /* Must be something like "s:", otherwise "ht" would be NULL. */
16591 switch (varname[-2])
16592 {
16593 case 's': return &SCRIPT_SV(current_SID).sv_var;
16594 case 'g': return &globvars_var;
16595 case 'v': return &vimvars_var;
16596 case 'b': return &curbuf->b_bufvar;
16597 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016598 case 'l': return current_funccal == NULL
16599 ? NULL : &current_funccal->l_vars_var;
16600 case 'a': return current_funccal == NULL
16601 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016602 }
16603 return NULL;
16604 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016605
16606 hi = hash_find(ht, varname);
16607 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016608 {
16609 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016610 * worked find the variable again. Don't auto-load a script if it was
16611 * loaded already, otherwise it would be loaded every time when
16612 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016613 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016614 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016615 hi = hash_find(ht, varname);
16616 if (HASHITEM_EMPTY(hi))
16617 return NULL;
16618 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016619 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016620}
16621
16622/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016623 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016624 * Set "varname" to the start of name without ':'.
16625 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016626 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016627find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 char_u *name;
16629 char_u **varname;
16630{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016631 hashitem_T *hi;
16632
Bram Moolenaar071d4272004-06-13 20:20:40 +000016633 if (name[1] != ':')
16634 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016635 /* The name must not start with a colon or #. */
16636 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637 return NULL;
16638 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016639
16640 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016641 hi = hash_find(&compat_hashtab, name);
16642 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016643 return &compat_hashtab;
16644
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016646 return &globvarht; /* global variable */
16647 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 }
16649 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016650 if (*name == 'g') /* global variable */
16651 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016652 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16653 */
16654 if (vim_strchr(name + 2, ':') != NULL
16655 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016656 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016658 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016660 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016661 if (*name == 'v') /* v: variable */
16662 return &vimvarht;
16663 if (*name == 'a' && current_funccal != NULL) /* function argument */
16664 return &current_funccal->l_avars.dv_hashtab;
16665 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16666 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 if (*name == 's' /* script variable */
16668 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16669 return &SCRIPT_VARS(current_SID);
16670 return NULL;
16671}
16672
16673/*
16674 * Get the string value of a (global/local) variable.
16675 * Returns NULL when it doesn't exist.
16676 */
16677 char_u *
16678get_var_value(name)
16679 char_u *name;
16680{
Bram Moolenaar33570922005-01-25 22:26:29 +000016681 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682
Bram Moolenaara7043832005-01-21 11:56:39 +000016683 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 if (v == NULL)
16685 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016686 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687}
16688
16689/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016690 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691 * sourcing this script and when executing functions defined in the script.
16692 */
16693 void
16694new_script_vars(id)
16695 scid_T id;
16696{
Bram Moolenaara7043832005-01-21 11:56:39 +000016697 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016698 hashtab_T *ht;
16699 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016700
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16702 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016703 /* Re-allocating ga_data means that an ht_array pointing to
16704 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016705 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016706 for (i = 1; i <= ga_scripts.ga_len; ++i)
16707 {
16708 ht = &SCRIPT_VARS(i);
16709 if (ht->ht_mask == HT_INIT_SIZE - 1)
16710 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016711 sv = &SCRIPT_SV(i);
16712 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016713 }
16714
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715 while (ga_scripts.ga_len < id)
16716 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016717 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16718 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 }
16721 }
16722}
16723
16724/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016725 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16726 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727 */
16728 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016729init_var_dict(dict, dict_var)
16730 dict_T *dict;
16731 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732{
Bram Moolenaar33570922005-01-25 22:26:29 +000016733 hash_init(&dict->dv_hashtab);
16734 dict->dv_refcount = 99999;
16735 dict_var->di_tv.vval.v_dict = dict;
16736 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016737 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016738 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16739 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740}
16741
16742/*
16743 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016744 * Frees all allocated variables and the value they contain.
16745 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746 */
16747 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016748vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016749 hashtab_T *ht;
16750{
16751 vars_clear_ext(ht, TRUE);
16752}
16753
16754/*
16755 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16756 */
16757 static void
16758vars_clear_ext(ht, free_val)
16759 hashtab_T *ht;
16760 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761{
Bram Moolenaara7043832005-01-21 11:56:39 +000016762 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016763 hashitem_T *hi;
16764 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765
Bram Moolenaar33570922005-01-25 22:26:29 +000016766 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016767 todo = ht->ht_used;
16768 for (hi = ht->ht_array; todo > 0; ++hi)
16769 {
16770 if (!HASHITEM_EMPTY(hi))
16771 {
16772 --todo;
16773
Bram Moolenaar33570922005-01-25 22:26:29 +000016774 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016775 * ht_array might change then. hash_clear() takes care of it
16776 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016777 v = HI2DI(hi);
16778 if (free_val)
16779 clear_tv(&v->di_tv);
16780 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16781 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016782 }
16783 }
16784 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016785 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786}
16787
Bram Moolenaara7043832005-01-21 11:56:39 +000016788/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016789 * Delete a variable from hashtab "ht" at item "hi".
16790 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016793delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016794 hashtab_T *ht;
16795 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796{
Bram Moolenaar33570922005-01-25 22:26:29 +000016797 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016798
16799 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016800 clear_tv(&di->di_tv);
16801 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802}
16803
16804/*
16805 * List the value of one internal variable.
16806 */
16807 static void
16808list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016809 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 char_u *prefix;
16811{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016812 char_u *tofree;
16813 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016814 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016815
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016816 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000016817 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016818 s == NULL ? (char_u *)"" : s);
16819 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820}
16821
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822 static void
16823list_one_var_a(prefix, name, type, string)
16824 char_u *prefix;
16825 char_u *name;
16826 int type;
16827 char_u *string;
16828{
16829 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16830 if (name != NULL) /* "a:" vars don't have a name stored */
16831 msg_puts(name);
16832 msg_putchar(' ');
16833 msg_advance(22);
16834 if (type == VAR_NUMBER)
16835 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016836 else if (type == VAR_FUNC)
16837 msg_putchar('*');
16838 else if (type == VAR_LIST)
16839 {
16840 msg_putchar('[');
16841 if (*string == '[')
16842 ++string;
16843 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016844 else if (type == VAR_DICT)
16845 {
16846 msg_putchar('{');
16847 if (*string == '{')
16848 ++string;
16849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 else
16851 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016852
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016854
16855 if (type == VAR_FUNC)
16856 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857}
16858
16859/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016860 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861 * If the variable already exists, the value is updated.
16862 * Otherwise the variable is created.
16863 */
16864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016865set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016867 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016868 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869{
Bram Moolenaar33570922005-01-25 22:26:29 +000016870 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016872 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016873 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016875 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016876 {
16877 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16878 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16879 ? name[2] : name[0]))
16880 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016881 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016882 return;
16883 }
16884 if (function_exists(name))
16885 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016886 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016887 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016888 return;
16889 }
16890 }
16891
Bram Moolenaara7043832005-01-21 11:56:39 +000016892 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016893 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016894 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016895 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016896 return;
16897 }
16898
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016899 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016900 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016902 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016903 if (var_check_ro(v->di_flags, name)
16904 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016905 return;
16906 if (v->di_tv.v_type != tv->v_type
16907 && !((v->di_tv.v_type == VAR_STRING
16908 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016909 && (tv->v_type == VAR_STRING
16910 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016911 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016912 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016913 return;
16914 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016915
16916 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016917 * Handle setting internal v: variables separately: we don't change
16918 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016919 */
16920 if (ht == &vimvarht)
16921 {
16922 if (v->di_tv.v_type == VAR_STRING)
16923 {
16924 vim_free(v->di_tv.vval.v_string);
16925 if (copy || tv->v_type != VAR_STRING)
16926 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16927 else
16928 {
16929 /* Take over the string to avoid an extra alloc/free. */
16930 v->di_tv.vval.v_string = tv->vval.v_string;
16931 tv->vval.v_string = NULL;
16932 }
16933 }
16934 else if (v->di_tv.v_type != VAR_NUMBER)
16935 EMSG2(_(e_intern2), "set_var()");
16936 else
16937 v->di_tv.vval.v_number = get_tv_number(tv);
16938 return;
16939 }
16940
16941 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942 }
16943 else /* add a new variable */
16944 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016945 /* Make sure the variable name is valid. */
16946 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000016947 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
16948 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000016949 {
16950 EMSG2(_(e_illvar), varname);
16951 return;
16952 }
16953
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016954 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16955 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016956 if (v == NULL)
16957 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016958 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016959 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016961 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962 return;
16963 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016964 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016965 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016967 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016968 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016969 else
16970 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016971 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016972 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016973 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975}
16976
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016977/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016978 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16979 * Also give an error message.
16980 */
16981 static int
16982var_check_ro(flags, name)
16983 int flags;
16984 char_u *name;
16985{
16986 if (flags & DI_FLAGS_RO)
16987 {
16988 EMSG2(_(e_readonlyvar), name);
16989 return TRUE;
16990 }
16991 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16992 {
16993 EMSG2(_(e_readonlysbx), name);
16994 return TRUE;
16995 }
16996 return FALSE;
16997}
16998
16999/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017000 * Return TRUE if typeval "tv" is set to be locked (immutable).
17001 * Also give an error message, using "name".
17002 */
17003 static int
17004tv_check_lock(lock, name)
17005 int lock;
17006 char_u *name;
17007{
17008 if (lock & VAR_LOCKED)
17009 {
17010 EMSG2(_("E741: Value is locked: %s"),
17011 name == NULL ? (char_u *)_("Unknown") : name);
17012 return TRUE;
17013 }
17014 if (lock & VAR_FIXED)
17015 {
17016 EMSG2(_("E742: Cannot change value of %s"),
17017 name == NULL ? (char_u *)_("Unknown") : name);
17018 return TRUE;
17019 }
17020 return FALSE;
17021}
17022
17023/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017024 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017025 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017026 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017029copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017030 typval_T *from;
17031 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017033 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017034 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017035 switch (from->v_type)
17036 {
17037 case VAR_NUMBER:
17038 to->vval.v_number = from->vval.v_number;
17039 break;
17040 case VAR_STRING:
17041 case VAR_FUNC:
17042 if (from->vval.v_string == NULL)
17043 to->vval.v_string = NULL;
17044 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017045 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017046 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017047 if (from->v_type == VAR_FUNC)
17048 func_ref(to->vval.v_string);
17049 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017050 break;
17051 case VAR_LIST:
17052 if (from->vval.v_list == NULL)
17053 to->vval.v_list = NULL;
17054 else
17055 {
17056 to->vval.v_list = from->vval.v_list;
17057 ++to->vval.v_list->lv_refcount;
17058 }
17059 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017060 case VAR_DICT:
17061 if (from->vval.v_dict == NULL)
17062 to->vval.v_dict = NULL;
17063 else
17064 {
17065 to->vval.v_dict = from->vval.v_dict;
17066 ++to->vval.v_dict->dv_refcount;
17067 }
17068 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017069 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017070 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017071 break;
17072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017073}
17074
17075/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017076 * Make a copy of an item.
17077 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017078 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17079 * reference to an already copied list/dict can be used.
17080 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017081 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017082 static int
17083item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017084 typval_T *from;
17085 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017086 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017087 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017088{
17089 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017090 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017091
Bram Moolenaar33570922005-01-25 22:26:29 +000017092 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017093 {
17094 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017095 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017096 }
17097 ++recurse;
17098
17099 switch (from->v_type)
17100 {
17101 case VAR_NUMBER:
17102 case VAR_STRING:
17103 case VAR_FUNC:
17104 copy_tv(from, to);
17105 break;
17106 case VAR_LIST:
17107 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017108 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017109 if (from->vval.v_list == NULL)
17110 to->vval.v_list = NULL;
17111 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17112 {
17113 /* use the copy made earlier */
17114 to->vval.v_list = from->vval.v_list->lv_copylist;
17115 ++to->vval.v_list->lv_refcount;
17116 }
17117 else
17118 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17119 if (to->vval.v_list == NULL)
17120 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017121 break;
17122 case VAR_DICT:
17123 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017124 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017125 if (from->vval.v_dict == NULL)
17126 to->vval.v_dict = NULL;
17127 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17128 {
17129 /* use the copy made earlier */
17130 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17131 ++to->vval.v_dict->dv_refcount;
17132 }
17133 else
17134 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17135 if (to->vval.v_dict == NULL)
17136 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017137 break;
17138 default:
17139 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017140 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017141 }
17142 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017143 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017144}
17145
17146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147 * ":echo expr1 ..." print each argument separated with a space, add a
17148 * newline at the end.
17149 * ":echon expr1 ..." print each argument plain.
17150 */
17151 void
17152ex_echo(eap)
17153 exarg_T *eap;
17154{
17155 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017156 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017157 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158 char_u *p;
17159 int needclr = TRUE;
17160 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017161 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162
17163 if (eap->skip)
17164 ++emsg_skip;
17165 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17166 {
17167 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017168 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169 {
17170 /*
17171 * Report the invalid expression unless the expression evaluation
17172 * has been cancelled due to an aborting error, an interrupt, or an
17173 * exception.
17174 */
17175 if (!aborting())
17176 EMSG2(_(e_invexpr2), p);
17177 break;
17178 }
17179 if (!eap->skip)
17180 {
17181 if (atstart)
17182 {
17183 atstart = FALSE;
17184 /* Call msg_start() after eval1(), evaluating the expression
17185 * may cause a message to appear. */
17186 if (eap->cmdidx == CMD_echo)
17187 msg_start();
17188 }
17189 else if (eap->cmdidx == CMD_echo)
17190 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017191 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017192 if (p != NULL)
17193 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017195 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017197 if (*p != TAB && needclr)
17198 {
17199 /* remove any text still there from the command */
17200 msg_clr_eos();
17201 needclr = FALSE;
17202 }
17203 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204 }
17205 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017206 {
17207#ifdef FEAT_MBYTE
17208 if (has_mbyte)
17209 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017210 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017211
17212 (void)msg_outtrans_len_attr(p, i, echo_attr);
17213 p += i - 1;
17214 }
17215 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017216#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017217 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017220 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017222 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 arg = skipwhite(arg);
17224 }
17225 eap->nextcmd = check_nextcmd(arg);
17226
17227 if (eap->skip)
17228 --emsg_skip;
17229 else
17230 {
17231 /* remove text that may still be there from the command */
17232 if (needclr)
17233 msg_clr_eos();
17234 if (eap->cmdidx == CMD_echo)
17235 msg_end();
17236 }
17237}
17238
17239/*
17240 * ":echohl {name}".
17241 */
17242 void
17243ex_echohl(eap)
17244 exarg_T *eap;
17245{
17246 int id;
17247
17248 id = syn_name2id(eap->arg);
17249 if (id == 0)
17250 echo_attr = 0;
17251 else
17252 echo_attr = syn_id2attr(id);
17253}
17254
17255/*
17256 * ":execute expr1 ..." execute the result of an expression.
17257 * ":echomsg expr1 ..." Print a message
17258 * ":echoerr expr1 ..." Print an error
17259 * Each gets spaces around each argument and a newline at the end for
17260 * echo commands
17261 */
17262 void
17263ex_execute(eap)
17264 exarg_T *eap;
17265{
17266 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017267 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 int ret = OK;
17269 char_u *p;
17270 garray_T ga;
17271 int len;
17272 int save_did_emsg;
17273
17274 ga_init2(&ga, 1, 80);
17275
17276 if (eap->skip)
17277 ++emsg_skip;
17278 while (*arg != NUL && *arg != '|' && *arg != '\n')
17279 {
17280 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017281 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 {
17283 /*
17284 * Report the invalid expression unless the expression evaluation
17285 * has been cancelled due to an aborting error, an interrupt, or an
17286 * exception.
17287 */
17288 if (!aborting())
17289 EMSG2(_(e_invexpr2), p);
17290 ret = FAIL;
17291 break;
17292 }
17293
17294 if (!eap->skip)
17295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017296 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297 len = (int)STRLEN(p);
17298 if (ga_grow(&ga, len + 2) == FAIL)
17299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017300 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301 ret = FAIL;
17302 break;
17303 }
17304 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 ga.ga_len += len;
17308 }
17309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017310 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311 arg = skipwhite(arg);
17312 }
17313
17314 if (ret != FAIL && ga.ga_data != NULL)
17315 {
17316 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017319 out_flush();
17320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321 else if (eap->cmdidx == CMD_echoerr)
17322 {
17323 /* We don't want to abort following commands, restore did_emsg. */
17324 save_did_emsg = did_emsg;
17325 EMSG((char_u *)ga.ga_data);
17326 if (!force_abort)
17327 did_emsg = save_did_emsg;
17328 }
17329 else if (eap->cmdidx == CMD_execute)
17330 do_cmdline((char_u *)ga.ga_data,
17331 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17332 }
17333
17334 ga_clear(&ga);
17335
17336 if (eap->skip)
17337 --emsg_skip;
17338
17339 eap->nextcmd = check_nextcmd(arg);
17340}
17341
17342/*
17343 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17344 * "arg" points to the "&" or '+' when called, to "option" when returning.
17345 * Returns NULL when no option name found. Otherwise pointer to the char
17346 * after the option name.
17347 */
17348 static char_u *
17349find_option_end(arg, opt_flags)
17350 char_u **arg;
17351 int *opt_flags;
17352{
17353 char_u *p = *arg;
17354
17355 ++p;
17356 if (*p == 'g' && p[1] == ':')
17357 {
17358 *opt_flags = OPT_GLOBAL;
17359 p += 2;
17360 }
17361 else if (*p == 'l' && p[1] == ':')
17362 {
17363 *opt_flags = OPT_LOCAL;
17364 p += 2;
17365 }
17366 else
17367 *opt_flags = 0;
17368
17369 if (!ASCII_ISALPHA(*p))
17370 return NULL;
17371 *arg = p;
17372
17373 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17374 p += 4; /* termcap option */
17375 else
17376 while (ASCII_ISALPHA(*p))
17377 ++p;
17378 return p;
17379}
17380
17381/*
17382 * ":function"
17383 */
17384 void
17385ex_function(eap)
17386 exarg_T *eap;
17387{
17388 char_u *theline;
17389 int j;
17390 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 char_u *name = NULL;
17393 char_u *p;
17394 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017395 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396 garray_T newargs;
17397 garray_T newlines;
17398 int varargs = FALSE;
17399 int mustend = FALSE;
17400 int flags = 0;
17401 ufunc_T *fp;
17402 int indent;
17403 int nesting;
17404 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017405 dictitem_T *v;
17406 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017407 static int func_nr = 0; /* number for nameless function */
17408 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017409 hashtab_T *ht;
17410 int todo;
17411 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412
17413 /*
17414 * ":function" without argument: list functions.
17415 */
17416 if (ends_excmd(*eap->arg))
17417 {
17418 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017419 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017420 todo = func_hashtab.ht_used;
17421 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017422 {
17423 if (!HASHITEM_EMPTY(hi))
17424 {
17425 --todo;
17426 fp = HI2UF(hi);
17427 if (!isdigit(*fp->uf_name))
17428 list_func_head(fp, FALSE);
17429 }
17430 }
17431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432 eap->nextcmd = check_nextcmd(eap->arg);
17433 return;
17434 }
17435
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017436 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017437 * ":function /pat": list functions matching pattern.
17438 */
17439 if (*eap->arg == '/')
17440 {
17441 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17442 if (!eap->skip)
17443 {
17444 regmatch_T regmatch;
17445
17446 c = *p;
17447 *p = NUL;
17448 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17449 *p = c;
17450 if (regmatch.regprog != NULL)
17451 {
17452 regmatch.rm_ic = p_ic;
17453
17454 todo = func_hashtab.ht_used;
17455 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17456 {
17457 if (!HASHITEM_EMPTY(hi))
17458 {
17459 --todo;
17460 fp = HI2UF(hi);
17461 if (!isdigit(*fp->uf_name)
17462 && vim_regexec(&regmatch, fp->uf_name, 0))
17463 list_func_head(fp, FALSE);
17464 }
17465 }
17466 }
17467 }
17468 if (*p == '/')
17469 ++p;
17470 eap->nextcmd = check_nextcmd(p);
17471 return;
17472 }
17473
17474 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017475 * Get the function name. There are these situations:
17476 * func normal function name
17477 * "name" == func, "fudi.fd_dict" == NULL
17478 * dict.func new dictionary entry
17479 * "name" == NULL, "fudi.fd_dict" set,
17480 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17481 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017482 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017483 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17484 * dict.func existing dict entry that's not a Funcref
17485 * "name" == NULL, "fudi.fd_dict" set,
17486 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017489 name = trans_function_name(&p, eap->skip, 0, &fudi);
17490 paren = (vim_strchr(p, '(') != NULL);
17491 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 {
17493 /*
17494 * Return on an invalid expression in braces, unless the expression
17495 * evaluation has been cancelled due to an aborting error, an
17496 * interrupt, or an exception.
17497 */
17498 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017499 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017500 if (!eap->skip && fudi.fd_newkey != NULL)
17501 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017502 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 else
17506 eap->skip = TRUE;
17507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508 /* An error in a function call during evaluation of an expression in magic
17509 * braces should not cause the function not to be defined. */
17510 saved_did_emsg = did_emsg;
17511 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512
17513 /*
17514 * ":function func" with only function name: list function.
17515 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017516 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517 {
17518 if (!ends_excmd(*skipwhite(p)))
17519 {
17520 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017521 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 }
17523 eap->nextcmd = check_nextcmd(p);
17524 if (eap->nextcmd != NULL)
17525 *p = NUL;
17526 if (!eap->skip && !got_int)
17527 {
17528 fp = find_func(name);
17529 if (fp != NULL)
17530 {
17531 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017532 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533 {
17534 msg_putchar('\n');
17535 msg_outnum((long)(j + 1));
17536 if (j < 9)
17537 msg_putchar(' ');
17538 if (j < 99)
17539 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017540 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541 out_flush(); /* show a line at a time */
17542 ui_breakcheck();
17543 }
17544 if (!got_int)
17545 {
17546 msg_putchar('\n');
17547 msg_puts((char_u *)" endfunction");
17548 }
17549 }
17550 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017551 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017553 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554 }
17555
17556 /*
17557 * ":function name(arg1, arg2)" Define function.
17558 */
17559 p = skipwhite(p);
17560 if (*p != '(')
17561 {
17562 if (!eap->skip)
17563 {
17564 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017565 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 }
17567 /* attempt to continue by skipping some text */
17568 if (vim_strchr(p, '(') != NULL)
17569 p = vim_strchr(p, '(');
17570 }
17571 p = skipwhite(p + 1);
17572
17573 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17574 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17575
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017576 if (!eap->skip)
17577 {
17578 /* Check the name of the function. */
17579 if (name != NULL)
17580 arg = name;
17581 else
17582 arg = fudi.fd_newkey;
17583 if (arg != NULL)
17584 {
17585 if (*arg == K_SPECIAL)
17586 j = 3;
17587 else
17588 j = 0;
17589 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17590 : eval_isnamec(arg[j])))
17591 ++j;
17592 if (arg[j] != NUL)
17593 emsg_funcname(_(e_invarg2), arg);
17594 }
17595 }
17596
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597 /*
17598 * Isolate the arguments: "arg1, arg2, ...)"
17599 */
17600 while (*p != ')')
17601 {
17602 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17603 {
17604 varargs = TRUE;
17605 p += 3;
17606 mustend = TRUE;
17607 }
17608 else
17609 {
17610 arg = p;
17611 while (ASCII_ISALNUM(*p) || *p == '_')
17612 ++p;
17613 if (arg == p || isdigit(*arg)
17614 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17615 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17616 {
17617 if (!eap->skip)
17618 EMSG2(_("E125: Illegal argument: %s"), arg);
17619 break;
17620 }
17621 if (ga_grow(&newargs, 1) == FAIL)
17622 goto erret;
17623 c = *p;
17624 *p = NUL;
17625 arg = vim_strsave(arg);
17626 if (arg == NULL)
17627 goto erret;
17628 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17629 *p = c;
17630 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 if (*p == ',')
17632 ++p;
17633 else
17634 mustend = TRUE;
17635 }
17636 p = skipwhite(p);
17637 if (mustend && *p != ')')
17638 {
17639 if (!eap->skip)
17640 EMSG2(_(e_invarg2), eap->arg);
17641 break;
17642 }
17643 }
17644 ++p; /* skip the ')' */
17645
Bram Moolenaare9a41262005-01-15 22:18:47 +000017646 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 for (;;)
17648 {
17649 p = skipwhite(p);
17650 if (STRNCMP(p, "range", 5) == 0)
17651 {
17652 flags |= FC_RANGE;
17653 p += 5;
17654 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017655 else if (STRNCMP(p, "dict", 4) == 0)
17656 {
17657 flags |= FC_DICT;
17658 p += 4;
17659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 else if (STRNCMP(p, "abort", 5) == 0)
17661 {
17662 flags |= FC_ABORT;
17663 p += 5;
17664 }
17665 else
17666 break;
17667 }
17668
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017669 /* When there is a line break use what follows for the function body.
17670 * Makes 'exe "func Test()\n...\nendfunc"' work. */
17671 if (*p == '\n')
17672 line_arg = p + 1;
17673 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674 EMSG(_(e_trailing));
17675
17676 /*
17677 * Read the body of the function, until ":endfunction" is found.
17678 */
17679 if (KeyTyped)
17680 {
17681 /* Check if the function already exists, don't let the user type the
17682 * whole function before telling him it doesn't work! For a script we
17683 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017684 if (!eap->skip && !eap->forceit)
17685 {
17686 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17687 EMSG(_(e_funcdict));
17688 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017689 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017692 if (!eap->skip && did_emsg)
17693 goto erret;
17694
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 msg_putchar('\n'); /* don't overwrite the function name */
17696 cmdline_row = msg_row;
17697 }
17698
17699 indent = 2;
17700 nesting = 0;
17701 for (;;)
17702 {
17703 msg_scroll = TRUE;
17704 need_wait_return = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017705 if (line_arg != NULL)
17706 {
17707 /* Use eap->arg, split up in parts by line breaks. */
17708 theline = line_arg;
17709 p = vim_strchr(theline, '\n');
17710 if (p == NULL)
17711 line_arg += STRLEN(line_arg);
17712 else
17713 {
17714 *p = NUL;
17715 line_arg = p + 1;
17716 }
17717 }
17718 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719 theline = getcmdline(':', 0L, indent);
17720 else
17721 theline = eap->getline(':', eap->cookie, indent);
17722 if (KeyTyped)
17723 lines_left = Rows - 1;
17724 if (theline == NULL)
17725 {
17726 EMSG(_("E126: Missing :endfunction"));
17727 goto erret;
17728 }
17729
17730 if (skip_until != NULL)
17731 {
17732 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17733 * don't check for ":endfunc". */
17734 if (STRCMP(theline, skip_until) == 0)
17735 {
17736 vim_free(skip_until);
17737 skip_until = NULL;
17738 }
17739 }
17740 else
17741 {
17742 /* skip ':' and blanks*/
17743 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17744 ;
17745
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017746 /* Check for "endfunction". */
17747 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017749 if (line_arg == NULL)
17750 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 break;
17752 }
17753
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017754 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755 * at "end". */
17756 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17757 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017758 else if (STRNCMP(p, "if", 2) == 0
17759 || STRNCMP(p, "wh", 2) == 0
17760 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761 || STRNCMP(p, "try", 3) == 0)
17762 indent += 2;
17763
17764 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017765 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017767 if (*p == '!')
17768 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 p += eval_fname_script(p);
17770 if (ASCII_ISALPHA(*p))
17771 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017772 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 if (*skipwhite(p) == '(')
17774 {
17775 ++nesting;
17776 indent += 2;
17777 }
17778 }
17779 }
17780
17781 /* Check for ":append" or ":insert". */
17782 p = skip_range(p, NULL);
17783 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17784 || (p[0] == 'i'
17785 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17786 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17787 skip_until = vim_strsave((char_u *)".");
17788
17789 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17790 arg = skipwhite(skiptowhite(p));
17791 if (arg[0] == '<' && arg[1] =='<'
17792 && ((p[0] == 'p' && p[1] == 'y'
17793 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17794 || (p[0] == 'p' && p[1] == 'e'
17795 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17796 || (p[0] == 't' && p[1] == 'c'
17797 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17798 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17799 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017800 || (p[0] == 'm' && p[1] == 'z'
17801 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802 ))
17803 {
17804 /* ":python <<" continues until a dot, like ":append" */
17805 p = skipwhite(arg + 2);
17806 if (*p == NUL)
17807 skip_until = vim_strsave((char_u *)".");
17808 else
17809 skip_until = vim_strsave(p);
17810 }
17811 }
17812
17813 /* Add the line to the function. */
17814 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017815 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017816 if (line_arg == NULL)
17817 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017819 }
17820
17821 /* Copy the line to newly allocated memory. get_one_sourceline()
17822 * allocates 250 bytes per line, this saves 80% on average. The cost
17823 * is an extra alloc/free. */
17824 p = vim_strsave(theline);
17825 if (p != NULL)
17826 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017827 if (line_arg == NULL)
17828 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017829 theline = p;
17830 }
17831
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17833 newlines.ga_len++;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017834
17835 /* Check for end of eap->arg. */
17836 if (line_arg != NULL && *line_arg == NUL)
17837 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838 }
17839
17840 /* Don't define the function when skipping commands or when an error was
17841 * detected. */
17842 if (eap->skip || did_emsg)
17843 goto erret;
17844
17845 /*
17846 * If there are no errors, add the function
17847 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017848 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017849 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017850 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017851 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017852 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017853 emsg_funcname("E707: Function name conflicts with variable: %s",
17854 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017855 goto erret;
17856 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017857
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017858 fp = find_func(name);
17859 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017861 if (!eap->forceit)
17862 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017863 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017864 goto erret;
17865 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017866 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017867 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017868 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017869 name);
17870 goto erret;
17871 }
17872 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017873 ga_clear_strings(&(fp->uf_args));
17874 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017875 vim_free(name);
17876 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878 }
17879 else
17880 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017881 char numbuf[20];
17882
17883 fp = NULL;
17884 if (fudi.fd_newkey == NULL && !eap->forceit)
17885 {
17886 EMSG(_(e_funcdict));
17887 goto erret;
17888 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017889 if (fudi.fd_di == NULL)
17890 {
17891 /* Can't add a function to a locked dictionary */
17892 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17893 goto erret;
17894 }
17895 /* Can't change an existing function if it is locked */
17896 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17897 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017898
17899 /* Give the function a sequential number. Can only be used with a
17900 * Funcref! */
17901 vim_free(name);
17902 sprintf(numbuf, "%d", ++func_nr);
17903 name = vim_strsave((char_u *)numbuf);
17904 if (name == NULL)
17905 goto erret;
17906 }
17907
17908 if (fp == NULL)
17909 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017910 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017911 {
17912 int slen, plen;
17913 char_u *scriptname;
17914
17915 /* Check that the autoload name matches the script name. */
17916 j = FAIL;
17917 if (sourcing_name != NULL)
17918 {
17919 scriptname = autoload_name(name);
17920 if (scriptname != NULL)
17921 {
17922 p = vim_strchr(scriptname, '/');
17923 plen = STRLEN(p);
17924 slen = STRLEN(sourcing_name);
17925 if (slen > plen && fnamecmp(p,
17926 sourcing_name + slen - plen) == 0)
17927 j = OK;
17928 vim_free(scriptname);
17929 }
17930 }
17931 if (j == FAIL)
17932 {
17933 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17934 goto erret;
17935 }
17936 }
17937
17938 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 if (fp == NULL)
17940 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017941
17942 if (fudi.fd_dict != NULL)
17943 {
17944 if (fudi.fd_di == NULL)
17945 {
17946 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017947 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017948 if (fudi.fd_di == NULL)
17949 {
17950 vim_free(fp);
17951 goto erret;
17952 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017953 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17954 {
17955 vim_free(fudi.fd_di);
17956 goto erret;
17957 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017958 }
17959 else
17960 /* overwrite existing dict entry */
17961 clear_tv(&fudi.fd_di->di_tv);
17962 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017963 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017964 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017965 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017966 }
17967
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017969 STRCPY(fp->uf_name, name);
17970 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017972 fp->uf_args = newargs;
17973 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017974#ifdef FEAT_PROFILE
17975 fp->uf_tml_count = NULL;
17976 fp->uf_tml_total = NULL;
17977 fp->uf_tml_self = NULL;
17978 fp->uf_profiling = FALSE;
17979 if (prof_def_func())
17980 func_do_profile(fp);
17981#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017982 fp->uf_varargs = varargs;
17983 fp->uf_flags = flags;
17984 fp->uf_calls = 0;
17985 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017986 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987
17988erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 ga_clear_strings(&newargs);
17990 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017991ret_free:
17992 vim_free(skip_until);
17993 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996}
17997
17998/*
17999 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018000 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018002 * flags:
18003 * TFN_INT: internal function name OK
18004 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005 * Advances "pp" to just after the function name (if no error).
18006 */
18007 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018008trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 char_u **pp;
18010 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018011 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018012 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018014 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015 char_u *start;
18016 char_u *end;
18017 int lead;
18018 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018020 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018021
18022 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018023 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018025
18026 /* Check for hard coded <SNR>: already translated function ID (from a user
18027 * command). */
18028 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18029 && (*pp)[2] == (int)KE_SNR)
18030 {
18031 *pp += 3;
18032 len = get_id_len(pp) + 3;
18033 return vim_strnsave(start, len);
18034 }
18035
18036 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18037 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018039 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018041
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018042 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18043 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018044 if (end == start)
18045 {
18046 if (!skip)
18047 EMSG(_("E129: Function name required"));
18048 goto theend;
18049 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018050 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018051 {
18052 /*
18053 * Report an invalid expression in braces, unless the expression
18054 * evaluation has been cancelled due to an aborting error, an
18055 * interrupt, or an exception.
18056 */
18057 if (!aborting())
18058 {
18059 if (end != NULL)
18060 EMSG2(_(e_invarg2), start);
18061 }
18062 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018063 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018064 goto theend;
18065 }
18066
18067 if (lv.ll_tv != NULL)
18068 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018069 if (fdp != NULL)
18070 {
18071 fdp->fd_dict = lv.ll_dict;
18072 fdp->fd_newkey = lv.ll_newkey;
18073 lv.ll_newkey = NULL;
18074 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018075 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018076 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18077 {
18078 name = vim_strsave(lv.ll_tv->vval.v_string);
18079 *pp = end;
18080 }
18081 else
18082 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018083 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18084 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018085 EMSG(_(e_funcref));
18086 else
18087 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018088 name = NULL;
18089 }
18090 goto theend;
18091 }
18092
18093 if (lv.ll_name == NULL)
18094 {
18095 /* Error found, but continue after the function name. */
18096 *pp = end;
18097 goto theend;
18098 }
18099
18100 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018101 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018102 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018103 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18104 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18105 {
18106 /* When there was "s:" already or the name expanded to get a
18107 * leading "s:" then remove it. */
18108 lv.ll_name += 2;
18109 len -= 2;
18110 lead = 2;
18111 }
18112 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018113 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018114 {
18115 if (lead == 2) /* skip over "s:" */
18116 lv.ll_name += 2;
18117 len = (int)(end - lv.ll_name);
18118 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018119
18120 /*
18121 * Copy the function name to allocated memory.
18122 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18123 * Accept <SNR>123_name() outside a script.
18124 */
18125 if (skip)
18126 lead = 0; /* do nothing */
18127 else if (lead > 0)
18128 {
18129 lead = 3;
18130 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18131 {
18132 if (current_SID <= 0)
18133 {
18134 EMSG(_(e_usingsid));
18135 goto theend;
18136 }
18137 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18138 lead += (int)STRLEN(sid_buf);
18139 }
18140 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018141 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018142 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018143 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018144 goto theend;
18145 }
18146 name = alloc((unsigned)(len + lead + 1));
18147 if (name != NULL)
18148 {
18149 if (lead > 0)
18150 {
18151 name[0] = K_SPECIAL;
18152 name[1] = KS_EXTRA;
18153 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018154 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018155 STRCPY(name + 3, sid_buf);
18156 }
18157 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18158 name[len + lead] = NUL;
18159 }
18160 *pp = end;
18161
18162theend:
18163 clear_lval(&lv);
18164 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165}
18166
18167/*
18168 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18169 * Return 2 if "p" starts with "s:".
18170 * Return 0 otherwise.
18171 */
18172 static int
18173eval_fname_script(p)
18174 char_u *p;
18175{
18176 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18177 || STRNICMP(p + 1, "SNR>", 4) == 0))
18178 return 5;
18179 if (p[0] == 's' && p[1] == ':')
18180 return 2;
18181 return 0;
18182}
18183
18184/*
18185 * Return TRUE if "p" starts with "<SID>" or "s:".
18186 * Only works if eval_fname_script() returned non-zero for "p"!
18187 */
18188 static int
18189eval_fname_sid(p)
18190 char_u *p;
18191{
18192 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18193}
18194
18195/*
18196 * List the head of the function: "name(arg1, arg2)".
18197 */
18198 static void
18199list_func_head(fp, indent)
18200 ufunc_T *fp;
18201 int indent;
18202{
18203 int j;
18204
18205 msg_start();
18206 if (indent)
18207 MSG_PUTS(" ");
18208 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018209 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210 {
18211 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018212 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213 }
18214 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018215 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018217 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218 {
18219 if (j)
18220 MSG_PUTS(", ");
18221 msg_puts(FUNCARG(fp, j));
18222 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018223 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224 {
18225 if (j)
18226 MSG_PUTS(", ");
18227 MSG_PUTS("...");
18228 }
18229 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018230 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018231 if (p_verbose > 0)
18232 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233}
18234
18235/*
18236 * Find a function by name, return pointer to it in ufuncs.
18237 * Return NULL for unknown function.
18238 */
18239 static ufunc_T *
18240find_func(name)
18241 char_u *name;
18242{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018243 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018245 hi = hash_find(&func_hashtab, name);
18246 if (!HASHITEM_EMPTY(hi))
18247 return HI2UF(hi);
18248 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249}
18250
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018251#if defined(EXITFREE) || defined(PROTO)
18252 void
18253free_all_functions()
18254{
18255 hashitem_T *hi;
18256
18257 /* Need to start all over every time, because func_free() may change the
18258 * hash table. */
18259 while (func_hashtab.ht_used > 0)
18260 for (hi = func_hashtab.ht_array; ; ++hi)
18261 if (!HASHITEM_EMPTY(hi))
18262 {
18263 func_free(HI2UF(hi));
18264 break;
18265 }
18266}
18267#endif
18268
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018269/*
18270 * Return TRUE if a function "name" exists.
18271 */
18272 static int
18273function_exists(name)
18274 char_u *name;
18275{
18276 char_u *p = name;
18277 int n = FALSE;
18278
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018279 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018280 if (p != NULL)
18281 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018282 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018283 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018284 else
18285 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018286 vim_free(p);
18287 }
18288 return n;
18289}
18290
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018291/*
18292 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018293 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018294 */
18295 static int
18296builtin_function(name)
18297 char_u *name;
18298{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018299 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18300 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018301}
18302
Bram Moolenaar05159a02005-02-26 23:04:13 +000018303#if defined(FEAT_PROFILE) || defined(PROTO)
18304/*
18305 * Start profiling function "fp".
18306 */
18307 static void
18308func_do_profile(fp)
18309 ufunc_T *fp;
18310{
18311 fp->uf_tm_count = 0;
18312 profile_zero(&fp->uf_tm_self);
18313 profile_zero(&fp->uf_tm_total);
18314 if (fp->uf_tml_count == NULL)
18315 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18316 (sizeof(int) * fp->uf_lines.ga_len));
18317 if (fp->uf_tml_total == NULL)
18318 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18319 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18320 if (fp->uf_tml_self == NULL)
18321 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18322 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18323 fp->uf_tml_idx = -1;
18324 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18325 || fp->uf_tml_self == NULL)
18326 return; /* out of memory */
18327
18328 fp->uf_profiling = TRUE;
18329}
18330
18331/*
18332 * Dump the profiling results for all functions in file "fd".
18333 */
18334 void
18335func_dump_profile(fd)
18336 FILE *fd;
18337{
18338 hashitem_T *hi;
18339 int todo;
18340 ufunc_T *fp;
18341 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018342 ufunc_T **sorttab;
18343 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018344
18345 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018346 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18347
Bram Moolenaar05159a02005-02-26 23:04:13 +000018348 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18349 {
18350 if (!HASHITEM_EMPTY(hi))
18351 {
18352 --todo;
18353 fp = HI2UF(hi);
18354 if (fp->uf_profiling)
18355 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018356 if (sorttab != NULL)
18357 sorttab[st_len++] = fp;
18358
Bram Moolenaar05159a02005-02-26 23:04:13 +000018359 if (fp->uf_name[0] == K_SPECIAL)
18360 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18361 else
18362 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18363 if (fp->uf_tm_count == 1)
18364 fprintf(fd, "Called 1 time\n");
18365 else
18366 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18367 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18368 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18369 fprintf(fd, "\n");
18370 fprintf(fd, "count total (s) self (s)\n");
18371
18372 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18373 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018374 prof_func_line(fd, fp->uf_tml_count[i],
18375 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018376 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18377 }
18378 fprintf(fd, "\n");
18379 }
18380 }
18381 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018382
18383 if (sorttab != NULL && st_len > 0)
18384 {
18385 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18386 prof_total_cmp);
18387 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18388 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18389 prof_self_cmp);
18390 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18391 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018392}
Bram Moolenaar73830342005-02-28 22:48:19 +000018393
18394 static void
18395prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18396 FILE *fd;
18397 ufunc_T **sorttab;
18398 int st_len;
18399 char *title;
18400 int prefer_self; /* when equal print only self time */
18401{
18402 int i;
18403 ufunc_T *fp;
18404
18405 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18406 fprintf(fd, "count total (s) self (s) function\n");
18407 for (i = 0; i < 20 && i < st_len; ++i)
18408 {
18409 fp = sorttab[i];
18410 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18411 prefer_self);
18412 if (fp->uf_name[0] == K_SPECIAL)
18413 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18414 else
18415 fprintf(fd, " %s()\n", fp->uf_name);
18416 }
18417 fprintf(fd, "\n");
18418}
18419
18420/*
18421 * Print the count and times for one function or function line.
18422 */
18423 static void
18424prof_func_line(fd, count, total, self, prefer_self)
18425 FILE *fd;
18426 int count;
18427 proftime_T *total;
18428 proftime_T *self;
18429 int prefer_self; /* when equal print only self time */
18430{
18431 if (count > 0)
18432 {
18433 fprintf(fd, "%5d ", count);
18434 if (prefer_self && profile_equal(total, self))
18435 fprintf(fd, " ");
18436 else
18437 fprintf(fd, "%s ", profile_msg(total));
18438 if (!prefer_self && profile_equal(total, self))
18439 fprintf(fd, " ");
18440 else
18441 fprintf(fd, "%s ", profile_msg(self));
18442 }
18443 else
18444 fprintf(fd, " ");
18445}
18446
18447/*
18448 * Compare function for total time sorting.
18449 */
18450 static int
18451#ifdef __BORLANDC__
18452_RTLENTRYF
18453#endif
18454prof_total_cmp(s1, s2)
18455 const void *s1;
18456 const void *s2;
18457{
18458 ufunc_T *p1, *p2;
18459
18460 p1 = *(ufunc_T **)s1;
18461 p2 = *(ufunc_T **)s2;
18462 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18463}
18464
18465/*
18466 * Compare function for self time sorting.
18467 */
18468 static int
18469#ifdef __BORLANDC__
18470_RTLENTRYF
18471#endif
18472prof_self_cmp(s1, s2)
18473 const void *s1;
18474 const void *s2;
18475{
18476 ufunc_T *p1, *p2;
18477
18478 p1 = *(ufunc_T **)s1;
18479 p2 = *(ufunc_T **)s2;
18480 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18481}
18482
Bram Moolenaar05159a02005-02-26 23:04:13 +000018483#endif
18484
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018485/* The names of packages that once were loaded is remembered. */
18486static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18487
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018488/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018489 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018490 * Return TRUE if a package was loaded.
18491 */
18492 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018493script_autoload(name, reload)
18494 char_u *name;
18495 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018496{
18497 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018498 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018499 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018500 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018501
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018502 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018503 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018504 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018505 return FALSE;
18506
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018507 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018508
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018509 /* Find the name in the list of previously loaded package names. Skip
18510 * "autoload/", it's always the same. */
18511 for (i = 0; i < ga_loaded.ga_len; ++i)
18512 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18513 break;
18514 if (!reload && i < ga_loaded.ga_len)
18515 ret = FALSE; /* was loaded already */
18516 else
18517 {
18518 /* Remember the name if it wasn't loaded already. */
18519 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18520 {
18521 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18522 tofree = NULL;
18523 }
18524
18525 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018526 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018527 ret = TRUE;
18528 }
18529
18530 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018531 return ret;
18532}
18533
18534/*
18535 * Return the autoload script name for a function or variable name.
18536 * Returns NULL when out of memory.
18537 */
18538 static char_u *
18539autoload_name(name)
18540 char_u *name;
18541{
18542 char_u *p;
18543 char_u *scriptname;
18544
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018545 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018546 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18547 if (scriptname == NULL)
18548 return FALSE;
18549 STRCPY(scriptname, "autoload/");
18550 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018551 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018552 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018553 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018554 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018555 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018556}
18557
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18559
18560/*
18561 * Function given to ExpandGeneric() to obtain the list of user defined
18562 * function names.
18563 */
18564 char_u *
18565get_user_func_name(xp, idx)
18566 expand_T *xp;
18567 int idx;
18568{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018569 static long_u done;
18570 static hashitem_T *hi;
18571 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572
18573 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018575 done = 0;
18576 hi = func_hashtab.ht_array;
18577 }
18578 if (done < func_hashtab.ht_used)
18579 {
18580 if (done++ > 0)
18581 ++hi;
18582 while (HASHITEM_EMPTY(hi))
18583 ++hi;
18584 fp = HI2UF(hi);
18585
18586 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18587 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588
18589 cat_func_name(IObuff, fp);
18590 if (xp->xp_context != EXPAND_USER_FUNC)
18591 {
18592 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018593 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594 STRCAT(IObuff, ")");
18595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 return IObuff;
18597 }
18598 return NULL;
18599}
18600
18601#endif /* FEAT_CMDL_COMPL */
18602
18603/*
18604 * Copy the function name of "fp" to buffer "buf".
18605 * "buf" must be able to hold the function name plus three bytes.
18606 * Takes care of script-local function names.
18607 */
18608 static void
18609cat_func_name(buf, fp)
18610 char_u *buf;
18611 ufunc_T *fp;
18612{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018613 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 {
18615 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018616 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018617 }
18618 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018619 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620}
18621
18622/*
18623 * ":delfunction {name}"
18624 */
18625 void
18626ex_delfunction(eap)
18627 exarg_T *eap;
18628{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018629 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 char_u *p;
18631 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018632 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633
18634 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018635 name = trans_function_name(&p, eap->skip, 0, &fudi);
18636 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018638 {
18639 if (fudi.fd_dict != NULL && !eap->skip)
18640 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 if (!ends_excmd(*skipwhite(p)))
18644 {
18645 vim_free(name);
18646 EMSG(_(e_trailing));
18647 return;
18648 }
18649 eap->nextcmd = check_nextcmd(p);
18650 if (eap->nextcmd != NULL)
18651 *p = NUL;
18652
18653 if (!eap->skip)
18654 fp = find_func(name);
18655 vim_free(name);
18656
18657 if (!eap->skip)
18658 {
18659 if (fp == NULL)
18660 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018661 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 return;
18663 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018664 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 {
18666 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18667 return;
18668 }
18669
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018670 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018672 /* Delete the dict item that refers to the function, it will
18673 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018674 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018676 else
18677 func_free(fp);
18678 }
18679}
18680
18681/*
18682 * Free a function and remove it from the list of functions.
18683 */
18684 static void
18685func_free(fp)
18686 ufunc_T *fp;
18687{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018688 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018689
18690 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018691 ga_clear_strings(&(fp->uf_args));
18692 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018693#ifdef FEAT_PROFILE
18694 vim_free(fp->uf_tml_count);
18695 vim_free(fp->uf_tml_total);
18696 vim_free(fp->uf_tml_self);
18697#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018698
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018699 /* remove the function from the function hashtable */
18700 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18701 if (HASHITEM_EMPTY(hi))
18702 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018703 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018704 hash_remove(&func_hashtab, hi);
18705
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018706 vim_free(fp);
18707}
18708
18709/*
18710 * Unreference a Function: decrement the reference count and free it when it
18711 * becomes zero. Only for numbered functions.
18712 */
18713 static void
18714func_unref(name)
18715 char_u *name;
18716{
18717 ufunc_T *fp;
18718
18719 if (name != NULL && isdigit(*name))
18720 {
18721 fp = find_func(name);
18722 if (fp == NULL)
18723 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018724 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018725 {
18726 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018727 * when "uf_calls" becomes zero. */
18728 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018729 func_free(fp);
18730 }
18731 }
18732}
18733
18734/*
18735 * Count a reference to a Function.
18736 */
18737 static void
18738func_ref(name)
18739 char_u *name;
18740{
18741 ufunc_T *fp;
18742
18743 if (name != NULL && isdigit(*name))
18744 {
18745 fp = find_func(name);
18746 if (fp == NULL)
18747 EMSG2(_(e_intern2), "func_ref()");
18748 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018749 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 }
18751}
18752
18753/*
18754 * Call a user function.
18755 */
18756 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018757call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 ufunc_T *fp; /* pointer to function */
18759 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018760 typval_T *argvars; /* arguments */
18761 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 linenr_T firstline; /* first line of range */
18763 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018764 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765{
Bram Moolenaar33570922005-01-25 22:26:29 +000018766 char_u *save_sourcing_name;
18767 linenr_T save_sourcing_lnum;
18768 scid_T save_current_SID;
18769 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018770 int save_did_emsg;
18771 static int depth = 0;
18772 dictitem_T *v;
18773 int fixvar_idx = 0; /* index in fixvar[] */
18774 int i;
18775 int ai;
18776 char_u numbuf[NUMBUFLEN];
18777 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018778#ifdef FEAT_PROFILE
18779 proftime_T wait_start;
18780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781
18782 /* If depth of calling is getting too high, don't execute the function */
18783 if (depth >= p_mfd)
18784 {
18785 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018786 rettv->v_type = VAR_NUMBER;
18787 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 return;
18789 }
18790 ++depth;
18791
18792 line_breakcheck(); /* check for CTRL-C hit */
18793
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018794 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018795 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018797 fc.rettv = rettv;
18798 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799 fc.linenr = 0;
18800 fc.returned = FALSE;
18801 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018803 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804 fc.dbg_tick = debug_tick;
18805
Bram Moolenaar33570922005-01-25 22:26:29 +000018806 /*
18807 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18808 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18809 * each argument variable and saves a lot of time.
18810 */
18811 /*
18812 * Init l: variables.
18813 */
18814 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018815 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018816 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018817 /* Set l:self to "selfdict". */
18818 v = &fc.fixvar[fixvar_idx++].var;
18819 STRCPY(v->di_key, "self");
18820 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18821 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18822 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018823 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018824 v->di_tv.vval.v_dict = selfdict;
18825 ++selfdict->dv_refcount;
18826 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018827
Bram Moolenaar33570922005-01-25 22:26:29 +000018828 /*
18829 * Init a: variables.
18830 * Set a:0 to "argcount".
18831 * Set a:000 to a list with room for the "..." arguments.
18832 */
18833 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18834 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018835 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018836 v = &fc.fixvar[fixvar_idx++].var;
18837 STRCPY(v->di_key, "000");
18838 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18839 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18840 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018841 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018842 v->di_tv.vval.v_list = &fc.l_varlist;
18843 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18844 fc.l_varlist.lv_refcount = 99999;
18845
18846 /*
18847 * Set a:firstline to "firstline" and a:lastline to "lastline".
18848 * Set a:name to named arguments.
18849 * Set a:N to the "..." arguments.
18850 */
18851 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18852 (varnumber_T)firstline);
18853 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18854 (varnumber_T)lastline);
18855 for (i = 0; i < argcount; ++i)
18856 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018857 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018858 if (ai < 0)
18859 /* named argument a:name */
18860 name = FUNCARG(fp, i);
18861 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018862 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018863 /* "..." argument a:1, a:2, etc. */
18864 sprintf((char *)numbuf, "%d", ai + 1);
18865 name = numbuf;
18866 }
18867 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18868 {
18869 v = &fc.fixvar[fixvar_idx++].var;
18870 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18871 }
18872 else
18873 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018874 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18875 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018876 if (v == NULL)
18877 break;
18878 v->di_flags = DI_FLAGS_RO;
18879 }
18880 STRCPY(v->di_key, name);
18881 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18882
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018883 /* Note: the values are copied directly to avoid alloc/free.
18884 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018885 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018886 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018887
18888 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18889 {
18890 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18891 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018892 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018893 }
18894 }
18895
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896 /* Don't redraw while executing the function. */
18897 ++RedrawingDisabled;
18898 save_sourcing_name = sourcing_name;
18899 save_sourcing_lnum = sourcing_lnum;
18900 sourcing_lnum = 1;
18901 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018902 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903 if (sourcing_name != NULL)
18904 {
18905 if (save_sourcing_name != NULL
18906 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18907 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18908 else
18909 STRCPY(sourcing_name, "function ");
18910 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18911
18912 if (p_verbose >= 12)
18913 {
18914 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018915 verbose_enter_scroll();
18916
Bram Moolenaar555b2802005-05-19 21:08:39 +000018917 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 if (p_verbose >= 14)
18919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018921 char_u numbuf[NUMBUFLEN];
18922 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923
18924 msg_puts((char_u *)"(");
18925 for (i = 0; i < argcount; ++i)
18926 {
18927 if (i > 0)
18928 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018929 if (argvars[i].v_type == VAR_NUMBER)
18930 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931 else
18932 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018933 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018934 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018936 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937 }
18938 }
18939 msg_puts((char_u *)")");
18940 }
18941 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018942
18943 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944 --no_wait_return;
18945 }
18946 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018947#ifdef FEAT_PROFILE
18948 if (do_profiling)
18949 {
18950 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18951 func_do_profile(fp);
18952 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018953 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018954 {
18955 ++fp->uf_tm_count;
18956 profile_start(&fp->uf_tm_start);
18957 profile_zero(&fp->uf_tm_children);
18958 }
18959 script_prof_save(&wait_start);
18960 }
18961#endif
18962
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018964 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965 save_did_emsg = did_emsg;
18966 did_emsg = FALSE;
18967
18968 /* call do_cmdline() to execute the lines */
18969 do_cmdline(NULL, get_func_line, (void *)&fc,
18970 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18971
18972 --RedrawingDisabled;
18973
18974 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018975 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018977 clear_tv(rettv);
18978 rettv->v_type = VAR_NUMBER;
18979 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 }
18981
Bram Moolenaar05159a02005-02-26 23:04:13 +000018982#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018983 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018984 {
18985 profile_end(&fp->uf_tm_start);
18986 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18987 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18988 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18989 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018990 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018991 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018992 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18993 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018994 }
18995 }
18996#endif
18997
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 /* when being verbose, mention the return value */
18999 if (p_verbose >= 12)
19000 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019002 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019005 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019006 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019007 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19008 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019009 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019011 char_u buf[MSG_BUF_LEN];
19012 char_u numbuf[NUMBUFLEN];
19013 char_u *tofree;
19014
Bram Moolenaar555b2802005-05-19 21:08:39 +000019015 /* The value may be very long. Skip the middle part, so that we
19016 * have some idea how it starts and ends. smsg() would always
19017 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019018 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019019 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019020 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019021 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022 }
19023 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019024
19025 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 --no_wait_return;
19027 }
19028
19029 vim_free(sourcing_name);
19030 sourcing_name = save_sourcing_name;
19031 sourcing_lnum = save_sourcing_lnum;
19032 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019033#ifdef FEAT_PROFILE
19034 if (do_profiling)
19035 script_prof_restore(&wait_start);
19036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037
19038 if (p_verbose >= 12 && sourcing_name != NULL)
19039 {
19040 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019041 verbose_enter_scroll();
19042
Bram Moolenaar555b2802005-05-19 21:08:39 +000019043 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019045
19046 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 --no_wait_return;
19048 }
19049
19050 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019051 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052
Bram Moolenaar33570922005-01-25 22:26:29 +000019053 /* The a: variables typevals were not alloced, only free the allocated
19054 * variables. */
19055 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19056
19057 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 --depth;
19059}
19060
19061/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019062 * Add a number variable "name" to dict "dp" with value "nr".
19063 */
19064 static void
19065add_nr_var(dp, v, name, nr)
19066 dict_T *dp;
19067 dictitem_T *v;
19068 char *name;
19069 varnumber_T nr;
19070{
19071 STRCPY(v->di_key, name);
19072 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19073 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19074 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019075 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019076 v->di_tv.vval.v_number = nr;
19077}
19078
19079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 * ":return [expr]"
19081 */
19082 void
19083ex_return(eap)
19084 exarg_T *eap;
19085{
19086 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019087 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088 int returning = FALSE;
19089
19090 if (current_funccal == NULL)
19091 {
19092 EMSG(_("E133: :return not inside a function"));
19093 return;
19094 }
19095
19096 if (eap->skip)
19097 ++emsg_skip;
19098
19099 eap->nextcmd = NULL;
19100 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019101 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102 {
19103 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019104 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019106 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 }
19108 /* It's safer to return also on error. */
19109 else if (!eap->skip)
19110 {
19111 /*
19112 * Return unless the expression evaluation has been cancelled due to an
19113 * aborting error, an interrupt, or an exception.
19114 */
19115 if (!aborting())
19116 returning = do_return(eap, FALSE, TRUE, NULL);
19117 }
19118
19119 /* When skipping or the return gets pending, advance to the next command
19120 * in this line (!returning). Otherwise, ignore the rest of the line.
19121 * Following lines will be ignored by get_func_line(). */
19122 if (returning)
19123 eap->nextcmd = NULL;
19124 else if (eap->nextcmd == NULL) /* no argument */
19125 eap->nextcmd = check_nextcmd(arg);
19126
19127 if (eap->skip)
19128 --emsg_skip;
19129}
19130
19131/*
19132 * Return from a function. Possibly makes the return pending. Also called
19133 * for a pending return at the ":endtry" or after returning from an extra
19134 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019135 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019136 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137 * FALSE when the return gets pending.
19138 */
19139 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019140do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 exarg_T *eap;
19142 int reanimate;
19143 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019144 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145{
19146 int idx;
19147 struct condstack *cstack = eap->cstack;
19148
19149 if (reanimate)
19150 /* Undo the return. */
19151 current_funccal->returned = FALSE;
19152
19153 /*
19154 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19155 * not in its finally clause (which then is to be executed next) is found.
19156 * In this case, make the ":return" pending for execution at the ":endtry".
19157 * Otherwise, return normally.
19158 */
19159 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19160 if (idx >= 0)
19161 {
19162 cstack->cs_pending[idx] = CSTP_RETURN;
19163
19164 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019165 /* A pending return again gets pending. "rettv" points to an
19166 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019167 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019168 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169 else
19170 {
19171 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019172 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019174 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019176 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177 {
19178 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019179 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019180 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181 else
19182 EMSG(_(e_outofmem));
19183 }
19184 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019185 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186
19187 if (reanimate)
19188 {
19189 /* The pending return value could be overwritten by a ":return"
19190 * without argument in a finally clause; reset the default
19191 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019192 current_funccal->rettv->v_type = VAR_NUMBER;
19193 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019194 }
19195 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019196 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019197 }
19198 else
19199 {
19200 current_funccal->returned = TRUE;
19201
19202 /* If the return is carried out now, store the return value. For
19203 * a return immediately after reanimation, the value is already
19204 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019205 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019207 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019208 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019210 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211 }
19212 }
19213
19214 return idx < 0;
19215}
19216
19217/*
19218 * Free the variable with a pending return value.
19219 */
19220 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019221discard_pending_return(rettv)
19222 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223{
Bram Moolenaar33570922005-01-25 22:26:29 +000019224 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225}
19226
19227/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019228 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229 * is an allocated string. Used by report_pending() for verbose messages.
19230 */
19231 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019232get_return_cmd(rettv)
19233 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019235 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019236 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019237 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019239 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019240 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019241 if (s == NULL)
19242 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019243
19244 STRCPY(IObuff, ":return ");
19245 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19246 if (STRLEN(s) + 8 >= IOSIZE)
19247 STRCPY(IObuff + IOSIZE - 4, "...");
19248 vim_free(tofree);
19249 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250}
19251
19252/*
19253 * Get next function line.
19254 * Called by do_cmdline() to get the next line.
19255 * Returns allocated string, or NULL for end of function.
19256 */
19257/* ARGSUSED */
19258 char_u *
19259get_func_line(c, cookie, indent)
19260 int c; /* not used */
19261 void *cookie;
19262 int indent; /* not used */
19263{
Bram Moolenaar33570922005-01-25 22:26:29 +000019264 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019265 ufunc_T *fp = fcp->func;
19266 char_u *retval;
19267 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268
19269 /* If breakpoints have been added/deleted need to check for it. */
19270 if (fcp->dbg_tick != debug_tick)
19271 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019272 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273 sourcing_lnum);
19274 fcp->dbg_tick = debug_tick;
19275 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019276#ifdef FEAT_PROFILE
19277 if (do_profiling)
19278 func_line_end(cookie);
19279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280
Bram Moolenaar05159a02005-02-26 23:04:13 +000019281 gap = &fp->uf_lines;
19282 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019283 retval = NULL;
19284 else if (fcp->returned || fcp->linenr >= gap->ga_len)
19285 retval = NULL;
19286 else
19287 {
19288 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19289 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019290#ifdef FEAT_PROFILE
19291 if (do_profiling)
19292 func_line_start(cookie);
19293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294 }
19295
19296 /* Did we encounter a breakpoint? */
19297 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19298 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019299 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019301 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302 sourcing_lnum);
19303 fcp->dbg_tick = debug_tick;
19304 }
19305
19306 return retval;
19307}
19308
Bram Moolenaar05159a02005-02-26 23:04:13 +000019309#if defined(FEAT_PROFILE) || defined(PROTO)
19310/*
19311 * Called when starting to read a function line.
19312 * "sourcing_lnum" must be correct!
19313 * When skipping lines it may not actually be executed, but we won't find out
19314 * until later and we need to store the time now.
19315 */
19316 void
19317func_line_start(cookie)
19318 void *cookie;
19319{
19320 funccall_T *fcp = (funccall_T *)cookie;
19321 ufunc_T *fp = fcp->func;
19322
19323 if (fp->uf_profiling && sourcing_lnum >= 1
19324 && sourcing_lnum <= fp->uf_lines.ga_len)
19325 {
19326 fp->uf_tml_idx = sourcing_lnum - 1;
19327 fp->uf_tml_execed = FALSE;
19328 profile_start(&fp->uf_tml_start);
19329 profile_zero(&fp->uf_tml_children);
19330 profile_get_wait(&fp->uf_tml_wait);
19331 }
19332}
19333
19334/*
19335 * Called when actually executing a function line.
19336 */
19337 void
19338func_line_exec(cookie)
19339 void *cookie;
19340{
19341 funccall_T *fcp = (funccall_T *)cookie;
19342 ufunc_T *fp = fcp->func;
19343
19344 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19345 fp->uf_tml_execed = TRUE;
19346}
19347
19348/*
19349 * Called when done with a function line.
19350 */
19351 void
19352func_line_end(cookie)
19353 void *cookie;
19354{
19355 funccall_T *fcp = (funccall_T *)cookie;
19356 ufunc_T *fp = fcp->func;
19357
19358 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19359 {
19360 if (fp->uf_tml_execed)
19361 {
19362 ++fp->uf_tml_count[fp->uf_tml_idx];
19363 profile_end(&fp->uf_tml_start);
19364 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
19365 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
19366 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
19367 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
19368 }
19369 fp->uf_tml_idx = -1;
19370 }
19371}
19372#endif
19373
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374/*
19375 * Return TRUE if the currently active function should be ended, because a
19376 * return was encountered or an error occured. Used inside a ":while".
19377 */
19378 int
19379func_has_ended(cookie)
19380 void *cookie;
19381{
Bram Moolenaar33570922005-01-25 22:26:29 +000019382 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383
19384 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19385 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019386 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387 || fcp->returned);
19388}
19389
19390/*
19391 * return TRUE if cookie indicates a function which "abort"s on errors.
19392 */
19393 int
19394func_has_abort(cookie)
19395 void *cookie;
19396{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019397 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398}
19399
19400#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19401typedef enum
19402{
19403 VAR_FLAVOUR_DEFAULT,
19404 VAR_FLAVOUR_SESSION,
19405 VAR_FLAVOUR_VIMINFO
19406} var_flavour_T;
19407
19408static var_flavour_T var_flavour __ARGS((char_u *varname));
19409
19410 static var_flavour_T
19411var_flavour(varname)
19412 char_u *varname;
19413{
19414 char_u *p = varname;
19415
19416 if (ASCII_ISUPPER(*p))
19417 {
19418 while (*(++p))
19419 if (ASCII_ISLOWER(*p))
19420 return VAR_FLAVOUR_SESSION;
19421 return VAR_FLAVOUR_VIMINFO;
19422 }
19423 else
19424 return VAR_FLAVOUR_DEFAULT;
19425}
19426#endif
19427
19428#if defined(FEAT_VIMINFO) || defined(PROTO)
19429/*
19430 * Restore global vars that start with a capital from the viminfo file
19431 */
19432 int
19433read_viminfo_varlist(virp, writing)
19434 vir_T *virp;
19435 int writing;
19436{
19437 char_u *tab;
19438 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019439 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440
19441 if (!writing && (find_viminfo_parameter('!') != NULL))
19442 {
19443 tab = vim_strchr(virp->vir_line + 1, '\t');
19444 if (tab != NULL)
19445 {
19446 *tab++ = '\0'; /* isolate the variable name */
19447 if (*tab == 'S') /* string var */
19448 is_string = TRUE;
19449
19450 tab = vim_strchr(tab, '\t');
19451 if (tab != NULL)
19452 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453 if (is_string)
19454 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019455 tv.v_type = VAR_STRING;
19456 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 }
19459 else
19460 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019461 tv.v_type = VAR_NUMBER;
19462 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019464 set_var(virp->vir_line + 1, &tv, FALSE);
19465 if (is_string)
19466 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467 }
19468 }
19469 }
19470
19471 return viminfo_readline(virp);
19472}
19473
19474/*
19475 * Write global vars that start with a capital to the viminfo file
19476 */
19477 void
19478write_viminfo_varlist(fp)
19479 FILE *fp;
19480{
Bram Moolenaar33570922005-01-25 22:26:29 +000019481 hashitem_T *hi;
19482 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019483 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019484 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019485 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019486 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019487 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488
19489 if (find_viminfo_parameter('!') == NULL)
19490 return;
19491
19492 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019493
Bram Moolenaar33570922005-01-25 22:26:29 +000019494 todo = globvarht.ht_used;
19495 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019497 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019499 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019500 this_var = HI2DI(hi);
19501 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019502 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019503 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019504 {
19505 case VAR_STRING: s = "STR"; break;
19506 case VAR_NUMBER: s = "NUM"; break;
19507 default: continue;
19508 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019509 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019510 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019511 if (p != NULL)
19512 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019513 vim_free(tofree);
19514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515 }
19516 }
19517}
19518#endif
19519
19520#if defined(FEAT_SESSION) || defined(PROTO)
19521 int
19522store_session_globals(fd)
19523 FILE *fd;
19524{
Bram Moolenaar33570922005-01-25 22:26:29 +000019525 hashitem_T *hi;
19526 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019527 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 char_u *p, *t;
19529
Bram Moolenaar33570922005-01-25 22:26:29 +000019530 todo = globvarht.ht_used;
19531 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019533 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019535 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019536 this_var = HI2DI(hi);
19537 if ((this_var->di_tv.v_type == VAR_NUMBER
19538 || this_var->di_tv.v_type == VAR_STRING)
19539 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019540 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019541 /* Escape special characters with a backslash. Turn a LF and
19542 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019543 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019544 (char_u *)"\\\"\n\r");
19545 if (p == NULL) /* out of memory */
19546 break;
19547 for (t = p; *t != NUL; ++t)
19548 if (*t == '\n')
19549 *t = 'n';
19550 else if (*t == '\r')
19551 *t = 'r';
19552 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019553 this_var->di_key,
19554 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19555 : ' ',
19556 p,
19557 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19558 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019559 || put_eol(fd) == FAIL)
19560 {
19561 vim_free(p);
19562 return FAIL;
19563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564 vim_free(p);
19565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566 }
19567 }
19568 return OK;
19569}
19570#endif
19571
Bram Moolenaar661b1822005-07-28 22:36:45 +000019572/*
19573 * Display script name where an item was last set.
19574 * Should only be invoked when 'verbose' is non-zero.
19575 */
19576 void
19577last_set_msg(scriptID)
19578 scid_T scriptID;
19579{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019580 char_u *p;
19581
Bram Moolenaar661b1822005-07-28 22:36:45 +000019582 if (scriptID != 0)
19583 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019584 p = home_replace_save(NULL, get_scriptname(scriptID));
19585 if (p != NULL)
19586 {
19587 verbose_enter();
19588 MSG_PUTS(_("\n\tLast set from "));
19589 MSG_PUTS(p);
19590 vim_free(p);
19591 verbose_leave();
19592 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019593 }
19594}
19595
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596#endif /* FEAT_EVAL */
19597
19598#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19599
19600
19601#ifdef WIN3264
19602/*
19603 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19604 */
19605static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19606static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19607static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19608
19609/*
19610 * Get the short pathname of a file.
19611 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19612 */
19613 static int
19614get_short_pathname(fnamep, bufp, fnamelen)
19615 char_u **fnamep;
19616 char_u **bufp;
19617 int *fnamelen;
19618{
19619 int l,len;
19620 char_u *newbuf;
19621
19622 len = *fnamelen;
19623
19624 l = GetShortPathName(*fnamep, *fnamep, len);
19625 if (l > len - 1)
19626 {
19627 /* If that doesn't work (not enough space), then save the string
19628 * and try again with a new buffer big enough
19629 */
19630 newbuf = vim_strnsave(*fnamep, l);
19631 if (newbuf == NULL)
19632 return 0;
19633
19634 vim_free(*bufp);
19635 *fnamep = *bufp = newbuf;
19636
19637 l = GetShortPathName(*fnamep,*fnamep,l+1);
19638
19639 /* Really should always succeed, as the buffer is big enough */
19640 }
19641
19642 *fnamelen = l;
19643 return 1;
19644}
19645
19646/*
19647 * Create a short path name. Returns the length of the buffer it needs.
19648 * Doesn't copy over the end of the buffer passed in.
19649 */
19650 static int
19651shortpath_for_invalid_fname(fname, bufp, fnamelen)
19652 char_u **fname;
19653 char_u **bufp;
19654 int *fnamelen;
19655{
19656 char_u *s, *p, *pbuf2, *pbuf3;
19657 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019658 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659
19660 /* Make a copy */
19661 len2 = *fnamelen;
19662 pbuf2 = vim_strnsave(*fname, len2);
19663 pbuf3 = NULL;
19664
19665 s = pbuf2 + len2 - 1; /* Find the end */
19666 slen = 1;
19667 plen = len2;
19668
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019669 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670 {
19671 --s;
19672 ++slen;
19673 --plen;
19674 }
19675
19676 do
19677 {
19678 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019679 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 {
19681 --s;
19682 ++slen;
19683 --plen;
19684 }
19685 if (s <= pbuf2)
19686 break;
19687
19688 /* Remeber the character that is about to be blatted */
19689 ch = *s;
19690 *s = 0; /* get_short_pathname requires a null-terminated string */
19691
19692 /* Try it in situ */
19693 p = pbuf2;
19694 if (!get_short_pathname(&p, &pbuf3, &plen))
19695 {
19696 vim_free(pbuf2);
19697 return -1;
19698 }
19699 *s = ch; /* Preserve the string */
19700 } while (plen == 0);
19701
19702 if (plen > 0)
19703 {
19704 /* Remeber the length of the new string. */
19705 *fnamelen = len = plen + slen;
19706 vim_free(*bufp);
19707 if (len > len2)
19708 {
19709 /* If there's not enough space in the currently allocated string,
19710 * then copy it to a buffer big enough.
19711 */
19712 *fname= *bufp = vim_strnsave(p, len);
19713 if (*fname == NULL)
19714 return -1;
19715 }
19716 else
19717 {
19718 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19719 *fname = *bufp = pbuf2;
19720 if (p != pbuf2)
19721 strncpy(*fname, p, plen);
19722 pbuf2 = NULL;
19723 }
19724 /* Concat the next bit */
19725 strncpy(*fname + plen, s, slen);
19726 (*fname)[len] = '\0';
19727 }
19728 vim_free(pbuf3);
19729 vim_free(pbuf2);
19730 return 0;
19731}
19732
19733/*
19734 * Get a pathname for a partial path.
19735 */
19736 static int
19737shortpath_for_partial(fnamep, bufp, fnamelen)
19738 char_u **fnamep;
19739 char_u **bufp;
19740 int *fnamelen;
19741{
19742 int sepcount, len, tflen;
19743 char_u *p;
19744 char_u *pbuf, *tfname;
19745 int hasTilde;
19746
19747 /* Count up the path seperators from the RHS.. so we know which part
19748 * of the path to return.
19749 */
19750 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019751 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752 if (vim_ispathsep(*p))
19753 ++sepcount;
19754
19755 /* Need full path first (use expand_env() to remove a "~/") */
19756 hasTilde = (**fnamep == '~');
19757 if (hasTilde)
19758 pbuf = tfname = expand_env_save(*fnamep);
19759 else
19760 pbuf = tfname = FullName_save(*fnamep, FALSE);
19761
19762 len = tflen = STRLEN(tfname);
19763
19764 if (!get_short_pathname(&tfname, &pbuf, &len))
19765 return -1;
19766
19767 if (len == 0)
19768 {
19769 /* Don't have a valid filename, so shorten the rest of the
19770 * path if we can. This CAN give us invalid 8.3 filenames, but
19771 * there's not a lot of point in guessing what it might be.
19772 */
19773 len = tflen;
19774 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19775 return -1;
19776 }
19777
19778 /* Count the paths backward to find the beginning of the desired string. */
19779 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019780 {
19781#ifdef FEAT_MBYTE
19782 if (has_mbyte)
19783 p -= mb_head_off(tfname, p);
19784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785 if (vim_ispathsep(*p))
19786 {
19787 if (sepcount == 0 || (hasTilde && sepcount == 1))
19788 break;
19789 else
19790 sepcount --;
19791 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 if (hasTilde)
19794 {
19795 --p;
19796 if (p >= tfname)
19797 *p = '~';
19798 else
19799 return -1;
19800 }
19801 else
19802 ++p;
19803
19804 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19805 vim_free(*bufp);
19806 *fnamelen = (int)STRLEN(p);
19807 *bufp = pbuf;
19808 *fnamep = p;
19809
19810 return 0;
19811}
19812#endif /* WIN3264 */
19813
19814/*
19815 * Adjust a filename, according to a string of modifiers.
19816 * *fnamep must be NUL terminated when called. When returning, the length is
19817 * determined by *fnamelen.
19818 * Returns valid flags.
19819 * When there is an error, *fnamep is set to NULL.
19820 */
19821 int
19822modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19823 char_u *src; /* string with modifiers */
19824 int *usedlen; /* characters after src that are used */
19825 char_u **fnamep; /* file name so far */
19826 char_u **bufp; /* buffer for allocated file name or NULL */
19827 int *fnamelen; /* length of fnamep */
19828{
19829 int valid = 0;
19830 char_u *tail;
19831 char_u *s, *p, *pbuf;
19832 char_u dirname[MAXPATHL];
19833 int c;
19834 int has_fullname = 0;
19835#ifdef WIN3264
19836 int has_shortname = 0;
19837#endif
19838
19839repeat:
19840 /* ":p" - full path/file_name */
19841 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19842 {
19843 has_fullname = 1;
19844
19845 valid |= VALID_PATH;
19846 *usedlen += 2;
19847
19848 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19849 if ((*fnamep)[0] == '~'
19850#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19851 && ((*fnamep)[1] == '/'
19852# ifdef BACKSLASH_IN_FILENAME
19853 || (*fnamep)[1] == '\\'
19854# endif
19855 || (*fnamep)[1] == NUL)
19856
19857#endif
19858 )
19859 {
19860 *fnamep = expand_env_save(*fnamep);
19861 vim_free(*bufp); /* free any allocated file name */
19862 *bufp = *fnamep;
19863 if (*fnamep == NULL)
19864 return -1;
19865 }
19866
19867 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019868 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869 {
19870 if (vim_ispathsep(*p)
19871 && p[1] == '.'
19872 && (p[2] == NUL
19873 || vim_ispathsep(p[2])
19874 || (p[2] == '.'
19875 && (p[3] == NUL || vim_ispathsep(p[3])))))
19876 break;
19877 }
19878
19879 /* FullName_save() is slow, don't use it when not needed. */
19880 if (*p != NUL || !vim_isAbsName(*fnamep))
19881 {
19882 *fnamep = FullName_save(*fnamep, *p != NUL);
19883 vim_free(*bufp); /* free any allocated file name */
19884 *bufp = *fnamep;
19885 if (*fnamep == NULL)
19886 return -1;
19887 }
19888
19889 /* Append a path separator to a directory. */
19890 if (mch_isdir(*fnamep))
19891 {
19892 /* Make room for one or two extra characters. */
19893 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19894 vim_free(*bufp); /* free any allocated file name */
19895 *bufp = *fnamep;
19896 if (*fnamep == NULL)
19897 return -1;
19898 add_pathsep(*fnamep);
19899 }
19900 }
19901
19902 /* ":." - path relative to the current directory */
19903 /* ":~" - path relative to the home directory */
19904 /* ":8" - shortname path - postponed till after */
19905 while (src[*usedlen] == ':'
19906 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19907 {
19908 *usedlen += 2;
19909 if (c == '8')
19910 {
19911#ifdef WIN3264
19912 has_shortname = 1; /* Postpone this. */
19913#endif
19914 continue;
19915 }
19916 pbuf = NULL;
19917 /* Need full path first (use expand_env() to remove a "~/") */
19918 if (!has_fullname)
19919 {
19920 if (c == '.' && **fnamep == '~')
19921 p = pbuf = expand_env_save(*fnamep);
19922 else
19923 p = pbuf = FullName_save(*fnamep, FALSE);
19924 }
19925 else
19926 p = *fnamep;
19927
19928 has_fullname = 0;
19929
19930 if (p != NULL)
19931 {
19932 if (c == '.')
19933 {
19934 mch_dirname(dirname, MAXPATHL);
19935 s = shorten_fname(p, dirname);
19936 if (s != NULL)
19937 {
19938 *fnamep = s;
19939 if (pbuf != NULL)
19940 {
19941 vim_free(*bufp); /* free any allocated file name */
19942 *bufp = pbuf;
19943 pbuf = NULL;
19944 }
19945 }
19946 }
19947 else
19948 {
19949 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19950 /* Only replace it when it starts with '~' */
19951 if (*dirname == '~')
19952 {
19953 s = vim_strsave(dirname);
19954 if (s != NULL)
19955 {
19956 *fnamep = s;
19957 vim_free(*bufp);
19958 *bufp = s;
19959 }
19960 }
19961 }
19962 vim_free(pbuf);
19963 }
19964 }
19965
19966 tail = gettail(*fnamep);
19967 *fnamelen = (int)STRLEN(*fnamep);
19968
19969 /* ":h" - head, remove "/file_name", can be repeated */
19970 /* Don't remove the first "/" or "c:\" */
19971 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19972 {
19973 valid |= VALID_HEAD;
19974 *usedlen += 2;
19975 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019976 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 --tail;
19978 *fnamelen = (int)(tail - *fnamep);
19979#ifdef VMS
19980 if (*fnamelen > 0)
19981 *fnamelen += 1; /* the path separator is part of the path */
19982#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019983 while (tail > s && !after_pathsep(s, tail))
19984 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985 }
19986
19987 /* ":8" - shortname */
19988 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19989 {
19990 *usedlen += 2;
19991#ifdef WIN3264
19992 has_shortname = 1;
19993#endif
19994 }
19995
19996#ifdef WIN3264
19997 /* Check shortname after we have done 'heads' and before we do 'tails'
19998 */
19999 if (has_shortname)
20000 {
20001 pbuf = NULL;
20002 /* Copy the string if it is shortened by :h */
20003 if (*fnamelen < (int)STRLEN(*fnamep))
20004 {
20005 p = vim_strnsave(*fnamep, *fnamelen);
20006 if (p == 0)
20007 return -1;
20008 vim_free(*bufp);
20009 *bufp = *fnamep = p;
20010 }
20011
20012 /* Split into two implementations - makes it easier. First is where
20013 * there isn't a full name already, second is where there is.
20014 */
20015 if (!has_fullname && !vim_isAbsName(*fnamep))
20016 {
20017 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20018 return -1;
20019 }
20020 else
20021 {
20022 int l;
20023
20024 /* Simple case, already have the full-name
20025 * Nearly always shorter, so try first time. */
20026 l = *fnamelen;
20027 if (!get_short_pathname(fnamep, bufp, &l))
20028 return -1;
20029
20030 if (l == 0)
20031 {
20032 /* Couldn't find the filename.. search the paths.
20033 */
20034 l = *fnamelen;
20035 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20036 return -1;
20037 }
20038 *fnamelen = l;
20039 }
20040 }
20041#endif /* WIN3264 */
20042
20043 /* ":t" - tail, just the basename */
20044 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20045 {
20046 *usedlen += 2;
20047 *fnamelen -= (int)(tail - *fnamep);
20048 *fnamep = tail;
20049 }
20050
20051 /* ":e" - extension, can be repeated */
20052 /* ":r" - root, without extension, can be repeated */
20053 while (src[*usedlen] == ':'
20054 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20055 {
20056 /* find a '.' in the tail:
20057 * - for second :e: before the current fname
20058 * - otherwise: The last '.'
20059 */
20060 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20061 s = *fnamep - 2;
20062 else
20063 s = *fnamep + *fnamelen - 1;
20064 for ( ; s > tail; --s)
20065 if (s[0] == '.')
20066 break;
20067 if (src[*usedlen + 1] == 'e') /* :e */
20068 {
20069 if (s > tail)
20070 {
20071 *fnamelen += (int)(*fnamep - (s + 1));
20072 *fnamep = s + 1;
20073#ifdef VMS
20074 /* cut version from the extension */
20075 s = *fnamep + *fnamelen - 1;
20076 for ( ; s > *fnamep; --s)
20077 if (s[0] == ';')
20078 break;
20079 if (s > *fnamep)
20080 *fnamelen = s - *fnamep;
20081#endif
20082 }
20083 else if (*fnamep <= tail)
20084 *fnamelen = 0;
20085 }
20086 else /* :r */
20087 {
20088 if (s > tail) /* remove one extension */
20089 *fnamelen = (int)(s - *fnamep);
20090 }
20091 *usedlen += 2;
20092 }
20093
20094 /* ":s?pat?foo?" - substitute */
20095 /* ":gs?pat?foo?" - global substitute */
20096 if (src[*usedlen] == ':'
20097 && (src[*usedlen + 1] == 's'
20098 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20099 {
20100 char_u *str;
20101 char_u *pat;
20102 char_u *sub;
20103 int sep;
20104 char_u *flags;
20105 int didit = FALSE;
20106
20107 flags = (char_u *)"";
20108 s = src + *usedlen + 2;
20109 if (src[*usedlen + 1] == 'g')
20110 {
20111 flags = (char_u *)"g";
20112 ++s;
20113 }
20114
20115 sep = *s++;
20116 if (sep)
20117 {
20118 /* find end of pattern */
20119 p = vim_strchr(s, sep);
20120 if (p != NULL)
20121 {
20122 pat = vim_strnsave(s, (int)(p - s));
20123 if (pat != NULL)
20124 {
20125 s = p + 1;
20126 /* find end of substitution */
20127 p = vim_strchr(s, sep);
20128 if (p != NULL)
20129 {
20130 sub = vim_strnsave(s, (int)(p - s));
20131 str = vim_strnsave(*fnamep, *fnamelen);
20132 if (sub != NULL && str != NULL)
20133 {
20134 *usedlen = (int)(p + 1 - src);
20135 s = do_string_sub(str, pat, sub, flags);
20136 if (s != NULL)
20137 {
20138 *fnamep = s;
20139 *fnamelen = (int)STRLEN(s);
20140 vim_free(*bufp);
20141 *bufp = s;
20142 didit = TRUE;
20143 }
20144 }
20145 vim_free(sub);
20146 vim_free(str);
20147 }
20148 vim_free(pat);
20149 }
20150 }
20151 /* after using ":s", repeat all the modifiers */
20152 if (didit)
20153 goto repeat;
20154 }
20155 }
20156
20157 return valid;
20158}
20159
20160/*
20161 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20162 * "flags" can be "g" to do a global substitute.
20163 * Returns an allocated string, NULL for error.
20164 */
20165 char_u *
20166do_string_sub(str, pat, sub, flags)
20167 char_u *str;
20168 char_u *pat;
20169 char_u *sub;
20170 char_u *flags;
20171{
20172 int sublen;
20173 regmatch_T regmatch;
20174 int i;
20175 int do_all;
20176 char_u *tail;
20177 garray_T ga;
20178 char_u *ret;
20179 char_u *save_cpo;
20180
20181 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20182 save_cpo = p_cpo;
20183 p_cpo = (char_u *)"";
20184
20185 ga_init2(&ga, 1, 200);
20186
20187 do_all = (flags[0] == 'g');
20188
20189 regmatch.rm_ic = p_ic;
20190 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20191 if (regmatch.regprog != NULL)
20192 {
20193 tail = str;
20194 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20195 {
20196 /*
20197 * Get some space for a temporary buffer to do the substitution
20198 * into. It will contain:
20199 * - The text up to where the match is.
20200 * - The substituted text.
20201 * - The text after the match.
20202 */
20203 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20204 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20205 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20206 {
20207 ga_clear(&ga);
20208 break;
20209 }
20210
20211 /* copy the text up to where the match is */
20212 i = (int)(regmatch.startp[0] - tail);
20213 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20214 /* add the substituted text */
20215 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20216 + ga.ga_len + i, TRUE, TRUE, FALSE);
20217 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218 /* avoid getting stuck on a match with an empty string */
20219 if (tail == regmatch.endp[0])
20220 {
20221 if (*tail == NUL)
20222 break;
20223 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20224 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 }
20226 else
20227 {
20228 tail = regmatch.endp[0];
20229 if (*tail == NUL)
20230 break;
20231 }
20232 if (!do_all)
20233 break;
20234 }
20235
20236 if (ga.ga_data != NULL)
20237 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20238
20239 vim_free(regmatch.regprog);
20240 }
20241
20242 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20243 ga_clear(&ga);
20244 p_cpo = save_cpo;
20245
20246 return ret;
20247}
20248
20249#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */