blob: b1bf7f10ea5f54805c2e0b0a6e3ecb93bc5874ad [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000344};
345
346/* shorthand */
347#define vv_type vv_di.di_tv.v_type
348#define vv_nr vv_di.di_tv.vval.v_number
349#define vv_str vv_di.di_tv.vval.v_string
350#define vv_tv vv_di.di_tv
351
352/*
353 * The v: variables are stored in dictionary "vimvardict".
354 * "vimvars_var" is the variable that is used for the "l:" scope.
355 */
356static dict_T vimvardict;
357static dictitem_T vimvars_var;
358#define vimvarht vimvardict.dv_hashtab
359
Bram Moolenaara40058a2005-07-11 22:42:07 +0000360static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
361static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
362#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
363static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
364#endif
365static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
366static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
367static char_u *skip_var_one __ARGS((char_u *arg));
368static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
369static void list_glob_vars __ARGS((void));
370static void list_buf_vars __ARGS((void));
371static void list_win_vars __ARGS((void));
372static void list_vim_vars __ARGS((void));
373static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
374static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
375static int check_changedtick __ARGS((char_u *arg));
376static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
377static void clear_lval __ARGS((lval_T *lp));
378static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
379static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
380static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
381static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
382static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
383static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
384static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
385static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
386static void item_lock __ARGS((typval_T *tv, int deep, int lock));
387static int tv_islocked __ARGS((typval_T *tv));
388
Bram Moolenaar33570922005-01-25 22:26:29 +0000389static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
390static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000398static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000403static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000404static listitem_T *listitem_alloc __ARGS((void));
405static void listitem_free __ARGS((listitem_T *item));
406static void listitem_remove __ARGS((list_T *l, listitem_T *item));
407static long list_len __ARGS((list_T *l));
408static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
409static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
410static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000411static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000412static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static void list_append __ARGS((list_T *l, listitem_T *item));
415static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000416static int list_append_string __ARGS((list_T *l, char_u *str, int len));
417static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
419static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
420static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000421static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000423static char_u *list2string __ARGS((typval_T *tv, int copyID));
424static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000425static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
426static void set_ref_in_list __ARGS((list_T *l, int copyID));
427static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static void dict_unref __ARGS((dict_T *d));
429static void dict_free __ARGS((dict_T *d));
430static dictitem_T *dictitem_alloc __ARGS((char_u *key));
431static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
432static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
433static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int dict_add __ARGS((dict_T *d, dictitem_T *item));
436static long dict_len __ARGS((dict_T *d));
437static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
441static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static char_u *string_quote __ARGS((char_u *str, int function));
443static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
444static int find_internal_func __ARGS((char_u *name));
445static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
446static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
447static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000448static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449
450static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000469#if defined(FEAT_INS_EXPAND)
470static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
472#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000473static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
478static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000504static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000506static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000512static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000513static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000520static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000521static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000544static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000550static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000567static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000571#ifdef vim_mkdir
572static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
573#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000578static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000579static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000581static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000593static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000595static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000602static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000603static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000604static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000608static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000609static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
612#ifdef HAVE_STRFTIME
613static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
614#endif
615static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000627static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000628static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000629static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000630static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000631static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000633static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000648static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000649
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
651static int get_env_len __ARGS((char_u **arg));
652static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000653static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000654static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
655#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
656#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
657 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000658static 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 +0000659static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000660static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000661static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
662static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static typval_T *alloc_tv __ARGS((void));
664static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static void init_tv __ARGS((typval_T *varp));
666static long get_tv_number __ARGS((typval_T *varp));
667static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000668static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static char_u *get_tv_string __ARGS((typval_T *varp));
670static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000671static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000673static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
675static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
676static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
677static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
678static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
679static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
680static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000681static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000683static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
685static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
686static int eval_fname_script __ARGS((char_u *p));
687static int eval_fname_sid __ARGS((char_u *p));
688static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static ufunc_T *find_func __ARGS((char_u *name));
690static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000691static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000692#ifdef FEAT_PROFILE
693static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000694static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
695static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
696static int
697# ifdef __BORLANDC__
698 _RTLENTRYF
699# endif
700 prof_total_cmp __ARGS((const void *s1, const void *s2));
701static int
702# ifdef __BORLANDC__
703 _RTLENTRYF
704# endif
705 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000706#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000707static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000708static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000709static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static void func_free __ARGS((ufunc_T *fp));
711static void func_unref __ARGS((char_u *name));
712static void func_ref __ARGS((char_u *name));
713static 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));
714static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000715static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000716static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
717static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar33570922005-01-25 22:26:29 +0000718
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000719/* Character used as separated in autoload function/variable names. */
720#define AUTOLOAD_CHAR '#'
721
Bram Moolenaar33570922005-01-25 22:26:29 +0000722/*
723 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000724 */
725 void
726eval_init()
727{
Bram Moolenaar33570922005-01-25 22:26:29 +0000728 int i;
729 struct vimvar *p;
730
731 init_var_dict(&globvardict, &globvars_var);
732 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000733 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000734 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000735
736 for (i = 0; i < VV_LEN; ++i)
737 {
738 p = &vimvars[i];
739 STRCPY(p->vv_di.di_key, p->vv_name);
740 if (p->vv_flags & VV_RO)
741 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
742 else if (p->vv_flags & VV_RO_SBX)
743 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
744 else
745 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000746
747 /* add to v: scope dict, unless the value is not always available */
748 if (p->vv_type != VAR_UNKNOWN)
749 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000750 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000751 /* add to compat scope dict */
752 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000753 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000754}
755
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000756#if defined(EXITFREE) || defined(PROTO)
757 void
758eval_clear()
759{
760 int i;
761 struct vimvar *p;
762
763 for (i = 0; i < VV_LEN; ++i)
764 {
765 p = &vimvars[i];
766 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000767 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000768 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000769 p->vv_di.di_tv.vval.v_string = NULL;
770 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000771 }
772 hash_clear(&vimvarht);
773 hash_clear(&compat_hashtab);
774
775 /* script-local variables */
776 for (i = 1; i <= ga_scripts.ga_len; ++i)
777 vars_clear(&SCRIPT_VARS(i));
778 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000779 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000780
781 /* global variables */
782 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000783
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000784 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000785 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000786 hash_clear(&func_hashtab);
787
788 /* unreferenced lists and dicts */
789 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000790}
791#endif
792
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 * Return the name of the executed function.
795 */
796 char_u *
797func_name(cookie)
798 void *cookie;
799{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000800 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801}
802
803/*
804 * Return the address holding the next breakpoint line for a funccall cookie.
805 */
806 linenr_T *
807func_breakpoint(cookie)
808 void *cookie;
809{
Bram Moolenaar33570922005-01-25 22:26:29 +0000810 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811}
812
813/*
814 * Return the address holding the debug tick for a funccall cookie.
815 */
816 int *
817func_dbg_tick(cookie)
818 void *cookie;
819{
Bram Moolenaar33570922005-01-25 22:26:29 +0000820 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821}
822
823/*
824 * Return the nesting level for a funccall cookie.
825 */
826 int
827func_level(cookie)
828 void *cookie;
829{
Bram Moolenaar33570922005-01-25 22:26:29 +0000830 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831}
832
833/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000834funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835
836/*
837 * Return TRUE when a function was ended by a ":return" command.
838 */
839 int
840current_func_returned()
841{
842 return current_funccal->returned;
843}
844
845
846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 * Set an internal variable to a string value. Creates the variable if it does
848 * not already exist.
849 */
850 void
851set_internal_string_var(name, value)
852 char_u *name;
853 char_u *value;
854{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000855 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000856 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
858 val = vim_strsave(value);
859 if (val != NULL)
860 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000861 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000862 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000864 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000865 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
867 }
868}
869
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000870static lval_T *redir_lval = NULL;
871static char_u *redir_endp = NULL;
872static char_u *redir_varname = NULL;
873
874/*
875 * Start recording command output to a variable
876 * Returns OK if successfully completed the setup. FAIL otherwise.
877 */
878 int
879var_redir_start(name, append)
880 char_u *name;
881 int append; /* append to an existing variable */
882{
883 int save_emsg;
884 int err;
885 typval_T tv;
886
887 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000888 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000889 {
890 EMSG(_(e_invarg));
891 return FAIL;
892 }
893
894 redir_varname = vim_strsave(name);
895 if (redir_varname == NULL)
896 return FAIL;
897
898 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
899 if (redir_lval == NULL)
900 {
901 var_redir_stop();
902 return FAIL;
903 }
904
905 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000906 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
907 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000908 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
909 {
910 if (redir_endp != NULL && *redir_endp != NUL)
911 /* Trailing characters are present after the variable name */
912 EMSG(_(e_trailing));
913 else
914 EMSG(_(e_invarg));
915 var_redir_stop();
916 return FAIL;
917 }
918
919 /* check if we can write to the variable: set it to or append an empty
920 * string */
921 save_emsg = did_emsg;
922 did_emsg = FALSE;
923 tv.v_type = VAR_STRING;
924 tv.vval.v_string = (char_u *)"";
925 if (append)
926 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
927 else
928 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
929 err = did_emsg;
930 did_emsg += save_emsg;
931 if (err)
932 {
933 var_redir_stop();
934 return FAIL;
935 }
936 if (redir_lval->ll_newkey != NULL)
937 {
938 /* Dictionary item was created, don't do it again. */
939 vim_free(redir_lval->ll_newkey);
940 redir_lval->ll_newkey = NULL;
941 }
942
943 return OK;
944}
945
946/*
947 * Append "value[len]" to the variable set by var_redir_start().
948 */
949 void
950var_redir_str(value, len)
951 char_u *value;
952 int len;
953{
954 char_u *val;
955 typval_T tv;
956 int save_emsg;
957 int err;
958
959 if (redir_lval == NULL)
960 return;
961
962 if (len == -1)
963 /* Append the entire string */
964 val = vim_strsave(value);
965 else
966 /* Append only the specified number of characters */
967 val = vim_strnsave(value, len);
968 if (val == NULL)
969 return;
970
971 tv.v_type = VAR_STRING;
972 tv.vval.v_string = val;
973
974 save_emsg = did_emsg;
975 did_emsg = FALSE;
976 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
977 err = did_emsg;
978 did_emsg += save_emsg;
979 if (err)
980 var_redir_stop();
981
982 vim_free(tv.vval.v_string);
983}
984
985/*
986 * Stop redirecting command output to a variable.
987 */
988 void
989var_redir_stop()
990{
991 if (redir_lval != NULL)
992 {
993 clear_lval(redir_lval);
994 vim_free(redir_lval);
995 redir_lval = NULL;
996 }
997 vim_free(redir_varname);
998 redir_varname = NULL;
999}
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001# if defined(FEAT_MBYTE) || defined(PROTO)
1002 int
1003eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1004 char_u *enc_from;
1005 char_u *enc_to;
1006 char_u *fname_from;
1007 char_u *fname_to;
1008{
1009 int err = FALSE;
1010
1011 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1012 set_vim_var_string(VV_CC_TO, enc_to, -1);
1013 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1014 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1015 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1016 err = TRUE;
1017 set_vim_var_string(VV_CC_FROM, NULL, -1);
1018 set_vim_var_string(VV_CC_TO, NULL, -1);
1019 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1020 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1021
1022 if (err)
1023 return FAIL;
1024 return OK;
1025}
1026# endif
1027
1028# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1029 int
1030eval_printexpr(fname, args)
1031 char_u *fname;
1032 char_u *args;
1033{
1034 int err = FALSE;
1035
1036 set_vim_var_string(VV_FNAME_IN, fname, -1);
1037 set_vim_var_string(VV_CMDARG, args, -1);
1038 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1039 err = TRUE;
1040 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1041 set_vim_var_string(VV_CMDARG, NULL, -1);
1042
1043 if (err)
1044 {
1045 mch_remove(fname);
1046 return FAIL;
1047 }
1048 return OK;
1049}
1050# endif
1051
1052# if defined(FEAT_DIFF) || defined(PROTO)
1053 void
1054eval_diff(origfile, newfile, outfile)
1055 char_u *origfile;
1056 char_u *newfile;
1057 char_u *outfile;
1058{
1059 int err = FALSE;
1060
1061 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1062 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1063 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1064 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1065 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1066 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1067 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1068}
1069
1070 void
1071eval_patch(origfile, difffile, outfile)
1072 char_u *origfile;
1073 char_u *difffile;
1074 char_u *outfile;
1075{
1076 int err;
1077
1078 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1079 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1080 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1081 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1082 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1083 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1084 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1085}
1086# endif
1087
1088/*
1089 * Top level evaluation function, returning a boolean.
1090 * Sets "error" to TRUE if there was an error.
1091 * Return TRUE or FALSE.
1092 */
1093 int
1094eval_to_bool(arg, error, nextcmd, skip)
1095 char_u *arg;
1096 int *error;
1097 char_u **nextcmd;
1098 int skip; /* only parse, don't execute */
1099{
Bram Moolenaar33570922005-01-25 22:26:29 +00001100 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 int retval = FALSE;
1102
1103 if (skip)
1104 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001105 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 else
1108 {
1109 *error = FALSE;
1110 if (!skip)
1111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001112 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001113 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 }
1115 }
1116 if (skip)
1117 --emsg_skip;
1118
1119 return retval;
1120}
1121
1122/*
1123 * Top level evaluation function, returning a string. If "skip" is TRUE,
1124 * only parsing to "nextcmd" is done, without reporting errors. Return
1125 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1126 */
1127 char_u *
1128eval_to_string_skip(arg, nextcmd, skip)
1129 char_u *arg;
1130 char_u **nextcmd;
1131 int skip; /* only parse, don't execute */
1132{
Bram Moolenaar33570922005-01-25 22:26:29 +00001133 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 char_u *retval;
1135
1136 if (skip)
1137 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001138 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 retval = NULL;
1140 else
1141 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 retval = vim_strsave(get_tv_string(&tv));
1143 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
1145 if (skip)
1146 --emsg_skip;
1147
1148 return retval;
1149}
1150
1151/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001152 * Skip over an expression at "*pp".
1153 * Return FAIL for an error, OK otherwise.
1154 */
1155 int
1156skip_expr(pp)
1157 char_u **pp;
1158{
Bram Moolenaar33570922005-01-25 22:26:29 +00001159 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001160
1161 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001162 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001163}
1164
1165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 * Top level evaluation function, returning a string.
1167 * Return pointer to allocated memory, or NULL for failure.
1168 */
1169 char_u *
1170eval_to_string(arg, nextcmd)
1171 char_u *arg;
1172 char_u **nextcmd;
1173{
Bram Moolenaar33570922005-01-25 22:26:29 +00001174 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 char_u *retval;
1176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001177 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 retval = NULL;
1179 else
1180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001181 retval = vim_strsave(get_tv_string(&tv));
1182 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
1184
1185 return retval;
1186}
1187
1188/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001189 * Call eval_to_string() without using current local variables and using
1190 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 */
1192 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001193eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 char_u *arg;
1195 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001196 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197{
1198 char_u *retval;
1199 void *save_funccalp;
1200
1201 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001202 if (use_sandbox)
1203 ++sandbox;
1204 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 retval = eval_to_string(arg, nextcmd);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001206 if (use_sandbox)
1207 --sandbox;
1208 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 restore_funccal(save_funccalp);
1210 return retval;
1211}
1212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213/*
1214 * Top level evaluation function, returning a number.
1215 * Evaluates "expr" silently.
1216 * Returns -1 for an error.
1217 */
1218 int
1219eval_to_number(expr)
1220 char_u *expr;
1221{
Bram Moolenaar33570922005-01-25 22:26:29 +00001222 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001224 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
1226 ++emsg_off;
1227
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001228 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 retval = -1;
1230 else
1231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001232 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001233 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 }
1235 --emsg_off;
1236
1237 return retval;
1238}
1239
Bram Moolenaara40058a2005-07-11 22:42:07 +00001240/*
1241 * Prepare v: variable "idx" to be used.
1242 * Save the current typeval in "save_tv".
1243 * When not used yet add the variable to the v: hashtable.
1244 */
1245 static void
1246prepare_vimvar(idx, save_tv)
1247 int idx;
1248 typval_T *save_tv;
1249{
1250 *save_tv = vimvars[idx].vv_tv;
1251 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1252 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1253}
1254
1255/*
1256 * Restore v: variable "idx" to typeval "save_tv".
1257 * When no longer defined, remove the variable from the v: hashtable.
1258 */
1259 static void
1260restore_vimvar(idx, save_tv)
1261 int idx;
1262 typval_T *save_tv;
1263{
1264 hashitem_T *hi;
1265
1266 clear_tv(&vimvars[idx].vv_tv);
1267 vimvars[idx].vv_tv = *save_tv;
1268 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1269 {
1270 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1271 if (HASHITEM_EMPTY(hi))
1272 EMSG2(_(e_intern2), "restore_vimvar()");
1273 else
1274 hash_remove(&vimvarht, hi);
1275 }
1276}
1277
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001278#if defined(FEAT_SYN_HL) || defined(PROTO)
1279/*
1280 * Evaluate an expression to a list with suggestions.
1281 * For the "expr:" part of 'spellsuggest'.
1282 */
1283 list_T *
1284eval_spell_expr(badword, expr)
1285 char_u *badword;
1286 char_u *expr;
1287{
1288 typval_T save_val;
1289 typval_T rettv;
1290 list_T *list = NULL;
1291 char_u *p = skipwhite(expr);
1292
1293 /* Set "v:val" to the bad word. */
1294 prepare_vimvar(VV_VAL, &save_val);
1295 vimvars[VV_VAL].vv_type = VAR_STRING;
1296 vimvars[VV_VAL].vv_str = badword;
1297 if (p_verbose == 0)
1298 ++emsg_off;
1299
1300 if (eval1(&p, &rettv, TRUE) == OK)
1301 {
1302 if (rettv.v_type != VAR_LIST)
1303 clear_tv(&rettv);
1304 else
1305 list = rettv.vval.v_list;
1306 }
1307
1308 if (p_verbose == 0)
1309 --emsg_off;
1310 vimvars[VV_VAL].vv_str = NULL;
1311 restore_vimvar(VV_VAL, &save_val);
1312
1313 return list;
1314}
1315
1316/*
1317 * "list" is supposed to contain two items: a word and a number. Return the
1318 * word in "pp" and the number as the return value.
1319 * Return -1 if anything isn't right.
1320 * Used to get the good word and score from the eval_spell_expr() result.
1321 */
1322 int
1323get_spellword(list, pp)
1324 list_T *list;
1325 char_u **pp;
1326{
1327 listitem_T *li;
1328
1329 li = list->lv_first;
1330 if (li == NULL)
1331 return -1;
1332 *pp = get_tv_string(&li->li_tv);
1333
1334 li = li->li_next;
1335 if (li == NULL)
1336 return -1;
1337 return get_tv_number(&li->li_tv);
1338}
1339#endif
1340
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001341/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001342 * Top level evaluation function.
1343 * Returns an allocated typval_T with the result.
1344 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001345 */
1346 typval_T *
1347eval_expr(arg, nextcmd)
1348 char_u *arg;
1349 char_u **nextcmd;
1350{
1351 typval_T *tv;
1352
1353 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001354 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001355 {
1356 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001357 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001358 }
1359
1360 return tv;
1361}
1362
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1365/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001366 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001368 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001370 static int
1371call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 char_u *func;
1373 int argc;
1374 char_u **argv;
1375 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
Bram Moolenaar33570922005-01-25 22:26:29 +00001378 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 long n;
1380 int len;
1381 int i;
1382 int doesrange;
1383 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001384 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
Bram Moolenaar33570922005-01-25 22:26:29 +00001386 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001388 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389
1390 for (i = 0; i < argc; i++)
1391 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001392 /* Pass a NULL or empty argument as an empty string */
1393 if (argv[i] == NULL || *argv[i] == NUL)
1394 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001395 argvars[i].v_type = VAR_STRING;
1396 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001397 continue;
1398 }
1399
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 /* Recognize a number argument, the others must be strings. */
1401 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1402 if (len != 0 && len == (int)STRLEN(argv[i]))
1403 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001404 argvars[i].v_type = VAR_NUMBER;
1405 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 }
1407 else
1408 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001409 argvars[i].v_type = VAR_STRING;
1410 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 }
1412 }
1413
1414 if (safe)
1415 {
1416 save_funccalp = save_funccal();
1417 ++sandbox;
1418 }
1419
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001420 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1421 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001423 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (safe)
1425 {
1426 --sandbox;
1427 restore_funccal(save_funccalp);
1428 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001429 vim_free(argvars);
1430
1431 if (ret == FAIL)
1432 clear_tv(rettv);
1433
1434 return ret;
1435}
1436
1437/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001438 * Call vimL function "func" and return the result as a string.
1439 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001440 * Uses argv[argc] for the function arguments.
1441 */
1442 void *
1443call_func_retstr(func, argc, argv, safe)
1444 char_u *func;
1445 int argc;
1446 char_u **argv;
1447 int safe; /* use the sandbox */
1448{
1449 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001450 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001451
1452 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1453 return NULL;
1454
1455 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001456 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 return retval;
1458}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001459
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001460#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001461/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001462 * Call vimL function "func" and return the result as a number.
1463 * Returns -1 when calling the function fails.
1464 * Uses argv[argc] for the function arguments.
1465 */
1466 long
1467call_func_retnr(func, argc, argv, safe)
1468 char_u *func;
1469 int argc;
1470 char_u **argv;
1471 int safe; /* use the sandbox */
1472{
1473 typval_T rettv;
1474 long retval;
1475
1476 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1477 return -1;
1478
1479 retval = get_tv_number_chk(&rettv, NULL);
1480 clear_tv(&rettv);
1481 return retval;
1482}
1483#endif
1484
1485/*
1486 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001487 * Uses argv[argc] for the function arguments.
1488 */
1489 void *
1490call_func_retlist(func, argc, argv, safe)
1491 char_u *func;
1492 int argc;
1493 char_u **argv;
1494 int safe; /* use the sandbox */
1495{
1496 typval_T rettv;
1497
1498 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1499 return NULL;
1500
1501 if (rettv.v_type != VAR_LIST)
1502 {
1503 clear_tv(&rettv);
1504 return NULL;
1505 }
1506
1507 return rettv.vval.v_list;
1508}
1509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510#endif
1511
1512/*
1513 * Save the current function call pointer, and set it to NULL.
1514 * Used when executing autocommands and for ":source".
1515 */
1516 void *
1517save_funccal()
1518{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001519 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 current_funccal = NULL;
1522 return (void *)fc;
1523}
1524
1525 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001526restore_funccal(vfc)
1527 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001529 funccall_T *fc = (funccall_T *)vfc;
1530
1531 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532}
1533
Bram Moolenaar05159a02005-02-26 23:04:13 +00001534#if defined(FEAT_PROFILE) || defined(PROTO)
1535/*
1536 * Prepare profiling for entering a child or something else that is not
1537 * counted for the script/function itself.
1538 * Should always be called in pair with prof_child_exit().
1539 */
1540 void
1541prof_child_enter(tm)
1542 proftime_T *tm; /* place to store waittime */
1543{
1544 funccall_T *fc = current_funccal;
1545
1546 if (fc != NULL && fc->func->uf_profiling)
1547 profile_start(&fc->prof_child);
1548 script_prof_save(tm);
1549}
1550
1551/*
1552 * Take care of time spent in a child.
1553 * Should always be called after prof_child_enter().
1554 */
1555 void
1556prof_child_exit(tm)
1557 proftime_T *tm; /* where waittime was stored */
1558{
1559 funccall_T *fc = current_funccal;
1560
1561 if (fc != NULL && fc->func->uf_profiling)
1562 {
1563 profile_end(&fc->prof_child);
1564 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1565 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1566 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1567 }
1568 script_prof_restore(tm);
1569}
1570#endif
1571
1572
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573#ifdef FEAT_FOLDING
1574/*
1575 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1576 * it in "*cp". Doesn't give error messages.
1577 */
1578 int
1579eval_foldexpr(arg, cp)
1580 char_u *arg;
1581 int *cp;
1582{
Bram Moolenaar33570922005-01-25 22:26:29 +00001583 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 int retval;
1585 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001586 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1587 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
1589 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001590 if (use_sandbox)
1591 ++sandbox;
1592 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 retval = 0;
1596 else
1597 {
1598 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001599 if (tv.v_type == VAR_NUMBER)
1600 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001601 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 retval = 0;
1603 else
1604 {
1605 /* If the result is a string, check if there is a non-digit before
1606 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001607 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (!VIM_ISDIGIT(*s) && *s != '-')
1609 *cp = *s++;
1610 retval = atol((char *)s);
1611 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001612 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001615 if (use_sandbox)
1616 --sandbox;
1617 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
1619 return retval;
1620}
1621#endif
1622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001624 * ":let" list all variable values
1625 * ":let var1 var2" list variable values
1626 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001627 * ":let var += expr" assignment command.
1628 * ":let var -= expr" assignment command.
1629 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001630 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 */
1632 void
1633ex_let(eap)
1634 exarg_T *eap;
1635{
1636 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001637 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001638 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001640 int var_count = 0;
1641 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001642 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001644 expr = skip_var_list(arg, &var_count, &semicolon);
1645 if (expr == NULL)
1646 return;
1647 expr = vim_strchr(expr, '=');
1648 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001650 /*
1651 * ":let" without "=": list variables
1652 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653 if (*arg == '[')
1654 EMSG(_(e_invarg));
1655 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656 /* ":let var1 var2" */
1657 arg = list_arg_vars(eap, arg);
1658 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001661 list_glob_vars();
1662 list_buf_vars();
1663 list_win_vars();
1664 list_vim_vars();
1665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 eap->nextcmd = check_nextcmd(arg);
1667 }
1668 else
1669 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001670 op[0] = '=';
1671 op[1] = NUL;
1672 if (expr > arg)
1673 {
1674 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1675 op[0] = expr[-1]; /* +=, -= or .= */
1676 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001678
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (eap->skip)
1680 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 if (eap->skip)
1683 {
1684 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001685 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 --emsg_skip;
1687 }
1688 else if (i != FAIL)
1689 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001691 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001692 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 }
1694 }
1695}
1696
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697/*
1698 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1699 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001700 * When "nextchars" is not NULL it points to a string with characters that
1701 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1702 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703 * Returns OK or FAIL;
1704 */
1705 static int
1706ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1707 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001708 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709 int copy; /* copy values from "tv", don't move */
1710 int semicolon; /* from skip_var_list() */
1711 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713{
1714 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001715 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001717 listitem_T *item;
1718 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719
1720 if (*arg != '[')
1721 {
1722 /*
1723 * ":let var = expr" or ":for var in list"
1724 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001725 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001726 return FAIL;
1727 return OK;
1728 }
1729
1730 /*
1731 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1732 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001733 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001734 {
1735 EMSG(_(e_listreq));
1736 return FAIL;
1737 }
1738
1739 i = list_len(l);
1740 if (semicolon == 0 && var_count < i)
1741 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001742 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001743 return FAIL;
1744 }
1745 if (var_count - semicolon > i)
1746 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001747 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 return FAIL;
1749 }
1750
1751 item = l->lv_first;
1752 while (*arg != ']')
1753 {
1754 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001755 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756 item = item->li_next;
1757 if (arg == NULL)
1758 return FAIL;
1759
1760 arg = skipwhite(arg);
1761 if (*arg == ';')
1762 {
1763 /* Put the rest of the list (may be empty) in the var after ';'.
1764 * Create a new list for this. */
1765 l = list_alloc();
1766 if (l == NULL)
1767 return FAIL;
1768 while (item != NULL)
1769 {
1770 list_append_tv(l, &item->li_tv);
1771 item = item->li_next;
1772 }
1773
1774 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001775 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776 ltv.vval.v_list = l;
1777 l->lv_refcount = 1;
1778
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001779 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1780 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781 clear_tv(&ltv);
1782 if (arg == NULL)
1783 return FAIL;
1784 break;
1785 }
1786 else if (*arg != ',' && *arg != ']')
1787 {
1788 EMSG2(_(e_intern2), "ex_let_vars()");
1789 return FAIL;
1790 }
1791 }
1792
1793 return OK;
1794}
1795
1796/*
1797 * Skip over assignable variable "var" or list of variables "[var, var]".
1798 * Used for ":let varvar = expr" and ":for varvar in expr".
1799 * For "[var, var]" increment "*var_count" for each variable.
1800 * for "[var, var; var]" set "semicolon".
1801 * Return NULL for an error.
1802 */
1803 static char_u *
1804skip_var_list(arg, var_count, semicolon)
1805 char_u *arg;
1806 int *var_count;
1807 int *semicolon;
1808{
1809 char_u *p, *s;
1810
1811 if (*arg == '[')
1812 {
1813 /* "[var, var]": find the matching ']'. */
1814 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001815 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 {
1817 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1818 s = skip_var_one(p);
1819 if (s == p)
1820 {
1821 EMSG2(_(e_invarg2), p);
1822 return NULL;
1823 }
1824 ++*var_count;
1825
1826 p = skipwhite(s);
1827 if (*p == ']')
1828 break;
1829 else if (*p == ';')
1830 {
1831 if (*semicolon == 1)
1832 {
1833 EMSG(_("Double ; in list of variables"));
1834 return NULL;
1835 }
1836 *semicolon = 1;
1837 }
1838 else if (*p != ',')
1839 {
1840 EMSG2(_(e_invarg2), p);
1841 return NULL;
1842 }
1843 }
1844 return p + 1;
1845 }
1846 else
1847 return skip_var_one(arg);
1848}
1849
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001850/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001851 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1852 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001853 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001854 static char_u *
1855skip_var_one(arg)
1856 char_u *arg;
1857{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001858 if (*arg == '@' && arg[1] != NUL)
1859 return arg + 2;
1860 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1861 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001862}
1863
Bram Moolenaara7043832005-01-21 11:56:39 +00001864/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001865 * List variables for hashtab "ht" with prefix "prefix".
1866 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001867 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001869list_hashtable_vars(ht, prefix, empty)
1870 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001871 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001872 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001873{
Bram Moolenaar33570922005-01-25 22:26:29 +00001874 hashitem_T *hi;
1875 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001876 int todo;
1877
1878 todo = ht->ht_used;
1879 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1880 {
1881 if (!HASHITEM_EMPTY(hi))
1882 {
1883 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001884 di = HI2DI(hi);
1885 if (empty || di->di_tv.v_type != VAR_STRING
1886 || di->di_tv.vval.v_string != NULL)
1887 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001888 }
1889 }
1890}
1891
1892/*
1893 * List global variables.
1894 */
1895 static void
1896list_glob_vars()
1897{
Bram Moolenaar33570922005-01-25 22:26:29 +00001898 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001899}
1900
1901/*
1902 * List buffer variables.
1903 */
1904 static void
1905list_buf_vars()
1906{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001907 char_u numbuf[NUMBUFLEN];
1908
Bram Moolenaar33570922005-01-25 22:26:29 +00001909 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001910
1911 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1912 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001913}
1914
1915/*
1916 * List window variables.
1917 */
1918 static void
1919list_win_vars()
1920{
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001922}
1923
1924/*
1925 * List Vim variables.
1926 */
1927 static void
1928list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929{
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931}
1932
1933/*
1934 * List variables in "arg".
1935 */
1936 static char_u *
1937list_arg_vars(eap, arg)
1938 exarg_T *eap;
1939 char_u *arg;
1940{
1941 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001942 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001944 char_u *name_start;
1945 char_u *arg_subsc;
1946 char_u *tofree;
1947 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948
1949 while (!ends_excmd(*arg) && !got_int)
1950 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001951 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001953 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001954 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1955 {
1956 emsg_severe = TRUE;
1957 EMSG(_(e_trailing));
1958 break;
1959 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001961 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001963 /* get_name_len() takes care of expanding curly braces */
1964 name_start = name = arg;
1965 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1966 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001967 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001968 /* This is mainly to keep test 49 working: when expanding
1969 * curly braces fails overrule the exception error message. */
1970 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001972 emsg_severe = TRUE;
1973 EMSG2(_(e_invarg2), arg);
1974 break;
1975 }
1976 error = TRUE;
1977 }
1978 else
1979 {
1980 if (tofree != NULL)
1981 name = tofree;
1982 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001984 else
1985 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001986 /* handle d.key, l[idx], f(expr) */
1987 arg_subsc = arg;
1988 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001989 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001991 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001992 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001993 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001994 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001995 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001996 case 'g': list_glob_vars(); break;
1997 case 'b': list_buf_vars(); break;
1998 case 'w': list_win_vars(); break;
1999 case 'v': list_vim_vars(); break;
2000 default:
2001 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002002 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002003 }
2004 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002005 {
2006 char_u numbuf[NUMBUFLEN];
2007 char_u *tf;
2008 int c;
2009 char_u *s;
2010
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002011 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002012 c = *arg;
2013 *arg = NUL;
2014 list_one_var_a((char_u *)"",
2015 arg == arg_subsc ? name : name_start,
2016 tv.v_type, s == NULL ? (char_u *)"" : s);
2017 *arg = c;
2018 vim_free(tf);
2019 }
2020 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002021 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002022 }
2023 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002024
2025 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002027
2028 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002029 }
2030
2031 return arg;
2032}
2033
2034/*
2035 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2036 * Returns a pointer to the char just after the var name.
2037 * Returns NULL if there is an error.
2038 */
2039 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002040ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002041 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002042 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002043 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002045 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046{
2047 int c1;
2048 char_u *name;
2049 char_u *p;
2050 char_u *arg_end = NULL;
2051 int len;
2052 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002053 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002054
2055 /*
2056 * ":let $VAR = expr": Set environment variable.
2057 */
2058 if (*arg == '$')
2059 {
2060 /* Find the end of the name. */
2061 ++arg;
2062 name = arg;
2063 len = get_env_len(&arg);
2064 if (len == 0)
2065 EMSG2(_(e_invarg2), name - 1);
2066 else
2067 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002068 if (op != NULL && (*op == '+' || *op == '-'))
2069 EMSG2(_(e_letwrong), op);
2070 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002071 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 EMSG(_(e_letunexp));
2073 else
2074 {
2075 c1 = name[len];
2076 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002077 p = get_tv_string_chk(tv);
2078 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002079 {
2080 int mustfree = FALSE;
2081 char_u *s = vim_getenv(name, &mustfree);
2082
2083 if (s != NULL)
2084 {
2085 p = tofree = concat_str(s, p);
2086 if (mustfree)
2087 vim_free(s);
2088 }
2089 }
2090 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002091 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002092 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002093 if (STRICMP(name, "HOME") == 0)
2094 init_homedir();
2095 else if (didset_vim && STRICMP(name, "VIM") == 0)
2096 didset_vim = FALSE;
2097 else if (didset_vimruntime
2098 && STRICMP(name, "VIMRUNTIME") == 0)
2099 didset_vimruntime = FALSE;
2100 arg_end = arg;
2101 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002102 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002103 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002104 }
2105 }
2106 }
2107
2108 /*
2109 * ":let &option = expr": Set option value.
2110 * ":let &l:option = expr": Set local option value.
2111 * ":let &g:option = expr": Set global option value.
2112 */
2113 else if (*arg == '&')
2114 {
2115 /* Find the end of the name. */
2116 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002117 if (p == NULL || (endchars != NULL
2118 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002119 EMSG(_(e_letunexp));
2120 else
2121 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002122 long n;
2123 int opt_type;
2124 long numval;
2125 char_u *stringval = NULL;
2126 char_u *s;
2127
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002128 c1 = *p;
2129 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002130
2131 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002132 s = get_tv_string_chk(tv); /* != NULL if number or string */
2133 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002134 {
2135 opt_type = get_option_value(arg, &numval,
2136 &stringval, opt_flags);
2137 if ((opt_type == 1 && *op == '.')
2138 || (opt_type == 0 && *op != '.'))
2139 EMSG2(_(e_letwrong), op);
2140 else
2141 {
2142 if (opt_type == 1) /* number */
2143 {
2144 if (*op == '+')
2145 n = numval + n;
2146 else
2147 n = numval - n;
2148 }
2149 else if (opt_type == 0 && stringval != NULL) /* string */
2150 {
2151 s = concat_str(stringval, s);
2152 vim_free(stringval);
2153 stringval = s;
2154 }
2155 }
2156 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002157 if (s != NULL)
2158 {
2159 set_option_value(arg, n, s, opt_flags);
2160 arg_end = p;
2161 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164 }
2165 }
2166
2167 /*
2168 * ":let @r = expr": Set register contents.
2169 */
2170 else if (*arg == '@')
2171 {
2172 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002173 if (op != NULL && (*op == '+' || *op == '-'))
2174 EMSG2(_(e_letwrong), op);
2175 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002176 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 EMSG(_(e_letunexp));
2178 else
2179 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002180 char_u *tofree = NULL;
2181 char_u *s;
2182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002183 p = get_tv_string_chk(tv);
2184 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002185 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002186 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002187 if (s != NULL)
2188 {
2189 p = tofree = concat_str(s, p);
2190 vim_free(s);
2191 }
2192 }
2193 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002194 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002195 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002196 arg_end = arg + 1;
2197 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002198 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 }
2200 }
2201
2202 /*
2203 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002204 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002206 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002208 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002210 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002213 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2214 EMSG(_(e_letunexp));
2215 else
2216 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002217 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 arg_end = p;
2219 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 }
2223
2224 else
2225 EMSG2(_(e_invarg2), arg);
2226
2227 return arg_end;
2228}
2229
2230/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002231 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2232 */
2233 static int
2234check_changedtick(arg)
2235 char_u *arg;
2236{
2237 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2238 {
2239 EMSG2(_(e_readonlyvar), arg);
2240 return TRUE;
2241 }
2242 return FALSE;
2243}
2244
2245/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246 * Get an lval: variable, Dict item or List item that can be assigned a value
2247 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2248 * "name.key", "name.key[expr]" etc.
2249 * Indexing only works if "name" is an existing List or Dictionary.
2250 * "name" points to the start of the name.
2251 * If "rettv" is not NULL it points to the value to be assigned.
2252 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2253 * wrong; must end in space or cmd separator.
2254 *
2255 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002256 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 */
2259 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002260get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002262 typval_T *rettv;
2263 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002264 int unlet;
2265 int skip;
2266 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002267 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270 char_u *expr_start, *expr_end;
2271 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002272 dictitem_T *v;
2273 typval_T var1;
2274 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002275 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002276 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002277 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002278 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002279 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002282 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002283
2284 if (skip)
2285 {
2286 /* When skipping just find the end of the name. */
2287 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002288 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 }
2290
2291 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002292 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002293 if (expr_start != NULL)
2294 {
2295 /* Don't expand the name when we already know there is an error. */
2296 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2297 && *p != '[' && *p != '.')
2298 {
2299 EMSG(_(e_trailing));
2300 return NULL;
2301 }
2302
2303 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2304 if (lp->ll_exp_name == NULL)
2305 {
2306 /* Report an invalid expression in braces, unless the
2307 * expression evaluation has been cancelled due to an
2308 * aborting error, an interrupt, or an exception. */
2309 if (!aborting() && !quiet)
2310 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002311 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 EMSG2(_(e_invarg2), name);
2313 return NULL;
2314 }
2315 }
2316 lp->ll_name = lp->ll_exp_name;
2317 }
2318 else
2319 lp->ll_name = name;
2320
2321 /* Without [idx] or .key we are done. */
2322 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2323 return p;
2324
2325 cc = *p;
2326 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002327 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002328 if (v == NULL && !quiet)
2329 EMSG2(_(e_undefvar), lp->ll_name);
2330 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 if (v == NULL)
2332 return NULL;
2333
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002334 /*
2335 * Loop until no more [idx] or .key is following.
2336 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002337 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2341 && !(lp->ll_tv->v_type == VAR_DICT
2342 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002344 if (!quiet)
2345 EMSG(_("E689: Can only index a List or Dictionary"));
2346 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002348 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002350 if (!quiet)
2351 EMSG(_("E708: [:] must come last"));
2352 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002354
Bram Moolenaar8c711452005-01-14 21:53:12 +00002355 len = -1;
2356 if (*p == '.')
2357 {
2358 key = p + 1;
2359 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2360 ;
2361 if (len == 0)
2362 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002363 if (!quiet)
2364 EMSG(_(e_emptykey));
2365 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002366 }
2367 p = key + len;
2368 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002369 else
2370 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002371 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002372 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002373 if (*p == ':')
2374 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002375 else
2376 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002377 empty1 = FALSE;
2378 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002379 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002380 if (get_tv_string_chk(&var1) == NULL)
2381 {
2382 /* not a number or string */
2383 clear_tv(&var1);
2384 return NULL;
2385 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002386 }
2387
2388 /* Optionally get the second index [ :expr]. */
2389 if (*p == ':')
2390 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002392 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002395 if (!empty1)
2396 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002398 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 if (rettv != NULL && (rettv->v_type != VAR_LIST
2400 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002401 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002402 if (!quiet)
2403 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002404 if (!empty1)
2405 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 }
2408 p = skipwhite(p + 1);
2409 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002411 else
2412 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2415 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 if (!empty1)
2417 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002420 if (get_tv_string_chk(&var2) == NULL)
2421 {
2422 /* not a number or string */
2423 if (!empty1)
2424 clear_tv(&var1);
2425 clear_tv(&var2);
2426 return NULL;
2427 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002428 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002430 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002433
Bram Moolenaar8c711452005-01-14 21:53:12 +00002434 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002435 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 if (!quiet)
2437 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 if (!empty1)
2439 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002441 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 }
2444
2445 /* Skip to past ']'. */
2446 ++p;
2447 }
2448
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 {
2451 if (len == -1)
2452 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002454 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 if (*key == NUL)
2456 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 if (!quiet)
2458 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002459 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 }
2462 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 lp->ll_list = NULL;
2464 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002465 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002467 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002468 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002470 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 if (len == -1)
2474 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002476 }
2477 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002479 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 if (len == -1)
2482 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 p = NULL;
2485 break;
2486 }
2487 if (len == -1)
2488 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 }
2491 else
2492 {
2493 /*
2494 * Get the number and item for the only or first index of the List.
2495 */
2496 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 else
2499 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002500 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 clear_tv(&var1);
2502 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002503 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 lp->ll_list = lp->ll_tv->vval.v_list;
2505 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2506 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002507 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 if (!quiet)
2509 EMSGN(_(e_listidx), lp->ll_n1);
2510 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513 }
2514
2515 /*
2516 * May need to find the item or absolute index for the second
2517 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 * When no index given: "lp->ll_empty2" is TRUE.
2519 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002523 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 if (ni == NULL)
2529 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 if (!quiet)
2531 EMSGN(_(e_listidx), lp->ll_n2);
2532 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 }
2536
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2538 if (lp->ll_n1 < 0)
2539 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2540 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002541 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 if (!quiet)
2543 EMSGN(_(e_listidx), lp->ll_n2);
2544 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002545 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002546 }
2547
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 }
2551
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 return p;
2553}
2554
2555/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 */
2558 static void
2559clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002560 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561{
2562 vim_free(lp->ll_exp_name);
2563 vim_free(lp->ll_newkey);
2564}
2565
2566/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002567 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002569 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 */
2571 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002572set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002573 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002575 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002577 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578{
2579 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 listitem_T *ri;
2581 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582
2583 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 cc = *endp;
2588 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002589 if (op != NULL && *op != '=')
2590 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002592
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002593 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002594 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2595 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002596 {
2597 if (tv_op(&tv, rettv, op) == OK)
2598 set_var(lp->ll_name, &tv, FALSE);
2599 clear_tv(&tv);
2600 }
2601 }
2602 else
2603 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002607 else if (tv_check_lock(lp->ll_newkey == NULL
2608 ? lp->ll_tv->v_lock
2609 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2610 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 else if (lp->ll_range)
2612 {
2613 /*
2614 * Assign the List values to the list items.
2615 */
2616 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002617 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002618 if (op != NULL && *op != '=')
2619 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2620 else
2621 {
2622 clear_tv(&lp->ll_li->li_tv);
2623 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2624 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 ri = ri->li_next;
2626 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2627 break;
2628 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002631 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 ri = NULL;
2634 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002635 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002636 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 lp->ll_li = lp->ll_li->li_next;
2638 ++lp->ll_n1;
2639 }
2640 if (ri != NULL)
2641 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002642 else if (lp->ll_empty2
2643 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 : lp->ll_n1 != lp->ll_n2)
2645 EMSG(_("E711: List value has not enough items"));
2646 }
2647 else
2648 {
2649 /*
2650 * Assign to a List or Dictionary item.
2651 */
2652 if (lp->ll_newkey != NULL)
2653 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002654 if (op != NULL && *op != '=')
2655 {
2656 EMSG2(_(e_letwrong), op);
2657 return;
2658 }
2659
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002661 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (di == NULL)
2663 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002664 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2665 {
2666 vim_free(di);
2667 return;
2668 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002670 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002671 else if (op != NULL && *op != '=')
2672 {
2673 tv_op(lp->ll_tv, rettv, op);
2674 return;
2675 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002676 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 /*
2680 * Assign the value to the variable or list item.
2681 */
2682 if (copy)
2683 copy_tv(rettv, lp->ll_tv);
2684 else
2685 {
2686 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002687 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002689 }
2690 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002691}
2692
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002693/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002694 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2695 * Returns OK or FAIL.
2696 */
2697 static int
2698tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002699 typval_T *tv1;
2700 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002701 char_u *op;
2702{
2703 long n;
2704 char_u numbuf[NUMBUFLEN];
2705 char_u *s;
2706
2707 /* Can't do anything with a Funcref or a Dict on the right. */
2708 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2709 {
2710 switch (tv1->v_type)
2711 {
2712 case VAR_DICT:
2713 case VAR_FUNC:
2714 break;
2715
2716 case VAR_LIST:
2717 if (*op != '+' || tv2->v_type != VAR_LIST)
2718 break;
2719 /* List += List */
2720 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2721 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2722 return OK;
2723
2724 case VAR_NUMBER:
2725 case VAR_STRING:
2726 if (tv2->v_type == VAR_LIST)
2727 break;
2728 if (*op == '+' || *op == '-')
2729 {
2730 /* nr += nr or nr -= nr*/
2731 n = get_tv_number(tv1);
2732 if (*op == '+')
2733 n += get_tv_number(tv2);
2734 else
2735 n -= get_tv_number(tv2);
2736 clear_tv(tv1);
2737 tv1->v_type = VAR_NUMBER;
2738 tv1->vval.v_number = n;
2739 }
2740 else
2741 {
2742 /* str .= str */
2743 s = get_tv_string(tv1);
2744 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2745 clear_tv(tv1);
2746 tv1->v_type = VAR_STRING;
2747 tv1->vval.v_string = s;
2748 }
2749 return OK;
2750 }
2751 }
2752
2753 EMSG2(_(e_letwrong), op);
2754 return FAIL;
2755}
2756
2757/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002758 * Add a watcher to a list.
2759 */
2760 static void
2761list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002762 list_T *l;
2763 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002764{
2765 lw->lw_next = l->lv_watch;
2766 l->lv_watch = lw;
2767}
2768
2769/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002770 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002771 * No warning when it isn't found...
2772 */
2773 static void
2774list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002775 list_T *l;
2776 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002777{
Bram Moolenaar33570922005-01-25 22:26:29 +00002778 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002779
2780 lwp = &l->lv_watch;
2781 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2782 {
2783 if (lw == lwrem)
2784 {
2785 *lwp = lw->lw_next;
2786 break;
2787 }
2788 lwp = &lw->lw_next;
2789 }
2790}
2791
2792/*
2793 * Just before removing an item from a list: advance watchers to the next
2794 * item.
2795 */
2796 static void
2797list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002798 list_T *l;
2799 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002800{
Bram Moolenaar33570922005-01-25 22:26:29 +00002801 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002802
2803 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2804 if (lw->lw_item == item)
2805 lw->lw_item = item->li_next;
2806}
2807
2808/*
2809 * Evaluate the expression used in a ":for var in expr" command.
2810 * "arg" points to "var".
2811 * Set "*errp" to TRUE for an error, FALSE otherwise;
2812 * Return a pointer that holds the info. Null when there is an error.
2813 */
2814 void *
2815eval_for_line(arg, errp, nextcmdp, skip)
2816 char_u *arg;
2817 int *errp;
2818 char_u **nextcmdp;
2819 int skip;
2820{
Bram Moolenaar33570922005-01-25 22:26:29 +00002821 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002822 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002823 typval_T tv;
2824 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002825
2826 *errp = TRUE; /* default: there is an error */
2827
Bram Moolenaar33570922005-01-25 22:26:29 +00002828 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002829 if (fi == NULL)
2830 return NULL;
2831
2832 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2833 if (expr == NULL)
2834 return fi;
2835
2836 expr = skipwhite(expr);
2837 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2838 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002839 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002840 return fi;
2841 }
2842
2843 if (skip)
2844 ++emsg_skip;
2845 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2846 {
2847 *errp = FALSE;
2848 if (!skip)
2849 {
2850 l = tv.vval.v_list;
2851 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002852 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002853 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002854 clear_tv(&tv);
2855 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002856 else
2857 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002858 /* No need to increment the refcount, it's already set for the
2859 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860 fi->fi_list = l;
2861 list_add_watch(l, &fi->fi_lw);
2862 fi->fi_lw.lw_item = l->lv_first;
2863 }
2864 }
2865 }
2866 if (skip)
2867 --emsg_skip;
2868
2869 return fi;
2870}
2871
2872/*
2873 * Use the first item in a ":for" list. Advance to the next.
2874 * Assign the values to the variable (list). "arg" points to the first one.
2875 * Return TRUE when a valid item was found, FALSE when at end of list or
2876 * something wrong.
2877 */
2878 int
2879next_for_item(fi_void, arg)
2880 void *fi_void;
2881 char_u *arg;
2882{
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002884 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002886
2887 item = fi->fi_lw.lw_item;
2888 if (item == NULL)
2889 result = FALSE;
2890 else
2891 {
2892 fi->fi_lw.lw_item = item->li_next;
2893 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2894 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2895 }
2896 return result;
2897}
2898
2899/*
2900 * Free the structure used to store info used by ":for".
2901 */
2902 void
2903free_for_info(fi_void)
2904 void *fi_void;
2905{
Bram Moolenaar33570922005-01-25 22:26:29 +00002906 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002907
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002908 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002909 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002910 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002911 list_unref(fi->fi_list);
2912 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002913 vim_free(fi);
2914}
2915
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2917
2918 void
2919set_context_for_expression(xp, arg, cmdidx)
2920 expand_T *xp;
2921 char_u *arg;
2922 cmdidx_T cmdidx;
2923{
2924 int got_eq = FALSE;
2925 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002926 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002928 if (cmdidx == CMD_let)
2929 {
2930 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002931 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002932 {
2933 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002934 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002935 {
2936 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002937 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002938 if (vim_iswhite(*p))
2939 break;
2940 }
2941 return;
2942 }
2943 }
2944 else
2945 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2946 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 while ((xp->xp_pattern = vim_strpbrk(arg,
2948 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2949 {
2950 c = *xp->xp_pattern;
2951 if (c == '&')
2952 {
2953 c = xp->xp_pattern[1];
2954 if (c == '&')
2955 {
2956 ++xp->xp_pattern;
2957 xp->xp_context = cmdidx != CMD_let || got_eq
2958 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2959 }
2960 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002961 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002963 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2964 xp->xp_pattern += 2;
2965
2966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 }
2968 else if (c == '$')
2969 {
2970 /* environment variable */
2971 xp->xp_context = EXPAND_ENV_VARS;
2972 }
2973 else if (c == '=')
2974 {
2975 got_eq = TRUE;
2976 xp->xp_context = EXPAND_EXPRESSION;
2977 }
2978 else if (c == '<'
2979 && xp->xp_context == EXPAND_FUNCTIONS
2980 && vim_strchr(xp->xp_pattern, '(') == NULL)
2981 {
2982 /* Function name can start with "<SNR>" */
2983 break;
2984 }
2985 else if (cmdidx != CMD_let || got_eq)
2986 {
2987 if (c == '"') /* string */
2988 {
2989 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2990 if (c == '\\' && xp->xp_pattern[1] != NUL)
2991 ++xp->xp_pattern;
2992 xp->xp_context = EXPAND_NOTHING;
2993 }
2994 else if (c == '\'') /* literal string */
2995 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002996 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2998 /* skip */ ;
2999 xp->xp_context = EXPAND_NOTHING;
3000 }
3001 else if (c == '|')
3002 {
3003 if (xp->xp_pattern[1] == '|')
3004 {
3005 ++xp->xp_pattern;
3006 xp->xp_context = EXPAND_EXPRESSION;
3007 }
3008 else
3009 xp->xp_context = EXPAND_COMMANDS;
3010 }
3011 else
3012 xp->xp_context = EXPAND_EXPRESSION;
3013 }
3014 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003015 /* Doesn't look like something valid, expand as an expression
3016 * anyway. */
3017 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 arg = xp->xp_pattern;
3019 if (*arg != NUL)
3020 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3021 /* skip */ ;
3022 }
3023 xp->xp_pattern = arg;
3024}
3025
3026#endif /* FEAT_CMDL_COMPL */
3027
3028/*
3029 * ":1,25call func(arg1, arg2)" function call.
3030 */
3031 void
3032ex_call(eap)
3033 exarg_T *eap;
3034{
3035 char_u *arg = eap->arg;
3036 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003038 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003040 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 linenr_T lnum;
3042 int doesrange;
3043 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003044 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003046 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3047 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003048 if (tofree == NULL)
3049 return;
3050
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003051 /* Increase refcount on dictionary, it could get deleted when evaluating
3052 * the arguments. */
3053 if (fudi.fd_dict != NULL)
3054 ++fudi.fd_dict->dv_refcount;
3055
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003056 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3057 len = STRLEN(tofree);
3058 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
Bram Moolenaar532c7802005-01-27 14:44:31 +00003060 /* Skip white space to allow ":call func ()". Not good, but required for
3061 * backward compatibility. */
3062 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003063 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
3065 if (*startarg != '(')
3066 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003067 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 goto end;
3069 }
3070
3071 /*
3072 * When skipping, evaluate the function once, to find the end of the
3073 * arguments.
3074 * When the function takes a range, this is discovered after the first
3075 * call, and the loop is broken.
3076 */
3077 if (eap->skip)
3078 {
3079 ++emsg_skip;
3080 lnum = eap->line2; /* do it once, also with an invalid range */
3081 }
3082 else
3083 lnum = eap->line1;
3084 for ( ; lnum <= eap->line2; ++lnum)
3085 {
3086 if (!eap->skip && eap->addr_count > 0)
3087 {
3088 curwin->w_cursor.lnum = lnum;
3089 curwin->w_cursor.col = 0;
3090 }
3091 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003092 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003093 eap->line1, eap->line2, &doesrange,
3094 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 {
3096 failed = TRUE;
3097 break;
3098 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003099 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (doesrange || eap->skip)
3101 break;
3102 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003103 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003104 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003105 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 if (aborting())
3107 break;
3108 }
3109 if (eap->skip)
3110 --emsg_skip;
3111
3112 if (!failed)
3113 {
3114 /* Check for trailing illegal characters and a following command. */
3115 if (!ends_excmd(*arg))
3116 {
3117 emsg_severe = TRUE;
3118 EMSG(_(e_trailing));
3119 }
3120 else
3121 eap->nextcmd = check_nextcmd(arg);
3122 }
3123
3124end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003125 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127}
3128
3129/*
3130 * ":unlet[!] var1 ... " command.
3131 */
3132 void
3133ex_unlet(eap)
3134 exarg_T *eap;
3135{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003136 ex_unletlock(eap, eap->arg, 0);
3137}
3138
3139/*
3140 * ":lockvar" and ":unlockvar" commands
3141 */
3142 void
3143ex_lockvar(eap)
3144 exarg_T *eap;
3145{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003147 int deep = 2;
3148
3149 if (eap->forceit)
3150 deep = -1;
3151 else if (vim_isdigit(*arg))
3152 {
3153 deep = getdigits(&arg);
3154 arg = skipwhite(arg);
3155 }
3156
3157 ex_unletlock(eap, arg, deep);
3158}
3159
3160/*
3161 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3162 */
3163 static void
3164ex_unletlock(eap, argstart, deep)
3165 exarg_T *eap;
3166 char_u *argstart;
3167 int deep;
3168{
3169 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003172 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
3174 do
3175 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003176 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003177 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3178 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003179 if (lv.ll_name == NULL)
3180 error = TRUE; /* error but continue parsing */
3181 if (name_end == NULL || (!vim_iswhite(*name_end)
3182 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003184 if (name_end != NULL)
3185 {
3186 emsg_severe = TRUE;
3187 EMSG(_(e_trailing));
3188 }
3189 if (!(eap->skip || error))
3190 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 break;
3192 }
3193
3194 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003195 {
3196 if (eap->cmdidx == CMD_unlet)
3197 {
3198 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3199 error = TRUE;
3200 }
3201 else
3202 {
3203 if (do_lock_var(&lv, name_end, deep,
3204 eap->cmdidx == CMD_lockvar) == FAIL)
3205 error = TRUE;
3206 }
3207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003209 if (!eap->skip)
3210 clear_lval(&lv);
3211
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 arg = skipwhite(name_end);
3213 } while (!ends_excmd(*arg));
3214
3215 eap->nextcmd = check_nextcmd(arg);
3216}
3217
Bram Moolenaar8c711452005-01-14 21:53:12 +00003218 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003219do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003221 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003222 int forceit;
3223{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003224 int ret = OK;
3225 int cc;
3226
3227 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003228 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003229 cc = *name_end;
3230 *name_end = NUL;
3231
3232 /* Normal name or expanded name. */
3233 if (check_changedtick(lp->ll_name))
3234 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003235 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003236 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003237 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003238 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003239 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3240 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003241 else if (lp->ll_range)
3242 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003243 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003244
3245 /* Delete a range of List items. */
3246 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3247 {
3248 li = lp->ll_li->li_next;
3249 listitem_remove(lp->ll_list, lp->ll_li);
3250 lp->ll_li = li;
3251 ++lp->ll_n1;
3252 }
3253 }
3254 else
3255 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003256 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003257 /* unlet a List item. */
3258 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003259 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003260 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003261 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003262 }
3263
3264 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003265}
3266
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267/*
3268 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003269 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 */
3271 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003272do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003274 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 hashtab_T *ht;
3277 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003278 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
Bram Moolenaar33570922005-01-25 22:26:29 +00003280 ht = find_var_ht(name, &varname);
3281 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003283 hi = hash_find(ht, varname);
3284 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003285 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003286 if (var_check_ro(HI2DI(hi)->di_flags, name))
3287 return FAIL;
3288 delete_var(ht, hi);
3289 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003292 if (forceit)
3293 return OK;
3294 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 return FAIL;
3296}
3297
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003298/*
3299 * Lock or unlock variable indicated by "lp".
3300 * "deep" is the levels to go (-1 for unlimited);
3301 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3302 */
3303 static int
3304do_lock_var(lp, name_end, deep, lock)
3305 lval_T *lp;
3306 char_u *name_end;
3307 int deep;
3308 int lock;
3309{
3310 int ret = OK;
3311 int cc;
3312 dictitem_T *di;
3313
3314 if (deep == 0) /* nothing to do */
3315 return OK;
3316
3317 if (lp->ll_tv == NULL)
3318 {
3319 cc = *name_end;
3320 *name_end = NUL;
3321
3322 /* Normal name or expanded name. */
3323 if (check_changedtick(lp->ll_name))
3324 ret = FAIL;
3325 else
3326 {
3327 di = find_var(lp->ll_name, NULL);
3328 if (di == NULL)
3329 ret = FAIL;
3330 else
3331 {
3332 if (lock)
3333 di->di_flags |= DI_FLAGS_LOCK;
3334 else
3335 di->di_flags &= ~DI_FLAGS_LOCK;
3336 item_lock(&di->di_tv, deep, lock);
3337 }
3338 }
3339 *name_end = cc;
3340 }
3341 else if (lp->ll_range)
3342 {
3343 listitem_T *li = lp->ll_li;
3344
3345 /* (un)lock a range of List items. */
3346 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3347 {
3348 item_lock(&li->li_tv, deep, lock);
3349 li = li->li_next;
3350 ++lp->ll_n1;
3351 }
3352 }
3353 else if (lp->ll_list != NULL)
3354 /* (un)lock a List item. */
3355 item_lock(&lp->ll_li->li_tv, deep, lock);
3356 else
3357 /* un(lock) a Dictionary item. */
3358 item_lock(&lp->ll_di->di_tv, deep, lock);
3359
3360 return ret;
3361}
3362
3363/*
3364 * Lock or unlock an item. "deep" is nr of levels to go.
3365 */
3366 static void
3367item_lock(tv, deep, lock)
3368 typval_T *tv;
3369 int deep;
3370 int lock;
3371{
3372 static int recurse = 0;
3373 list_T *l;
3374 listitem_T *li;
3375 dict_T *d;
3376 hashitem_T *hi;
3377 int todo;
3378
3379 if (recurse >= DICT_MAXNEST)
3380 {
3381 EMSG(_("E743: variable nested too deep for (un)lock"));
3382 return;
3383 }
3384 if (deep == 0)
3385 return;
3386 ++recurse;
3387
3388 /* lock/unlock the item itself */
3389 if (lock)
3390 tv->v_lock |= VAR_LOCKED;
3391 else
3392 tv->v_lock &= ~VAR_LOCKED;
3393
3394 switch (tv->v_type)
3395 {
3396 case VAR_LIST:
3397 if ((l = tv->vval.v_list) != NULL)
3398 {
3399 if (lock)
3400 l->lv_lock |= VAR_LOCKED;
3401 else
3402 l->lv_lock &= ~VAR_LOCKED;
3403 if (deep < 0 || deep > 1)
3404 /* recursive: lock/unlock the items the List contains */
3405 for (li = l->lv_first; li != NULL; li = li->li_next)
3406 item_lock(&li->li_tv, deep - 1, lock);
3407 }
3408 break;
3409 case VAR_DICT:
3410 if ((d = tv->vval.v_dict) != NULL)
3411 {
3412 if (lock)
3413 d->dv_lock |= VAR_LOCKED;
3414 else
3415 d->dv_lock &= ~VAR_LOCKED;
3416 if (deep < 0 || deep > 1)
3417 {
3418 /* recursive: lock/unlock the items the List contains */
3419 todo = d->dv_hashtab.ht_used;
3420 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3421 {
3422 if (!HASHITEM_EMPTY(hi))
3423 {
3424 --todo;
3425 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3426 }
3427 }
3428 }
3429 }
3430 }
3431 --recurse;
3432}
3433
Bram Moolenaara40058a2005-07-11 22:42:07 +00003434/*
3435 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3436 * it refers to a List or Dictionary that is locked.
3437 */
3438 static int
3439tv_islocked(tv)
3440 typval_T *tv;
3441{
3442 return (tv->v_lock & VAR_LOCKED)
3443 || (tv->v_type == VAR_LIST
3444 && tv->vval.v_list != NULL
3445 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3446 || (tv->v_type == VAR_DICT
3447 && tv->vval.v_dict != NULL
3448 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3449}
3450
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3452/*
3453 * Delete all "menutrans_" variables.
3454 */
3455 void
3456del_menutrans_vars()
3457{
Bram Moolenaar33570922005-01-25 22:26:29 +00003458 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003459 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaar33570922005-01-25 22:26:29 +00003461 hash_lock(&globvarht);
3462 todo = globvarht.ht_used;
3463 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003464 {
3465 if (!HASHITEM_EMPTY(hi))
3466 {
3467 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003468 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3469 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003470 }
3471 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003472 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473}
3474#endif
3475
3476#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3477
3478/*
3479 * Local string buffer for the next two functions to store a variable name
3480 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3481 * get_user_var_name().
3482 */
3483
3484static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3485
3486static char_u *varnamebuf = NULL;
3487static int varnamebuflen = 0;
3488
3489/*
3490 * Function to concatenate a prefix and a variable name.
3491 */
3492 static char_u *
3493cat_prefix_varname(prefix, name)
3494 int prefix;
3495 char_u *name;
3496{
3497 int len;
3498
3499 len = (int)STRLEN(name) + 3;
3500 if (len > varnamebuflen)
3501 {
3502 vim_free(varnamebuf);
3503 len += 10; /* some additional space */
3504 varnamebuf = alloc(len);
3505 if (varnamebuf == NULL)
3506 {
3507 varnamebuflen = 0;
3508 return NULL;
3509 }
3510 varnamebuflen = len;
3511 }
3512 *varnamebuf = prefix;
3513 varnamebuf[1] = ':';
3514 STRCPY(varnamebuf + 2, name);
3515 return varnamebuf;
3516}
3517
3518/*
3519 * Function given to ExpandGeneric() to obtain the list of user defined
3520 * (global/buffer/window/built-in) variable names.
3521 */
3522/*ARGSUSED*/
3523 char_u *
3524get_user_var_name(xp, idx)
3525 expand_T *xp;
3526 int idx;
3527{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003528 static long_u gdone;
3529 static long_u bdone;
3530 static long_u wdone;
3531 static int vidx;
3532 static hashitem_T *hi;
3533 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
3535 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003536 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003537
3538 /* Global variables */
3539 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003541 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003542 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003543 else
3544 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003545 while (HASHITEM_EMPTY(hi))
3546 ++hi;
3547 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3548 return cat_prefix_varname('g', hi->hi_key);
3549 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003551
3552 /* b: variables */
3553 ht = &curbuf->b_vars.dv_hashtab;
3554 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003556 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003557 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003558 else
3559 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003560 while (HASHITEM_EMPTY(hi))
3561 ++hi;
3562 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003564 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003566 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 return (char_u *)"b:changedtick";
3568 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003569
3570 /* w: variables */
3571 ht = &curwin->w_vars.dv_hashtab;
3572 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003574 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003575 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003576 else
3577 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003578 while (HASHITEM_EMPTY(hi))
3579 ++hi;
3580 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003582
3583 /* v: variables */
3584 if (vidx < VV_LEN)
3585 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
3587 vim_free(varnamebuf);
3588 varnamebuf = NULL;
3589 varnamebuflen = 0;
3590 return NULL;
3591}
3592
3593#endif /* FEAT_CMDL_COMPL */
3594
3595/*
3596 * types for expressions.
3597 */
3598typedef enum
3599{
3600 TYPE_UNKNOWN = 0
3601 , TYPE_EQUAL /* == */
3602 , TYPE_NEQUAL /* != */
3603 , TYPE_GREATER /* > */
3604 , TYPE_GEQUAL /* >= */
3605 , TYPE_SMALLER /* < */
3606 , TYPE_SEQUAL /* <= */
3607 , TYPE_MATCH /* =~ */
3608 , TYPE_NOMATCH /* !~ */
3609} exptype_T;
3610
3611/*
3612 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003613 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3615 */
3616
3617/*
3618 * Handle zero level expression.
3619 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003620 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003621 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 * Return OK or FAIL.
3623 */
3624 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003625eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 char_u **nextcmd;
3629 int evaluate;
3630{
3631 int ret;
3632 char_u *p;
3633
3634 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003635 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (ret == FAIL || !ends_excmd(*p))
3637 {
3638 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003639 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 /*
3641 * Report the invalid expression unless the expression evaluation has
3642 * been cancelled due to an aborting error, an interrupt, or an
3643 * exception.
3644 */
3645 if (!aborting())
3646 EMSG2(_(e_invexpr2), arg);
3647 ret = FAIL;
3648 }
3649 if (nextcmd != NULL)
3650 *nextcmd = check_nextcmd(p);
3651
3652 return ret;
3653}
3654
3655/*
3656 * Handle top level expression:
3657 * expr1 ? expr0 : expr0
3658 *
3659 * "arg" must point to the first non-white of the expression.
3660 * "arg" is advanced to the next non-white after the recognized expression.
3661 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003662 * Note: "rettv.v_lock" is not set.
3663 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 * Return OK or FAIL.
3665 */
3666 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003667eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 int evaluate;
3671{
3672 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
3675 /*
3676 * Get the first variable.
3677 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003678 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 return FAIL;
3680
3681 if ((*arg)[0] == '?')
3682 {
3683 result = FALSE;
3684 if (evaluate)
3685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003686 int error = FALSE;
3687
3688 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003690 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003691 if (error)
3692 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 }
3694
3695 /*
3696 * Get the second variable.
3697 */
3698 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003699 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 return FAIL;
3701
3702 /*
3703 * Check for the ":".
3704 */
3705 if ((*arg)[0] != ':')
3706 {
3707 EMSG(_("E109: Missing ':' after '?'"));
3708 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003709 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 return FAIL;
3711 }
3712
3713 /*
3714 * Get the third variable.
3715 */
3716 *arg = skipwhite(*arg + 1);
3717 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3718 {
3719 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003720 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 return FAIL;
3722 }
3723 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003724 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
3726
3727 return OK;
3728}
3729
3730/*
3731 * Handle first level expression:
3732 * expr2 || expr2 || expr2 logical OR
3733 *
3734 * "arg" must point to the first non-white of the expression.
3735 * "arg" is advanced to the next non-white after the recognized expression.
3736 *
3737 * Return OK or FAIL.
3738 */
3739 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003740eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 int evaluate;
3744{
Bram Moolenaar33570922005-01-25 22:26:29 +00003745 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 long result;
3747 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003748 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
3750 /*
3751 * Get the first variable.
3752 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003753 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 return FAIL;
3755
3756 /*
3757 * Repeat until there is no following "||".
3758 */
3759 first = TRUE;
3760 result = FALSE;
3761 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3762 {
3763 if (evaluate && first)
3764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003765 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003767 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003768 if (error)
3769 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 first = FALSE;
3771 }
3772
3773 /*
3774 * Get the second variable.
3775 */
3776 *arg = skipwhite(*arg + 2);
3777 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3778 return FAIL;
3779
3780 /*
3781 * Compute the result.
3782 */
3783 if (evaluate && !result)
3784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003785 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003787 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003788 if (error)
3789 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791 if (evaluate)
3792 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003793 rettv->v_type = VAR_NUMBER;
3794 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 }
3796 }
3797
3798 return OK;
3799}
3800
3801/*
3802 * Handle second level expression:
3803 * expr3 && expr3 && expr3 logical AND
3804 *
3805 * "arg" must point to the first non-white of the expression.
3806 * "arg" is advanced to the next non-white after the recognized expression.
3807 *
3808 * Return OK or FAIL.
3809 */
3810 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003811eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 int evaluate;
3815{
Bram Moolenaar33570922005-01-25 22:26:29 +00003816 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 long result;
3818 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003819 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820
3821 /*
3822 * Get the first variable.
3823 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003824 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 return FAIL;
3826
3827 /*
3828 * Repeat until there is no following "&&".
3829 */
3830 first = TRUE;
3831 result = TRUE;
3832 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3833 {
3834 if (evaluate && first)
3835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003836 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003838 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003839 if (error)
3840 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 first = FALSE;
3842 }
3843
3844 /*
3845 * Get the second variable.
3846 */
3847 *arg = skipwhite(*arg + 2);
3848 if (eval4(arg, &var2, evaluate && result) == FAIL)
3849 return FAIL;
3850
3851 /*
3852 * Compute the result.
3853 */
3854 if (evaluate && result)
3855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003856 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003858 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003859 if (error)
3860 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 }
3862 if (evaluate)
3863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003864 rettv->v_type = VAR_NUMBER;
3865 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 }
3867 }
3868
3869 return OK;
3870}
3871
3872/*
3873 * Handle third level expression:
3874 * var1 == var2
3875 * var1 =~ var2
3876 * var1 != var2
3877 * var1 !~ var2
3878 * var1 > var2
3879 * var1 >= var2
3880 * var1 < var2
3881 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003882 * var1 is var2
3883 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 *
3885 * "arg" must point to the first non-white of the expression.
3886 * "arg" is advanced to the next non-white after the recognized expression.
3887 *
3888 * Return OK or FAIL.
3889 */
3890 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003891eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 int evaluate;
3895{
Bram Moolenaar33570922005-01-25 22:26:29 +00003896 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 char_u *p;
3898 int i;
3899 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003900 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 int len = 2;
3902 long n1, n2;
3903 char_u *s1, *s2;
3904 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3905 regmatch_T regmatch;
3906 int ic;
3907 char_u *save_cpo;
3908
3909 /*
3910 * Get the first variable.
3911 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003912 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 return FAIL;
3914
3915 p = *arg;
3916 switch (p[0])
3917 {
3918 case '=': if (p[1] == '=')
3919 type = TYPE_EQUAL;
3920 else if (p[1] == '~')
3921 type = TYPE_MATCH;
3922 break;
3923 case '!': if (p[1] == '=')
3924 type = TYPE_NEQUAL;
3925 else if (p[1] == '~')
3926 type = TYPE_NOMATCH;
3927 break;
3928 case '>': if (p[1] != '=')
3929 {
3930 type = TYPE_GREATER;
3931 len = 1;
3932 }
3933 else
3934 type = TYPE_GEQUAL;
3935 break;
3936 case '<': if (p[1] != '=')
3937 {
3938 type = TYPE_SMALLER;
3939 len = 1;
3940 }
3941 else
3942 type = TYPE_SEQUAL;
3943 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003944 case 'i': if (p[1] == 's')
3945 {
3946 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3947 len = 5;
3948 if (!vim_isIDc(p[len]))
3949 {
3950 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3951 type_is = TRUE;
3952 }
3953 }
3954 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
3956
3957 /*
3958 * If there is a comparitive operator, use it.
3959 */
3960 if (type != TYPE_UNKNOWN)
3961 {
3962 /* extra question mark appended: ignore case */
3963 if (p[len] == '?')
3964 {
3965 ic = TRUE;
3966 ++len;
3967 }
3968 /* extra '#' appended: match case */
3969 else if (p[len] == '#')
3970 {
3971 ic = FALSE;
3972 ++len;
3973 }
3974 /* nothing appened: use 'ignorecase' */
3975 else
3976 ic = p_ic;
3977
3978 /*
3979 * Get the second variable.
3980 */
3981 *arg = skipwhite(p + len);
3982 if (eval5(arg, &var2, evaluate) == FAIL)
3983 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003984 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 return FAIL;
3986 }
3987
3988 if (evaluate)
3989 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003990 if (type_is && rettv->v_type != var2.v_type)
3991 {
3992 /* For "is" a different type always means FALSE, for "notis"
3993 * it means TRUE. */
3994 n1 = (type == TYPE_NEQUAL);
3995 }
3996 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3997 {
3998 if (type_is)
3999 {
4000 n1 = (rettv->v_type == var2.v_type
4001 && rettv->vval.v_list == var2.vval.v_list);
4002 if (type == TYPE_NEQUAL)
4003 n1 = !n1;
4004 }
4005 else if (rettv->v_type != var2.v_type
4006 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4007 {
4008 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004009 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004010 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004011 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004012 clear_tv(rettv);
4013 clear_tv(&var2);
4014 return FAIL;
4015 }
4016 else
4017 {
4018 /* Compare two Lists for being equal or unequal. */
4019 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4020 if (type == TYPE_NEQUAL)
4021 n1 = !n1;
4022 }
4023 }
4024
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004025 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4026 {
4027 if (type_is)
4028 {
4029 n1 = (rettv->v_type == var2.v_type
4030 && rettv->vval.v_dict == var2.vval.v_dict);
4031 if (type == TYPE_NEQUAL)
4032 n1 = !n1;
4033 }
4034 else if (rettv->v_type != var2.v_type
4035 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4036 {
4037 if (rettv->v_type != var2.v_type)
4038 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4039 else
4040 EMSG(_("E736: Invalid operation for Dictionary"));
4041 clear_tv(rettv);
4042 clear_tv(&var2);
4043 return FAIL;
4044 }
4045 else
4046 {
4047 /* Compare two Dictionaries for being equal or unequal. */
4048 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4049 if (type == TYPE_NEQUAL)
4050 n1 = !n1;
4051 }
4052 }
4053
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004054 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4055 {
4056 if (rettv->v_type != var2.v_type
4057 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4058 {
4059 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004060 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004061 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004062 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004063 clear_tv(rettv);
4064 clear_tv(&var2);
4065 return FAIL;
4066 }
4067 else
4068 {
4069 /* Compare two Funcrefs for being equal or unequal. */
4070 if (rettv->vval.v_string == NULL
4071 || var2.vval.v_string == NULL)
4072 n1 = FALSE;
4073 else
4074 n1 = STRCMP(rettv->vval.v_string,
4075 var2.vval.v_string) == 0;
4076 if (type == TYPE_NEQUAL)
4077 n1 = !n1;
4078 }
4079 }
4080
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 /*
4082 * If one of the two variables is a number, compare as a number.
4083 * When using "=~" or "!~", always compare as string.
4084 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004085 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4087 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004088 n1 = get_tv_number(rettv);
4089 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 switch (type)
4091 {
4092 case TYPE_EQUAL: n1 = (n1 == n2); break;
4093 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4094 case TYPE_GREATER: n1 = (n1 > n2); break;
4095 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4096 case TYPE_SMALLER: n1 = (n1 < n2); break;
4097 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4098 case TYPE_UNKNOWN:
4099 case TYPE_MATCH:
4100 case TYPE_NOMATCH: break; /* avoid gcc warning */
4101 }
4102 }
4103 else
4104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105 s1 = get_tv_string_buf(rettv, buf1);
4106 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4108 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4109 else
4110 i = 0;
4111 n1 = FALSE;
4112 switch (type)
4113 {
4114 case TYPE_EQUAL: n1 = (i == 0); break;
4115 case TYPE_NEQUAL: n1 = (i != 0); break;
4116 case TYPE_GREATER: n1 = (i > 0); break;
4117 case TYPE_GEQUAL: n1 = (i >= 0); break;
4118 case TYPE_SMALLER: n1 = (i < 0); break;
4119 case TYPE_SEQUAL: n1 = (i <= 0); break;
4120
4121 case TYPE_MATCH:
4122 case TYPE_NOMATCH:
4123 /* avoid 'l' flag in 'cpoptions' */
4124 save_cpo = p_cpo;
4125 p_cpo = (char_u *)"";
4126 regmatch.regprog = vim_regcomp(s2,
4127 RE_MAGIC + RE_STRING);
4128 regmatch.rm_ic = ic;
4129 if (regmatch.regprog != NULL)
4130 {
4131 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4132 vim_free(regmatch.regprog);
4133 if (type == TYPE_NOMATCH)
4134 n1 = !n1;
4135 }
4136 p_cpo = save_cpo;
4137 break;
4138
4139 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4140 }
4141 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142 clear_tv(rettv);
4143 clear_tv(&var2);
4144 rettv->v_type = VAR_NUMBER;
4145 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 }
4147 }
4148
4149 return OK;
4150}
4151
4152/*
4153 * Handle fourth level expression:
4154 * + number addition
4155 * - number subtraction
4156 * . string concatenation
4157 *
4158 * "arg" must point to the first non-white of the expression.
4159 * "arg" is advanced to the next non-white after the recognized expression.
4160 *
4161 * Return OK or FAIL.
4162 */
4163 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 int evaluate;
4168{
Bram Moolenaar33570922005-01-25 22:26:29 +00004169 typval_T var2;
4170 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 int op;
4172 long n1, n2;
4173 char_u *s1, *s2;
4174 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4175 char_u *p;
4176
4177 /*
4178 * Get the first variable.
4179 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 return FAIL;
4182
4183 /*
4184 * Repeat computing, until no '+', '-' or '.' is following.
4185 */
4186 for (;;)
4187 {
4188 op = **arg;
4189 if (op != '+' && op != '-' && op != '.')
4190 break;
4191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004192 if (op != '+' || rettv->v_type != VAR_LIST)
4193 {
4194 /* For "list + ...", an illegal use of the first operand as
4195 * a number cannot be determined before evaluating the 2nd
4196 * operand: if this is also a list, all is ok.
4197 * For "something . ...", "something - ..." or "non-list + ...",
4198 * we know that the first operand needs to be a string or number
4199 * without evaluating the 2nd operand. So check before to avoid
4200 * side effects after an error. */
4201 if (evaluate && get_tv_string_chk(rettv) == NULL)
4202 {
4203 clear_tv(rettv);
4204 return FAIL;
4205 }
4206 }
4207
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 /*
4209 * Get the second variable.
4210 */
4211 *arg = skipwhite(*arg + 1);
4212 if (eval6(arg, &var2, evaluate) == FAIL)
4213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 return FAIL;
4216 }
4217
4218 if (evaluate)
4219 {
4220 /*
4221 * Compute the result.
4222 */
4223 if (op == '.')
4224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004225 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4226 s2 = get_tv_string_buf_chk(&var2, buf2);
4227 if (s2 == NULL) /* type error ? */
4228 {
4229 clear_tv(rettv);
4230 clear_tv(&var2);
4231 return FAIL;
4232 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004233 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 clear_tv(rettv);
4235 rettv->v_type = VAR_STRING;
4236 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004238 else if (op == '+' && rettv->v_type == VAR_LIST
4239 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004240 {
4241 /* concatenate Lists */
4242 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4243 &var3) == FAIL)
4244 {
4245 clear_tv(rettv);
4246 clear_tv(&var2);
4247 return FAIL;
4248 }
4249 clear_tv(rettv);
4250 *rettv = var3;
4251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 else
4253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004254 int error = FALSE;
4255
4256 n1 = get_tv_number_chk(rettv, &error);
4257 if (error)
4258 {
4259 /* This can only happen for "list + non-list".
4260 * For "non-list + ..." or "something - ...", we returned
4261 * before evaluating the 2nd operand. */
4262 clear_tv(rettv);
4263 return FAIL;
4264 }
4265 n2 = get_tv_number_chk(&var2, &error);
4266 if (error)
4267 {
4268 clear_tv(rettv);
4269 clear_tv(&var2);
4270 return FAIL;
4271 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 if (op == '+')
4274 n1 = n1 + n2;
4275 else
4276 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 rettv->v_type = VAR_NUMBER;
4278 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 }
4283 return OK;
4284}
4285
4286/*
4287 * Handle fifth level expression:
4288 * * number multiplication
4289 * / number division
4290 * % number modulo
4291 *
4292 * "arg" must point to the first non-white of the expression.
4293 * "arg" is advanced to the next non-white after the recognized expression.
4294 *
4295 * Return OK or FAIL.
4296 */
4297 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 int evaluate;
4302{
Bram Moolenaar33570922005-01-25 22:26:29 +00004303 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 int op;
4305 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004306 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 /*
4309 * Get the first variable.
4310 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 return FAIL;
4313
4314 /*
4315 * Repeat computing, until no '*', '/' or '%' is following.
4316 */
4317 for (;;)
4318 {
4319 op = **arg;
4320 if (op != '*' && op != '/' && op != '%')
4321 break;
4322
4323 if (evaluate)
4324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004325 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004327 if (error)
4328 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 }
4330 else
4331 n1 = 0;
4332
4333 /*
4334 * Get the second variable.
4335 */
4336 *arg = skipwhite(*arg + 1);
4337 if (eval7(arg, &var2, evaluate) == FAIL)
4338 return FAIL;
4339
4340 if (evaluate)
4341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004343 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004344 if (error)
4345 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
4347 /*
4348 * Compute the result.
4349 */
4350 if (op == '*')
4351 n1 = n1 * n2;
4352 else if (op == '/')
4353 {
4354 if (n2 == 0) /* give an error message? */
4355 n1 = 0x7fffffffL;
4356 else
4357 n1 = n1 / n2;
4358 }
4359 else
4360 {
4361 if (n2 == 0) /* give an error message? */
4362 n1 = 0;
4363 else
4364 n1 = n1 % n2;
4365 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 rettv->v_type = VAR_NUMBER;
4367 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 }
4370
4371 return OK;
4372}
4373
4374/*
4375 * Handle sixth level expression:
4376 * number number constant
4377 * "string" string contstant
4378 * 'string' literal string contstant
4379 * &option-name option value
4380 * @r register contents
4381 * identifier variable value
4382 * function() function call
4383 * $VAR environment variable
4384 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004385 * [expr, expr] List
4386 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 *
4388 * Also handle:
4389 * ! in front logical NOT
4390 * - in front unary minus
4391 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004392 * trailing [] subscript in String or List
4393 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 *
4395 * "arg" must point to the first non-white of the expression.
4396 * "arg" is advanced to the next non-white after the recognized expression.
4397 *
4398 * Return OK or FAIL.
4399 */
4400 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004401eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 int evaluate;
4405{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 long n;
4407 int len;
4408 char_u *s;
4409 int val;
4410 char_u *start_leader, *end_leader;
4411 int ret = OK;
4412 char_u *alias;
4413
4414 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004415 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004416 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004418 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
4420 /*
4421 * Skip '!' and '-' characters. They are handled later.
4422 */
4423 start_leader = *arg;
4424 while (**arg == '!' || **arg == '-' || **arg == '+')
4425 *arg = skipwhite(*arg + 1);
4426 end_leader = *arg;
4427
4428 switch (**arg)
4429 {
4430 /*
4431 * Number constant.
4432 */
4433 case '0':
4434 case '1':
4435 case '2':
4436 case '3':
4437 case '4':
4438 case '5':
4439 case '6':
4440 case '7':
4441 case '8':
4442 case '9':
4443 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4444 *arg += len;
4445 if (evaluate)
4446 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004447 rettv->v_type = VAR_NUMBER;
4448 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
4450 break;
4451
4452 /*
4453 * String constant: "string".
4454 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 break;
4457
4458 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004459 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004461 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004462 break;
4463
4464 /*
4465 * List: [expr, expr]
4466 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004467 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 break;
4469
4470 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004471 * Dictionary: {key: val, key: val}
4472 */
4473 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4474 break;
4475
4476 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004477 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004479 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 break;
4481
4482 /*
4483 * Environment variable: $VAR.
4484 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004485 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 break;
4487
4488 /*
4489 * Register contents: @r.
4490 */
4491 case '@': ++*arg;
4492 if (evaluate)
4493 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004494 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004495 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
4497 if (**arg != NUL)
4498 ++*arg;
4499 break;
4500
4501 /*
4502 * nested expression: (expression).
4503 */
4504 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004505 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 if (**arg == ')')
4507 ++*arg;
4508 else if (ret == OK)
4509 {
4510 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004511 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 ret = FAIL;
4513 }
4514 break;
4515
Bram Moolenaar8c711452005-01-14 21:53:12 +00004516 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 break;
4518 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004519
4520 if (ret == NOTDONE)
4521 {
4522 /*
4523 * Must be a variable or function name.
4524 * Can also be a curly-braces kind of name: {expr}.
4525 */
4526 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004527 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004528 if (alias != NULL)
4529 s = alias;
4530
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004531 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004532 ret = FAIL;
4533 else
4534 {
4535 if (**arg == '(') /* recursive! */
4536 {
4537 /* If "s" is the name of a variable of type VAR_FUNC
4538 * use its contents. */
4539 s = deref_func_name(s, &len);
4540
4541 /* Invoke the function. */
4542 ret = get_func_tv(s, len, rettv, arg,
4543 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004544 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004545 /* Stop the expression evaluation when immediately
4546 * aborting on error, or when an interrupt occurred or
4547 * an exception was thrown but not caught. */
4548 if (aborting())
4549 {
4550 if (ret == OK)
4551 clear_tv(rettv);
4552 ret = FAIL;
4553 }
4554 }
4555 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004556 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004557 else
4558 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004559 }
4560
4561 if (alias != NULL)
4562 vim_free(alias);
4563 }
4564
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 *arg = skipwhite(*arg);
4566
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004567 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4568 * expr(expr). */
4569 if (ret == OK)
4570 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571
4572 /*
4573 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4574 */
4575 if (ret == OK && evaluate && end_leader > start_leader)
4576 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004577 int error = FALSE;
4578
4579 val = get_tv_number_chk(rettv, &error);
4580 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004582 clear_tv(rettv);
4583 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004585 else
4586 {
4587 while (end_leader > start_leader)
4588 {
4589 --end_leader;
4590 if (*end_leader == '!')
4591 val = !val;
4592 else if (*end_leader == '-')
4593 val = -val;
4594 }
4595 clear_tv(rettv);
4596 rettv->v_type = VAR_NUMBER;
4597 rettv->vval.v_number = val;
4598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
4600
4601 return ret;
4602}
4603
4604/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004605 * Evaluate an "[expr]" or "[expr:expr]" index.
4606 * "*arg" points to the '['.
4607 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4608 */
4609 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004610eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004611 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004612 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004613 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004614 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615{
4616 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004617 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004618 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004619 long len = -1;
4620 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004622 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004624 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004625 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004626 if (verbose)
4627 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004628 return FAIL;
4629 }
4630
Bram Moolenaar8c711452005-01-14 21:53:12 +00004631 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004633 /*
4634 * dict.name
4635 */
4636 key = *arg + 1;
4637 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4638 ;
4639 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004640 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004641 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004642 }
4643 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004644 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004645 /*
4646 * something[idx]
4647 *
4648 * Get the (first) variable from inside the [].
4649 */
4650 *arg = skipwhite(*arg + 1);
4651 if (**arg == ':')
4652 empty1 = TRUE;
4653 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4654 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004655 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4656 {
4657 /* not a number or string */
4658 clear_tv(&var1);
4659 return FAIL;
4660 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004661
4662 /*
4663 * Get the second variable from inside the [:].
4664 */
4665 if (**arg == ':')
4666 {
4667 range = TRUE;
4668 *arg = skipwhite(*arg + 1);
4669 if (**arg == ']')
4670 empty2 = TRUE;
4671 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004673 if (!empty1)
4674 clear_tv(&var1);
4675 return FAIL;
4676 }
4677 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4678 {
4679 /* not a number or string */
4680 if (!empty1)
4681 clear_tv(&var1);
4682 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683 return FAIL;
4684 }
4685 }
4686
4687 /* Check for the ']'. */
4688 if (**arg != ']')
4689 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004690 if (verbose)
4691 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004692 clear_tv(&var1);
4693 if (range)
4694 clear_tv(&var2);
4695 return FAIL;
4696 }
4697 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004698 }
4699
4700 if (evaluate)
4701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004702 n1 = 0;
4703 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004705 n1 = get_tv_number(&var1);
4706 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004707 }
4708 if (range)
4709 {
4710 if (empty2)
4711 n2 = -1;
4712 else
4713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004714 n2 = get_tv_number(&var2);
4715 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004716 }
4717 }
4718
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 {
4721 case VAR_NUMBER:
4722 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004723 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004724 len = (long)STRLEN(s);
4725 if (range)
4726 {
4727 /* The resulting variable is a substring. If the indexes
4728 * are out of range the result is empty. */
4729 if (n1 < 0)
4730 {
4731 n1 = len + n1;
4732 if (n1 < 0)
4733 n1 = 0;
4734 }
4735 if (n2 < 0)
4736 n2 = len + n2;
4737 else if (n2 >= len)
4738 n2 = len;
4739 if (n1 >= len || n2 < 0 || n1 > n2)
4740 s = NULL;
4741 else
4742 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4743 }
4744 else
4745 {
4746 /* The resulting variable is a string of a single
4747 * character. If the index is too big or negative the
4748 * result is empty. */
4749 if (n1 >= len || n1 < 0)
4750 s = NULL;
4751 else
4752 s = vim_strnsave(s + n1, 1);
4753 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 clear_tv(rettv);
4755 rettv->v_type = VAR_STRING;
4756 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004757 break;
4758
4759 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004760 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004761 if (n1 < 0)
4762 n1 = len + n1;
4763 if (!empty1 && (n1 < 0 || n1 >= len))
4764 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004765 if (verbose)
4766 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004767 return FAIL;
4768 }
4769 if (range)
4770 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004771 list_T *l;
4772 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773
4774 if (n2 < 0)
4775 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004776 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004777 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004778 if (verbose)
4779 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 return FAIL;
4781 }
4782 l = list_alloc();
4783 if (l == NULL)
4784 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004785 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004786 n1 <= n2; ++n1)
4787 {
4788 if (list_append_tv(l, &item->li_tv) == FAIL)
4789 {
4790 list_free(l);
4791 return FAIL;
4792 }
4793 item = item->li_next;
4794 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004795 clear_tv(rettv);
4796 rettv->v_type = VAR_LIST;
4797 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004798 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799 }
4800 else
4801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004802 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004803 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004804 clear_tv(rettv);
4805 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004806 }
4807 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004808
4809 case VAR_DICT:
4810 if (range)
4811 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004812 if (verbose)
4813 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004814 if (len == -1)
4815 clear_tv(&var1);
4816 return FAIL;
4817 }
4818 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004819 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004820
4821 if (len == -1)
4822 {
4823 key = get_tv_string(&var1);
4824 if (*key == NUL)
4825 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004826 if (verbose)
4827 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004828 clear_tv(&var1);
4829 return FAIL;
4830 }
4831 }
4832
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004833 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004834
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004835 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004836 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004837 if (len == -1)
4838 clear_tv(&var1);
4839 if (item == NULL)
4840 return FAIL;
4841
4842 copy_tv(&item->di_tv, &var1);
4843 clear_tv(rettv);
4844 *rettv = var1;
4845 }
4846 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004847 }
4848 }
4849
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004850 return OK;
4851}
4852
4853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 * Get an option value.
4855 * "arg" points to the '&' or '+' before the option name.
4856 * "arg" is advanced to character after the option name.
4857 * Return OK or FAIL.
4858 */
4859 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004862 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 int evaluate;
4864{
4865 char_u *option_end;
4866 long numval;
4867 char_u *stringval;
4868 int opt_type;
4869 int c;
4870 int working = (**arg == '+'); /* has("+option") */
4871 int ret = OK;
4872 int opt_flags;
4873
4874 /*
4875 * Isolate the option name and find its value.
4876 */
4877 option_end = find_option_end(arg, &opt_flags);
4878 if (option_end == NULL)
4879 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 EMSG2(_("E112: Option name missing: %s"), *arg);
4882 return FAIL;
4883 }
4884
4885 if (!evaluate)
4886 {
4887 *arg = option_end;
4888 return OK;
4889 }
4890
4891 c = *option_end;
4892 *option_end = NUL;
4893 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004894 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
4896 if (opt_type == -3) /* invalid name */
4897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004898 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 EMSG2(_("E113: Unknown option: %s"), *arg);
4900 ret = FAIL;
4901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004902 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
4904 if (opt_type == -2) /* hidden string option */
4905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004906 rettv->v_type = VAR_STRING;
4907 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
4909 else if (opt_type == -1) /* hidden number option */
4910 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 rettv->v_type = VAR_NUMBER;
4912 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 }
4914 else if (opt_type == 1) /* number option */
4915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004916 rettv->v_type = VAR_NUMBER;
4917 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919 else /* string option */
4920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 rettv->v_type = VAR_STRING;
4922 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 }
4924 }
4925 else if (working && (opt_type == -2 || opt_type == -1))
4926 ret = FAIL;
4927
4928 *option_end = c; /* put back for error messages */
4929 *arg = option_end;
4930
4931 return ret;
4932}
4933
4934/*
4935 * Allocate a variable for a string constant.
4936 * Return OK or FAIL.
4937 */
4938 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004939get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 int evaluate;
4943{
4944 char_u *p;
4945 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 int extra = 0;
4947
4948 /*
4949 * Find the end of the string, skipping backslashed characters.
4950 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004951 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 {
4953 if (*p == '\\' && p[1] != NUL)
4954 {
4955 ++p;
4956 /* A "\<x>" form occupies at least 4 characters, and produces up
4957 * to 6 characters: reserve space for 2 extra */
4958 if (*p == '<')
4959 extra += 2;
4960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 }
4962
4963 if (*p != '"')
4964 {
4965 EMSG2(_("E114: Missing quote: %s"), *arg);
4966 return FAIL;
4967 }
4968
4969 /* If only parsing, set *arg and return here */
4970 if (!evaluate)
4971 {
4972 *arg = p + 1;
4973 return OK;
4974 }
4975
4976 /*
4977 * Copy the string into allocated memory, handling backslashed
4978 * characters.
4979 */
4980 name = alloc((unsigned)(p - *arg + extra));
4981 if (name == NULL)
4982 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 rettv->v_type = VAR_STRING;
4984 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
4988 if (*p == '\\')
4989 {
4990 switch (*++p)
4991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004992 case 'b': *name++ = BS; ++p; break;
4993 case 'e': *name++ = ESC; ++p; break;
4994 case 'f': *name++ = FF; ++p; break;
4995 case 'n': *name++ = NL; ++p; break;
4996 case 'r': *name++ = CAR; ++p; break;
4997 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
4999 case 'X': /* hex: "\x1", "\x12" */
5000 case 'x':
5001 case 'u': /* Unicode: "\u0023" */
5002 case 'U':
5003 if (vim_isxdigit(p[1]))
5004 {
5005 int n, nr;
5006 int c = toupper(*p);
5007
5008 if (c == 'X')
5009 n = 2;
5010 else
5011 n = 4;
5012 nr = 0;
5013 while (--n >= 0 && vim_isxdigit(p[1]))
5014 {
5015 ++p;
5016 nr = (nr << 4) + hex2nr(*p);
5017 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005018 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019#ifdef FEAT_MBYTE
5020 /* For "\u" store the number according to
5021 * 'encoding'. */
5022 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005023 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 else
5025#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005026 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 break;
5029
5030 /* octal: "\1", "\12", "\123" */
5031 case '0':
5032 case '1':
5033 case '2':
5034 case '3':
5035 case '4':
5036 case '5':
5037 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 case '7': *name = *p++ - '0';
5039 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 *name = (*name << 3) + *p++ - '0';
5042 if (*p >= '0' && *p <= '7')
5043 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 break;
5047
5048 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005049 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 if (extra != 0)
5051 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005052 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 break;
5054 }
5055 /* FALLTHROUGH */
5056
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 break;
5059 }
5060 }
5061 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 *arg = p + 1;
5067
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 return OK;
5069}
5070
5071/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 * Return OK or FAIL.
5074 */
5075 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005076get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 int evaluate;
5080{
5081 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005082 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005083 int reduce = 0;
5084
5085 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005087 */
5088 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5089 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005091 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005093 break;
5094 ++reduce;
5095 ++p;
5096 }
5097 }
5098
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005100 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 return FAIL;
5103 }
5104
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005106 if (!evaluate)
5107 {
5108 *arg = p + 1;
5109 return OK;
5110 }
5111
5112 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005113 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005114 */
5115 str = alloc((unsigned)((p - *arg) - reduce));
5116 if (str == NULL)
5117 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118 rettv->v_type = VAR_STRING;
5119 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005120
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005122 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005123 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005124 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005126 break;
5127 ++p;
5128 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005130 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005131 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005132 *arg = p + 1;
5133
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005134 return OK;
5135}
5136
5137/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005138 * Allocate a variable for a List and fill it from "*arg".
5139 * Return OK or FAIL.
5140 */
5141 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005142get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005144 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005145 int evaluate;
5146{
Bram Moolenaar33570922005-01-25 22:26:29 +00005147 list_T *l = NULL;
5148 typval_T tv;
5149 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005150
5151 if (evaluate)
5152 {
5153 l = list_alloc();
5154 if (l == NULL)
5155 return FAIL;
5156 }
5157
5158 *arg = skipwhite(*arg + 1);
5159 while (**arg != ']' && **arg != NUL)
5160 {
5161 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5162 goto failret;
5163 if (evaluate)
5164 {
5165 item = listitem_alloc();
5166 if (item != NULL)
5167 {
5168 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005169 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005170 list_append(l, item);
5171 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005172 else
5173 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 }
5175
5176 if (**arg == ']')
5177 break;
5178 if (**arg != ',')
5179 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005181 goto failret;
5182 }
5183 *arg = skipwhite(*arg + 1);
5184 }
5185
5186 if (**arg != ']')
5187 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005189failret:
5190 if (evaluate)
5191 list_free(l);
5192 return FAIL;
5193 }
5194
5195 *arg = skipwhite(*arg + 1);
5196 if (evaluate)
5197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005198 rettv->v_type = VAR_LIST;
5199 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005200 ++l->lv_refcount;
5201 }
5202
5203 return OK;
5204}
5205
5206/*
5207 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005208 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005209 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005210 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005211list_alloc()
5212{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005213 list_T *l;
5214
5215 l = (list_T *)alloc_clear(sizeof(list_T));
5216 if (l != NULL)
5217 {
5218 /* Prepend the list to the list of lists for garbage collection. */
5219 if (first_list != NULL)
5220 first_list->lv_used_prev = l;
5221 l->lv_used_prev = NULL;
5222 l->lv_used_next = first_list;
5223 first_list = l;
5224 }
5225 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226}
5227
5228/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005229 * Allocate an empty list for a return value.
5230 * Returns OK or FAIL.
5231 */
5232 static int
5233rettv_list_alloc(rettv)
5234 typval_T *rettv;
5235{
5236 list_T *l = list_alloc();
5237
5238 if (l == NULL)
5239 return FAIL;
5240
5241 rettv->vval.v_list = l;
5242 rettv->v_type = VAR_LIST;
5243 ++l->lv_refcount;
5244 return OK;
5245}
5246
5247/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248 * Unreference a list: decrement the reference count and free it when it
5249 * becomes zero.
5250 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005251 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005253 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005255 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5256 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257}
5258
5259/*
5260 * Free a list, including all items it points to.
5261 * Ignores the reference count.
5262 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005263 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005265 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266{
Bram Moolenaar33570922005-01-25 22:26:29 +00005267 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268
Bram Moolenaard9fba312005-06-26 22:34:35 +00005269 /* Avoid that recursive reference to the list frees us again. */
5270 l->lv_refcount = DEL_REFCOUNT;
5271
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005272 /* Remove the list from the list of lists for garbage collection. */
5273 if (l->lv_used_prev == NULL)
5274 first_list = l->lv_used_next;
5275 else
5276 l->lv_used_prev->lv_used_next = l->lv_used_next;
5277 if (l->lv_used_next != NULL)
5278 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5279
Bram Moolenaard9fba312005-06-26 22:34:35 +00005280 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005282 /* Remove the item before deleting it. */
5283 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284 listitem_free(item);
5285 }
5286 vim_free(l);
5287}
5288
5289/*
5290 * Allocate a list item.
5291 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005292 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293listitem_alloc()
5294{
Bram Moolenaar33570922005-01-25 22:26:29 +00005295 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296}
5297
5298/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005299 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 */
5301 static void
5302listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005303 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005305 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 vim_free(item);
5307}
5308
5309/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005310 * Remove a list item from a List and free it. Also clears the value.
5311 */
5312 static void
5313listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005314 list_T *l;
5315 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005316{
5317 list_remove(l, item, item);
5318 listitem_free(item);
5319}
5320
5321/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 * Get the number of items in a list.
5323 */
5324 static long
5325list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005326 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 if (l == NULL)
5329 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005330 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331}
5332
5333/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005334 * Return TRUE when two lists have exactly the same values.
5335 */
5336 static int
5337list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005338 list_T *l1;
5339 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005340 int ic; /* ignore case for strings */
5341{
Bram Moolenaar33570922005-01-25 22:26:29 +00005342 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005343
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005344 if (list_len(l1) != list_len(l2))
5345 return FALSE;
5346
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005347 for (item1 = l1->lv_first, item2 = l2->lv_first;
5348 item1 != NULL && item2 != NULL;
5349 item1 = item1->li_next, item2 = item2->li_next)
5350 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5351 return FALSE;
5352 return item1 == NULL && item2 == NULL;
5353}
5354
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005355#if defined(FEAT_PYTHON) || defined(PROTO)
5356/*
5357 * Return the dictitem that an entry in a hashtable points to.
5358 */
5359 dictitem_T *
5360dict_lookup(hi)
5361 hashitem_T *hi;
5362{
5363 return HI2DI(hi);
5364}
5365#endif
5366
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005367/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005368 * Return TRUE when two dictionaries have exactly the same key/values.
5369 */
5370 static int
5371dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005372 dict_T *d1;
5373 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005374 int ic; /* ignore case for strings */
5375{
Bram Moolenaar33570922005-01-25 22:26:29 +00005376 hashitem_T *hi;
5377 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005378 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005379
5380 if (dict_len(d1) != dict_len(d2))
5381 return FALSE;
5382
Bram Moolenaar33570922005-01-25 22:26:29 +00005383 todo = d1->dv_hashtab.ht_used;
5384 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005385 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005386 if (!HASHITEM_EMPTY(hi))
5387 {
5388 item2 = dict_find(d2, hi->hi_key, -1);
5389 if (item2 == NULL)
5390 return FALSE;
5391 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5392 return FALSE;
5393 --todo;
5394 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005395 }
5396 return TRUE;
5397}
5398
5399/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005400 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005401 * Compares the items just like "==" would compare them, but strings and
5402 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005403 */
5404 static int
5405tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005406 typval_T *tv1;
5407 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005408 int ic; /* ignore case */
5409{
5410 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005411 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005412
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005413 if (tv1->v_type != tv2->v_type)
5414 return FALSE;
5415
5416 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005417 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005418 case VAR_LIST:
5419 /* recursive! */
5420 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5421
5422 case VAR_DICT:
5423 /* recursive! */
5424 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5425
5426 case VAR_FUNC:
5427 return (tv1->vval.v_string != NULL
5428 && tv2->vval.v_string != NULL
5429 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5430
5431 case VAR_NUMBER:
5432 return tv1->vval.v_number == tv2->vval.v_number;
5433
5434 case VAR_STRING:
5435 s1 = get_tv_string_buf(tv1, buf1);
5436 s2 = get_tv_string_buf(tv2, buf2);
5437 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005438 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005439
5440 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005441 return TRUE;
5442}
5443
5444/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 * Locate item with index "n" in list "l" and return it.
5446 * A negative index is counted from the end; -1 is the last item.
5447 * Returns NULL when "n" is out of range.
5448 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005449 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005451 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 long n;
5453{
Bram Moolenaar33570922005-01-25 22:26:29 +00005454 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 long idx;
5456
5457 if (l == NULL)
5458 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005459
5460 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005462 n = l->lv_len + n;
5463
5464 /* Check for index out of range. */
5465 if (n < 0 || n >= l->lv_len)
5466 return NULL;
5467
5468 /* When there is a cached index may start search from there. */
5469 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005471 if (n < l->lv_idx / 2)
5472 {
5473 /* closest to the start of the list */
5474 item = l->lv_first;
5475 idx = 0;
5476 }
5477 else if (n > (l->lv_idx + l->lv_len) / 2)
5478 {
5479 /* closest to the end of the list */
5480 item = l->lv_last;
5481 idx = l->lv_len - 1;
5482 }
5483 else
5484 {
5485 /* closest to the cached index */
5486 item = l->lv_idx_item;
5487 idx = l->lv_idx;
5488 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 }
5490 else
5491 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005492 if (n < l->lv_len / 2)
5493 {
5494 /* closest to the start of the list */
5495 item = l->lv_first;
5496 idx = 0;
5497 }
5498 else
5499 {
5500 /* closest to the end of the list */
5501 item = l->lv_last;
5502 idx = l->lv_len - 1;
5503 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005505
5506 while (n > idx)
5507 {
5508 /* search forward */
5509 item = item->li_next;
5510 ++idx;
5511 }
5512 while (n < idx)
5513 {
5514 /* search backward */
5515 item = item->li_prev;
5516 --idx;
5517 }
5518
5519 /* cache the used index */
5520 l->lv_idx = idx;
5521 l->lv_idx_item = item;
5522
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 return item;
5524}
5525
5526/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005527 * Get list item "l[idx]" as a number.
5528 */
5529 static long
5530list_find_nr(l, idx, errorp)
5531 list_T *l;
5532 long idx;
5533 int *errorp; /* set to TRUE when something wrong */
5534{
5535 listitem_T *li;
5536
5537 li = list_find(l, idx);
5538 if (li == NULL)
5539 {
5540 if (errorp != NULL)
5541 *errorp = TRUE;
5542 return -1L;
5543 }
5544 return get_tv_number_chk(&li->li_tv, errorp);
5545}
5546
5547/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005548 * Locate "item" list "l" and return its index.
5549 * Returns -1 when "item" is not in the list.
5550 */
5551 static long
5552list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005553 list_T *l;
5554 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005555{
5556 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005557 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005558
5559 if (l == NULL)
5560 return -1;
5561 idx = 0;
5562 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5563 ++idx;
5564 if (li == NULL)
5565 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005566 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005567}
5568
5569/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 * Append item "item" to the end of list "l".
5571 */
5572 static void
5573list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005574 list_T *l;
5575 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005576{
5577 if (l->lv_last == NULL)
5578 {
5579 /* empty list */
5580 l->lv_first = item;
5581 l->lv_last = item;
5582 item->li_prev = NULL;
5583 }
5584 else
5585 {
5586 l->lv_last->li_next = item;
5587 item->li_prev = l->lv_last;
5588 l->lv_last = item;
5589 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005590 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005591 item->li_next = NULL;
5592}
5593
5594/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005595 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005596 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597 */
5598 static int
5599list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 list_T *l;
5601 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005603 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604
Bram Moolenaar05159a02005-02-26 23:04:13 +00005605 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005606 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005607 copy_tv(tv, &li->li_tv);
5608 list_append(l, li);
5609 return OK;
5610}
5611
5612/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005613 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005614 * Return FAIL when out of memory.
5615 */
5616 int
5617list_append_dict(list, dict)
5618 list_T *list;
5619 dict_T *dict;
5620{
5621 listitem_T *li = listitem_alloc();
5622
5623 if (li == NULL)
5624 return FAIL;
5625 li->li_tv.v_type = VAR_DICT;
5626 li->li_tv.v_lock = 0;
5627 li->li_tv.vval.v_dict = dict;
5628 list_append(list, li);
5629 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005630 return OK;
5631}
5632
5633/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005634 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005635 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005636 * Returns FAIL when out of memory.
5637 */
5638 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005639list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005640 list_T *l;
5641 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005642 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005643{
5644 listitem_T *li = listitem_alloc();
5645
5646 if (li == NULL)
5647 return FAIL;
5648 list_append(l, li);
5649 li->li_tv.v_type = VAR_STRING;
5650 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005651 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5652 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005653 return FAIL;
5654 return OK;
5655}
5656
5657/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005658 * Append "n" to list "l".
5659 * Returns FAIL when out of memory.
5660 */
5661 static int
5662list_append_number(l, n)
5663 list_T *l;
5664 varnumber_T n;
5665{
5666 listitem_T *li;
5667
5668 li = listitem_alloc();
5669 if (li == NULL)
5670 return FAIL;
5671 li->li_tv.v_type = VAR_NUMBER;
5672 li->li_tv.v_lock = 0;
5673 li->li_tv.vval.v_number = n;
5674 list_append(l, li);
5675 return OK;
5676}
5677
5678/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005679 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005680 * If "item" is NULL append at the end.
5681 * Return FAIL when out of memory.
5682 */
5683 static int
5684list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005685 list_T *l;
5686 typval_T *tv;
5687 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005688{
Bram Moolenaar33570922005-01-25 22:26:29 +00005689 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005690
5691 if (ni == NULL)
5692 return FAIL;
5693 copy_tv(tv, &ni->li_tv);
5694 if (item == NULL)
5695 /* Append new item at end of list. */
5696 list_append(l, ni);
5697 else
5698 {
5699 /* Insert new item before existing item. */
5700 ni->li_prev = item->li_prev;
5701 ni->li_next = item;
5702 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005703 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005704 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005705 ++l->lv_idx;
5706 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005707 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005708 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005709 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005710 l->lv_idx_item = NULL;
5711 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005712 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005713 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005714 }
5715 return OK;
5716}
5717
5718/*
5719 * Extend "l1" with "l2".
5720 * If "bef" is NULL append at the end, otherwise insert before this item.
5721 * Returns FAIL when out of memory.
5722 */
5723 static int
5724list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005725 list_T *l1;
5726 list_T *l2;
5727 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005728{
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005730
5731 for (item = l2->lv_first; item != NULL; item = item->li_next)
5732 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5733 return FAIL;
5734 return OK;
5735}
5736
5737/*
5738 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5739 * Return FAIL when out of memory.
5740 */
5741 static int
5742list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005743 list_T *l1;
5744 list_T *l2;
5745 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005746{
Bram Moolenaar33570922005-01-25 22:26:29 +00005747 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005748
5749 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005750 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005751 if (l == NULL)
5752 return FAIL;
5753 tv->v_type = VAR_LIST;
5754 tv->vval.v_list = l;
5755
5756 /* append all items from the second list */
5757 return list_extend(l, l2, NULL);
5758}
5759
5760/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005761 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005762 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005763 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764 * Returns NULL when out of memory.
5765 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005766 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005767list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005768 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005770 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771{
Bram Moolenaar33570922005-01-25 22:26:29 +00005772 list_T *copy;
5773 listitem_T *item;
5774 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775
5776 if (orig == NULL)
5777 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778
5779 copy = list_alloc();
5780 if (copy != NULL)
5781 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005782 if (copyID != 0)
5783 {
5784 /* Do this before adding the items, because one of the items may
5785 * refer back to this list. */
5786 orig->lv_copyID = copyID;
5787 orig->lv_copylist = copy;
5788 }
5789 for (item = orig->lv_first; item != NULL && !got_int;
5790 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005791 {
5792 ni = listitem_alloc();
5793 if (ni == NULL)
5794 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005795 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005796 {
5797 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5798 {
5799 vim_free(ni);
5800 break;
5801 }
5802 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005804 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805 list_append(copy, ni);
5806 }
5807 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005808 if (item != NULL)
5809 {
5810 list_unref(copy);
5811 copy = NULL;
5812 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 }
5814
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005815 return copy;
5816}
5817
5818/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005819 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005820 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005822 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005823list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005824 list_T *l;
5825 listitem_T *item;
5826 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005827{
Bram Moolenaar33570922005-01-25 22:26:29 +00005828 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005830 /* notify watchers */
5831 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005833 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005834 list_fix_watch(l, ip);
5835 if (ip == item2)
5836 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005838
5839 if (item2->li_next == NULL)
5840 l->lv_last = item->li_prev;
5841 else
5842 item2->li_next->li_prev = item->li_prev;
5843 if (item->li_prev == NULL)
5844 l->lv_first = item2->li_next;
5845 else
5846 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005847 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005848}
5849
5850/*
5851 * Return an allocated string with the string representation of a list.
5852 * May return NULL.
5853 */
5854 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005855list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005856 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005857 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858{
5859 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860
5861 if (tv->vval.v_list == NULL)
5862 return NULL;
5863 ga_init2(&ga, (int)sizeof(char), 80);
5864 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005865 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005866 {
5867 vim_free(ga.ga_data);
5868 return NULL;
5869 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870 ga_append(&ga, ']');
5871 ga_append(&ga, NUL);
5872 return (char_u *)ga.ga_data;
5873}
5874
5875/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 * Join list "l" into a string in "*gap", using separator "sep".
5877 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005878 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005879 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005880 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005881list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005883 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 char_u *sep;
5885 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005886 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887{
5888 int first = TRUE;
5889 char_u *tofree;
5890 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005891 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 char_u *s;
5893
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005894 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 {
5896 if (first)
5897 first = FALSE;
5898 else
5899 ga_concat(gap, sep);
5900
5901 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005902 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005904 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005905 if (s != NULL)
5906 ga_concat(gap, s);
5907 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005908 if (s == NULL)
5909 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005910 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005911 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912}
5913
5914/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005915 * Garbage collection for lists and dictionaries.
5916 *
5917 * We use reference counts to be able to free most items right away when they
5918 * are no longer used. But for composite items it's possible that it becomes
5919 * unused while the reference count is > 0: When there is a recursive
5920 * reference. Example:
5921 * :let l = [1, 2, 3]
5922 * :let d = {9: l}
5923 * :let l[1] = d
5924 *
5925 * Since this is quite unusual we handle this with garbage collection: every
5926 * once in a while find out which lists and dicts are not referenced from any
5927 * variable.
5928 *
5929 * Here is a good reference text about garbage collection (refers to Python
5930 * but it applies to all reference-counting mechanisms):
5931 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005932 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005933
5934/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005935 * Do garbage collection for lists and dicts.
5936 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005937 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005938 int
5939garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005940{
5941 dict_T *dd;
5942 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005943 int copyID = ++current_copyID;
5944 buf_T *buf;
5945 win_T *wp;
5946 int i;
5947 funccall_T *fc;
5948 int did_free = FALSE;
5949
5950 /*
5951 * 1. Go through all accessible variables and mark all lists and dicts
5952 * with copyID.
5953 */
5954 /* script-local variables */
5955 for (i = 1; i <= ga_scripts.ga_len; ++i)
5956 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5957
5958 /* buffer-local variables */
5959 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5960 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5961
5962 /* window-local variables */
5963 FOR_ALL_WINDOWS(wp)
5964 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5965
5966 /* global variables */
5967 set_ref_in_ht(&globvarht, copyID);
5968
5969 /* function-local variables */
5970 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5971 {
5972 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5973 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5974 }
5975
5976 /*
5977 * 2. Go through the list of dicts and free items without the copyID.
5978 */
5979 for (dd = first_dict; dd != NULL; )
5980 if (dd->dv_copyID != copyID)
5981 {
5982 dict_free(dd);
5983 did_free = TRUE;
5984
5985 /* restart, next dict may also have been freed */
5986 dd = first_dict;
5987 }
5988 else
5989 dd = dd->dv_used_next;
5990
5991 /*
5992 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005993 * But don't free a list that has a watcher (used in a for loop), these
5994 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005995 */
5996 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005997 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005998 {
5999 list_free(ll);
6000 did_free = TRUE;
6001
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006002 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006003 ll = first_list;
6004 }
6005 else
6006 ll = ll->lv_used_next;
6007
6008 return did_free;
6009}
6010
6011/*
6012 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6013 */
6014 static void
6015set_ref_in_ht(ht, copyID)
6016 hashtab_T *ht;
6017 int copyID;
6018{
6019 int todo;
6020 hashitem_T *hi;
6021
6022 todo = ht->ht_used;
6023 for (hi = ht->ht_array; todo > 0; ++hi)
6024 if (!HASHITEM_EMPTY(hi))
6025 {
6026 --todo;
6027 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6028 }
6029}
6030
6031/*
6032 * Mark all lists and dicts referenced through list "l" with "copyID".
6033 */
6034 static void
6035set_ref_in_list(l, copyID)
6036 list_T *l;
6037 int copyID;
6038{
6039 listitem_T *li;
6040
6041 for (li = l->lv_first; li != NULL; li = li->li_next)
6042 set_ref_in_item(&li->li_tv, copyID);
6043}
6044
6045/*
6046 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6047 */
6048 static void
6049set_ref_in_item(tv, copyID)
6050 typval_T *tv;
6051 int copyID;
6052{
6053 dict_T *dd;
6054 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006055
6056 switch (tv->v_type)
6057 {
6058 case VAR_DICT:
6059 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006060 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006061 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006062 /* Didn't see this dict yet. */
6063 dd->dv_copyID = copyID;
6064 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006065 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006066 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006067
6068 case VAR_LIST:
6069 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006070 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006071 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006072 /* Didn't see this list yet. */
6073 ll->lv_copyID = copyID;
6074 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006075 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006076 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006077 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006078 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006079}
6080
6081/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006082 * Allocate an empty header for a dictionary.
6083 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006084 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006085dict_alloc()
6086{
Bram Moolenaar33570922005-01-25 22:26:29 +00006087 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006088
Bram Moolenaar33570922005-01-25 22:26:29 +00006089 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006090 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006091 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006092 /* Add the list to the hashtable for garbage collection. */
6093 if (first_dict != NULL)
6094 first_dict->dv_used_prev = d;
6095 d->dv_used_next = first_dict;
6096 d->dv_used_prev = NULL;
6097
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006099 d->dv_lock = 0;
6100 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006101 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006102 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006103 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006104}
6105
6106/*
6107 * Unreference a Dictionary: decrement the reference count and free it when it
6108 * becomes zero.
6109 */
6110 static void
6111dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006113{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006114 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6115 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006116}
6117
6118/*
6119 * Free a Dictionary, including all items it contains.
6120 * Ignores the reference count.
6121 */
6122 static void
6123dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006124 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006125{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006126 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006127 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006128 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006129
Bram Moolenaard9fba312005-06-26 22:34:35 +00006130 /* Avoid that recursive reference to the dict frees us again. */
6131 d->dv_refcount = DEL_REFCOUNT;
6132
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006133 /* Remove the dict from the list of dicts for garbage collection. */
6134 if (d->dv_used_prev == NULL)
6135 first_dict = d->dv_used_next;
6136 else
6137 d->dv_used_prev->dv_used_next = d->dv_used_next;
6138 if (d->dv_used_next != NULL)
6139 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6140
6141 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006142 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 todo = d->dv_hashtab.ht_used;
6144 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006145 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006146 if (!HASHITEM_EMPTY(hi))
6147 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006148 /* Remove the item before deleting it, just in case there is
6149 * something recursive causing trouble. */
6150 di = HI2DI(hi);
6151 hash_remove(&d->dv_hashtab, hi);
6152 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006153 --todo;
6154 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006155 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006156 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006157 vim_free(d);
6158}
6159
6160/*
6161 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006162 * The "key" is copied to the new item.
6163 * Note that the value of the item "di_tv" still needs to be initialized!
6164 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006165 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006166 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006167dictitem_alloc(key)
6168 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006169{
Bram Moolenaar33570922005-01-25 22:26:29 +00006170 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006171
Bram Moolenaar33570922005-01-25 22:26:29 +00006172 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006173 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006174 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006175 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006176 di->di_flags = 0;
6177 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006178 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006179}
6180
6181/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006182 * Make a copy of a Dictionary item.
6183 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006184 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006185dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006186 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006187{
Bram Moolenaar33570922005-01-25 22:26:29 +00006188 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006189
Bram Moolenaar33570922005-01-25 22:26:29 +00006190 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006191 if (di != NULL)
6192 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006193 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006194 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006195 copy_tv(&org->di_tv, &di->di_tv);
6196 }
6197 return di;
6198}
6199
6200/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006201 * Remove item "item" from Dictionary "dict" and free it.
6202 */
6203 static void
6204dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006205 dict_T *dict;
6206 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006207{
Bram Moolenaar33570922005-01-25 22:26:29 +00006208 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006209
Bram Moolenaar33570922005-01-25 22:26:29 +00006210 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006211 if (HASHITEM_EMPTY(hi))
6212 EMSG2(_(e_intern2), "dictitem_remove()");
6213 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006214 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006215 dictitem_free(item);
6216}
6217
6218/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006219 * Free a dict item. Also clears the value.
6220 */
6221 static void
6222dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006223 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006224{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006225 clear_tv(&item->di_tv);
6226 vim_free(item);
6227}
6228
6229/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006230 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6231 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006232 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006233 * Returns NULL when out of memory.
6234 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006236dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006238 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006239 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006240{
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 dict_T *copy;
6242 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006243 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006244 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006245
6246 if (orig == NULL)
6247 return NULL;
6248
6249 copy = dict_alloc();
6250 if (copy != NULL)
6251 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006252 if (copyID != 0)
6253 {
6254 orig->dv_copyID = copyID;
6255 orig->dv_copydict = copy;
6256 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006258 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006259 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006260 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006261 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006262 --todo;
6263
6264 di = dictitem_alloc(hi->hi_key);
6265 if (di == NULL)
6266 break;
6267 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006268 {
6269 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6270 copyID) == FAIL)
6271 {
6272 vim_free(di);
6273 break;
6274 }
6275 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006276 else
6277 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6278 if (dict_add(copy, di) == FAIL)
6279 {
6280 dictitem_free(di);
6281 break;
6282 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006283 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006284 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006285
Bram Moolenaare9a41262005-01-15 22:18:47 +00006286 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006287 if (todo > 0)
6288 {
6289 dict_unref(copy);
6290 copy = NULL;
6291 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006292 }
6293
6294 return copy;
6295}
6296
6297/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006298 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006299 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006300 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006301 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006302dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006303 dict_T *d;
6304 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006305{
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006307}
6308
Bram Moolenaar8c711452005-01-14 21:53:12 +00006309/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006310 * Add a number or string entry to dictionary "d".
6311 * When "str" is NULL use number "nr", otherwise use "str".
6312 * Returns FAIL when out of memory and when key already exists.
6313 */
6314 int
6315dict_add_nr_str(d, key, nr, str)
6316 dict_T *d;
6317 char *key;
6318 long nr;
6319 char_u *str;
6320{
6321 dictitem_T *item;
6322
6323 item = dictitem_alloc((char_u *)key);
6324 if (item == NULL)
6325 return FAIL;
6326 item->di_tv.v_lock = 0;
6327 if (str == NULL)
6328 {
6329 item->di_tv.v_type = VAR_NUMBER;
6330 item->di_tv.vval.v_number = nr;
6331 }
6332 else
6333 {
6334 item->di_tv.v_type = VAR_STRING;
6335 item->di_tv.vval.v_string = vim_strsave(str);
6336 }
6337 if (dict_add(d, item) == FAIL)
6338 {
6339 dictitem_free(item);
6340 return FAIL;
6341 }
6342 return OK;
6343}
6344
6345/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006346 * Get the number of items in a Dictionary.
6347 */
6348 static long
6349dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006351{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006352 if (d == NULL)
6353 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006354 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006355}
6356
6357/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006358 * Find item "key[len]" in Dictionary "d".
6359 * If "len" is negative use strlen(key).
6360 * Returns NULL when not found.
6361 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006362 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006363dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006365 char_u *key;
6366 int len;
6367{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006368#define AKEYLEN 200
6369 char_u buf[AKEYLEN];
6370 char_u *akey;
6371 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006372 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006373
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006374 if (len < 0)
6375 akey = key;
6376 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006377 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006378 tofree = akey = vim_strnsave(key, len);
6379 if (akey == NULL)
6380 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006381 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006382 else
6383 {
6384 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006385 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006386 akey = buf;
6387 }
6388
Bram Moolenaar33570922005-01-25 22:26:29 +00006389 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006390 vim_free(tofree);
6391 if (HASHITEM_EMPTY(hi))
6392 return NULL;
6393 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006394}
6395
6396/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006397 * Get a string item from a dictionary.
6398 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006399 * Returns NULL if the entry doesn't exist or out of memory.
6400 */
6401 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006402get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006403 dict_T *d;
6404 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006405 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006406{
6407 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006408 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006409
6410 di = dict_find(d, key, -1);
6411 if (di == NULL)
6412 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006413 s = get_tv_string(&di->di_tv);
6414 if (save && s != NULL)
6415 s = vim_strsave(s);
6416 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006417}
6418
6419/*
6420 * Get a number item from a dictionary.
6421 * Returns 0 if the entry doesn't exist or out of memory.
6422 */
6423 long
6424get_dict_number(d, key)
6425 dict_T *d;
6426 char_u *key;
6427{
6428 dictitem_T *di;
6429
6430 di = dict_find(d, key, -1);
6431 if (di == NULL)
6432 return 0;
6433 return get_tv_number(&di->di_tv);
6434}
6435
6436/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006437 * Return an allocated string with the string representation of a Dictionary.
6438 * May return NULL.
6439 */
6440 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006441dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006442 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006443 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006444{
6445 garray_T ga;
6446 int first = TRUE;
6447 char_u *tofree;
6448 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006449 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006450 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006451 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006452 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006453
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006454 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006455 return NULL;
6456 ga_init2(&ga, (int)sizeof(char), 80);
6457 ga_append(&ga, '{');
6458
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006460 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006461 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006462 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006463 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006464 --todo;
6465
6466 if (first)
6467 first = FALSE;
6468 else
6469 ga_concat(&ga, (char_u *)", ");
6470
6471 tofree = string_quote(hi->hi_key, FALSE);
6472 if (tofree != NULL)
6473 {
6474 ga_concat(&ga, tofree);
6475 vim_free(tofree);
6476 }
6477 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006478 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006479 if (s != NULL)
6480 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006481 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006482 if (s == NULL)
6483 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006484 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006485 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006486 if (todo > 0)
6487 {
6488 vim_free(ga.ga_data);
6489 return NULL;
6490 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006491
6492 ga_append(&ga, '}');
6493 ga_append(&ga, NUL);
6494 return (char_u *)ga.ga_data;
6495}
6496
6497/*
6498 * Allocate a variable for a Dictionary and fill it from "*arg".
6499 * Return OK or FAIL. Returns NOTDONE for {expr}.
6500 */
6501 static int
6502get_dict_tv(arg, rettv, evaluate)
6503 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006504 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006505 int evaluate;
6506{
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 dict_T *d = NULL;
6508 typval_T tvkey;
6509 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006510 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006511 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006512 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006513 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006514
6515 /*
6516 * First check if it's not a curly-braces thing: {expr}.
6517 * Must do this without evaluating, otherwise a function may be called
6518 * twice. Unfortunately this means we need to call eval1() twice for the
6519 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006521 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006522 if (*start != '}')
6523 {
6524 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6525 return FAIL;
6526 if (*start == '}')
6527 return NOTDONE;
6528 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006529
6530 if (evaluate)
6531 {
6532 d = dict_alloc();
6533 if (d == NULL)
6534 return FAIL;
6535 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006536 tvkey.v_type = VAR_UNKNOWN;
6537 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006538
6539 *arg = skipwhite(*arg + 1);
6540 while (**arg != '}' && **arg != NUL)
6541 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006542 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006543 goto failret;
6544 if (**arg != ':')
6545 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006546 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006547 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006548 goto failret;
6549 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006550 key = get_tv_string_buf_chk(&tvkey, buf);
6551 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006553 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6554 if (key != NULL)
6555 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006556 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006557 goto failret;
6558 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006559
6560 *arg = skipwhite(*arg + 1);
6561 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6562 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006563 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006564 goto failret;
6565 }
6566 if (evaluate)
6567 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006568 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006569 if (item != NULL)
6570 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006571 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006572 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006573 clear_tv(&tv);
6574 goto failret;
6575 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006576 item = dictitem_alloc(key);
6577 clear_tv(&tvkey);
6578 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006579 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006580 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006581 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006582 if (dict_add(d, item) == FAIL)
6583 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006584 }
6585 }
6586
6587 if (**arg == '}')
6588 break;
6589 if (**arg != ',')
6590 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006591 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006592 goto failret;
6593 }
6594 *arg = skipwhite(*arg + 1);
6595 }
6596
6597 if (**arg != '}')
6598 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006599 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006600failret:
6601 if (evaluate)
6602 dict_free(d);
6603 return FAIL;
6604 }
6605
6606 *arg = skipwhite(*arg + 1);
6607 if (evaluate)
6608 {
6609 rettv->v_type = VAR_DICT;
6610 rettv->vval.v_dict = d;
6611 ++d->dv_refcount;
6612 }
6613
6614 return OK;
6615}
6616
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618 * Return a string with the string representation of a variable.
6619 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006620 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006621 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006622 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 * May return NULL;
6624 */
6625 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006626echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006627 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006629 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006630 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006631{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006632 static int recurse = 0;
6633 char_u *r = NULL;
6634
Bram Moolenaar33570922005-01-25 22:26:29 +00006635 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006636 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006637 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006638 *tofree = NULL;
6639 return NULL;
6640 }
6641 ++recurse;
6642
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 switch (tv->v_type)
6644 {
6645 case VAR_FUNC:
6646 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006647 r = tv->vval.v_string;
6648 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006649
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006651 if (tv->vval.v_list == NULL)
6652 {
6653 *tofree = NULL;
6654 r = NULL;
6655 }
6656 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6657 {
6658 *tofree = NULL;
6659 r = (char_u *)"[...]";
6660 }
6661 else
6662 {
6663 tv->vval.v_list->lv_copyID = copyID;
6664 *tofree = list2string(tv, copyID);
6665 r = *tofree;
6666 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006667 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006668
Bram Moolenaar8c711452005-01-14 21:53:12 +00006669 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006670 if (tv->vval.v_dict == NULL)
6671 {
6672 *tofree = NULL;
6673 r = NULL;
6674 }
6675 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6676 {
6677 *tofree = NULL;
6678 r = (char_u *)"{...}";
6679 }
6680 else
6681 {
6682 tv->vval.v_dict->dv_copyID = copyID;
6683 *tofree = dict2string(tv, copyID);
6684 r = *tofree;
6685 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006686 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006687
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006688 case VAR_STRING:
6689 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006690 *tofree = NULL;
6691 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006692 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006693
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006694 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006695 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006696 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006697 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006698
6699 --recurse;
6700 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006701}
6702
6703/*
6704 * Return a string with the string representation of a variable.
6705 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6706 * "numbuf" is used for a number.
6707 * Puts quotes around strings, so that they can be parsed back by eval().
6708 * May return NULL;
6709 */
6710 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006711tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006712 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006713 char_u **tofree;
6714 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006715 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006716{
6717 switch (tv->v_type)
6718 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006719 case VAR_FUNC:
6720 *tofree = string_quote(tv->vval.v_string, TRUE);
6721 return *tofree;
6722 case VAR_STRING:
6723 *tofree = string_quote(tv->vval.v_string, FALSE);
6724 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006725 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006726 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006727 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006728 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006729 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006730 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006731 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006732 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006733}
6734
6735/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006736 * Return string "str" in ' quotes, doubling ' characters.
6737 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006738 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006739 */
6740 static char_u *
6741string_quote(str, function)
6742 char_u *str;
6743 int function;
6744{
Bram Moolenaar33570922005-01-25 22:26:29 +00006745 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006746 char_u *p, *r, *s;
6747
Bram Moolenaar33570922005-01-25 22:26:29 +00006748 len = (function ? 13 : 3);
6749 if (str != NULL)
6750 {
6751 len += STRLEN(str);
6752 for (p = str; *p != NUL; mb_ptr_adv(p))
6753 if (*p == '\'')
6754 ++len;
6755 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006756 s = r = alloc(len);
6757 if (r != NULL)
6758 {
6759 if (function)
6760 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006761 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006762 r += 10;
6763 }
6764 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006765 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006766 if (str != NULL)
6767 for (p = str; *p != NUL; )
6768 {
6769 if (*p == '\'')
6770 *r++ = '\'';
6771 MB_COPY_CHAR(p, r);
6772 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006773 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006774 if (function)
6775 *r++ = ')';
6776 *r++ = NUL;
6777 }
6778 return s;
6779}
6780
6781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 * Get the value of an environment variable.
6783 * "arg" is pointing to the '$'. It is advanced to after the name.
6784 * If the environment variable was not set, silently assume it is empty.
6785 * Always return OK.
6786 */
6787 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006788get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 int evaluate;
6792{
6793 char_u *string = NULL;
6794 int len;
6795 int cc;
6796 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006797 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
6799 ++*arg;
6800 name = *arg;
6801 len = get_env_len(arg);
6802 if (evaluate)
6803 {
6804 if (len != 0)
6805 {
6806 cc = name[len];
6807 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006808 /* first try vim_getenv(), fast for normal environment vars */
6809 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006811 {
6812 if (!mustfree)
6813 string = vim_strsave(string);
6814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 else
6816 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006817 if (mustfree)
6818 vim_free(string);
6819
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 /* next try expanding things like $VIM and ${HOME} */
6821 string = expand_env_save(name - 1);
6822 if (string != NULL && *string == '$')
6823 {
6824 vim_free(string);
6825 string = NULL;
6826 }
6827 }
6828 name[len] = cc;
6829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006830 rettv->v_type = VAR_STRING;
6831 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 }
6833
6834 return OK;
6835}
6836
6837/*
6838 * Array with names and number of arguments of all internal functions
6839 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6840 */
6841static struct fst
6842{
6843 char *f_name; /* function name */
6844 char f_min_argc; /* minimal number of arguments */
6845 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006846 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006847 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848} functions[] =
6849{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006850 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {"append", 2, 2, f_append},
6852 {"argc", 0, 0, f_argc},
6853 {"argidx", 0, 0, f_argidx},
6854 {"argv", 1, 1, f_argv},
6855 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006856 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 {"bufexists", 1, 1, f_bufexists},
6858 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6859 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6860 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6861 {"buflisted", 1, 1, f_buflisted},
6862 {"bufloaded", 1, 1, f_bufloaded},
6863 {"bufname", 1, 1, f_bufname},
6864 {"bufnr", 1, 1, f_bufnr},
6865 {"bufwinnr", 1, 1, f_bufwinnr},
6866 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006867 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006868 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 {"char2nr", 1, 1, f_char2nr},
6870 {"cindent", 1, 1, f_cindent},
6871 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006872#if defined(FEAT_INS_EXPAND)
6873 {"complete_add", 1, 1, f_complete_add},
6874 {"complete_check", 0, 0, f_complete_check},
6875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006877 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006878 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00006880 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006881 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {"delete", 1, 1, f_delete},
6883 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006884 {"diff_filler", 1, 1, f_diff_filler},
6885 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006886 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006888 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {"eventhandler", 0, 0, f_eventhandler},
6890 {"executable", 1, 1, f_executable},
6891 {"exists", 1, 1, f_exists},
6892 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006893 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6895 {"filereadable", 1, 1, f_filereadable},
6896 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006897 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006898 {"finddir", 1, 3, f_finddir},
6899 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {"fnamemodify", 2, 2, f_fnamemodify},
6901 {"foldclosed", 1, 1, f_foldclosed},
6902 {"foldclosedend", 1, 1, f_foldclosedend},
6903 {"foldlevel", 1, 1, f_foldlevel},
6904 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006905 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006907 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006909 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006910 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 {"getbufvar", 2, 2, f_getbufvar},
6912 {"getchar", 0, 1, f_getchar},
6913 {"getcharmod", 0, 0, f_getcharmod},
6914 {"getcmdline", 0, 0, f_getcmdline},
6915 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006916 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006918 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006919 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 {"getfsize", 1, 1, f_getfsize},
6921 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006922 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006923 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006924 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00006925 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006926 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006927 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {"getregtype", 0, 1, f_getregtype},
6929 {"getwinposx", 0, 0, f_getwinposx},
6930 {"getwinposy", 0, 0, f_getwinposy},
6931 {"getwinvar", 2, 2, f_getwinvar},
6932 {"glob", 1, 1, f_glob},
6933 {"globpath", 2, 2, f_globpath},
6934 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006935 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {"hasmapto", 1, 2, f_hasmapto},
6937 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6938 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6939 {"histadd", 2, 2, f_histadd},
6940 {"histdel", 1, 2, f_histdel},
6941 {"histget", 1, 2, f_histget},
6942 {"histnr", 1, 1, f_histnr},
6943 {"hlID", 1, 1, f_hlID},
6944 {"hlexists", 1, 1, f_hlexists},
6945 {"hostname", 0, 0, f_hostname},
6946 {"iconv", 3, 3, f_iconv},
6947 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006948 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006949 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006951 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 {"inputrestore", 0, 0, f_inputrestore},
6953 {"inputsave", 0, 0, f_inputsave},
6954 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006955 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006957 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006958 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006959 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006960 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006962 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 {"libcall", 3, 3, f_libcall},
6964 {"libcallnr", 3, 3, f_libcallnr},
6965 {"line", 1, 1, f_line},
6966 {"line2byte", 1, 1, f_line2byte},
6967 {"lispindent", 1, 1, f_lispindent},
6968 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006969 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 {"maparg", 1, 2, f_maparg},
6971 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006972 {"match", 2, 4, f_match},
6973 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006974 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006975 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006976 {"max", 1, 1, f_max},
6977 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006978#ifdef vim_mkdir
6979 {"mkdir", 1, 3, f_mkdir},
6980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 {"mode", 0, 0, f_mode},
6982 {"nextnonblank", 1, 1, f_nextnonblank},
6983 {"nr2char", 1, 1, f_nr2char},
6984 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006985 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006986 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006987 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006988 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 {"remote_expr", 2, 3, f_remote_expr},
6990 {"remote_foreground", 1, 1, f_remote_foreground},
6991 {"remote_peek", 1, 2, f_remote_peek},
6992 {"remote_read", 1, 1, f_remote_read},
6993 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006994 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006996 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006998 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006999 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007000 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007001 {"searchpair", 3, 6, f_searchpair},
7002 {"searchpairpos", 3, 6, f_searchpairpos},
7003 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 {"server2client", 2, 2, f_server2client},
7005 {"serverlist", 0, 0, f_serverlist},
7006 {"setbufvar", 3, 3, f_setbufvar},
7007 {"setcmdpos", 1, 1, f_setcmdpos},
7008 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007009 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007010 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {"setreg", 2, 3, f_setreg},
7012 {"setwinvar", 3, 3, f_setwinvar},
7013 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007014 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007015 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007016 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007017 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007018 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019#ifdef HAVE_STRFTIME
7020 {"strftime", 1, 2, f_strftime},
7021#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007022 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007023 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 {"strlen", 1, 1, f_strlen},
7025 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007026 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {"strtrans", 1, 1, f_strtrans},
7028 {"submatch", 1, 1, f_submatch},
7029 {"substitute", 4, 4, f_substitute},
7030 {"synID", 3, 3, f_synID},
7031 {"synIDattr", 2, 3, f_synIDattr},
7032 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007033 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007034 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007035 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007036 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007037 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007038 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007040 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 {"tolower", 1, 1, f_tolower},
7042 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007043 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007045 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {"virtcol", 1, 1, f_virtcol},
7047 {"visualmode", 0, 1, f_visualmode},
7048 {"winbufnr", 1, 1, f_winbufnr},
7049 {"wincol", 0, 0, f_wincol},
7050 {"winheight", 1, 1, f_winheight},
7051 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007052 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 {"winrestcmd", 0, 0, f_winrestcmd},
7054 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007055 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056};
7057
7058#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7059
7060/*
7061 * Function given to ExpandGeneric() to obtain the list of internal
7062 * or user defined function names.
7063 */
7064 char_u *
7065get_function_name(xp, idx)
7066 expand_T *xp;
7067 int idx;
7068{
7069 static int intidx = -1;
7070 char_u *name;
7071
7072 if (idx == 0)
7073 intidx = -1;
7074 if (intidx < 0)
7075 {
7076 name = get_user_func_name(xp, idx);
7077 if (name != NULL)
7078 return name;
7079 }
7080 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7081 {
7082 STRCPY(IObuff, functions[intidx].f_name);
7083 STRCAT(IObuff, "(");
7084 if (functions[intidx].f_max_argc == 0)
7085 STRCAT(IObuff, ")");
7086 return IObuff;
7087 }
7088
7089 return NULL;
7090}
7091
7092/*
7093 * Function given to ExpandGeneric() to obtain the list of internal or
7094 * user defined variable or function names.
7095 */
7096/*ARGSUSED*/
7097 char_u *
7098get_expr_name(xp, idx)
7099 expand_T *xp;
7100 int idx;
7101{
7102 static int intidx = -1;
7103 char_u *name;
7104
7105 if (idx == 0)
7106 intidx = -1;
7107 if (intidx < 0)
7108 {
7109 name = get_function_name(xp, idx);
7110 if (name != NULL)
7111 return name;
7112 }
7113 return get_user_var_name(xp, ++intidx);
7114}
7115
7116#endif /* FEAT_CMDL_COMPL */
7117
7118/*
7119 * Find internal function in table above.
7120 * Return index, or -1 if not found
7121 */
7122 static int
7123find_internal_func(name)
7124 char_u *name; /* name of the function */
7125{
7126 int first = 0;
7127 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7128 int cmp;
7129 int x;
7130
7131 /*
7132 * Find the function name in the table. Binary search.
7133 */
7134 while (first <= last)
7135 {
7136 x = first + ((unsigned)(last - first) >> 1);
7137 cmp = STRCMP(name, functions[x].f_name);
7138 if (cmp < 0)
7139 last = x - 1;
7140 else if (cmp > 0)
7141 first = x + 1;
7142 else
7143 return x;
7144 }
7145 return -1;
7146}
7147
7148/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007149 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7150 * name it contains, otherwise return "name".
7151 */
7152 static char_u *
7153deref_func_name(name, lenp)
7154 char_u *name;
7155 int *lenp;
7156{
Bram Moolenaar33570922005-01-25 22:26:29 +00007157 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007158 int cc;
7159
7160 cc = name[*lenp];
7161 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007162 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007163 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007164 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007165 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007166 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007167 {
7168 *lenp = 0;
7169 return (char_u *)""; /* just in case */
7170 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007171 *lenp = STRLEN(v->di_tv.vval.v_string);
7172 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007173 }
7174
7175 return name;
7176}
7177
7178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 * Allocate a variable for the result of a function.
7180 * Return OK or FAIL.
7181 */
7182 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007183get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7184 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 char_u *name; /* name of the function */
7186 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 char_u **arg; /* argument, pointing to the '(' */
7189 linenr_T firstline; /* first line of range */
7190 linenr_T lastline; /* last line of range */
7191 int *doesrange; /* return: function handled range */
7192 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007193 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195 char_u *argp;
7196 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007197 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 int argcount = 0; /* number of arguments found */
7199
7200 /*
7201 * Get the arguments.
7202 */
7203 argp = *arg;
7204 while (argcount < MAX_FUNC_ARGS)
7205 {
7206 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7207 if (*argp == ')' || *argp == ',' || *argp == NUL)
7208 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7210 {
7211 ret = FAIL;
7212 break;
7213 }
7214 ++argcount;
7215 if (*argp != ',')
7216 break;
7217 }
7218 if (*argp == ')')
7219 ++argp;
7220 else
7221 ret = FAIL;
7222
7223 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007224 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007225 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007227 {
7228 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007229 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007230 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007231 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233
7234 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007235 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236
7237 *arg = skipwhite(argp);
7238 return ret;
7239}
7240
7241
7242/*
7243 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007244 * Return OK when the function can't be called, FAIL otherwise.
7245 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 */
7247 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007248call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007249 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 char_u *name; /* name of the function */
7251 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 linenr_T firstline; /* first line of range */
7256 linenr_T lastline; /* last line of range */
7257 int *doesrange; /* return: function handled range */
7258 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007259 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
7261 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262#define ERROR_UNKNOWN 0
7263#define ERROR_TOOMANY 1
7264#define ERROR_TOOFEW 2
7265#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007266#define ERROR_DICT 4
7267#define ERROR_NONE 5
7268#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 int error = ERROR_NONE;
7270 int i;
7271 int llen;
7272 ufunc_T *fp;
7273 int cc;
7274#define FLEN_FIXED 40
7275 char_u fname_buf[FLEN_FIXED + 1];
7276 char_u *fname;
7277
7278 /*
7279 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7280 * Change <SNR>123_name() to K_SNR 123_name().
7281 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7282 */
7283 cc = name[len];
7284 name[len] = NUL;
7285 llen = eval_fname_script(name);
7286 if (llen > 0)
7287 {
7288 fname_buf[0] = K_SPECIAL;
7289 fname_buf[1] = KS_EXTRA;
7290 fname_buf[2] = (int)KE_SNR;
7291 i = 3;
7292 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7293 {
7294 if (current_SID <= 0)
7295 error = ERROR_SCRIPT;
7296 else
7297 {
7298 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7299 i = (int)STRLEN(fname_buf);
7300 }
7301 }
7302 if (i + STRLEN(name + llen) < FLEN_FIXED)
7303 {
7304 STRCPY(fname_buf + i, name + llen);
7305 fname = fname_buf;
7306 }
7307 else
7308 {
7309 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7310 if (fname == NULL)
7311 error = ERROR_OTHER;
7312 else
7313 {
7314 mch_memmove(fname, fname_buf, (size_t)i);
7315 STRCPY(fname + i, name + llen);
7316 }
7317 }
7318 }
7319 else
7320 fname = name;
7321
7322 *doesrange = FALSE;
7323
7324
7325 /* execute the function if no errors detected and executing */
7326 if (evaluate && error == ERROR_NONE)
7327 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007328 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 error = ERROR_UNKNOWN;
7330
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007331 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 {
7333 /*
7334 * User defined function.
7335 */
7336 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007337
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007339 /* Trigger FuncUndefined event, may load the function. */
7340 if (fp == NULL
7341 && apply_autocmds(EVENT_FUNCUNDEFINED,
7342 fname, fname, TRUE, NULL)
7343 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007345 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 fp = find_func(fname);
7347 }
7348#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007349 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007350 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007351 {
7352 /* loaded a package, search for the function again */
7353 fp = find_func(fname);
7354 }
7355
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 if (fp != NULL)
7357 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007358 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007360 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007362 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007364 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 else
7367 {
7368 /*
7369 * Call the user function.
7370 * Save and restore search patterns, script variables and
7371 * redo buffer.
7372 */
7373 save_search_patterns();
7374 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007375 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007376 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007377 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007378 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7379 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7380 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007381 /* Function was unreferenced while being used, free it
7382 * now. */
7383 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 restoreRedobuff();
7385 restore_search_patterns();
7386 error = ERROR_NONE;
7387 }
7388 }
7389 }
7390 else
7391 {
7392 /*
7393 * Find the function name in the table, call its implementation.
7394 */
7395 i = find_internal_func(fname);
7396 if (i >= 0)
7397 {
7398 if (argcount < functions[i].f_min_argc)
7399 error = ERROR_TOOFEW;
7400 else if (argcount > functions[i].f_max_argc)
7401 error = ERROR_TOOMANY;
7402 else
7403 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007404 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007405 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 error = ERROR_NONE;
7407 }
7408 }
7409 }
7410 /*
7411 * The function call (or "FuncUndefined" autocommand sequence) might
7412 * have been aborted by an error, an interrupt, or an explicitly thrown
7413 * exception that has not been caught so far. This situation can be
7414 * tested for by calling aborting(). For an error in an internal
7415 * function or for the "E132" error in call_user_func(), however, the
7416 * throw point at which the "force_abort" flag (temporarily reset by
7417 * emsg()) is normally updated has not been reached yet. We need to
7418 * update that flag first to make aborting() reliable.
7419 */
7420 update_force_abort();
7421 }
7422 if (error == ERROR_NONE)
7423 ret = OK;
7424
7425 /*
7426 * Report an error unless the argument evaluation or function call has been
7427 * cancelled due to an aborting error, an interrupt, or an exception.
7428 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429 if (!aborting())
7430 {
7431 switch (error)
7432 {
7433 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007434 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007435 break;
7436 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007437 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438 break;
7439 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007440 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007441 name);
7442 break;
7443 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007444 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007445 name);
7446 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007447 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007448 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007449 name);
7450 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 }
7452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453
7454 name[len] = cc;
7455 if (fname != name && fname != fname_buf)
7456 vim_free(fname);
7457
7458 return ret;
7459}
7460
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007461/*
7462 * Give an error message with a function name. Handle <SNR> things.
7463 */
7464 static void
7465emsg_funcname(msg, name)
7466 char *msg;
7467 char_u *name;
7468{
7469 char_u *p;
7470
7471 if (*name == K_SPECIAL)
7472 p = concat_str((char_u *)"<SNR>", name + 3);
7473 else
7474 p = name;
7475 EMSG2(_(msg), p);
7476 if (p != name)
7477 vim_free(p);
7478}
7479
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480/*********************************************
7481 * Implementation of the built-in functions
7482 */
7483
7484/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007485 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 */
7487 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007488f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007489 typval_T *argvars;
7490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007494 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007495 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007497 if ((l = argvars[0].vval.v_list) != NULL
7498 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7499 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007500 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007501 }
7502 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007503 EMSG(_(e_listreq));
7504}
7505
7506/*
7507 * "append(lnum, string/list)" function
7508 */
7509 static void
7510f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 typval_T *argvars;
7512 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007513{
7514 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007515 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 list_T *l = NULL;
7517 listitem_T *li = NULL;
7518 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007519 long added = 0;
7520
Bram Moolenaar0d660222005-01-07 21:51:51 +00007521 lnum = get_tv_lnum(argvars);
7522 if (lnum >= 0
7523 && lnum <= curbuf->b_ml.ml_line_count
7524 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007525 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007526 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007527 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007528 l = argvars[1].vval.v_list;
7529 if (l == NULL)
7530 return;
7531 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007532 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007533 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007534 for (;;)
7535 {
7536 if (l == NULL)
7537 tv = &argvars[1]; /* append a string */
7538 else if (li == NULL)
7539 break; /* end of list */
7540 else
7541 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007542 line = get_tv_string_chk(tv);
7543 if (line == NULL) /* type error */
7544 {
7545 rettv->vval.v_number = 1; /* Failed */
7546 break;
7547 }
7548 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007549 ++added;
7550 if (l == NULL)
7551 break;
7552 li = li->li_next;
7553 }
7554
7555 appended_lines_mark(lnum, added);
7556 if (curwin->w_cursor.lnum > lnum)
7557 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007559 else
7560 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561}
7562
7563/*
7564 * "argc()" function
7565 */
7566/* ARGSUSED */
7567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007568f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 typval_T *argvars;
7570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007572 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573}
7574
7575/*
7576 * "argidx()" function
7577 */
7578/* ARGSUSED */
7579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007580f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007581 typval_T *argvars;
7582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007584 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585}
7586
7587/*
7588 * "argv(nr)" function
7589 */
7590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007591f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007592 typval_T *argvars;
7593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594{
7595 int idx;
7596
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007597 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007599 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007601 rettv->vval.v_string = NULL;
7602 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603}
7604
7605/*
7606 * "browse(save, title, initdir, default)" function
7607 */
7608/* ARGSUSED */
7609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007610f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007611 typval_T *argvars;
7612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613{
7614#ifdef FEAT_BROWSE
7615 int save;
7616 char_u *title;
7617 char_u *initdir;
7618 char_u *defname;
7619 char_u buf[NUMBUFLEN];
7620 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007621 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007623 save = get_tv_number_chk(&argvars[0], &error);
7624 title = get_tv_string_chk(&argvars[1]);
7625 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7626 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007628 if (error || title == NULL || initdir == NULL || defname == NULL)
7629 rettv->vval.v_string = NULL;
7630 else
7631 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007632 do_browse(save ? BROWSE_SAVE : 0,
7633 title, defname, NULL, initdir, NULL, curbuf);
7634#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007635 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007636#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007637 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007638}
7639
7640/*
7641 * "browsedir(title, initdir)" function
7642 */
7643/* ARGSUSED */
7644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007645f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 typval_T *argvars;
7647 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007648{
7649#ifdef FEAT_BROWSE
7650 char_u *title;
7651 char_u *initdir;
7652 char_u buf[NUMBUFLEN];
7653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007654 title = get_tv_string_chk(&argvars[0]);
7655 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007657 if (title == NULL || initdir == NULL)
7658 rettv->vval.v_string = NULL;
7659 else
7660 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007661 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007663 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007665 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666}
7667
Bram Moolenaar33570922005-01-25 22:26:29 +00007668static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007669
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670/*
7671 * Find a buffer by number or exact name.
7672 */
7673 static buf_T *
7674find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007675 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676{
7677 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007679 if (avar->v_type == VAR_NUMBER)
7680 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007681 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007683 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007684 if (buf == NULL)
7685 {
7686 /* No full path name match, try a match with a URL or a "nofile"
7687 * buffer, these don't use the full path. */
7688 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7689 if (buf->b_fname != NULL
7690 && (path_with_url(buf->b_fname)
7691#ifdef FEAT_QUICKFIX
7692 || bt_nofile(buf)
7693#endif
7694 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007695 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007696 break;
7697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 }
7699 return buf;
7700}
7701
7702/*
7703 * "bufexists(expr)" function
7704 */
7705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007706f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 typval_T *argvars;
7708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007710 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711}
7712
7713/*
7714 * "buflisted(expr)" function
7715 */
7716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007717f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 typval_T *argvars;
7719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
7721 buf_T *buf;
7722
7723 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007724 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725}
7726
7727/*
7728 * "bufloaded(expr)" function
7729 */
7730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007731f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007732 typval_T *argvars;
7733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734{
7735 buf_T *buf;
7736
7737 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007738 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739}
7740
Bram Moolenaar33570922005-01-25 22:26:29 +00007741static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007742
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743/*
7744 * Get buffer by number or pattern.
7745 */
7746 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007747get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007748 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007750 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 int save_magic;
7752 char_u *save_cpo;
7753 buf_T *buf;
7754
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007755 if (tv->v_type == VAR_NUMBER)
7756 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007757 if (tv->v_type != VAR_STRING)
7758 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 if (name == NULL || *name == NUL)
7760 return curbuf;
7761 if (name[0] == '$' && name[1] == NUL)
7762 return lastbuf;
7763
7764 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7765 save_magic = p_magic;
7766 p_magic = TRUE;
7767 save_cpo = p_cpo;
7768 p_cpo = (char_u *)"";
7769
7770 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7771 TRUE, FALSE));
7772
7773 p_magic = save_magic;
7774 p_cpo = save_cpo;
7775
7776 /* If not found, try expanding the name, like done for bufexists(). */
7777 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007778 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779
7780 return buf;
7781}
7782
7783/*
7784 * "bufname(expr)" function
7785 */
7786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007787f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007788 typval_T *argvars;
7789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790{
7791 buf_T *buf;
7792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007793 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007795 buf = get_buf_tv(&argvars[0]);
7796 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007798 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007800 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 --emsg_off;
7802}
7803
7804/*
7805 * "bufnr(expr)" function
7806 */
7807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007808f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007809 typval_T *argvars;
7810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811{
7812 buf_T *buf;
7813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007814 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007816 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007818 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007820 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 --emsg_off;
7822}
7823
7824/*
7825 * "bufwinnr(nr)" function
7826 */
7827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007828f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007829 typval_T *argvars;
7830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831{
7832#ifdef FEAT_WINDOWS
7833 win_T *wp;
7834 int winnr = 0;
7835#endif
7836 buf_T *buf;
7837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007838 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007840 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841#ifdef FEAT_WINDOWS
7842 for (wp = firstwin; wp; wp = wp->w_next)
7843 {
7844 ++winnr;
7845 if (wp->w_buffer == buf)
7846 break;
7847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007848 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007850 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851#endif
7852 --emsg_off;
7853}
7854
7855/*
7856 * "byte2line(byte)" function
7857 */
7858/*ARGSUSED*/
7859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007860f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007861 typval_T *argvars;
7862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863{
7864#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866#else
7867 long boff = 0;
7868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007869 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007873 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 (linenr_T)0, &boff);
7875#endif
7876}
7877
7878/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007879 * "byteidx()" function
7880 */
7881/*ARGSUSED*/
7882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007883f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007884 typval_T *argvars;
7885 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007886{
7887#ifdef FEAT_MBYTE
7888 char_u *t;
7889#endif
7890 char_u *str;
7891 long idx;
7892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007893 str = get_tv_string_chk(&argvars[0]);
7894 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007895 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007896 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007897 return;
7898
7899#ifdef FEAT_MBYTE
7900 t = str;
7901 for ( ; idx > 0; idx--)
7902 {
7903 if (*t == NUL) /* EOL reached */
7904 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007905 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007906 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007907 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007908#else
7909 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007910 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007911#endif
7912}
7913
7914/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007915 * "call(func, arglist)" function
7916 */
7917 static void
7918f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007919 typval_T *argvars;
7920 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007921{
7922 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007923 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007924 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007925 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007926 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007927 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007928
7929 rettv->vval.v_number = 0;
7930 if (argvars[1].v_type != VAR_LIST)
7931 {
7932 EMSG(_(e_listreq));
7933 return;
7934 }
7935 if (argvars[1].vval.v_list == NULL)
7936 return;
7937
7938 if (argvars[0].v_type == VAR_FUNC)
7939 func = argvars[0].vval.v_string;
7940 else
7941 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007942 if (*func == NUL)
7943 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007944
Bram Moolenaare9a41262005-01-15 22:18:47 +00007945 if (argvars[2].v_type != VAR_UNKNOWN)
7946 {
7947 if (argvars[2].v_type != VAR_DICT)
7948 {
7949 EMSG(_(e_dictreq));
7950 return;
7951 }
7952 selfdict = argvars[2].vval.v_dict;
7953 }
7954
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007955 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7956 item = item->li_next)
7957 {
7958 if (argc == MAX_FUNC_ARGS)
7959 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007960 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007961 break;
7962 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007963 /* Make a copy of each argument. This is needed to be able to set
7964 * v_lock to VAR_FIXED in the copy without changing the original list.
7965 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007966 copy_tv(&item->li_tv, &argv[argc++]);
7967 }
7968
7969 if (item == NULL)
7970 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007971 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7972 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007973
7974 /* Free the arguments. */
7975 while (argc > 0)
7976 clear_tv(&argv[--argc]);
7977}
7978
7979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 * "char2nr(string)" function
7981 */
7982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007983f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007984 typval_T *argvars;
7985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986{
7987#ifdef FEAT_MBYTE
7988 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007989 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 else
7991#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007992 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993}
7994
7995/*
7996 * "cindent(lnum)" function
7997 */
7998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007999f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008000 typval_T *argvars;
8001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002{
8003#ifdef FEAT_CINDENT
8004 pos_T pos;
8005 linenr_T lnum;
8006
8007 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008008 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8010 {
8011 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008012 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 curwin->w_cursor = pos;
8014 }
8015 else
8016#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008017 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018}
8019
8020/*
8021 * "col(string)" function
8022 */
8023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008024f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008025 typval_T *argvars;
8026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027{
8028 colnr_T col = 0;
8029 pos_T *fp;
8030
8031 fp = var2fpos(&argvars[0], FALSE);
8032 if (fp != NULL)
8033 {
8034 if (fp->col == MAXCOL)
8035 {
8036 /* '> can be MAXCOL, get the length of the line then */
8037 if (fp->lnum <= curbuf->b_ml.ml_line_count)
8038 col = STRLEN(ml_get(fp->lnum)) + 1;
8039 else
8040 col = MAXCOL;
8041 }
8042 else
8043 {
8044 col = fp->col + 1;
8045#ifdef FEAT_VIRTUALEDIT
8046 /* col(".") when the cursor is on the NUL at the end of the line
8047 * because of "coladd" can be seen as an extra column. */
8048 if (virtual_active() && fp == &curwin->w_cursor)
8049 {
8050 char_u *p = ml_get_cursor();
8051
8052 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8053 curwin->w_virtcol - curwin->w_cursor.coladd))
8054 {
8055# ifdef FEAT_MBYTE
8056 int l;
8057
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008058 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 col += l;
8060# else
8061 if (*p != NUL && p[1] == NUL)
8062 ++col;
8063# endif
8064 }
8065 }
8066#endif
8067 }
8068 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008069 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070}
8071
Bram Moolenaar572cb562005-08-05 21:35:02 +00008072#if defined(FEAT_INS_EXPAND)
8073/*
8074 * "complete_add()" function
8075 */
8076/*ARGSUSED*/
8077 static void
8078f_complete_add(argvars, rettv)
8079 typval_T *argvars;
8080 typval_T *rettv;
8081{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008082 char_u *word;
8083 char_u *extra = NULL;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008084 int icase = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008085
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008086 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8087 {
8088 word = get_dict_string(argvars[0].vval.v_dict,
8089 (char_u *)"word", FALSE);
8090 extra = get_dict_string(argvars[0].vval.v_dict,
8091 (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008092 icase = get_dict_number(argvars[0].vval.v_dict, (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008093 }
8094 else
8095 word = get_tv_string_chk(&argvars[0]);
8096 if (word != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008097 rettv->vval.v_number = ins_compl_add(word, -1, icase,
8098 NULL, extra, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008099}
8100
8101/*
8102 * "complete_check()" function
8103 */
8104/*ARGSUSED*/
8105 static void
8106f_complete_check(argvars, rettv)
8107 typval_T *argvars;
8108 typval_T *rettv;
8109{
8110 int saved = RedrawingDisabled;
8111
8112 RedrawingDisabled = 0;
8113 ins_compl_check_keys(0);
8114 rettv->vval.v_number = compl_interrupted;
8115 RedrawingDisabled = saved;
8116}
8117#endif
8118
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119/*
8120 * "confirm(message, buttons[, default [, type]])" function
8121 */
8122/*ARGSUSED*/
8123 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008124f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008125 typval_T *argvars;
8126 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127{
8128#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8129 char_u *message;
8130 char_u *buttons = NULL;
8131 char_u buf[NUMBUFLEN];
8132 char_u buf2[NUMBUFLEN];
8133 int def = 1;
8134 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008135 char_u *typestr;
8136 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008138 message = get_tv_string_chk(&argvars[0]);
8139 if (message == NULL)
8140 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008141 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008143 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8144 if (buttons == NULL)
8145 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008146 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008148 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008149 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008151 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8152 if (typestr == NULL)
8153 error = TRUE;
8154 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008156 switch (TOUPPER_ASC(*typestr))
8157 {
8158 case 'E': type = VIM_ERROR; break;
8159 case 'Q': type = VIM_QUESTION; break;
8160 case 'I': type = VIM_INFO; break;
8161 case 'W': type = VIM_WARNING; break;
8162 case 'G': type = VIM_GENERIC; break;
8163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 }
8165 }
8166 }
8167 }
8168
8169 if (buttons == NULL || *buttons == NUL)
8170 buttons = (char_u *)_("&Ok");
8171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008172 if (error)
8173 rettv->vval.v_number = 0;
8174 else
8175 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 def, NULL);
8177#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008178 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179#endif
8180}
8181
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008182/*
8183 * "copy()" function
8184 */
8185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008186f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008187 typval_T *argvars;
8188 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008189{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008190 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008191}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192
8193/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008194 * "count()" function
8195 */
8196 static void
8197f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008198 typval_T *argvars;
8199 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008200{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008201 long n = 0;
8202 int ic = FALSE;
8203
Bram Moolenaare9a41262005-01-15 22:18:47 +00008204 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008205 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008206 listitem_T *li;
8207 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008208 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008209
Bram Moolenaare9a41262005-01-15 22:18:47 +00008210 if ((l = argvars[0].vval.v_list) != NULL)
8211 {
8212 li = l->lv_first;
8213 if (argvars[2].v_type != VAR_UNKNOWN)
8214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008215 int error = FALSE;
8216
8217 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008218 if (argvars[3].v_type != VAR_UNKNOWN)
8219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008220 idx = get_tv_number_chk(&argvars[3], &error);
8221 if (!error)
8222 {
8223 li = list_find(l, idx);
8224 if (li == NULL)
8225 EMSGN(_(e_listidx), idx);
8226 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008227 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008228 if (error)
8229 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008230 }
8231
8232 for ( ; li != NULL; li = li->li_next)
8233 if (tv_equal(&li->li_tv, &argvars[1], ic))
8234 ++n;
8235 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008236 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008237 else if (argvars[0].v_type == VAR_DICT)
8238 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008239 int todo;
8240 dict_T *d;
8241 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008242
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008243 if ((d = argvars[0].vval.v_dict) != NULL)
8244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008245 int error = FALSE;
8246
Bram Moolenaare9a41262005-01-15 22:18:47 +00008247 if (argvars[2].v_type != VAR_UNKNOWN)
8248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008249 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008250 if (argvars[3].v_type != VAR_UNKNOWN)
8251 EMSG(_(e_invarg));
8252 }
8253
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008254 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008255 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008256 {
8257 if (!HASHITEM_EMPTY(hi))
8258 {
8259 --todo;
8260 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8261 ++n;
8262 }
8263 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008264 }
8265 }
8266 else
8267 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008268 rettv->vval.v_number = n;
8269}
8270
8271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8273 *
8274 * Checks the existence of a cscope connection.
8275 */
8276/*ARGSUSED*/
8277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008278f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008279 typval_T *argvars;
8280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281{
8282#ifdef FEAT_CSCOPE
8283 int num = 0;
8284 char_u *dbpath = NULL;
8285 char_u *prepend = NULL;
8286 char_u buf[NUMBUFLEN];
8287
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008288 if (argvars[0].v_type != VAR_UNKNOWN
8289 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008291 num = (int)get_tv_number(&argvars[0]);
8292 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008293 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008294 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 }
8296
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008297 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008299 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300#endif
8301}
8302
8303/*
8304 * "cursor(lnum, col)" function
8305 *
8306 * Moves the cursor to the specified line and column
8307 */
8308/*ARGSUSED*/
8309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008310f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008311 typval_T *argvars;
8312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313{
8314 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008315#ifdef FEAT_VIRTUALEDIT
8316 long coladd = 0;
8317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318
Bram Moolenaara5525202006-03-02 22:52:09 +00008319 if (argvars[1].v_type == VAR_UNKNOWN)
8320 {
8321 list_T *l = argvars->vval.v_list;
8322
8323 /* Argument can be [lnum, col, coladd]. */
8324 if (argvars->v_type != VAR_LIST || l == NULL)
8325 return;
8326 line = list_find_nr(l, 0L, NULL);
8327 col = list_find_nr(l, 1L, NULL);
8328#ifdef FEAT_VIRTUALEDIT
8329 coladd = list_find_nr(l, 2L, NULL);
8330 if (coladd < 0)
8331 coladd = 0;
8332#endif
8333 }
8334 else
8335 {
8336 line = get_tv_lnum(argvars);
8337 col = get_tv_number_chk(&argvars[1], NULL);
8338#ifdef FEAT_VIRTUALEDIT
8339 if (argvars[2].v_type != VAR_UNKNOWN)
8340 coladd = get_tv_number_chk(&argvars[2], NULL);
8341#endif
8342 }
8343 if (line < 0 || col < 0
8344#ifdef FEAT_VIRTUALEDIT
8345 || coladd < 0
8346#endif
8347 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008348 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 if (line > 0)
8350 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 if (col > 0)
8352 curwin->w_cursor.col = col - 1;
8353#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008354 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355#endif
8356
8357 /* Make sure the cursor is in a valid position. */
8358 check_cursor();
8359#ifdef FEAT_MBYTE
8360 /* Correct cursor for multi-byte character. */
8361 if (has_mbyte)
8362 mb_adjust_cursor();
8363#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008364
8365 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366}
8367
8368/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008369 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 */
8371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008372f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008373 typval_T *argvars;
8374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008376 int noref = 0;
8377
8378 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008379 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008380 if (noref < 0 || noref > 1)
8381 EMSG(_(e_invarg));
8382 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008383 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384}
8385
8386/*
8387 * "delete()" function
8388 */
8389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008390f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008391 typval_T *argvars;
8392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393{
8394 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008395 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008397 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398}
8399
8400/*
8401 * "did_filetype()" function
8402 */
8403/*ARGSUSED*/
8404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008405f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008406 typval_T *argvars;
8407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408{
8409#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008410 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008412 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413#endif
8414}
8415
8416/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008417 * "diff_filler()" function
8418 */
8419/*ARGSUSED*/
8420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008421f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008422 typval_T *argvars;
8423 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008424{
8425#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008426 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008427#endif
8428}
8429
8430/*
8431 * "diff_hlID()" function
8432 */
8433/*ARGSUSED*/
8434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008435f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008436 typval_T *argvars;
8437 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008438{
8439#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008440 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008441 static linenr_T prev_lnum = 0;
8442 static int changedtick = 0;
8443 static int fnum = 0;
8444 static int change_start = 0;
8445 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008446 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008447 int filler_lines;
8448 int col;
8449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008450 if (lnum < 0) /* ignore type error in {lnum} arg */
8451 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008452 if (lnum != prev_lnum
8453 || changedtick != curbuf->b_changedtick
8454 || fnum != curbuf->b_fnum)
8455 {
8456 /* New line, buffer, change: need to get the values. */
8457 filler_lines = diff_check(curwin, lnum);
8458 if (filler_lines < 0)
8459 {
8460 if (filler_lines == -1)
8461 {
8462 change_start = MAXCOL;
8463 change_end = -1;
8464 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8465 hlID = HLF_ADD; /* added line */
8466 else
8467 hlID = HLF_CHD; /* changed line */
8468 }
8469 else
8470 hlID = HLF_ADD; /* added line */
8471 }
8472 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008473 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008474 prev_lnum = lnum;
8475 changedtick = curbuf->b_changedtick;
8476 fnum = curbuf->b_fnum;
8477 }
8478
8479 if (hlID == HLF_CHD || hlID == HLF_TXD)
8480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008481 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008482 if (col >= change_start && col <= change_end)
8483 hlID = HLF_TXD; /* changed text */
8484 else
8485 hlID = HLF_CHD; /* changed line */
8486 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008487 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008488#endif
8489}
8490
8491/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008492 * "empty({expr})" function
8493 */
8494 static void
8495f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008496 typval_T *argvars;
8497 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008498{
8499 int n;
8500
8501 switch (argvars[0].v_type)
8502 {
8503 case VAR_STRING:
8504 case VAR_FUNC:
8505 n = argvars[0].vval.v_string == NULL
8506 || *argvars[0].vval.v_string == NUL;
8507 break;
8508 case VAR_NUMBER:
8509 n = argvars[0].vval.v_number == 0;
8510 break;
8511 case VAR_LIST:
8512 n = argvars[0].vval.v_list == NULL
8513 || argvars[0].vval.v_list->lv_first == NULL;
8514 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008515 case VAR_DICT:
8516 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008517 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008518 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008519 default:
8520 EMSG2(_(e_intern2), "f_empty()");
8521 n = 0;
8522 }
8523
8524 rettv->vval.v_number = n;
8525}
8526
8527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 * "escape({string}, {chars})" function
8529 */
8530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008531f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008532 typval_T *argvars;
8533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534{
8535 char_u buf[NUMBUFLEN];
8536
Bram Moolenaar758711c2005-02-02 23:11:38 +00008537 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8538 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008539 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540}
8541
8542/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008543 * "eval()" function
8544 */
8545/*ARGSUSED*/
8546 static void
8547f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 typval_T *argvars;
8549 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008550{
8551 char_u *s;
8552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008553 s = get_tv_string_chk(&argvars[0]);
8554 if (s != NULL)
8555 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008556
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008557 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8558 {
8559 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008560 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008561 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008562 else if (*s != NUL)
8563 EMSG(_(e_trailing));
8564}
8565
8566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 * "eventhandler()" function
8568 */
8569/*ARGSUSED*/
8570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008571f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008572 typval_T *argvars;
8573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008575 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576}
8577
8578/*
8579 * "executable()" function
8580 */
8581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008582f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008583 typval_T *argvars;
8584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587}
8588
8589/*
8590 * "exists()" function
8591 */
8592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008593f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008594 typval_T *argvars;
8595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 char_u *p;
8598 char_u *name;
8599 int n = FALSE;
8600 int len = 0;
8601
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008602 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 if (*p == '$') /* environment variable */
8604 {
8605 /* first try "normal" environment variables (fast) */
8606 if (mch_getenv(p + 1) != NULL)
8607 n = TRUE;
8608 else
8609 {
8610 /* try expanding things like $VIM and ${HOME} */
8611 p = expand_env_save(p);
8612 if (p != NULL && *p != '$')
8613 n = TRUE;
8614 vim_free(p);
8615 }
8616 }
8617 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008618 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 else if (*p == '*') /* internal or user defined function */
8620 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008621 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 }
8623 else if (*p == ':')
8624 {
8625 n = cmd_exists(p + 1);
8626 }
8627 else if (*p == '#')
8628 {
8629#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008630 if (p[1] == '#')
8631 n = autocmd_supported(p + 2);
8632 else
8633 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634#endif
8635 }
8636 else /* internal variable */
8637 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008638 char_u *tofree;
8639 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008641 /* get_name_len() takes care of expanding curly braces */
8642 name = p;
8643 len = get_name_len(&p, &tofree, TRUE, FALSE);
8644 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008646 if (tofree != NULL)
8647 name = tofree;
8648 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8649 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008651 /* handle d.key, l[idx], f(expr) */
8652 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8653 if (n)
8654 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 }
8656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008658 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 }
8660
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008661 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662}
8663
8664/*
8665 * "expand()" function
8666 */
8667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008668f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008669 typval_T *argvars;
8670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671{
8672 char_u *s;
8673 int len;
8674 char_u *errormsg;
8675 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8676 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008677 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679 rettv->v_type = VAR_STRING;
8680 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 if (*s == '%' || *s == '#' || *s == '<')
8682 {
8683 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008684 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 --emsg_off;
8686 }
8687 else
8688 {
8689 /* When the optional second argument is non-zero, don't remove matches
8690 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008691 if (argvars[1].v_type != VAR_UNKNOWN
8692 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008694 if (!error)
8695 {
8696 ExpandInit(&xpc);
8697 xpc.xp_context = EXPAND_FILES;
8698 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8699 ExpandCleanup(&xpc);
8700 }
8701 else
8702 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 }
8704}
8705
8706/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008707 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008708 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008709 */
8710 static void
8711f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008712 typval_T *argvars;
8713 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008714{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008715 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008716 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008717 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008718 list_T *l1, *l2;
8719 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008720 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008721 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008722
Bram Moolenaare9a41262005-01-15 22:18:47 +00008723 l1 = argvars[0].vval.v_list;
8724 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008725 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8726 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008727 {
8728 if (argvars[2].v_type != VAR_UNKNOWN)
8729 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008730 before = get_tv_number_chk(&argvars[2], &error);
8731 if (error)
8732 return; /* type error; errmsg already given */
8733
Bram Moolenaar758711c2005-02-02 23:11:38 +00008734 if (before == l1->lv_len)
8735 item = NULL;
8736 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008737 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008738 item = list_find(l1, before);
8739 if (item == NULL)
8740 {
8741 EMSGN(_(e_listidx), before);
8742 return;
8743 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008744 }
8745 }
8746 else
8747 item = NULL;
8748 list_extend(l1, l2, item);
8749
Bram Moolenaare9a41262005-01-15 22:18:47 +00008750 copy_tv(&argvars[0], rettv);
8751 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008752 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008753 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8754 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008755 dict_T *d1, *d2;
8756 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008757 char_u *action;
8758 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008759 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008760 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008761
8762 d1 = argvars[0].vval.v_dict;
8763 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008764 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8765 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008766 {
8767 /* Check the third argument. */
8768 if (argvars[2].v_type != VAR_UNKNOWN)
8769 {
8770 static char *(av[]) = {"keep", "force", "error"};
8771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008772 action = get_tv_string_chk(&argvars[2]);
8773 if (action == NULL)
8774 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008775 for (i = 0; i < 3; ++i)
8776 if (STRCMP(action, av[i]) == 0)
8777 break;
8778 if (i == 3)
8779 {
8780 EMSGN(_(e_invarg2), action);
8781 return;
8782 }
8783 }
8784 else
8785 action = (char_u *)"force";
8786
8787 /* Go over all entries in the second dict and add them to the
8788 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008789 todo = d2->dv_hashtab.ht_used;
8790 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008791 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008792 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008793 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008794 --todo;
8795 di1 = dict_find(d1, hi2->hi_key, -1);
8796 if (di1 == NULL)
8797 {
8798 di1 = dictitem_copy(HI2DI(hi2));
8799 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8800 dictitem_free(di1);
8801 }
8802 else if (*action == 'e')
8803 {
8804 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8805 break;
8806 }
8807 else if (*action == 'f')
8808 {
8809 clear_tv(&di1->di_tv);
8810 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8811 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008812 }
8813 }
8814
Bram Moolenaare9a41262005-01-15 22:18:47 +00008815 copy_tv(&argvars[0], rettv);
8816 }
8817 }
8818 else
8819 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008820}
8821
8822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 * "filereadable()" function
8824 */
8825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008826f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008827 typval_T *argvars;
8828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829{
8830 FILE *fd;
8831 char_u *p;
8832 int n;
8833
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008834 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8836 {
8837 n = TRUE;
8838 fclose(fd);
8839 }
8840 else
8841 n = FALSE;
8842
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008843 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844}
8845
8846/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008847 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 * rights to write into.
8849 */
8850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008851f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008852 typval_T *argvars;
8853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008855 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856}
8857
Bram Moolenaar33570922005-01-25 22:26:29 +00008858static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008859
8860 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008861findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008862 typval_T *argvars;
8863 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008864 int dir;
8865{
8866#ifdef FEAT_SEARCHPATH
8867 char_u *fname;
8868 char_u *fresult = NULL;
8869 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8870 char_u *p;
8871 char_u pathbuf[NUMBUFLEN];
8872 int count = 1;
8873 int first = TRUE;
8874
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008875 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008876
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008877 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008879 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8880 if (p == NULL)
8881 count = -1; /* error */
8882 else
8883 {
8884 if (*p != NUL)
8885 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008887 if (argvars[2].v_type != VAR_UNKNOWN)
8888 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8889 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008890 }
8891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008892 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008893 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008894 do
8895 {
8896 vim_free(fresult);
8897 fresult = find_file_in_path_option(first ? fname : NULL,
8898 first ? (int)STRLEN(fname) : 0,
8899 0, first, path, dir, NULL);
8900 first = FALSE;
8901 } while (--count > 0 && fresult != NULL);
8902 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008903
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008904 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008905#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008906 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008907#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008908 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008909}
8910
Bram Moolenaar33570922005-01-25 22:26:29 +00008911static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8912static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008913
8914/*
8915 * Implementation of map() and filter().
8916 */
8917 static void
8918filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008919 typval_T *argvars;
8920 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008921 int map;
8922{
8923 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008924 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008925 listitem_T *li, *nli;
8926 list_T *l = NULL;
8927 dictitem_T *di;
8928 hashtab_T *ht;
8929 hashitem_T *hi;
8930 dict_T *d = NULL;
8931 typval_T save_val;
8932 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008933 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008934 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008935 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar280f1262006-01-30 00:14:18 +00008936 int save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008937
8938 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008939 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008940 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008941 if ((l = argvars[0].vval.v_list) == NULL
8942 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008943 return;
8944 }
8945 else if (argvars[0].v_type == VAR_DICT)
8946 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008947 if ((d = argvars[0].vval.v_dict) == NULL
8948 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008949 return;
8950 }
8951 else
8952 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008953 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008954 return;
8955 }
8956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008957 expr = get_tv_string_buf_chk(&argvars[1], buf);
8958 /* On type errors, the preceding call has already displayed an error
8959 * message. Avoid a misleading error message for an empty string that
8960 * was not passed as argument. */
8961 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008963 prepare_vimvar(VV_VAL, &save_val);
8964 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008965
Bram Moolenaar280f1262006-01-30 00:14:18 +00008966 /* We reset "called_emsg" to be able to detect whether an error
8967 * occurred during evaluation of the expression. "did_emsg" can't be
8968 * used, because it is reset when calling a function. */
8969 save_called_emsg = called_emsg;
8970 called_emsg = FALSE;
8971
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008972 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008974 prepare_vimvar(VV_KEY, &save_key);
8975 vimvars[VV_KEY].vv_type = VAR_STRING;
8976
8977 ht = &d->dv_hashtab;
8978 hash_lock(ht);
8979 todo = ht->ht_used;
8980 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008981 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008982 if (!HASHITEM_EMPTY(hi))
8983 {
8984 --todo;
8985 di = HI2DI(hi);
8986 if (tv_check_lock(di->di_tv.v_lock, msg))
8987 break;
8988 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008989 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
8990 || called_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008991 break;
8992 if (!map && rem)
8993 dictitem_remove(d, di);
8994 clear_tv(&vimvars[VV_KEY].vv_tv);
8995 }
8996 }
8997 hash_unlock(ht);
8998
8999 restore_vimvar(VV_KEY, &save_key);
9000 }
9001 else
9002 {
9003 for (li = l->lv_first; li != NULL; li = nli)
9004 {
9005 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009006 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009007 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009008 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
9009 || called_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009010 break;
9011 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009012 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009013 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009014 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009016 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009017
9018 called_emsg |= save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009019 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009020
9021 copy_tv(&argvars[0], rettv);
9022}
9023
9024 static int
9025filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009026 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009027 char_u *expr;
9028 int map;
9029 int *remp;
9030{
Bram Moolenaar33570922005-01-25 22:26:29 +00009031 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009032 char_u *s;
9033
Bram Moolenaar33570922005-01-25 22:26:29 +00009034 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009035 s = expr;
9036 if (eval1(&s, &rettv, TRUE) == FAIL)
9037 return FAIL;
9038 if (*s != NUL) /* check for trailing chars after expr */
9039 {
9040 EMSG2(_(e_invexpr2), s);
9041 return FAIL;
9042 }
9043 if (map)
9044 {
9045 /* map(): replace the list item value */
9046 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009047 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009048 *tv = rettv;
9049 }
9050 else
9051 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009052 int error = FALSE;
9053
Bram Moolenaare9a41262005-01-15 22:18:47 +00009054 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009055 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009056 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009057 /* On type error, nothing has been removed; return FAIL to stop the
9058 * loop. The error message was given by get_tv_number_chk(). */
9059 if (error)
9060 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009061 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009063 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009064}
9065
9066/*
9067 * "filter()" function
9068 */
9069 static void
9070f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009071 typval_T *argvars;
9072 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009073{
9074 filter_map(argvars, rettv, FALSE);
9075}
9076
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009077/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009078 * "finddir({fname}[, {path}[, {count}]])" function
9079 */
9080 static void
9081f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009082 typval_T *argvars;
9083 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009084{
9085 findfilendir(argvars, rettv, TRUE);
9086}
9087
9088/*
9089 * "findfile({fname}[, {path}[, {count}]])" function
9090 */
9091 static void
9092f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009093 typval_T *argvars;
9094 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009095{
9096 findfilendir(argvars, rettv, FALSE);
9097}
9098
9099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 * "fnamemodify({fname}, {mods})" function
9101 */
9102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009104 typval_T *argvars;
9105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
9107 char_u *fname;
9108 char_u *mods;
9109 int usedlen = 0;
9110 int len;
9111 char_u *fbuf = NULL;
9112 char_u buf[NUMBUFLEN];
9113
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009114 fname = get_tv_string_chk(&argvars[0]);
9115 mods = get_tv_string_buf_chk(&argvars[1], buf);
9116 if (fname == NULL || mods == NULL)
9117 fname = NULL;
9118 else
9119 {
9120 len = (int)STRLEN(fname);
9121 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009124 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009126 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009128 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 vim_free(fbuf);
9130}
9131
Bram Moolenaar33570922005-01-25 22:26:29 +00009132static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133
9134/*
9135 * "foldclosed()" function
9136 */
9137 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009138foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009139 typval_T *argvars;
9140 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 int end;
9142{
9143#ifdef FEAT_FOLDING
9144 linenr_T lnum;
9145 linenr_T first, last;
9146
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009147 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9149 {
9150 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9151 {
9152 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009155 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 return;
9157 }
9158 }
9159#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009160 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161}
9162
9163/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009164 * "foldclosed()" function
9165 */
9166 static void
9167f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009168 typval_T *argvars;
9169 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009170{
9171 foldclosed_both(argvars, rettv, FALSE);
9172}
9173
9174/*
9175 * "foldclosedend()" function
9176 */
9177 static void
9178f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009179 typval_T *argvars;
9180 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009181{
9182 foldclosed_both(argvars, rettv, TRUE);
9183}
9184
9185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 * "foldlevel()" function
9187 */
9188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009190 typval_T *argvars;
9191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192{
9193#ifdef FEAT_FOLDING
9194 linenr_T lnum;
9195
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 else
9200#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009201 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202}
9203
9204/*
9205 * "foldtext()" function
9206 */
9207/*ARGSUSED*/
9208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009209f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009210 typval_T *argvars;
9211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212{
9213#ifdef FEAT_FOLDING
9214 linenr_T lnum;
9215 char_u *s;
9216 char_u *r;
9217 int len;
9218 char *txt;
9219#endif
9220
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009221 rettv->v_type = VAR_STRING;
9222 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009224 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9225 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9226 <= curbuf->b_ml.ml_line_count
9227 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 {
9229 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009230 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9231 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 {
9233 if (!linewhite(lnum))
9234 break;
9235 ++lnum;
9236 }
9237
9238 /* Find interesting text in this line. */
9239 s = skipwhite(ml_get(lnum));
9240 /* skip C comment-start */
9241 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009244 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009245 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009246 {
9247 s = skipwhite(ml_get(lnum + 1));
9248 if (*s == '*')
9249 s = skipwhite(s + 1);
9250 }
9251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 txt = _("+-%s%3ld lines: ");
9253 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009254 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 + 20 /* for %3ld */
9256 + STRLEN(s))); /* concatenated */
9257 if (r != NULL)
9258 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009259 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9260 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9261 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 len = (int)STRLEN(r);
9263 STRCAT(r, s);
9264 /* remove 'foldmarker' and 'commentstring' */
9265 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009266 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 }
9268 }
9269#endif
9270}
9271
9272/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009273 * "foldtextresult(lnum)" function
9274 */
9275/*ARGSUSED*/
9276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009277f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009278 typval_T *argvars;
9279 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009280{
9281#ifdef FEAT_FOLDING
9282 linenr_T lnum;
9283 char_u *text;
9284 char_u buf[51];
9285 foldinfo_T foldinfo;
9286 int fold_count;
9287#endif
9288
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009289 rettv->v_type = VAR_STRING;
9290 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009291#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009292 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009293 /* treat illegal types and illegal string values for {lnum} the same */
9294 if (lnum < 0)
9295 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009296 fold_count = foldedCount(curwin, lnum, &foldinfo);
9297 if (fold_count > 0)
9298 {
9299 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9300 &foldinfo, buf);
9301 if (text == buf)
9302 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009304 }
9305#endif
9306}
9307
9308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 * "foreground()" function
9310 */
9311/*ARGSUSED*/
9312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009313f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009314 typval_T *argvars;
9315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009317 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318#ifdef FEAT_GUI
9319 if (gui.in_use)
9320 gui_mch_set_foreground();
9321#else
9322# ifdef WIN32
9323 win32_set_foreground();
9324# endif
9325#endif
9326}
9327
9328/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009329 * "function()" function
9330 */
9331/*ARGSUSED*/
9332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009333f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009334 typval_T *argvars;
9335 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009336{
9337 char_u *s;
9338
Bram Moolenaara7043832005-01-21 11:56:39 +00009339 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009340 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009341 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009342 EMSG2(_(e_invarg2), s);
9343 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009344 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009345 else
9346 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009347 rettv->vval.v_string = vim_strsave(s);
9348 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009349 }
9350}
9351
9352/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009353 * "garbagecollect()" function
9354 */
9355/*ARGSUSED*/
9356 static void
9357f_garbagecollect(argvars, rettv)
9358 typval_T *argvars;
9359 typval_T *rettv;
9360{
9361 garbage_collect();
9362}
9363
9364/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009365 * "get()" function
9366 */
9367 static void
9368f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009369 typval_T *argvars;
9370 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009371{
Bram Moolenaar33570922005-01-25 22:26:29 +00009372 listitem_T *li;
9373 list_T *l;
9374 dictitem_T *di;
9375 dict_T *d;
9376 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009377
Bram Moolenaare9a41262005-01-15 22:18:47 +00009378 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009379 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009380 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009382 int error = FALSE;
9383
9384 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9385 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009386 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009387 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009388 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009389 else if (argvars[0].v_type == VAR_DICT)
9390 {
9391 if ((d = argvars[0].vval.v_dict) != NULL)
9392 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009393 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009394 if (di != NULL)
9395 tv = &di->di_tv;
9396 }
9397 }
9398 else
9399 EMSG2(_(e_listdictarg), "get()");
9400
9401 if (tv == NULL)
9402 {
9403 if (argvars[2].v_type == VAR_UNKNOWN)
9404 rettv->vval.v_number = 0;
9405 else
9406 copy_tv(&argvars[2], rettv);
9407 }
9408 else
9409 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009410}
9411
Bram Moolenaar342337a2005-07-21 21:11:17 +00009412static 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 +00009413
9414/*
9415 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009416 * Return a range (from start to end) of lines in rettv from the specified
9417 * buffer.
9418 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009419 */
9420 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009421get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009422 buf_T *buf;
9423 linenr_T start;
9424 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009425 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009426 typval_T *rettv;
9427{
9428 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009429
Bram Moolenaar342337a2005-07-21 21:11:17 +00009430 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009431 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009432 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009433 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009434 }
9435 else
9436 rettv->vval.v_number = 0;
9437
9438 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9439 return;
9440
9441 if (!retlist)
9442 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009443 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9444 p = ml_get_buf(buf, start, FALSE);
9445 else
9446 p = (char_u *)"";
9447
9448 rettv->v_type = VAR_STRING;
9449 rettv->vval.v_string = vim_strsave(p);
9450 }
9451 else
9452 {
9453 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009454 return;
9455
9456 if (start < 1)
9457 start = 1;
9458 if (end > buf->b_ml.ml_line_count)
9459 end = buf->b_ml.ml_line_count;
9460 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009461 if (list_append_string(rettv->vval.v_list,
9462 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009463 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009464 }
9465}
9466
9467/*
9468 * "getbufline()" function
9469 */
9470 static void
9471f_getbufline(argvars, rettv)
9472 typval_T *argvars;
9473 typval_T *rettv;
9474{
9475 linenr_T lnum;
9476 linenr_T end;
9477 buf_T *buf;
9478
9479 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9480 ++emsg_off;
9481 buf = get_buf_tv(&argvars[0]);
9482 --emsg_off;
9483
Bram Moolenaar661b1822005-07-28 22:36:45 +00009484 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009485 if (argvars[2].v_type == VAR_UNKNOWN)
9486 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009487 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009488 end = get_tv_lnum_buf(&argvars[2], buf);
9489
Bram Moolenaar342337a2005-07-21 21:11:17 +00009490 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009491}
9492
Bram Moolenaar0d660222005-01-07 21:51:51 +00009493/*
9494 * "getbufvar()" function
9495 */
9496 static void
9497f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009498 typval_T *argvars;
9499 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009500{
9501 buf_T *buf;
9502 buf_T *save_curbuf;
9503 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009504 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009505
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009506 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9507 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009508 ++emsg_off;
9509 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009510
9511 rettv->v_type = VAR_STRING;
9512 rettv->vval.v_string = NULL;
9513
9514 if (buf != NULL && varname != NULL)
9515 {
9516 if (*varname == '&') /* buffer-local-option */
9517 {
9518 /* set curbuf to be our buf, temporarily */
9519 save_curbuf = curbuf;
9520 curbuf = buf;
9521
9522 get_option_tv(&varname, rettv, TRUE);
9523
9524 /* restore previous notion of curbuf */
9525 curbuf = save_curbuf;
9526 }
9527 else
9528 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009529 if (*varname == NUL)
9530 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9531 * scope prefix before the NUL byte is required by
9532 * find_var_in_ht(). */
9533 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009534 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009535 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009536 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009537 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009538 }
9539 }
9540
9541 --emsg_off;
9542}
9543
9544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 * "getchar()" function
9546 */
9547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009548f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009549 typval_T *argvars;
9550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551{
9552 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009553 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554
9555 ++no_mapping;
9556 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009557 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 /* getchar(): blocking wait. */
9559 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009560 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 /* getchar(1): only check if char avail */
9562 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009563 else if (error || vpeekc() == NUL)
9564 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 n = 0;
9566 else
9567 /* getchar(0) and char avail: return char */
9568 n = safe_vgetc();
9569 --no_mapping;
9570 --allow_keys;
9571
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 if (IS_SPECIAL(n) || mod_mask != 0)
9574 {
9575 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9576 int i = 0;
9577
9578 /* Turn a special key into three bytes, plus modifier. */
9579 if (mod_mask != 0)
9580 {
9581 temp[i++] = K_SPECIAL;
9582 temp[i++] = KS_MODIFIER;
9583 temp[i++] = mod_mask;
9584 }
9585 if (IS_SPECIAL(n))
9586 {
9587 temp[i++] = K_SPECIAL;
9588 temp[i++] = K_SECOND(n);
9589 temp[i++] = K_THIRD(n);
9590 }
9591#ifdef FEAT_MBYTE
9592 else if (has_mbyte)
9593 i += (*mb_char2bytes)(n, temp + i);
9594#endif
9595 else
9596 temp[i++] = n;
9597 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->v_type = VAR_STRING;
9599 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 }
9601}
9602
9603/*
9604 * "getcharmod()" function
9605 */
9606/*ARGSUSED*/
9607 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009609 typval_T *argvars;
9610 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613}
9614
9615/*
9616 * "getcmdline()" function
9617 */
9618/*ARGSUSED*/
9619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009621 typval_T *argvars;
9622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624 rettv->v_type = VAR_STRING;
9625 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626}
9627
9628/*
9629 * "getcmdpos()" function
9630 */
9631/*ARGSUSED*/
9632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009633f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009634 typval_T *argvars;
9635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009637 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638}
9639
9640/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009641 * "getcmdtype()" function
9642 */
9643/*ARGSUSED*/
9644 static void
9645f_getcmdtype(argvars, rettv)
9646 typval_T *argvars;
9647 typval_T *rettv;
9648{
9649 rettv->v_type = VAR_STRING;
9650 rettv->vval.v_string = alloc(2);
9651 if (rettv->vval.v_string != NULL)
9652 {
9653 rettv->vval.v_string[0] = get_cmdline_type();
9654 rettv->vval.v_string[1] = NUL;
9655 }
9656}
9657
9658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 * "getcwd()" function
9660 */
9661/*ARGSUSED*/
9662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009663f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009664 typval_T *argvars;
9665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666{
9667 char_u cwd[MAXPATHL];
9668
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 else
9673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009676 if (rettv->vval.v_string != NULL)
9677 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678#endif
9679 }
9680}
9681
9682/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009683 * "getfontname()" function
9684 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009685/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009687f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009688 typval_T *argvars;
9689 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009690{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691 rettv->v_type = VAR_STRING;
9692 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009693#ifdef FEAT_GUI
9694 if (gui.in_use)
9695 {
9696 GuiFont font;
9697 char_u *name = NULL;
9698
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009699 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009700 {
9701 /* Get the "Normal" font. Either the name saved by
9702 * hl_set_font_name() or from the font ID. */
9703 font = gui.norm_font;
9704 name = hl_get_font_name();
9705 }
9706 else
9707 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009709 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9710 return;
9711 font = gui_mch_get_font(name, FALSE);
9712 if (font == NOFONT)
9713 return; /* Invalid font name, return empty string. */
9714 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009715 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009716 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009717 gui_mch_free_font(font);
9718 }
9719#endif
9720}
9721
9722/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009723 * "getfperm({fname})" function
9724 */
9725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009726f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009727 typval_T *argvars;
9728 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009729{
9730 char_u *fname;
9731 struct stat st;
9732 char_u *perm = NULL;
9733 char_u flags[] = "rwx";
9734 int i;
9735
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009737
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009739 if (mch_stat((char *)fname, &st) >= 0)
9740 {
9741 perm = vim_strsave((char_u *)"---------");
9742 if (perm != NULL)
9743 {
9744 for (i = 0; i < 9; i++)
9745 {
9746 if (st.st_mode & (1 << (8 - i)))
9747 perm[i] = flags[i % 3];
9748 }
9749 }
9750 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009751 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009752}
9753
9754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 * "getfsize({fname})" function
9756 */
9757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009759 typval_T *argvars;
9760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761{
9762 char_u *fname;
9763 struct stat st;
9764
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768
9769 if (mch_stat((char *)fname, &st) >= 0)
9770 {
9771 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 }
9776 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778}
9779
9780/*
9781 * "getftime({fname})" function
9782 */
9783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009785 typval_T *argvars;
9786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787{
9788 char_u *fname;
9789 struct stat st;
9790
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792
9793 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797}
9798
9799/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009800 * "getftype({fname})" function
9801 */
9802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009804 typval_T *argvars;
9805 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009806{
9807 char_u *fname;
9808 struct stat st;
9809 char_u *type = NULL;
9810 char *t;
9811
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009813
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009814 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009815 if (mch_lstat((char *)fname, &st) >= 0)
9816 {
9817#ifdef S_ISREG
9818 if (S_ISREG(st.st_mode))
9819 t = "file";
9820 else if (S_ISDIR(st.st_mode))
9821 t = "dir";
9822# ifdef S_ISLNK
9823 else if (S_ISLNK(st.st_mode))
9824 t = "link";
9825# endif
9826# ifdef S_ISBLK
9827 else if (S_ISBLK(st.st_mode))
9828 t = "bdev";
9829# endif
9830# ifdef S_ISCHR
9831 else if (S_ISCHR(st.st_mode))
9832 t = "cdev";
9833# endif
9834# ifdef S_ISFIFO
9835 else if (S_ISFIFO(st.st_mode))
9836 t = "fifo";
9837# endif
9838# ifdef S_ISSOCK
9839 else if (S_ISSOCK(st.st_mode))
9840 t = "fifo";
9841# endif
9842 else
9843 t = "other";
9844#else
9845# ifdef S_IFMT
9846 switch (st.st_mode & S_IFMT)
9847 {
9848 case S_IFREG: t = "file"; break;
9849 case S_IFDIR: t = "dir"; break;
9850# ifdef S_IFLNK
9851 case S_IFLNK: t = "link"; break;
9852# endif
9853# ifdef S_IFBLK
9854 case S_IFBLK: t = "bdev"; break;
9855# endif
9856# ifdef S_IFCHR
9857 case S_IFCHR: t = "cdev"; break;
9858# endif
9859# ifdef S_IFIFO
9860 case S_IFIFO: t = "fifo"; break;
9861# endif
9862# ifdef S_IFSOCK
9863 case S_IFSOCK: t = "socket"; break;
9864# endif
9865 default: t = "other";
9866 }
9867# else
9868 if (mch_isdir(fname))
9869 t = "dir";
9870 else
9871 t = "file";
9872# endif
9873#endif
9874 type = vim_strsave((char_u *)t);
9875 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009877}
9878
9879/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009880 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009881 */
9882 static void
9883f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009884 typval_T *argvars;
9885 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009886{
9887 linenr_T lnum;
9888 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009889 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009890
9891 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009892 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009893 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009894 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009895 retlist = FALSE;
9896 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009897 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009898 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009899 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009900 retlist = TRUE;
9901 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009902
Bram Moolenaar342337a2005-07-21 21:11:17 +00009903 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009904}
9905
9906/*
Bram Moolenaara5525202006-03-02 22:52:09 +00009907 * "getpos(string)" function
9908 */
9909 static void
9910f_getpos(argvars, rettv)
9911 typval_T *argvars;
9912 typval_T *rettv;
9913{
9914 pos_T *fp;
9915 list_T *l;
9916
9917 if (rettv_list_alloc(rettv) == OK)
9918 {
9919 l = rettv->vval.v_list;
9920 fp = var2fpos(&argvars[0], TRUE);
9921 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
9922 : (varnumber_T)0);
9923 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
9924 : (varnumber_T)0);
9925 list_append_number(l,
9926#ifdef FEAT_VIRTUALEDIT
9927 (fp != NULL) ? (varnumber_T)fp->coladd :
9928#endif
9929 (varnumber_T)0);
9930 }
9931 else
9932 rettv->vval.v_number = FALSE;
9933}
9934
9935/*
Bram Moolenaar280f1262006-01-30 00:14:18 +00009936 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +00009937 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00009938/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +00009939 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +00009940f_getqflist(argvars, rettv)
9941 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009942 typval_T *rettv;
9943{
9944#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +00009945 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009946#endif
9947
9948 rettv->vval.v_number = FALSE;
9949#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009950 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +00009951 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00009952 wp = NULL;
9953 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
9954 {
9955 wp = find_win_by_nr(&argvars[0]);
9956 if (wp == NULL)
9957 return;
9958 }
9959
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009960 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009961 }
9962#endif
9963}
9964
9965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 * "getreg()" function
9967 */
9968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009969f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009970 typval_T *argvars;
9971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972{
9973 char_u *strregname;
9974 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009975 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009976 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009978 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009980 strregname = get_tv_string_chk(&argvars[0]);
9981 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009982 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009983 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009986 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 regname = (strregname == NULL ? '"' : *strregname);
9988 if (regname == 0)
9989 regname = '"';
9990
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009991 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009992 rettv->vval.v_string = error ? NULL :
9993 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994}
9995
9996/*
9997 * "getregtype()" function
9998 */
9999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010001 typval_T *argvars;
10002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003{
10004 char_u *strregname;
10005 int regname;
10006 char_u buf[NUMBUFLEN + 2];
10007 long reglen = 0;
10008
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010009 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010010 {
10011 strregname = get_tv_string_chk(&argvars[0]);
10012 if (strregname == NULL) /* type error; errmsg already given */
10013 {
10014 rettv->v_type = VAR_STRING;
10015 rettv->vval.v_string = NULL;
10016 return;
10017 }
10018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 else
10020 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010021 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022
10023 regname = (strregname == NULL ? '"' : *strregname);
10024 if (regname == 0)
10025 regname = '"';
10026
10027 buf[0] = NUL;
10028 buf[1] = NUL;
10029 switch (get_reg_type(regname, &reglen))
10030 {
10031 case MLINE: buf[0] = 'V'; break;
10032 case MCHAR: buf[0] = 'v'; break;
10033#ifdef FEAT_VISUAL
10034 case MBLOCK:
10035 buf[0] = Ctrl_V;
10036 sprintf((char *)buf + 1, "%ld", reglen + 1);
10037 break;
10038#endif
10039 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010040 rettv->v_type = VAR_STRING;
10041 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042}
10043
10044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 * "getwinposx()" function
10046 */
10047/*ARGSUSED*/
10048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010050 typval_T *argvars;
10051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010053 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054#ifdef FEAT_GUI
10055 if (gui.in_use)
10056 {
10057 int x, y;
10058
10059 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010060 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 }
10062#endif
10063}
10064
10065/*
10066 * "getwinposy()" function
10067 */
10068/*ARGSUSED*/
10069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010071 typval_T *argvars;
10072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010074 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075#ifdef FEAT_GUI
10076 if (gui.in_use)
10077 {
10078 int x, y;
10079
10080 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 }
10083#endif
10084}
10085
Bram Moolenaara40058a2005-07-11 22:42:07 +000010086 static win_T *
10087find_win_by_nr(vp)
10088 typval_T *vp;
10089{
10090#ifdef FEAT_WINDOWS
10091 win_T *wp;
10092#endif
10093 int nr;
10094
10095 nr = get_tv_number_chk(vp, NULL);
10096
10097#ifdef FEAT_WINDOWS
10098 if (nr < 0)
10099 return NULL;
10100 if (nr == 0)
10101 return curwin;
10102
10103 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10104 if (--nr <= 0)
10105 break;
10106 return wp;
10107#else
10108 if (nr == 0 || nr == 1)
10109 return curwin;
10110 return NULL;
10111#endif
10112}
10113
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114/*
10115 * "getwinvar()" function
10116 */
10117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010118f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010119 typval_T *argvars;
10120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121{
10122 win_T *win, *oldcurwin;
10123 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010124 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 varname = get_tv_string_chk(&argvars[1]);
10128 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010130 rettv->v_type = VAR_STRING;
10131 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132
10133 if (win != NULL && varname != NULL)
10134 {
10135 if (*varname == '&') /* window-local-option */
10136 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010137 /* Set curwin to be our win, temporarily. Also set curbuf, so
10138 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 oldcurwin = curwin;
10140 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010141 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144
10145 /* restore previous notion of curwin */
10146 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010147 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 }
10149 else
10150 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010151 if (*varname == NUL)
10152 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10153 * scope prefix before the NUL byte is required by
10154 * find_var_in_ht(). */
10155 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010157 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010159 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 }
10161 }
10162
10163 --emsg_off;
10164}
10165
10166/*
10167 * "glob()" function
10168 */
10169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010170f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010171 typval_T *argvars;
10172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173{
10174 expand_T xpc;
10175
10176 ExpandInit(&xpc);
10177 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010178 rettv->v_type = VAR_STRING;
10179 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10181 ExpandCleanup(&xpc);
10182}
10183
10184/*
10185 * "globpath()" function
10186 */
10187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010188f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010189 typval_T *argvars;
10190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191{
10192 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010193 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010195 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010196 if (file == NULL)
10197 rettv->vval.v_string = NULL;
10198 else
10199 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200}
10201
10202/*
10203 * "has()" function
10204 */
10205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010206f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010207 typval_T *argvars;
10208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209{
10210 int i;
10211 char_u *name;
10212 int n = FALSE;
10213 static char *(has_list[]) =
10214 {
10215#ifdef AMIGA
10216 "amiga",
10217# ifdef FEAT_ARP
10218 "arp",
10219# endif
10220#endif
10221#ifdef __BEOS__
10222 "beos",
10223#endif
10224#ifdef MSDOS
10225# ifdef DJGPP
10226 "dos32",
10227# else
10228 "dos16",
10229# endif
10230#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010231#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 "mac",
10233#endif
10234#if defined(MACOS_X_UNIX)
10235 "macunix",
10236#endif
10237#ifdef OS2
10238 "os2",
10239#endif
10240#ifdef __QNX__
10241 "qnx",
10242#endif
10243#ifdef RISCOS
10244 "riscos",
10245#endif
10246#ifdef UNIX
10247 "unix",
10248#endif
10249#ifdef VMS
10250 "vms",
10251#endif
10252#ifdef WIN16
10253 "win16",
10254#endif
10255#ifdef WIN32
10256 "win32",
10257#endif
10258#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10259 "win32unix",
10260#endif
10261#ifdef WIN64
10262 "win64",
10263#endif
10264#ifdef EBCDIC
10265 "ebcdic",
10266#endif
10267#ifndef CASE_INSENSITIVE_FILENAME
10268 "fname_case",
10269#endif
10270#ifdef FEAT_ARABIC
10271 "arabic",
10272#endif
10273#ifdef FEAT_AUTOCMD
10274 "autocmd",
10275#endif
10276#ifdef FEAT_BEVAL
10277 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010278# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10279 "balloon_multiline",
10280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281#endif
10282#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10283 "builtin_terms",
10284# ifdef ALL_BUILTIN_TCAPS
10285 "all_builtin_terms",
10286# endif
10287#endif
10288#ifdef FEAT_BYTEOFF
10289 "byte_offset",
10290#endif
10291#ifdef FEAT_CINDENT
10292 "cindent",
10293#endif
10294#ifdef FEAT_CLIENTSERVER
10295 "clientserver",
10296#endif
10297#ifdef FEAT_CLIPBOARD
10298 "clipboard",
10299#endif
10300#ifdef FEAT_CMDL_COMPL
10301 "cmdline_compl",
10302#endif
10303#ifdef FEAT_CMDHIST
10304 "cmdline_hist",
10305#endif
10306#ifdef FEAT_COMMENTS
10307 "comments",
10308#endif
10309#ifdef FEAT_CRYPT
10310 "cryptv",
10311#endif
10312#ifdef FEAT_CSCOPE
10313 "cscope",
10314#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010315#ifdef CURSOR_SHAPE
10316 "cursorshape",
10317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318#ifdef DEBUG
10319 "debug",
10320#endif
10321#ifdef FEAT_CON_DIALOG
10322 "dialog_con",
10323#endif
10324#ifdef FEAT_GUI_DIALOG
10325 "dialog_gui",
10326#endif
10327#ifdef FEAT_DIFF
10328 "diff",
10329#endif
10330#ifdef FEAT_DIGRAPHS
10331 "digraphs",
10332#endif
10333#ifdef FEAT_DND
10334 "dnd",
10335#endif
10336#ifdef FEAT_EMACS_TAGS
10337 "emacs_tags",
10338#endif
10339 "eval", /* always present, of course! */
10340#ifdef FEAT_EX_EXTRA
10341 "ex_extra",
10342#endif
10343#ifdef FEAT_SEARCH_EXTRA
10344 "extra_search",
10345#endif
10346#ifdef FEAT_FKMAP
10347 "farsi",
10348#endif
10349#ifdef FEAT_SEARCHPATH
10350 "file_in_path",
10351#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010352#if defined(UNIX) && !defined(USE_SYSTEM)
10353 "filterpipe",
10354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355#ifdef FEAT_FIND_ID
10356 "find_in_path",
10357#endif
10358#ifdef FEAT_FOLDING
10359 "folding",
10360#endif
10361#ifdef FEAT_FOOTER
10362 "footer",
10363#endif
10364#if !defined(USE_SYSTEM) && defined(UNIX)
10365 "fork",
10366#endif
10367#ifdef FEAT_GETTEXT
10368 "gettext",
10369#endif
10370#ifdef FEAT_GUI
10371 "gui",
10372#endif
10373#ifdef FEAT_GUI_ATHENA
10374# ifdef FEAT_GUI_NEXTAW
10375 "gui_neXtaw",
10376# else
10377 "gui_athena",
10378# endif
10379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380#ifdef FEAT_GUI_GTK
10381 "gui_gtk",
10382# ifdef HAVE_GTK2
10383 "gui_gtk2",
10384# endif
10385#endif
10386#ifdef FEAT_GUI_MAC
10387 "gui_mac",
10388#endif
10389#ifdef FEAT_GUI_MOTIF
10390 "gui_motif",
10391#endif
10392#ifdef FEAT_GUI_PHOTON
10393 "gui_photon",
10394#endif
10395#ifdef FEAT_GUI_W16
10396 "gui_win16",
10397#endif
10398#ifdef FEAT_GUI_W32
10399 "gui_win32",
10400#endif
10401#ifdef FEAT_HANGULIN
10402 "hangul_input",
10403#endif
10404#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10405 "iconv",
10406#endif
10407#ifdef FEAT_INS_EXPAND
10408 "insert_expand",
10409#endif
10410#ifdef FEAT_JUMPLIST
10411 "jumplist",
10412#endif
10413#ifdef FEAT_KEYMAP
10414 "keymap",
10415#endif
10416#ifdef FEAT_LANGMAP
10417 "langmap",
10418#endif
10419#ifdef FEAT_LIBCALL
10420 "libcall",
10421#endif
10422#ifdef FEAT_LINEBREAK
10423 "linebreak",
10424#endif
10425#ifdef FEAT_LISP
10426 "lispindent",
10427#endif
10428#ifdef FEAT_LISTCMDS
10429 "listcmds",
10430#endif
10431#ifdef FEAT_LOCALMAP
10432 "localmap",
10433#endif
10434#ifdef FEAT_MENU
10435 "menu",
10436#endif
10437#ifdef FEAT_SESSION
10438 "mksession",
10439#endif
10440#ifdef FEAT_MODIFY_FNAME
10441 "modify_fname",
10442#endif
10443#ifdef FEAT_MOUSE
10444 "mouse",
10445#endif
10446#ifdef FEAT_MOUSESHAPE
10447 "mouseshape",
10448#endif
10449#if defined(UNIX) || defined(VMS)
10450# ifdef FEAT_MOUSE_DEC
10451 "mouse_dec",
10452# endif
10453# ifdef FEAT_MOUSE_GPM
10454 "mouse_gpm",
10455# endif
10456# ifdef FEAT_MOUSE_JSB
10457 "mouse_jsbterm",
10458# endif
10459# ifdef FEAT_MOUSE_NET
10460 "mouse_netterm",
10461# endif
10462# ifdef FEAT_MOUSE_PTERM
10463 "mouse_pterm",
10464# endif
10465# ifdef FEAT_MOUSE_XTERM
10466 "mouse_xterm",
10467# endif
10468#endif
10469#ifdef FEAT_MBYTE
10470 "multi_byte",
10471#endif
10472#ifdef FEAT_MBYTE_IME
10473 "multi_byte_ime",
10474#endif
10475#ifdef FEAT_MULTI_LANG
10476 "multi_lang",
10477#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010478#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010479#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010480 "mzscheme",
10481#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483#ifdef FEAT_OLE
10484 "ole",
10485#endif
10486#ifdef FEAT_OSFILETYPE
10487 "osfiletype",
10488#endif
10489#ifdef FEAT_PATH_EXTRA
10490 "path_extra",
10491#endif
10492#ifdef FEAT_PERL
10493#ifndef DYNAMIC_PERL
10494 "perl",
10495#endif
10496#endif
10497#ifdef FEAT_PYTHON
10498#ifndef DYNAMIC_PYTHON
10499 "python",
10500#endif
10501#endif
10502#ifdef FEAT_POSTSCRIPT
10503 "postscript",
10504#endif
10505#ifdef FEAT_PRINTER
10506 "printer",
10507#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010508#ifdef FEAT_PROFILE
10509 "profile",
10510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511#ifdef FEAT_QUICKFIX
10512 "quickfix",
10513#endif
10514#ifdef FEAT_RIGHTLEFT
10515 "rightleft",
10516#endif
10517#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10518 "ruby",
10519#endif
10520#ifdef FEAT_SCROLLBIND
10521 "scrollbind",
10522#endif
10523#ifdef FEAT_CMDL_INFO
10524 "showcmd",
10525 "cmdline_info",
10526#endif
10527#ifdef FEAT_SIGNS
10528 "signs",
10529#endif
10530#ifdef FEAT_SMARTINDENT
10531 "smartindent",
10532#endif
10533#ifdef FEAT_SNIFF
10534 "sniff",
10535#endif
10536#ifdef FEAT_STL_OPT
10537 "statusline",
10538#endif
10539#ifdef FEAT_SUN_WORKSHOP
10540 "sun_workshop",
10541#endif
10542#ifdef FEAT_NETBEANS_INTG
10543 "netbeans_intg",
10544#endif
10545#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010546 "spell",
10547#endif
10548#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 "syntax",
10550#endif
10551#if defined(USE_SYSTEM) || !defined(UNIX)
10552 "system",
10553#endif
10554#ifdef FEAT_TAG_BINS
10555 "tag_binary",
10556#endif
10557#ifdef FEAT_TAG_OLDSTATIC
10558 "tag_old_static",
10559#endif
10560#ifdef FEAT_TAG_ANYWHITE
10561 "tag_any_white",
10562#endif
10563#ifdef FEAT_TCL
10564# ifndef DYNAMIC_TCL
10565 "tcl",
10566# endif
10567#endif
10568#ifdef TERMINFO
10569 "terminfo",
10570#endif
10571#ifdef FEAT_TERMRESPONSE
10572 "termresponse",
10573#endif
10574#ifdef FEAT_TEXTOBJ
10575 "textobjects",
10576#endif
10577#ifdef HAVE_TGETENT
10578 "tgetent",
10579#endif
10580#ifdef FEAT_TITLE
10581 "title",
10582#endif
10583#ifdef FEAT_TOOLBAR
10584 "toolbar",
10585#endif
10586#ifdef FEAT_USR_CMDS
10587 "user-commands", /* was accidentally included in 5.4 */
10588 "user_commands",
10589#endif
10590#ifdef FEAT_VIMINFO
10591 "viminfo",
10592#endif
10593#ifdef FEAT_VERTSPLIT
10594 "vertsplit",
10595#endif
10596#ifdef FEAT_VIRTUALEDIT
10597 "virtualedit",
10598#endif
10599#ifdef FEAT_VISUAL
10600 "visual",
10601#endif
10602#ifdef FEAT_VISUALEXTRA
10603 "visualextra",
10604#endif
10605#ifdef FEAT_VREPLACE
10606 "vreplace",
10607#endif
10608#ifdef FEAT_WILDIGN
10609 "wildignore",
10610#endif
10611#ifdef FEAT_WILDMENU
10612 "wildmenu",
10613#endif
10614#ifdef FEAT_WINDOWS
10615 "windows",
10616#endif
10617#ifdef FEAT_WAK
10618 "winaltkeys",
10619#endif
10620#ifdef FEAT_WRITEBACKUP
10621 "writebackup",
10622#endif
10623#ifdef FEAT_XIM
10624 "xim",
10625#endif
10626#ifdef FEAT_XFONTSET
10627 "xfontset",
10628#endif
10629#ifdef USE_XSMP
10630 "xsmp",
10631#endif
10632#ifdef USE_XSMP_INTERACT
10633 "xsmp_interact",
10634#endif
10635#ifdef FEAT_XCLIPBOARD
10636 "xterm_clipboard",
10637#endif
10638#ifdef FEAT_XTERM_SAVE
10639 "xterm_save",
10640#endif
10641#if defined(UNIX) && defined(FEAT_X11)
10642 "X11",
10643#endif
10644 NULL
10645 };
10646
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010647 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 for (i = 0; has_list[i] != NULL; ++i)
10649 if (STRICMP(name, has_list[i]) == 0)
10650 {
10651 n = TRUE;
10652 break;
10653 }
10654
10655 if (n == FALSE)
10656 {
10657 if (STRNICMP(name, "patch", 5) == 0)
10658 n = has_patch(atoi((char *)name + 5));
10659 else if (STRICMP(name, "vim_starting") == 0)
10660 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010661#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10662 else if (STRICMP(name, "balloon_multiline") == 0)
10663 n = multiline_balloon_available();
10664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665#ifdef DYNAMIC_TCL
10666 else if (STRICMP(name, "tcl") == 0)
10667 n = tcl_enabled(FALSE);
10668#endif
10669#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10670 else if (STRICMP(name, "iconv") == 0)
10671 n = iconv_enabled(FALSE);
10672#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010673#ifdef DYNAMIC_MZSCHEME
10674 else if (STRICMP(name, "mzscheme") == 0)
10675 n = mzscheme_enabled(FALSE);
10676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677#ifdef DYNAMIC_RUBY
10678 else if (STRICMP(name, "ruby") == 0)
10679 n = ruby_enabled(FALSE);
10680#endif
10681#ifdef DYNAMIC_PYTHON
10682 else if (STRICMP(name, "python") == 0)
10683 n = python_enabled(FALSE);
10684#endif
10685#ifdef DYNAMIC_PERL
10686 else if (STRICMP(name, "perl") == 0)
10687 n = perl_enabled(FALSE);
10688#endif
10689#ifdef FEAT_GUI
10690 else if (STRICMP(name, "gui_running") == 0)
10691 n = (gui.in_use || gui.starting);
10692# ifdef FEAT_GUI_W32
10693 else if (STRICMP(name, "gui_win32s") == 0)
10694 n = gui_is_win32s();
10695# endif
10696# ifdef FEAT_BROWSE
10697 else if (STRICMP(name, "browse") == 0)
10698 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10699# endif
10700#endif
10701#ifdef FEAT_SYN_HL
10702 else if (STRICMP(name, "syntax_items") == 0)
10703 n = syntax_present(curbuf);
10704#endif
10705#if defined(WIN3264)
10706 else if (STRICMP(name, "win95") == 0)
10707 n = mch_windows95();
10708#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010709#ifdef FEAT_NETBEANS_INTG
10710 else if (STRICMP(name, "netbeans_enabled") == 0)
10711 n = usingNetbeans;
10712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 }
10714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010715 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716}
10717
10718/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010719 * "has_key()" function
10720 */
10721 static void
10722f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010723 typval_T *argvars;
10724 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010725{
10726 rettv->vval.v_number = 0;
10727 if (argvars[0].v_type != VAR_DICT)
10728 {
10729 EMSG(_(e_dictreq));
10730 return;
10731 }
10732 if (argvars[0].vval.v_dict == NULL)
10733 return;
10734
10735 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010736 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010737}
10738
10739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 * "hasmapto()" function
10741 */
10742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010743f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010744 typval_T *argvars;
10745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746{
10747 char_u *name;
10748 char_u *mode;
10749 char_u buf[NUMBUFLEN];
10750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010751 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010752 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 mode = (char_u *)"nvo";
10754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010755 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756
10757 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010758 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010760 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761}
10762
10763/*
10764 * "histadd()" function
10765 */
10766/*ARGSUSED*/
10767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010768f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010769 typval_T *argvars;
10770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771{
10772#ifdef FEAT_CMDHIST
10773 int histype;
10774 char_u *str;
10775 char_u buf[NUMBUFLEN];
10776#endif
10777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010778 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 if (check_restricted() || check_secure())
10780 return;
10781#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010782 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10783 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 if (histype >= 0)
10785 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010786 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 if (*str != NUL)
10788 {
10789 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010790 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 return;
10792 }
10793 }
10794#endif
10795}
10796
10797/*
10798 * "histdel()" function
10799 */
10800/*ARGSUSED*/
10801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010802f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010803 typval_T *argvars;
10804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805{
10806#ifdef FEAT_CMDHIST
10807 int n;
10808 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010809 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010811 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10812 if (str == NULL)
10813 n = 0;
10814 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010816 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010817 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010819 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010820 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 else
10822 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010823 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010824 get_tv_string_buf(&argvars[1], buf));
10825 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010827 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828#endif
10829}
10830
10831/*
10832 * "histget()" function
10833 */
10834/*ARGSUSED*/
10835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010836f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010837 typval_T *argvars;
10838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839{
10840#ifdef FEAT_CMDHIST
10841 int type;
10842 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010843 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010845 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10846 if (str == NULL)
10847 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010849 {
10850 type = get_histtype(str);
10851 if (argvars[1].v_type == VAR_UNKNOWN)
10852 idx = get_history_idx(type);
10853 else
10854 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10855 /* -1 on type error */
10856 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010859 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010861 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862}
10863
10864/*
10865 * "histnr()" function
10866 */
10867/*ARGSUSED*/
10868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010870 typval_T *argvars;
10871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872{
10873 int i;
10874
10875#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010876 char_u *history = get_tv_string_chk(&argvars[0]);
10877
10878 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 if (i >= HIST_CMD && i < HIST_COUNT)
10880 i = get_history_idx(i);
10881 else
10882#endif
10883 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885}
10886
10887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 * "highlightID(name)" function
10889 */
10890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010891f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010892 typval_T *argvars;
10893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010895 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896}
10897
10898/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010899 * "highlight_exists()" function
10900 */
10901 static void
10902f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010903 typval_T *argvars;
10904 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010905{
10906 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10907}
10908
10909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 * "hostname()" function
10911 */
10912/*ARGSUSED*/
10913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010914f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010915 typval_T *argvars;
10916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917{
10918 char_u hostname[256];
10919
10920 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010921 rettv->v_type = VAR_STRING;
10922 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923}
10924
10925/*
10926 * iconv() function
10927 */
10928/*ARGSUSED*/
10929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010930f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010931 typval_T *argvars;
10932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933{
10934#ifdef FEAT_MBYTE
10935 char_u buf1[NUMBUFLEN];
10936 char_u buf2[NUMBUFLEN];
10937 char_u *from, *to, *str;
10938 vimconv_T vimconv;
10939#endif
10940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010941 rettv->v_type = VAR_STRING;
10942 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943
10944#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010945 str = get_tv_string(&argvars[0]);
10946 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10947 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 vimconv.vc_type = CONV_NONE;
10949 convert_setup(&vimconv, from, to);
10950
10951 /* If the encodings are equal, no conversion needed. */
10952 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010953 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010955 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956
10957 convert_setup(&vimconv, NULL, NULL);
10958 vim_free(from);
10959 vim_free(to);
10960#endif
10961}
10962
10963/*
10964 * "indent()" function
10965 */
10966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010967f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010968 typval_T *argvars;
10969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970{
10971 linenr_T lnum;
10972
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010973 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978}
10979
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010980/*
10981 * "index()" function
10982 */
10983 static void
10984f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010985 typval_T *argvars;
10986 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010987{
Bram Moolenaar33570922005-01-25 22:26:29 +000010988 list_T *l;
10989 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010990 long idx = 0;
10991 int ic = FALSE;
10992
10993 rettv->vval.v_number = -1;
10994 if (argvars[0].v_type != VAR_LIST)
10995 {
10996 EMSG(_(e_listreq));
10997 return;
10998 }
10999 l = argvars[0].vval.v_list;
11000 if (l != NULL)
11001 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011002 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011003 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011005 int error = FALSE;
11006
Bram Moolenaar758711c2005-02-02 23:11:38 +000011007 /* Start at specified item. Use the cached index that list_find()
11008 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011009 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011010 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011011 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011012 ic = get_tv_number_chk(&argvars[3], &error);
11013 if (error)
11014 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011015 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011016
Bram Moolenaar758711c2005-02-02 23:11:38 +000011017 for ( ; item != NULL; item = item->li_next, ++idx)
11018 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011019 {
11020 rettv->vval.v_number = idx;
11021 break;
11022 }
11023 }
11024}
11025
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026static int inputsecret_flag = 0;
11027
11028/*
11029 * "input()" function
11030 * Also handles inputsecret() when inputsecret is set.
11031 */
11032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011034 typval_T *argvars;
11035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011037 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 char_u *p = NULL;
11039 int c;
11040 char_u buf[NUMBUFLEN];
11041 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011042 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011043 int xp_type = EXPAND_NOTHING;
11044 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047
11048#ifdef NO_CONSOLE_INPUT
11049 /* While starting up, there is no place to enter text. */
11050 if (no_console_input())
11051 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 return;
11054 }
11055#endif
11056
11057 cmd_silent = FALSE; /* Want to see the prompt. */
11058 if (prompt != NULL)
11059 {
11060 /* Only the part of the message after the last NL is considered as
11061 * prompt for the command line */
11062 p = vim_strrchr(prompt, '\n');
11063 if (p == NULL)
11064 p = prompt;
11065 else
11066 {
11067 ++p;
11068 c = *p;
11069 *p = NUL;
11070 msg_start();
11071 msg_clr_eos();
11072 msg_puts_attr(prompt, echo_attr);
11073 msg_didout = FALSE;
11074 msg_starthere();
11075 *p = c;
11076 }
11077 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011079 if (argvars[1].v_type != VAR_UNKNOWN)
11080 {
11081 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11082 if (defstr != NULL)
11083 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084
Bram Moolenaar4463f292005-09-25 22:20:24 +000011085 if (argvars[2].v_type != VAR_UNKNOWN)
11086 {
11087 char_u *xp_name;
11088 int xp_namelen;
11089 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011090
Bram Moolenaar4463f292005-09-25 22:20:24 +000011091 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011092
Bram Moolenaar4463f292005-09-25 22:20:24 +000011093 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11094 if (xp_name == NULL)
11095 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011096
Bram Moolenaar4463f292005-09-25 22:20:24 +000011097 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011098
Bram Moolenaar4463f292005-09-25 22:20:24 +000011099 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11100 &xp_arg) == FAIL)
11101 return;
11102 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011103 }
11104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011105 if (defstr != NULL)
11106 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011107 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11108 xp_type, xp_arg);
11109
11110 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011112 /* since the user typed this, no need to wait for return */
11113 need_wait_return = FALSE;
11114 msg_didout = FALSE;
11115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 cmd_silent = cmd_silent_save;
11117}
11118
11119/*
11120 * "inputdialog()" function
11121 */
11122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011124 typval_T *argvars;
11125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126{
11127#if defined(FEAT_GUI_TEXTDIALOG)
11128 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11129 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11130 {
11131 char_u *message;
11132 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011133 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011135 message = get_tv_string_chk(&argvars[0]);
11136 if (argvars[1].v_type != VAR_UNKNOWN
11137 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011138 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 else
11140 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011141 if (message != NULL && defstr != NULL
11142 && do_dialog(VIM_QUESTION, NULL, message,
11143 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 else
11146 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011147 if (message != NULL && defstr != NULL
11148 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011149 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150 rettv->vval.v_string = vim_strsave(
11151 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011153 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 }
11157 else
11158#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011159 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160}
11161
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011162/*
11163 * "inputlist()" function
11164 */
11165 static void
11166f_inputlist(argvars, rettv)
11167 typval_T *argvars;
11168 typval_T *rettv;
11169{
11170 listitem_T *li;
11171 int selected;
11172 int mouse_used;
11173
11174 rettv->vval.v_number = 0;
11175#ifdef NO_CONSOLE_INPUT
11176 /* While starting up, there is no place to enter text. */
11177 if (no_console_input())
11178 return;
11179#endif
11180 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11181 {
11182 EMSG2(_(e_listarg), "inputlist()");
11183 return;
11184 }
11185
11186 msg_start();
11187 lines_left = Rows; /* avoid more prompt */
11188 msg_scroll = TRUE;
11189 msg_clr_eos();
11190
11191 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11192 {
11193 msg_puts(get_tv_string(&li->li_tv));
11194 msg_putchar('\n');
11195 }
11196
11197 /* Ask for choice. */
11198 selected = prompt_for_number(&mouse_used);
11199 if (mouse_used)
11200 selected -= lines_left;
11201
11202 rettv->vval.v_number = selected;
11203}
11204
11205
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11207
11208/*
11209 * "inputrestore()" function
11210 */
11211/*ARGSUSED*/
11212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011214 typval_T *argvars;
11215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216{
11217 if (ga_userinput.ga_len > 0)
11218 {
11219 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11221 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011222 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 }
11224 else if (p_verbose > 1)
11225 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011226 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011227 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 }
11229}
11230
11231/*
11232 * "inputsave()" function
11233 */
11234/*ARGSUSED*/
11235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011237 typval_T *argvars;
11238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239{
11240 /* Add an entry to the stack of typehead storage. */
11241 if (ga_grow(&ga_userinput, 1) == OK)
11242 {
11243 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11244 + ga_userinput.ga_len);
11245 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011246 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 }
11248 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011249 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250}
11251
11252/*
11253 * "inputsecret()" function
11254 */
11255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011256f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011257 typval_T *argvars;
11258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259{
11260 ++cmdline_star;
11261 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 --cmdline_star;
11264 --inputsecret_flag;
11265}
11266
11267/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011268 * "insert()" function
11269 */
11270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011272 typval_T *argvars;
11273 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011274{
11275 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011276 listitem_T *item;
11277 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011278 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011279
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011280 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011281 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011282 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011283 else if ((l = argvars[0].vval.v_list) != NULL
11284 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011285 {
11286 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011287 before = get_tv_number_chk(&argvars[2], &error);
11288 if (error)
11289 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011290
Bram Moolenaar758711c2005-02-02 23:11:38 +000011291 if (before == l->lv_len)
11292 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011293 else
11294 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011295 item = list_find(l, before);
11296 if (item == NULL)
11297 {
11298 EMSGN(_(e_listidx), before);
11299 l = NULL;
11300 }
11301 }
11302 if (l != NULL)
11303 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011304 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011305 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011306 }
11307 }
11308}
11309
11310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 * "isdirectory()" function
11312 */
11313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011315 typval_T *argvars;
11316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319}
11320
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011321/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011322 * "islocked()" function
11323 */
11324 static void
11325f_islocked(argvars, rettv)
11326 typval_T *argvars;
11327 typval_T *rettv;
11328{
11329 lval_T lv;
11330 char_u *end;
11331 dictitem_T *di;
11332
11333 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011334 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11335 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011336 if (end != NULL && lv.ll_name != NULL)
11337 {
11338 if (*end != NUL)
11339 EMSG(_(e_trailing));
11340 else
11341 {
11342 if (lv.ll_tv == NULL)
11343 {
11344 if (check_changedtick(lv.ll_name))
11345 rettv->vval.v_number = 1; /* always locked */
11346 else
11347 {
11348 di = find_var(lv.ll_name, NULL);
11349 if (di != NULL)
11350 {
11351 /* Consider a variable locked when:
11352 * 1. the variable itself is locked
11353 * 2. the value of the variable is locked.
11354 * 3. the List or Dict value is locked.
11355 */
11356 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11357 || tv_islocked(&di->di_tv));
11358 }
11359 }
11360 }
11361 else if (lv.ll_range)
11362 EMSG(_("E745: Range not allowed"));
11363 else if (lv.ll_newkey != NULL)
11364 EMSG2(_(e_dictkey), lv.ll_newkey);
11365 else if (lv.ll_list != NULL)
11366 /* List item. */
11367 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11368 else
11369 /* Dictionary item. */
11370 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11371 }
11372 }
11373
11374 clear_lval(&lv);
11375}
11376
Bram Moolenaar33570922005-01-25 22:26:29 +000011377static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011378
11379/*
11380 * Turn a dict into a list:
11381 * "what" == 0: list of keys
11382 * "what" == 1: list of values
11383 * "what" == 2: list of items
11384 */
11385 static void
11386dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011387 typval_T *argvars;
11388 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011389 int what;
11390{
Bram Moolenaar33570922005-01-25 22:26:29 +000011391 list_T *l2;
11392 dictitem_T *di;
11393 hashitem_T *hi;
11394 listitem_T *li;
11395 listitem_T *li2;
11396 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011397 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011398
11399 rettv->vval.v_number = 0;
11400 if (argvars[0].v_type != VAR_DICT)
11401 {
11402 EMSG(_(e_dictreq));
11403 return;
11404 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011405 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011406 return;
11407
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011408 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011409 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011410
Bram Moolenaar33570922005-01-25 22:26:29 +000011411 todo = d->dv_hashtab.ht_used;
11412 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011413 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011414 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011415 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011416 --todo;
11417 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011418
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011419 li = listitem_alloc();
11420 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011421 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011422 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011423
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011424 if (what == 0)
11425 {
11426 /* keys() */
11427 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011428 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011429 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11430 }
11431 else if (what == 1)
11432 {
11433 /* values() */
11434 copy_tv(&di->di_tv, &li->li_tv);
11435 }
11436 else
11437 {
11438 /* items() */
11439 l2 = list_alloc();
11440 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011441 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011442 li->li_tv.vval.v_list = l2;
11443 if (l2 == NULL)
11444 break;
11445 ++l2->lv_refcount;
11446
11447 li2 = listitem_alloc();
11448 if (li2 == NULL)
11449 break;
11450 list_append(l2, li2);
11451 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011452 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011453 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11454
11455 li2 = listitem_alloc();
11456 if (li2 == NULL)
11457 break;
11458 list_append(l2, li2);
11459 copy_tv(&di->di_tv, &li2->li_tv);
11460 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011461 }
11462 }
11463}
11464
11465/*
11466 * "items(dict)" function
11467 */
11468 static void
11469f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011470 typval_T *argvars;
11471 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011472{
11473 dict_list(argvars, rettv, 2);
11474}
11475
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011477 * "join()" function
11478 */
11479 static void
11480f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011481 typval_T *argvars;
11482 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011483{
11484 garray_T ga;
11485 char_u *sep;
11486
11487 rettv->vval.v_number = 0;
11488 if (argvars[0].v_type != VAR_LIST)
11489 {
11490 EMSG(_(e_listreq));
11491 return;
11492 }
11493 if (argvars[0].vval.v_list == NULL)
11494 return;
11495 if (argvars[1].v_type == VAR_UNKNOWN)
11496 sep = (char_u *)" ";
11497 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011498 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011499
11500 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501
11502 if (sep != NULL)
11503 {
11504 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011505 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 ga_append(&ga, NUL);
11507 rettv->vval.v_string = (char_u *)ga.ga_data;
11508 }
11509 else
11510 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011511}
11512
11513/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011514 * "keys()" function
11515 */
11516 static void
11517f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011518 typval_T *argvars;
11519 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011520{
11521 dict_list(argvars, rettv, 0);
11522}
11523
11524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 * "last_buffer_nr()" function.
11526 */
11527/*ARGSUSED*/
11528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011530 typval_T *argvars;
11531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532{
11533 int n = 0;
11534 buf_T *buf;
11535
11536 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11537 if (n < buf->b_fnum)
11538 n = buf->b_fnum;
11539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011541}
11542
11543/*
11544 * "len()" function
11545 */
11546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011547f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011548 typval_T *argvars;
11549 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011550{
11551 switch (argvars[0].v_type)
11552 {
11553 case VAR_STRING:
11554 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011555 rettv->vval.v_number = (varnumber_T)STRLEN(
11556 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011557 break;
11558 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011559 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011560 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011561 case VAR_DICT:
11562 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11563 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011564 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011565 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011566 break;
11567 }
11568}
11569
Bram Moolenaar33570922005-01-25 22:26:29 +000011570static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011571
11572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011573libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011574 typval_T *argvars;
11575 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011576 int type;
11577{
11578#ifdef FEAT_LIBCALL
11579 char_u *string_in;
11580 char_u **string_result;
11581 int nr_result;
11582#endif
11583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011585 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011586 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011587 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011588 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011589
11590 if (check_restricted() || check_secure())
11591 return;
11592
11593#ifdef FEAT_LIBCALL
11594 /* The first two args must be strings, otherwise its meaningless */
11595 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11596 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011597 string_in = NULL;
11598 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011599 string_in = argvars[2].vval.v_string;
11600 if (type == VAR_NUMBER)
11601 string_result = NULL;
11602 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011603 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011604 if (mch_libcall(argvars[0].vval.v_string,
11605 argvars[1].vval.v_string,
11606 string_in,
11607 argvars[2].vval.v_number,
11608 string_result,
11609 &nr_result) == OK
11610 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011611 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011612 }
11613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614}
11615
11616/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011617 * "libcall()" function
11618 */
11619 static void
11620f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011621 typval_T *argvars;
11622 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011623{
11624 libcall_common(argvars, rettv, VAR_STRING);
11625}
11626
11627/*
11628 * "libcallnr()" function
11629 */
11630 static void
11631f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011632 typval_T *argvars;
11633 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011634{
11635 libcall_common(argvars, rettv, VAR_NUMBER);
11636}
11637
11638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 * "line(string)" function
11640 */
11641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011642f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011643 typval_T *argvars;
11644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645{
11646 linenr_T lnum = 0;
11647 pos_T *fp;
11648
11649 fp = var2fpos(&argvars[0], TRUE);
11650 if (fp != NULL)
11651 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011652 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653}
11654
11655/*
11656 * "line2byte(lnum)" function
11657 */
11658/*ARGSUSED*/
11659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011660f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011661 typval_T *argvars;
11662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663{
11664#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666#else
11667 linenr_T lnum;
11668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011669 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11674 if (rettv->vval.v_number >= 0)
11675 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676#endif
11677}
11678
11679/*
11680 * "lispindent(lnum)" function
11681 */
11682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011683f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011684 typval_T *argvars;
11685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686{
11687#ifdef FEAT_LISP
11688 pos_T pos;
11689 linenr_T lnum;
11690
11691 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011692 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11694 {
11695 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 curwin->w_cursor = pos;
11698 }
11699 else
11700#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011701 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702}
11703
11704/*
11705 * "localtime()" function
11706 */
11707/*ARGSUSED*/
11708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011710 typval_T *argvars;
11711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011713 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714}
11715
Bram Moolenaar33570922005-01-25 22:26:29 +000011716static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717
11718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011719get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011720 typval_T *argvars;
11721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 int exact;
11723{
11724 char_u *keys;
11725 char_u *which;
11726 char_u buf[NUMBUFLEN];
11727 char_u *keys_buf = NULL;
11728 char_u *rhs;
11729 int mode;
11730 garray_T ga;
11731
11732 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011733 rettv->v_type = VAR_STRING;
11734 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011736 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 if (*keys == NUL)
11738 return;
11739
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011740 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011741 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011742 else
11743 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011744 if (which == NULL)
11745 return;
11746
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747 mode = get_map_mode(&which, 0);
11748
11749 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011750 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 vim_free(keys_buf);
11752 if (rhs != NULL)
11753 {
11754 ga_init(&ga);
11755 ga.ga_itemsize = 1;
11756 ga.ga_growsize = 40;
11757
11758 while (*rhs != NUL)
11759 ga_concat(&ga, str2special(&rhs, FALSE));
11760
11761 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011762 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 }
11764}
11765
11766/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011767 * "map()" function
11768 */
11769 static void
11770f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011771 typval_T *argvars;
11772 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011773{
11774 filter_map(argvars, rettv, TRUE);
11775}
11776
11777/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011778 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779 */
11780 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011781f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011782 typval_T *argvars;
11783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011785 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786}
11787
11788/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011789 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 */
11791 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011792f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011793 typval_T *argvars;
11794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011796 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797}
11798
Bram Moolenaar33570922005-01-25 22:26:29 +000011799static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800
11801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011803 typval_T *argvars;
11804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 int type;
11806{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011807 char_u *str = NULL;
11808 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 char_u *pat;
11810 regmatch_T regmatch;
11811 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011812 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 char_u *save_cpo;
11814 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011815 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011816 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011817 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011818 list_T *l = NULL;
11819 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011820 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011821 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822
11823 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11824 save_cpo = p_cpo;
11825 p_cpo = (char_u *)"";
11826
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011827 rettv->vval.v_number = -1;
11828 if (type == 3)
11829 {
11830 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011831 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011832 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011833 }
11834 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011836 rettv->v_type = VAR_STRING;
11837 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011840 if (argvars[0].v_type == VAR_LIST)
11841 {
11842 if ((l = argvars[0].vval.v_list) == NULL)
11843 goto theend;
11844 li = l->lv_first;
11845 }
11846 else
11847 expr = str = get_tv_string(&argvars[0]);
11848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011849 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11850 if (pat == NULL)
11851 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011852
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011853 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011855 int error = FALSE;
11856
11857 start = get_tv_number_chk(&argvars[2], &error);
11858 if (error)
11859 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011860 if (l != NULL)
11861 {
11862 li = list_find(l, start);
11863 if (li == NULL)
11864 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011865 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011866 }
11867 else
11868 {
11869 if (start < 0)
11870 start = 0;
11871 if (start > (long)STRLEN(str))
11872 goto theend;
11873 str += start;
11874 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011875
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011876 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011877 nth = get_tv_number_chk(&argvars[3], &error);
11878 if (error)
11879 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880 }
11881
11882 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11883 if (regmatch.regprog != NULL)
11884 {
11885 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011886
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011887 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011888 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011889 if (l != NULL)
11890 {
11891 if (li == NULL)
11892 {
11893 match = FALSE;
11894 break;
11895 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011896 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011897 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011898 if (str == NULL)
11899 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011900 }
11901
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011902 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011903
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011904 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011905 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011906 if (l == NULL && !match)
11907 break;
11908
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011909 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011910 if (l != NULL)
11911 {
11912 li = li->li_next;
11913 ++idx;
11914 }
11915 else
11916 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011917#ifdef FEAT_MBYTE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011918 startcol = regmatch.startp[0]
11919 + (*mb_ptr2len)(regmatch.startp[0]) - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011920#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011921 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011922#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011923 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011924 }
11925
11926 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011928 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011929 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011930 int i;
11931
11932 /* return list with matched string and submatches */
11933 for (i = 0; i < NSUBEXP; ++i)
11934 {
11935 if (regmatch.endp[i] == NULL)
11936 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011937 if (list_append_string(rettv->vval.v_list,
11938 regmatch.startp[i],
11939 (int)(regmatch.endp[i] - regmatch.startp[i]))
11940 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011941 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011942 }
11943 }
11944 else if (type == 2)
11945 {
11946 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011947 if (l != NULL)
11948 copy_tv(&li->li_tv, rettv);
11949 else
11950 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011952 }
11953 else if (l != NULL)
11954 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955 else
11956 {
11957 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959 (varnumber_T)(regmatch.startp[0] - str);
11960 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011961 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011963 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964 }
11965 }
11966 vim_free(regmatch.regprog);
11967 }
11968
11969theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011970 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971 p_cpo = save_cpo;
11972}
11973
11974/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011975 * "match()" function
11976 */
11977 static void
11978f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011979 typval_T *argvars;
11980 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011981{
11982 find_some_match(argvars, rettv, 1);
11983}
11984
11985/*
11986 * "matchend()" function
11987 */
11988 static void
11989f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011990 typval_T *argvars;
11991 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011992{
11993 find_some_match(argvars, rettv, 0);
11994}
11995
11996/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011997 * "matchlist()" function
11998 */
11999 static void
12000f_matchlist(argvars, rettv)
12001 typval_T *argvars;
12002 typval_T *rettv;
12003{
12004 find_some_match(argvars, rettv, 3);
12005}
12006
12007/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012008 * "matchstr()" function
12009 */
12010 static void
12011f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012012 typval_T *argvars;
12013 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012014{
12015 find_some_match(argvars, rettv, 2);
12016}
12017
Bram Moolenaar33570922005-01-25 22:26:29 +000012018static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012019
12020 static void
12021max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012022 typval_T *argvars;
12023 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012024 int domax;
12025{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012026 long n = 0;
12027 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012028 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012029
12030 if (argvars[0].v_type == VAR_LIST)
12031 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012032 list_T *l;
12033 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012034
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012035 l = argvars[0].vval.v_list;
12036 if (l != NULL)
12037 {
12038 li = l->lv_first;
12039 if (li != NULL)
12040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012041 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012042 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012043 {
12044 li = li->li_next;
12045 if (li == NULL)
12046 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012047 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012048 if (domax ? i > n : i < n)
12049 n = i;
12050 }
12051 }
12052 }
12053 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012054 else if (argvars[0].v_type == VAR_DICT)
12055 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012056 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012057 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012058 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012059 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012060
12061 d = argvars[0].vval.v_dict;
12062 if (d != NULL)
12063 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012064 todo = d->dv_hashtab.ht_used;
12065 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012066 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012067 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012068 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012069 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012070 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012071 if (first)
12072 {
12073 n = i;
12074 first = FALSE;
12075 }
12076 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012077 n = i;
12078 }
12079 }
12080 }
12081 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012082 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012083 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012084 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012085}
12086
12087/*
12088 * "max()" function
12089 */
12090 static void
12091f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012092 typval_T *argvars;
12093 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012094{
12095 max_min(argvars, rettv, TRUE);
12096}
12097
12098/*
12099 * "min()" function
12100 */
12101 static void
12102f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012103 typval_T *argvars;
12104 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012105{
12106 max_min(argvars, rettv, FALSE);
12107}
12108
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012109static int mkdir_recurse __ARGS((char_u *dir, int prot));
12110
12111/*
12112 * Create the directory in which "dir" is located, and higher levels when
12113 * needed.
12114 */
12115 static int
12116mkdir_recurse(dir, prot)
12117 char_u *dir;
12118 int prot;
12119{
12120 char_u *p;
12121 char_u *updir;
12122 int r = FAIL;
12123
12124 /* Get end of directory name in "dir".
12125 * We're done when it's "/" or "c:/". */
12126 p = gettail_sep(dir);
12127 if (p <= get_past_head(dir))
12128 return OK;
12129
12130 /* If the directory exists we're done. Otherwise: create it.*/
12131 updir = vim_strnsave(dir, (int)(p - dir));
12132 if (updir == NULL)
12133 return FAIL;
12134 if (mch_isdir(updir))
12135 r = OK;
12136 else if (mkdir_recurse(updir, prot) == OK)
12137 r = vim_mkdir_emsg(updir, prot);
12138 vim_free(updir);
12139 return r;
12140}
12141
12142#ifdef vim_mkdir
12143/*
12144 * "mkdir()" function
12145 */
12146 static void
12147f_mkdir(argvars, rettv)
12148 typval_T *argvars;
12149 typval_T *rettv;
12150{
12151 char_u *dir;
12152 char_u buf[NUMBUFLEN];
12153 int prot = 0755;
12154
12155 rettv->vval.v_number = FAIL;
12156 if (check_restricted() || check_secure())
12157 return;
12158
12159 dir = get_tv_string_buf(&argvars[0], buf);
12160 if (argvars[1].v_type != VAR_UNKNOWN)
12161 {
12162 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012163 prot = get_tv_number_chk(&argvars[2], NULL);
12164 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012165 mkdir_recurse(dir, prot);
12166 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012167 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012168}
12169#endif
12170
Bram Moolenaar0d660222005-01-07 21:51:51 +000012171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172 * "mode()" function
12173 */
12174/*ARGSUSED*/
12175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012176f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012177 typval_T *argvars;
12178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179{
12180 char_u buf[2];
12181
12182#ifdef FEAT_VISUAL
12183 if (VIsual_active)
12184 {
12185 if (VIsual_select)
12186 buf[0] = VIsual_mode + 's' - 'v';
12187 else
12188 buf[0] = VIsual_mode;
12189 }
12190 else
12191#endif
12192 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12193 buf[0] = 'r';
12194 else if (State & INSERT)
12195 {
12196 if (State & REPLACE_FLAG)
12197 buf[0] = 'R';
12198 else
12199 buf[0] = 'i';
12200 }
12201 else if (State & CMDLINE)
12202 buf[0] = 'c';
12203 else
12204 buf[0] = 'n';
12205
12206 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012207 rettv->vval.v_string = vim_strsave(buf);
12208 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209}
12210
12211/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012212 * "nextnonblank()" function
12213 */
12214 static void
12215f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012216 typval_T *argvars;
12217 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012218{
12219 linenr_T lnum;
12220
12221 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012223 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012224 {
12225 lnum = 0;
12226 break;
12227 }
12228 if (*skipwhite(ml_get(lnum)) != NUL)
12229 break;
12230 }
12231 rettv->vval.v_number = lnum;
12232}
12233
12234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235 * "nr2char()" function
12236 */
12237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012238f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012239 typval_T *argvars;
12240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241{
12242 char_u buf[NUMBUFLEN];
12243
12244#ifdef FEAT_MBYTE
12245 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012246 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247 else
12248#endif
12249 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012250 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251 buf[1] = NUL;
12252 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012253 rettv->v_type = VAR_STRING;
12254 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012255}
12256
12257/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012258 * "prevnonblank()" function
12259 */
12260 static void
12261f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012262 typval_T *argvars;
12263 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012264{
12265 linenr_T lnum;
12266
12267 lnum = get_tv_lnum(argvars);
12268 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12269 lnum = 0;
12270 else
12271 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12272 --lnum;
12273 rettv->vval.v_number = lnum;
12274}
12275
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012276#ifdef HAVE_STDARG_H
12277/* This dummy va_list is here because:
12278 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12279 * - locally in the function results in a "used before set" warning
12280 * - using va_start() to initialize it gives "function with fixed args" error */
12281static va_list ap;
12282#endif
12283
Bram Moolenaar8c711452005-01-14 21:53:12 +000012284/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012285 * "printf()" function
12286 */
12287 static void
12288f_printf(argvars, rettv)
12289 typval_T *argvars;
12290 typval_T *rettv;
12291{
12292 rettv->v_type = VAR_STRING;
12293 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012294#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012295 {
12296 char_u buf[NUMBUFLEN];
12297 int len;
12298 char_u *s;
12299 int saved_did_emsg = did_emsg;
12300 char *fmt;
12301
12302 /* Get the required length, allocate the buffer and do it for real. */
12303 did_emsg = FALSE;
12304 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012305 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012306 if (!did_emsg)
12307 {
12308 s = alloc(len + 1);
12309 if (s != NULL)
12310 {
12311 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012312 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012313 }
12314 }
12315 did_emsg |= saved_did_emsg;
12316 }
12317#endif
12318}
12319
12320/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012321 * "pumvisible()" function
12322 */
12323/*ARGSUSED*/
12324 static void
12325f_pumvisible(argvars, rettv)
12326 typval_T *argvars;
12327 typval_T *rettv;
12328{
12329 rettv->vval.v_number = 0;
12330#ifdef FEAT_INS_EXPAND
12331 if (pum_visible())
12332 rettv->vval.v_number = 1;
12333#endif
12334}
12335
12336/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012337 * "range()" function
12338 */
12339 static void
12340f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012341 typval_T *argvars;
12342 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012343{
12344 long start;
12345 long end;
12346 long stride = 1;
12347 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012348 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012349
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012350 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012351 if (argvars[1].v_type == VAR_UNKNOWN)
12352 {
12353 end = start - 1;
12354 start = 0;
12355 }
12356 else
12357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012358 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012359 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012360 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012361 }
12362
12363 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012364 if (error)
12365 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012366 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012367 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012368 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012369 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012370 else
12371 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012372 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012373 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012374 if (list_append_number(rettv->vval.v_list,
12375 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012376 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012377 }
12378}
12379
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012380/*
12381 * "readfile()" function
12382 */
12383 static void
12384f_readfile(argvars, rettv)
12385 typval_T *argvars;
12386 typval_T *rettv;
12387{
12388 int binary = FALSE;
12389 char_u *fname;
12390 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012391 listitem_T *li;
12392#define FREAD_SIZE 200 /* optimized for text lines */
12393 char_u buf[FREAD_SIZE];
12394 int readlen; /* size of last fread() */
12395 int buflen; /* nr of valid chars in buf[] */
12396 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12397 int tolist; /* first byte in buf[] still to be put in list */
12398 int chop; /* how many CR to chop off */
12399 char_u *prev = NULL; /* previously read bytes, if any */
12400 int prevlen = 0; /* length of "prev" if not NULL */
12401 char_u *s;
12402 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012403 long maxline = MAXLNUM;
12404 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012405
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012406 if (argvars[1].v_type != VAR_UNKNOWN)
12407 {
12408 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12409 binary = TRUE;
12410 if (argvars[2].v_type != VAR_UNKNOWN)
12411 maxline = get_tv_number(&argvars[2]);
12412 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012413
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012414 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012415 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012416
12417 /* Always open the file in binary mode, library functions have a mind of
12418 * their own about CR-LF conversion. */
12419 fname = get_tv_string(&argvars[0]);
12420 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12421 {
12422 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12423 return;
12424 }
12425
12426 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012427 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012428 {
12429 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12430 buflen = filtd + readlen;
12431 tolist = 0;
12432 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12433 {
12434 if (buf[filtd] == '\n' || readlen <= 0)
12435 {
12436 /* Only when in binary mode add an empty list item when the
12437 * last line ends in a '\n'. */
12438 if (!binary && readlen == 0 && filtd == 0)
12439 break;
12440
12441 /* Found end-of-line or end-of-file: add a text line to the
12442 * list. */
12443 chop = 0;
12444 if (!binary)
12445 while (filtd - chop - 1 >= tolist
12446 && buf[filtd - chop - 1] == '\r')
12447 ++chop;
12448 len = filtd - tolist - chop;
12449 if (prev == NULL)
12450 s = vim_strnsave(buf + tolist, len);
12451 else
12452 {
12453 s = alloc((unsigned)(prevlen + len + 1));
12454 if (s != NULL)
12455 {
12456 mch_memmove(s, prev, prevlen);
12457 vim_free(prev);
12458 prev = NULL;
12459 mch_memmove(s + prevlen, buf + tolist, len);
12460 s[prevlen + len] = NUL;
12461 }
12462 }
12463 tolist = filtd + 1;
12464
12465 li = listitem_alloc();
12466 if (li == NULL)
12467 {
12468 vim_free(s);
12469 break;
12470 }
12471 li->li_tv.v_type = VAR_STRING;
12472 li->li_tv.v_lock = 0;
12473 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012474 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012475
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012476 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012477 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012478 if (readlen <= 0)
12479 break;
12480 }
12481 else if (buf[filtd] == NUL)
12482 buf[filtd] = '\n';
12483 }
12484 if (readlen <= 0)
12485 break;
12486
12487 if (tolist == 0)
12488 {
12489 /* "buf" is full, need to move text to an allocated buffer */
12490 if (prev == NULL)
12491 {
12492 prev = vim_strnsave(buf, buflen);
12493 prevlen = buflen;
12494 }
12495 else
12496 {
12497 s = alloc((unsigned)(prevlen + buflen));
12498 if (s != NULL)
12499 {
12500 mch_memmove(s, prev, prevlen);
12501 mch_memmove(s + prevlen, buf, buflen);
12502 vim_free(prev);
12503 prev = s;
12504 prevlen += buflen;
12505 }
12506 }
12507 filtd = 0;
12508 }
12509 else
12510 {
12511 mch_memmove(buf, buf + tolist, buflen - tolist);
12512 filtd -= tolist;
12513 }
12514 }
12515
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012516 /*
12517 * For a negative line count use only the lines at the end of the file,
12518 * free the rest.
12519 */
12520 if (maxline < 0)
12521 while (cnt > -maxline)
12522 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012523 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012524 --cnt;
12525 }
12526
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012527 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012528 fclose(fd);
12529}
12530
12531
Bram Moolenaar0d660222005-01-07 21:51:51 +000012532#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12533static void make_connection __ARGS((void));
12534static int check_connection __ARGS((void));
12535
12536 static void
12537make_connection()
12538{
12539 if (X_DISPLAY == NULL
12540# ifdef FEAT_GUI
12541 && !gui.in_use
12542# endif
12543 )
12544 {
12545 x_force_connect = TRUE;
12546 setup_term_clip();
12547 x_force_connect = FALSE;
12548 }
12549}
12550
12551 static int
12552check_connection()
12553{
12554 make_connection();
12555 if (X_DISPLAY == NULL)
12556 {
12557 EMSG(_("E240: No connection to Vim server"));
12558 return FAIL;
12559 }
12560 return OK;
12561}
12562#endif
12563
12564#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012565static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012566
12567 static void
12568remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012569 typval_T *argvars;
12570 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012571 int expr;
12572{
12573 char_u *server_name;
12574 char_u *keys;
12575 char_u *r = NULL;
12576 char_u buf[NUMBUFLEN];
12577# ifdef WIN32
12578 HWND w;
12579# else
12580 Window w;
12581# endif
12582
12583 if (check_restricted() || check_secure())
12584 return;
12585
12586# ifdef FEAT_X11
12587 if (check_connection() == FAIL)
12588 return;
12589# endif
12590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012591 server_name = get_tv_string_chk(&argvars[0]);
12592 if (server_name == NULL)
12593 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012594 keys = get_tv_string_buf(&argvars[1], buf);
12595# ifdef WIN32
12596 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12597# else
12598 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12599 < 0)
12600# endif
12601 {
12602 if (r != NULL)
12603 EMSG(r); /* sending worked but evaluation failed */
12604 else
12605 EMSG2(_("E241: Unable to send to %s"), server_name);
12606 return;
12607 }
12608
12609 rettv->vval.v_string = r;
12610
12611 if (argvars[2].v_type != VAR_UNKNOWN)
12612 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012613 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012614 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012615 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012616
12617 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012618 v.di_tv.v_type = VAR_STRING;
12619 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012620 idvar = get_tv_string_chk(&argvars[2]);
12621 if (idvar != NULL)
12622 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012623 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012624 }
12625}
12626#endif
12627
12628/*
12629 * "remote_expr()" function
12630 */
12631/*ARGSUSED*/
12632 static void
12633f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012634 typval_T *argvars;
12635 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012636{
12637 rettv->v_type = VAR_STRING;
12638 rettv->vval.v_string = NULL;
12639#ifdef FEAT_CLIENTSERVER
12640 remote_common(argvars, rettv, TRUE);
12641#endif
12642}
12643
12644/*
12645 * "remote_foreground()" function
12646 */
12647/*ARGSUSED*/
12648 static void
12649f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012650 typval_T *argvars;
12651 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012652{
12653 rettv->vval.v_number = 0;
12654#ifdef FEAT_CLIENTSERVER
12655# ifdef WIN32
12656 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012657 {
12658 char_u *server_name = get_tv_string_chk(&argvars[0]);
12659
12660 if (server_name != NULL)
12661 serverForeground(server_name);
12662 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012663# else
12664 /* Send a foreground() expression to the server. */
12665 argvars[1].v_type = VAR_STRING;
12666 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12667 argvars[2].v_type = VAR_UNKNOWN;
12668 remote_common(argvars, rettv, TRUE);
12669 vim_free(argvars[1].vval.v_string);
12670# endif
12671#endif
12672}
12673
12674/*ARGSUSED*/
12675 static void
12676f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012677 typval_T *argvars;
12678 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012679{
12680#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012681 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012682 char_u *s = NULL;
12683# ifdef WIN32
12684 int n = 0;
12685# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012686 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012687
12688 if (check_restricted() || check_secure())
12689 {
12690 rettv->vval.v_number = -1;
12691 return;
12692 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012693 serverid = get_tv_string_chk(&argvars[0]);
12694 if (serverid == NULL)
12695 {
12696 rettv->vval.v_number = -1;
12697 return; /* type error; errmsg already given */
12698 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012699# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012700 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012701 if (n == 0)
12702 rettv->vval.v_number = -1;
12703 else
12704 {
12705 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12706 rettv->vval.v_number = (s != NULL);
12707 }
12708# else
12709 rettv->vval.v_number = 0;
12710 if (check_connection() == FAIL)
12711 return;
12712
12713 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012714 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012715# endif
12716
12717 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12718 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012719 char_u *retvar;
12720
Bram Moolenaar33570922005-01-25 22:26:29 +000012721 v.di_tv.v_type = VAR_STRING;
12722 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012723 retvar = get_tv_string_chk(&argvars[1]);
12724 if (retvar != NULL)
12725 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012726 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012727 }
12728#else
12729 rettv->vval.v_number = -1;
12730#endif
12731}
12732
12733/*ARGSUSED*/
12734 static void
12735f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012736 typval_T *argvars;
12737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012738{
12739 char_u *r = NULL;
12740
12741#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012742 char_u *serverid = get_tv_string_chk(&argvars[0]);
12743
12744 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012745 {
12746# ifdef WIN32
12747 /* The server's HWND is encoded in the 'id' parameter */
12748 int n = 0;
12749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012750 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012751 if (n != 0)
12752 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12753 if (r == NULL)
12754# else
12755 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012756 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012757# endif
12758 EMSG(_("E277: Unable to read a server reply"));
12759 }
12760#endif
12761 rettv->v_type = VAR_STRING;
12762 rettv->vval.v_string = r;
12763}
12764
12765/*
12766 * "remote_send()" function
12767 */
12768/*ARGSUSED*/
12769 static void
12770f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012771 typval_T *argvars;
12772 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012773{
12774 rettv->v_type = VAR_STRING;
12775 rettv->vval.v_string = NULL;
12776#ifdef FEAT_CLIENTSERVER
12777 remote_common(argvars, rettv, FALSE);
12778#endif
12779}
12780
12781/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012782 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012783 */
12784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012786 typval_T *argvars;
12787 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012788{
Bram Moolenaar33570922005-01-25 22:26:29 +000012789 list_T *l;
12790 listitem_T *item, *item2;
12791 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012792 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012793 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012794 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012795 dict_T *d;
12796 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012797
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012798 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012799 if (argvars[0].v_type == VAR_DICT)
12800 {
12801 if (argvars[2].v_type != VAR_UNKNOWN)
12802 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012803 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012804 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012806 key = get_tv_string_chk(&argvars[1]);
12807 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012809 di = dict_find(d, key, -1);
12810 if (di == NULL)
12811 EMSG2(_(e_dictkey), key);
12812 else
12813 {
12814 *rettv = di->di_tv;
12815 init_tv(&di->di_tv);
12816 dictitem_remove(d, di);
12817 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012818 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012819 }
12820 }
12821 else if (argvars[0].v_type != VAR_LIST)
12822 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012823 else if ((l = argvars[0].vval.v_list) != NULL
12824 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012826 int error = FALSE;
12827
12828 idx = get_tv_number_chk(&argvars[1], &error);
12829 if (error)
12830 ; /* type error: do nothing, errmsg already given */
12831 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012832 EMSGN(_(e_listidx), idx);
12833 else
12834 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012835 if (argvars[2].v_type == VAR_UNKNOWN)
12836 {
12837 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012838 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012839 *rettv = item->li_tv;
12840 vim_free(item);
12841 }
12842 else
12843 {
12844 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012845 end = get_tv_number_chk(&argvars[2], &error);
12846 if (error)
12847 ; /* type error: do nothing */
12848 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012849 EMSGN(_(e_listidx), end);
12850 else
12851 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012852 int cnt = 0;
12853
12854 for (li = item; li != NULL; li = li->li_next)
12855 {
12856 ++cnt;
12857 if (li == item2)
12858 break;
12859 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012860 if (li == NULL) /* didn't find "item2" after "item" */
12861 EMSG(_(e_invrange));
12862 else
12863 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012864 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012865 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012866 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012867 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012868 l->lv_first = item;
12869 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012870 item->li_prev = NULL;
12871 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012872 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012873 }
12874 }
12875 }
12876 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012877 }
12878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879}
12880
12881/*
12882 * "rename({from}, {to})" function
12883 */
12884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885f_rename(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 buf[NUMBUFLEN];
12890
12891 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012892 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12895 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896}
12897
12898/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012899 * "repeat()" function
12900 */
12901/*ARGSUSED*/
12902 static void
12903f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012904 typval_T *argvars;
12905 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012906{
12907 char_u *p;
12908 int n;
12909 int slen;
12910 int len;
12911 char_u *r;
12912 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012913
12914 n = get_tv_number(&argvars[1]);
12915 if (argvars[0].v_type == VAR_LIST)
12916 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012917 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012918 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012919 if (list_extend(rettv->vval.v_list,
12920 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012921 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012922 }
12923 else
12924 {
12925 p = get_tv_string(&argvars[0]);
12926 rettv->v_type = VAR_STRING;
12927 rettv->vval.v_string = NULL;
12928
12929 slen = (int)STRLEN(p);
12930 len = slen * n;
12931 if (len <= 0)
12932 return;
12933
12934 r = alloc(len + 1);
12935 if (r != NULL)
12936 {
12937 for (i = 0; i < n; i++)
12938 mch_memmove(r + i * slen, p, (size_t)slen);
12939 r[len] = NUL;
12940 }
12941
12942 rettv->vval.v_string = r;
12943 }
12944}
12945
12946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947 * "resolve()" function
12948 */
12949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012950f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012951 typval_T *argvars;
12952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953{
12954 char_u *p;
12955
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012956 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957#ifdef FEAT_SHORTCUT
12958 {
12959 char_u *v = NULL;
12960
12961 v = mch_resolve_shortcut(p);
12962 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012963 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012965 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966 }
12967#else
12968# ifdef HAVE_READLINK
12969 {
12970 char_u buf[MAXPATHL + 1];
12971 char_u *cpy;
12972 int len;
12973 char_u *remain = NULL;
12974 char_u *q;
12975 int is_relative_to_current = FALSE;
12976 int has_trailing_pathsep = FALSE;
12977 int limit = 100;
12978
12979 p = vim_strsave(p);
12980
12981 if (p[0] == '.' && (vim_ispathsep(p[1])
12982 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12983 is_relative_to_current = TRUE;
12984
12985 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012986 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987 has_trailing_pathsep = TRUE;
12988
12989 q = getnextcomp(p);
12990 if (*q != NUL)
12991 {
12992 /* Separate the first path component in "p", and keep the
12993 * remainder (beginning with the path separator). */
12994 remain = vim_strsave(q - 1);
12995 q[-1] = NUL;
12996 }
12997
12998 for (;;)
12999 {
13000 for (;;)
13001 {
13002 len = readlink((char *)p, (char *)buf, MAXPATHL);
13003 if (len <= 0)
13004 break;
13005 buf[len] = NUL;
13006
13007 if (limit-- == 0)
13008 {
13009 vim_free(p);
13010 vim_free(remain);
13011 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013012 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013 goto fail;
13014 }
13015
13016 /* Ensure that the result will have a trailing path separator
13017 * if the argument has one. */
13018 if (remain == NULL && has_trailing_pathsep)
13019 add_pathsep(buf);
13020
13021 /* Separate the first path component in the link value and
13022 * concatenate the remainders. */
13023 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13024 if (*q != NUL)
13025 {
13026 if (remain == NULL)
13027 remain = vim_strsave(q - 1);
13028 else
13029 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013030 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 if (cpy != NULL)
13032 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033 vim_free(remain);
13034 remain = cpy;
13035 }
13036 }
13037 q[-1] = NUL;
13038 }
13039
13040 q = gettail(p);
13041 if (q > p && *q == NUL)
13042 {
13043 /* Ignore trailing path separator. */
13044 q[-1] = NUL;
13045 q = gettail(p);
13046 }
13047 if (q > p && !mch_isFullName(buf))
13048 {
13049 /* symlink is relative to directory of argument */
13050 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13051 if (cpy != NULL)
13052 {
13053 STRCPY(cpy, p);
13054 STRCPY(gettail(cpy), buf);
13055 vim_free(p);
13056 p = cpy;
13057 }
13058 }
13059 else
13060 {
13061 vim_free(p);
13062 p = vim_strsave(buf);
13063 }
13064 }
13065
13066 if (remain == NULL)
13067 break;
13068
13069 /* Append the first path component of "remain" to "p". */
13070 q = getnextcomp(remain + 1);
13071 len = q - remain - (*q != NUL);
13072 cpy = vim_strnsave(p, STRLEN(p) + len);
13073 if (cpy != NULL)
13074 {
13075 STRNCAT(cpy, remain, len);
13076 vim_free(p);
13077 p = cpy;
13078 }
13079 /* Shorten "remain". */
13080 if (*q != NUL)
13081 STRCPY(remain, q - 1);
13082 else
13083 {
13084 vim_free(remain);
13085 remain = NULL;
13086 }
13087 }
13088
13089 /* If the result is a relative path name, make it explicitly relative to
13090 * the current directory if and only if the argument had this form. */
13091 if (!vim_ispathsep(*p))
13092 {
13093 if (is_relative_to_current
13094 && *p != NUL
13095 && !(p[0] == '.'
13096 && (p[1] == NUL
13097 || vim_ispathsep(p[1])
13098 || (p[1] == '.'
13099 && (p[2] == NUL
13100 || vim_ispathsep(p[2]))))))
13101 {
13102 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013103 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104 if (cpy != NULL)
13105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106 vim_free(p);
13107 p = cpy;
13108 }
13109 }
13110 else if (!is_relative_to_current)
13111 {
13112 /* Strip leading "./". */
13113 q = p;
13114 while (q[0] == '.' && vim_ispathsep(q[1]))
13115 q += 2;
13116 if (q > p)
13117 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13118 }
13119 }
13120
13121 /* Ensure that the result will have no trailing path separator
13122 * if the argument had none. But keep "/" or "//". */
13123 if (!has_trailing_pathsep)
13124 {
13125 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013126 if (after_pathsep(p, q))
13127 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128 }
13129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013130 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131 }
13132# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013133 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134# endif
13135#endif
13136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013137 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138
13139#ifdef HAVE_READLINK
13140fail:
13141#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013142 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143}
13144
13145/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013146 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 */
13148 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013149f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013150 typval_T *argvars;
13151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152{
Bram Moolenaar33570922005-01-25 22:26:29 +000013153 list_T *l;
13154 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155
Bram Moolenaar0d660222005-01-07 21:51:51 +000013156 rettv->vval.v_number = 0;
13157 if (argvars[0].v_type != VAR_LIST)
13158 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013159 else if ((l = argvars[0].vval.v_list) != NULL
13160 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013161 {
13162 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013163 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013164 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013165 while (li != NULL)
13166 {
13167 ni = li->li_prev;
13168 list_append(l, li);
13169 li = ni;
13170 }
13171 rettv->vval.v_list = l;
13172 rettv->v_type = VAR_LIST;
13173 ++l->lv_refcount;
13174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175}
13176
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013177#define SP_NOMOVE 1 /* don't move cursor */
13178#define SP_REPEAT 2 /* repeat to find outer pair */
13179#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000013180#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013181
Bram Moolenaar33570922005-01-25 22:26:29 +000013182static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013183
13184/*
13185 * Get flags for a search function.
13186 * Possibly sets "p_ws".
13187 * Returns BACKWARD, FORWARD or zero (for an error).
13188 */
13189 static int
13190get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013191 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013192 int *flagsp;
13193{
13194 int dir = FORWARD;
13195 char_u *flags;
13196 char_u nbuf[NUMBUFLEN];
13197 int mask;
13198
13199 if (varp->v_type != VAR_UNKNOWN)
13200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013201 flags = get_tv_string_buf_chk(varp, nbuf);
13202 if (flags == NULL)
13203 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013204 while (*flags != NUL)
13205 {
13206 switch (*flags)
13207 {
13208 case 'b': dir = BACKWARD; break;
13209 case 'w': p_ws = TRUE; break;
13210 case 'W': p_ws = FALSE; break;
13211 default: mask = 0;
13212 if (flagsp != NULL)
13213 switch (*flags)
13214 {
13215 case 'n': mask = SP_NOMOVE; break;
13216 case 'r': mask = SP_REPEAT; break;
13217 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013218 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013219 }
13220 if (mask == 0)
13221 {
13222 EMSG2(_(e_invarg2), flags);
13223 dir = 0;
13224 }
13225 else
13226 *flagsp |= mask;
13227 }
13228 if (dir == 0)
13229 break;
13230 ++flags;
13231 }
13232 }
13233 return dir;
13234}
13235
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013237 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013239 static int
13240search_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013241 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013242 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243{
13244 char_u *pat;
13245 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013246 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 int save_p_ws = p_ws;
13248 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013249 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013250 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013251 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013253 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013254 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13255 if (dir == 0)
13256 goto theend;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013257
13258 /* Optional extra argument: line number to stop searching. */
13259 if (argvars[1].v_type != VAR_UNKNOWN
13260 && argvars[2].v_type != VAR_UNKNOWN)
13261 {
13262 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13263 if (lnum_stop < 0)
13264 goto theend;
13265 }
13266
Bram Moolenaar231334e2005-07-25 20:46:57 +000013267 /*
13268 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13269 * Check to make sure only those flags are set.
13270 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13271 * flags cannot be set. Check for that condition also.
13272 */
13273 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13274 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013276 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013277 goto theend;
13278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013280 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013282 SEARCH_KEEP, RE_SEARCH, (linenr_T)lnum_stop) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013284 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013285 if (flags & SP_SETPCMARK)
13286 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013288 if (match_pos != NULL)
13289 {
13290 /* Store the match cursor position */
13291 match_pos->lnum = pos.lnum;
13292 match_pos->col = pos.col + 1;
13293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294 /* "/$" will put the cursor after the end of the line, may need to
13295 * correct that here */
13296 check_cursor();
13297 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013298
13299 /* If 'n' flag is used: restore cursor position. */
13300 if (flags & SP_NOMOVE)
13301 curwin->w_cursor = save_cursor;
13302theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013304
13305 return retval;
13306}
13307
13308/*
13309 * "search()" function
13310 */
13311 static void
13312f_search(argvars, rettv)
13313 typval_T *argvars;
13314 typval_T *rettv;
13315{
13316 rettv->vval.v_number = search_cmn(argvars, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317}
13318
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013320 * "searchdecl()" function
13321 */
13322 static void
13323f_searchdecl(argvars, rettv)
13324 typval_T *argvars;
13325 typval_T *rettv;
13326{
13327 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013328 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013329 int error = FALSE;
13330 char_u *name;
13331
13332 rettv->vval.v_number = 1; /* default: FAIL */
13333
13334 name = get_tv_string_chk(&argvars[0]);
13335 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013336 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013337 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013338 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13339 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13340 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013341 if (!error && name != NULL)
13342 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013343 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013344}
13345
13346/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013347 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013349 static int
13350searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013351 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013352 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353{
13354 char_u *spat, *mpat, *epat;
13355 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357 int dir;
13358 int flags = 0;
13359 char_u nbuf1[NUMBUFLEN];
13360 char_u nbuf2[NUMBUFLEN];
13361 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013362 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013363 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013366 spat = get_tv_string_chk(&argvars[0]);
13367 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13368 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13369 if (spat == NULL || mpat == NULL || epat == NULL)
13370 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372 /* Handle the optional fourth argument: flags */
13373 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013374 if (dir == 0)
13375 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013376 /*
13377 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13378 */
13379 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13380 {
13381 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13382 goto theend;
13383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013385 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013386 if (argvars[3].v_type == VAR_UNKNOWN
13387 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388 skip = (char_u *)"";
13389 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013391 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013392 if (argvars[5].v_type != VAR_UNKNOWN)
13393 {
13394 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13395 if (lnum_stop < 0)
13396 goto theend;
13397 }
13398 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013399 if (skip == NULL)
13400 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013402 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13403 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013404
13405theend:
13406 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013407
13408 return retval;
13409}
13410
13411/*
13412 * "searchpair()" function
13413 */
13414 static void
13415f_searchpair(argvars, rettv)
13416 typval_T *argvars;
13417 typval_T *rettv;
13418{
13419 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13420}
13421
13422/*
13423 * "searchpairpos()" function
13424 */
13425 static void
13426f_searchpairpos(argvars, rettv)
13427 typval_T *argvars;
13428 typval_T *rettv;
13429{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013430 pos_T match_pos;
13431 int lnum = 0;
13432 int col = 0;
13433
13434 rettv->vval.v_number = 0;
13435
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013436 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013437 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013438
13439 if (searchpair_cmn(argvars, &match_pos) > 0)
13440 {
13441 lnum = match_pos.lnum;
13442 col = match_pos.col;
13443 }
13444
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013445 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13446 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013447}
13448
13449/*
13450 * Search for a start/middle/end thing.
13451 * Used by searchpair(), see its documentation for the details.
13452 * Returns 0 or -1 for no match,
13453 */
13454 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013455do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013456 char_u *spat; /* start pattern */
13457 char_u *mpat; /* middle pattern */
13458 char_u *epat; /* end pattern */
13459 int dir; /* BACKWARD or FORWARD */
13460 char_u *skip; /* skip expression */
13461 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013462 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013463 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013464{
13465 char_u *save_cpo;
13466 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13467 long retval = 0;
13468 pos_T pos;
13469 pos_T firstpos;
13470 pos_T foundpos;
13471 pos_T save_cursor;
13472 pos_T save_pos;
13473 int n;
13474 int r;
13475 int nest = 1;
13476 int err;
13477
13478 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13479 save_cpo = p_cpo;
13480 p_cpo = (char_u *)"";
13481
13482 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13483 * start/middle/end (pat3, for the top pair). */
13484 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13485 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13486 if (pat2 == NULL || pat3 == NULL)
13487 goto theend;
13488 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13489 if (*mpat == NUL)
13490 STRCPY(pat3, pat2);
13491 else
13492 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13493 spat, epat, mpat);
13494
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495 save_cursor = curwin->w_cursor;
13496 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013497 clearpos(&firstpos);
13498 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499 pat = pat3;
13500 for (;;)
13501 {
13502 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013503 SEARCH_KEEP, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13505 /* didn't find it or found the first match again: FAIL */
13506 break;
13507
13508 if (firstpos.lnum == 0)
13509 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013510 if (equalpos(pos, foundpos))
13511 {
13512 /* Found the same position again. Can happen with a pattern that
13513 * has "\zs" at the end and searching backwards. Advance one
13514 * character and try again. */
13515 if (dir == BACKWARD)
13516 decl(&pos);
13517 else
13518 incl(&pos);
13519 }
13520 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521
13522 /* If the skip pattern matches, ignore this match. */
13523 if (*skip != NUL)
13524 {
13525 save_pos = curwin->w_cursor;
13526 curwin->w_cursor = pos;
13527 r = eval_to_bool(skip, &err, NULL, FALSE);
13528 curwin->w_cursor = save_pos;
13529 if (err)
13530 {
13531 /* Evaluating {skip} caused an error, break here. */
13532 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013533 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 break;
13535 }
13536 if (r)
13537 continue;
13538 }
13539
13540 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13541 {
13542 /* Found end when searching backwards or start when searching
13543 * forward: nested pair. */
13544 ++nest;
13545 pat = pat2; /* nested, don't search for middle */
13546 }
13547 else
13548 {
13549 /* Found end when searching forward or start when searching
13550 * backward: end of (nested) pair; or found middle in outer pair. */
13551 if (--nest == 1)
13552 pat = pat3; /* outer level, search for middle */
13553 }
13554
13555 if (nest == 0)
13556 {
13557 /* Found the match: return matchcount or line number. */
13558 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013559 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013561 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013562 if (flags & SP_SETPCMARK)
13563 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 curwin->w_cursor = pos;
13565 if (!(flags & SP_REPEAT))
13566 break;
13567 nest = 1; /* search for next unmatched */
13568 }
13569 }
13570
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013571 if (match_pos != NULL)
13572 {
13573 /* Store the match cursor position */
13574 match_pos->lnum = curwin->w_cursor.lnum;
13575 match_pos->col = curwin->w_cursor.col + 1;
13576 }
13577
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013579 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 curwin->w_cursor = save_cursor;
13581
13582theend:
13583 vim_free(pat2);
13584 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013586
13587 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588}
13589
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013590/*
13591 * "searchpos()" function
13592 */
13593 static void
13594f_searchpos(argvars, rettv)
13595 typval_T *argvars;
13596 typval_T *rettv;
13597{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013598 pos_T match_pos;
13599 int lnum = 0;
13600 int col = 0;
13601
13602 rettv->vval.v_number = 0;
13603
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013604 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013605 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013606
13607 if (search_cmn(argvars, &match_pos) > 0)
13608 {
13609 lnum = match_pos.lnum;
13610 col = match_pos.col;
13611 }
13612
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013613 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13614 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013615
13616}
13617
13618
Bram Moolenaar0d660222005-01-07 21:51:51 +000013619/*ARGSUSED*/
13620 static void
13621f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013622 typval_T *argvars;
13623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013625#ifdef FEAT_CLIENTSERVER
13626 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013627 char_u *server = get_tv_string_chk(&argvars[0]);
13628 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629
Bram Moolenaar0d660222005-01-07 21:51:51 +000013630 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013631 if (server == NULL || reply == NULL)
13632 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013633 if (check_restricted() || check_secure())
13634 return;
13635# ifdef FEAT_X11
13636 if (check_connection() == FAIL)
13637 return;
13638# endif
13639
13640 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013642 EMSG(_("E258: Unable to send to client"));
13643 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013645 rettv->vval.v_number = 0;
13646#else
13647 rettv->vval.v_number = -1;
13648#endif
13649}
13650
13651/*ARGSUSED*/
13652 static void
13653f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013654 typval_T *argvars;
13655 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013656{
13657 char_u *r = NULL;
13658
13659#ifdef FEAT_CLIENTSERVER
13660# ifdef WIN32
13661 r = serverGetVimNames();
13662# else
13663 make_connection();
13664 if (X_DISPLAY != NULL)
13665 r = serverGetVimNames(X_DISPLAY);
13666# endif
13667#endif
13668 rettv->v_type = VAR_STRING;
13669 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670}
13671
13672/*
13673 * "setbufvar()" function
13674 */
13675/*ARGSUSED*/
13676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013678 typval_T *argvars;
13679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680{
13681 buf_T *buf;
13682#ifdef FEAT_AUTOCMD
13683 aco_save_T aco;
13684#else
13685 buf_T *save_curbuf;
13686#endif
13687 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013688 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 char_u nbuf[NUMBUFLEN];
13690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013691 rettv->vval.v_number = 0;
13692
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 if (check_restricted() || check_secure())
13694 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013695 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13696 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013697 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698 varp = &argvars[2];
13699
13700 if (buf != NULL && varname != NULL && varp != NULL)
13701 {
13702 /* set curbuf to be our buf, temporarily */
13703#ifdef FEAT_AUTOCMD
13704 aucmd_prepbuf(&aco, buf);
13705#else
13706 save_curbuf = curbuf;
13707 curbuf = buf;
13708#endif
13709
13710 if (*varname == '&')
13711 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013712 long numval;
13713 char_u *strval;
13714 int error = FALSE;
13715
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013717 numval = get_tv_number_chk(varp, &error);
13718 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013719 if (!error && strval != NULL)
13720 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 }
13722 else
13723 {
13724 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13725 if (bufvarname != NULL)
13726 {
13727 STRCPY(bufvarname, "b:");
13728 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013729 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 vim_free(bufvarname);
13731 }
13732 }
13733
13734 /* reset notion of buffer */
13735#ifdef FEAT_AUTOCMD
13736 aucmd_restbuf(&aco);
13737#else
13738 curbuf = save_curbuf;
13739#endif
13740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741}
13742
13743/*
13744 * "setcmdpos()" function
13745 */
13746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013747f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013748 typval_T *argvars;
13749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013751 int pos = (int)get_tv_number(&argvars[0]) - 1;
13752
13753 if (pos >= 0)
13754 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755}
13756
13757/*
13758 * "setline()" function
13759 */
13760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013761f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013762 typval_T *argvars;
13763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764{
13765 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013766 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013767 list_T *l = NULL;
13768 listitem_T *li = NULL;
13769 long added = 0;
13770 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013772 lnum = get_tv_lnum(&argvars[0]);
13773 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013775 l = argvars[1].vval.v_list;
13776 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013778 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013779 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013780
13781 rettv->vval.v_number = 0; /* OK */
13782 for (;;)
13783 {
13784 if (l != NULL)
13785 {
13786 /* list argument, get next string */
13787 if (li == NULL)
13788 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013789 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013790 li = li->li_next;
13791 }
13792
13793 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013794 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013795 break;
13796 if (lnum <= curbuf->b_ml.ml_line_count)
13797 {
13798 /* existing line, replace it */
13799 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13800 {
13801 changed_bytes(lnum, 0);
13802 check_cursor_col();
13803 rettv->vval.v_number = 0; /* OK */
13804 }
13805 }
13806 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13807 {
13808 /* lnum is one past the last line, append the line */
13809 ++added;
13810 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13811 rettv->vval.v_number = 0; /* OK */
13812 }
13813
13814 if (l == NULL) /* only one string argument */
13815 break;
13816 ++lnum;
13817 }
13818
13819 if (added > 0)
13820 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821}
13822
13823/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013824 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013825 */
13826/*ARGSUSED*/
13827 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013828set_qf_ll_list(wp, list_arg, action_arg, rettv)
13829 win_T *wp;
13830 typval_T *list_arg;
13831 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013832 typval_T *rettv;
13833{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013834#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013835 char_u *act;
13836 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013837#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013838
Bram Moolenaar2641f772005-03-25 21:58:17 +000013839 rettv->vval.v_number = -1;
13840
13841#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013842 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013843 EMSG(_(e_listreq));
13844 else
13845 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013846 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013847
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013848 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013849 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013850 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013851 if (act == NULL)
13852 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013853 if (*act == 'a' || *act == 'r')
13854 action = *act;
13855 }
13856
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013857 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013858 rettv->vval.v_number = 0;
13859 }
13860#endif
13861}
13862
13863/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013864 * "setloclist()" function
13865 */
13866/*ARGSUSED*/
13867 static void
13868f_setloclist(argvars, rettv)
13869 typval_T *argvars;
13870 typval_T *rettv;
13871{
13872 win_T *win;
13873
13874 rettv->vval.v_number = -1;
13875
13876 win = find_win_by_nr(&argvars[0]);
13877 if (win != NULL)
13878 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13879}
13880
13881/*
13882 * "setqflist()" function
13883 */
13884/*ARGSUSED*/
13885 static void
13886f_setqflist(argvars, rettv)
13887 typval_T *argvars;
13888 typval_T *rettv;
13889{
13890 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
13891}
13892
13893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894 * "setreg()" function
13895 */
13896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013897f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013898 typval_T *argvars;
13899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900{
13901 int regname;
13902 char_u *strregname;
13903 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013904 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 int append;
13906 char_u yank_type;
13907 long block_len;
13908
13909 block_len = -1;
13910 yank_type = MAUTO;
13911 append = FALSE;
13912
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013913 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013914 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013916 if (strregname == NULL)
13917 return; /* type error; errmsg already given */
13918 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 if (regname == 0 || regname == '@')
13920 regname = '"';
13921 else if (regname == '=')
13922 return;
13923
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013924 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013926 stropt = get_tv_string_chk(&argvars[2]);
13927 if (stropt == NULL)
13928 return; /* type error */
13929 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 switch (*stropt)
13931 {
13932 case 'a': case 'A': /* append */
13933 append = TRUE;
13934 break;
13935 case 'v': case 'c': /* character-wise selection */
13936 yank_type = MCHAR;
13937 break;
13938 case 'V': case 'l': /* line-wise selection */
13939 yank_type = MLINE;
13940 break;
13941#ifdef FEAT_VISUAL
13942 case 'b': case Ctrl_V: /* block-wise selection */
13943 yank_type = MBLOCK;
13944 if (VIM_ISDIGIT(stropt[1]))
13945 {
13946 ++stropt;
13947 block_len = getdigits(&stropt) - 1;
13948 --stropt;
13949 }
13950 break;
13951#endif
13952 }
13953 }
13954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013955 strval = get_tv_string_chk(&argvars[1]);
13956 if (strval != NULL)
13957 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013959 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960}
13961
13962
13963/*
13964 * "setwinvar(expr)" function
13965 */
13966/*ARGSUSED*/
13967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013968f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013969 typval_T *argvars;
13970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971{
13972 win_T *win;
13973#ifdef FEAT_WINDOWS
13974 win_T *save_curwin;
13975#endif
13976 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013977 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 char_u nbuf[NUMBUFLEN];
13979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013980 rettv->vval.v_number = 0;
13981
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 if (check_restricted() || check_secure())
13983 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986 varp = &argvars[2];
13987
13988 if (win != NULL && varname != NULL && varp != NULL)
13989 {
13990#ifdef FEAT_WINDOWS
13991 /* set curwin to be our win, temporarily */
13992 save_curwin = curwin;
13993 curwin = win;
13994 curbuf = curwin->w_buffer;
13995#endif
13996
13997 if (*varname == '&')
13998 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013999 long numval;
14000 char_u *strval;
14001 int error = FALSE;
14002
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014004 numval = get_tv_number_chk(varp, &error);
14005 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014006 if (!error && strval != NULL)
14007 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008 }
14009 else
14010 {
14011 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14012 if (winvarname != NULL)
14013 {
14014 STRCPY(winvarname, "w:");
14015 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014016 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017 vim_free(winvarname);
14018 }
14019 }
14020
14021#ifdef FEAT_WINDOWS
14022 /* Restore current window, if it's still valid (autocomands can make
14023 * it invalid). */
14024 if (win_valid(save_curwin))
14025 {
14026 curwin = save_curwin;
14027 curbuf = curwin->w_buffer;
14028 }
14029#endif
14030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031}
14032
14033/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014034 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 */
14036 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014037f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014038 typval_T *argvars;
14039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014041 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042
Bram Moolenaar0d660222005-01-07 21:51:51 +000014043 p = get_tv_string(&argvars[0]);
14044 rettv->vval.v_string = vim_strsave(p);
14045 simplify_filename(rettv->vval.v_string); /* simplify in place */
14046 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047}
14048
Bram Moolenaar0d660222005-01-07 21:51:51 +000014049static int
14050#ifdef __BORLANDC__
14051 _RTLENTRYF
14052#endif
14053 item_compare __ARGS((const void *s1, const void *s2));
14054static int
14055#ifdef __BORLANDC__
14056 _RTLENTRYF
14057#endif
14058 item_compare2 __ARGS((const void *s1, const void *s2));
14059
14060static int item_compare_ic;
14061static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014062static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014063#define ITEM_COMPARE_FAIL 999
14064
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014066 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014068 static int
14069#ifdef __BORLANDC__
14070_RTLENTRYF
14071#endif
14072item_compare(s1, s2)
14073 const void *s1;
14074 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014076 char_u *p1, *p2;
14077 char_u *tofree1, *tofree2;
14078 int res;
14079 char_u numbuf1[NUMBUFLEN];
14080 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014082 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14083 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014084 if (item_compare_ic)
14085 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014087 res = STRCMP(p1, p2);
14088 vim_free(tofree1);
14089 vim_free(tofree2);
14090 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091}
14092
14093 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014094#ifdef __BORLANDC__
14095_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014097item_compare2(s1, s2)
14098 const void *s1;
14099 const void *s2;
14100{
14101 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014102 typval_T rettv;
14103 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014104 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014106 /* shortcut after failure in previous call; compare all items equal */
14107 if (item_compare_func_err)
14108 return 0;
14109
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014110 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14111 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014112 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14113 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014114
14115 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14116 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014117 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014118 clear_tv(&argv[0]);
14119 clear_tv(&argv[1]);
14120
14121 if (res == FAIL)
14122 res = ITEM_COMPARE_FAIL;
14123 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014124 /* return value has wrong type */
14125 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14126 if (item_compare_func_err)
14127 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014128 clear_tv(&rettv);
14129 return res;
14130}
14131
14132/*
14133 * "sort({list})" function
14134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014136f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014137 typval_T *argvars;
14138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139{
Bram Moolenaar33570922005-01-25 22:26:29 +000014140 list_T *l;
14141 listitem_T *li;
14142 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014143 long len;
14144 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145
Bram Moolenaar0d660222005-01-07 21:51:51 +000014146 rettv->vval.v_number = 0;
14147 if (argvars[0].v_type != VAR_LIST)
14148 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 else
14150 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014151 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014152 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014153 return;
14154 rettv->vval.v_list = l;
14155 rettv->v_type = VAR_LIST;
14156 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157
Bram Moolenaar0d660222005-01-07 21:51:51 +000014158 len = list_len(l);
14159 if (len <= 1)
14160 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161
Bram Moolenaar0d660222005-01-07 21:51:51 +000014162 item_compare_ic = FALSE;
14163 item_compare_func = NULL;
14164 if (argvars[1].v_type != VAR_UNKNOWN)
14165 {
14166 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014167 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014168 else
14169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014170 int error = FALSE;
14171
14172 i = get_tv_number_chk(&argvars[1], &error);
14173 if (error)
14174 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014175 if (i == 1)
14176 item_compare_ic = TRUE;
14177 else
14178 item_compare_func = get_tv_string(&argvars[1]);
14179 }
14180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181
Bram Moolenaar0d660222005-01-07 21:51:51 +000014182 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014183 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014184 if (ptrs == NULL)
14185 return;
14186 i = 0;
14187 for (li = l->lv_first; li != NULL; li = li->li_next)
14188 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014190 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014191 /* test the compare function */
14192 if (item_compare_func != NULL
14193 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14194 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014195 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014197 {
14198 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014199 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014200 item_compare_func == NULL ? item_compare : item_compare2);
14201
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014202 if (!item_compare_func_err)
14203 {
14204 /* Clear the List and append the items in the sorted order. */
14205 l->lv_first = l->lv_last = NULL;
14206 l->lv_len = 0;
14207 for (i = 0; i < len; ++i)
14208 list_append(l, ptrs[i]);
14209 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014210 }
14211
14212 vim_free(ptrs);
14213 }
14214}
14215
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014216/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014217 * "soundfold({word})" function
14218 */
14219 static void
14220f_soundfold(argvars, rettv)
14221 typval_T *argvars;
14222 typval_T *rettv;
14223{
14224 char_u *s;
14225
14226 rettv->v_type = VAR_STRING;
14227 s = get_tv_string(&argvars[0]);
14228#ifdef FEAT_SYN_HL
14229 rettv->vval.v_string = eval_soundfold(s);
14230#else
14231 rettv->vval.v_string = vim_strsave(s);
14232#endif
14233}
14234
14235/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014236 * "spellbadword()" function
14237 */
14238/* ARGSUSED */
14239 static void
14240f_spellbadword(argvars, rettv)
14241 typval_T *argvars;
14242 typval_T *rettv;
14243{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014244 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014245 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014246 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014247
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014248 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014249 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014250
14251#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014252 if (argvars[0].v_type == VAR_UNKNOWN)
14253 {
14254 /* Find the start and length of the badly spelled word. */
14255 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14256 if (len != 0)
14257 word = ml_get_cursor();
14258 }
14259 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14260 {
14261 char_u *str = get_tv_string_chk(&argvars[0]);
14262 int capcol = -1;
14263
14264 if (str != NULL)
14265 {
14266 /* Check the argument for spelling. */
14267 while (*str != NUL)
14268 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014269 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014270 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014271 {
14272 word = str;
14273 break;
14274 }
14275 str += len;
14276 }
14277 }
14278 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014279#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014280
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014281 list_append_string(rettv->vval.v_list, word, len);
14282 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014283 attr == HLF_SPB ? "bad" :
14284 attr == HLF_SPR ? "rare" :
14285 attr == HLF_SPL ? "local" :
14286 attr == HLF_SPC ? "caps" :
14287 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014288}
14289
14290/*
14291 * "spellsuggest()" function
14292 */
14293 static void
14294f_spellsuggest(argvars, rettv)
14295 typval_T *argvars;
14296 typval_T *rettv;
14297{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014298#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014299 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014300 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014301 int maxcount;
14302 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014303 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014304 listitem_T *li;
14305 int need_capital = FALSE;
14306#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014307
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014308 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014309 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014310
14311#ifdef FEAT_SYN_HL
14312 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14313 {
14314 str = get_tv_string(&argvars[0]);
14315 if (argvars[1].v_type != VAR_UNKNOWN)
14316 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014317 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014318 if (maxcount <= 0)
14319 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014320 if (argvars[2].v_type != VAR_UNKNOWN)
14321 {
14322 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14323 if (typeerr)
14324 return;
14325 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014326 }
14327 else
14328 maxcount = 25;
14329
Bram Moolenaar4770d092006-01-12 23:22:24 +000014330 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014331
14332 for (i = 0; i < ga.ga_len; ++i)
14333 {
14334 str = ((char_u **)ga.ga_data)[i];
14335
14336 li = listitem_alloc();
14337 if (li == NULL)
14338 vim_free(str);
14339 else
14340 {
14341 li->li_tv.v_type = VAR_STRING;
14342 li->li_tv.v_lock = 0;
14343 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014344 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014345 }
14346 }
14347 ga_clear(&ga);
14348 }
14349#endif
14350}
14351
Bram Moolenaar0d660222005-01-07 21:51:51 +000014352 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014353f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014354 typval_T *argvars;
14355 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014356{
14357 char_u *str;
14358 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014359 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014360 regmatch_T regmatch;
14361 char_u patbuf[NUMBUFLEN];
14362 char_u *save_cpo;
14363 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014364 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014365 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014367
14368 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14369 save_cpo = p_cpo;
14370 p_cpo = (char_u *)"";
14371
14372 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014373 if (argvars[1].v_type != VAR_UNKNOWN)
14374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014375 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14376 if (pat == NULL)
14377 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014378 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014379 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014380 }
14381 if (pat == NULL || *pat == NUL)
14382 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014383
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014384 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014386 if (typeerr)
14387 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388
Bram Moolenaar0d660222005-01-07 21:51:51 +000014389 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14390 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014392 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014393 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014394 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014395 if (*str == NUL)
14396 match = FALSE; /* empty item at the end */
14397 else
14398 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014399 if (match)
14400 end = regmatch.startp[0];
14401 else
14402 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014403 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14404 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014405 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014406 if (list_append_string(rettv->vval.v_list, str,
14407 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014408 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014409 }
14410 if (!match)
14411 break;
14412 /* Advance to just after the match. */
14413 if (regmatch.endp[0] > str)
14414 col = 0;
14415 else
14416 {
14417 /* Don't get stuck at the same match. */
14418#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014419 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014420#else
14421 col = 1;
14422#endif
14423 }
14424 str = regmatch.endp[0];
14425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426
Bram Moolenaar0d660222005-01-07 21:51:51 +000014427 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429
Bram Moolenaar0d660222005-01-07 21:51:51 +000014430 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431}
14432
14433#ifdef HAVE_STRFTIME
14434/*
14435 * "strftime({format}[, {time}])" function
14436 */
14437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014438f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014439 typval_T *argvars;
14440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441{
14442 char_u result_buf[256];
14443 struct tm *curtime;
14444 time_t seconds;
14445 char_u *p;
14446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014447 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014449 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014450 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 seconds = time(NULL);
14452 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014453 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 curtime = localtime(&seconds);
14455 /* MSVC returns NULL for an invalid value of seconds. */
14456 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014457 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458 else
14459 {
14460# ifdef FEAT_MBYTE
14461 vimconv_T conv;
14462 char_u *enc;
14463
14464 conv.vc_type = CONV_NONE;
14465 enc = enc_locale();
14466 convert_setup(&conv, p_enc, enc);
14467 if (conv.vc_type != CONV_NONE)
14468 p = string_convert(&conv, p, NULL);
14469# endif
14470 if (p != NULL)
14471 (void)strftime((char *)result_buf, sizeof(result_buf),
14472 (char *)p, curtime);
14473 else
14474 result_buf[0] = NUL;
14475
14476# ifdef FEAT_MBYTE
14477 if (conv.vc_type != CONV_NONE)
14478 vim_free(p);
14479 convert_setup(&conv, enc, p_enc);
14480 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014481 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 else
14483# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014484 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485
14486# ifdef FEAT_MBYTE
14487 /* Release conversion descriptors */
14488 convert_setup(&conv, NULL, NULL);
14489 vim_free(enc);
14490# endif
14491 }
14492}
14493#endif
14494
14495/*
14496 * "stridx()" function
14497 */
14498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014499f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014500 typval_T *argvars;
14501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502{
14503 char_u buf[NUMBUFLEN];
14504 char_u *needle;
14505 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014506 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014508 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014510 needle = get_tv_string_chk(&argvars[1]);
14511 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014512 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014513 if (needle == NULL || haystack == NULL)
14514 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515
Bram Moolenaar33570922005-01-25 22:26:29 +000014516 if (argvars[2].v_type != VAR_UNKNOWN)
14517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014518 int error = FALSE;
14519
14520 start_idx = get_tv_number_chk(&argvars[2], &error);
14521 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014522 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014523 if (start_idx >= 0)
14524 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014525 }
14526
14527 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14528 if (pos != NULL)
14529 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530}
14531
14532/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014533 * "string()" function
14534 */
14535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014536f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014537 typval_T *argvars;
14538 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014539{
14540 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014541 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014542
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014543 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014544 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014545 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014546 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547}
14548
14549/*
14550 * "strlen()" function
14551 */
14552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014553f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014554 typval_T *argvars;
14555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014557 rettv->vval.v_number = (varnumber_T)(STRLEN(
14558 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559}
14560
14561/*
14562 * "strpart()" function
14563 */
14564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014565f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014566 typval_T *argvars;
14567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568{
14569 char_u *p;
14570 int n;
14571 int len;
14572 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014573 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014575 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576 slen = (int)STRLEN(p);
14577
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014578 n = get_tv_number_chk(&argvars[1], &error);
14579 if (error)
14580 len = 0;
14581 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014582 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583 else
14584 len = slen - n; /* default len: all bytes that are available. */
14585
14586 /*
14587 * Only return the overlap between the specified part and the actual
14588 * string.
14589 */
14590 if (n < 0)
14591 {
14592 len += n;
14593 n = 0;
14594 }
14595 else if (n > slen)
14596 n = slen;
14597 if (len < 0)
14598 len = 0;
14599 else if (n + len > slen)
14600 len = slen - n;
14601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014602 rettv->v_type = VAR_STRING;
14603 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604}
14605
14606/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014607 * "strridx()" function
14608 */
14609 static void
14610f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014611 typval_T *argvars;
14612 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014613{
14614 char_u buf[NUMBUFLEN];
14615 char_u *needle;
14616 char_u *haystack;
14617 char_u *rest;
14618 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014619 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014620
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 needle = get_tv_string_chk(&argvars[1]);
14622 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014623 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014624
14625 rettv->vval.v_number = -1;
14626 if (needle == NULL || haystack == NULL)
14627 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014628 if (argvars[2].v_type != VAR_UNKNOWN)
14629 {
14630 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014631 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014632 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014633 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014634 }
14635 else
14636 end_idx = haystack_len;
14637
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014639 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014640 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014641 lastmatch = haystack + end_idx;
14642 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014643 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014644 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014645 for (rest = haystack; *rest != '\0'; ++rest)
14646 {
14647 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014648 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649 break;
14650 lastmatch = rest;
14651 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014652 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014653
14654 if (lastmatch == NULL)
14655 rettv->vval.v_number = -1;
14656 else
14657 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14658}
14659
14660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661 * "strtrans()" function
14662 */
14663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014664f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014665 typval_T *argvars;
14666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014668 rettv->v_type = VAR_STRING;
14669 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670}
14671
14672/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014673 * "submatch()" function
14674 */
14675 static void
14676f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014677 typval_T *argvars;
14678 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014679{
14680 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014681 rettv->vval.v_string =
14682 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014683}
14684
14685/*
14686 * "substitute()" function
14687 */
14688 static void
14689f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014690 typval_T *argvars;
14691 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014692{
14693 char_u patbuf[NUMBUFLEN];
14694 char_u subbuf[NUMBUFLEN];
14695 char_u flagsbuf[NUMBUFLEN];
14696
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014697 char_u *str = get_tv_string_chk(&argvars[0]);
14698 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14699 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14700 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14701
Bram Moolenaar0d660222005-01-07 21:51:51 +000014702 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014703 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14704 rettv->vval.v_string = NULL;
14705 else
14706 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014707}
14708
14709/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014710 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711 */
14712/*ARGSUSED*/
14713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014714f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014715 typval_T *argvars;
14716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717{
14718 int id = 0;
14719#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014720 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721 long col;
14722 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014723 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014725 lnum = get_tv_lnum(argvars); /* -1 on type error */
14726 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14727 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014729 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014730 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014731 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732#endif
14733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014734 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735}
14736
14737/*
14738 * "synIDattr(id, what [, mode])" function
14739 */
14740/*ARGSUSED*/
14741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014742f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014743 typval_T *argvars;
14744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745{
14746 char_u *p = NULL;
14747#ifdef FEAT_SYN_HL
14748 int id;
14749 char_u *what;
14750 char_u *mode;
14751 char_u modebuf[NUMBUFLEN];
14752 int modec;
14753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754 id = get_tv_number(&argvars[0]);
14755 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014756 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014758 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759 modec = TOLOWER_ASC(mode[0]);
14760 if (modec != 't' && modec != 'c'
14761#ifdef FEAT_GUI
14762 && modec != 'g'
14763#endif
14764 )
14765 modec = 0; /* replace invalid with current */
14766 }
14767 else
14768 {
14769#ifdef FEAT_GUI
14770 if (gui.in_use)
14771 modec = 'g';
14772 else
14773#endif
14774 if (t_colors > 1)
14775 modec = 'c';
14776 else
14777 modec = 't';
14778 }
14779
14780
14781 switch (TOLOWER_ASC(what[0]))
14782 {
14783 case 'b':
14784 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14785 p = highlight_color(id, what, modec);
14786 else /* bold */
14787 p = highlight_has_attr(id, HL_BOLD, modec);
14788 break;
14789
14790 case 'f': /* fg[#] */
14791 p = highlight_color(id, what, modec);
14792 break;
14793
14794 case 'i':
14795 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14796 p = highlight_has_attr(id, HL_INVERSE, modec);
14797 else /* italic */
14798 p = highlight_has_attr(id, HL_ITALIC, modec);
14799 break;
14800
14801 case 'n': /* name */
14802 p = get_highlight_name(NULL, id - 1);
14803 break;
14804
14805 case 'r': /* reverse */
14806 p = highlight_has_attr(id, HL_INVERSE, modec);
14807 break;
14808
14809 case 's': /* standout */
14810 p = highlight_has_attr(id, HL_STANDOUT, modec);
14811 break;
14812
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014813 case 'u':
14814 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14815 /* underline */
14816 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14817 else
14818 /* undercurl */
14819 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 break;
14821 }
14822
14823 if (p != NULL)
14824 p = vim_strsave(p);
14825#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014826 rettv->v_type = VAR_STRING;
14827 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828}
14829
14830/*
14831 * "synIDtrans(id)" function
14832 */
14833/*ARGSUSED*/
14834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014835f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014836 typval_T *argvars;
14837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838{
14839 int id;
14840
14841#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014842 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843
14844 if (id > 0)
14845 id = syn_get_final_id(id);
14846 else
14847#endif
14848 id = 0;
14849
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014850 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851}
14852
14853/*
14854 * "system()" function
14855 */
14856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014857f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014858 typval_T *argvars;
14859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014861 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014863 char_u *infile = NULL;
14864 char_u buf[NUMBUFLEN];
14865 int err = FALSE;
14866 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014868 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014869 {
14870 /*
14871 * Write the string to a temp file, to be used for input of the shell
14872 * command.
14873 */
14874 if ((infile = vim_tempname('i')) == NULL)
14875 {
14876 EMSG(_(e_notmp));
14877 return;
14878 }
14879
14880 fd = mch_fopen((char *)infile, WRITEBIN);
14881 if (fd == NULL)
14882 {
14883 EMSG2(_(e_notopen), infile);
14884 goto done;
14885 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014886 p = get_tv_string_buf_chk(&argvars[1], buf);
14887 if (p == NULL)
14888 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014889 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14890 err = TRUE;
14891 if (fclose(fd) != 0)
14892 err = TRUE;
14893 if (err)
14894 {
14895 EMSG(_("E677: Error writing temp file"));
14896 goto done;
14897 }
14898 }
14899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014900 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014901
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902#ifdef USE_CR
14903 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014904 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905 {
14906 char_u *s;
14907
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014908 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909 {
14910 if (*s == CAR)
14911 *s = NL;
14912 }
14913 }
14914#else
14915# ifdef USE_CRNL
14916 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014917 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918 {
14919 char_u *s, *d;
14920
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014921 d = res;
14922 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014923 {
14924 if (s[0] == CAR && s[1] == NL)
14925 ++s;
14926 *d++ = *s;
14927 }
14928 *d = NUL;
14929 }
14930# endif
14931#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014932
14933done:
14934 if (infile != NULL)
14935 {
14936 mch_remove(infile);
14937 vim_free(infile);
14938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014939 rettv->v_type = VAR_STRING;
14940 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941}
14942
14943/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014944 * "tabpagebuflist()" function
14945 */
14946/* ARGSUSED */
14947 static void
14948f_tabpagebuflist(argvars, rettv)
14949 typval_T *argvars;
14950 typval_T *rettv;
14951{
14952#ifndef FEAT_WINDOWS
14953 rettv->vval.v_number = 0;
14954#else
14955 tabpage_T *tp;
14956 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014957
14958 if (argvars[0].v_type == VAR_UNKNOWN)
14959 wp = firstwin;
14960 else
14961 {
14962 tp = find_tabpage((int)get_tv_number(&argvars[0]));
14963 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000014964 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014965 }
14966 if (wp == NULL)
14967 rettv->vval.v_number = 0;
14968 else
14969 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014970 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014971 rettv->vval.v_number = 0;
14972 else
14973 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014974 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014975 if (list_append_number(rettv->vval.v_list,
14976 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014977 break;
14978 }
14979 }
14980#endif
14981}
14982
14983
14984/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000014985 * "tabpagenr()" function
14986 */
14987/* ARGSUSED */
14988 static void
14989f_tabpagenr(argvars, rettv)
14990 typval_T *argvars;
14991 typval_T *rettv;
14992{
14993 int nr = 1;
14994#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000014995 char_u *arg;
14996
14997 if (argvars[0].v_type != VAR_UNKNOWN)
14998 {
14999 arg = get_tv_string_chk(&argvars[0]);
15000 nr = 0;
15001 if (arg != NULL)
15002 {
15003 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015004 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015005 else
15006 EMSG2(_(e_invexpr2), arg);
15007 }
15008 }
15009 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015010 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015011#endif
15012 rettv->vval.v_number = nr;
15013}
15014
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015015
15016#ifdef FEAT_WINDOWS
15017static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15018
15019/*
15020 * Common code for tabpagewinnr() and winnr().
15021 */
15022 static int
15023get_winnr(tp, argvar)
15024 tabpage_T *tp;
15025 typval_T *argvar;
15026{
15027 win_T *twin;
15028 int nr = 1;
15029 win_T *wp;
15030 char_u *arg;
15031
15032 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15033 if (argvar->v_type != VAR_UNKNOWN)
15034 {
15035 arg = get_tv_string_chk(argvar);
15036 if (arg == NULL)
15037 nr = 0; /* type error; errmsg already given */
15038 else if (STRCMP(arg, "$") == 0)
15039 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15040 else if (STRCMP(arg, "#") == 0)
15041 {
15042 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15043 if (twin == NULL)
15044 nr = 0;
15045 }
15046 else
15047 {
15048 EMSG2(_(e_invexpr2), arg);
15049 nr = 0;
15050 }
15051 }
15052
15053 if (nr > 0)
15054 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15055 wp != twin; wp = wp->w_next)
15056 ++nr;
15057 return nr;
15058}
15059#endif
15060
15061/*
15062 * "tabpagewinnr()" function
15063 */
15064/* ARGSUSED */
15065 static void
15066f_tabpagewinnr(argvars, rettv)
15067 typval_T *argvars;
15068 typval_T *rettv;
15069{
15070 int nr = 1;
15071#ifdef FEAT_WINDOWS
15072 tabpage_T *tp;
15073
15074 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15075 if (tp == NULL)
15076 nr = 0;
15077 else
15078 nr = get_winnr(tp, &argvars[1]);
15079#endif
15080 rettv->vval.v_number = nr;
15081}
15082
15083
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015084/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015085 * "tagfiles()" function
15086 */
15087/*ARGSUSED*/
15088 static void
15089f_tagfiles(argvars, rettv)
15090 typval_T *argvars;
15091 typval_T *rettv;
15092{
15093 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015094 tagname_T tn;
15095 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015096
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015097 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015098 {
15099 rettv->vval.v_number = 0;
15100 return;
15101 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015102
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015103 for (first = TRUE; ; first = FALSE)
15104 if (get_tagfname(&tn, first, fname) == FAIL
15105 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015106 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015107 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015108}
15109
15110/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015111 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015112 */
15113 static void
15114f_taglist(argvars, rettv)
15115 typval_T *argvars;
15116 typval_T *rettv;
15117{
15118 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015119
15120 tag_pattern = get_tv_string(&argvars[0]);
15121
15122 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015123 if (*tag_pattern == NUL)
15124 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015125
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015126 if (rettv_list_alloc(rettv) == OK)
15127 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015128}
15129
15130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 * "tempname()" function
15132 */
15133/*ARGSUSED*/
15134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015135f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015136 typval_T *argvars;
15137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138{
15139 static int x = 'A';
15140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015141 rettv->v_type = VAR_STRING;
15142 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143
15144 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15145 * names. Skip 'I' and 'O', they are used for shell redirection. */
15146 do
15147 {
15148 if (x == 'Z')
15149 x = '0';
15150 else if (x == '9')
15151 x = 'A';
15152 else
15153 {
15154#ifdef EBCDIC
15155 if (x == 'I')
15156 x = 'J';
15157 else if (x == 'R')
15158 x = 'S';
15159 else
15160#endif
15161 ++x;
15162 }
15163 } while (x == 'I' || x == 'O');
15164}
15165
15166/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015167 * "test(list)" function: Just checking the walls...
15168 */
15169/*ARGSUSED*/
15170 static void
15171f_test(argvars, rettv)
15172 typval_T *argvars;
15173 typval_T *rettv;
15174{
15175 /* Used for unit testing. Change the code below to your liking. */
15176#if 0
15177 listitem_T *li;
15178 list_T *l;
15179 char_u *bad, *good;
15180
15181 if (argvars[0].v_type != VAR_LIST)
15182 return;
15183 l = argvars[0].vval.v_list;
15184 if (l == NULL)
15185 return;
15186 li = l->lv_first;
15187 if (li == NULL)
15188 return;
15189 bad = get_tv_string(&li->li_tv);
15190 li = li->li_next;
15191 if (li == NULL)
15192 return;
15193 good = get_tv_string(&li->li_tv);
15194 rettv->vval.v_number = test_edit_score(bad, good);
15195#endif
15196}
15197
15198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199 * "tolower(string)" function
15200 */
15201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015202f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015203 typval_T *argvars;
15204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205{
15206 char_u *p;
15207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015208 p = vim_strsave(get_tv_string(&argvars[0]));
15209 rettv->v_type = VAR_STRING;
15210 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211
15212 if (p != NULL)
15213 while (*p != NUL)
15214 {
15215#ifdef FEAT_MBYTE
15216 int l;
15217
15218 if (enc_utf8)
15219 {
15220 int c, lc;
15221
15222 c = utf_ptr2char(p);
15223 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015224 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225 /* TODO: reallocate string when byte count changes. */
15226 if (utf_char2len(lc) == l)
15227 utf_char2bytes(lc, p);
15228 p += l;
15229 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015230 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015231 p += l; /* skip multi-byte character */
15232 else
15233#endif
15234 {
15235 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15236 ++p;
15237 }
15238 }
15239}
15240
15241/*
15242 * "toupper(string)" function
15243 */
15244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015245f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015246 typval_T *argvars;
15247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015248{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015249 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015250 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251}
15252
15253/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015254 * "tr(string, fromstr, tostr)" function
15255 */
15256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015257f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015258 typval_T *argvars;
15259 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015260{
15261 char_u *instr;
15262 char_u *fromstr;
15263 char_u *tostr;
15264 char_u *p;
15265#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015266 int inlen;
15267 int fromlen;
15268 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015269 int idx;
15270 char_u *cpstr;
15271 int cplen;
15272 int first = TRUE;
15273#endif
15274 char_u buf[NUMBUFLEN];
15275 char_u buf2[NUMBUFLEN];
15276 garray_T ga;
15277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015278 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015279 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15280 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015281
15282 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015283 rettv->v_type = VAR_STRING;
15284 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015285 if (fromstr == NULL || tostr == NULL)
15286 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015287 ga_init2(&ga, (int)sizeof(char), 80);
15288
15289#ifdef FEAT_MBYTE
15290 if (!has_mbyte)
15291#endif
15292 /* not multi-byte: fromstr and tostr must be the same length */
15293 if (STRLEN(fromstr) != STRLEN(tostr))
15294 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015295#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015296error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015297#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015298 EMSG2(_(e_invarg2), fromstr);
15299 ga_clear(&ga);
15300 return;
15301 }
15302
15303 /* fromstr and tostr have to contain the same number of chars */
15304 while (*instr != NUL)
15305 {
15306#ifdef FEAT_MBYTE
15307 if (has_mbyte)
15308 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015309 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015310 cpstr = instr;
15311 cplen = inlen;
15312 idx = 0;
15313 for (p = fromstr; *p != NUL; p += fromlen)
15314 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015315 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015316 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15317 {
15318 for (p = tostr; *p != NUL; p += tolen)
15319 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015320 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015321 if (idx-- == 0)
15322 {
15323 cplen = tolen;
15324 cpstr = p;
15325 break;
15326 }
15327 }
15328 if (*p == NUL) /* tostr is shorter than fromstr */
15329 goto error;
15330 break;
15331 }
15332 ++idx;
15333 }
15334
15335 if (first && cpstr == instr)
15336 {
15337 /* Check that fromstr and tostr have the same number of
15338 * (multi-byte) characters. Done only once when a character
15339 * of instr doesn't appear in fromstr. */
15340 first = FALSE;
15341 for (p = tostr; *p != NUL; p += tolen)
15342 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015343 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015344 --idx;
15345 }
15346 if (idx != 0)
15347 goto error;
15348 }
15349
15350 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015351 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015352 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015353
15354 instr += inlen;
15355 }
15356 else
15357#endif
15358 {
15359 /* When not using multi-byte chars we can do it faster. */
15360 p = vim_strchr(fromstr, *instr);
15361 if (p != NULL)
15362 ga_append(&ga, tostr[p - fromstr]);
15363 else
15364 ga_append(&ga, *instr);
15365 ++instr;
15366 }
15367 }
15368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015369 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015370}
15371
15372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 * "type(expr)" function
15374 */
15375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015376f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015377 typval_T *argvars;
15378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015380 int n;
15381
15382 switch (argvars[0].v_type)
15383 {
15384 case VAR_NUMBER: n = 0; break;
15385 case VAR_STRING: n = 1; break;
15386 case VAR_FUNC: n = 2; break;
15387 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015388 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015389 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15390 }
15391 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392}
15393
15394/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015395 * "values(dict)" function
15396 */
15397 static void
15398f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015399 typval_T *argvars;
15400 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015401{
15402 dict_list(argvars, rettv, 1);
15403}
15404
15405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406 * "virtcol(string)" function
15407 */
15408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015410 typval_T *argvars;
15411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412{
15413 colnr_T vcol = 0;
15414 pos_T *fp;
15415
15416 fp = var2fpos(&argvars[0], FALSE);
15417 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
15418 {
15419 getvvcol(curwin, fp, NULL, NULL, &vcol);
15420 ++vcol;
15421 }
15422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015423 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424}
15425
15426/*
15427 * "visualmode()" function
15428 */
15429/*ARGSUSED*/
15430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015431f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015432 typval_T *argvars;
15433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434{
15435#ifdef FEAT_VISUAL
15436 char_u str[2];
15437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015438 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 str[0] = curbuf->b_visual_mode_eval;
15440 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015441 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442
15443 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015444 if ((argvars[0].v_type == VAR_NUMBER
15445 && argvars[0].vval.v_number != 0)
15446 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 curbuf->b_visual_mode_eval = NUL;
15449#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015450 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451#endif
15452}
15453
15454/*
15455 * "winbufnr(nr)" function
15456 */
15457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015458f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015459 typval_T *argvars;
15460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461{
15462 win_T *wp;
15463
15464 wp = find_win_by_nr(&argvars[0]);
15465 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015466 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015468 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469}
15470
15471/*
15472 * "wincol()" function
15473 */
15474/*ARGSUSED*/
15475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015476f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015477 typval_T *argvars;
15478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479{
15480 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015481 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482}
15483
15484/*
15485 * "winheight(nr)" function
15486 */
15487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015488f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015489 typval_T *argvars;
15490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491{
15492 win_T *wp;
15493
15494 wp = find_win_by_nr(&argvars[0]);
15495 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015496 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015498 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499}
15500
15501/*
15502 * "winline()" function
15503 */
15504/*ARGSUSED*/
15505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015506f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015507 typval_T *argvars;
15508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509{
15510 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015511 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512}
15513
15514/*
15515 * "winnr()" function
15516 */
15517/* ARGSUSED */
15518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015519f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015520 typval_T *argvars;
15521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522{
15523 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015524
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015526 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015528 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529}
15530
15531/*
15532 * "winrestcmd()" function
15533 */
15534/* ARGSUSED */
15535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015536f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015537 typval_T *argvars;
15538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539{
15540#ifdef FEAT_WINDOWS
15541 win_T *wp;
15542 int winnr = 1;
15543 garray_T ga;
15544 char_u buf[50];
15545
15546 ga_init2(&ga, (int)sizeof(char), 70);
15547 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15548 {
15549 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15550 ga_concat(&ga, buf);
15551# ifdef FEAT_VERTSPLIT
15552 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15553 ga_concat(&ga, buf);
15554# endif
15555 ++winnr;
15556 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015557 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015559 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015561 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015563 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564}
15565
15566/*
15567 * "winwidth(nr)" function
15568 */
15569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015570f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015571 typval_T *argvars;
15572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573{
15574 win_T *wp;
15575
15576 wp = find_win_by_nr(&argvars[0]);
15577 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015578 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 else
15580#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015581 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015583 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584#endif
15585}
15586
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015588 * "writefile()" function
15589 */
15590 static void
15591f_writefile(argvars, rettv)
15592 typval_T *argvars;
15593 typval_T *rettv;
15594{
15595 int binary = FALSE;
15596 char_u *fname;
15597 FILE *fd;
15598 listitem_T *li;
15599 char_u *s;
15600 int ret = 0;
15601 int c;
15602
15603 if (argvars[0].v_type != VAR_LIST)
15604 {
15605 EMSG2(_(e_listarg), "writefile()");
15606 return;
15607 }
15608 if (argvars[0].vval.v_list == NULL)
15609 return;
15610
15611 if (argvars[2].v_type != VAR_UNKNOWN
15612 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15613 binary = TRUE;
15614
15615 /* Always open the file in binary mode, library functions have a mind of
15616 * their own about CR-LF conversion. */
15617 fname = get_tv_string(&argvars[1]);
15618 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15619 {
15620 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15621 ret = -1;
15622 }
15623 else
15624 {
15625 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15626 li = li->li_next)
15627 {
15628 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15629 {
15630 if (*s == '\n')
15631 c = putc(NUL, fd);
15632 else
15633 c = putc(*s, fd);
15634 if (c == EOF)
15635 {
15636 ret = -1;
15637 break;
15638 }
15639 }
15640 if (!binary || li->li_next != NULL)
15641 if (putc('\n', fd) == EOF)
15642 {
15643 ret = -1;
15644 break;
15645 }
15646 if (ret < 0)
15647 {
15648 EMSG(_(e_write));
15649 break;
15650 }
15651 }
15652 fclose(fd);
15653 }
15654
15655 rettv->vval.v_number = ret;
15656}
15657
15658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015660 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 */
15662 static pos_T *
15663var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015664 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 int lnum; /* TRUE when $ is last line */
15666{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015667 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015669 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670
Bram Moolenaara5525202006-03-02 22:52:09 +000015671 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015672 if (varp->v_type == VAR_LIST)
15673 {
15674 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015675 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000015676 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015677
15678 l = varp->vval.v_list;
15679 if (l == NULL)
15680 return NULL;
15681
15682 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015683 pos.lnum = list_find_nr(l, 0L, &error);
15684 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015685 return NULL; /* invalid line number */
15686
15687 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015688 pos.col = list_find_nr(l, 1L, &error);
15689 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015690 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015691 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000015692 /* Accept a position up to the NUL after the line. */
15693 if (pos.col <= 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015694 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015695 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015696
Bram Moolenaara5525202006-03-02 22:52:09 +000015697#ifdef FEAT_VIRTUALEDIT
15698 /* Get the virtual offset. Defaults to zero. */
15699 pos.coladd = list_find_nr(l, 2L, &error);
15700 if (error)
15701 pos.coladd = 0;
15702#endif
15703
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015704 return &pos;
15705 }
15706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015707 name = get_tv_string_chk(varp);
15708 if (name == NULL)
15709 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 if (name[0] == '.') /* cursor */
15711 return &curwin->w_cursor;
15712 if (name[0] == '\'') /* mark */
15713 {
15714 pp = getmark(name[1], FALSE);
15715 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15716 return NULL;
15717 return pp;
15718 }
Bram Moolenaara5525202006-03-02 22:52:09 +000015719
15720#ifdef FEAT_VIRTUALEDIT
15721 pos.coladd = 0;
15722#endif
15723
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015724 if (name[0] == 'w' && lnum)
15725 {
15726 pos.col = 0;
15727 if (name[1] == '0') /* "w0": first visible line */
15728 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015729 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015730 pos.lnum = curwin->w_topline;
15731 return &pos;
15732 }
15733 else if (name[1] == '$') /* "w$": last visible line */
15734 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015735 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015736 pos.lnum = curwin->w_botline - 1;
15737 return &pos;
15738 }
15739 }
15740 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 {
15742 if (lnum)
15743 {
15744 pos.lnum = curbuf->b_ml.ml_line_count;
15745 pos.col = 0;
15746 }
15747 else
15748 {
15749 pos.lnum = curwin->w_cursor.lnum;
15750 pos.col = (colnr_T)STRLEN(ml_get_curline());
15751 }
15752 return &pos;
15753 }
15754 return NULL;
15755}
15756
15757/*
15758 * Get the length of an environment variable name.
15759 * Advance "arg" to the first character after the name.
15760 * Return 0 for error.
15761 */
15762 static int
15763get_env_len(arg)
15764 char_u **arg;
15765{
15766 char_u *p;
15767 int len;
15768
15769 for (p = *arg; vim_isIDc(*p); ++p)
15770 ;
15771 if (p == *arg) /* no name found */
15772 return 0;
15773
15774 len = (int)(p - *arg);
15775 *arg = p;
15776 return len;
15777}
15778
15779/*
15780 * Get the length of the name of a function or internal variable.
15781 * "arg" is advanced to the first non-white character after the name.
15782 * Return 0 if something is wrong.
15783 */
15784 static int
15785get_id_len(arg)
15786 char_u **arg;
15787{
15788 char_u *p;
15789 int len;
15790
15791 /* Find the end of the name. */
15792 for (p = *arg; eval_isnamec(*p); ++p)
15793 ;
15794 if (p == *arg) /* no name found */
15795 return 0;
15796
15797 len = (int)(p - *arg);
15798 *arg = skipwhite(p);
15799
15800 return len;
15801}
15802
15803/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015804 * Get the length of the name of a variable or function.
15805 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015807 * Return -1 if curly braces expansion failed.
15808 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 * If the name contains 'magic' {}'s, expand them and return the
15810 * expanded name in an allocated string via 'alias' - caller must free.
15811 */
15812 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015813get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814 char_u **arg;
15815 char_u **alias;
15816 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015817 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818{
15819 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820 char_u *p;
15821 char_u *expr_start;
15822 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823
15824 *alias = NULL; /* default to no alias */
15825
15826 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15827 && (*arg)[2] == (int)KE_SNR)
15828 {
15829 /* hard coded <SNR>, already translated */
15830 *arg += 3;
15831 return get_id_len(arg) + 3;
15832 }
15833 len = eval_fname_script(*arg);
15834 if (len > 0)
15835 {
15836 /* literal "<SID>", "s:" or "<SNR>" */
15837 *arg += len;
15838 }
15839
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015841 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015843 p = find_name_end(*arg, &expr_start, &expr_end,
15844 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845 if (expr_start != NULL)
15846 {
15847 char_u *temp_string;
15848
15849 if (!evaluate)
15850 {
15851 len += (int)(p - *arg);
15852 *arg = skipwhite(p);
15853 return len;
15854 }
15855
15856 /*
15857 * Include any <SID> etc in the expanded string:
15858 * Thus the -len here.
15859 */
15860 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15861 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015862 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863 *alias = temp_string;
15864 *arg = skipwhite(p);
15865 return (int)STRLEN(temp_string);
15866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867
15868 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015869 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870 EMSG2(_(e_invexpr2), *arg);
15871
15872 return len;
15873}
15874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015875/*
15876 * Find the end of a variable or function name, taking care of magic braces.
15877 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15878 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015879 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015880 * Return a pointer to just after the name. Equal to "arg" if there is no
15881 * valid name.
15882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015883 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015884find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885 char_u *arg;
15886 char_u **expr_start;
15887 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015888 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015890 int mb_nest = 0;
15891 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 char_u *p;
15893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015894 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015896 *expr_start = NULL;
15897 *expr_end = NULL;
15898 }
15899
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015900 /* Quick check for valid starting character. */
15901 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15902 return arg;
15903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015904 for (p = arg; *p != NUL
15905 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015906 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015907 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015908 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015909 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015910 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015911 if (*p == '\'')
15912 {
15913 /* skip over 'string' to avoid counting [ and ] inside it. */
15914 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15915 ;
15916 if (*p == NUL)
15917 break;
15918 }
15919 else if (*p == '"')
15920 {
15921 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15922 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15923 if (*p == '\\' && p[1] != NUL)
15924 ++p;
15925 if (*p == NUL)
15926 break;
15927 }
15928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015929 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015931 if (*p == '[')
15932 ++br_nest;
15933 else if (*p == ']')
15934 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015937 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015939 if (*p == '{')
15940 {
15941 mb_nest++;
15942 if (expr_start != NULL && *expr_start == NULL)
15943 *expr_start = p;
15944 }
15945 else if (*p == '}')
15946 {
15947 mb_nest--;
15948 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15949 *expr_end = p;
15950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952 }
15953
15954 return p;
15955}
15956
15957/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015958 * Expands out the 'magic' {}'s in a variable/function name.
15959 * Note that this can call itself recursively, to deal with
15960 * constructs like foo{bar}{baz}{bam}
15961 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15962 * "in_start" ^
15963 * "expr_start" ^
15964 * "expr_end" ^
15965 * "in_end" ^
15966 *
15967 * Returns a new allocated string, which the caller must free.
15968 * Returns NULL for failure.
15969 */
15970 static char_u *
15971make_expanded_name(in_start, expr_start, expr_end, in_end)
15972 char_u *in_start;
15973 char_u *expr_start;
15974 char_u *expr_end;
15975 char_u *in_end;
15976{
15977 char_u c1;
15978 char_u *retval = NULL;
15979 char_u *temp_result;
15980 char_u *nextcmd = NULL;
15981
15982 if (expr_end == NULL || in_end == NULL)
15983 return NULL;
15984 *expr_start = NUL;
15985 *expr_end = NUL;
15986 c1 = *in_end;
15987 *in_end = NUL;
15988
15989 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15990 if (temp_result != NULL && nextcmd == NULL)
15991 {
15992 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15993 + (in_end - expr_end) + 1));
15994 if (retval != NULL)
15995 {
15996 STRCPY(retval, in_start);
15997 STRCAT(retval, temp_result);
15998 STRCAT(retval, expr_end + 1);
15999 }
16000 }
16001 vim_free(temp_result);
16002
16003 *in_end = c1; /* put char back for error messages */
16004 *expr_start = '{';
16005 *expr_end = '}';
16006
16007 if (retval != NULL)
16008 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016009 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016010 if (expr_start != NULL)
16011 {
16012 /* Further expansion! */
16013 temp_result = make_expanded_name(retval, expr_start,
16014 expr_end, temp_result);
16015 vim_free(retval);
16016 retval = temp_result;
16017 }
16018 }
16019
16020 return retval;
16021}
16022
16023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016025 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016026 */
16027 static int
16028eval_isnamec(c)
16029 int c;
16030{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016031 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16032}
16033
16034/*
16035 * Return TRUE if character "c" can be used as the first character in a
16036 * variable or function name (excluding '{' and '}').
16037 */
16038 static int
16039eval_isnamec1(c)
16040 int c;
16041{
16042 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043}
16044
16045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046 * Set number v: variable to "val".
16047 */
16048 void
16049set_vim_var_nr(idx, val)
16050 int idx;
16051 long val;
16052{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016053 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054}
16055
16056/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016057 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058 */
16059 long
16060get_vim_var_nr(idx)
16061 int idx;
16062{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016063 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064}
16065
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016066#if defined(FEAT_AUTOCMD) || defined(PROTO)
16067/*
16068 * Get string v: variable value. Uses a static buffer, can only be used once.
16069 */
16070 char_u *
16071get_vim_var_str(idx)
16072 int idx;
16073{
16074 return get_tv_string(&vimvars[idx].vv_tv);
16075}
16076#endif
16077
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078/*
16079 * Set v:count, v:count1 and v:prevcount.
16080 */
16081 void
16082set_vcount(count, count1)
16083 long count;
16084 long count1;
16085{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016086 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16087 vimvars[VV_COUNT].vv_nr = count;
16088 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089}
16090
16091/*
16092 * Set string v: variable to a copy of "val".
16093 */
16094 void
16095set_vim_var_string(idx, val, len)
16096 int idx;
16097 char_u *val;
16098 int len; /* length of "val" to use or -1 (whole string) */
16099{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016100 /* Need to do this (at least) once, since we can't initialize a union.
16101 * Will always be invoked when "v:progname" is set. */
16102 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16103
Bram Moolenaare9a41262005-01-15 22:18:47 +000016104 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016106 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016108 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016110 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111}
16112
16113/*
16114 * Set v:register if needed.
16115 */
16116 void
16117set_reg_var(c)
16118 int c;
16119{
16120 char_u regname;
16121
16122 if (c == 0 || c == ' ')
16123 regname = '"';
16124 else
16125 regname = c;
16126 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016127 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128 set_vim_var_string(VV_REG, &regname, 1);
16129}
16130
16131/*
16132 * Get or set v:exception. If "oldval" == NULL, return the current value.
16133 * Otherwise, restore the value to "oldval" and return NULL.
16134 * Must always be called in pairs to save and restore v:exception! Does not
16135 * take care of memory allocations.
16136 */
16137 char_u *
16138v_exception(oldval)
16139 char_u *oldval;
16140{
16141 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016142 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143
Bram Moolenaare9a41262005-01-15 22:18:47 +000016144 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145 return NULL;
16146}
16147
16148/*
16149 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16150 * Otherwise, restore the value to "oldval" and return NULL.
16151 * Must always be called in pairs to save and restore v:throwpoint! Does not
16152 * take care of memory allocations.
16153 */
16154 char_u *
16155v_throwpoint(oldval)
16156 char_u *oldval;
16157{
16158 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016159 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160
Bram Moolenaare9a41262005-01-15 22:18:47 +000016161 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162 return NULL;
16163}
16164
16165#if defined(FEAT_AUTOCMD) || defined(PROTO)
16166/*
16167 * Set v:cmdarg.
16168 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16169 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16170 * Must always be called in pairs!
16171 */
16172 char_u *
16173set_cmdarg(eap, oldarg)
16174 exarg_T *eap;
16175 char_u *oldarg;
16176{
16177 char_u *oldval;
16178 char_u *newval;
16179 unsigned len;
16180
Bram Moolenaare9a41262005-01-15 22:18:47 +000016181 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016182 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016184 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016185 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016186 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187 }
16188
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016189 if (eap->force_bin == FORCE_BIN)
16190 len = 6;
16191 else if (eap->force_bin == FORCE_NOBIN)
16192 len = 8;
16193 else
16194 len = 0;
16195 if (eap->force_ff != 0)
16196 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16197# ifdef FEAT_MBYTE
16198 if (eap->force_enc != 0)
16199 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016200 if (eap->bad_char != 0)
16201 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016202# endif
16203
16204 newval = alloc(len + 1);
16205 if (newval == NULL)
16206 return NULL;
16207
16208 if (eap->force_bin == FORCE_BIN)
16209 sprintf((char *)newval, " ++bin");
16210 else if (eap->force_bin == FORCE_NOBIN)
16211 sprintf((char *)newval, " ++nobin");
16212 else
16213 *newval = NUL;
16214 if (eap->force_ff != 0)
16215 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16216 eap->cmd + eap->force_ff);
16217# ifdef FEAT_MBYTE
16218 if (eap->force_enc != 0)
16219 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16220 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016221 if (eap->bad_char != 0)
16222 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16223 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016224# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016225 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016226 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227}
16228#endif
16229
16230/*
16231 * Get the value of internal variable "name".
16232 * Return OK or FAIL.
16233 */
16234 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016235get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236 char_u *name;
16237 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016238 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016239 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240{
16241 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016242 typval_T *tv = NULL;
16243 typval_T atv;
16244 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246
16247 /* truncate the name, so that we can use strcmp() */
16248 cc = name[len];
16249 name[len] = NUL;
16250
16251 /*
16252 * Check for "b:changedtick".
16253 */
16254 if (STRCMP(name, "b:changedtick") == 0)
16255 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016256 atv.v_type = VAR_NUMBER;
16257 atv.vval.v_number = curbuf->b_changedtick;
16258 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259 }
16260
16261 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262 * Check for user-defined variables.
16263 */
16264 else
16265 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016266 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016268 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 }
16270
Bram Moolenaare9a41262005-01-15 22:18:47 +000016271 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016273 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016274 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275 ret = FAIL;
16276 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016277 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016278 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279
16280 name[len] = cc;
16281
16282 return ret;
16283}
16284
16285/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016286 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16287 * Also handle function call with Funcref variable: func(expr)
16288 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16289 */
16290 static int
16291handle_subscript(arg, rettv, evaluate, verbose)
16292 char_u **arg;
16293 typval_T *rettv;
16294 int evaluate; /* do more than finding the end */
16295 int verbose; /* give error messages */
16296{
16297 int ret = OK;
16298 dict_T *selfdict = NULL;
16299 char_u *s;
16300 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016301 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016302
16303 while (ret == OK
16304 && (**arg == '['
16305 || (**arg == '.' && rettv->v_type == VAR_DICT)
16306 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16307 && !vim_iswhite(*(*arg - 1)))
16308 {
16309 if (**arg == '(')
16310 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016311 /* need to copy the funcref so that we can clear rettv */
16312 functv = *rettv;
16313 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016314
16315 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016316 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016317 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016318 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16319 &len, evaluate, selfdict);
16320
16321 /* Clear the funcref afterwards, so that deleting it while
16322 * evaluating the arguments is possible (see test55). */
16323 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016324
16325 /* Stop the expression evaluation when immediately aborting on
16326 * error, or when an interrupt occurred or an exception was thrown
16327 * but not caught. */
16328 if (aborting())
16329 {
16330 if (ret == OK)
16331 clear_tv(rettv);
16332 ret = FAIL;
16333 }
16334 dict_unref(selfdict);
16335 selfdict = NULL;
16336 }
16337 else /* **arg == '[' || **arg == '.' */
16338 {
16339 dict_unref(selfdict);
16340 if (rettv->v_type == VAR_DICT)
16341 {
16342 selfdict = rettv->vval.v_dict;
16343 if (selfdict != NULL)
16344 ++selfdict->dv_refcount;
16345 }
16346 else
16347 selfdict = NULL;
16348 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16349 {
16350 clear_tv(rettv);
16351 ret = FAIL;
16352 }
16353 }
16354 }
16355 dict_unref(selfdict);
16356 return ret;
16357}
16358
16359/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016360 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16361 * value).
16362 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016363 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016364alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016365{
Bram Moolenaar33570922005-01-25 22:26:29 +000016366 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016367}
16368
16369/*
16370 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371 * The string "s" must have been allocated, it is consumed.
16372 * Return NULL for out of memory, the variable otherwise.
16373 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016374 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016375alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376 char_u *s;
16377{
Bram Moolenaar33570922005-01-25 22:26:29 +000016378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016380 rettv = alloc_tv();
16381 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016383 rettv->v_type = VAR_STRING;
16384 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385 }
16386 else
16387 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016388 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389}
16390
16391/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016392 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016394 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016395free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016396 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397{
16398 if (varp != NULL)
16399 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016400 switch (varp->v_type)
16401 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016402 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016403 func_unref(varp->vval.v_string);
16404 /*FALLTHROUGH*/
16405 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016406 vim_free(varp->vval.v_string);
16407 break;
16408 case VAR_LIST:
16409 list_unref(varp->vval.v_list);
16410 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016411 case VAR_DICT:
16412 dict_unref(varp->vval.v_dict);
16413 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016414 case VAR_NUMBER:
16415 case VAR_UNKNOWN:
16416 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016417 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016418 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016419 break;
16420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016421 vim_free(varp);
16422 }
16423}
16424
16425/*
16426 * Free the memory for a variable value and set the value to NULL or 0.
16427 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016428 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016429clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016430 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431{
16432 if (varp != NULL)
16433 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016434 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016435 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016436 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016437 func_unref(varp->vval.v_string);
16438 /*FALLTHROUGH*/
16439 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016440 vim_free(varp->vval.v_string);
16441 varp->vval.v_string = NULL;
16442 break;
16443 case VAR_LIST:
16444 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016445 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016446 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016447 case VAR_DICT:
16448 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016449 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016450 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016451 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016452 varp->vval.v_number = 0;
16453 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016454 case VAR_UNKNOWN:
16455 break;
16456 default:
16457 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016459 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 }
16461}
16462
16463/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016464 * Set the value of a variable to NULL without freeing items.
16465 */
16466 static void
16467init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016468 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016469{
16470 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016471 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016472}
16473
16474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475 * Get the number value of a variable.
16476 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016477 * For incompatible types, return 0.
16478 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16479 * caller of incompatible types: it sets *denote to TRUE if "denote"
16480 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481 */
16482 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016483get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016484 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016486 int error = FALSE;
16487
16488 return get_tv_number_chk(varp, &error); /* return 0L on error */
16489}
16490
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016491 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016492get_tv_number_chk(varp, denote)
16493 typval_T *varp;
16494 int *denote;
16495{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016496 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016498 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016500 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016501 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016502 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016503 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016504 break;
16505 case VAR_STRING:
16506 if (varp->vval.v_string != NULL)
16507 vim_str2nr(varp->vval.v_string, NULL, NULL,
16508 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016509 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016510 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016511 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016512 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016513 case VAR_DICT:
16514 EMSG(_("E728: Using a Dictionary as a number"));
16515 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016516 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016517 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016518 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016520 if (denote == NULL) /* useful for values that must be unsigned */
16521 n = -1;
16522 else
16523 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016524 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525}
16526
16527/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016528 * Get the lnum from the first argument.
16529 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016530 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 */
16532 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016533get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016534 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535{
Bram Moolenaar33570922005-01-25 22:26:29 +000016536 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 linenr_T lnum;
16538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016539 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 if (lnum == 0) /* no valid number, try using line() */
16541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016542 rettv.v_type = VAR_NUMBER;
16543 f_line(argvars, &rettv);
16544 lnum = rettv.vval.v_number;
16545 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 }
16547 return lnum;
16548}
16549
16550/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016551 * Get the lnum from the first argument.
16552 * Also accepts "$", then "buf" is used.
16553 * Returns 0 on error.
16554 */
16555 static linenr_T
16556get_tv_lnum_buf(argvars, buf)
16557 typval_T *argvars;
16558 buf_T *buf;
16559{
16560 if (argvars[0].v_type == VAR_STRING
16561 && argvars[0].vval.v_string != NULL
16562 && argvars[0].vval.v_string[0] == '$'
16563 && buf != NULL)
16564 return buf->b_ml.ml_line_count;
16565 return get_tv_number_chk(&argvars[0], NULL);
16566}
16567
16568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569 * Get the string value of a variable.
16570 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016571 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16572 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573 * If the String variable has never been set, return an empty string.
16574 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016575 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16576 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016577 */
16578 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016579get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016580 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581{
16582 static char_u mybuf[NUMBUFLEN];
16583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016584 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585}
16586
16587 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016588get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016589 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016590 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016592 char_u *res = get_tv_string_buf_chk(varp, buf);
16593
16594 return res != NULL ? res : (char_u *)"";
16595}
16596
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016597 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016598get_tv_string_chk(varp)
16599 typval_T *varp;
16600{
16601 static char_u mybuf[NUMBUFLEN];
16602
16603 return get_tv_string_buf_chk(varp, mybuf);
16604}
16605
16606 static char_u *
16607get_tv_string_buf_chk(varp, buf)
16608 typval_T *varp;
16609 char_u *buf;
16610{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016611 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016612 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016613 case VAR_NUMBER:
16614 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16615 return buf;
16616 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016617 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016618 break;
16619 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016620 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016621 break;
16622 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016623 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016624 break;
16625 case VAR_STRING:
16626 if (varp->vval.v_string != NULL)
16627 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016628 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016629 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016630 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016631 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016633 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634}
16635
16636/*
16637 * Find variable "name" in the list of variables.
16638 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016639 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016640 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016641 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016643 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016644find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016646 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016649 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650
Bram Moolenaara7043832005-01-21 11:56:39 +000016651 ht = find_var_ht(name, &varname);
16652 if (htp != NULL)
16653 *htp = ht;
16654 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016656 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657}
16658
16659/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016660 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016661 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016663 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016664find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016665 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016666 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016667 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016668{
Bram Moolenaar33570922005-01-25 22:26:29 +000016669 hashitem_T *hi;
16670
16671 if (*varname == NUL)
16672 {
16673 /* Must be something like "s:", otherwise "ht" would be NULL. */
16674 switch (varname[-2])
16675 {
16676 case 's': return &SCRIPT_SV(current_SID).sv_var;
16677 case 'g': return &globvars_var;
16678 case 'v': return &vimvars_var;
16679 case 'b': return &curbuf->b_bufvar;
16680 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016681 case 'l': return current_funccal == NULL
16682 ? NULL : &current_funccal->l_vars_var;
16683 case 'a': return current_funccal == NULL
16684 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016685 }
16686 return NULL;
16687 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016688
16689 hi = hash_find(ht, varname);
16690 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016691 {
16692 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016693 * worked find the variable again. Don't auto-load a script if it was
16694 * loaded already, otherwise it would be loaded every time when
16695 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016696 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016697 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016698 hi = hash_find(ht, varname);
16699 if (HASHITEM_EMPTY(hi))
16700 return NULL;
16701 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016702 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016703}
16704
16705/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016706 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016707 * Set "varname" to the start of name without ':'.
16708 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016709 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016710find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711 char_u *name;
16712 char_u **varname;
16713{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016714 hashitem_T *hi;
16715
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 if (name[1] != ':')
16717 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016718 /* The name must not start with a colon or #. */
16719 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 return NULL;
16721 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016722
16723 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016724 hi = hash_find(&compat_hashtab, name);
16725 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016726 return &compat_hashtab;
16727
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016729 return &globvarht; /* global variable */
16730 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731 }
16732 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016733 if (*name == 'g') /* global variable */
16734 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016735 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16736 */
16737 if (vim_strchr(name + 2, ':') != NULL
16738 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016739 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016741 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016742 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016743 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016744 if (*name == 'v') /* v: variable */
16745 return &vimvarht;
16746 if (*name == 'a' && current_funccal != NULL) /* function argument */
16747 return &current_funccal->l_avars.dv_hashtab;
16748 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16749 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750 if (*name == 's' /* script variable */
16751 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16752 return &SCRIPT_VARS(current_SID);
16753 return NULL;
16754}
16755
16756/*
16757 * Get the string value of a (global/local) variable.
16758 * Returns NULL when it doesn't exist.
16759 */
16760 char_u *
16761get_var_value(name)
16762 char_u *name;
16763{
Bram Moolenaar33570922005-01-25 22:26:29 +000016764 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765
Bram Moolenaara7043832005-01-21 11:56:39 +000016766 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 if (v == NULL)
16768 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016769 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770}
16771
16772/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016773 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 * sourcing this script and when executing functions defined in the script.
16775 */
16776 void
16777new_script_vars(id)
16778 scid_T id;
16779{
Bram Moolenaara7043832005-01-21 11:56:39 +000016780 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016781 hashtab_T *ht;
16782 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016783
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16785 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016786 /* Re-allocating ga_data means that an ht_array pointing to
16787 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016788 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016789 for (i = 1; i <= ga_scripts.ga_len; ++i)
16790 {
16791 ht = &SCRIPT_VARS(i);
16792 if (ht->ht_mask == HT_INIT_SIZE - 1)
16793 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016794 sv = &SCRIPT_SV(i);
16795 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016796 }
16797
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 while (ga_scripts.ga_len < id)
16799 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016800 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16801 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 }
16804 }
16805}
16806
16807/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016808 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16809 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 */
16811 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016812init_var_dict(dict, dict_var)
16813 dict_T *dict;
16814 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815{
Bram Moolenaar33570922005-01-25 22:26:29 +000016816 hash_init(&dict->dv_hashtab);
16817 dict->dv_refcount = 99999;
16818 dict_var->di_tv.vval.v_dict = dict;
16819 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016820 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016821 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16822 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823}
16824
16825/*
16826 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016827 * Frees all allocated variables and the value they contain.
16828 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829 */
16830 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016831vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016832 hashtab_T *ht;
16833{
16834 vars_clear_ext(ht, TRUE);
16835}
16836
16837/*
16838 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16839 */
16840 static void
16841vars_clear_ext(ht, free_val)
16842 hashtab_T *ht;
16843 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844{
Bram Moolenaara7043832005-01-21 11:56:39 +000016845 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016846 hashitem_T *hi;
16847 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848
Bram Moolenaar33570922005-01-25 22:26:29 +000016849 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016850 todo = ht->ht_used;
16851 for (hi = ht->ht_array; todo > 0; ++hi)
16852 {
16853 if (!HASHITEM_EMPTY(hi))
16854 {
16855 --todo;
16856
Bram Moolenaar33570922005-01-25 22:26:29 +000016857 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016858 * ht_array might change then. hash_clear() takes care of it
16859 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016860 v = HI2DI(hi);
16861 if (free_val)
16862 clear_tv(&v->di_tv);
16863 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16864 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016865 }
16866 }
16867 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016868 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869}
16870
Bram Moolenaara7043832005-01-21 11:56:39 +000016871/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016872 * Delete a variable from hashtab "ht" at item "hi".
16873 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016876delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016877 hashtab_T *ht;
16878 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879{
Bram Moolenaar33570922005-01-25 22:26:29 +000016880 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016881
16882 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016883 clear_tv(&di->di_tv);
16884 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885}
16886
16887/*
16888 * List the value of one internal variable.
16889 */
16890 static void
16891list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016892 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893 char_u *prefix;
16894{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016895 char_u *tofree;
16896 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016897 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016898
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016899 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000016900 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016901 s == NULL ? (char_u *)"" : s);
16902 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903}
16904
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905 static void
16906list_one_var_a(prefix, name, type, string)
16907 char_u *prefix;
16908 char_u *name;
16909 int type;
16910 char_u *string;
16911{
16912 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16913 if (name != NULL) /* "a:" vars don't have a name stored */
16914 msg_puts(name);
16915 msg_putchar(' ');
16916 msg_advance(22);
16917 if (type == VAR_NUMBER)
16918 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016919 else if (type == VAR_FUNC)
16920 msg_putchar('*');
16921 else if (type == VAR_LIST)
16922 {
16923 msg_putchar('[');
16924 if (*string == '[')
16925 ++string;
16926 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016927 else if (type == VAR_DICT)
16928 {
16929 msg_putchar('{');
16930 if (*string == '{')
16931 ++string;
16932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 else
16934 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016935
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016937
16938 if (type == VAR_FUNC)
16939 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940}
16941
16942/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016943 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 * If the variable already exists, the value is updated.
16945 * Otherwise the variable is created.
16946 */
16947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016948set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016950 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016951 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952{
Bram Moolenaar33570922005-01-25 22:26:29 +000016953 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016955 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016956 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016958 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016959 {
16960 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16961 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16962 ? name[2] : name[0]))
16963 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016964 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016965 return;
16966 }
16967 if (function_exists(name))
16968 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016969 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016970 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016971 return;
16972 }
16973 }
16974
Bram Moolenaara7043832005-01-21 11:56:39 +000016975 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016976 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016977 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016978 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016979 return;
16980 }
16981
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016982 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016983 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016985 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016986 if (var_check_ro(v->di_flags, name)
16987 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016988 return;
16989 if (v->di_tv.v_type != tv->v_type
16990 && !((v->di_tv.v_type == VAR_STRING
16991 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016992 && (tv->v_type == VAR_STRING
16993 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016994 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016995 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016996 return;
16997 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016998
16999 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017000 * Handle setting internal v: variables separately: we don't change
17001 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017002 */
17003 if (ht == &vimvarht)
17004 {
17005 if (v->di_tv.v_type == VAR_STRING)
17006 {
17007 vim_free(v->di_tv.vval.v_string);
17008 if (copy || tv->v_type != VAR_STRING)
17009 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17010 else
17011 {
17012 /* Take over the string to avoid an extra alloc/free. */
17013 v->di_tv.vval.v_string = tv->vval.v_string;
17014 tv->vval.v_string = NULL;
17015 }
17016 }
17017 else if (v->di_tv.v_type != VAR_NUMBER)
17018 EMSG2(_(e_intern2), "set_var()");
17019 else
17020 v->di_tv.vval.v_number = get_tv_number(tv);
17021 return;
17022 }
17023
17024 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 }
17026 else /* add a new variable */
17027 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017028 /* Make sure the variable name is valid. */
17029 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017030 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17031 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017032 {
17033 EMSG2(_(e_illvar), varname);
17034 return;
17035 }
17036
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017037 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17038 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017039 if (v == NULL)
17040 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017041 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017042 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017044 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 return;
17046 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017047 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017050 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017051 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017052 else
17053 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017054 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017055 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017056 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058}
17059
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017060/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017061 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17062 * Also give an error message.
17063 */
17064 static int
17065var_check_ro(flags, name)
17066 int flags;
17067 char_u *name;
17068{
17069 if (flags & DI_FLAGS_RO)
17070 {
17071 EMSG2(_(e_readonlyvar), name);
17072 return TRUE;
17073 }
17074 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17075 {
17076 EMSG2(_(e_readonlysbx), name);
17077 return TRUE;
17078 }
17079 return FALSE;
17080}
17081
17082/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017083 * Return TRUE if typeval "tv" is set to be locked (immutable).
17084 * Also give an error message, using "name".
17085 */
17086 static int
17087tv_check_lock(lock, name)
17088 int lock;
17089 char_u *name;
17090{
17091 if (lock & VAR_LOCKED)
17092 {
17093 EMSG2(_("E741: Value is locked: %s"),
17094 name == NULL ? (char_u *)_("Unknown") : name);
17095 return TRUE;
17096 }
17097 if (lock & VAR_FIXED)
17098 {
17099 EMSG2(_("E742: Cannot change value of %s"),
17100 name == NULL ? (char_u *)_("Unknown") : name);
17101 return TRUE;
17102 }
17103 return FALSE;
17104}
17105
17106/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017107 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017108 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017109 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017112copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017113 typval_T *from;
17114 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017116 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017117 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017118 switch (from->v_type)
17119 {
17120 case VAR_NUMBER:
17121 to->vval.v_number = from->vval.v_number;
17122 break;
17123 case VAR_STRING:
17124 case VAR_FUNC:
17125 if (from->vval.v_string == NULL)
17126 to->vval.v_string = NULL;
17127 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017128 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017129 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017130 if (from->v_type == VAR_FUNC)
17131 func_ref(to->vval.v_string);
17132 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017133 break;
17134 case VAR_LIST:
17135 if (from->vval.v_list == NULL)
17136 to->vval.v_list = NULL;
17137 else
17138 {
17139 to->vval.v_list = from->vval.v_list;
17140 ++to->vval.v_list->lv_refcount;
17141 }
17142 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017143 case VAR_DICT:
17144 if (from->vval.v_dict == NULL)
17145 to->vval.v_dict = NULL;
17146 else
17147 {
17148 to->vval.v_dict = from->vval.v_dict;
17149 ++to->vval.v_dict->dv_refcount;
17150 }
17151 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017152 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017153 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017154 break;
17155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156}
17157
17158/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017159 * Make a copy of an item.
17160 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017161 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17162 * reference to an already copied list/dict can be used.
17163 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017164 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017165 static int
17166item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017167 typval_T *from;
17168 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017169 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017170 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017171{
17172 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017173 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017174
Bram Moolenaar33570922005-01-25 22:26:29 +000017175 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017176 {
17177 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017178 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017179 }
17180 ++recurse;
17181
17182 switch (from->v_type)
17183 {
17184 case VAR_NUMBER:
17185 case VAR_STRING:
17186 case VAR_FUNC:
17187 copy_tv(from, to);
17188 break;
17189 case VAR_LIST:
17190 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017191 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017192 if (from->vval.v_list == NULL)
17193 to->vval.v_list = NULL;
17194 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17195 {
17196 /* use the copy made earlier */
17197 to->vval.v_list = from->vval.v_list->lv_copylist;
17198 ++to->vval.v_list->lv_refcount;
17199 }
17200 else
17201 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17202 if (to->vval.v_list == NULL)
17203 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017204 break;
17205 case VAR_DICT:
17206 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017207 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017208 if (from->vval.v_dict == NULL)
17209 to->vval.v_dict = NULL;
17210 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17211 {
17212 /* use the copy made earlier */
17213 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17214 ++to->vval.v_dict->dv_refcount;
17215 }
17216 else
17217 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17218 if (to->vval.v_dict == NULL)
17219 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017220 break;
17221 default:
17222 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017223 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017224 }
17225 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017226 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017227}
17228
17229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230 * ":echo expr1 ..." print each argument separated with a space, add a
17231 * newline at the end.
17232 * ":echon expr1 ..." print each argument plain.
17233 */
17234 void
17235ex_echo(eap)
17236 exarg_T *eap;
17237{
17238 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017239 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017240 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 char_u *p;
17242 int needclr = TRUE;
17243 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017244 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245
17246 if (eap->skip)
17247 ++emsg_skip;
17248 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17249 {
17250 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017251 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252 {
17253 /*
17254 * Report the invalid expression unless the expression evaluation
17255 * has been cancelled due to an aborting error, an interrupt, or an
17256 * exception.
17257 */
17258 if (!aborting())
17259 EMSG2(_(e_invexpr2), p);
17260 break;
17261 }
17262 if (!eap->skip)
17263 {
17264 if (atstart)
17265 {
17266 atstart = FALSE;
17267 /* Call msg_start() after eval1(), evaluating the expression
17268 * may cause a message to appear. */
17269 if (eap->cmdidx == CMD_echo)
17270 msg_start();
17271 }
17272 else if (eap->cmdidx == CMD_echo)
17273 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017274 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017275 if (p != NULL)
17276 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017278 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017280 if (*p != TAB && needclr)
17281 {
17282 /* remove any text still there from the command */
17283 msg_clr_eos();
17284 needclr = FALSE;
17285 }
17286 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287 }
17288 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017289 {
17290#ifdef FEAT_MBYTE
17291 if (has_mbyte)
17292 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017293 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017294
17295 (void)msg_outtrans_len_attr(p, i, echo_attr);
17296 p += i - 1;
17297 }
17298 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017300 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017303 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017305 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306 arg = skipwhite(arg);
17307 }
17308 eap->nextcmd = check_nextcmd(arg);
17309
17310 if (eap->skip)
17311 --emsg_skip;
17312 else
17313 {
17314 /* remove text that may still be there from the command */
17315 if (needclr)
17316 msg_clr_eos();
17317 if (eap->cmdidx == CMD_echo)
17318 msg_end();
17319 }
17320}
17321
17322/*
17323 * ":echohl {name}".
17324 */
17325 void
17326ex_echohl(eap)
17327 exarg_T *eap;
17328{
17329 int id;
17330
17331 id = syn_name2id(eap->arg);
17332 if (id == 0)
17333 echo_attr = 0;
17334 else
17335 echo_attr = syn_id2attr(id);
17336}
17337
17338/*
17339 * ":execute expr1 ..." execute the result of an expression.
17340 * ":echomsg expr1 ..." Print a message
17341 * ":echoerr expr1 ..." Print an error
17342 * Each gets spaces around each argument and a newline at the end for
17343 * echo commands
17344 */
17345 void
17346ex_execute(eap)
17347 exarg_T *eap;
17348{
17349 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017350 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 int ret = OK;
17352 char_u *p;
17353 garray_T ga;
17354 int len;
17355 int save_did_emsg;
17356
17357 ga_init2(&ga, 1, 80);
17358
17359 if (eap->skip)
17360 ++emsg_skip;
17361 while (*arg != NUL && *arg != '|' && *arg != '\n')
17362 {
17363 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017364 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365 {
17366 /*
17367 * Report the invalid expression unless the expression evaluation
17368 * has been cancelled due to an aborting error, an interrupt, or an
17369 * exception.
17370 */
17371 if (!aborting())
17372 EMSG2(_(e_invexpr2), p);
17373 ret = FAIL;
17374 break;
17375 }
17376
17377 if (!eap->skip)
17378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017379 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380 len = (int)STRLEN(p);
17381 if (ga_grow(&ga, len + 2) == FAIL)
17382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017383 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 ret = FAIL;
17385 break;
17386 }
17387 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 ga.ga_len += len;
17391 }
17392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017393 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394 arg = skipwhite(arg);
17395 }
17396
17397 if (ret != FAIL && ga.ga_data != NULL)
17398 {
17399 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017402 out_flush();
17403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 else if (eap->cmdidx == CMD_echoerr)
17405 {
17406 /* We don't want to abort following commands, restore did_emsg. */
17407 save_did_emsg = did_emsg;
17408 EMSG((char_u *)ga.ga_data);
17409 if (!force_abort)
17410 did_emsg = save_did_emsg;
17411 }
17412 else if (eap->cmdidx == CMD_execute)
17413 do_cmdline((char_u *)ga.ga_data,
17414 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17415 }
17416
17417 ga_clear(&ga);
17418
17419 if (eap->skip)
17420 --emsg_skip;
17421
17422 eap->nextcmd = check_nextcmd(arg);
17423}
17424
17425/*
17426 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17427 * "arg" points to the "&" or '+' when called, to "option" when returning.
17428 * Returns NULL when no option name found. Otherwise pointer to the char
17429 * after the option name.
17430 */
17431 static char_u *
17432find_option_end(arg, opt_flags)
17433 char_u **arg;
17434 int *opt_flags;
17435{
17436 char_u *p = *arg;
17437
17438 ++p;
17439 if (*p == 'g' && p[1] == ':')
17440 {
17441 *opt_flags = OPT_GLOBAL;
17442 p += 2;
17443 }
17444 else if (*p == 'l' && p[1] == ':')
17445 {
17446 *opt_flags = OPT_LOCAL;
17447 p += 2;
17448 }
17449 else
17450 *opt_flags = 0;
17451
17452 if (!ASCII_ISALPHA(*p))
17453 return NULL;
17454 *arg = p;
17455
17456 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17457 p += 4; /* termcap option */
17458 else
17459 while (ASCII_ISALPHA(*p))
17460 ++p;
17461 return p;
17462}
17463
17464/*
17465 * ":function"
17466 */
17467 void
17468ex_function(eap)
17469 exarg_T *eap;
17470{
17471 char_u *theline;
17472 int j;
17473 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475 char_u *name = NULL;
17476 char_u *p;
17477 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017478 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 garray_T newargs;
17480 garray_T newlines;
17481 int varargs = FALSE;
17482 int mustend = FALSE;
17483 int flags = 0;
17484 ufunc_T *fp;
17485 int indent;
17486 int nesting;
17487 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017488 dictitem_T *v;
17489 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017490 static int func_nr = 0; /* number for nameless function */
17491 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017492 hashtab_T *ht;
17493 int todo;
17494 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017495 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496
17497 /*
17498 * ":function" without argument: list functions.
17499 */
17500 if (ends_excmd(*eap->arg))
17501 {
17502 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017503 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017504 todo = func_hashtab.ht_used;
17505 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017506 {
17507 if (!HASHITEM_EMPTY(hi))
17508 {
17509 --todo;
17510 fp = HI2UF(hi);
17511 if (!isdigit(*fp->uf_name))
17512 list_func_head(fp, FALSE);
17513 }
17514 }
17515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 eap->nextcmd = check_nextcmd(eap->arg);
17517 return;
17518 }
17519
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017520 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017521 * ":function /pat": list functions matching pattern.
17522 */
17523 if (*eap->arg == '/')
17524 {
17525 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17526 if (!eap->skip)
17527 {
17528 regmatch_T regmatch;
17529
17530 c = *p;
17531 *p = NUL;
17532 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17533 *p = c;
17534 if (regmatch.regprog != NULL)
17535 {
17536 regmatch.rm_ic = p_ic;
17537
17538 todo = func_hashtab.ht_used;
17539 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17540 {
17541 if (!HASHITEM_EMPTY(hi))
17542 {
17543 --todo;
17544 fp = HI2UF(hi);
17545 if (!isdigit(*fp->uf_name)
17546 && vim_regexec(&regmatch, fp->uf_name, 0))
17547 list_func_head(fp, FALSE);
17548 }
17549 }
17550 }
17551 }
17552 if (*p == '/')
17553 ++p;
17554 eap->nextcmd = check_nextcmd(p);
17555 return;
17556 }
17557
17558 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017559 * Get the function name. There are these situations:
17560 * func normal function name
17561 * "name" == func, "fudi.fd_dict" == NULL
17562 * dict.func new dictionary entry
17563 * "name" == NULL, "fudi.fd_dict" set,
17564 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17565 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017566 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017567 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17568 * dict.func existing dict entry that's not a Funcref
17569 * "name" == NULL, "fudi.fd_dict" set,
17570 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017573 name = trans_function_name(&p, eap->skip, 0, &fudi);
17574 paren = (vim_strchr(p, '(') != NULL);
17575 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576 {
17577 /*
17578 * Return on an invalid expression in braces, unless the expression
17579 * evaluation has been cancelled due to an aborting error, an
17580 * interrupt, or an exception.
17581 */
17582 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017583 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017584 if (!eap->skip && fudi.fd_newkey != NULL)
17585 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017586 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 else
17590 eap->skip = TRUE;
17591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 /* An error in a function call during evaluation of an expression in magic
17593 * braces should not cause the function not to be defined. */
17594 saved_did_emsg = did_emsg;
17595 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596
17597 /*
17598 * ":function func" with only function name: list function.
17599 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017600 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601 {
17602 if (!ends_excmd(*skipwhite(p)))
17603 {
17604 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017605 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 }
17607 eap->nextcmd = check_nextcmd(p);
17608 if (eap->nextcmd != NULL)
17609 *p = NUL;
17610 if (!eap->skip && !got_int)
17611 {
17612 fp = find_func(name);
17613 if (fp != NULL)
17614 {
17615 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017616 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017618 if (FUNCLINE(fp, j) == NULL)
17619 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 msg_putchar('\n');
17621 msg_outnum((long)(j + 1));
17622 if (j < 9)
17623 msg_putchar(' ');
17624 if (j < 99)
17625 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017626 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 out_flush(); /* show a line at a time */
17628 ui_breakcheck();
17629 }
17630 if (!got_int)
17631 {
17632 msg_putchar('\n');
17633 msg_puts((char_u *)" endfunction");
17634 }
17635 }
17636 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017637 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017639 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 }
17641
17642 /*
17643 * ":function name(arg1, arg2)" Define function.
17644 */
17645 p = skipwhite(p);
17646 if (*p != '(')
17647 {
17648 if (!eap->skip)
17649 {
17650 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017651 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652 }
17653 /* attempt to continue by skipping some text */
17654 if (vim_strchr(p, '(') != NULL)
17655 p = vim_strchr(p, '(');
17656 }
17657 p = skipwhite(p + 1);
17658
17659 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17660 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17661
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017662 if (!eap->skip)
17663 {
17664 /* Check the name of the function. */
17665 if (name != NULL)
17666 arg = name;
17667 else
17668 arg = fudi.fd_newkey;
17669 if (arg != NULL)
17670 {
17671 if (*arg == K_SPECIAL)
17672 j = 3;
17673 else
17674 j = 0;
17675 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17676 : eval_isnamec(arg[j])))
17677 ++j;
17678 if (arg[j] != NUL)
17679 emsg_funcname(_(e_invarg2), arg);
17680 }
17681 }
17682
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 /*
17684 * Isolate the arguments: "arg1, arg2, ...)"
17685 */
17686 while (*p != ')')
17687 {
17688 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17689 {
17690 varargs = TRUE;
17691 p += 3;
17692 mustend = TRUE;
17693 }
17694 else
17695 {
17696 arg = p;
17697 while (ASCII_ISALNUM(*p) || *p == '_')
17698 ++p;
17699 if (arg == p || isdigit(*arg)
17700 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17701 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17702 {
17703 if (!eap->skip)
17704 EMSG2(_("E125: Illegal argument: %s"), arg);
17705 break;
17706 }
17707 if (ga_grow(&newargs, 1) == FAIL)
17708 goto erret;
17709 c = *p;
17710 *p = NUL;
17711 arg = vim_strsave(arg);
17712 if (arg == NULL)
17713 goto erret;
17714 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17715 *p = c;
17716 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 if (*p == ',')
17718 ++p;
17719 else
17720 mustend = TRUE;
17721 }
17722 p = skipwhite(p);
17723 if (mustend && *p != ')')
17724 {
17725 if (!eap->skip)
17726 EMSG2(_(e_invarg2), eap->arg);
17727 break;
17728 }
17729 }
17730 ++p; /* skip the ')' */
17731
Bram Moolenaare9a41262005-01-15 22:18:47 +000017732 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733 for (;;)
17734 {
17735 p = skipwhite(p);
17736 if (STRNCMP(p, "range", 5) == 0)
17737 {
17738 flags |= FC_RANGE;
17739 p += 5;
17740 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017741 else if (STRNCMP(p, "dict", 4) == 0)
17742 {
17743 flags |= FC_DICT;
17744 p += 4;
17745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 else if (STRNCMP(p, "abort", 5) == 0)
17747 {
17748 flags |= FC_ABORT;
17749 p += 5;
17750 }
17751 else
17752 break;
17753 }
17754
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017755 /* When there is a line break use what follows for the function body.
17756 * Makes 'exe "func Test()\n...\nendfunc"' work. */
17757 if (*p == '\n')
17758 line_arg = p + 1;
17759 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 EMSG(_(e_trailing));
17761
17762 /*
17763 * Read the body of the function, until ":endfunction" is found.
17764 */
17765 if (KeyTyped)
17766 {
17767 /* Check if the function already exists, don't let the user type the
17768 * whole function before telling him it doesn't work! For a script we
17769 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017770 if (!eap->skip && !eap->forceit)
17771 {
17772 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17773 EMSG(_(e_funcdict));
17774 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017775 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017778 if (!eap->skip && did_emsg)
17779 goto erret;
17780
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781 msg_putchar('\n'); /* don't overwrite the function name */
17782 cmdline_row = msg_row;
17783 }
17784
17785 indent = 2;
17786 nesting = 0;
17787 for (;;)
17788 {
17789 msg_scroll = TRUE;
17790 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017791 sourcing_lnum_off = sourcing_lnum;
17792
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017793 if (line_arg != NULL)
17794 {
17795 /* Use eap->arg, split up in parts by line breaks. */
17796 theline = line_arg;
17797 p = vim_strchr(theline, '\n');
17798 if (p == NULL)
17799 line_arg += STRLEN(line_arg);
17800 else
17801 {
17802 *p = NUL;
17803 line_arg = p + 1;
17804 }
17805 }
17806 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 theline = getcmdline(':', 0L, indent);
17808 else
17809 theline = eap->getline(':', eap->cookie, indent);
17810 if (KeyTyped)
17811 lines_left = Rows - 1;
17812 if (theline == NULL)
17813 {
17814 EMSG(_("E126: Missing :endfunction"));
17815 goto erret;
17816 }
17817
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017818 /* Detect line continuation: sourcing_lnum increased more than one. */
17819 if (sourcing_lnum > sourcing_lnum_off + 1)
17820 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
17821 else
17822 sourcing_lnum_off = 0;
17823
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824 if (skip_until != NULL)
17825 {
17826 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17827 * don't check for ":endfunc". */
17828 if (STRCMP(theline, skip_until) == 0)
17829 {
17830 vim_free(skip_until);
17831 skip_until = NULL;
17832 }
17833 }
17834 else
17835 {
17836 /* skip ':' and blanks*/
17837 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17838 ;
17839
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017840 /* Check for "endfunction". */
17841 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017843 if (line_arg == NULL)
17844 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 break;
17846 }
17847
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017848 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849 * at "end". */
17850 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17851 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017852 else if (STRNCMP(p, "if", 2) == 0
17853 || STRNCMP(p, "wh", 2) == 0
17854 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 || STRNCMP(p, "try", 3) == 0)
17856 indent += 2;
17857
17858 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017859 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017861 if (*p == '!')
17862 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863 p += eval_fname_script(p);
17864 if (ASCII_ISALPHA(*p))
17865 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017866 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 if (*skipwhite(p) == '(')
17868 {
17869 ++nesting;
17870 indent += 2;
17871 }
17872 }
17873 }
17874
17875 /* Check for ":append" or ":insert". */
17876 p = skip_range(p, NULL);
17877 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17878 || (p[0] == 'i'
17879 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17880 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17881 skip_until = vim_strsave((char_u *)".");
17882
17883 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17884 arg = skipwhite(skiptowhite(p));
17885 if (arg[0] == '<' && arg[1] =='<'
17886 && ((p[0] == 'p' && p[1] == 'y'
17887 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17888 || (p[0] == 'p' && p[1] == 'e'
17889 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17890 || (p[0] == 't' && p[1] == 'c'
17891 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17892 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17893 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017894 || (p[0] == 'm' && p[1] == 'z'
17895 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 ))
17897 {
17898 /* ":python <<" continues until a dot, like ":append" */
17899 p = skipwhite(arg + 2);
17900 if (*p == NUL)
17901 skip_until = vim_strsave((char_u *)".");
17902 else
17903 skip_until = vim_strsave(p);
17904 }
17905 }
17906
17907 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017908 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017909 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017910 if (line_arg == NULL)
17911 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017913 }
17914
17915 /* Copy the line to newly allocated memory. get_one_sourceline()
17916 * allocates 250 bytes per line, this saves 80% on average. The cost
17917 * is an extra alloc/free. */
17918 p = vim_strsave(theline);
17919 if (p != NULL)
17920 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017921 if (line_arg == NULL)
17922 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017923 theline = p;
17924 }
17925
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017926 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
17927
17928 /* Add NULL lines for continuation lines, so that the line count is
17929 * equal to the index in the growarray. */
17930 while (sourcing_lnum_off-- > 0)
17931 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017932
17933 /* Check for end of eap->arg. */
17934 if (line_arg != NULL && *line_arg == NUL)
17935 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 }
17937
17938 /* Don't define the function when skipping commands or when an error was
17939 * detected. */
17940 if (eap->skip || did_emsg)
17941 goto erret;
17942
17943 /*
17944 * If there are no errors, add the function
17945 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017946 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017947 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017948 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017949 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017950 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017951 emsg_funcname("E707: Function name conflicts with variable: %s",
17952 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017953 goto erret;
17954 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017955
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017956 fp = find_func(name);
17957 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017959 if (!eap->forceit)
17960 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017961 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017962 goto erret;
17963 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017964 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017965 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017966 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017967 name);
17968 goto erret;
17969 }
17970 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017971 ga_clear_strings(&(fp->uf_args));
17972 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017973 vim_free(name);
17974 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976 }
17977 else
17978 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017979 char numbuf[20];
17980
17981 fp = NULL;
17982 if (fudi.fd_newkey == NULL && !eap->forceit)
17983 {
17984 EMSG(_(e_funcdict));
17985 goto erret;
17986 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017987 if (fudi.fd_di == NULL)
17988 {
17989 /* Can't add a function to a locked dictionary */
17990 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17991 goto erret;
17992 }
17993 /* Can't change an existing function if it is locked */
17994 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17995 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017996
17997 /* Give the function a sequential number. Can only be used with a
17998 * Funcref! */
17999 vim_free(name);
18000 sprintf(numbuf, "%d", ++func_nr);
18001 name = vim_strsave((char_u *)numbuf);
18002 if (name == NULL)
18003 goto erret;
18004 }
18005
18006 if (fp == NULL)
18007 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018008 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018009 {
18010 int slen, plen;
18011 char_u *scriptname;
18012
18013 /* Check that the autoload name matches the script name. */
18014 j = FAIL;
18015 if (sourcing_name != NULL)
18016 {
18017 scriptname = autoload_name(name);
18018 if (scriptname != NULL)
18019 {
18020 p = vim_strchr(scriptname, '/');
18021 plen = STRLEN(p);
18022 slen = STRLEN(sourcing_name);
18023 if (slen > plen && fnamecmp(p,
18024 sourcing_name + slen - plen) == 0)
18025 j = OK;
18026 vim_free(scriptname);
18027 }
18028 }
18029 if (j == FAIL)
18030 {
18031 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18032 goto erret;
18033 }
18034 }
18035
18036 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037 if (fp == NULL)
18038 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018039
18040 if (fudi.fd_dict != NULL)
18041 {
18042 if (fudi.fd_di == NULL)
18043 {
18044 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018045 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018046 if (fudi.fd_di == NULL)
18047 {
18048 vim_free(fp);
18049 goto erret;
18050 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018051 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18052 {
18053 vim_free(fudi.fd_di);
18054 goto erret;
18055 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018056 }
18057 else
18058 /* overwrite existing dict entry */
18059 clear_tv(&fudi.fd_di->di_tv);
18060 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018061 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018062 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018063 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018064 }
18065
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018067 STRCPY(fp->uf_name, name);
18068 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018070 fp->uf_args = newargs;
18071 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018072#ifdef FEAT_PROFILE
18073 fp->uf_tml_count = NULL;
18074 fp->uf_tml_total = NULL;
18075 fp->uf_tml_self = NULL;
18076 fp->uf_profiling = FALSE;
18077 if (prof_def_func())
18078 func_do_profile(fp);
18079#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018080 fp->uf_varargs = varargs;
18081 fp->uf_flags = flags;
18082 fp->uf_calls = 0;
18083 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018084 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085
18086erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087 ga_clear_strings(&newargs);
18088 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018089ret_free:
18090 vim_free(skip_until);
18091 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094}
18095
18096/*
18097 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018098 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018100 * flags:
18101 * TFN_INT: internal function name OK
18102 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103 * Advances "pp" to just after the function name (if no error).
18104 */
18105 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018106trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107 char_u **pp;
18108 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018109 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018110 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018112 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 char_u *start;
18114 char_u *end;
18115 int lead;
18116 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018118 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018119
18120 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018121 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018123
18124 /* Check for hard coded <SNR>: already translated function ID (from a user
18125 * command). */
18126 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18127 && (*pp)[2] == (int)KE_SNR)
18128 {
18129 *pp += 3;
18130 len = get_id_len(pp) + 3;
18131 return vim_strnsave(start, len);
18132 }
18133
18134 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18135 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018137 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018139
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018140 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18141 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018142 if (end == start)
18143 {
18144 if (!skip)
18145 EMSG(_("E129: Function name required"));
18146 goto theend;
18147 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018148 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018149 {
18150 /*
18151 * Report an invalid expression in braces, unless the expression
18152 * evaluation has been cancelled due to an aborting error, an
18153 * interrupt, or an exception.
18154 */
18155 if (!aborting())
18156 {
18157 if (end != NULL)
18158 EMSG2(_(e_invarg2), start);
18159 }
18160 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018161 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018162 goto theend;
18163 }
18164
18165 if (lv.ll_tv != NULL)
18166 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018167 if (fdp != NULL)
18168 {
18169 fdp->fd_dict = lv.ll_dict;
18170 fdp->fd_newkey = lv.ll_newkey;
18171 lv.ll_newkey = NULL;
18172 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018173 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018174 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18175 {
18176 name = vim_strsave(lv.ll_tv->vval.v_string);
18177 *pp = end;
18178 }
18179 else
18180 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018181 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18182 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018183 EMSG(_(e_funcref));
18184 else
18185 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018186 name = NULL;
18187 }
18188 goto theend;
18189 }
18190
18191 if (lv.ll_name == NULL)
18192 {
18193 /* Error found, but continue after the function name. */
18194 *pp = end;
18195 goto theend;
18196 }
18197
18198 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018199 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018200 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018201 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18202 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18203 {
18204 /* When there was "s:" already or the name expanded to get a
18205 * leading "s:" then remove it. */
18206 lv.ll_name += 2;
18207 len -= 2;
18208 lead = 2;
18209 }
18210 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018211 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018212 {
18213 if (lead == 2) /* skip over "s:" */
18214 lv.ll_name += 2;
18215 len = (int)(end - lv.ll_name);
18216 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018217
18218 /*
18219 * Copy the function name to allocated memory.
18220 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18221 * Accept <SNR>123_name() outside a script.
18222 */
18223 if (skip)
18224 lead = 0; /* do nothing */
18225 else if (lead > 0)
18226 {
18227 lead = 3;
18228 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18229 {
18230 if (current_SID <= 0)
18231 {
18232 EMSG(_(e_usingsid));
18233 goto theend;
18234 }
18235 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18236 lead += (int)STRLEN(sid_buf);
18237 }
18238 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018239 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018240 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018241 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018242 goto theend;
18243 }
18244 name = alloc((unsigned)(len + lead + 1));
18245 if (name != NULL)
18246 {
18247 if (lead > 0)
18248 {
18249 name[0] = K_SPECIAL;
18250 name[1] = KS_EXTRA;
18251 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018252 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018253 STRCPY(name + 3, sid_buf);
18254 }
18255 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18256 name[len + lead] = NUL;
18257 }
18258 *pp = end;
18259
18260theend:
18261 clear_lval(&lv);
18262 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263}
18264
18265/*
18266 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18267 * Return 2 if "p" starts with "s:".
18268 * Return 0 otherwise.
18269 */
18270 static int
18271eval_fname_script(p)
18272 char_u *p;
18273{
18274 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18275 || STRNICMP(p + 1, "SNR>", 4) == 0))
18276 return 5;
18277 if (p[0] == 's' && p[1] == ':')
18278 return 2;
18279 return 0;
18280}
18281
18282/*
18283 * Return TRUE if "p" starts with "<SID>" or "s:".
18284 * Only works if eval_fname_script() returned non-zero for "p"!
18285 */
18286 static int
18287eval_fname_sid(p)
18288 char_u *p;
18289{
18290 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18291}
18292
18293/*
18294 * List the head of the function: "name(arg1, arg2)".
18295 */
18296 static void
18297list_func_head(fp, indent)
18298 ufunc_T *fp;
18299 int indent;
18300{
18301 int j;
18302
18303 msg_start();
18304 if (indent)
18305 MSG_PUTS(" ");
18306 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018307 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308 {
18309 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018310 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 }
18312 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018313 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018315 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 {
18317 if (j)
18318 MSG_PUTS(", ");
18319 msg_puts(FUNCARG(fp, j));
18320 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018321 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322 {
18323 if (j)
18324 MSG_PUTS(", ");
18325 MSG_PUTS("...");
18326 }
18327 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018328 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018329 if (p_verbose > 0)
18330 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331}
18332
18333/*
18334 * Find a function by name, return pointer to it in ufuncs.
18335 * Return NULL for unknown function.
18336 */
18337 static ufunc_T *
18338find_func(name)
18339 char_u *name;
18340{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018341 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018343 hi = hash_find(&func_hashtab, name);
18344 if (!HASHITEM_EMPTY(hi))
18345 return HI2UF(hi);
18346 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347}
18348
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018349#if defined(EXITFREE) || defined(PROTO)
18350 void
18351free_all_functions()
18352{
18353 hashitem_T *hi;
18354
18355 /* Need to start all over every time, because func_free() may change the
18356 * hash table. */
18357 while (func_hashtab.ht_used > 0)
18358 for (hi = func_hashtab.ht_array; ; ++hi)
18359 if (!HASHITEM_EMPTY(hi))
18360 {
18361 func_free(HI2UF(hi));
18362 break;
18363 }
18364}
18365#endif
18366
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018367/*
18368 * Return TRUE if a function "name" exists.
18369 */
18370 static int
18371function_exists(name)
18372 char_u *name;
18373{
18374 char_u *p = name;
18375 int n = FALSE;
18376
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018377 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018378 if (p != NULL)
18379 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018380 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018381 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018382 else
18383 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018384 vim_free(p);
18385 }
18386 return n;
18387}
18388
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018389/*
18390 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018391 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018392 */
18393 static int
18394builtin_function(name)
18395 char_u *name;
18396{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018397 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18398 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018399}
18400
Bram Moolenaar05159a02005-02-26 23:04:13 +000018401#if defined(FEAT_PROFILE) || defined(PROTO)
18402/*
18403 * Start profiling function "fp".
18404 */
18405 static void
18406func_do_profile(fp)
18407 ufunc_T *fp;
18408{
18409 fp->uf_tm_count = 0;
18410 profile_zero(&fp->uf_tm_self);
18411 profile_zero(&fp->uf_tm_total);
18412 if (fp->uf_tml_count == NULL)
18413 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18414 (sizeof(int) * fp->uf_lines.ga_len));
18415 if (fp->uf_tml_total == NULL)
18416 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18417 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18418 if (fp->uf_tml_self == NULL)
18419 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18420 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18421 fp->uf_tml_idx = -1;
18422 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18423 || fp->uf_tml_self == NULL)
18424 return; /* out of memory */
18425
18426 fp->uf_profiling = TRUE;
18427}
18428
18429/*
18430 * Dump the profiling results for all functions in file "fd".
18431 */
18432 void
18433func_dump_profile(fd)
18434 FILE *fd;
18435{
18436 hashitem_T *hi;
18437 int todo;
18438 ufunc_T *fp;
18439 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018440 ufunc_T **sorttab;
18441 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018442
18443 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018444 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18445
Bram Moolenaar05159a02005-02-26 23:04:13 +000018446 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18447 {
18448 if (!HASHITEM_EMPTY(hi))
18449 {
18450 --todo;
18451 fp = HI2UF(hi);
18452 if (fp->uf_profiling)
18453 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018454 if (sorttab != NULL)
18455 sorttab[st_len++] = fp;
18456
Bram Moolenaar05159a02005-02-26 23:04:13 +000018457 if (fp->uf_name[0] == K_SPECIAL)
18458 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18459 else
18460 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18461 if (fp->uf_tm_count == 1)
18462 fprintf(fd, "Called 1 time\n");
18463 else
18464 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18465 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18466 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18467 fprintf(fd, "\n");
18468 fprintf(fd, "count total (s) self (s)\n");
18469
18470 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18471 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018472 if (FUNCLINE(fp, i) == NULL)
18473 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000018474 prof_func_line(fd, fp->uf_tml_count[i],
18475 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018476 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18477 }
18478 fprintf(fd, "\n");
18479 }
18480 }
18481 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018482
18483 if (sorttab != NULL && st_len > 0)
18484 {
18485 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18486 prof_total_cmp);
18487 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18488 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18489 prof_self_cmp);
18490 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18491 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018492}
Bram Moolenaar73830342005-02-28 22:48:19 +000018493
18494 static void
18495prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18496 FILE *fd;
18497 ufunc_T **sorttab;
18498 int st_len;
18499 char *title;
18500 int prefer_self; /* when equal print only self time */
18501{
18502 int i;
18503 ufunc_T *fp;
18504
18505 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18506 fprintf(fd, "count total (s) self (s) function\n");
18507 for (i = 0; i < 20 && i < st_len; ++i)
18508 {
18509 fp = sorttab[i];
18510 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18511 prefer_self);
18512 if (fp->uf_name[0] == K_SPECIAL)
18513 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18514 else
18515 fprintf(fd, " %s()\n", fp->uf_name);
18516 }
18517 fprintf(fd, "\n");
18518}
18519
18520/*
18521 * Print the count and times for one function or function line.
18522 */
18523 static void
18524prof_func_line(fd, count, total, self, prefer_self)
18525 FILE *fd;
18526 int count;
18527 proftime_T *total;
18528 proftime_T *self;
18529 int prefer_self; /* when equal print only self time */
18530{
18531 if (count > 0)
18532 {
18533 fprintf(fd, "%5d ", count);
18534 if (prefer_self && profile_equal(total, self))
18535 fprintf(fd, " ");
18536 else
18537 fprintf(fd, "%s ", profile_msg(total));
18538 if (!prefer_self && profile_equal(total, self))
18539 fprintf(fd, " ");
18540 else
18541 fprintf(fd, "%s ", profile_msg(self));
18542 }
18543 else
18544 fprintf(fd, " ");
18545}
18546
18547/*
18548 * Compare function for total time sorting.
18549 */
18550 static int
18551#ifdef __BORLANDC__
18552_RTLENTRYF
18553#endif
18554prof_total_cmp(s1, s2)
18555 const void *s1;
18556 const void *s2;
18557{
18558 ufunc_T *p1, *p2;
18559
18560 p1 = *(ufunc_T **)s1;
18561 p2 = *(ufunc_T **)s2;
18562 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18563}
18564
18565/*
18566 * Compare function for self time sorting.
18567 */
18568 static int
18569#ifdef __BORLANDC__
18570_RTLENTRYF
18571#endif
18572prof_self_cmp(s1, s2)
18573 const void *s1;
18574 const void *s2;
18575{
18576 ufunc_T *p1, *p2;
18577
18578 p1 = *(ufunc_T **)s1;
18579 p2 = *(ufunc_T **)s2;
18580 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18581}
18582
Bram Moolenaar05159a02005-02-26 23:04:13 +000018583#endif
18584
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018585/* The names of packages that once were loaded is remembered. */
18586static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18587
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018588/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018589 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018590 * Return TRUE if a package was loaded.
18591 */
18592 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018593script_autoload(name, reload)
18594 char_u *name;
18595 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018596{
18597 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018598 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018599 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018600 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018601
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018602 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018603 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018604 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018605 return FALSE;
18606
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018607 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018608
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018609 /* Find the name in the list of previously loaded package names. Skip
18610 * "autoload/", it's always the same. */
18611 for (i = 0; i < ga_loaded.ga_len; ++i)
18612 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18613 break;
18614 if (!reload && i < ga_loaded.ga_len)
18615 ret = FALSE; /* was loaded already */
18616 else
18617 {
18618 /* Remember the name if it wasn't loaded already. */
18619 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18620 {
18621 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18622 tofree = NULL;
18623 }
18624
18625 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018626 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018627 ret = TRUE;
18628 }
18629
18630 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018631 return ret;
18632}
18633
18634/*
18635 * Return the autoload script name for a function or variable name.
18636 * Returns NULL when out of memory.
18637 */
18638 static char_u *
18639autoload_name(name)
18640 char_u *name;
18641{
18642 char_u *p;
18643 char_u *scriptname;
18644
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018645 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018646 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18647 if (scriptname == NULL)
18648 return FALSE;
18649 STRCPY(scriptname, "autoload/");
18650 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018651 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018652 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018653 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018654 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018655 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018656}
18657
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18659
18660/*
18661 * Function given to ExpandGeneric() to obtain the list of user defined
18662 * function names.
18663 */
18664 char_u *
18665get_user_func_name(xp, idx)
18666 expand_T *xp;
18667 int idx;
18668{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018669 static long_u done;
18670 static hashitem_T *hi;
18671 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672
18673 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018675 done = 0;
18676 hi = func_hashtab.ht_array;
18677 }
18678 if (done < func_hashtab.ht_used)
18679 {
18680 if (done++ > 0)
18681 ++hi;
18682 while (HASHITEM_EMPTY(hi))
18683 ++hi;
18684 fp = HI2UF(hi);
18685
18686 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18687 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688
18689 cat_func_name(IObuff, fp);
18690 if (xp->xp_context != EXPAND_USER_FUNC)
18691 {
18692 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018693 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694 STRCAT(IObuff, ")");
18695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 return IObuff;
18697 }
18698 return NULL;
18699}
18700
18701#endif /* FEAT_CMDL_COMPL */
18702
18703/*
18704 * Copy the function name of "fp" to buffer "buf".
18705 * "buf" must be able to hold the function name plus three bytes.
18706 * Takes care of script-local function names.
18707 */
18708 static void
18709cat_func_name(buf, fp)
18710 char_u *buf;
18711 ufunc_T *fp;
18712{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018713 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714 {
18715 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018716 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717 }
18718 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018719 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720}
18721
18722/*
18723 * ":delfunction {name}"
18724 */
18725 void
18726ex_delfunction(eap)
18727 exarg_T *eap;
18728{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018729 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 char_u *p;
18731 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018732 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733
18734 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018735 name = trans_function_name(&p, eap->skip, 0, &fudi);
18736 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018738 {
18739 if (fudi.fd_dict != NULL && !eap->skip)
18740 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 if (!ends_excmd(*skipwhite(p)))
18744 {
18745 vim_free(name);
18746 EMSG(_(e_trailing));
18747 return;
18748 }
18749 eap->nextcmd = check_nextcmd(p);
18750 if (eap->nextcmd != NULL)
18751 *p = NUL;
18752
18753 if (!eap->skip)
18754 fp = find_func(name);
18755 vim_free(name);
18756
18757 if (!eap->skip)
18758 {
18759 if (fp == NULL)
18760 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018761 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 return;
18763 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018764 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 {
18766 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18767 return;
18768 }
18769
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018770 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018772 /* Delete the dict item that refers to the function, it will
18773 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018774 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018776 else
18777 func_free(fp);
18778 }
18779}
18780
18781/*
18782 * Free a function and remove it from the list of functions.
18783 */
18784 static void
18785func_free(fp)
18786 ufunc_T *fp;
18787{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018788 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018789
18790 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018791 ga_clear_strings(&(fp->uf_args));
18792 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018793#ifdef FEAT_PROFILE
18794 vim_free(fp->uf_tml_count);
18795 vim_free(fp->uf_tml_total);
18796 vim_free(fp->uf_tml_self);
18797#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018798
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018799 /* remove the function from the function hashtable */
18800 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18801 if (HASHITEM_EMPTY(hi))
18802 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018803 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018804 hash_remove(&func_hashtab, hi);
18805
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018806 vim_free(fp);
18807}
18808
18809/*
18810 * Unreference a Function: decrement the reference count and free it when it
18811 * becomes zero. Only for numbered functions.
18812 */
18813 static void
18814func_unref(name)
18815 char_u *name;
18816{
18817 ufunc_T *fp;
18818
18819 if (name != NULL && isdigit(*name))
18820 {
18821 fp = find_func(name);
18822 if (fp == NULL)
18823 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018824 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018825 {
18826 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018827 * when "uf_calls" becomes zero. */
18828 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018829 func_free(fp);
18830 }
18831 }
18832}
18833
18834/*
18835 * Count a reference to a Function.
18836 */
18837 static void
18838func_ref(name)
18839 char_u *name;
18840{
18841 ufunc_T *fp;
18842
18843 if (name != NULL && isdigit(*name))
18844 {
18845 fp = find_func(name);
18846 if (fp == NULL)
18847 EMSG2(_(e_intern2), "func_ref()");
18848 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018849 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850 }
18851}
18852
18853/*
18854 * Call a user function.
18855 */
18856 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018857call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 ufunc_T *fp; /* pointer to function */
18859 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018860 typval_T *argvars; /* arguments */
18861 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862 linenr_T firstline; /* first line of range */
18863 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018864 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865{
Bram Moolenaar33570922005-01-25 22:26:29 +000018866 char_u *save_sourcing_name;
18867 linenr_T save_sourcing_lnum;
18868 scid_T save_current_SID;
18869 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018870 int save_did_emsg;
18871 static int depth = 0;
18872 dictitem_T *v;
18873 int fixvar_idx = 0; /* index in fixvar[] */
18874 int i;
18875 int ai;
18876 char_u numbuf[NUMBUFLEN];
18877 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018878#ifdef FEAT_PROFILE
18879 proftime_T wait_start;
18880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881
18882 /* If depth of calling is getting too high, don't execute the function */
18883 if (depth >= p_mfd)
18884 {
18885 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018886 rettv->v_type = VAR_NUMBER;
18887 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 return;
18889 }
18890 ++depth;
18891
18892 line_breakcheck(); /* check for CTRL-C hit */
18893
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018894 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018895 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018897 fc.rettv = rettv;
18898 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899 fc.linenr = 0;
18900 fc.returned = FALSE;
18901 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018903 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 fc.dbg_tick = debug_tick;
18905
Bram Moolenaar33570922005-01-25 22:26:29 +000018906 /*
18907 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18908 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18909 * each argument variable and saves a lot of time.
18910 */
18911 /*
18912 * Init l: variables.
18913 */
18914 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018915 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018916 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018917 /* Set l:self to "selfdict". */
18918 v = &fc.fixvar[fixvar_idx++].var;
18919 STRCPY(v->di_key, "self");
18920 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18921 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18922 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018923 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018924 v->di_tv.vval.v_dict = selfdict;
18925 ++selfdict->dv_refcount;
18926 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018927
Bram Moolenaar33570922005-01-25 22:26:29 +000018928 /*
18929 * Init a: variables.
18930 * Set a:0 to "argcount".
18931 * Set a:000 to a list with room for the "..." arguments.
18932 */
18933 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18934 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018935 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018936 v = &fc.fixvar[fixvar_idx++].var;
18937 STRCPY(v->di_key, "000");
18938 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18939 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18940 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018941 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018942 v->di_tv.vval.v_list = &fc.l_varlist;
18943 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18944 fc.l_varlist.lv_refcount = 99999;
18945
18946 /*
18947 * Set a:firstline to "firstline" and a:lastline to "lastline".
18948 * Set a:name to named arguments.
18949 * Set a:N to the "..." arguments.
18950 */
18951 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18952 (varnumber_T)firstline);
18953 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18954 (varnumber_T)lastline);
18955 for (i = 0; i < argcount; ++i)
18956 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018957 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018958 if (ai < 0)
18959 /* named argument a:name */
18960 name = FUNCARG(fp, i);
18961 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018962 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018963 /* "..." argument a:1, a:2, etc. */
18964 sprintf((char *)numbuf, "%d", ai + 1);
18965 name = numbuf;
18966 }
18967 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18968 {
18969 v = &fc.fixvar[fixvar_idx++].var;
18970 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18971 }
18972 else
18973 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018974 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18975 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018976 if (v == NULL)
18977 break;
18978 v->di_flags = DI_FLAGS_RO;
18979 }
18980 STRCPY(v->di_key, name);
18981 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18982
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018983 /* Note: the values are copied directly to avoid alloc/free.
18984 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018985 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018986 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018987
18988 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18989 {
18990 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18991 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018992 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018993 }
18994 }
18995
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 /* Don't redraw while executing the function. */
18997 ++RedrawingDisabled;
18998 save_sourcing_name = sourcing_name;
18999 save_sourcing_lnum = sourcing_lnum;
19000 sourcing_lnum = 1;
19001 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019002 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 if (sourcing_name != NULL)
19004 {
19005 if (save_sourcing_name != NULL
19006 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19007 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19008 else
19009 STRCPY(sourcing_name, "function ");
19010 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19011
19012 if (p_verbose >= 12)
19013 {
19014 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019015 verbose_enter_scroll();
19016
Bram Moolenaar555b2802005-05-19 21:08:39 +000019017 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 if (p_verbose >= 14)
19019 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019021 char_u numbuf[NUMBUFLEN];
19022 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023
19024 msg_puts((char_u *)"(");
19025 for (i = 0; i < argcount; ++i)
19026 {
19027 if (i > 0)
19028 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019029 if (argvars[i].v_type == VAR_NUMBER)
19030 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031 else
19032 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019033 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019034 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019036 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037 }
19038 }
19039 msg_puts((char_u *)")");
19040 }
19041 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019042
19043 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 --no_wait_return;
19045 }
19046 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019047#ifdef FEAT_PROFILE
19048 if (do_profiling)
19049 {
19050 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19051 func_do_profile(fp);
19052 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019053 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019054 {
19055 ++fp->uf_tm_count;
19056 profile_start(&fp->uf_tm_start);
19057 profile_zero(&fp->uf_tm_children);
19058 }
19059 script_prof_save(&wait_start);
19060 }
19061#endif
19062
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019064 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065 save_did_emsg = did_emsg;
19066 did_emsg = FALSE;
19067
19068 /* call do_cmdline() to execute the lines */
19069 do_cmdline(NULL, get_func_line, (void *)&fc,
19070 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19071
19072 --RedrawingDisabled;
19073
19074 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019075 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019077 clear_tv(rettv);
19078 rettv->v_type = VAR_NUMBER;
19079 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 }
19081
Bram Moolenaar05159a02005-02-26 23:04:13 +000019082#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019083 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019084 {
19085 profile_end(&fp->uf_tm_start);
19086 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19087 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
19088 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
19089 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019090 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019091 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019092 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19093 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019094 }
19095 }
19096#endif
19097
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 /* when being verbose, mention the return value */
19099 if (p_verbose >= 12)
19100 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019102 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019105 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019106 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019107 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19108 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019109 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019111 char_u buf[MSG_BUF_LEN];
19112 char_u numbuf[NUMBUFLEN];
19113 char_u *tofree;
19114
Bram Moolenaar555b2802005-05-19 21:08:39 +000019115 /* The value may be very long. Skip the middle part, so that we
19116 * have some idea how it starts and ends. smsg() would always
19117 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019118 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019119 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019120 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019121 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 }
19123 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019124
19125 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126 --no_wait_return;
19127 }
19128
19129 vim_free(sourcing_name);
19130 sourcing_name = save_sourcing_name;
19131 sourcing_lnum = save_sourcing_lnum;
19132 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019133#ifdef FEAT_PROFILE
19134 if (do_profiling)
19135 script_prof_restore(&wait_start);
19136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137
19138 if (p_verbose >= 12 && sourcing_name != NULL)
19139 {
19140 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019141 verbose_enter_scroll();
19142
Bram Moolenaar555b2802005-05-19 21:08:39 +000019143 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019145
19146 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 --no_wait_return;
19148 }
19149
19150 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019151 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152
Bram Moolenaar33570922005-01-25 22:26:29 +000019153 /* The a: variables typevals were not alloced, only free the allocated
19154 * variables. */
19155 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19156
19157 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 --depth;
19159}
19160
19161/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019162 * Add a number variable "name" to dict "dp" with value "nr".
19163 */
19164 static void
19165add_nr_var(dp, v, name, nr)
19166 dict_T *dp;
19167 dictitem_T *v;
19168 char *name;
19169 varnumber_T nr;
19170{
19171 STRCPY(v->di_key, name);
19172 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19173 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19174 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019175 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019176 v->di_tv.vval.v_number = nr;
19177}
19178
19179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180 * ":return [expr]"
19181 */
19182 void
19183ex_return(eap)
19184 exarg_T *eap;
19185{
19186 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019187 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188 int returning = FALSE;
19189
19190 if (current_funccal == NULL)
19191 {
19192 EMSG(_("E133: :return not inside a function"));
19193 return;
19194 }
19195
19196 if (eap->skip)
19197 ++emsg_skip;
19198
19199 eap->nextcmd = NULL;
19200 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019201 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202 {
19203 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019204 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019206 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 }
19208 /* It's safer to return also on error. */
19209 else if (!eap->skip)
19210 {
19211 /*
19212 * Return unless the expression evaluation has been cancelled due to an
19213 * aborting error, an interrupt, or an exception.
19214 */
19215 if (!aborting())
19216 returning = do_return(eap, FALSE, TRUE, NULL);
19217 }
19218
19219 /* When skipping or the return gets pending, advance to the next command
19220 * in this line (!returning). Otherwise, ignore the rest of the line.
19221 * Following lines will be ignored by get_func_line(). */
19222 if (returning)
19223 eap->nextcmd = NULL;
19224 else if (eap->nextcmd == NULL) /* no argument */
19225 eap->nextcmd = check_nextcmd(arg);
19226
19227 if (eap->skip)
19228 --emsg_skip;
19229}
19230
19231/*
19232 * Return from a function. Possibly makes the return pending. Also called
19233 * for a pending return at the ":endtry" or after returning from an extra
19234 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019235 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019236 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237 * FALSE when the return gets pending.
19238 */
19239 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019240do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241 exarg_T *eap;
19242 int reanimate;
19243 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019244 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245{
19246 int idx;
19247 struct condstack *cstack = eap->cstack;
19248
19249 if (reanimate)
19250 /* Undo the return. */
19251 current_funccal->returned = FALSE;
19252
19253 /*
19254 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19255 * not in its finally clause (which then is to be executed next) is found.
19256 * In this case, make the ":return" pending for execution at the ":endtry".
19257 * Otherwise, return normally.
19258 */
19259 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19260 if (idx >= 0)
19261 {
19262 cstack->cs_pending[idx] = CSTP_RETURN;
19263
19264 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019265 /* A pending return again gets pending. "rettv" points to an
19266 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019267 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019268 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269 else
19270 {
19271 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019272 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019274 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019276 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277 {
19278 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019279 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019280 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281 else
19282 EMSG(_(e_outofmem));
19283 }
19284 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019285 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286
19287 if (reanimate)
19288 {
19289 /* The pending return value could be overwritten by a ":return"
19290 * without argument in a finally clause; reset the default
19291 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019292 current_funccal->rettv->v_type = VAR_NUMBER;
19293 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294 }
19295 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019296 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019297 }
19298 else
19299 {
19300 current_funccal->returned = TRUE;
19301
19302 /* If the return is carried out now, store the return value. For
19303 * a return immediately after reanimation, the value is already
19304 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019305 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019307 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019308 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019310 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 }
19312 }
19313
19314 return idx < 0;
19315}
19316
19317/*
19318 * Free the variable with a pending return value.
19319 */
19320 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019321discard_pending_return(rettv)
19322 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323{
Bram Moolenaar33570922005-01-25 22:26:29 +000019324 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325}
19326
19327/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019328 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 * is an allocated string. Used by report_pending() for verbose messages.
19330 */
19331 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019332get_return_cmd(rettv)
19333 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019334{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019335 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019336 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019337 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019339 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019340 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019341 if (s == NULL)
19342 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019343
19344 STRCPY(IObuff, ":return ");
19345 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19346 if (STRLEN(s) + 8 >= IOSIZE)
19347 STRCPY(IObuff + IOSIZE - 4, "...");
19348 vim_free(tofree);
19349 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350}
19351
19352/*
19353 * Get next function line.
19354 * Called by do_cmdline() to get the next line.
19355 * Returns allocated string, or NULL for end of function.
19356 */
19357/* ARGSUSED */
19358 char_u *
19359get_func_line(c, cookie, indent)
19360 int c; /* not used */
19361 void *cookie;
19362 int indent; /* not used */
19363{
Bram Moolenaar33570922005-01-25 22:26:29 +000019364 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019365 ufunc_T *fp = fcp->func;
19366 char_u *retval;
19367 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368
19369 /* If breakpoints have been added/deleted need to check for it. */
19370 if (fcp->dbg_tick != debug_tick)
19371 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019372 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373 sourcing_lnum);
19374 fcp->dbg_tick = debug_tick;
19375 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019376#ifdef FEAT_PROFILE
19377 if (do_profiling)
19378 func_line_end(cookie);
19379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380
Bram Moolenaar05159a02005-02-26 23:04:13 +000019381 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019382 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
19383 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384 retval = NULL;
19385 else
19386 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019387 /* Skip NULL lines (continuation lines). */
19388 while (fcp->linenr < gap->ga_len
19389 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
19390 ++fcp->linenr;
19391 if (fcp->linenr >= gap->ga_len)
19392 retval = NULL;
19393 else
19394 {
19395 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19396 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019397#ifdef FEAT_PROFILE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019398 if (do_profiling)
19399 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019400#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402 }
19403
19404 /* Did we encounter a breakpoint? */
19405 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19406 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019407 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019409 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410 sourcing_lnum);
19411 fcp->dbg_tick = debug_tick;
19412 }
19413
19414 return retval;
19415}
19416
Bram Moolenaar05159a02005-02-26 23:04:13 +000019417#if defined(FEAT_PROFILE) || defined(PROTO)
19418/*
19419 * Called when starting to read a function line.
19420 * "sourcing_lnum" must be correct!
19421 * When skipping lines it may not actually be executed, but we won't find out
19422 * until later and we need to store the time now.
19423 */
19424 void
19425func_line_start(cookie)
19426 void *cookie;
19427{
19428 funccall_T *fcp = (funccall_T *)cookie;
19429 ufunc_T *fp = fcp->func;
19430
19431 if (fp->uf_profiling && sourcing_lnum >= 1
19432 && sourcing_lnum <= fp->uf_lines.ga_len)
19433 {
19434 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019435 /* Skip continuation lines. */
19436 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
19437 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019438 fp->uf_tml_execed = FALSE;
19439 profile_start(&fp->uf_tml_start);
19440 profile_zero(&fp->uf_tml_children);
19441 profile_get_wait(&fp->uf_tml_wait);
19442 }
19443}
19444
19445/*
19446 * Called when actually executing a function line.
19447 */
19448 void
19449func_line_exec(cookie)
19450 void *cookie;
19451{
19452 funccall_T *fcp = (funccall_T *)cookie;
19453 ufunc_T *fp = fcp->func;
19454
19455 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19456 fp->uf_tml_execed = TRUE;
19457}
19458
19459/*
19460 * Called when done with a function line.
19461 */
19462 void
19463func_line_end(cookie)
19464 void *cookie;
19465{
19466 funccall_T *fcp = (funccall_T *)cookie;
19467 ufunc_T *fp = fcp->func;
19468
19469 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19470 {
19471 if (fp->uf_tml_execed)
19472 {
19473 ++fp->uf_tml_count[fp->uf_tml_idx];
19474 profile_end(&fp->uf_tml_start);
19475 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
19476 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
19477 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
19478 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
19479 }
19480 fp->uf_tml_idx = -1;
19481 }
19482}
19483#endif
19484
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485/*
19486 * Return TRUE if the currently active function should be ended, because a
19487 * return was encountered or an error occured. Used inside a ":while".
19488 */
19489 int
19490func_has_ended(cookie)
19491 void *cookie;
19492{
Bram Moolenaar33570922005-01-25 22:26:29 +000019493 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494
19495 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19496 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019497 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498 || fcp->returned);
19499}
19500
19501/*
19502 * return TRUE if cookie indicates a function which "abort"s on errors.
19503 */
19504 int
19505func_has_abort(cookie)
19506 void *cookie;
19507{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019508 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509}
19510
19511#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19512typedef enum
19513{
19514 VAR_FLAVOUR_DEFAULT,
19515 VAR_FLAVOUR_SESSION,
19516 VAR_FLAVOUR_VIMINFO
19517} var_flavour_T;
19518
19519static var_flavour_T var_flavour __ARGS((char_u *varname));
19520
19521 static var_flavour_T
19522var_flavour(varname)
19523 char_u *varname;
19524{
19525 char_u *p = varname;
19526
19527 if (ASCII_ISUPPER(*p))
19528 {
19529 while (*(++p))
19530 if (ASCII_ISLOWER(*p))
19531 return VAR_FLAVOUR_SESSION;
19532 return VAR_FLAVOUR_VIMINFO;
19533 }
19534 else
19535 return VAR_FLAVOUR_DEFAULT;
19536}
19537#endif
19538
19539#if defined(FEAT_VIMINFO) || defined(PROTO)
19540/*
19541 * Restore global vars that start with a capital from the viminfo file
19542 */
19543 int
19544read_viminfo_varlist(virp, writing)
19545 vir_T *virp;
19546 int writing;
19547{
19548 char_u *tab;
19549 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019550 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551
19552 if (!writing && (find_viminfo_parameter('!') != NULL))
19553 {
19554 tab = vim_strchr(virp->vir_line + 1, '\t');
19555 if (tab != NULL)
19556 {
19557 *tab++ = '\0'; /* isolate the variable name */
19558 if (*tab == 'S') /* string var */
19559 is_string = TRUE;
19560
19561 tab = vim_strchr(tab, '\t');
19562 if (tab != NULL)
19563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564 if (is_string)
19565 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019566 tv.v_type = VAR_STRING;
19567 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569 }
19570 else
19571 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019572 tv.v_type = VAR_NUMBER;
19573 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019575 set_var(virp->vir_line + 1, &tv, FALSE);
19576 if (is_string)
19577 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019578 }
19579 }
19580 }
19581
19582 return viminfo_readline(virp);
19583}
19584
19585/*
19586 * Write global vars that start with a capital to the viminfo file
19587 */
19588 void
19589write_viminfo_varlist(fp)
19590 FILE *fp;
19591{
Bram Moolenaar33570922005-01-25 22:26:29 +000019592 hashitem_T *hi;
19593 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019594 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019595 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019596 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019597 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019598 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599
19600 if (find_viminfo_parameter('!') == NULL)
19601 return;
19602
19603 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019604
Bram Moolenaar33570922005-01-25 22:26:29 +000019605 todo = globvarht.ht_used;
19606 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019608 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019610 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019611 this_var = HI2DI(hi);
19612 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019613 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019614 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019615 {
19616 case VAR_STRING: s = "STR"; break;
19617 case VAR_NUMBER: s = "NUM"; break;
19618 default: continue;
19619 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019620 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019621 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019622 if (p != NULL)
19623 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019624 vim_free(tofree);
19625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 }
19627 }
19628}
19629#endif
19630
19631#if defined(FEAT_SESSION) || defined(PROTO)
19632 int
19633store_session_globals(fd)
19634 FILE *fd;
19635{
Bram Moolenaar33570922005-01-25 22:26:29 +000019636 hashitem_T *hi;
19637 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019638 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 char_u *p, *t;
19640
Bram Moolenaar33570922005-01-25 22:26:29 +000019641 todo = globvarht.ht_used;
19642 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019644 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019646 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019647 this_var = HI2DI(hi);
19648 if ((this_var->di_tv.v_type == VAR_NUMBER
19649 || this_var->di_tv.v_type == VAR_STRING)
19650 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019651 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019652 /* Escape special characters with a backslash. Turn a LF and
19653 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019654 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019655 (char_u *)"\\\"\n\r");
19656 if (p == NULL) /* out of memory */
19657 break;
19658 for (t = p; *t != NUL; ++t)
19659 if (*t == '\n')
19660 *t = 'n';
19661 else if (*t == '\r')
19662 *t = 'r';
19663 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019664 this_var->di_key,
19665 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19666 : ' ',
19667 p,
19668 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19669 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019670 || put_eol(fd) == FAIL)
19671 {
19672 vim_free(p);
19673 return FAIL;
19674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675 vim_free(p);
19676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677 }
19678 }
19679 return OK;
19680}
19681#endif
19682
Bram Moolenaar661b1822005-07-28 22:36:45 +000019683/*
19684 * Display script name where an item was last set.
19685 * Should only be invoked when 'verbose' is non-zero.
19686 */
19687 void
19688last_set_msg(scriptID)
19689 scid_T scriptID;
19690{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019691 char_u *p;
19692
Bram Moolenaar661b1822005-07-28 22:36:45 +000019693 if (scriptID != 0)
19694 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019695 p = home_replace_save(NULL, get_scriptname(scriptID));
19696 if (p != NULL)
19697 {
19698 verbose_enter();
19699 MSG_PUTS(_("\n\tLast set from "));
19700 MSG_PUTS(p);
19701 vim_free(p);
19702 verbose_leave();
19703 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019704 }
19705}
19706
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707#endif /* FEAT_EVAL */
19708
19709#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19710
19711
19712#ifdef WIN3264
19713/*
19714 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19715 */
19716static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19717static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19718static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19719
19720/*
19721 * Get the short pathname of a file.
19722 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19723 */
19724 static int
19725get_short_pathname(fnamep, bufp, fnamelen)
19726 char_u **fnamep;
19727 char_u **bufp;
19728 int *fnamelen;
19729{
19730 int l,len;
19731 char_u *newbuf;
19732
19733 len = *fnamelen;
19734
19735 l = GetShortPathName(*fnamep, *fnamep, len);
19736 if (l > len - 1)
19737 {
19738 /* If that doesn't work (not enough space), then save the string
19739 * and try again with a new buffer big enough
19740 */
19741 newbuf = vim_strnsave(*fnamep, l);
19742 if (newbuf == NULL)
19743 return 0;
19744
19745 vim_free(*bufp);
19746 *fnamep = *bufp = newbuf;
19747
19748 l = GetShortPathName(*fnamep,*fnamep,l+1);
19749
19750 /* Really should always succeed, as the buffer is big enough */
19751 }
19752
19753 *fnamelen = l;
19754 return 1;
19755}
19756
19757/*
19758 * Create a short path name. Returns the length of the buffer it needs.
19759 * Doesn't copy over the end of the buffer passed in.
19760 */
19761 static int
19762shortpath_for_invalid_fname(fname, bufp, fnamelen)
19763 char_u **fname;
19764 char_u **bufp;
19765 int *fnamelen;
19766{
19767 char_u *s, *p, *pbuf2, *pbuf3;
19768 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019769 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770
19771 /* Make a copy */
19772 len2 = *fnamelen;
19773 pbuf2 = vim_strnsave(*fname, len2);
19774 pbuf3 = NULL;
19775
19776 s = pbuf2 + len2 - 1; /* Find the end */
19777 slen = 1;
19778 plen = len2;
19779
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019780 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781 {
19782 --s;
19783 ++slen;
19784 --plen;
19785 }
19786
19787 do
19788 {
19789 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019790 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 {
19792 --s;
19793 ++slen;
19794 --plen;
19795 }
19796 if (s <= pbuf2)
19797 break;
19798
19799 /* Remeber the character that is about to be blatted */
19800 ch = *s;
19801 *s = 0; /* get_short_pathname requires a null-terminated string */
19802
19803 /* Try it in situ */
19804 p = pbuf2;
19805 if (!get_short_pathname(&p, &pbuf3, &plen))
19806 {
19807 vim_free(pbuf2);
19808 return -1;
19809 }
19810 *s = ch; /* Preserve the string */
19811 } while (plen == 0);
19812
19813 if (plen > 0)
19814 {
19815 /* Remeber the length of the new string. */
19816 *fnamelen = len = plen + slen;
19817 vim_free(*bufp);
19818 if (len > len2)
19819 {
19820 /* If there's not enough space in the currently allocated string,
19821 * then copy it to a buffer big enough.
19822 */
19823 *fname= *bufp = vim_strnsave(p, len);
19824 if (*fname == NULL)
19825 return -1;
19826 }
19827 else
19828 {
19829 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19830 *fname = *bufp = pbuf2;
19831 if (p != pbuf2)
19832 strncpy(*fname, p, plen);
19833 pbuf2 = NULL;
19834 }
19835 /* Concat the next bit */
19836 strncpy(*fname + plen, s, slen);
19837 (*fname)[len] = '\0';
19838 }
19839 vim_free(pbuf3);
19840 vim_free(pbuf2);
19841 return 0;
19842}
19843
19844/*
19845 * Get a pathname for a partial path.
19846 */
19847 static int
19848shortpath_for_partial(fnamep, bufp, fnamelen)
19849 char_u **fnamep;
19850 char_u **bufp;
19851 int *fnamelen;
19852{
19853 int sepcount, len, tflen;
19854 char_u *p;
19855 char_u *pbuf, *tfname;
19856 int hasTilde;
19857
19858 /* Count up the path seperators from the RHS.. so we know which part
19859 * of the path to return.
19860 */
19861 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019862 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863 if (vim_ispathsep(*p))
19864 ++sepcount;
19865
19866 /* Need full path first (use expand_env() to remove a "~/") */
19867 hasTilde = (**fnamep == '~');
19868 if (hasTilde)
19869 pbuf = tfname = expand_env_save(*fnamep);
19870 else
19871 pbuf = tfname = FullName_save(*fnamep, FALSE);
19872
19873 len = tflen = STRLEN(tfname);
19874
19875 if (!get_short_pathname(&tfname, &pbuf, &len))
19876 return -1;
19877
19878 if (len == 0)
19879 {
19880 /* Don't have a valid filename, so shorten the rest of the
19881 * path if we can. This CAN give us invalid 8.3 filenames, but
19882 * there's not a lot of point in guessing what it might be.
19883 */
19884 len = tflen;
19885 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19886 return -1;
19887 }
19888
19889 /* Count the paths backward to find the beginning of the desired string. */
19890 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019891 {
19892#ifdef FEAT_MBYTE
19893 if (has_mbyte)
19894 p -= mb_head_off(tfname, p);
19895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896 if (vim_ispathsep(*p))
19897 {
19898 if (sepcount == 0 || (hasTilde && sepcount == 1))
19899 break;
19900 else
19901 sepcount --;
19902 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 if (hasTilde)
19905 {
19906 --p;
19907 if (p >= tfname)
19908 *p = '~';
19909 else
19910 return -1;
19911 }
19912 else
19913 ++p;
19914
19915 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19916 vim_free(*bufp);
19917 *fnamelen = (int)STRLEN(p);
19918 *bufp = pbuf;
19919 *fnamep = p;
19920
19921 return 0;
19922}
19923#endif /* WIN3264 */
19924
19925/*
19926 * Adjust a filename, according to a string of modifiers.
19927 * *fnamep must be NUL terminated when called. When returning, the length is
19928 * determined by *fnamelen.
19929 * Returns valid flags.
19930 * When there is an error, *fnamep is set to NULL.
19931 */
19932 int
19933modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19934 char_u *src; /* string with modifiers */
19935 int *usedlen; /* characters after src that are used */
19936 char_u **fnamep; /* file name so far */
19937 char_u **bufp; /* buffer for allocated file name or NULL */
19938 int *fnamelen; /* length of fnamep */
19939{
19940 int valid = 0;
19941 char_u *tail;
19942 char_u *s, *p, *pbuf;
19943 char_u dirname[MAXPATHL];
19944 int c;
19945 int has_fullname = 0;
19946#ifdef WIN3264
19947 int has_shortname = 0;
19948#endif
19949
19950repeat:
19951 /* ":p" - full path/file_name */
19952 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19953 {
19954 has_fullname = 1;
19955
19956 valid |= VALID_PATH;
19957 *usedlen += 2;
19958
19959 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19960 if ((*fnamep)[0] == '~'
19961#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19962 && ((*fnamep)[1] == '/'
19963# ifdef BACKSLASH_IN_FILENAME
19964 || (*fnamep)[1] == '\\'
19965# endif
19966 || (*fnamep)[1] == NUL)
19967
19968#endif
19969 )
19970 {
19971 *fnamep = expand_env_save(*fnamep);
19972 vim_free(*bufp); /* free any allocated file name */
19973 *bufp = *fnamep;
19974 if (*fnamep == NULL)
19975 return -1;
19976 }
19977
19978 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019979 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 {
19981 if (vim_ispathsep(*p)
19982 && p[1] == '.'
19983 && (p[2] == NUL
19984 || vim_ispathsep(p[2])
19985 || (p[2] == '.'
19986 && (p[3] == NUL || vim_ispathsep(p[3])))))
19987 break;
19988 }
19989
19990 /* FullName_save() is slow, don't use it when not needed. */
19991 if (*p != NUL || !vim_isAbsName(*fnamep))
19992 {
19993 *fnamep = FullName_save(*fnamep, *p != NUL);
19994 vim_free(*bufp); /* free any allocated file name */
19995 *bufp = *fnamep;
19996 if (*fnamep == NULL)
19997 return -1;
19998 }
19999
20000 /* Append a path separator to a directory. */
20001 if (mch_isdir(*fnamep))
20002 {
20003 /* Make room for one or two extra characters. */
20004 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20005 vim_free(*bufp); /* free any allocated file name */
20006 *bufp = *fnamep;
20007 if (*fnamep == NULL)
20008 return -1;
20009 add_pathsep(*fnamep);
20010 }
20011 }
20012
20013 /* ":." - path relative to the current directory */
20014 /* ":~" - path relative to the home directory */
20015 /* ":8" - shortname path - postponed till after */
20016 while (src[*usedlen] == ':'
20017 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20018 {
20019 *usedlen += 2;
20020 if (c == '8')
20021 {
20022#ifdef WIN3264
20023 has_shortname = 1; /* Postpone this. */
20024#endif
20025 continue;
20026 }
20027 pbuf = NULL;
20028 /* Need full path first (use expand_env() to remove a "~/") */
20029 if (!has_fullname)
20030 {
20031 if (c == '.' && **fnamep == '~')
20032 p = pbuf = expand_env_save(*fnamep);
20033 else
20034 p = pbuf = FullName_save(*fnamep, FALSE);
20035 }
20036 else
20037 p = *fnamep;
20038
20039 has_fullname = 0;
20040
20041 if (p != NULL)
20042 {
20043 if (c == '.')
20044 {
20045 mch_dirname(dirname, MAXPATHL);
20046 s = shorten_fname(p, dirname);
20047 if (s != NULL)
20048 {
20049 *fnamep = s;
20050 if (pbuf != NULL)
20051 {
20052 vim_free(*bufp); /* free any allocated file name */
20053 *bufp = pbuf;
20054 pbuf = NULL;
20055 }
20056 }
20057 }
20058 else
20059 {
20060 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20061 /* Only replace it when it starts with '~' */
20062 if (*dirname == '~')
20063 {
20064 s = vim_strsave(dirname);
20065 if (s != NULL)
20066 {
20067 *fnamep = s;
20068 vim_free(*bufp);
20069 *bufp = s;
20070 }
20071 }
20072 }
20073 vim_free(pbuf);
20074 }
20075 }
20076
20077 tail = gettail(*fnamep);
20078 *fnamelen = (int)STRLEN(*fnamep);
20079
20080 /* ":h" - head, remove "/file_name", can be repeated */
20081 /* Don't remove the first "/" or "c:\" */
20082 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20083 {
20084 valid |= VALID_HEAD;
20085 *usedlen += 2;
20086 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020087 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088 --tail;
20089 *fnamelen = (int)(tail - *fnamep);
20090#ifdef VMS
20091 if (*fnamelen > 0)
20092 *fnamelen += 1; /* the path separator is part of the path */
20093#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020094 while (tail > s && !after_pathsep(s, tail))
20095 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 }
20097
20098 /* ":8" - shortname */
20099 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20100 {
20101 *usedlen += 2;
20102#ifdef WIN3264
20103 has_shortname = 1;
20104#endif
20105 }
20106
20107#ifdef WIN3264
20108 /* Check shortname after we have done 'heads' and before we do 'tails'
20109 */
20110 if (has_shortname)
20111 {
20112 pbuf = NULL;
20113 /* Copy the string if it is shortened by :h */
20114 if (*fnamelen < (int)STRLEN(*fnamep))
20115 {
20116 p = vim_strnsave(*fnamep, *fnamelen);
20117 if (p == 0)
20118 return -1;
20119 vim_free(*bufp);
20120 *bufp = *fnamep = p;
20121 }
20122
20123 /* Split into two implementations - makes it easier. First is where
20124 * there isn't a full name already, second is where there is.
20125 */
20126 if (!has_fullname && !vim_isAbsName(*fnamep))
20127 {
20128 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20129 return -1;
20130 }
20131 else
20132 {
20133 int l;
20134
20135 /* Simple case, already have the full-name
20136 * Nearly always shorter, so try first time. */
20137 l = *fnamelen;
20138 if (!get_short_pathname(fnamep, bufp, &l))
20139 return -1;
20140
20141 if (l == 0)
20142 {
20143 /* Couldn't find the filename.. search the paths.
20144 */
20145 l = *fnamelen;
20146 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20147 return -1;
20148 }
20149 *fnamelen = l;
20150 }
20151 }
20152#endif /* WIN3264 */
20153
20154 /* ":t" - tail, just the basename */
20155 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20156 {
20157 *usedlen += 2;
20158 *fnamelen -= (int)(tail - *fnamep);
20159 *fnamep = tail;
20160 }
20161
20162 /* ":e" - extension, can be repeated */
20163 /* ":r" - root, without extension, can be repeated */
20164 while (src[*usedlen] == ':'
20165 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20166 {
20167 /* find a '.' in the tail:
20168 * - for second :e: before the current fname
20169 * - otherwise: The last '.'
20170 */
20171 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20172 s = *fnamep - 2;
20173 else
20174 s = *fnamep + *fnamelen - 1;
20175 for ( ; s > tail; --s)
20176 if (s[0] == '.')
20177 break;
20178 if (src[*usedlen + 1] == 'e') /* :e */
20179 {
20180 if (s > tail)
20181 {
20182 *fnamelen += (int)(*fnamep - (s + 1));
20183 *fnamep = s + 1;
20184#ifdef VMS
20185 /* cut version from the extension */
20186 s = *fnamep + *fnamelen - 1;
20187 for ( ; s > *fnamep; --s)
20188 if (s[0] == ';')
20189 break;
20190 if (s > *fnamep)
20191 *fnamelen = s - *fnamep;
20192#endif
20193 }
20194 else if (*fnamep <= tail)
20195 *fnamelen = 0;
20196 }
20197 else /* :r */
20198 {
20199 if (s > tail) /* remove one extension */
20200 *fnamelen = (int)(s - *fnamep);
20201 }
20202 *usedlen += 2;
20203 }
20204
20205 /* ":s?pat?foo?" - substitute */
20206 /* ":gs?pat?foo?" - global substitute */
20207 if (src[*usedlen] == ':'
20208 && (src[*usedlen + 1] == 's'
20209 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20210 {
20211 char_u *str;
20212 char_u *pat;
20213 char_u *sub;
20214 int sep;
20215 char_u *flags;
20216 int didit = FALSE;
20217
20218 flags = (char_u *)"";
20219 s = src + *usedlen + 2;
20220 if (src[*usedlen + 1] == 'g')
20221 {
20222 flags = (char_u *)"g";
20223 ++s;
20224 }
20225
20226 sep = *s++;
20227 if (sep)
20228 {
20229 /* find end of pattern */
20230 p = vim_strchr(s, sep);
20231 if (p != NULL)
20232 {
20233 pat = vim_strnsave(s, (int)(p - s));
20234 if (pat != NULL)
20235 {
20236 s = p + 1;
20237 /* find end of substitution */
20238 p = vim_strchr(s, sep);
20239 if (p != NULL)
20240 {
20241 sub = vim_strnsave(s, (int)(p - s));
20242 str = vim_strnsave(*fnamep, *fnamelen);
20243 if (sub != NULL && str != NULL)
20244 {
20245 *usedlen = (int)(p + 1 - src);
20246 s = do_string_sub(str, pat, sub, flags);
20247 if (s != NULL)
20248 {
20249 *fnamep = s;
20250 *fnamelen = (int)STRLEN(s);
20251 vim_free(*bufp);
20252 *bufp = s;
20253 didit = TRUE;
20254 }
20255 }
20256 vim_free(sub);
20257 vim_free(str);
20258 }
20259 vim_free(pat);
20260 }
20261 }
20262 /* after using ":s", repeat all the modifiers */
20263 if (didit)
20264 goto repeat;
20265 }
20266 }
20267
20268 return valid;
20269}
20270
20271/*
20272 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20273 * "flags" can be "g" to do a global substitute.
20274 * Returns an allocated string, NULL for error.
20275 */
20276 char_u *
20277do_string_sub(str, pat, sub, flags)
20278 char_u *str;
20279 char_u *pat;
20280 char_u *sub;
20281 char_u *flags;
20282{
20283 int sublen;
20284 regmatch_T regmatch;
20285 int i;
20286 int do_all;
20287 char_u *tail;
20288 garray_T ga;
20289 char_u *ret;
20290 char_u *save_cpo;
20291
20292 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20293 save_cpo = p_cpo;
20294 p_cpo = (char_u *)"";
20295
20296 ga_init2(&ga, 1, 200);
20297
20298 do_all = (flags[0] == 'g');
20299
20300 regmatch.rm_ic = p_ic;
20301 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20302 if (regmatch.regprog != NULL)
20303 {
20304 tail = str;
20305 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20306 {
20307 /*
20308 * Get some space for a temporary buffer to do the substitution
20309 * into. It will contain:
20310 * - The text up to where the match is.
20311 * - The substituted text.
20312 * - The text after the match.
20313 */
20314 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20315 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20316 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20317 {
20318 ga_clear(&ga);
20319 break;
20320 }
20321
20322 /* copy the text up to where the match is */
20323 i = (int)(regmatch.startp[0] - tail);
20324 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20325 /* add the substituted text */
20326 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20327 + ga.ga_len + i, TRUE, TRUE, FALSE);
20328 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 /* avoid getting stuck on a match with an empty string */
20330 if (tail == regmatch.endp[0])
20331 {
20332 if (*tail == NUL)
20333 break;
20334 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20335 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336 }
20337 else
20338 {
20339 tail = regmatch.endp[0];
20340 if (*tail == NUL)
20341 break;
20342 }
20343 if (!do_all)
20344 break;
20345 }
20346
20347 if (ga.ga_data != NULL)
20348 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20349
20350 vim_free(regmatch.regprog);
20351 }
20352
20353 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20354 ga_clear(&ga);
20355 p_cpo = save_cpo;
20356
20357 return ret;
20358}
20359
20360#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */