blob: c44aae048930c40802c42ae06588e5e70c58dc30 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000344};
345
346/* shorthand */
347#define vv_type vv_di.di_tv.v_type
348#define vv_nr vv_di.di_tv.vval.v_number
349#define vv_str vv_di.di_tv.vval.v_string
350#define vv_tv vv_di.di_tv
351
352/*
353 * The v: variables are stored in dictionary "vimvardict".
354 * "vimvars_var" is the variable that is used for the "l:" scope.
355 */
356static dict_T vimvardict;
357static dictitem_T vimvars_var;
358#define vimvarht vimvardict.dv_hashtab
359
Bram Moolenaara40058a2005-07-11 22:42:07 +0000360static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
361static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
362#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
363static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
364#endif
365static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
366static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
367static char_u *skip_var_one __ARGS((char_u *arg));
368static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
369static void list_glob_vars __ARGS((void));
370static void list_buf_vars __ARGS((void));
371static void list_win_vars __ARGS((void));
372static void list_vim_vars __ARGS((void));
373static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
374static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
375static int check_changedtick __ARGS((char_u *arg));
376static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
377static void clear_lval __ARGS((lval_T *lp));
378static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
379static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
380static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
381static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
382static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
383static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
384static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
385static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
386static void item_lock __ARGS((typval_T *tv, int deep, int lock));
387static int tv_islocked __ARGS((typval_T *tv));
388
Bram Moolenaar33570922005-01-25 22:26:29 +0000389static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
390static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000398static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000403static listitem_T *listitem_alloc __ARGS((void));
404static void listitem_free __ARGS((listitem_T *item));
405static void listitem_remove __ARGS((list_T *l, listitem_T *item));
406static long list_len __ARGS((list_T *l));
407static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
408static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
409static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static listitem_T *list_find __ARGS((list_T *l, long n));
411static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static void list_append __ARGS((list_T *l, listitem_T *item));
413static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000414static int list_append_string __ARGS((list_T *l, char_u *str, int len));
415static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
417static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
418static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000419static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000421static char_u *list2string __ARGS((typval_T *tv, int copyID));
422static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000423static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
424static void set_ref_in_list __ARGS((list_T *l, int copyID));
425static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static void dict_unref __ARGS((dict_T *d));
427static void dict_free __ARGS((dict_T *d));
428static dictitem_T *dictitem_alloc __ARGS((char_u *key));
429static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
430static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
431static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000432static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static int dict_add __ARGS((dict_T *d, dictitem_T *item));
434static long dict_len __ARGS((dict_T *d));
435static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
439static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static char_u *string_quote __ARGS((char_u *str, int function));
441static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
442static int find_internal_func __ARGS((char_u *name));
443static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
444static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
445static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447
448static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000467#if defined(FEAT_INS_EXPAND)
468static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
476static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000502static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000504static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000510static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000518static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000541static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000547static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000564static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000568#ifdef vim_mkdir
569static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
570#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000571static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000575static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000577static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000589static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000596static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000597static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000598static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000602static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000603static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
606#ifdef HAVE_STRFTIME
607static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
608#endif
609static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000621static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000622static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000624static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000639static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
642static int get_env_len __ARGS((char_u **arg));
643static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000644static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000645static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
646#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
647#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
648 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000649static 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 +0000650static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000651static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000652static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
653static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static typval_T *alloc_tv __ARGS((void));
655static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void init_tv __ARGS((typval_T *varp));
657static long get_tv_number __ARGS((typval_T *varp));
658static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000659static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static char_u *get_tv_string __ARGS((typval_T *varp));
661static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000662static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000664static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
666static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
667static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
668static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
669static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
670static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
671static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000672static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000674static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
676static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
677static int eval_fname_script __ARGS((char_u *p));
678static int eval_fname_sid __ARGS((char_u *p));
679static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static ufunc_T *find_func __ARGS((char_u *name));
681static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000682static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000683#ifdef FEAT_PROFILE
684static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000685static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
686static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
687static int
688# ifdef __BORLANDC__
689 _RTLENTRYF
690# endif
691 prof_total_cmp __ARGS((const void *s1, const void *s2));
692static int
693# ifdef __BORLANDC__
694 _RTLENTRYF
695# endif
696 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000698static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000699static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000700static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void func_free __ARGS((ufunc_T *fp));
702static void func_unref __ARGS((char_u *name));
703static void func_ref __ARGS((char_u *name));
704static 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));
705static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000706static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000708/* Character used as separated in autoload function/variable names. */
709#define AUTOLOAD_CHAR '#'
710
Bram Moolenaar33570922005-01-25 22:26:29 +0000711/*
712 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000713 */
714 void
715eval_init()
716{
Bram Moolenaar33570922005-01-25 22:26:29 +0000717 int i;
718 struct vimvar *p;
719
720 init_var_dict(&globvardict, &globvars_var);
721 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000722 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000723 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000724
725 for (i = 0; i < VV_LEN; ++i)
726 {
727 p = &vimvars[i];
728 STRCPY(p->vv_di.di_key, p->vv_name);
729 if (p->vv_flags & VV_RO)
730 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
731 else if (p->vv_flags & VV_RO_SBX)
732 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
733 else
734 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000735
736 /* add to v: scope dict, unless the value is not always available */
737 if (p->vv_type != VAR_UNKNOWN)
738 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000739 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000740 /* add to compat scope dict */
741 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000742 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000743}
744
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000745#if defined(EXITFREE) || defined(PROTO)
746 void
747eval_clear()
748{
749 int i;
750 struct vimvar *p;
751
752 for (i = 0; i < VV_LEN; ++i)
753 {
754 p = &vimvars[i];
755 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000756 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000757 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000758 p->vv_di.di_tv.vval.v_string = NULL;
759 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000760 }
761 hash_clear(&vimvarht);
762 hash_clear(&compat_hashtab);
763
764 /* script-local variables */
765 for (i = 1; i <= ga_scripts.ga_len; ++i)
766 vars_clear(&SCRIPT_VARS(i));
767 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000768 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000769
770 /* global variables */
771 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000772
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000773 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000774 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000775 hash_clear(&func_hashtab);
776
777 /* unreferenced lists and dicts */
778 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000779}
780#endif
781
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 * Return the name of the executed function.
784 */
785 char_u *
786func_name(cookie)
787 void *cookie;
788{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000789 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790}
791
792/*
793 * Return the address holding the next breakpoint line for a funccall cookie.
794 */
795 linenr_T *
796func_breakpoint(cookie)
797 void *cookie;
798{
Bram Moolenaar33570922005-01-25 22:26:29 +0000799 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800}
801
802/*
803 * Return the address holding the debug tick for a funccall cookie.
804 */
805 int *
806func_dbg_tick(cookie)
807 void *cookie;
808{
Bram Moolenaar33570922005-01-25 22:26:29 +0000809 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810}
811
812/*
813 * Return the nesting level for a funccall cookie.
814 */
815 int
816func_level(cookie)
817 void *cookie;
818{
Bram Moolenaar33570922005-01-25 22:26:29 +0000819 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820}
821
822/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000823funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825/*
826 * Return TRUE when a function was ended by a ":return" command.
827 */
828 int
829current_func_returned()
830{
831 return current_funccal->returned;
832}
833
834
835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 * Set an internal variable to a string value. Creates the variable if it does
837 * not already exist.
838 */
839 void
840set_internal_string_var(name, value)
841 char_u *name;
842 char_u *value;
843{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000844 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000845 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
847 val = vim_strsave(value);
848 if (val != NULL)
849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000850 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000851 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000853 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000854 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
856 }
857}
858
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000859static lval_T *redir_lval = NULL;
860static char_u *redir_endp = NULL;
861static char_u *redir_varname = NULL;
862
863/*
864 * Start recording command output to a variable
865 * Returns OK if successfully completed the setup. FAIL otherwise.
866 */
867 int
868var_redir_start(name, append)
869 char_u *name;
870 int append; /* append to an existing variable */
871{
872 int save_emsg;
873 int err;
874 typval_T tv;
875
876 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000877 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000878 {
879 EMSG(_(e_invarg));
880 return FAIL;
881 }
882
883 redir_varname = vim_strsave(name);
884 if (redir_varname == NULL)
885 return FAIL;
886
887 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
888 if (redir_lval == NULL)
889 {
890 var_redir_stop();
891 return FAIL;
892 }
893
894 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000895 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
896 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000897 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
898 {
899 if (redir_endp != NULL && *redir_endp != NUL)
900 /* Trailing characters are present after the variable name */
901 EMSG(_(e_trailing));
902 else
903 EMSG(_(e_invarg));
904 var_redir_stop();
905 return FAIL;
906 }
907
908 /* check if we can write to the variable: set it to or append an empty
909 * string */
910 save_emsg = did_emsg;
911 did_emsg = FALSE;
912 tv.v_type = VAR_STRING;
913 tv.vval.v_string = (char_u *)"";
914 if (append)
915 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
916 else
917 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
918 err = did_emsg;
919 did_emsg += save_emsg;
920 if (err)
921 {
922 var_redir_stop();
923 return FAIL;
924 }
925 if (redir_lval->ll_newkey != NULL)
926 {
927 /* Dictionary item was created, don't do it again. */
928 vim_free(redir_lval->ll_newkey);
929 redir_lval->ll_newkey = NULL;
930 }
931
932 return OK;
933}
934
935/*
936 * Append "value[len]" to the variable set by var_redir_start().
937 */
938 void
939var_redir_str(value, len)
940 char_u *value;
941 int len;
942{
943 char_u *val;
944 typval_T tv;
945 int save_emsg;
946 int err;
947
948 if (redir_lval == NULL)
949 return;
950
951 if (len == -1)
952 /* Append the entire string */
953 val = vim_strsave(value);
954 else
955 /* Append only the specified number of characters */
956 val = vim_strnsave(value, len);
957 if (val == NULL)
958 return;
959
960 tv.v_type = VAR_STRING;
961 tv.vval.v_string = val;
962
963 save_emsg = did_emsg;
964 did_emsg = FALSE;
965 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
966 err = did_emsg;
967 did_emsg += save_emsg;
968 if (err)
969 var_redir_stop();
970
971 vim_free(tv.vval.v_string);
972}
973
974/*
975 * Stop redirecting command output to a variable.
976 */
977 void
978var_redir_stop()
979{
980 if (redir_lval != NULL)
981 {
982 clear_lval(redir_lval);
983 vim_free(redir_lval);
984 redir_lval = NULL;
985 }
986 vim_free(redir_varname);
987 redir_varname = NULL;
988}
989
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990# if defined(FEAT_MBYTE) || defined(PROTO)
991 int
992eval_charconvert(enc_from, enc_to, fname_from, fname_to)
993 char_u *enc_from;
994 char_u *enc_to;
995 char_u *fname_from;
996 char_u *fname_to;
997{
998 int err = FALSE;
999
1000 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1001 set_vim_var_string(VV_CC_TO, enc_to, -1);
1002 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1003 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1004 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1005 err = TRUE;
1006 set_vim_var_string(VV_CC_FROM, NULL, -1);
1007 set_vim_var_string(VV_CC_TO, NULL, -1);
1008 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1009 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1010
1011 if (err)
1012 return FAIL;
1013 return OK;
1014}
1015# endif
1016
1017# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1018 int
1019eval_printexpr(fname, args)
1020 char_u *fname;
1021 char_u *args;
1022{
1023 int err = FALSE;
1024
1025 set_vim_var_string(VV_FNAME_IN, fname, -1);
1026 set_vim_var_string(VV_CMDARG, args, -1);
1027 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1028 err = TRUE;
1029 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1030 set_vim_var_string(VV_CMDARG, NULL, -1);
1031
1032 if (err)
1033 {
1034 mch_remove(fname);
1035 return FAIL;
1036 }
1037 return OK;
1038}
1039# endif
1040
1041# if defined(FEAT_DIFF) || defined(PROTO)
1042 void
1043eval_diff(origfile, newfile, outfile)
1044 char_u *origfile;
1045 char_u *newfile;
1046 char_u *outfile;
1047{
1048 int err = FALSE;
1049
1050 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1051 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1052 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1053 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1054 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1055 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1056 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1057}
1058
1059 void
1060eval_patch(origfile, difffile, outfile)
1061 char_u *origfile;
1062 char_u *difffile;
1063 char_u *outfile;
1064{
1065 int err;
1066
1067 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1068 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1069 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1070 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1071 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1072 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1073 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1074}
1075# endif
1076
1077/*
1078 * Top level evaluation function, returning a boolean.
1079 * Sets "error" to TRUE if there was an error.
1080 * Return TRUE or FALSE.
1081 */
1082 int
1083eval_to_bool(arg, error, nextcmd, skip)
1084 char_u *arg;
1085 int *error;
1086 char_u **nextcmd;
1087 int skip; /* only parse, don't execute */
1088{
Bram Moolenaar33570922005-01-25 22:26:29 +00001089 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 int retval = FALSE;
1091
1092 if (skip)
1093 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001094 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 else
1097 {
1098 *error = FALSE;
1099 if (!skip)
1100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001101 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001102 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 }
1104 }
1105 if (skip)
1106 --emsg_skip;
1107
1108 return retval;
1109}
1110
1111/*
1112 * Top level evaluation function, returning a string. If "skip" is TRUE,
1113 * only parsing to "nextcmd" is done, without reporting errors. Return
1114 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1115 */
1116 char_u *
1117eval_to_string_skip(arg, nextcmd, skip)
1118 char_u *arg;
1119 char_u **nextcmd;
1120 int skip; /* only parse, don't execute */
1121{
Bram Moolenaar33570922005-01-25 22:26:29 +00001122 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 char_u *retval;
1124
1125 if (skip)
1126 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001127 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 retval = NULL;
1129 else
1130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001131 retval = vim_strsave(get_tv_string(&tv));
1132 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 }
1134 if (skip)
1135 --emsg_skip;
1136
1137 return retval;
1138}
1139
1140/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001141 * Skip over an expression at "*pp".
1142 * Return FAIL for an error, OK otherwise.
1143 */
1144 int
1145skip_expr(pp)
1146 char_u **pp;
1147{
Bram Moolenaar33570922005-01-25 22:26:29 +00001148 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001149
1150 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001151 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001152}
1153
1154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 * Top level evaluation function, returning a string.
1156 * Return pointer to allocated memory, or NULL for failure.
1157 */
1158 char_u *
1159eval_to_string(arg, nextcmd)
1160 char_u *arg;
1161 char_u **nextcmd;
1162{
Bram Moolenaar33570922005-01-25 22:26:29 +00001163 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 char_u *retval;
1165
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 retval = NULL;
1168 else
1169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001170 retval = vim_strsave(get_tv_string(&tv));
1171 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173
1174 return retval;
1175}
1176
1177/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001178 * Call eval_to_string() without using current local variables and using
1179 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 */
1181 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001182eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 char_u *arg;
1184 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001185 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186{
1187 char_u *retval;
1188 void *save_funccalp;
1189
1190 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001191 if (use_sandbox)
1192 ++sandbox;
1193 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 retval = eval_to_string(arg, nextcmd);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001195 if (use_sandbox)
1196 --sandbox;
1197 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 restore_funccal(save_funccalp);
1199 return retval;
1200}
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202/*
1203 * Top level evaluation function, returning a number.
1204 * Evaluates "expr" silently.
1205 * Returns -1 for an error.
1206 */
1207 int
1208eval_to_number(expr)
1209 char_u *expr;
1210{
Bram Moolenaar33570922005-01-25 22:26:29 +00001211 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001213 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
1215 ++emsg_off;
1216
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 retval = -1;
1219 else
1220 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001221 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001222 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
1224 --emsg_off;
1225
1226 return retval;
1227}
1228
Bram Moolenaara40058a2005-07-11 22:42:07 +00001229/*
1230 * Prepare v: variable "idx" to be used.
1231 * Save the current typeval in "save_tv".
1232 * When not used yet add the variable to the v: hashtable.
1233 */
1234 static void
1235prepare_vimvar(idx, save_tv)
1236 int idx;
1237 typval_T *save_tv;
1238{
1239 *save_tv = vimvars[idx].vv_tv;
1240 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1241 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1242}
1243
1244/*
1245 * Restore v: variable "idx" to typeval "save_tv".
1246 * When no longer defined, remove the variable from the v: hashtable.
1247 */
1248 static void
1249restore_vimvar(idx, save_tv)
1250 int idx;
1251 typval_T *save_tv;
1252{
1253 hashitem_T *hi;
1254
1255 clear_tv(&vimvars[idx].vv_tv);
1256 vimvars[idx].vv_tv = *save_tv;
1257 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1258 {
1259 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1260 if (HASHITEM_EMPTY(hi))
1261 EMSG2(_(e_intern2), "restore_vimvar()");
1262 else
1263 hash_remove(&vimvarht, hi);
1264 }
1265}
1266
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001267#if defined(FEAT_SYN_HL) || defined(PROTO)
1268/*
1269 * Evaluate an expression to a list with suggestions.
1270 * For the "expr:" part of 'spellsuggest'.
1271 */
1272 list_T *
1273eval_spell_expr(badword, expr)
1274 char_u *badword;
1275 char_u *expr;
1276{
1277 typval_T save_val;
1278 typval_T rettv;
1279 list_T *list = NULL;
1280 char_u *p = skipwhite(expr);
1281
1282 /* Set "v:val" to the bad word. */
1283 prepare_vimvar(VV_VAL, &save_val);
1284 vimvars[VV_VAL].vv_type = VAR_STRING;
1285 vimvars[VV_VAL].vv_str = badword;
1286 if (p_verbose == 0)
1287 ++emsg_off;
1288
1289 if (eval1(&p, &rettv, TRUE) == OK)
1290 {
1291 if (rettv.v_type != VAR_LIST)
1292 clear_tv(&rettv);
1293 else
1294 list = rettv.vval.v_list;
1295 }
1296
1297 if (p_verbose == 0)
1298 --emsg_off;
1299 vimvars[VV_VAL].vv_str = NULL;
1300 restore_vimvar(VV_VAL, &save_val);
1301
1302 return list;
1303}
1304
1305/*
1306 * "list" is supposed to contain two items: a word and a number. Return the
1307 * word in "pp" and the number as the return value.
1308 * Return -1 if anything isn't right.
1309 * Used to get the good word and score from the eval_spell_expr() result.
1310 */
1311 int
1312get_spellword(list, pp)
1313 list_T *list;
1314 char_u **pp;
1315{
1316 listitem_T *li;
1317
1318 li = list->lv_first;
1319 if (li == NULL)
1320 return -1;
1321 *pp = get_tv_string(&li->li_tv);
1322
1323 li = li->li_next;
1324 if (li == NULL)
1325 return -1;
1326 return get_tv_number(&li->li_tv);
1327}
1328#endif
1329
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001330/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001331 * Top level evaluation function.
1332 * Returns an allocated typval_T with the result.
1333 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001334 */
1335 typval_T *
1336eval_expr(arg, nextcmd)
1337 char_u *arg;
1338 char_u **nextcmd;
1339{
1340 typval_T *tv;
1341
1342 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001343 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001344 {
1345 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001346 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001347 }
1348
1349 return tv;
1350}
1351
1352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1354/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001355 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001357 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001359 static int
1360call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 char_u *func;
1362 int argc;
1363 char_u **argv;
1364 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366{
Bram Moolenaar33570922005-01-25 22:26:29 +00001367 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 long n;
1369 int len;
1370 int i;
1371 int doesrange;
1372 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001373 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
Bram Moolenaar33570922005-01-25 22:26:29 +00001375 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001377 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
1379 for (i = 0; i < argc; i++)
1380 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001381 /* Pass a NULL or empty argument as an empty string */
1382 if (argv[i] == NULL || *argv[i] == NUL)
1383 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001384 argvars[i].v_type = VAR_STRING;
1385 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001386 continue;
1387 }
1388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 /* Recognize a number argument, the others must be strings. */
1390 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1391 if (len != 0 && len == (int)STRLEN(argv[i]))
1392 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001393 argvars[i].v_type = VAR_NUMBER;
1394 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 else
1397 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001398 argvars[i].v_type = VAR_STRING;
1399 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
1401 }
1402
1403 if (safe)
1404 {
1405 save_funccalp = save_funccal();
1406 ++sandbox;
1407 }
1408
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001409 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1410 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001412 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (safe)
1414 {
1415 --sandbox;
1416 restore_funccal(save_funccalp);
1417 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001418 vim_free(argvars);
1419
1420 if (ret == FAIL)
1421 clear_tv(rettv);
1422
1423 return ret;
1424}
1425
1426/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001427 * Call vimL function "func" and return the result as a string.
1428 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001429 * Uses argv[argc] for the function arguments.
1430 */
1431 void *
1432call_func_retstr(func, argc, argv, safe)
1433 char_u *func;
1434 int argc;
1435 char_u **argv;
1436 int safe; /* use the sandbox */
1437{
1438 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001439 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001440
1441 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1442 return NULL;
1443
1444 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001445 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 return retval;
1447}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001448
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001449#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001450/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001451 * Call vimL function "func" and return the result as a number.
1452 * Returns -1 when calling the function fails.
1453 * Uses argv[argc] for the function arguments.
1454 */
1455 long
1456call_func_retnr(func, argc, argv, safe)
1457 char_u *func;
1458 int argc;
1459 char_u **argv;
1460 int safe; /* use the sandbox */
1461{
1462 typval_T rettv;
1463 long retval;
1464
1465 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1466 return -1;
1467
1468 retval = get_tv_number_chk(&rettv, NULL);
1469 clear_tv(&rettv);
1470 return retval;
1471}
1472#endif
1473
1474/*
1475 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001476 * Uses argv[argc] for the function arguments.
1477 */
1478 void *
1479call_func_retlist(func, argc, argv, safe)
1480 char_u *func;
1481 int argc;
1482 char_u **argv;
1483 int safe; /* use the sandbox */
1484{
1485 typval_T rettv;
1486
1487 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1488 return NULL;
1489
1490 if (rettv.v_type != VAR_LIST)
1491 {
1492 clear_tv(&rettv);
1493 return NULL;
1494 }
1495
1496 return rettv.vval.v_list;
1497}
1498
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499#endif
1500
1501/*
1502 * Save the current function call pointer, and set it to NULL.
1503 * Used when executing autocommands and for ":source".
1504 */
1505 void *
1506save_funccal()
1507{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001508 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 current_funccal = NULL;
1511 return (void *)fc;
1512}
1513
1514 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001515restore_funccal(vfc)
1516 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001518 funccall_T *fc = (funccall_T *)vfc;
1519
1520 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521}
1522
Bram Moolenaar05159a02005-02-26 23:04:13 +00001523#if defined(FEAT_PROFILE) || defined(PROTO)
1524/*
1525 * Prepare profiling for entering a child or something else that is not
1526 * counted for the script/function itself.
1527 * Should always be called in pair with prof_child_exit().
1528 */
1529 void
1530prof_child_enter(tm)
1531 proftime_T *tm; /* place to store waittime */
1532{
1533 funccall_T *fc = current_funccal;
1534
1535 if (fc != NULL && fc->func->uf_profiling)
1536 profile_start(&fc->prof_child);
1537 script_prof_save(tm);
1538}
1539
1540/*
1541 * Take care of time spent in a child.
1542 * Should always be called after prof_child_enter().
1543 */
1544 void
1545prof_child_exit(tm)
1546 proftime_T *tm; /* where waittime was stored */
1547{
1548 funccall_T *fc = current_funccal;
1549
1550 if (fc != NULL && fc->func->uf_profiling)
1551 {
1552 profile_end(&fc->prof_child);
1553 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1554 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1555 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1556 }
1557 script_prof_restore(tm);
1558}
1559#endif
1560
1561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562#ifdef FEAT_FOLDING
1563/*
1564 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1565 * it in "*cp". Doesn't give error messages.
1566 */
1567 int
1568eval_foldexpr(arg, cp)
1569 char_u *arg;
1570 int *cp;
1571{
Bram Moolenaar33570922005-01-25 22:26:29 +00001572 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 int retval;
1574 char_u *s;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001575 int use_sandbox = was_set_insecurely((char_u *)"foldexpr");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
1577 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001578 if (use_sandbox)
1579 ++sandbox;
1580 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001582 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 retval = 0;
1584 else
1585 {
1586 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001587 if (tv.v_type == VAR_NUMBER)
1588 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001589 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 retval = 0;
1591 else
1592 {
1593 /* If the result is a string, check if there is a non-digit before
1594 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001595 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (!VIM_ISDIGIT(*s) && *s != '-')
1597 *cp = *s++;
1598 retval = atol((char *)s);
1599 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001600 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001603 if (use_sandbox)
1604 --sandbox;
1605 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
1607 return retval;
1608}
1609#endif
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001612 * ":let" list all variable values
1613 * ":let var1 var2" list variable values
1614 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001615 * ":let var += expr" assignment command.
1616 * ":let var -= expr" assignment command.
1617 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 */
1620 void
1621ex_let(eap)
1622 exarg_T *eap;
1623{
1624 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001625 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001626 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 int var_count = 0;
1629 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001630 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 expr = skip_var_list(arg, &var_count, &semicolon);
1633 if (expr == NULL)
1634 return;
1635 expr = vim_strchr(expr, '=');
1636 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001638 /*
1639 * ":let" without "=": list variables
1640 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001641 if (*arg == '[')
1642 EMSG(_(e_invarg));
1643 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001644 /* ":let var1 var2" */
1645 arg = list_arg_vars(eap, arg);
1646 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001648 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001649 list_glob_vars();
1650 list_buf_vars();
1651 list_win_vars();
1652 list_vim_vars();
1653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 eap->nextcmd = check_nextcmd(arg);
1655 }
1656 else
1657 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001658 op[0] = '=';
1659 op[1] = NUL;
1660 if (expr > arg)
1661 {
1662 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1663 op[0] = expr[-1]; /* +=, -= or .= */
1664 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001666
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 if (eap->skip)
1668 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if (eap->skip)
1671 {
1672 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001673 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 --emsg_skip;
1675 }
1676 else if (i != FAIL)
1677 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001678 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001679 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
1682 }
1683}
1684
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685/*
1686 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1687 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 * When "nextchars" is not NULL it points to a string with characters that
1689 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1690 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 * Returns OK or FAIL;
1692 */
1693 static int
1694ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1695 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001696 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 int copy; /* copy values from "tv", don't move */
1698 int semicolon; /* from skip_var_list() */
1699 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001700 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701{
1702 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001703 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001705 listitem_T *item;
1706 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707
1708 if (*arg != '[')
1709 {
1710 /*
1711 * ":let var = expr" or ":for var in list"
1712 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001713 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 return FAIL;
1715 return OK;
1716 }
1717
1718 /*
1719 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1720 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001721 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001722 {
1723 EMSG(_(e_listreq));
1724 return FAIL;
1725 }
1726
1727 i = list_len(l);
1728 if (semicolon == 0 && var_count < i)
1729 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001730 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001731 return FAIL;
1732 }
1733 if (var_count - semicolon > i)
1734 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001735 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 return FAIL;
1737 }
1738
1739 item = l->lv_first;
1740 while (*arg != ']')
1741 {
1742 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001743 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744 item = item->li_next;
1745 if (arg == NULL)
1746 return FAIL;
1747
1748 arg = skipwhite(arg);
1749 if (*arg == ';')
1750 {
1751 /* Put the rest of the list (may be empty) in the var after ';'.
1752 * Create a new list for this. */
1753 l = list_alloc();
1754 if (l == NULL)
1755 return FAIL;
1756 while (item != NULL)
1757 {
1758 list_append_tv(l, &item->li_tv);
1759 item = item->li_next;
1760 }
1761
1762 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001763 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 ltv.vval.v_list = l;
1765 l->lv_refcount = 1;
1766
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001767 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1768 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 clear_tv(&ltv);
1770 if (arg == NULL)
1771 return FAIL;
1772 break;
1773 }
1774 else if (*arg != ',' && *arg != ']')
1775 {
1776 EMSG2(_(e_intern2), "ex_let_vars()");
1777 return FAIL;
1778 }
1779 }
1780
1781 return OK;
1782}
1783
1784/*
1785 * Skip over assignable variable "var" or list of variables "[var, var]".
1786 * Used for ":let varvar = expr" and ":for varvar in expr".
1787 * For "[var, var]" increment "*var_count" for each variable.
1788 * for "[var, var; var]" set "semicolon".
1789 * Return NULL for an error.
1790 */
1791 static char_u *
1792skip_var_list(arg, var_count, semicolon)
1793 char_u *arg;
1794 int *var_count;
1795 int *semicolon;
1796{
1797 char_u *p, *s;
1798
1799 if (*arg == '[')
1800 {
1801 /* "[var, var]": find the matching ']'. */
1802 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001803 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 {
1805 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1806 s = skip_var_one(p);
1807 if (s == p)
1808 {
1809 EMSG2(_(e_invarg2), p);
1810 return NULL;
1811 }
1812 ++*var_count;
1813
1814 p = skipwhite(s);
1815 if (*p == ']')
1816 break;
1817 else if (*p == ';')
1818 {
1819 if (*semicolon == 1)
1820 {
1821 EMSG(_("Double ; in list of variables"));
1822 return NULL;
1823 }
1824 *semicolon = 1;
1825 }
1826 else if (*p != ',')
1827 {
1828 EMSG2(_(e_invarg2), p);
1829 return NULL;
1830 }
1831 }
1832 return p + 1;
1833 }
1834 else
1835 return skip_var_one(arg);
1836}
1837
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001838/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001839 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1840 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001841 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 static char_u *
1843skip_var_one(arg)
1844 char_u *arg;
1845{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001846 if (*arg == '@' && arg[1] != NUL)
1847 return arg + 2;
1848 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1849 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850}
1851
Bram Moolenaara7043832005-01-21 11:56:39 +00001852/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001853 * List variables for hashtab "ht" with prefix "prefix".
1854 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001855 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001857list_hashtable_vars(ht, prefix, empty)
1858 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001859 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001860 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001861{
Bram Moolenaar33570922005-01-25 22:26:29 +00001862 hashitem_T *hi;
1863 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001864 int todo;
1865
1866 todo = ht->ht_used;
1867 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1868 {
1869 if (!HASHITEM_EMPTY(hi))
1870 {
1871 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001872 di = HI2DI(hi);
1873 if (empty || di->di_tv.v_type != VAR_STRING
1874 || di->di_tv.vval.v_string != NULL)
1875 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001876 }
1877 }
1878}
1879
1880/*
1881 * List global variables.
1882 */
1883 static void
1884list_glob_vars()
1885{
Bram Moolenaar33570922005-01-25 22:26:29 +00001886 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001887}
1888
1889/*
1890 * List buffer variables.
1891 */
1892 static void
1893list_buf_vars()
1894{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001895 char_u numbuf[NUMBUFLEN];
1896
Bram Moolenaar33570922005-01-25 22:26:29 +00001897 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001898
1899 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1900 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001901}
1902
1903/*
1904 * List window variables.
1905 */
1906 static void
1907list_win_vars()
1908{
Bram Moolenaar33570922005-01-25 22:26:29 +00001909 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001910}
1911
1912/*
1913 * List Vim variables.
1914 */
1915 static void
1916list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917{
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919}
1920
1921/*
1922 * List variables in "arg".
1923 */
1924 static char_u *
1925list_arg_vars(eap, arg)
1926 exarg_T *eap;
1927 char_u *arg;
1928{
1929 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001930 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001932 char_u *name_start;
1933 char_u *arg_subsc;
1934 char_u *tofree;
1935 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936
1937 while (!ends_excmd(*arg) && !got_int)
1938 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001939 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001941 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001942 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1943 {
1944 emsg_severe = TRUE;
1945 EMSG(_(e_trailing));
1946 break;
1947 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001949 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001951 /* get_name_len() takes care of expanding curly braces */
1952 name_start = name = arg;
1953 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1954 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001956 /* This is mainly to keep test 49 working: when expanding
1957 * curly braces fails overrule the exception error message. */
1958 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001960 emsg_severe = TRUE;
1961 EMSG2(_(e_invarg2), arg);
1962 break;
1963 }
1964 error = TRUE;
1965 }
1966 else
1967 {
1968 if (tofree != NULL)
1969 name = tofree;
1970 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001972 else
1973 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001974 /* handle d.key, l[idx], f(expr) */
1975 arg_subsc = arg;
1976 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001977 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001978 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001979 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001980 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001981 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001982 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001983 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001984 case 'g': list_glob_vars(); break;
1985 case 'b': list_buf_vars(); break;
1986 case 'w': list_win_vars(); break;
1987 case 'v': list_vim_vars(); break;
1988 default:
1989 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001990 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001991 }
1992 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001993 {
1994 char_u numbuf[NUMBUFLEN];
1995 char_u *tf;
1996 int c;
1997 char_u *s;
1998
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001999 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002000 c = *arg;
2001 *arg = NUL;
2002 list_one_var_a((char_u *)"",
2003 arg == arg_subsc ? name : name_start,
2004 tv.v_type, s == NULL ? (char_u *)"" : s);
2005 *arg = c;
2006 vim_free(tf);
2007 }
2008 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002010 }
2011 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002012
2013 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002015
2016 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017 }
2018
2019 return arg;
2020}
2021
2022/*
2023 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2024 * Returns a pointer to the char just after the var name.
2025 * Returns NULL if there is an error.
2026 */
2027 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002028ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002029 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002030 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002033 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002034{
2035 int c1;
2036 char_u *name;
2037 char_u *p;
2038 char_u *arg_end = NULL;
2039 int len;
2040 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002041 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042
2043 /*
2044 * ":let $VAR = expr": Set environment variable.
2045 */
2046 if (*arg == '$')
2047 {
2048 /* Find the end of the name. */
2049 ++arg;
2050 name = arg;
2051 len = get_env_len(&arg);
2052 if (len == 0)
2053 EMSG2(_(e_invarg2), name - 1);
2054 else
2055 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002056 if (op != NULL && (*op == '+' || *op == '-'))
2057 EMSG2(_(e_letwrong), op);
2058 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060 EMSG(_(e_letunexp));
2061 else
2062 {
2063 c1 = name[len];
2064 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002065 p = get_tv_string_chk(tv);
2066 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002067 {
2068 int mustfree = FALSE;
2069 char_u *s = vim_getenv(name, &mustfree);
2070
2071 if (s != NULL)
2072 {
2073 p = tofree = concat_str(s, p);
2074 if (mustfree)
2075 vim_free(s);
2076 }
2077 }
2078 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002079 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002080 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002081 if (STRICMP(name, "HOME") == 0)
2082 init_homedir();
2083 else if (didset_vim && STRICMP(name, "VIM") == 0)
2084 didset_vim = FALSE;
2085 else if (didset_vimruntime
2086 && STRICMP(name, "VIMRUNTIME") == 0)
2087 didset_vimruntime = FALSE;
2088 arg_end = arg;
2089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002091 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002092 }
2093 }
2094 }
2095
2096 /*
2097 * ":let &option = expr": Set option value.
2098 * ":let &l:option = expr": Set local option value.
2099 * ":let &g:option = expr": Set global option value.
2100 */
2101 else if (*arg == '&')
2102 {
2103 /* Find the end of the name. */
2104 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002105 if (p == NULL || (endchars != NULL
2106 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002107 EMSG(_(e_letunexp));
2108 else
2109 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002110 long n;
2111 int opt_type;
2112 long numval;
2113 char_u *stringval = NULL;
2114 char_u *s;
2115
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002116 c1 = *p;
2117 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002118
2119 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002120 s = get_tv_string_chk(tv); /* != NULL if number or string */
2121 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002122 {
2123 opt_type = get_option_value(arg, &numval,
2124 &stringval, opt_flags);
2125 if ((opt_type == 1 && *op == '.')
2126 || (opt_type == 0 && *op != '.'))
2127 EMSG2(_(e_letwrong), op);
2128 else
2129 {
2130 if (opt_type == 1) /* number */
2131 {
2132 if (*op == '+')
2133 n = numval + n;
2134 else
2135 n = numval - n;
2136 }
2137 else if (opt_type == 0 && stringval != NULL) /* string */
2138 {
2139 s = concat_str(stringval, s);
2140 vim_free(stringval);
2141 stringval = s;
2142 }
2143 }
2144 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002145 if (s != NULL)
2146 {
2147 set_option_value(arg, n, s, opt_flags);
2148 arg_end = p;
2149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002151 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 }
2153 }
2154
2155 /*
2156 * ":let @r = expr": Set register contents.
2157 */
2158 else if (*arg == '@')
2159 {
2160 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002161 if (op != NULL && (*op == '+' || *op == '-'))
2162 EMSG2(_(e_letwrong), op);
2163 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002164 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002165 EMSG(_(e_letunexp));
2166 else
2167 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002168 char_u *tofree = NULL;
2169 char_u *s;
2170
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002171 p = get_tv_string_chk(tv);
2172 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002173 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002174 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002175 if (s != NULL)
2176 {
2177 p = tofree = concat_str(s, p);
2178 vim_free(s);
2179 }
2180 }
2181 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002182 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002183 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002184 arg_end = arg + 1;
2185 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 }
2188 }
2189
2190 /*
2191 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002194 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002196 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002198 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2202 EMSG(_(e_letunexp));
2203 else
2204 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002205 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 arg_end = p;
2207 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 }
2211
2212 else
2213 EMSG2(_(e_invarg2), arg);
2214
2215 return arg_end;
2216}
2217
2218/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002219 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2220 */
2221 static int
2222check_changedtick(arg)
2223 char_u *arg;
2224{
2225 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2226 {
2227 EMSG2(_(e_readonlyvar), arg);
2228 return TRUE;
2229 }
2230 return FALSE;
2231}
2232
2233/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 * Get an lval: variable, Dict item or List item that can be assigned a value
2235 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2236 * "name.key", "name.key[expr]" etc.
2237 * Indexing only works if "name" is an existing List or Dictionary.
2238 * "name" points to the start of the name.
2239 * If "rettv" is not NULL it points to the value to be assigned.
2240 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2241 * wrong; must end in space or cmd separator.
2242 *
2243 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 */
2247 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002248get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002250 typval_T *rettv;
2251 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 int unlet;
2253 int skip;
2254 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002255 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 char_u *expr_start, *expr_end;
2259 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002260 dictitem_T *v;
2261 typval_T var1;
2262 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002263 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002264 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002265 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002266 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002267 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002270 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002271
2272 if (skip)
2273 {
2274 /* When skipping just find the end of the name. */
2275 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002276 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002277 }
2278
2279 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002280 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 if (expr_start != NULL)
2282 {
2283 /* Don't expand the name when we already know there is an error. */
2284 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2285 && *p != '[' && *p != '.')
2286 {
2287 EMSG(_(e_trailing));
2288 return NULL;
2289 }
2290
2291 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2292 if (lp->ll_exp_name == NULL)
2293 {
2294 /* Report an invalid expression in braces, unless the
2295 * expression evaluation has been cancelled due to an
2296 * aborting error, an interrupt, or an exception. */
2297 if (!aborting() && !quiet)
2298 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002299 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002300 EMSG2(_(e_invarg2), name);
2301 return NULL;
2302 }
2303 }
2304 lp->ll_name = lp->ll_exp_name;
2305 }
2306 else
2307 lp->ll_name = name;
2308
2309 /* Without [idx] or .key we are done. */
2310 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2311 return p;
2312
2313 cc = *p;
2314 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002315 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 if (v == NULL && !quiet)
2317 EMSG2(_(e_undefvar), lp->ll_name);
2318 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 if (v == NULL)
2320 return NULL;
2321
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 /*
2323 * Loop until no more [idx] or .key is following.
2324 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002325 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002328 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2329 && !(lp->ll_tv->v_type == VAR_DICT
2330 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 if (!quiet)
2333 EMSG(_("E689: Can only index a List or Dictionary"));
2334 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 if (!quiet)
2339 EMSG(_("E708: [:] must come last"));
2340 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002342
Bram Moolenaar8c711452005-01-14 21:53:12 +00002343 len = -1;
2344 if (*p == '.')
2345 {
2346 key = p + 1;
2347 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2348 ;
2349 if (len == 0)
2350 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002351 if (!quiet)
2352 EMSG(_(e_emptykey));
2353 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002354 }
2355 p = key + len;
2356 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002357 else
2358 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002359 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002360 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002361 if (*p == ':')
2362 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002363 else
2364 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002365 empty1 = FALSE;
2366 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002367 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368 if (get_tv_string_chk(&var1) == NULL)
2369 {
2370 /* not a number or string */
2371 clear_tv(&var1);
2372 return NULL;
2373 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002374 }
2375
2376 /* Optionally get the second index [ :expr]. */
2377 if (*p == ':')
2378 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002379 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002380 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002381 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002383 if (!empty1)
2384 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002385 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002386 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 if (rettv != NULL && (rettv->v_type != VAR_LIST
2388 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002389 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 if (!quiet)
2391 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002392 if (!empty1)
2393 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 }
2396 p = skipwhite(p + 1);
2397 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002398 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002399 else
2400 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002402 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2403 {
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 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002408 if (get_tv_string_chk(&var2) == NULL)
2409 {
2410 /* not a number or string */
2411 if (!empty1)
2412 clear_tv(&var1);
2413 clear_tv(&var2);
2414 return NULL;
2415 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002421
Bram Moolenaar8c711452005-01-14 21:53:12 +00002422 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002423 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 if (!quiet)
2425 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002426 if (!empty1)
2427 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002429 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 }
2432
2433 /* Skip to past ']'. */
2434 ++p;
2435 }
2436
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 {
2439 if (len == -1)
2440 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 if (*key == NUL)
2444 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 if (!quiet)
2446 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 }
2450 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 lp->ll_list = NULL;
2452 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002453 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002456 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 if (len == -1)
2462 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002464 }
2465 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002467 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 if (len == -1)
2470 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 p = NULL;
2473 break;
2474 }
2475 if (len == -1)
2476 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002478 }
2479 else
2480 {
2481 /*
2482 * Get the number and item for the only or first index of the List.
2483 */
2484 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 else
2487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002488 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489 clear_tv(&var1);
2490 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 lp->ll_list = lp->ll_tv->vval.v_list;
2493 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2494 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (!quiet)
2497 EMSGN(_(e_listidx), lp->ll_n1);
2498 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 }
2502
2503 /*
2504 * May need to find the item or absolute index for the second
2505 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 * When no index given: "lp->ll_empty2" is TRUE.
2507 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002508 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002511 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002512 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 if (ni == NULL)
2517 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (!quiet)
2519 EMSGN(_(e_listidx), lp->ll_n2);
2520 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 }
2524
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2526 if (lp->ll_n1 < 0)
2527 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2528 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002529 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 if (!quiet)
2531 EMSGN(_(e_listidx), lp->ll_n2);
2532 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002533 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002534 }
2535
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002538 }
2539
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 return p;
2541}
2542
2543/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 */
2546 static void
2547clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002548 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549{
2550 vim_free(lp->ll_exp_name);
2551 vim_free(lp->ll_newkey);
2552}
2553
2554/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002555 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002557 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 */
2559 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002560set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002563 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002565 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566{
2567 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 listitem_T *ri;
2569 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570
2571 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 cc = *endp;
2576 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002577 if (op != NULL && *op != '=')
2578 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002579 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002580
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002581 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002582 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2583 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002584 {
2585 if (tv_op(&tv, rettv, op) == OK)
2586 set_var(lp->ll_name, &tv, FALSE);
2587 clear_tv(&tv);
2588 }
2589 }
2590 else
2591 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002595 else if (tv_check_lock(lp->ll_newkey == NULL
2596 ? lp->ll_tv->v_lock
2597 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2598 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 else if (lp->ll_range)
2600 {
2601 /*
2602 * Assign the List values to the list items.
2603 */
2604 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002605 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002606 if (op != NULL && *op != '=')
2607 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2608 else
2609 {
2610 clear_tv(&lp->ll_li->li_tv);
2611 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2612 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 ri = ri->li_next;
2614 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2615 break;
2616 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002619 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002620 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 ri = NULL;
2622 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002623 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002624 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 lp->ll_li = lp->ll_li->li_next;
2626 ++lp->ll_n1;
2627 }
2628 if (ri != NULL)
2629 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002630 else if (lp->ll_empty2
2631 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 : lp->ll_n1 != lp->ll_n2)
2633 EMSG(_("E711: List value has not enough items"));
2634 }
2635 else
2636 {
2637 /*
2638 * Assign to a List or Dictionary item.
2639 */
2640 if (lp->ll_newkey != NULL)
2641 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002642 if (op != NULL && *op != '=')
2643 {
2644 EMSG2(_(e_letwrong), op);
2645 return;
2646 }
2647
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002649 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (di == NULL)
2651 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002652 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2653 {
2654 vim_free(di);
2655 return;
2656 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002659 else if (op != NULL && *op != '=')
2660 {
2661 tv_op(lp->ll_tv, rettv, op);
2662 return;
2663 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 /*
2668 * Assign the value to the variable or list item.
2669 */
2670 if (copy)
2671 copy_tv(rettv, lp->ll_tv);
2672 else
2673 {
2674 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002675 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002677 }
2678 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002679}
2680
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002681/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002682 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2683 * Returns OK or FAIL.
2684 */
2685 static int
2686tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002687 typval_T *tv1;
2688 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002689 char_u *op;
2690{
2691 long n;
2692 char_u numbuf[NUMBUFLEN];
2693 char_u *s;
2694
2695 /* Can't do anything with a Funcref or a Dict on the right. */
2696 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2697 {
2698 switch (tv1->v_type)
2699 {
2700 case VAR_DICT:
2701 case VAR_FUNC:
2702 break;
2703
2704 case VAR_LIST:
2705 if (*op != '+' || tv2->v_type != VAR_LIST)
2706 break;
2707 /* List += List */
2708 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2709 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2710 return OK;
2711
2712 case VAR_NUMBER:
2713 case VAR_STRING:
2714 if (tv2->v_type == VAR_LIST)
2715 break;
2716 if (*op == '+' || *op == '-')
2717 {
2718 /* nr += nr or nr -= nr*/
2719 n = get_tv_number(tv1);
2720 if (*op == '+')
2721 n += get_tv_number(tv2);
2722 else
2723 n -= get_tv_number(tv2);
2724 clear_tv(tv1);
2725 tv1->v_type = VAR_NUMBER;
2726 tv1->vval.v_number = n;
2727 }
2728 else
2729 {
2730 /* str .= str */
2731 s = get_tv_string(tv1);
2732 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2733 clear_tv(tv1);
2734 tv1->v_type = VAR_STRING;
2735 tv1->vval.v_string = s;
2736 }
2737 return OK;
2738 }
2739 }
2740
2741 EMSG2(_(e_letwrong), op);
2742 return FAIL;
2743}
2744
2745/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002746 * Add a watcher to a list.
2747 */
2748 static void
2749list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002750 list_T *l;
2751 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002752{
2753 lw->lw_next = l->lv_watch;
2754 l->lv_watch = lw;
2755}
2756
2757/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002758 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002759 * No warning when it isn't found...
2760 */
2761 static void
2762list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002763 list_T *l;
2764 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002765{
Bram Moolenaar33570922005-01-25 22:26:29 +00002766 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002767
2768 lwp = &l->lv_watch;
2769 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2770 {
2771 if (lw == lwrem)
2772 {
2773 *lwp = lw->lw_next;
2774 break;
2775 }
2776 lwp = &lw->lw_next;
2777 }
2778}
2779
2780/*
2781 * Just before removing an item from a list: advance watchers to the next
2782 * item.
2783 */
2784 static void
2785list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002786 list_T *l;
2787 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002788{
Bram Moolenaar33570922005-01-25 22:26:29 +00002789 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002790
2791 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2792 if (lw->lw_item == item)
2793 lw->lw_item = item->li_next;
2794}
2795
2796/*
2797 * Evaluate the expression used in a ":for var in expr" command.
2798 * "arg" points to "var".
2799 * Set "*errp" to TRUE for an error, FALSE otherwise;
2800 * Return a pointer that holds the info. Null when there is an error.
2801 */
2802 void *
2803eval_for_line(arg, errp, nextcmdp, skip)
2804 char_u *arg;
2805 int *errp;
2806 char_u **nextcmdp;
2807 int skip;
2808{
Bram Moolenaar33570922005-01-25 22:26:29 +00002809 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002810 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002811 typval_T tv;
2812 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002813
2814 *errp = TRUE; /* default: there is an error */
2815
Bram Moolenaar33570922005-01-25 22:26:29 +00002816 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002817 if (fi == NULL)
2818 return NULL;
2819
2820 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2821 if (expr == NULL)
2822 return fi;
2823
2824 expr = skipwhite(expr);
2825 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2826 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002827 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002828 return fi;
2829 }
2830
2831 if (skip)
2832 ++emsg_skip;
2833 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2834 {
2835 *errp = FALSE;
2836 if (!skip)
2837 {
2838 l = tv.vval.v_list;
2839 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002840 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002841 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002842 clear_tv(&tv);
2843 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002844 else
2845 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002846 /* No need to increment the refcount, it's already set for the
2847 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002848 fi->fi_list = l;
2849 list_add_watch(l, &fi->fi_lw);
2850 fi->fi_lw.lw_item = l->lv_first;
2851 }
2852 }
2853 }
2854 if (skip)
2855 --emsg_skip;
2856
2857 return fi;
2858}
2859
2860/*
2861 * Use the first item in a ":for" list. Advance to the next.
2862 * Assign the values to the variable (list). "arg" points to the first one.
2863 * Return TRUE when a valid item was found, FALSE when at end of list or
2864 * something wrong.
2865 */
2866 int
2867next_for_item(fi_void, arg)
2868 void *fi_void;
2869 char_u *arg;
2870{
Bram Moolenaar33570922005-01-25 22:26:29 +00002871 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002872 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002873 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002874
2875 item = fi->fi_lw.lw_item;
2876 if (item == NULL)
2877 result = FALSE;
2878 else
2879 {
2880 fi->fi_lw.lw_item = item->li_next;
2881 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2882 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2883 }
2884 return result;
2885}
2886
2887/*
2888 * Free the structure used to store info used by ":for".
2889 */
2890 void
2891free_for_info(fi_void)
2892 void *fi_void;
2893{
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002895
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002896 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002897 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002898 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002899 list_unref(fi->fi_list);
2900 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002901 vim_free(fi);
2902}
2903
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2905
2906 void
2907set_context_for_expression(xp, arg, cmdidx)
2908 expand_T *xp;
2909 char_u *arg;
2910 cmdidx_T cmdidx;
2911{
2912 int got_eq = FALSE;
2913 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002914 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002916 if (cmdidx == CMD_let)
2917 {
2918 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002919 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002920 {
2921 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002922 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002923 {
2924 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002925 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002926 if (vim_iswhite(*p))
2927 break;
2928 }
2929 return;
2930 }
2931 }
2932 else
2933 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2934 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 while ((xp->xp_pattern = vim_strpbrk(arg,
2936 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2937 {
2938 c = *xp->xp_pattern;
2939 if (c == '&')
2940 {
2941 c = xp->xp_pattern[1];
2942 if (c == '&')
2943 {
2944 ++xp->xp_pattern;
2945 xp->xp_context = cmdidx != CMD_let || got_eq
2946 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2947 }
2948 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002951 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2952 xp->xp_pattern += 2;
2953
2954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 }
2956 else if (c == '$')
2957 {
2958 /* environment variable */
2959 xp->xp_context = EXPAND_ENV_VARS;
2960 }
2961 else if (c == '=')
2962 {
2963 got_eq = TRUE;
2964 xp->xp_context = EXPAND_EXPRESSION;
2965 }
2966 else if (c == '<'
2967 && xp->xp_context == EXPAND_FUNCTIONS
2968 && vim_strchr(xp->xp_pattern, '(') == NULL)
2969 {
2970 /* Function name can start with "<SNR>" */
2971 break;
2972 }
2973 else if (cmdidx != CMD_let || got_eq)
2974 {
2975 if (c == '"') /* string */
2976 {
2977 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2978 if (c == '\\' && xp->xp_pattern[1] != NUL)
2979 ++xp->xp_pattern;
2980 xp->xp_context = EXPAND_NOTHING;
2981 }
2982 else if (c == '\'') /* literal string */
2983 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002984 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2986 /* skip */ ;
2987 xp->xp_context = EXPAND_NOTHING;
2988 }
2989 else if (c == '|')
2990 {
2991 if (xp->xp_pattern[1] == '|')
2992 {
2993 ++xp->xp_pattern;
2994 xp->xp_context = EXPAND_EXPRESSION;
2995 }
2996 else
2997 xp->xp_context = EXPAND_COMMANDS;
2998 }
2999 else
3000 xp->xp_context = EXPAND_EXPRESSION;
3001 }
3002 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003003 /* Doesn't look like something valid, expand as an expression
3004 * anyway. */
3005 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 arg = xp->xp_pattern;
3007 if (*arg != NUL)
3008 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3009 /* skip */ ;
3010 }
3011 xp->xp_pattern = arg;
3012}
3013
3014#endif /* FEAT_CMDL_COMPL */
3015
3016/*
3017 * ":1,25call func(arg1, arg2)" function call.
3018 */
3019 void
3020ex_call(eap)
3021 exarg_T *eap;
3022{
3023 char_u *arg = eap->arg;
3024 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003026 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003028 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 linenr_T lnum;
3030 int doesrange;
3031 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003032 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003034 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3035 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 if (tofree == NULL)
3037 return;
3038
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003039 /* Increase refcount on dictionary, it could get deleted when evaluating
3040 * the arguments. */
3041 if (fudi.fd_dict != NULL)
3042 ++fudi.fd_dict->dv_refcount;
3043
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3045 len = STRLEN(tofree);
3046 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047
Bram Moolenaar532c7802005-01-27 14:44:31 +00003048 /* Skip white space to allow ":call func ()". Not good, but required for
3049 * backward compatibility. */
3050 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003051 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
3053 if (*startarg != '(')
3054 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003055 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 goto end;
3057 }
3058
3059 /*
3060 * When skipping, evaluate the function once, to find the end of the
3061 * arguments.
3062 * When the function takes a range, this is discovered after the first
3063 * call, and the loop is broken.
3064 */
3065 if (eap->skip)
3066 {
3067 ++emsg_skip;
3068 lnum = eap->line2; /* do it once, also with an invalid range */
3069 }
3070 else
3071 lnum = eap->line1;
3072 for ( ; lnum <= eap->line2; ++lnum)
3073 {
3074 if (!eap->skip && eap->addr_count > 0)
3075 {
3076 curwin->w_cursor.lnum = lnum;
3077 curwin->w_cursor.col = 0;
3078 }
3079 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003081 eap->line1, eap->line2, &doesrange,
3082 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 {
3084 failed = TRUE;
3085 break;
3086 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003087 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 if (doesrange || eap->skip)
3089 break;
3090 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003091 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003092 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003093 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (aborting())
3095 break;
3096 }
3097 if (eap->skip)
3098 --emsg_skip;
3099
3100 if (!failed)
3101 {
3102 /* Check for trailing illegal characters and a following command. */
3103 if (!ends_excmd(*arg))
3104 {
3105 emsg_severe = TRUE;
3106 EMSG(_(e_trailing));
3107 }
3108 else
3109 eap->nextcmd = check_nextcmd(arg);
3110 }
3111
3112end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003113 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115}
3116
3117/*
3118 * ":unlet[!] var1 ... " command.
3119 */
3120 void
3121ex_unlet(eap)
3122 exarg_T *eap;
3123{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003124 ex_unletlock(eap, eap->arg, 0);
3125}
3126
3127/*
3128 * ":lockvar" and ":unlockvar" commands
3129 */
3130 void
3131ex_lockvar(eap)
3132 exarg_T *eap;
3133{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003135 int deep = 2;
3136
3137 if (eap->forceit)
3138 deep = -1;
3139 else if (vim_isdigit(*arg))
3140 {
3141 deep = getdigits(&arg);
3142 arg = skipwhite(arg);
3143 }
3144
3145 ex_unletlock(eap, arg, deep);
3146}
3147
3148/*
3149 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3150 */
3151 static void
3152ex_unletlock(eap, argstart, deep)
3153 exarg_T *eap;
3154 char_u *argstart;
3155 int deep;
3156{
3157 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003160 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
3162 do
3163 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003164 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003165 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3166 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003167 if (lv.ll_name == NULL)
3168 error = TRUE; /* error but continue parsing */
3169 if (name_end == NULL || (!vim_iswhite(*name_end)
3170 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003172 if (name_end != NULL)
3173 {
3174 emsg_severe = TRUE;
3175 EMSG(_(e_trailing));
3176 }
3177 if (!(eap->skip || error))
3178 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 break;
3180 }
3181
3182 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003183 {
3184 if (eap->cmdidx == CMD_unlet)
3185 {
3186 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3187 error = TRUE;
3188 }
3189 else
3190 {
3191 if (do_lock_var(&lv, name_end, deep,
3192 eap->cmdidx == CMD_lockvar) == FAIL)
3193 error = TRUE;
3194 }
3195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003197 if (!eap->skip)
3198 clear_lval(&lv);
3199
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 arg = skipwhite(name_end);
3201 } while (!ends_excmd(*arg));
3202
3203 eap->nextcmd = check_nextcmd(arg);
3204}
3205
Bram Moolenaar8c711452005-01-14 21:53:12 +00003206 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003207do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003208 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003209 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003210 int forceit;
3211{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003212 int ret = OK;
3213 int cc;
3214
3215 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003216 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003217 cc = *name_end;
3218 *name_end = NUL;
3219
3220 /* Normal name or expanded name. */
3221 if (check_changedtick(lp->ll_name))
3222 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003223 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003224 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003225 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003226 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003227 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3228 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003229 else if (lp->ll_range)
3230 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003231 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003232
3233 /* Delete a range of List items. */
3234 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3235 {
3236 li = lp->ll_li->li_next;
3237 listitem_remove(lp->ll_list, lp->ll_li);
3238 lp->ll_li = li;
3239 ++lp->ll_n1;
3240 }
3241 }
3242 else
3243 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003244 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003245 /* unlet a List item. */
3246 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003247 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003248 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003249 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003250 }
3251
3252 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003253}
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255/*
3256 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003257 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 */
3259 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003260do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003262 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
Bram Moolenaar33570922005-01-25 22:26:29 +00003264 hashtab_T *ht;
3265 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003266 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
Bram Moolenaar33570922005-01-25 22:26:29 +00003268 ht = find_var_ht(name, &varname);
3269 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003271 hi = hash_find(ht, varname);
3272 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003273 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003274 if (var_check_ro(HI2DI(hi)->di_flags, name))
3275 return FAIL;
3276 delete_var(ht, hi);
3277 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003280 if (forceit)
3281 return OK;
3282 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 return FAIL;
3284}
3285
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003286/*
3287 * Lock or unlock variable indicated by "lp".
3288 * "deep" is the levels to go (-1 for unlimited);
3289 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3290 */
3291 static int
3292do_lock_var(lp, name_end, deep, lock)
3293 lval_T *lp;
3294 char_u *name_end;
3295 int deep;
3296 int lock;
3297{
3298 int ret = OK;
3299 int cc;
3300 dictitem_T *di;
3301
3302 if (deep == 0) /* nothing to do */
3303 return OK;
3304
3305 if (lp->ll_tv == NULL)
3306 {
3307 cc = *name_end;
3308 *name_end = NUL;
3309
3310 /* Normal name or expanded name. */
3311 if (check_changedtick(lp->ll_name))
3312 ret = FAIL;
3313 else
3314 {
3315 di = find_var(lp->ll_name, NULL);
3316 if (di == NULL)
3317 ret = FAIL;
3318 else
3319 {
3320 if (lock)
3321 di->di_flags |= DI_FLAGS_LOCK;
3322 else
3323 di->di_flags &= ~DI_FLAGS_LOCK;
3324 item_lock(&di->di_tv, deep, lock);
3325 }
3326 }
3327 *name_end = cc;
3328 }
3329 else if (lp->ll_range)
3330 {
3331 listitem_T *li = lp->ll_li;
3332
3333 /* (un)lock a range of List items. */
3334 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3335 {
3336 item_lock(&li->li_tv, deep, lock);
3337 li = li->li_next;
3338 ++lp->ll_n1;
3339 }
3340 }
3341 else if (lp->ll_list != NULL)
3342 /* (un)lock a List item. */
3343 item_lock(&lp->ll_li->li_tv, deep, lock);
3344 else
3345 /* un(lock) a Dictionary item. */
3346 item_lock(&lp->ll_di->di_tv, deep, lock);
3347
3348 return ret;
3349}
3350
3351/*
3352 * Lock or unlock an item. "deep" is nr of levels to go.
3353 */
3354 static void
3355item_lock(tv, deep, lock)
3356 typval_T *tv;
3357 int deep;
3358 int lock;
3359{
3360 static int recurse = 0;
3361 list_T *l;
3362 listitem_T *li;
3363 dict_T *d;
3364 hashitem_T *hi;
3365 int todo;
3366
3367 if (recurse >= DICT_MAXNEST)
3368 {
3369 EMSG(_("E743: variable nested too deep for (un)lock"));
3370 return;
3371 }
3372 if (deep == 0)
3373 return;
3374 ++recurse;
3375
3376 /* lock/unlock the item itself */
3377 if (lock)
3378 tv->v_lock |= VAR_LOCKED;
3379 else
3380 tv->v_lock &= ~VAR_LOCKED;
3381
3382 switch (tv->v_type)
3383 {
3384 case VAR_LIST:
3385 if ((l = tv->vval.v_list) != NULL)
3386 {
3387 if (lock)
3388 l->lv_lock |= VAR_LOCKED;
3389 else
3390 l->lv_lock &= ~VAR_LOCKED;
3391 if (deep < 0 || deep > 1)
3392 /* recursive: lock/unlock the items the List contains */
3393 for (li = l->lv_first; li != NULL; li = li->li_next)
3394 item_lock(&li->li_tv, deep - 1, lock);
3395 }
3396 break;
3397 case VAR_DICT:
3398 if ((d = tv->vval.v_dict) != NULL)
3399 {
3400 if (lock)
3401 d->dv_lock |= VAR_LOCKED;
3402 else
3403 d->dv_lock &= ~VAR_LOCKED;
3404 if (deep < 0 || deep > 1)
3405 {
3406 /* recursive: lock/unlock the items the List contains */
3407 todo = d->dv_hashtab.ht_used;
3408 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3409 {
3410 if (!HASHITEM_EMPTY(hi))
3411 {
3412 --todo;
3413 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3414 }
3415 }
3416 }
3417 }
3418 }
3419 --recurse;
3420}
3421
Bram Moolenaara40058a2005-07-11 22:42:07 +00003422/*
3423 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3424 * it refers to a List or Dictionary that is locked.
3425 */
3426 static int
3427tv_islocked(tv)
3428 typval_T *tv;
3429{
3430 return (tv->v_lock & VAR_LOCKED)
3431 || (tv->v_type == VAR_LIST
3432 && tv->vval.v_list != NULL
3433 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3434 || (tv->v_type == VAR_DICT
3435 && tv->vval.v_dict != NULL
3436 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3437}
3438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3440/*
3441 * Delete all "menutrans_" variables.
3442 */
3443 void
3444del_menutrans_vars()
3445{
Bram Moolenaar33570922005-01-25 22:26:29 +00003446 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003447 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
Bram Moolenaar33570922005-01-25 22:26:29 +00003449 hash_lock(&globvarht);
3450 todo = globvarht.ht_used;
3451 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003452 {
3453 if (!HASHITEM_EMPTY(hi))
3454 {
3455 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3457 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003458 }
3459 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003460 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461}
3462#endif
3463
3464#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3465
3466/*
3467 * Local string buffer for the next two functions to store a variable name
3468 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3469 * get_user_var_name().
3470 */
3471
3472static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3473
3474static char_u *varnamebuf = NULL;
3475static int varnamebuflen = 0;
3476
3477/*
3478 * Function to concatenate a prefix and a variable name.
3479 */
3480 static char_u *
3481cat_prefix_varname(prefix, name)
3482 int prefix;
3483 char_u *name;
3484{
3485 int len;
3486
3487 len = (int)STRLEN(name) + 3;
3488 if (len > varnamebuflen)
3489 {
3490 vim_free(varnamebuf);
3491 len += 10; /* some additional space */
3492 varnamebuf = alloc(len);
3493 if (varnamebuf == NULL)
3494 {
3495 varnamebuflen = 0;
3496 return NULL;
3497 }
3498 varnamebuflen = len;
3499 }
3500 *varnamebuf = prefix;
3501 varnamebuf[1] = ':';
3502 STRCPY(varnamebuf + 2, name);
3503 return varnamebuf;
3504}
3505
3506/*
3507 * Function given to ExpandGeneric() to obtain the list of user defined
3508 * (global/buffer/window/built-in) variable names.
3509 */
3510/*ARGSUSED*/
3511 char_u *
3512get_user_var_name(xp, idx)
3513 expand_T *xp;
3514 int idx;
3515{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003516 static long_u gdone;
3517 static long_u bdone;
3518 static long_u wdone;
3519 static int vidx;
3520 static hashitem_T *hi;
3521 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
3523 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003524 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003525
3526 /* Global variables */
3527 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003529 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003530 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003531 else
3532 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003533 while (HASHITEM_EMPTY(hi))
3534 ++hi;
3535 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3536 return cat_prefix_varname('g', hi->hi_key);
3537 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003539
3540 /* b: variables */
3541 ht = &curbuf->b_vars.dv_hashtab;
3542 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003544 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003545 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003546 else
3547 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003548 while (HASHITEM_EMPTY(hi))
3549 ++hi;
3550 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003552 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003554 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 return (char_u *)"b:changedtick";
3556 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003557
3558 /* w: variables */
3559 ht = &curwin->w_vars.dv_hashtab;
3560 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003562 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003563 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003564 else
3565 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003566 while (HASHITEM_EMPTY(hi))
3567 ++hi;
3568 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003570
3571 /* v: variables */
3572 if (vidx < VV_LEN)
3573 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
3575 vim_free(varnamebuf);
3576 varnamebuf = NULL;
3577 varnamebuflen = 0;
3578 return NULL;
3579}
3580
3581#endif /* FEAT_CMDL_COMPL */
3582
3583/*
3584 * types for expressions.
3585 */
3586typedef enum
3587{
3588 TYPE_UNKNOWN = 0
3589 , TYPE_EQUAL /* == */
3590 , TYPE_NEQUAL /* != */
3591 , TYPE_GREATER /* > */
3592 , TYPE_GEQUAL /* >= */
3593 , TYPE_SMALLER /* < */
3594 , TYPE_SEQUAL /* <= */
3595 , TYPE_MATCH /* =~ */
3596 , TYPE_NOMATCH /* !~ */
3597} exptype_T;
3598
3599/*
3600 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003601 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3603 */
3604
3605/*
3606 * Handle zero level expression.
3607 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003608 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003609 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 * Return OK or FAIL.
3611 */
3612 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003613eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 char_u **nextcmd;
3617 int evaluate;
3618{
3619 int ret;
3620 char_u *p;
3621
3622 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003623 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 if (ret == FAIL || !ends_excmd(*p))
3625 {
3626 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003627 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 /*
3629 * Report the invalid expression unless the expression evaluation has
3630 * been cancelled due to an aborting error, an interrupt, or an
3631 * exception.
3632 */
3633 if (!aborting())
3634 EMSG2(_(e_invexpr2), arg);
3635 ret = FAIL;
3636 }
3637 if (nextcmd != NULL)
3638 *nextcmd = check_nextcmd(p);
3639
3640 return ret;
3641}
3642
3643/*
3644 * Handle top level expression:
3645 * expr1 ? expr0 : expr0
3646 *
3647 * "arg" must point to the first non-white of the expression.
3648 * "arg" is advanced to the next non-white after the recognized expression.
3649 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003650 * Note: "rettv.v_lock" is not set.
3651 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 * Return OK or FAIL.
3653 */
3654 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003655eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 int evaluate;
3659{
3660 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003661 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
3663 /*
3664 * Get the first variable.
3665 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003666 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 return FAIL;
3668
3669 if ((*arg)[0] == '?')
3670 {
3671 result = FALSE;
3672 if (evaluate)
3673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003674 int error = FALSE;
3675
3676 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003678 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003679 if (error)
3680 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
3682
3683 /*
3684 * Get the second variable.
3685 */
3686 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003687 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 return FAIL;
3689
3690 /*
3691 * Check for the ":".
3692 */
3693 if ((*arg)[0] != ':')
3694 {
3695 EMSG(_("E109: Missing ':' after '?'"));
3696 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003697 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 return FAIL;
3699 }
3700
3701 /*
3702 * Get the third variable.
3703 */
3704 *arg = skipwhite(*arg + 1);
3705 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3706 {
3707 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003708 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 return FAIL;
3710 }
3711 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003712 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 }
3714
3715 return OK;
3716}
3717
3718/*
3719 * Handle first level expression:
3720 * expr2 || expr2 || expr2 logical OR
3721 *
3722 * "arg" must point to the first non-white of the expression.
3723 * "arg" is advanced to the next non-white after the recognized expression.
3724 *
3725 * Return OK or FAIL.
3726 */
3727 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003728eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 int evaluate;
3732{
Bram Moolenaar33570922005-01-25 22:26:29 +00003733 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 long result;
3735 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003736 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
3738 /*
3739 * Get the first variable.
3740 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003741 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 return FAIL;
3743
3744 /*
3745 * Repeat until there is no following "||".
3746 */
3747 first = TRUE;
3748 result = FALSE;
3749 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3750 {
3751 if (evaluate && first)
3752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003753 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003755 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003756 if (error)
3757 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 first = FALSE;
3759 }
3760
3761 /*
3762 * Get the second variable.
3763 */
3764 *arg = skipwhite(*arg + 2);
3765 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3766 return FAIL;
3767
3768 /*
3769 * Compute the result.
3770 */
3771 if (evaluate && !result)
3772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003773 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003775 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003776 if (error)
3777 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779 if (evaluate)
3780 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781 rettv->v_type = VAR_NUMBER;
3782 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784 }
3785
3786 return OK;
3787}
3788
3789/*
3790 * Handle second level expression:
3791 * expr3 && expr3 && expr3 logical AND
3792 *
3793 * "arg" must point to the first non-white of the expression.
3794 * "arg" is advanced to the next non-white after the recognized expression.
3795 *
3796 * Return OK or FAIL.
3797 */
3798 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 int evaluate;
3803{
Bram Moolenaar33570922005-01-25 22:26:29 +00003804 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 long result;
3806 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003807 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808
3809 /*
3810 * Get the first variable.
3811 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003812 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 return FAIL;
3814
3815 /*
3816 * Repeat until there is no following "&&".
3817 */
3818 first = TRUE;
3819 result = TRUE;
3820 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3821 {
3822 if (evaluate && first)
3823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003824 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003826 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003827 if (error)
3828 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 first = FALSE;
3830 }
3831
3832 /*
3833 * Get the second variable.
3834 */
3835 *arg = skipwhite(*arg + 2);
3836 if (eval4(arg, &var2, evaluate && result) == FAIL)
3837 return FAIL;
3838
3839 /*
3840 * Compute the result.
3841 */
3842 if (evaluate && result)
3843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003844 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003846 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003847 if (error)
3848 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 }
3850 if (evaluate)
3851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003852 rettv->v_type = VAR_NUMBER;
3853 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
3855 }
3856
3857 return OK;
3858}
3859
3860/*
3861 * Handle third level expression:
3862 * var1 == var2
3863 * var1 =~ var2
3864 * var1 != var2
3865 * var1 !~ var2
3866 * var1 > var2
3867 * var1 >= var2
3868 * var1 < var2
3869 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003870 * var1 is var2
3871 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 *
3873 * "arg" must point to the first non-white of the expression.
3874 * "arg" is advanced to the next non-white after the recognized expression.
3875 *
3876 * Return OK or FAIL.
3877 */
3878 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003879eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 int evaluate;
3883{
Bram Moolenaar33570922005-01-25 22:26:29 +00003884 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 char_u *p;
3886 int i;
3887 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003888 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 int len = 2;
3890 long n1, n2;
3891 char_u *s1, *s2;
3892 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3893 regmatch_T regmatch;
3894 int ic;
3895 char_u *save_cpo;
3896
3897 /*
3898 * Get the first variable.
3899 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003900 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 return FAIL;
3902
3903 p = *arg;
3904 switch (p[0])
3905 {
3906 case '=': if (p[1] == '=')
3907 type = TYPE_EQUAL;
3908 else if (p[1] == '~')
3909 type = TYPE_MATCH;
3910 break;
3911 case '!': if (p[1] == '=')
3912 type = TYPE_NEQUAL;
3913 else if (p[1] == '~')
3914 type = TYPE_NOMATCH;
3915 break;
3916 case '>': if (p[1] != '=')
3917 {
3918 type = TYPE_GREATER;
3919 len = 1;
3920 }
3921 else
3922 type = TYPE_GEQUAL;
3923 break;
3924 case '<': if (p[1] != '=')
3925 {
3926 type = TYPE_SMALLER;
3927 len = 1;
3928 }
3929 else
3930 type = TYPE_SEQUAL;
3931 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003932 case 'i': if (p[1] == 's')
3933 {
3934 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3935 len = 5;
3936 if (!vim_isIDc(p[len]))
3937 {
3938 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3939 type_is = TRUE;
3940 }
3941 }
3942 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
3944
3945 /*
3946 * If there is a comparitive operator, use it.
3947 */
3948 if (type != TYPE_UNKNOWN)
3949 {
3950 /* extra question mark appended: ignore case */
3951 if (p[len] == '?')
3952 {
3953 ic = TRUE;
3954 ++len;
3955 }
3956 /* extra '#' appended: match case */
3957 else if (p[len] == '#')
3958 {
3959 ic = FALSE;
3960 ++len;
3961 }
3962 /* nothing appened: use 'ignorecase' */
3963 else
3964 ic = p_ic;
3965
3966 /*
3967 * Get the second variable.
3968 */
3969 *arg = skipwhite(p + len);
3970 if (eval5(arg, &var2, evaluate) == FAIL)
3971 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003972 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 return FAIL;
3974 }
3975
3976 if (evaluate)
3977 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003978 if (type_is && rettv->v_type != var2.v_type)
3979 {
3980 /* For "is" a different type always means FALSE, for "notis"
3981 * it means TRUE. */
3982 n1 = (type == TYPE_NEQUAL);
3983 }
3984 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3985 {
3986 if (type_is)
3987 {
3988 n1 = (rettv->v_type == var2.v_type
3989 && rettv->vval.v_list == var2.vval.v_list);
3990 if (type == TYPE_NEQUAL)
3991 n1 = !n1;
3992 }
3993 else if (rettv->v_type != var2.v_type
3994 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3995 {
3996 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003997 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003998 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003999 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004000 clear_tv(rettv);
4001 clear_tv(&var2);
4002 return FAIL;
4003 }
4004 else
4005 {
4006 /* Compare two Lists for being equal or unequal. */
4007 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4008 if (type == TYPE_NEQUAL)
4009 n1 = !n1;
4010 }
4011 }
4012
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004013 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4014 {
4015 if (type_is)
4016 {
4017 n1 = (rettv->v_type == var2.v_type
4018 && rettv->vval.v_dict == var2.vval.v_dict);
4019 if (type == TYPE_NEQUAL)
4020 n1 = !n1;
4021 }
4022 else if (rettv->v_type != var2.v_type
4023 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4024 {
4025 if (rettv->v_type != var2.v_type)
4026 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4027 else
4028 EMSG(_("E736: Invalid operation for Dictionary"));
4029 clear_tv(rettv);
4030 clear_tv(&var2);
4031 return FAIL;
4032 }
4033 else
4034 {
4035 /* Compare two Dictionaries for being equal or unequal. */
4036 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4037 if (type == TYPE_NEQUAL)
4038 n1 = !n1;
4039 }
4040 }
4041
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004042 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4043 {
4044 if (rettv->v_type != var2.v_type
4045 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4046 {
4047 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004048 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004049 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004050 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004051 clear_tv(rettv);
4052 clear_tv(&var2);
4053 return FAIL;
4054 }
4055 else
4056 {
4057 /* Compare two Funcrefs for being equal or unequal. */
4058 if (rettv->vval.v_string == NULL
4059 || var2.vval.v_string == NULL)
4060 n1 = FALSE;
4061 else
4062 n1 = STRCMP(rettv->vval.v_string,
4063 var2.vval.v_string) == 0;
4064 if (type == TYPE_NEQUAL)
4065 n1 = !n1;
4066 }
4067 }
4068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 /*
4070 * If one of the two variables is a number, compare as a number.
4071 * When using "=~" or "!~", always compare as string.
4072 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004073 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004076 n1 = get_tv_number(rettv);
4077 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 switch (type)
4079 {
4080 case TYPE_EQUAL: n1 = (n1 == n2); break;
4081 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4082 case TYPE_GREATER: n1 = (n1 > n2); break;
4083 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4084 case TYPE_SMALLER: n1 = (n1 < n2); break;
4085 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4086 case TYPE_UNKNOWN:
4087 case TYPE_MATCH:
4088 case TYPE_NOMATCH: break; /* avoid gcc warning */
4089 }
4090 }
4091 else
4092 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 s1 = get_tv_string_buf(rettv, buf1);
4094 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4096 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4097 else
4098 i = 0;
4099 n1 = FALSE;
4100 switch (type)
4101 {
4102 case TYPE_EQUAL: n1 = (i == 0); break;
4103 case TYPE_NEQUAL: n1 = (i != 0); break;
4104 case TYPE_GREATER: n1 = (i > 0); break;
4105 case TYPE_GEQUAL: n1 = (i >= 0); break;
4106 case TYPE_SMALLER: n1 = (i < 0); break;
4107 case TYPE_SEQUAL: n1 = (i <= 0); break;
4108
4109 case TYPE_MATCH:
4110 case TYPE_NOMATCH:
4111 /* avoid 'l' flag in 'cpoptions' */
4112 save_cpo = p_cpo;
4113 p_cpo = (char_u *)"";
4114 regmatch.regprog = vim_regcomp(s2,
4115 RE_MAGIC + RE_STRING);
4116 regmatch.rm_ic = ic;
4117 if (regmatch.regprog != NULL)
4118 {
4119 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4120 vim_free(regmatch.regprog);
4121 if (type == TYPE_NOMATCH)
4122 n1 = !n1;
4123 }
4124 p_cpo = save_cpo;
4125 break;
4126
4127 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4128 }
4129 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 clear_tv(rettv);
4131 clear_tv(&var2);
4132 rettv->v_type = VAR_NUMBER;
4133 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 }
4135 }
4136
4137 return OK;
4138}
4139
4140/*
4141 * Handle fourth level expression:
4142 * + number addition
4143 * - number subtraction
4144 * . string concatenation
4145 *
4146 * "arg" must point to the first non-white of the expression.
4147 * "arg" is advanced to the next non-white after the recognized expression.
4148 *
4149 * Return OK or FAIL.
4150 */
4151 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 int evaluate;
4156{
Bram Moolenaar33570922005-01-25 22:26:29 +00004157 typval_T var2;
4158 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 int op;
4160 long n1, n2;
4161 char_u *s1, *s2;
4162 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4163 char_u *p;
4164
4165 /*
4166 * Get the first variable.
4167 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004168 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 return FAIL;
4170
4171 /*
4172 * Repeat computing, until no '+', '-' or '.' is following.
4173 */
4174 for (;;)
4175 {
4176 op = **arg;
4177 if (op != '+' && op != '-' && op != '.')
4178 break;
4179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004180 if (op != '+' || rettv->v_type != VAR_LIST)
4181 {
4182 /* For "list + ...", an illegal use of the first operand as
4183 * a number cannot be determined before evaluating the 2nd
4184 * operand: if this is also a list, all is ok.
4185 * For "something . ...", "something - ..." or "non-list + ...",
4186 * we know that the first operand needs to be a string or number
4187 * without evaluating the 2nd operand. So check before to avoid
4188 * side effects after an error. */
4189 if (evaluate && get_tv_string_chk(rettv) == NULL)
4190 {
4191 clear_tv(rettv);
4192 return FAIL;
4193 }
4194 }
4195
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 /*
4197 * Get the second variable.
4198 */
4199 *arg = skipwhite(*arg + 1);
4200 if (eval6(arg, &var2, evaluate) == FAIL)
4201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 return FAIL;
4204 }
4205
4206 if (evaluate)
4207 {
4208 /*
4209 * Compute the result.
4210 */
4211 if (op == '.')
4212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004213 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4214 s2 = get_tv_string_buf_chk(&var2, buf2);
4215 if (s2 == NULL) /* type error ? */
4216 {
4217 clear_tv(rettv);
4218 clear_tv(&var2);
4219 return FAIL;
4220 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004221 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 clear_tv(rettv);
4223 rettv->v_type = VAR_STRING;
4224 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004226 else if (op == '+' && rettv->v_type == VAR_LIST
4227 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004228 {
4229 /* concatenate Lists */
4230 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4231 &var3) == FAIL)
4232 {
4233 clear_tv(rettv);
4234 clear_tv(&var2);
4235 return FAIL;
4236 }
4237 clear_tv(rettv);
4238 *rettv = var3;
4239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 else
4241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 int error = FALSE;
4243
4244 n1 = get_tv_number_chk(rettv, &error);
4245 if (error)
4246 {
4247 /* This can only happen for "list + non-list".
4248 * For "non-list + ..." or "something - ...", we returned
4249 * before evaluating the 2nd operand. */
4250 clear_tv(rettv);
4251 return FAIL;
4252 }
4253 n2 = get_tv_number_chk(&var2, &error);
4254 if (error)
4255 {
4256 clear_tv(rettv);
4257 clear_tv(&var2);
4258 return FAIL;
4259 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 if (op == '+')
4262 n1 = n1 + n2;
4263 else
4264 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 rettv->v_type = VAR_NUMBER;
4266 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 }
4271 return OK;
4272}
4273
4274/*
4275 * Handle fifth level expression:
4276 * * number multiplication
4277 * / number division
4278 * % number modulo
4279 *
4280 * "arg" must point to the first non-white of the expression.
4281 * "arg" is advanced to the next non-white after the recognized expression.
4282 *
4283 * Return OK or FAIL.
4284 */
4285 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 int evaluate;
4290{
Bram Moolenaar33570922005-01-25 22:26:29 +00004291 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 int op;
4293 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004294 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
4296 /*
4297 * Get the first variable.
4298 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 return FAIL;
4301
4302 /*
4303 * Repeat computing, until no '*', '/' or '%' is following.
4304 */
4305 for (;;)
4306 {
4307 op = **arg;
4308 if (op != '*' && op != '/' && op != '%')
4309 break;
4310
4311 if (evaluate)
4312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004313 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004315 if (error)
4316 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 }
4318 else
4319 n1 = 0;
4320
4321 /*
4322 * Get the second variable.
4323 */
4324 *arg = skipwhite(*arg + 1);
4325 if (eval7(arg, &var2, evaluate) == FAIL)
4326 return FAIL;
4327
4328 if (evaluate)
4329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004330 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004331 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 if (error)
4333 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
4335 /*
4336 * Compute the result.
4337 */
4338 if (op == '*')
4339 n1 = n1 * n2;
4340 else if (op == '/')
4341 {
4342 if (n2 == 0) /* give an error message? */
4343 n1 = 0x7fffffffL;
4344 else
4345 n1 = n1 / n2;
4346 }
4347 else
4348 {
4349 if (n2 == 0) /* give an error message? */
4350 n1 = 0;
4351 else
4352 n1 = n1 % n2;
4353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004354 rettv->v_type = VAR_NUMBER;
4355 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 }
4358
4359 return OK;
4360}
4361
4362/*
4363 * Handle sixth level expression:
4364 * number number constant
4365 * "string" string contstant
4366 * 'string' literal string contstant
4367 * &option-name option value
4368 * @r register contents
4369 * identifier variable value
4370 * function() function call
4371 * $VAR environment variable
4372 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004373 * [expr, expr] List
4374 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 *
4376 * Also handle:
4377 * ! in front logical NOT
4378 * - in front unary minus
4379 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004380 * trailing [] subscript in String or List
4381 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 *
4383 * "arg" must point to the first non-white of the expression.
4384 * "arg" is advanced to the next non-white after the recognized expression.
4385 *
4386 * Return OK or FAIL.
4387 */
4388 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004389eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 int evaluate;
4393{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 long n;
4395 int len;
4396 char_u *s;
4397 int val;
4398 char_u *start_leader, *end_leader;
4399 int ret = OK;
4400 char_u *alias;
4401
4402 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004403 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004404 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004406 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
4408 /*
4409 * Skip '!' and '-' characters. They are handled later.
4410 */
4411 start_leader = *arg;
4412 while (**arg == '!' || **arg == '-' || **arg == '+')
4413 *arg = skipwhite(*arg + 1);
4414 end_leader = *arg;
4415
4416 switch (**arg)
4417 {
4418 /*
4419 * Number constant.
4420 */
4421 case '0':
4422 case '1':
4423 case '2':
4424 case '3':
4425 case '4':
4426 case '5':
4427 case '6':
4428 case '7':
4429 case '8':
4430 case '9':
4431 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4432 *arg += len;
4433 if (evaluate)
4434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004435 rettv->v_type = VAR_NUMBER;
4436 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 break;
4439
4440 /*
4441 * String constant: "string".
4442 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004443 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 break;
4445
4446 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004447 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004449 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004450 break;
4451
4452 /*
4453 * List: [expr, expr]
4454 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 break;
4457
4458 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004459 * Dictionary: {key: val, key: val}
4460 */
4461 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4462 break;
4463
4464 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004465 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004467 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 break;
4469
4470 /*
4471 * Environment variable: $VAR.
4472 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004473 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 break;
4475
4476 /*
4477 * Register contents: @r.
4478 */
4479 case '@': ++*arg;
4480 if (evaluate)
4481 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004482 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004483 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485 if (**arg != NUL)
4486 ++*arg;
4487 break;
4488
4489 /*
4490 * nested expression: (expression).
4491 */
4492 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004493 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 if (**arg == ')')
4495 ++*arg;
4496 else if (ret == OK)
4497 {
4498 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004499 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 ret = FAIL;
4501 }
4502 break;
4503
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 break;
4506 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004507
4508 if (ret == NOTDONE)
4509 {
4510 /*
4511 * Must be a variable or function name.
4512 * Can also be a curly-braces kind of name: {expr}.
4513 */
4514 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004515 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004516 if (alias != NULL)
4517 s = alias;
4518
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004519 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 ret = FAIL;
4521 else
4522 {
4523 if (**arg == '(') /* recursive! */
4524 {
4525 /* If "s" is the name of a variable of type VAR_FUNC
4526 * use its contents. */
4527 s = deref_func_name(s, &len);
4528
4529 /* Invoke the function. */
4530 ret = get_func_tv(s, len, rettv, arg,
4531 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004532 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004533 /* Stop the expression evaluation when immediately
4534 * aborting on error, or when an interrupt occurred or
4535 * an exception was thrown but not caught. */
4536 if (aborting())
4537 {
4538 if (ret == OK)
4539 clear_tv(rettv);
4540 ret = FAIL;
4541 }
4542 }
4543 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004544 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004545 else
4546 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004547 }
4548
4549 if (alias != NULL)
4550 vim_free(alias);
4551 }
4552
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 *arg = skipwhite(*arg);
4554
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004555 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4556 * expr(expr). */
4557 if (ret == OK)
4558 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
4560 /*
4561 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4562 */
4563 if (ret == OK && evaluate && end_leader > start_leader)
4564 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004565 int error = FALSE;
4566
4567 val = get_tv_number_chk(rettv, &error);
4568 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004570 clear_tv(rettv);
4571 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004573 else
4574 {
4575 while (end_leader > start_leader)
4576 {
4577 --end_leader;
4578 if (*end_leader == '!')
4579 val = !val;
4580 else if (*end_leader == '-')
4581 val = -val;
4582 }
4583 clear_tv(rettv);
4584 rettv->v_type = VAR_NUMBER;
4585 rettv->vval.v_number = val;
4586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588
4589 return ret;
4590}
4591
4592/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 * Evaluate an "[expr]" or "[expr:expr]" index.
4594 * "*arg" points to the '['.
4595 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4596 */
4597 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004598eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004599 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004600 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004601 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004602 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004603{
4604 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004605 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004606 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004607 long len = -1;
4608 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004610 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004611
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004612 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004613 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004614 if (verbose)
4615 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616 return FAIL;
4617 }
4618
Bram Moolenaar8c711452005-01-14 21:53:12 +00004619 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004621 /*
4622 * dict.name
4623 */
4624 key = *arg + 1;
4625 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4626 ;
4627 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004628 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004630 }
4631 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004633 /*
4634 * something[idx]
4635 *
4636 * Get the (first) variable from inside the [].
4637 */
4638 *arg = skipwhite(*arg + 1);
4639 if (**arg == ':')
4640 empty1 = TRUE;
4641 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4642 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004643 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4644 {
4645 /* not a number or string */
4646 clear_tv(&var1);
4647 return FAIL;
4648 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004649
4650 /*
4651 * Get the second variable from inside the [:].
4652 */
4653 if (**arg == ':')
4654 {
4655 range = TRUE;
4656 *arg = skipwhite(*arg + 1);
4657 if (**arg == ']')
4658 empty2 = TRUE;
4659 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004661 if (!empty1)
4662 clear_tv(&var1);
4663 return FAIL;
4664 }
4665 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4666 {
4667 /* not a number or string */
4668 if (!empty1)
4669 clear_tv(&var1);
4670 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671 return FAIL;
4672 }
4673 }
4674
4675 /* Check for the ']'. */
4676 if (**arg != ']')
4677 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004678 if (verbose)
4679 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004680 clear_tv(&var1);
4681 if (range)
4682 clear_tv(&var2);
4683 return FAIL;
4684 }
4685 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004686 }
4687
4688 if (evaluate)
4689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004690 n1 = 0;
4691 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693 n1 = get_tv_number(&var1);
4694 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 }
4696 if (range)
4697 {
4698 if (empty2)
4699 n2 = -1;
4700 else
4701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004702 n2 = get_tv_number(&var2);
4703 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004704 }
4705 }
4706
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004707 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004708 {
4709 case VAR_NUMBER:
4710 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004711 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004712 len = (long)STRLEN(s);
4713 if (range)
4714 {
4715 /* The resulting variable is a substring. If the indexes
4716 * are out of range the result is empty. */
4717 if (n1 < 0)
4718 {
4719 n1 = len + n1;
4720 if (n1 < 0)
4721 n1 = 0;
4722 }
4723 if (n2 < 0)
4724 n2 = len + n2;
4725 else if (n2 >= len)
4726 n2 = len;
4727 if (n1 >= len || n2 < 0 || n1 > n2)
4728 s = NULL;
4729 else
4730 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4731 }
4732 else
4733 {
4734 /* The resulting variable is a string of a single
4735 * character. If the index is too big or negative the
4736 * result is empty. */
4737 if (n1 >= len || n1 < 0)
4738 s = NULL;
4739 else
4740 s = vim_strnsave(s + n1, 1);
4741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004742 clear_tv(rettv);
4743 rettv->v_type = VAR_STRING;
4744 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 break;
4746
4747 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004748 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 if (n1 < 0)
4750 n1 = len + n1;
4751 if (!empty1 && (n1 < 0 || n1 >= len))
4752 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004753 if (verbose)
4754 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 return FAIL;
4756 }
4757 if (range)
4758 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004759 list_T *l;
4760 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004761
4762 if (n2 < 0)
4763 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004764 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004766 if (verbose)
4767 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 return FAIL;
4769 }
4770 l = list_alloc();
4771 if (l == NULL)
4772 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004774 n1 <= n2; ++n1)
4775 {
4776 if (list_append_tv(l, &item->li_tv) == FAIL)
4777 {
4778 list_free(l);
4779 return FAIL;
4780 }
4781 item = item->li_next;
4782 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 clear_tv(rettv);
4784 rettv->v_type = VAR_LIST;
4785 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004786 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004787 }
4788 else
4789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004790 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004791 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004792 clear_tv(rettv);
4793 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004794 }
4795 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796
4797 case VAR_DICT:
4798 if (range)
4799 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004800 if (verbose)
4801 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004802 if (len == -1)
4803 clear_tv(&var1);
4804 return FAIL;
4805 }
4806 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004807 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004808
4809 if (len == -1)
4810 {
4811 key = get_tv_string(&var1);
4812 if (*key == NUL)
4813 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004814 if (verbose)
4815 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816 clear_tv(&var1);
4817 return FAIL;
4818 }
4819 }
4820
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004821 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004822
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004823 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004824 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004825 if (len == -1)
4826 clear_tv(&var1);
4827 if (item == NULL)
4828 return FAIL;
4829
4830 copy_tv(&item->di_tv, &var1);
4831 clear_tv(rettv);
4832 *rettv = var1;
4833 }
4834 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004835 }
4836 }
4837
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004838 return OK;
4839}
4840
4841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 * Get an option value.
4843 * "arg" points to the '&' or '+' before the option name.
4844 * "arg" is advanced to character after the option name.
4845 * Return OK or FAIL.
4846 */
4847 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004850 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 int evaluate;
4852{
4853 char_u *option_end;
4854 long numval;
4855 char_u *stringval;
4856 int opt_type;
4857 int c;
4858 int working = (**arg == '+'); /* has("+option") */
4859 int ret = OK;
4860 int opt_flags;
4861
4862 /*
4863 * Isolate the option name and find its value.
4864 */
4865 option_end = find_option_end(arg, &opt_flags);
4866 if (option_end == NULL)
4867 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004868 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 EMSG2(_("E112: Option name missing: %s"), *arg);
4870 return FAIL;
4871 }
4872
4873 if (!evaluate)
4874 {
4875 *arg = option_end;
4876 return OK;
4877 }
4878
4879 c = *option_end;
4880 *option_end = NUL;
4881 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004882 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 if (opt_type == -3) /* invalid name */
4885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 EMSG2(_("E113: Unknown option: %s"), *arg);
4888 ret = FAIL;
4889 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004890 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 {
4892 if (opt_type == -2) /* hidden string option */
4893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004894 rettv->v_type = VAR_STRING;
4895 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 }
4897 else if (opt_type == -1) /* hidden number option */
4898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004899 rettv->v_type = VAR_NUMBER;
4900 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902 else if (opt_type == 1) /* number option */
4903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 rettv->v_type = VAR_NUMBER;
4905 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907 else /* string option */
4908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909 rettv->v_type = VAR_STRING;
4910 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 }
4913 else if (working && (opt_type == -2 || opt_type == -1))
4914 ret = FAIL;
4915
4916 *option_end = c; /* put back for error messages */
4917 *arg = option_end;
4918
4919 return ret;
4920}
4921
4922/*
4923 * Allocate a variable for a string constant.
4924 * Return OK or FAIL.
4925 */
4926 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004927get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 int evaluate;
4931{
4932 char_u *p;
4933 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 int extra = 0;
4935
4936 /*
4937 * Find the end of the string, skipping backslashed characters.
4938 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004939 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
4941 if (*p == '\\' && p[1] != NUL)
4942 {
4943 ++p;
4944 /* A "\<x>" form occupies at least 4 characters, and produces up
4945 * to 6 characters: reserve space for 2 extra */
4946 if (*p == '<')
4947 extra += 2;
4948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950
4951 if (*p != '"')
4952 {
4953 EMSG2(_("E114: Missing quote: %s"), *arg);
4954 return FAIL;
4955 }
4956
4957 /* If only parsing, set *arg and return here */
4958 if (!evaluate)
4959 {
4960 *arg = p + 1;
4961 return OK;
4962 }
4963
4964 /*
4965 * Copy the string into allocated memory, handling backslashed
4966 * characters.
4967 */
4968 name = alloc((unsigned)(p - *arg + extra));
4969 if (name == NULL)
4970 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 rettv->v_type = VAR_STRING;
4972 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 {
4976 if (*p == '\\')
4977 {
4978 switch (*++p)
4979 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980 case 'b': *name++ = BS; ++p; break;
4981 case 'e': *name++ = ESC; ++p; break;
4982 case 'f': *name++ = FF; ++p; break;
4983 case 'n': *name++ = NL; ++p; break;
4984 case 'r': *name++ = CAR; ++p; break;
4985 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986
4987 case 'X': /* hex: "\x1", "\x12" */
4988 case 'x':
4989 case 'u': /* Unicode: "\u0023" */
4990 case 'U':
4991 if (vim_isxdigit(p[1]))
4992 {
4993 int n, nr;
4994 int c = toupper(*p);
4995
4996 if (c == 'X')
4997 n = 2;
4998 else
4999 n = 4;
5000 nr = 0;
5001 while (--n >= 0 && vim_isxdigit(p[1]))
5002 {
5003 ++p;
5004 nr = (nr << 4) + hex2nr(*p);
5005 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005006 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007#ifdef FEAT_MBYTE
5008 /* For "\u" store the number according to
5009 * 'encoding'. */
5010 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 else
5013#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 break;
5017
5018 /* octal: "\1", "\12", "\123" */
5019 case '0':
5020 case '1':
5021 case '2':
5022 case '3':
5023 case '4':
5024 case '5':
5025 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005026 case '7': *name = *p++ - '0';
5027 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 *name = (*name << 3) + *p++ - '0';
5030 if (*p >= '0' && *p <= '7')
5031 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005033 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 break;
5035
5036 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005037 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 if (extra != 0)
5039 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 break;
5042 }
5043 /* FALLTHROUGH */
5044
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 break;
5047 }
5048 }
5049 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005050 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 *arg = p + 1;
5055
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 return OK;
5057}
5058
5059/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 * Return OK or FAIL.
5062 */
5063 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 int evaluate;
5068{
5069 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005071 int reduce = 0;
5072
5073 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005075 */
5076 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5077 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005078 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005079 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005081 break;
5082 ++reduce;
5083 ++p;
5084 }
5085 }
5086
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005088 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005089 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005090 return FAIL;
5091 }
5092
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005094 if (!evaluate)
5095 {
5096 *arg = p + 1;
5097 return OK;
5098 }
5099
5100 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 */
5103 str = alloc((unsigned)((p - *arg) - reduce));
5104 if (str == NULL)
5105 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 rettv->v_type = VAR_STRING;
5107 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005108
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005112 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005113 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005114 break;
5115 ++p;
5116 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005118 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005120 *arg = p + 1;
5121
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005122 return OK;
5123}
5124
5125/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005126 * Allocate a variable for a List and fill it from "*arg".
5127 * Return OK or FAIL.
5128 */
5129 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005130get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005131 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005132 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005133 int evaluate;
5134{
Bram Moolenaar33570922005-01-25 22:26:29 +00005135 list_T *l = NULL;
5136 typval_T tv;
5137 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005138
5139 if (evaluate)
5140 {
5141 l = list_alloc();
5142 if (l == NULL)
5143 return FAIL;
5144 }
5145
5146 *arg = skipwhite(*arg + 1);
5147 while (**arg != ']' && **arg != NUL)
5148 {
5149 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5150 goto failret;
5151 if (evaluate)
5152 {
5153 item = listitem_alloc();
5154 if (item != NULL)
5155 {
5156 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005157 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 list_append(l, item);
5159 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005160 else
5161 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162 }
5163
5164 if (**arg == ']')
5165 break;
5166 if (**arg != ',')
5167 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005169 goto failret;
5170 }
5171 *arg = skipwhite(*arg + 1);
5172 }
5173
5174 if (**arg != ']')
5175 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005177failret:
5178 if (evaluate)
5179 list_free(l);
5180 return FAIL;
5181 }
5182
5183 *arg = skipwhite(*arg + 1);
5184 if (evaluate)
5185 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 rettv->v_type = VAR_LIST;
5187 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188 ++l->lv_refcount;
5189 }
5190
5191 return OK;
5192}
5193
5194/*
5195 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005196 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005197 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005198 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005199list_alloc()
5200{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005201 list_T *l;
5202
5203 l = (list_T *)alloc_clear(sizeof(list_T));
5204 if (l != NULL)
5205 {
5206 /* Prepend the list to the list of lists for garbage collection. */
5207 if (first_list != NULL)
5208 first_list->lv_used_prev = l;
5209 l->lv_used_prev = NULL;
5210 l->lv_used_next = first_list;
5211 first_list = l;
5212 }
5213 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005214}
5215
5216/*
5217 * Unreference a list: decrement the reference count and free it when it
5218 * becomes zero.
5219 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005220 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005221list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005222 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005223{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005224 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5225 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226}
5227
5228/*
5229 * Free a list, including all items it points to.
5230 * Ignores the reference count.
5231 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005232 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005233list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005234 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005235{
Bram Moolenaar33570922005-01-25 22:26:29 +00005236 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237
Bram Moolenaard9fba312005-06-26 22:34:35 +00005238 /* Avoid that recursive reference to the list frees us again. */
5239 l->lv_refcount = DEL_REFCOUNT;
5240
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005241 /* Remove the list from the list of lists for garbage collection. */
5242 if (l->lv_used_prev == NULL)
5243 first_list = l->lv_used_next;
5244 else
5245 l->lv_used_prev->lv_used_next = l->lv_used_next;
5246 if (l->lv_used_next != NULL)
5247 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5248
Bram Moolenaard9fba312005-06-26 22:34:35 +00005249 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005251 /* Remove the item before deleting it. */
5252 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 listitem_free(item);
5254 }
5255 vim_free(l);
5256}
5257
5258/*
5259 * Allocate a list item.
5260 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005261 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262listitem_alloc()
5263{
Bram Moolenaar33570922005-01-25 22:26:29 +00005264 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265}
5266
5267/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005268 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 */
5270 static void
5271listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005272 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 vim_free(item);
5276}
5277
5278/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005279 * Remove a list item from a List and free it. Also clears the value.
5280 */
5281 static void
5282listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005283 list_T *l;
5284 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005285{
5286 list_remove(l, item, item);
5287 listitem_free(item);
5288}
5289
5290/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 * Get the number of items in a list.
5292 */
5293 static long
5294list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005295 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 if (l == NULL)
5298 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005299 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300}
5301
5302/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005303 * Return TRUE when two lists have exactly the same values.
5304 */
5305 static int
5306list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005307 list_T *l1;
5308 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005309 int ic; /* ignore case for strings */
5310{
Bram Moolenaar33570922005-01-25 22:26:29 +00005311 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005312
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005313 if (list_len(l1) != list_len(l2))
5314 return FALSE;
5315
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005316 for (item1 = l1->lv_first, item2 = l2->lv_first;
5317 item1 != NULL && item2 != NULL;
5318 item1 = item1->li_next, item2 = item2->li_next)
5319 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5320 return FALSE;
5321 return item1 == NULL && item2 == NULL;
5322}
5323
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005324#if defined(FEAT_PYTHON) || defined(PROTO)
5325/*
5326 * Return the dictitem that an entry in a hashtable points to.
5327 */
5328 dictitem_T *
5329dict_lookup(hi)
5330 hashitem_T *hi;
5331{
5332 return HI2DI(hi);
5333}
5334#endif
5335
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005336/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005337 * Return TRUE when two dictionaries have exactly the same key/values.
5338 */
5339 static int
5340dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005341 dict_T *d1;
5342 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005343 int ic; /* ignore case for strings */
5344{
Bram Moolenaar33570922005-01-25 22:26:29 +00005345 hashitem_T *hi;
5346 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005347 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005348
5349 if (dict_len(d1) != dict_len(d2))
5350 return FALSE;
5351
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 todo = d1->dv_hashtab.ht_used;
5353 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005355 if (!HASHITEM_EMPTY(hi))
5356 {
5357 item2 = dict_find(d2, hi->hi_key, -1);
5358 if (item2 == NULL)
5359 return FALSE;
5360 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5361 return FALSE;
5362 --todo;
5363 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005364 }
5365 return TRUE;
5366}
5367
5368/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005369 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005370 * Compares the items just like "==" would compare them, but strings and
5371 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005372 */
5373 static int
5374tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005375 typval_T *tv1;
5376 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005377 int ic; /* ignore case */
5378{
5379 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005380 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005381
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005382 if (tv1->v_type != tv2->v_type)
5383 return FALSE;
5384
5385 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005386 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005387 case VAR_LIST:
5388 /* recursive! */
5389 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5390
5391 case VAR_DICT:
5392 /* recursive! */
5393 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5394
5395 case VAR_FUNC:
5396 return (tv1->vval.v_string != NULL
5397 && tv2->vval.v_string != NULL
5398 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5399
5400 case VAR_NUMBER:
5401 return tv1->vval.v_number == tv2->vval.v_number;
5402
5403 case VAR_STRING:
5404 s1 = get_tv_string_buf(tv1, buf1);
5405 s2 = get_tv_string_buf(tv2, buf2);
5406 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005407 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005408
5409 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005410 return TRUE;
5411}
5412
5413/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 * Locate item with index "n" in list "l" and return it.
5415 * A negative index is counted from the end; -1 is the last item.
5416 * Returns NULL when "n" is out of range.
5417 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005418 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005420 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 long n;
5422{
Bram Moolenaar33570922005-01-25 22:26:29 +00005423 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424 long idx;
5425
5426 if (l == NULL)
5427 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005428
5429 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005431 n = l->lv_len + n;
5432
5433 /* Check for index out of range. */
5434 if (n < 0 || n >= l->lv_len)
5435 return NULL;
5436
5437 /* When there is a cached index may start search from there. */
5438 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005440 if (n < l->lv_idx / 2)
5441 {
5442 /* closest to the start of the list */
5443 item = l->lv_first;
5444 idx = 0;
5445 }
5446 else if (n > (l->lv_idx + l->lv_len) / 2)
5447 {
5448 /* closest to the end of the list */
5449 item = l->lv_last;
5450 idx = l->lv_len - 1;
5451 }
5452 else
5453 {
5454 /* closest to the cached index */
5455 item = l->lv_idx_item;
5456 idx = l->lv_idx;
5457 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 }
5459 else
5460 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005461 if (n < l->lv_len / 2)
5462 {
5463 /* closest to the start of the list */
5464 item = l->lv_first;
5465 idx = 0;
5466 }
5467 else
5468 {
5469 /* closest to the end of the list */
5470 item = l->lv_last;
5471 idx = l->lv_len - 1;
5472 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005474
5475 while (n > idx)
5476 {
5477 /* search forward */
5478 item = item->li_next;
5479 ++idx;
5480 }
5481 while (n < idx)
5482 {
5483 /* search backward */
5484 item = item->li_prev;
5485 --idx;
5486 }
5487
5488 /* cache the used index */
5489 l->lv_idx = idx;
5490 l->lv_idx_item = item;
5491
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 return item;
5493}
5494
5495/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005496 * Locate "item" list "l" and return its index.
5497 * Returns -1 when "item" is not in the list.
5498 */
5499 static long
5500list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005501 list_T *l;
5502 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005503{
5504 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005505 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005506
5507 if (l == NULL)
5508 return -1;
5509 idx = 0;
5510 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5511 ++idx;
5512 if (li == NULL)
5513 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005514 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005515}
5516
5517/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 * Append item "item" to the end of list "l".
5519 */
5520 static void
5521list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005522 list_T *l;
5523 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524{
5525 if (l->lv_last == NULL)
5526 {
5527 /* empty list */
5528 l->lv_first = item;
5529 l->lv_last = item;
5530 item->li_prev = NULL;
5531 }
5532 else
5533 {
5534 l->lv_last->li_next = item;
5535 item->li_prev = l->lv_last;
5536 l->lv_last = item;
5537 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005538 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005539 item->li_next = NULL;
5540}
5541
5542/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005543 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005544 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 */
5546 static int
5547list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005548 list_T *l;
5549 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005551 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552
Bram Moolenaar05159a02005-02-26 23:04:13 +00005553 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005555 copy_tv(tv, &li->li_tv);
5556 list_append(l, li);
5557 return OK;
5558}
5559
5560/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005561 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005562 * Return FAIL when out of memory.
5563 */
5564 int
5565list_append_dict(list, dict)
5566 list_T *list;
5567 dict_T *dict;
5568{
5569 listitem_T *li = listitem_alloc();
5570
5571 if (li == NULL)
5572 return FAIL;
5573 li->li_tv.v_type = VAR_DICT;
5574 li->li_tv.v_lock = 0;
5575 li->li_tv.vval.v_dict = dict;
5576 list_append(list, li);
5577 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005578 return OK;
5579}
5580
5581/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005582 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005583 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005584 * Returns FAIL when out of memory.
5585 */
5586 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005587list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005588 list_T *l;
5589 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005590 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005591{
5592 listitem_T *li = listitem_alloc();
5593
5594 if (li == NULL)
5595 return FAIL;
5596 list_append(l, li);
5597 li->li_tv.v_type = VAR_STRING;
5598 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005599 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5600 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005601 return FAIL;
5602 return OK;
5603}
5604
5605/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005606 * Append "n" to list "l".
5607 * Returns FAIL when out of memory.
5608 */
5609 static int
5610list_append_number(l, n)
5611 list_T *l;
5612 varnumber_T n;
5613{
5614 listitem_T *li;
5615
5616 li = listitem_alloc();
5617 if (li == NULL)
5618 return FAIL;
5619 li->li_tv.v_type = VAR_NUMBER;
5620 li->li_tv.v_lock = 0;
5621 li->li_tv.vval.v_number = n;
5622 list_append(l, li);
5623 return OK;
5624}
5625
5626/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005628 * If "item" is NULL append at the end.
5629 * Return FAIL when out of memory.
5630 */
5631 static int
5632list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005633 list_T *l;
5634 typval_T *tv;
5635 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005636{
Bram Moolenaar33570922005-01-25 22:26:29 +00005637 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005638
5639 if (ni == NULL)
5640 return FAIL;
5641 copy_tv(tv, &ni->li_tv);
5642 if (item == NULL)
5643 /* Append new item at end of list. */
5644 list_append(l, ni);
5645 else
5646 {
5647 /* Insert new item before existing item. */
5648 ni->li_prev = item->li_prev;
5649 ni->li_next = item;
5650 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005651 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005652 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005653 ++l->lv_idx;
5654 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005655 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005656 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005657 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005658 l->lv_idx_item = NULL;
5659 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005660 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005661 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005662 }
5663 return OK;
5664}
5665
5666/*
5667 * Extend "l1" with "l2".
5668 * If "bef" is NULL append at the end, otherwise insert before this item.
5669 * Returns FAIL when out of memory.
5670 */
5671 static int
5672list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005673 list_T *l1;
5674 list_T *l2;
5675 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005676{
Bram Moolenaar33570922005-01-25 22:26:29 +00005677 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005678
5679 for (item = l2->lv_first; item != NULL; item = item->li_next)
5680 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5681 return FAIL;
5682 return OK;
5683}
5684
5685/*
5686 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5687 * Return FAIL when out of memory.
5688 */
5689 static int
5690list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005691 list_T *l1;
5692 list_T *l2;
5693 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005694{
Bram Moolenaar33570922005-01-25 22:26:29 +00005695 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005696
5697 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005698 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005699 if (l == NULL)
5700 return FAIL;
5701 tv->v_type = VAR_LIST;
5702 tv->vval.v_list = l;
5703
5704 /* append all items from the second list */
5705 return list_extend(l, l2, NULL);
5706}
5707
5708/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005709 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005710 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005711 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005712 * Returns NULL when out of memory.
5713 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005714 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005715list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005716 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005718 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719{
Bram Moolenaar33570922005-01-25 22:26:29 +00005720 list_T *copy;
5721 listitem_T *item;
5722 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005723
5724 if (orig == NULL)
5725 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726
5727 copy = list_alloc();
5728 if (copy != NULL)
5729 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005730 if (copyID != 0)
5731 {
5732 /* Do this before adding the items, because one of the items may
5733 * refer back to this list. */
5734 orig->lv_copyID = copyID;
5735 orig->lv_copylist = copy;
5736 }
5737 for (item = orig->lv_first; item != NULL && !got_int;
5738 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005739 {
5740 ni = listitem_alloc();
5741 if (ni == NULL)
5742 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005743 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005744 {
5745 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5746 {
5747 vim_free(ni);
5748 break;
5749 }
5750 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005751 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005752 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 list_append(copy, ni);
5754 }
5755 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005756 if (item != NULL)
5757 {
5758 list_unref(copy);
5759 copy = NULL;
5760 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761 }
5762
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005763 return copy;
5764}
5765
5766/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005767 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005768 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005770 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005771list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005772 list_T *l;
5773 listitem_T *item;
5774 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005775{
Bram Moolenaar33570922005-01-25 22:26:29 +00005776 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005778 /* notify watchers */
5779 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005781 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005782 list_fix_watch(l, ip);
5783 if (ip == item2)
5784 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005785 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005786
5787 if (item2->li_next == NULL)
5788 l->lv_last = item->li_prev;
5789 else
5790 item2->li_next->li_prev = item->li_prev;
5791 if (item->li_prev == NULL)
5792 l->lv_first = item2->li_next;
5793 else
5794 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005795 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796}
5797
5798/*
5799 * Return an allocated string with the string representation of a list.
5800 * May return NULL.
5801 */
5802 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005803list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005804 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005805 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806{
5807 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808
5809 if (tv->vval.v_list == NULL)
5810 return NULL;
5811 ga_init2(&ga, (int)sizeof(char), 80);
5812 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005813 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005814 {
5815 vim_free(ga.ga_data);
5816 return NULL;
5817 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818 ga_append(&ga, ']');
5819 ga_append(&ga, NUL);
5820 return (char_u *)ga.ga_data;
5821}
5822
5823/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005824 * Join list "l" into a string in "*gap", using separator "sep".
5825 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005826 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005827 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005828 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005829list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005830 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005831 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005832 char_u *sep;
5833 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005834 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835{
5836 int first = TRUE;
5837 char_u *tofree;
5838 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005839 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005840 char_u *s;
5841
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005842 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 {
5844 if (first)
5845 first = FALSE;
5846 else
5847 ga_concat(gap, sep);
5848
5849 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005850 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005852 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 if (s != NULL)
5854 ga_concat(gap, s);
5855 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005856 if (s == NULL)
5857 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005859 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860}
5861
5862/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005863 * Garbage collection for lists and dictionaries.
5864 *
5865 * We use reference counts to be able to free most items right away when they
5866 * are no longer used. But for composite items it's possible that it becomes
5867 * unused while the reference count is > 0: When there is a recursive
5868 * reference. Example:
5869 * :let l = [1, 2, 3]
5870 * :let d = {9: l}
5871 * :let l[1] = d
5872 *
5873 * Since this is quite unusual we handle this with garbage collection: every
5874 * once in a while find out which lists and dicts are not referenced from any
5875 * variable.
5876 *
5877 * Here is a good reference text about garbage collection (refers to Python
5878 * but it applies to all reference-counting mechanisms):
5879 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005880 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005881
5882/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005883 * Do garbage collection for lists and dicts.
5884 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005885 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005886 int
5887garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005888{
5889 dict_T *dd;
5890 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005891 int copyID = ++current_copyID;
5892 buf_T *buf;
5893 win_T *wp;
5894 int i;
5895 funccall_T *fc;
5896 int did_free = FALSE;
5897
5898 /*
5899 * 1. Go through all accessible variables and mark all lists and dicts
5900 * with copyID.
5901 */
5902 /* script-local variables */
5903 for (i = 1; i <= ga_scripts.ga_len; ++i)
5904 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5905
5906 /* buffer-local variables */
5907 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5908 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5909
5910 /* window-local variables */
5911 FOR_ALL_WINDOWS(wp)
5912 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5913
5914 /* global variables */
5915 set_ref_in_ht(&globvarht, copyID);
5916
5917 /* function-local variables */
5918 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5919 {
5920 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5921 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5922 }
5923
5924 /*
5925 * 2. Go through the list of dicts and free items without the copyID.
5926 */
5927 for (dd = first_dict; dd != NULL; )
5928 if (dd->dv_copyID != copyID)
5929 {
5930 dict_free(dd);
5931 did_free = TRUE;
5932
5933 /* restart, next dict may also have been freed */
5934 dd = first_dict;
5935 }
5936 else
5937 dd = dd->dv_used_next;
5938
5939 /*
5940 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005941 * But don't free a list that has a watcher (used in a for loop), these
5942 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005943 */
5944 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005945 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005946 {
5947 list_free(ll);
5948 did_free = TRUE;
5949
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005950 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005951 ll = first_list;
5952 }
5953 else
5954 ll = ll->lv_used_next;
5955
5956 return did_free;
5957}
5958
5959/*
5960 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5961 */
5962 static void
5963set_ref_in_ht(ht, copyID)
5964 hashtab_T *ht;
5965 int copyID;
5966{
5967 int todo;
5968 hashitem_T *hi;
5969
5970 todo = ht->ht_used;
5971 for (hi = ht->ht_array; todo > 0; ++hi)
5972 if (!HASHITEM_EMPTY(hi))
5973 {
5974 --todo;
5975 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5976 }
5977}
5978
5979/*
5980 * Mark all lists and dicts referenced through list "l" with "copyID".
5981 */
5982 static void
5983set_ref_in_list(l, copyID)
5984 list_T *l;
5985 int copyID;
5986{
5987 listitem_T *li;
5988
5989 for (li = l->lv_first; li != NULL; li = li->li_next)
5990 set_ref_in_item(&li->li_tv, copyID);
5991}
5992
5993/*
5994 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5995 */
5996 static void
5997set_ref_in_item(tv, copyID)
5998 typval_T *tv;
5999 int copyID;
6000{
6001 dict_T *dd;
6002 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006003
6004 switch (tv->v_type)
6005 {
6006 case VAR_DICT:
6007 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006008 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006009 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006010 /* Didn't see this dict yet. */
6011 dd->dv_copyID = copyID;
6012 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006013 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006014 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006015
6016 case VAR_LIST:
6017 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006018 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006019 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006020 /* Didn't see this list yet. */
6021 ll->lv_copyID = copyID;
6022 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006023 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006024 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006025 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006026 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006027}
6028
6029/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006030 * Allocate an empty header for a dictionary.
6031 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006032 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006033dict_alloc()
6034{
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006036
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006038 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006039 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006040 /* Add the list to the hashtable for garbage collection. */
6041 if (first_dict != NULL)
6042 first_dict->dv_used_prev = d;
6043 d->dv_used_next = first_dict;
6044 d->dv_used_prev = NULL;
6045
Bram Moolenaar33570922005-01-25 22:26:29 +00006046 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006047 d->dv_lock = 0;
6048 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006049 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006050 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006051 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006052}
6053
6054/*
6055 * Unreference a Dictionary: decrement the reference count and free it when it
6056 * becomes zero.
6057 */
6058 static void
6059dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006061{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006062 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6063 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006064}
6065
6066/*
6067 * Free a Dictionary, including all items it contains.
6068 * Ignores the reference count.
6069 */
6070 static void
6071dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006073{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006074 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006075 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006076 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006077
Bram Moolenaard9fba312005-06-26 22:34:35 +00006078 /* Avoid that recursive reference to the dict frees us again. */
6079 d->dv_refcount = DEL_REFCOUNT;
6080
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006081 /* Remove the dict from the list of dicts for garbage collection. */
6082 if (d->dv_used_prev == NULL)
6083 first_dict = d->dv_used_next;
6084 else
6085 d->dv_used_prev->dv_used_next = d->dv_used_next;
6086 if (d->dv_used_next != NULL)
6087 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6088
6089 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006090 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 todo = d->dv_hashtab.ht_used;
6092 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006093 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006094 if (!HASHITEM_EMPTY(hi))
6095 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006096 /* Remove the item before deleting it, just in case there is
6097 * something recursive causing trouble. */
6098 di = HI2DI(hi);
6099 hash_remove(&d->dv_hashtab, hi);
6100 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006101 --todo;
6102 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006103 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006105 vim_free(d);
6106}
6107
6108/*
6109 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006110 * The "key" is copied to the new item.
6111 * Note that the value of the item "di_tv" still needs to be initialized!
6112 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006113 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006114 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006115dictitem_alloc(key)
6116 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006117{
Bram Moolenaar33570922005-01-25 22:26:29 +00006118 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006119
Bram Moolenaar33570922005-01-25 22:26:29 +00006120 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006121 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006122 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006123 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006124 di->di_flags = 0;
6125 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006126 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006127}
6128
6129/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006130 * Make a copy of a Dictionary item.
6131 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006132 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006133dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006134 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006135{
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006137
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006139 if (di != NULL)
6140 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006141 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006142 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006143 copy_tv(&org->di_tv, &di->di_tv);
6144 }
6145 return di;
6146}
6147
6148/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006149 * Remove item "item" from Dictionary "dict" and free it.
6150 */
6151 static void
6152dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 dict_T *dict;
6154 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155{
Bram Moolenaar33570922005-01-25 22:26:29 +00006156 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006157
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006159 if (HASHITEM_EMPTY(hi))
6160 EMSG2(_(e_intern2), "dictitem_remove()");
6161 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006162 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006163 dictitem_free(item);
6164}
6165
6166/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006167 * Free a dict item. Also clears the value.
6168 */
6169 static void
6170dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006171 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006172{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006173 clear_tv(&item->di_tv);
6174 vim_free(item);
6175}
6176
6177/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006178 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6179 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006180 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006181 * Returns NULL when out of memory.
6182 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006183 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006184dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006185 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006186 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006187 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006188{
Bram Moolenaar33570922005-01-25 22:26:29 +00006189 dict_T *copy;
6190 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006191 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006193
6194 if (orig == NULL)
6195 return NULL;
6196
6197 copy = dict_alloc();
6198 if (copy != NULL)
6199 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006200 if (copyID != 0)
6201 {
6202 orig->dv_copyID = copyID;
6203 orig->dv_copydict = copy;
6204 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006205 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006206 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006207 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006208 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006209 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006210 --todo;
6211
6212 di = dictitem_alloc(hi->hi_key);
6213 if (di == NULL)
6214 break;
6215 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006216 {
6217 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6218 copyID) == FAIL)
6219 {
6220 vim_free(di);
6221 break;
6222 }
6223 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006224 else
6225 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6226 if (dict_add(copy, di) == FAIL)
6227 {
6228 dictitem_free(di);
6229 break;
6230 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006231 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006232 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006233
Bram Moolenaare9a41262005-01-15 22:18:47 +00006234 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006235 if (todo > 0)
6236 {
6237 dict_unref(copy);
6238 copy = NULL;
6239 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006240 }
6241
6242 return copy;
6243}
6244
6245/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006246 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006247 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006248 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006249 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006250dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006251 dict_T *d;
6252 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006253{
Bram Moolenaar33570922005-01-25 22:26:29 +00006254 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006255}
6256
Bram Moolenaar8c711452005-01-14 21:53:12 +00006257/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006258 * Add a number or string entry to dictionary "d".
6259 * When "str" is NULL use number "nr", otherwise use "str".
6260 * Returns FAIL when out of memory and when key already exists.
6261 */
6262 int
6263dict_add_nr_str(d, key, nr, str)
6264 dict_T *d;
6265 char *key;
6266 long nr;
6267 char_u *str;
6268{
6269 dictitem_T *item;
6270
6271 item = dictitem_alloc((char_u *)key);
6272 if (item == NULL)
6273 return FAIL;
6274 item->di_tv.v_lock = 0;
6275 if (str == NULL)
6276 {
6277 item->di_tv.v_type = VAR_NUMBER;
6278 item->di_tv.vval.v_number = nr;
6279 }
6280 else
6281 {
6282 item->di_tv.v_type = VAR_STRING;
6283 item->di_tv.vval.v_string = vim_strsave(str);
6284 }
6285 if (dict_add(d, item) == FAIL)
6286 {
6287 dictitem_free(item);
6288 return FAIL;
6289 }
6290 return OK;
6291}
6292
6293/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006294 * Get the number of items in a Dictionary.
6295 */
6296 static long
6297dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006298 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006299{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006300 if (d == NULL)
6301 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006302 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006303}
6304
6305/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006306 * Find item "key[len]" in Dictionary "d".
6307 * If "len" is negative use strlen(key).
6308 * Returns NULL when not found.
6309 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006311dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006313 char_u *key;
6314 int len;
6315{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006316#define AKEYLEN 200
6317 char_u buf[AKEYLEN];
6318 char_u *akey;
6319 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006321
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006322 if (len < 0)
6323 akey = key;
6324 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006325 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006326 tofree = akey = vim_strnsave(key, len);
6327 if (akey == NULL)
6328 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006329 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006330 else
6331 {
6332 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006333 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006334 akey = buf;
6335 }
6336
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006338 vim_free(tofree);
6339 if (HASHITEM_EMPTY(hi))
6340 return NULL;
6341 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342}
6343
6344/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006345 * Get a string item from a dictionary in allocated memory.
6346 * Returns NULL if the entry doesn't exist or out of memory.
6347 */
6348 char_u *
6349get_dict_string(d, key)
6350 dict_T *d;
6351 char_u *key;
6352{
6353 dictitem_T *di;
6354
6355 di = dict_find(d, key, -1);
6356 if (di == NULL)
6357 return NULL;
6358 return vim_strsave(get_tv_string(&di->di_tv));
6359}
6360
6361/*
6362 * Get a number item from a dictionary.
6363 * Returns 0 if the entry doesn't exist or out of memory.
6364 */
6365 long
6366get_dict_number(d, key)
6367 dict_T *d;
6368 char_u *key;
6369{
6370 dictitem_T *di;
6371
6372 di = dict_find(d, key, -1);
6373 if (di == NULL)
6374 return 0;
6375 return get_tv_number(&di->di_tv);
6376}
6377
6378/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006379 * Return an allocated string with the string representation of a Dictionary.
6380 * May return NULL.
6381 */
6382 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006383dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006385 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006386{
6387 garray_T ga;
6388 int first = TRUE;
6389 char_u *tofree;
6390 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006391 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006392 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006393 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006394 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006395
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006396 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006397 return NULL;
6398 ga_init2(&ga, (int)sizeof(char), 80);
6399 ga_append(&ga, '{');
6400
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006402 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006403 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006404 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006405 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006406 --todo;
6407
6408 if (first)
6409 first = FALSE;
6410 else
6411 ga_concat(&ga, (char_u *)", ");
6412
6413 tofree = string_quote(hi->hi_key, FALSE);
6414 if (tofree != NULL)
6415 {
6416 ga_concat(&ga, tofree);
6417 vim_free(tofree);
6418 }
6419 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006420 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006421 if (s != NULL)
6422 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006423 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006424 if (s == NULL)
6425 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006426 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006427 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006428 if (todo > 0)
6429 {
6430 vim_free(ga.ga_data);
6431 return NULL;
6432 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006433
6434 ga_append(&ga, '}');
6435 ga_append(&ga, NUL);
6436 return (char_u *)ga.ga_data;
6437}
6438
6439/*
6440 * Allocate a variable for a Dictionary and fill it from "*arg".
6441 * Return OK or FAIL. Returns NOTDONE for {expr}.
6442 */
6443 static int
6444get_dict_tv(arg, rettv, evaluate)
6445 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006446 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006447 int evaluate;
6448{
Bram Moolenaar33570922005-01-25 22:26:29 +00006449 dict_T *d = NULL;
6450 typval_T tvkey;
6451 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006452 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006453 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006454 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006455 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006456
6457 /*
6458 * First check if it's not a curly-braces thing: {expr}.
6459 * Must do this without evaluating, otherwise a function may be called
6460 * twice. Unfortunately this means we need to call eval1() twice for the
6461 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006462 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006463 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006464 if (*start != '}')
6465 {
6466 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6467 return FAIL;
6468 if (*start == '}')
6469 return NOTDONE;
6470 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006471
6472 if (evaluate)
6473 {
6474 d = dict_alloc();
6475 if (d == NULL)
6476 return FAIL;
6477 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006478 tvkey.v_type = VAR_UNKNOWN;
6479 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006480
6481 *arg = skipwhite(*arg + 1);
6482 while (**arg != '}' && **arg != NUL)
6483 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006484 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006485 goto failret;
6486 if (**arg != ':')
6487 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006488 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006489 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006490 goto failret;
6491 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006492 key = get_tv_string_buf_chk(&tvkey, buf);
6493 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006495 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6496 if (key != NULL)
6497 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006498 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 goto failret;
6500 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006501
6502 *arg = skipwhite(*arg + 1);
6503 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6504 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006505 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506 goto failret;
6507 }
6508 if (evaluate)
6509 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006510 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006511 if (item != NULL)
6512 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006513 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006514 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006515 clear_tv(&tv);
6516 goto failret;
6517 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006518 item = dictitem_alloc(key);
6519 clear_tv(&tvkey);
6520 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006521 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006522 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006523 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006524 if (dict_add(d, item) == FAIL)
6525 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006526 }
6527 }
6528
6529 if (**arg == '}')
6530 break;
6531 if (**arg != ',')
6532 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006533 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006534 goto failret;
6535 }
6536 *arg = skipwhite(*arg + 1);
6537 }
6538
6539 if (**arg != '}')
6540 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006541 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006542failret:
6543 if (evaluate)
6544 dict_free(d);
6545 return FAIL;
6546 }
6547
6548 *arg = skipwhite(*arg + 1);
6549 if (evaluate)
6550 {
6551 rettv->v_type = VAR_DICT;
6552 rettv->vval.v_dict = d;
6553 ++d->dv_refcount;
6554 }
6555
6556 return OK;
6557}
6558
Bram Moolenaar8c711452005-01-14 21:53:12 +00006559/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560 * Return a string with the string representation of a variable.
6561 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006563 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006564 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565 * May return NULL;
6566 */
6567 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006568echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006569 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006571 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006572 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006573{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006574 static int recurse = 0;
6575 char_u *r = NULL;
6576
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006578 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006579 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006580 *tofree = NULL;
6581 return NULL;
6582 }
6583 ++recurse;
6584
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585 switch (tv->v_type)
6586 {
6587 case VAR_FUNC:
6588 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006589 r = tv->vval.v_string;
6590 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006591
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006593 if (tv->vval.v_list == NULL)
6594 {
6595 *tofree = NULL;
6596 r = NULL;
6597 }
6598 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6599 {
6600 *tofree = NULL;
6601 r = (char_u *)"[...]";
6602 }
6603 else
6604 {
6605 tv->vval.v_list->lv_copyID = copyID;
6606 *tofree = list2string(tv, copyID);
6607 r = *tofree;
6608 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006609 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006610
Bram Moolenaar8c711452005-01-14 21:53:12 +00006611 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006612 if (tv->vval.v_dict == NULL)
6613 {
6614 *tofree = NULL;
6615 r = NULL;
6616 }
6617 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6618 {
6619 *tofree = NULL;
6620 r = (char_u *)"{...}";
6621 }
6622 else
6623 {
6624 tv->vval.v_dict->dv_copyID = copyID;
6625 *tofree = dict2string(tv, copyID);
6626 r = *tofree;
6627 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006628 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006629
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006630 case VAR_STRING:
6631 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006632 *tofree = NULL;
6633 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006634 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006635
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006636 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006637 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006638 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006639 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006640
6641 --recurse;
6642 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006643}
6644
6645/*
6646 * Return a string with the string representation of a variable.
6647 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6648 * "numbuf" is used for a number.
6649 * Puts quotes around strings, so that they can be parsed back by eval().
6650 * May return NULL;
6651 */
6652 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006653tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006654 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006655 char_u **tofree;
6656 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006657 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006658{
6659 switch (tv->v_type)
6660 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006661 case VAR_FUNC:
6662 *tofree = string_quote(tv->vval.v_string, TRUE);
6663 return *tofree;
6664 case VAR_STRING:
6665 *tofree = string_quote(tv->vval.v_string, FALSE);
6666 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006667 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006668 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006669 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006670 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006671 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006672 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006673 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006674 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675}
6676
6677/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006678 * Return string "str" in ' quotes, doubling ' characters.
6679 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006680 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006681 */
6682 static char_u *
6683string_quote(str, function)
6684 char_u *str;
6685 int function;
6686{
Bram Moolenaar33570922005-01-25 22:26:29 +00006687 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006688 char_u *p, *r, *s;
6689
Bram Moolenaar33570922005-01-25 22:26:29 +00006690 len = (function ? 13 : 3);
6691 if (str != NULL)
6692 {
6693 len += STRLEN(str);
6694 for (p = str; *p != NUL; mb_ptr_adv(p))
6695 if (*p == '\'')
6696 ++len;
6697 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006698 s = r = alloc(len);
6699 if (r != NULL)
6700 {
6701 if (function)
6702 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006703 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006704 r += 10;
6705 }
6706 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006707 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006708 if (str != NULL)
6709 for (p = str; *p != NUL; )
6710 {
6711 if (*p == '\'')
6712 *r++ = '\'';
6713 MB_COPY_CHAR(p, r);
6714 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006715 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006716 if (function)
6717 *r++ = ')';
6718 *r++ = NUL;
6719 }
6720 return s;
6721}
6722
6723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 * Get the value of an environment variable.
6725 * "arg" is pointing to the '$'. It is advanced to after the name.
6726 * If the environment variable was not set, silently assume it is empty.
6727 * Always return OK.
6728 */
6729 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006730get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 int evaluate;
6734{
6735 char_u *string = NULL;
6736 int len;
6737 int cc;
6738 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006739 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740
6741 ++*arg;
6742 name = *arg;
6743 len = get_env_len(arg);
6744 if (evaluate)
6745 {
6746 if (len != 0)
6747 {
6748 cc = name[len];
6749 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006750 /* first try vim_getenv(), fast for normal environment vars */
6751 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006753 {
6754 if (!mustfree)
6755 string = vim_strsave(string);
6756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 else
6758 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006759 if (mustfree)
6760 vim_free(string);
6761
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 /* next try expanding things like $VIM and ${HOME} */
6763 string = expand_env_save(name - 1);
6764 if (string != NULL && *string == '$')
6765 {
6766 vim_free(string);
6767 string = NULL;
6768 }
6769 }
6770 name[len] = cc;
6771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006772 rettv->v_type = VAR_STRING;
6773 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 }
6775
6776 return OK;
6777}
6778
6779/*
6780 * Array with names and number of arguments of all internal functions
6781 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6782 */
6783static struct fst
6784{
6785 char *f_name; /* function name */
6786 char f_min_argc; /* minimal number of arguments */
6787 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006788 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006789 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790} functions[] =
6791{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006792 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {"append", 2, 2, f_append},
6794 {"argc", 0, 0, f_argc},
6795 {"argidx", 0, 0, f_argidx},
6796 {"argv", 1, 1, f_argv},
6797 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006798 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {"bufexists", 1, 1, f_bufexists},
6800 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6801 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6802 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6803 {"buflisted", 1, 1, f_buflisted},
6804 {"bufloaded", 1, 1, f_bufloaded},
6805 {"bufname", 1, 1, f_bufname},
6806 {"bufnr", 1, 1, f_bufnr},
6807 {"bufwinnr", 1, 1, f_bufwinnr},
6808 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006809 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006810 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {"char2nr", 1, 1, f_char2nr},
6812 {"cindent", 1, 1, f_cindent},
6813 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006814#if defined(FEAT_INS_EXPAND)
6815 {"complete_add", 1, 1, f_complete_add},
6816 {"complete_check", 0, 0, f_complete_check},
6817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006819 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006820 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 {"cscope_connection",0,3, f_cscope_connection},
6822 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006823 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 {"delete", 1, 1, f_delete},
6825 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006826 {"diff_filler", 1, 1, f_diff_filler},
6827 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006828 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006830 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 {"eventhandler", 0, 0, f_eventhandler},
6832 {"executable", 1, 1, f_executable},
6833 {"exists", 1, 1, f_exists},
6834 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006835 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6837 {"filereadable", 1, 1, f_filereadable},
6838 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006839 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006840 {"finddir", 1, 3, f_finddir},
6841 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 {"fnamemodify", 2, 2, f_fnamemodify},
6843 {"foldclosed", 1, 1, f_foldclosed},
6844 {"foldclosedend", 1, 1, f_foldclosedend},
6845 {"foldlevel", 1, 1, f_foldlevel},
6846 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006847 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006849 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006851 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006852 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {"getbufvar", 2, 2, f_getbufvar},
6854 {"getchar", 0, 1, f_getchar},
6855 {"getcharmod", 0, 0, f_getcharmod},
6856 {"getcmdline", 0, 0, f_getcmdline},
6857 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006858 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006860 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006861 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {"getfsize", 1, 1, f_getfsize},
6863 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006864 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006865 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006866 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006867 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006868 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 {"getregtype", 0, 1, f_getregtype},
6870 {"getwinposx", 0, 0, f_getwinposx},
6871 {"getwinposy", 0, 0, f_getwinposy},
6872 {"getwinvar", 2, 2, f_getwinvar},
6873 {"glob", 1, 1, f_glob},
6874 {"globpath", 2, 2, f_globpath},
6875 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006876 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {"hasmapto", 1, 2, f_hasmapto},
6878 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6879 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6880 {"histadd", 2, 2, f_histadd},
6881 {"histdel", 1, 2, f_histdel},
6882 {"histget", 1, 2, f_histget},
6883 {"histnr", 1, 1, f_histnr},
6884 {"hlID", 1, 1, f_hlID},
6885 {"hlexists", 1, 1, f_hlexists},
6886 {"hostname", 0, 0, f_hostname},
6887 {"iconv", 3, 3, f_iconv},
6888 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006889 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006890 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006892 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 {"inputrestore", 0, 0, f_inputrestore},
6894 {"inputsave", 0, 0, f_inputsave},
6895 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006896 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006898 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006899 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006900 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006901 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006903 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {"libcall", 3, 3, f_libcall},
6905 {"libcallnr", 3, 3, f_libcallnr},
6906 {"line", 1, 1, f_line},
6907 {"line2byte", 1, 1, f_line2byte},
6908 {"lispindent", 1, 1, f_lispindent},
6909 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006910 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 {"maparg", 1, 2, f_maparg},
6912 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006913 {"match", 2, 4, f_match},
6914 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006915 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006916 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006917 {"max", 1, 1, f_max},
6918 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006919#ifdef vim_mkdir
6920 {"mkdir", 1, 3, f_mkdir},
6921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 {"mode", 0, 0, f_mode},
6923 {"nextnonblank", 1, 1, f_nextnonblank},
6924 {"nr2char", 1, 1, f_nr2char},
6925 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006926 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006927 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006928 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 {"remote_expr", 2, 3, f_remote_expr},
6930 {"remote_foreground", 1, 1, f_remote_foreground},
6931 {"remote_peek", 1, 2, f_remote_peek},
6932 {"remote_read", 1, 1, f_remote_read},
6933 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006934 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006936 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006938 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006940 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {"searchpair", 3, 5, f_searchpair},
6942 {"server2client", 2, 2, f_server2client},
6943 {"serverlist", 0, 0, f_serverlist},
6944 {"setbufvar", 3, 3, f_setbufvar},
6945 {"setcmdpos", 1, 1, f_setcmdpos},
6946 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006947 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006948 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 {"setreg", 2, 3, f_setreg},
6950 {"setwinvar", 3, 3, f_setwinvar},
6951 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006952 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006953 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006954 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006955 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006956 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957#ifdef HAVE_STRFTIME
6958 {"strftime", 1, 2, f_strftime},
6959#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006960 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006961 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {"strlen", 1, 1, f_strlen},
6963 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006964 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {"strtrans", 1, 1, f_strtrans},
6966 {"submatch", 1, 1, f_submatch},
6967 {"substitute", 4, 4, f_substitute},
6968 {"synID", 3, 3, f_synID},
6969 {"synIDattr", 2, 3, f_synIDattr},
6970 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006971 {"system", 1, 2, f_system},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006972 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006973 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006975 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 {"tolower", 1, 1, f_tolower},
6977 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006978 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006980 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 {"virtcol", 1, 1, f_virtcol},
6982 {"visualmode", 0, 1, f_visualmode},
6983 {"winbufnr", 1, 1, f_winbufnr},
6984 {"wincol", 0, 0, f_wincol},
6985 {"winheight", 1, 1, f_winheight},
6986 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006987 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {"winrestcmd", 0, 0, f_winrestcmd},
6989 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006990 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991};
6992
6993#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6994
6995/*
6996 * Function given to ExpandGeneric() to obtain the list of internal
6997 * or user defined function names.
6998 */
6999 char_u *
7000get_function_name(xp, idx)
7001 expand_T *xp;
7002 int idx;
7003{
7004 static int intidx = -1;
7005 char_u *name;
7006
7007 if (idx == 0)
7008 intidx = -1;
7009 if (intidx < 0)
7010 {
7011 name = get_user_func_name(xp, idx);
7012 if (name != NULL)
7013 return name;
7014 }
7015 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7016 {
7017 STRCPY(IObuff, functions[intidx].f_name);
7018 STRCAT(IObuff, "(");
7019 if (functions[intidx].f_max_argc == 0)
7020 STRCAT(IObuff, ")");
7021 return IObuff;
7022 }
7023
7024 return NULL;
7025}
7026
7027/*
7028 * Function given to ExpandGeneric() to obtain the list of internal or
7029 * user defined variable or function names.
7030 */
7031/*ARGSUSED*/
7032 char_u *
7033get_expr_name(xp, idx)
7034 expand_T *xp;
7035 int idx;
7036{
7037 static int intidx = -1;
7038 char_u *name;
7039
7040 if (idx == 0)
7041 intidx = -1;
7042 if (intidx < 0)
7043 {
7044 name = get_function_name(xp, idx);
7045 if (name != NULL)
7046 return name;
7047 }
7048 return get_user_var_name(xp, ++intidx);
7049}
7050
7051#endif /* FEAT_CMDL_COMPL */
7052
7053/*
7054 * Find internal function in table above.
7055 * Return index, or -1 if not found
7056 */
7057 static int
7058find_internal_func(name)
7059 char_u *name; /* name of the function */
7060{
7061 int first = 0;
7062 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7063 int cmp;
7064 int x;
7065
7066 /*
7067 * Find the function name in the table. Binary search.
7068 */
7069 while (first <= last)
7070 {
7071 x = first + ((unsigned)(last - first) >> 1);
7072 cmp = STRCMP(name, functions[x].f_name);
7073 if (cmp < 0)
7074 last = x - 1;
7075 else if (cmp > 0)
7076 first = x + 1;
7077 else
7078 return x;
7079 }
7080 return -1;
7081}
7082
7083/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007084 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7085 * name it contains, otherwise return "name".
7086 */
7087 static char_u *
7088deref_func_name(name, lenp)
7089 char_u *name;
7090 int *lenp;
7091{
Bram Moolenaar33570922005-01-25 22:26:29 +00007092 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007093 int cc;
7094
7095 cc = name[*lenp];
7096 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007097 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007098 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007099 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007100 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007102 {
7103 *lenp = 0;
7104 return (char_u *)""; /* just in case */
7105 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 *lenp = STRLEN(v->di_tv.vval.v_string);
7107 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007108 }
7109
7110 return name;
7111}
7112
7113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 * Allocate a variable for the result of a function.
7115 * Return OK or FAIL.
7116 */
7117 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007118get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7119 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 char_u *name; /* name of the function */
7121 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 char_u **arg; /* argument, pointing to the '(' */
7124 linenr_T firstline; /* first line of range */
7125 linenr_T lastline; /* last line of range */
7126 int *doesrange; /* return: function handled range */
7127 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007128 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129{
7130 char_u *argp;
7131 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007132 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 int argcount = 0; /* number of arguments found */
7134
7135 /*
7136 * Get the arguments.
7137 */
7138 argp = *arg;
7139 while (argcount < MAX_FUNC_ARGS)
7140 {
7141 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7142 if (*argp == ')' || *argp == ',' || *argp == NUL)
7143 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7145 {
7146 ret = FAIL;
7147 break;
7148 }
7149 ++argcount;
7150 if (*argp != ',')
7151 break;
7152 }
7153 if (*argp == ')')
7154 ++argp;
7155 else
7156 ret = FAIL;
7157
7158 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007159 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007160 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007162 {
7163 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007164 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007165 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007166 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
7169 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007170 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171
7172 *arg = skipwhite(argp);
7173 return ret;
7174}
7175
7176
7177/*
7178 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007179 * Return OK when the function can't be called, FAIL otherwise.
7180 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 */
7182 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007183call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007184 doesrange, 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; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 linenr_T firstline; /* first line of range */
7191 linenr_T lastline; /* last line of range */
7192 int *doesrange; /* return: function handled range */
7193 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197#define ERROR_UNKNOWN 0
7198#define ERROR_TOOMANY 1
7199#define ERROR_TOOFEW 2
7200#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007201#define ERROR_DICT 4
7202#define ERROR_NONE 5
7203#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 int error = ERROR_NONE;
7205 int i;
7206 int llen;
7207 ufunc_T *fp;
7208 int cc;
7209#define FLEN_FIXED 40
7210 char_u fname_buf[FLEN_FIXED + 1];
7211 char_u *fname;
7212
7213 /*
7214 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7215 * Change <SNR>123_name() to K_SNR 123_name().
7216 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7217 */
7218 cc = name[len];
7219 name[len] = NUL;
7220 llen = eval_fname_script(name);
7221 if (llen > 0)
7222 {
7223 fname_buf[0] = K_SPECIAL;
7224 fname_buf[1] = KS_EXTRA;
7225 fname_buf[2] = (int)KE_SNR;
7226 i = 3;
7227 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7228 {
7229 if (current_SID <= 0)
7230 error = ERROR_SCRIPT;
7231 else
7232 {
7233 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7234 i = (int)STRLEN(fname_buf);
7235 }
7236 }
7237 if (i + STRLEN(name + llen) < FLEN_FIXED)
7238 {
7239 STRCPY(fname_buf + i, name + llen);
7240 fname = fname_buf;
7241 }
7242 else
7243 {
7244 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7245 if (fname == NULL)
7246 error = ERROR_OTHER;
7247 else
7248 {
7249 mch_memmove(fname, fname_buf, (size_t)i);
7250 STRCPY(fname + i, name + llen);
7251 }
7252 }
7253 }
7254 else
7255 fname = name;
7256
7257 *doesrange = FALSE;
7258
7259
7260 /* execute the function if no errors detected and executing */
7261 if (evaluate && error == ERROR_NONE)
7262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007263 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 error = ERROR_UNKNOWN;
7265
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007266 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 {
7268 /*
7269 * User defined function.
7270 */
7271 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007272
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007274 /* Trigger FuncUndefined event, may load the function. */
7275 if (fp == NULL
7276 && apply_autocmds(EVENT_FUNCUNDEFINED,
7277 fname, fname, TRUE, NULL)
7278 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007280 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 fp = find_func(fname);
7282 }
7283#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007284 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007285 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007286 {
7287 /* loaded a package, search for the function again */
7288 fp = find_func(fname);
7289 }
7290
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 if (fp != NULL)
7292 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007293 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007295 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007297 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007299 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 else
7302 {
7303 /*
7304 * Call the user function.
7305 * Save and restore search patterns, script variables and
7306 * redo buffer.
7307 */
7308 save_search_patterns();
7309 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007310 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007311 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007312 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007313 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7314 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7315 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007316 /* Function was unreferenced while being used, free it
7317 * now. */
7318 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 restoreRedobuff();
7320 restore_search_patterns();
7321 error = ERROR_NONE;
7322 }
7323 }
7324 }
7325 else
7326 {
7327 /*
7328 * Find the function name in the table, call its implementation.
7329 */
7330 i = find_internal_func(fname);
7331 if (i >= 0)
7332 {
7333 if (argcount < functions[i].f_min_argc)
7334 error = ERROR_TOOFEW;
7335 else if (argcount > functions[i].f_max_argc)
7336 error = ERROR_TOOMANY;
7337 else
7338 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007339 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007340 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 error = ERROR_NONE;
7342 }
7343 }
7344 }
7345 /*
7346 * The function call (or "FuncUndefined" autocommand sequence) might
7347 * have been aborted by an error, an interrupt, or an explicitly thrown
7348 * exception that has not been caught so far. This situation can be
7349 * tested for by calling aborting(). For an error in an internal
7350 * function or for the "E132" error in call_user_func(), however, the
7351 * throw point at which the "force_abort" flag (temporarily reset by
7352 * emsg()) is normally updated has not been reached yet. We need to
7353 * update that flag first to make aborting() reliable.
7354 */
7355 update_force_abort();
7356 }
7357 if (error == ERROR_NONE)
7358 ret = OK;
7359
7360 /*
7361 * Report an error unless the argument evaluation or function call has been
7362 * cancelled due to an aborting error, an interrupt, or an exception.
7363 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007364 if (!aborting())
7365 {
7366 switch (error)
7367 {
7368 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007369 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007370 break;
7371 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007372 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007373 break;
7374 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007375 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376 name);
7377 break;
7378 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007379 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007380 name);
7381 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007383 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007384 name);
7385 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 }
7387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388
7389 name[len] = cc;
7390 if (fname != name && fname != fname_buf)
7391 vim_free(fname);
7392
7393 return ret;
7394}
7395
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007396/*
7397 * Give an error message with a function name. Handle <SNR> things.
7398 */
7399 static void
7400emsg_funcname(msg, name)
7401 char *msg;
7402 char_u *name;
7403{
7404 char_u *p;
7405
7406 if (*name == K_SPECIAL)
7407 p = concat_str((char_u *)"<SNR>", name + 3);
7408 else
7409 p = name;
7410 EMSG2(_(msg), p);
7411 if (p != name)
7412 vim_free(p);
7413}
7414
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415/*********************************************
7416 * Implementation of the built-in functions
7417 */
7418
7419/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007420 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 */
7422 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007423f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007424 typval_T *argvars;
7425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426{
Bram Moolenaar33570922005-01-25 22:26:29 +00007427 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007429 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007430 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007432 if ((l = argvars[0].vval.v_list) != NULL
7433 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7434 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007435 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007436 }
7437 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007438 EMSG(_(e_listreq));
7439}
7440
7441/*
7442 * "append(lnum, string/list)" function
7443 */
7444 static void
7445f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 typval_T *argvars;
7447 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007448{
7449 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007450 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 list_T *l = NULL;
7452 listitem_T *li = NULL;
7453 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007454 long added = 0;
7455
Bram Moolenaar0d660222005-01-07 21:51:51 +00007456 lnum = get_tv_lnum(argvars);
7457 if (lnum >= 0
7458 && lnum <= curbuf->b_ml.ml_line_count
7459 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007460 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007461 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007462 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007463 l = argvars[1].vval.v_list;
7464 if (l == NULL)
7465 return;
7466 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007467 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007468 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007469 for (;;)
7470 {
7471 if (l == NULL)
7472 tv = &argvars[1]; /* append a string */
7473 else if (li == NULL)
7474 break; /* end of list */
7475 else
7476 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007477 line = get_tv_string_chk(tv);
7478 if (line == NULL) /* type error */
7479 {
7480 rettv->vval.v_number = 1; /* Failed */
7481 break;
7482 }
7483 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007484 ++added;
7485 if (l == NULL)
7486 break;
7487 li = li->li_next;
7488 }
7489
7490 appended_lines_mark(lnum, added);
7491 if (curwin->w_cursor.lnum > lnum)
7492 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007494 else
7495 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496}
7497
7498/*
7499 * "argc()" function
7500 */
7501/* ARGSUSED */
7502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007503f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007504 typval_T *argvars;
7505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007507 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508}
7509
7510/*
7511 * "argidx()" function
7512 */
7513/* ARGSUSED */
7514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007515f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 typval_T *argvars;
7517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007519 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520}
7521
7522/*
7523 * "argv(nr)" function
7524 */
7525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007526f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 typval_T *argvars;
7528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529{
7530 int idx;
7531
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007532 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007534 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007536 rettv->vval.v_string = NULL;
7537 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538}
7539
7540/*
7541 * "browse(save, title, initdir, default)" function
7542 */
7543/* ARGSUSED */
7544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007545f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 typval_T *argvars;
7547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548{
7549#ifdef FEAT_BROWSE
7550 int save;
7551 char_u *title;
7552 char_u *initdir;
7553 char_u *defname;
7554 char_u buf[NUMBUFLEN];
7555 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007556 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007558 save = get_tv_number_chk(&argvars[0], &error);
7559 title = get_tv_string_chk(&argvars[1]);
7560 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7561 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007563 if (error || title == NULL || initdir == NULL || defname == NULL)
7564 rettv->vval.v_string = NULL;
7565 else
7566 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007567 do_browse(save ? BROWSE_SAVE : 0,
7568 title, defname, NULL, initdir, NULL, curbuf);
7569#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007570 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007571#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007572 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007573}
7574
7575/*
7576 * "browsedir(title, initdir)" function
7577 */
7578/* ARGSUSED */
7579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007580f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007581 typval_T *argvars;
7582 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007583{
7584#ifdef FEAT_BROWSE
7585 char_u *title;
7586 char_u *initdir;
7587 char_u buf[NUMBUFLEN];
7588
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007589 title = get_tv_string_chk(&argvars[0]);
7590 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007592 if (title == NULL || initdir == NULL)
7593 rettv->vval.v_string = NULL;
7594 else
7595 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007596 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007598 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601}
7602
Bram Moolenaar33570922005-01-25 22:26:29 +00007603static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007604
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605/*
7606 * Find a buffer by number or exact name.
7607 */
7608 static buf_T *
7609find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007610 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611{
7612 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007614 if (avar->v_type == VAR_NUMBER)
7615 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007616 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007618 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007619 if (buf == NULL)
7620 {
7621 /* No full path name match, try a match with a URL or a "nofile"
7622 * buffer, these don't use the full path. */
7623 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7624 if (buf->b_fname != NULL
7625 && (path_with_url(buf->b_fname)
7626#ifdef FEAT_QUICKFIX
7627 || bt_nofile(buf)
7628#endif
7629 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007630 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007631 break;
7632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 }
7634 return buf;
7635}
7636
7637/*
7638 * "bufexists(expr)" function
7639 */
7640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007642 typval_T *argvars;
7643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007645 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646}
7647
7648/*
7649 * "buflisted(expr)" function
7650 */
7651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007653 typval_T *argvars;
7654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655{
7656 buf_T *buf;
7657
7658 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007659 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660}
7661
7662/*
7663 * "bufloaded(expr)" function
7664 */
7665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007666f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007667 typval_T *argvars;
7668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669{
7670 buf_T *buf;
7671
7672 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674}
7675
Bram Moolenaar33570922005-01-25 22:26:29 +00007676static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007677
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678/*
7679 * Get buffer by number or pattern.
7680 */
7681 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007682get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007683 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 int save_magic;
7687 char_u *save_cpo;
7688 buf_T *buf;
7689
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007690 if (tv->v_type == VAR_NUMBER)
7691 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007692 if (tv->v_type != VAR_STRING)
7693 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 if (name == NULL || *name == NUL)
7695 return curbuf;
7696 if (name[0] == '$' && name[1] == NUL)
7697 return lastbuf;
7698
7699 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7700 save_magic = p_magic;
7701 p_magic = TRUE;
7702 save_cpo = p_cpo;
7703 p_cpo = (char_u *)"";
7704
7705 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7706 TRUE, FALSE));
7707
7708 p_magic = save_magic;
7709 p_cpo = save_cpo;
7710
7711 /* If not found, try expanding the name, like done for bufexists(). */
7712 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007713 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714
7715 return buf;
7716}
7717
7718/*
7719 * "bufname(expr)" function
7720 */
7721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007722f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007723 typval_T *argvars;
7724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
7726 buf_T *buf;
7727
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007728 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007730 buf = get_buf_tv(&argvars[0]);
7731 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007733 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007735 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 --emsg_off;
7737}
7738
7739/*
7740 * "bufnr(expr)" function
7741 */
7742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007743f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007744 typval_T *argvars;
7745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
7747 buf_T *buf;
7748
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007749 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007751 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007753 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007755 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 --emsg_off;
7757}
7758
7759/*
7760 * "bufwinnr(nr)" function
7761 */
7762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007763f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 typval_T *argvars;
7765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
7767#ifdef FEAT_WINDOWS
7768 win_T *wp;
7769 int winnr = 0;
7770#endif
7771 buf_T *buf;
7772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007773 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007775 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776#ifdef FEAT_WINDOWS
7777 for (wp = firstwin; wp; wp = wp->w_next)
7778 {
7779 ++winnr;
7780 if (wp->w_buffer == buf)
7781 break;
7782 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007783 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007785 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786#endif
7787 --emsg_off;
7788}
7789
7790/*
7791 * "byte2line(byte)" function
7792 */
7793/*ARGSUSED*/
7794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007795f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 typval_T *argvars;
7797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798{
7799#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007800 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801#else
7802 long boff = 0;
7803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007804 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007806 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007808 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 (linenr_T)0, &boff);
7810#endif
7811}
7812
7813/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007814 * "byteidx()" function
7815 */
7816/*ARGSUSED*/
7817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007818f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007819 typval_T *argvars;
7820 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007821{
7822#ifdef FEAT_MBYTE
7823 char_u *t;
7824#endif
7825 char_u *str;
7826 long idx;
7827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007828 str = get_tv_string_chk(&argvars[0]);
7829 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007830 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007831 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007832 return;
7833
7834#ifdef FEAT_MBYTE
7835 t = str;
7836 for ( ; idx > 0; idx--)
7837 {
7838 if (*t == NUL) /* EOL reached */
7839 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007840 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007842 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007843#else
7844 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007845 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007846#endif
7847}
7848
7849/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007850 * "call(func, arglist)" function
7851 */
7852 static void
7853f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007854 typval_T *argvars;
7855 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007856{
7857 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007859 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007861 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007863
7864 rettv->vval.v_number = 0;
7865 if (argvars[1].v_type != VAR_LIST)
7866 {
7867 EMSG(_(e_listreq));
7868 return;
7869 }
7870 if (argvars[1].vval.v_list == NULL)
7871 return;
7872
7873 if (argvars[0].v_type == VAR_FUNC)
7874 func = argvars[0].vval.v_string;
7875 else
7876 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007877 if (*func == NUL)
7878 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007879
Bram Moolenaare9a41262005-01-15 22:18:47 +00007880 if (argvars[2].v_type != VAR_UNKNOWN)
7881 {
7882 if (argvars[2].v_type != VAR_DICT)
7883 {
7884 EMSG(_(e_dictreq));
7885 return;
7886 }
7887 selfdict = argvars[2].vval.v_dict;
7888 }
7889
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007890 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7891 item = item->li_next)
7892 {
7893 if (argc == MAX_FUNC_ARGS)
7894 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007895 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007896 break;
7897 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007898 /* Make a copy of each argument. This is needed to be able to set
7899 * v_lock to VAR_FIXED in the copy without changing the original list.
7900 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007901 copy_tv(&item->li_tv, &argv[argc++]);
7902 }
7903
7904 if (item == NULL)
7905 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007906 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7907 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007908
7909 /* Free the arguments. */
7910 while (argc > 0)
7911 clear_tv(&argv[--argc]);
7912}
7913
7914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 * "char2nr(string)" function
7916 */
7917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007918f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007919 typval_T *argvars;
7920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921{
7922#ifdef FEAT_MBYTE
7923 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007924 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 else
7926#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928}
7929
7930/*
7931 * "cindent(lnum)" function
7932 */
7933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007934f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007935 typval_T *argvars;
7936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
7938#ifdef FEAT_CINDENT
7939 pos_T pos;
7940 linenr_T lnum;
7941
7942 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007943 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7945 {
7946 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007947 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 curwin->w_cursor = pos;
7949 }
7950 else
7951#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007952 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953}
7954
7955/*
7956 * "col(string)" function
7957 */
7958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007959f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007960 typval_T *argvars;
7961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962{
7963 colnr_T col = 0;
7964 pos_T *fp;
7965
7966 fp = var2fpos(&argvars[0], FALSE);
7967 if (fp != NULL)
7968 {
7969 if (fp->col == MAXCOL)
7970 {
7971 /* '> can be MAXCOL, get the length of the line then */
7972 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7973 col = STRLEN(ml_get(fp->lnum)) + 1;
7974 else
7975 col = MAXCOL;
7976 }
7977 else
7978 {
7979 col = fp->col + 1;
7980#ifdef FEAT_VIRTUALEDIT
7981 /* col(".") when the cursor is on the NUL at the end of the line
7982 * because of "coladd" can be seen as an extra column. */
7983 if (virtual_active() && fp == &curwin->w_cursor)
7984 {
7985 char_u *p = ml_get_cursor();
7986
7987 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7988 curwin->w_virtcol - curwin->w_cursor.coladd))
7989 {
7990# ifdef FEAT_MBYTE
7991 int l;
7992
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007993 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 col += l;
7995# else
7996 if (*p != NUL && p[1] == NUL)
7997 ++col;
7998# endif
7999 }
8000 }
8001#endif
8002 }
8003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008004 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005}
8006
Bram Moolenaar572cb562005-08-05 21:35:02 +00008007#if defined(FEAT_INS_EXPAND)
8008/*
8009 * "complete_add()" function
8010 */
8011/*ARGSUSED*/
8012 static void
8013f_complete_add(argvars, rettv)
8014 typval_T *argvars;
8015 typval_T *rettv;
8016{
8017 char_u *s;
8018
8019 s = get_tv_string_chk(&argvars[0]);
8020 if (s != NULL)
8021 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
8022}
8023
8024/*
8025 * "complete_check()" function
8026 */
8027/*ARGSUSED*/
8028 static void
8029f_complete_check(argvars, rettv)
8030 typval_T *argvars;
8031 typval_T *rettv;
8032{
8033 int saved = RedrawingDisabled;
8034
8035 RedrawingDisabled = 0;
8036 ins_compl_check_keys(0);
8037 rettv->vval.v_number = compl_interrupted;
8038 RedrawingDisabled = saved;
8039}
8040#endif
8041
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042/*
8043 * "confirm(message, buttons[, default [, type]])" function
8044 */
8045/*ARGSUSED*/
8046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008047f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 typval_T *argvars;
8049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050{
8051#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8052 char_u *message;
8053 char_u *buttons = NULL;
8054 char_u buf[NUMBUFLEN];
8055 char_u buf2[NUMBUFLEN];
8056 int def = 1;
8057 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008058 char_u *typestr;
8059 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008061 message = get_tv_string_chk(&argvars[0]);
8062 if (message == NULL)
8063 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008064 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008066 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8067 if (buttons == NULL)
8068 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008069 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008071 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008072 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008074 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8075 if (typestr == NULL)
8076 error = TRUE;
8077 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008079 switch (TOUPPER_ASC(*typestr))
8080 {
8081 case 'E': type = VIM_ERROR; break;
8082 case 'Q': type = VIM_QUESTION; break;
8083 case 'I': type = VIM_INFO; break;
8084 case 'W': type = VIM_WARNING; break;
8085 case 'G': type = VIM_GENERIC; break;
8086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 }
8088 }
8089 }
8090 }
8091
8092 if (buttons == NULL || *buttons == NUL)
8093 buttons = (char_u *)_("&Ok");
8094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008095 if (error)
8096 rettv->vval.v_number = 0;
8097 else
8098 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 def, NULL);
8100#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008101 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102#endif
8103}
8104
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008105/*
8106 * "copy()" function
8107 */
8108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008109f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008110 typval_T *argvars;
8111 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008112{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008113 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008114}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115
8116/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008117 * "count()" function
8118 */
8119 static void
8120f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008121 typval_T *argvars;
8122 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008123{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008124 long n = 0;
8125 int ic = FALSE;
8126
Bram Moolenaare9a41262005-01-15 22:18:47 +00008127 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008128 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008129 listitem_T *li;
8130 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008131 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008132
Bram Moolenaare9a41262005-01-15 22:18:47 +00008133 if ((l = argvars[0].vval.v_list) != NULL)
8134 {
8135 li = l->lv_first;
8136 if (argvars[2].v_type != VAR_UNKNOWN)
8137 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008138 int error = FALSE;
8139
8140 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008141 if (argvars[3].v_type != VAR_UNKNOWN)
8142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008143 idx = get_tv_number_chk(&argvars[3], &error);
8144 if (!error)
8145 {
8146 li = list_find(l, idx);
8147 if (li == NULL)
8148 EMSGN(_(e_listidx), idx);
8149 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008150 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008151 if (error)
8152 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008153 }
8154
8155 for ( ; li != NULL; li = li->li_next)
8156 if (tv_equal(&li->li_tv, &argvars[1], ic))
8157 ++n;
8158 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008159 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008160 else if (argvars[0].v_type == VAR_DICT)
8161 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008162 int todo;
8163 dict_T *d;
8164 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008165
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008166 if ((d = argvars[0].vval.v_dict) != NULL)
8167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008168 int error = FALSE;
8169
Bram Moolenaare9a41262005-01-15 22:18:47 +00008170 if (argvars[2].v_type != VAR_UNKNOWN)
8171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008172 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008173 if (argvars[3].v_type != VAR_UNKNOWN)
8174 EMSG(_(e_invarg));
8175 }
8176
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008177 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008178 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008179 {
8180 if (!HASHITEM_EMPTY(hi))
8181 {
8182 --todo;
8183 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8184 ++n;
8185 }
8186 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008187 }
8188 }
8189 else
8190 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008191 rettv->vval.v_number = n;
8192}
8193
8194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8196 *
8197 * Checks the existence of a cscope connection.
8198 */
8199/*ARGSUSED*/
8200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008201f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008202 typval_T *argvars;
8203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204{
8205#ifdef FEAT_CSCOPE
8206 int num = 0;
8207 char_u *dbpath = NULL;
8208 char_u *prepend = NULL;
8209 char_u buf[NUMBUFLEN];
8210
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008211 if (argvars[0].v_type != VAR_UNKNOWN
8212 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008214 num = (int)get_tv_number(&argvars[0]);
8215 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008216 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008217 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 }
8219
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008220 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008222 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223#endif
8224}
8225
8226/*
8227 * "cursor(lnum, col)" function
8228 *
8229 * Moves the cursor to the specified line and column
8230 */
8231/*ARGSUSED*/
8232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008233f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008234 typval_T *argvars;
8235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237 long line, col;
8238
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008239 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008240 col = get_tv_number_chk(&argvars[1], NULL);
8241 if (line < 0 || col < 0)
8242 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 if (line > 0)
8244 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 if (col > 0)
8246 curwin->w_cursor.col = col - 1;
8247#ifdef FEAT_VIRTUALEDIT
8248 curwin->w_cursor.coladd = 0;
8249#endif
8250
8251 /* Make sure the cursor is in a valid position. */
8252 check_cursor();
8253#ifdef FEAT_MBYTE
8254 /* Correct cursor for multi-byte character. */
8255 if (has_mbyte)
8256 mb_adjust_cursor();
8257#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008258
8259 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260}
8261
8262/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008263 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 */
8265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008266f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008267 typval_T *argvars;
8268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008270 int noref = 0;
8271
8272 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008273 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008274 if (noref < 0 || noref > 1)
8275 EMSG(_(e_invarg));
8276 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008277 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278}
8279
8280/*
8281 * "delete()" function
8282 */
8283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008284f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008285 typval_T *argvars;
8286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287{
8288 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008289 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008291 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292}
8293
8294/*
8295 * "did_filetype()" function
8296 */
8297/*ARGSUSED*/
8298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008299f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008300 typval_T *argvars;
8301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
8303#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008304 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008306 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307#endif
8308}
8309
8310/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008311 * "diff_filler()" function
8312 */
8313/*ARGSUSED*/
8314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008315f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008316 typval_T *argvars;
8317 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008318{
8319#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008320 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008321#endif
8322}
8323
8324/*
8325 * "diff_hlID()" function
8326 */
8327/*ARGSUSED*/
8328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008329f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008330 typval_T *argvars;
8331 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008332{
8333#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008334 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008335 static linenr_T prev_lnum = 0;
8336 static int changedtick = 0;
8337 static int fnum = 0;
8338 static int change_start = 0;
8339 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008340 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008341 int filler_lines;
8342 int col;
8343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008344 if (lnum < 0) /* ignore type error in {lnum} arg */
8345 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008346 if (lnum != prev_lnum
8347 || changedtick != curbuf->b_changedtick
8348 || fnum != curbuf->b_fnum)
8349 {
8350 /* New line, buffer, change: need to get the values. */
8351 filler_lines = diff_check(curwin, lnum);
8352 if (filler_lines < 0)
8353 {
8354 if (filler_lines == -1)
8355 {
8356 change_start = MAXCOL;
8357 change_end = -1;
8358 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8359 hlID = HLF_ADD; /* added line */
8360 else
8361 hlID = HLF_CHD; /* changed line */
8362 }
8363 else
8364 hlID = HLF_ADD; /* added line */
8365 }
8366 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008367 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008368 prev_lnum = lnum;
8369 changedtick = curbuf->b_changedtick;
8370 fnum = curbuf->b_fnum;
8371 }
8372
8373 if (hlID == HLF_CHD || hlID == HLF_TXD)
8374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008375 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008376 if (col >= change_start && col <= change_end)
8377 hlID = HLF_TXD; /* changed text */
8378 else
8379 hlID = HLF_CHD; /* changed line */
8380 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008381 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008382#endif
8383}
8384
8385/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008386 * "empty({expr})" function
8387 */
8388 static void
8389f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008390 typval_T *argvars;
8391 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008392{
8393 int n;
8394
8395 switch (argvars[0].v_type)
8396 {
8397 case VAR_STRING:
8398 case VAR_FUNC:
8399 n = argvars[0].vval.v_string == NULL
8400 || *argvars[0].vval.v_string == NUL;
8401 break;
8402 case VAR_NUMBER:
8403 n = argvars[0].vval.v_number == 0;
8404 break;
8405 case VAR_LIST:
8406 n = argvars[0].vval.v_list == NULL
8407 || argvars[0].vval.v_list->lv_first == NULL;
8408 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008409 case VAR_DICT:
8410 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008411 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008412 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008413 default:
8414 EMSG2(_(e_intern2), "f_empty()");
8415 n = 0;
8416 }
8417
8418 rettv->vval.v_number = n;
8419}
8420
8421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 * "escape({string}, {chars})" function
8423 */
8424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008425f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008426 typval_T *argvars;
8427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428{
8429 char_u buf[NUMBUFLEN];
8430
Bram Moolenaar758711c2005-02-02 23:11:38 +00008431 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8432 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008433 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434}
8435
8436/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008437 * "eval()" function
8438 */
8439/*ARGSUSED*/
8440 static void
8441f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008442 typval_T *argvars;
8443 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008444{
8445 char_u *s;
8446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008447 s = get_tv_string_chk(&argvars[0]);
8448 if (s != NULL)
8449 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008451 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8452 {
8453 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008454 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008455 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008456 else if (*s != NUL)
8457 EMSG(_(e_trailing));
8458}
8459
8460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 * "eventhandler()" function
8462 */
8463/*ARGSUSED*/
8464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008465f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008466 typval_T *argvars;
8467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008469 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470}
8471
8472/*
8473 * "executable()" function
8474 */
8475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008477 typval_T *argvars;
8478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008480 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481}
8482
8483/*
8484 * "exists()" function
8485 */
8486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008487f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008488 typval_T *argvars;
8489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490{
8491 char_u *p;
8492 char_u *name;
8493 int n = FALSE;
8494 int len = 0;
8495
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008496 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 if (*p == '$') /* environment variable */
8498 {
8499 /* first try "normal" environment variables (fast) */
8500 if (mch_getenv(p + 1) != NULL)
8501 n = TRUE;
8502 else
8503 {
8504 /* try expanding things like $VIM and ${HOME} */
8505 p = expand_env_save(p);
8506 if (p != NULL && *p != '$')
8507 n = TRUE;
8508 vim_free(p);
8509 }
8510 }
8511 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008512 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 else if (*p == '*') /* internal or user defined function */
8514 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008515 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 }
8517 else if (*p == ':')
8518 {
8519 n = cmd_exists(p + 1);
8520 }
8521 else if (*p == '#')
8522 {
8523#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008524 if (p[1] == '#')
8525 n = autocmd_supported(p + 2);
8526 else
8527 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528#endif
8529 }
8530 else /* internal variable */
8531 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008532 char_u *tofree;
8533 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008535 /* get_name_len() takes care of expanding curly braces */
8536 name = p;
8537 len = get_name_len(&p, &tofree, TRUE, FALSE);
8538 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008540 if (tofree != NULL)
8541 name = tofree;
8542 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8543 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008545 /* handle d.key, l[idx], f(expr) */
8546 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8547 if (n)
8548 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 }
8550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008552 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 }
8554
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008555 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556}
8557
8558/*
8559 * "expand()" function
8560 */
8561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008562f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008563 typval_T *argvars;
8564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565{
8566 char_u *s;
8567 int len;
8568 char_u *errormsg;
8569 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8570 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008571 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008573 rettv->v_type = VAR_STRING;
8574 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 if (*s == '%' || *s == '#' || *s == '<')
8576 {
8577 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008578 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 --emsg_off;
8580 }
8581 else
8582 {
8583 /* When the optional second argument is non-zero, don't remove matches
8584 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008585 if (argvars[1].v_type != VAR_UNKNOWN
8586 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008588 if (!error)
8589 {
8590 ExpandInit(&xpc);
8591 xpc.xp_context = EXPAND_FILES;
8592 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8593 ExpandCleanup(&xpc);
8594 }
8595 else
8596 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 }
8598}
8599
8600/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008601 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008602 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008603 */
8604 static void
8605f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008606 typval_T *argvars;
8607 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008608{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008609 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008610 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008611 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008612 list_T *l1, *l2;
8613 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008614 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008615 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008616
Bram Moolenaare9a41262005-01-15 22:18:47 +00008617 l1 = argvars[0].vval.v_list;
8618 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008619 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8620 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008621 {
8622 if (argvars[2].v_type != VAR_UNKNOWN)
8623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008624 before = get_tv_number_chk(&argvars[2], &error);
8625 if (error)
8626 return; /* type error; errmsg already given */
8627
Bram Moolenaar758711c2005-02-02 23:11:38 +00008628 if (before == l1->lv_len)
8629 item = NULL;
8630 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008631 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008632 item = list_find(l1, before);
8633 if (item == NULL)
8634 {
8635 EMSGN(_(e_listidx), before);
8636 return;
8637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008638 }
8639 }
8640 else
8641 item = NULL;
8642 list_extend(l1, l2, item);
8643
Bram Moolenaare9a41262005-01-15 22:18:47 +00008644 copy_tv(&argvars[0], rettv);
8645 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008646 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008647 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8648 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008649 dict_T *d1, *d2;
8650 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008651 char_u *action;
8652 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008653 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008654 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008655
8656 d1 = argvars[0].vval.v_dict;
8657 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008658 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8659 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008660 {
8661 /* Check the third argument. */
8662 if (argvars[2].v_type != VAR_UNKNOWN)
8663 {
8664 static char *(av[]) = {"keep", "force", "error"};
8665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008666 action = get_tv_string_chk(&argvars[2]);
8667 if (action == NULL)
8668 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008669 for (i = 0; i < 3; ++i)
8670 if (STRCMP(action, av[i]) == 0)
8671 break;
8672 if (i == 3)
8673 {
8674 EMSGN(_(e_invarg2), action);
8675 return;
8676 }
8677 }
8678 else
8679 action = (char_u *)"force";
8680
8681 /* Go over all entries in the second dict and add them to the
8682 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008683 todo = d2->dv_hashtab.ht_used;
8684 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008685 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008686 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008687 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008688 --todo;
8689 di1 = dict_find(d1, hi2->hi_key, -1);
8690 if (di1 == NULL)
8691 {
8692 di1 = dictitem_copy(HI2DI(hi2));
8693 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8694 dictitem_free(di1);
8695 }
8696 else if (*action == 'e')
8697 {
8698 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8699 break;
8700 }
8701 else if (*action == 'f')
8702 {
8703 clear_tv(&di1->di_tv);
8704 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8705 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008706 }
8707 }
8708
Bram Moolenaare9a41262005-01-15 22:18:47 +00008709 copy_tv(&argvars[0], rettv);
8710 }
8711 }
8712 else
8713 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008714}
8715
8716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 * "filereadable()" function
8718 */
8719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008720f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008721 typval_T *argvars;
8722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723{
8724 FILE *fd;
8725 char_u *p;
8726 int n;
8727
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008728 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8730 {
8731 n = TRUE;
8732 fclose(fd);
8733 }
8734 else
8735 n = FALSE;
8736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738}
8739
8740/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008741 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 * rights to write into.
8743 */
8744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008745f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008746 typval_T *argvars;
8747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008749 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750}
8751
Bram Moolenaar33570922005-01-25 22:26:29 +00008752static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008753
8754 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008755findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008756 typval_T *argvars;
8757 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008758 int dir;
8759{
8760#ifdef FEAT_SEARCHPATH
8761 char_u *fname;
8762 char_u *fresult = NULL;
8763 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8764 char_u *p;
8765 char_u pathbuf[NUMBUFLEN];
8766 int count = 1;
8767 int first = TRUE;
8768
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008770
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008771 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008773 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8774 if (p == NULL)
8775 count = -1; /* error */
8776 else
8777 {
8778 if (*p != NUL)
8779 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008781 if (argvars[2].v_type != VAR_UNKNOWN)
8782 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8783 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008784 }
8785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008786 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008788 do
8789 {
8790 vim_free(fresult);
8791 fresult = find_file_in_path_option(first ? fname : NULL,
8792 first ? (int)STRLEN(fname) : 0,
8793 0, first, path, dir, NULL);
8794 first = FALSE;
8795 } while (--count > 0 && fresult != NULL);
8796 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008797
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008798 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008799#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008800 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008801#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008803}
8804
Bram Moolenaar33570922005-01-25 22:26:29 +00008805static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8806static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008807
8808/*
8809 * Implementation of map() and filter().
8810 */
8811 static void
8812filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008813 typval_T *argvars;
8814 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008815 int map;
8816{
8817 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008818 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008819 listitem_T *li, *nli;
8820 list_T *l = NULL;
8821 dictitem_T *di;
8822 hashtab_T *ht;
8823 hashitem_T *hi;
8824 dict_T *d = NULL;
8825 typval_T save_val;
8826 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008827 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008828 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008829 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar280f1262006-01-30 00:14:18 +00008830 int save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008831
8832 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008833 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008834 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008835 if ((l = argvars[0].vval.v_list) == NULL
8836 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008837 return;
8838 }
8839 else if (argvars[0].v_type == VAR_DICT)
8840 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008841 if ((d = argvars[0].vval.v_dict) == NULL
8842 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008843 return;
8844 }
8845 else
8846 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008847 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008848 return;
8849 }
8850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008851 expr = get_tv_string_buf_chk(&argvars[1], buf);
8852 /* On type errors, the preceding call has already displayed an error
8853 * message. Avoid a misleading error message for an empty string that
8854 * was not passed as argument. */
8855 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008857 prepare_vimvar(VV_VAL, &save_val);
8858 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008859
Bram Moolenaar280f1262006-01-30 00:14:18 +00008860 /* We reset "called_emsg" to be able to detect whether an error
8861 * occurred during evaluation of the expression. "did_emsg" can't be
8862 * used, because it is reset when calling a function. */
8863 save_called_emsg = called_emsg;
8864 called_emsg = FALSE;
8865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008866 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008868 prepare_vimvar(VV_KEY, &save_key);
8869 vimvars[VV_KEY].vv_type = VAR_STRING;
8870
8871 ht = &d->dv_hashtab;
8872 hash_lock(ht);
8873 todo = ht->ht_used;
8874 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008876 if (!HASHITEM_EMPTY(hi))
8877 {
8878 --todo;
8879 di = HI2DI(hi);
8880 if (tv_check_lock(di->di_tv.v_lock, msg))
8881 break;
8882 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008883 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
8884 || called_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008885 break;
8886 if (!map && rem)
8887 dictitem_remove(d, di);
8888 clear_tv(&vimvars[VV_KEY].vv_tv);
8889 }
8890 }
8891 hash_unlock(ht);
8892
8893 restore_vimvar(VV_KEY, &save_key);
8894 }
8895 else
8896 {
8897 for (li = l->lv_first; li != NULL; li = nli)
8898 {
8899 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008900 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008901 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008902 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
8903 || called_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008904 break;
8905 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008906 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008907 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008908 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008911
8912 called_emsg |= save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008913 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008914
8915 copy_tv(&argvars[0], rettv);
8916}
8917
8918 static int
8919filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008920 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008921 char_u *expr;
8922 int map;
8923 int *remp;
8924{
Bram Moolenaar33570922005-01-25 22:26:29 +00008925 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008926 char_u *s;
8927
Bram Moolenaar33570922005-01-25 22:26:29 +00008928 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008929 s = expr;
8930 if (eval1(&s, &rettv, TRUE) == FAIL)
8931 return FAIL;
8932 if (*s != NUL) /* check for trailing chars after expr */
8933 {
8934 EMSG2(_(e_invexpr2), s);
8935 return FAIL;
8936 }
8937 if (map)
8938 {
8939 /* map(): replace the list item value */
8940 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008941 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008942 *tv = rettv;
8943 }
8944 else
8945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008946 int error = FALSE;
8947
Bram Moolenaare9a41262005-01-15 22:18:47 +00008948 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008949 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008950 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008951 /* On type error, nothing has been removed; return FAIL to stop the
8952 * loop. The error message was given by get_tv_number_chk(). */
8953 if (error)
8954 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008956 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008957 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008958}
8959
8960/*
8961 * "filter()" function
8962 */
8963 static void
8964f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008965 typval_T *argvars;
8966 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008967{
8968 filter_map(argvars, rettv, FALSE);
8969}
8970
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008971/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008972 * "finddir({fname}[, {path}[, {count}]])" function
8973 */
8974 static void
8975f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 typval_T *argvars;
8977 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008978{
8979 findfilendir(argvars, rettv, TRUE);
8980}
8981
8982/*
8983 * "findfile({fname}[, {path}[, {count}]])" function
8984 */
8985 static void
8986f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008987 typval_T *argvars;
8988 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008989{
8990 findfilendir(argvars, rettv, FALSE);
8991}
8992
8993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 * "fnamemodify({fname}, {mods})" function
8995 */
8996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008997f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008998 typval_T *argvars;
8999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000{
9001 char_u *fname;
9002 char_u *mods;
9003 int usedlen = 0;
9004 int len;
9005 char_u *fbuf = NULL;
9006 char_u buf[NUMBUFLEN];
9007
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009008 fname = get_tv_string_chk(&argvars[0]);
9009 mods = get_tv_string_buf_chk(&argvars[1], buf);
9010 if (fname == NULL || mods == NULL)
9011 fname = NULL;
9012 else
9013 {
9014 len = (int)STRLEN(fname);
9015 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009020 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009022 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 vim_free(fbuf);
9024}
9025
Bram Moolenaar33570922005-01-25 22:26:29 +00009026static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027
9028/*
9029 * "foldclosed()" function
9030 */
9031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009032foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009033 typval_T *argvars;
9034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 int end;
9036{
9037#ifdef FEAT_FOLDING
9038 linenr_T lnum;
9039 linenr_T first, last;
9040
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9043 {
9044 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9045 {
9046 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009047 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 return;
9051 }
9052 }
9053#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009054 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055}
9056
9057/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009058 * "foldclosed()" function
9059 */
9060 static void
9061f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 typval_T *argvars;
9063 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009064{
9065 foldclosed_both(argvars, rettv, FALSE);
9066}
9067
9068/*
9069 * "foldclosedend()" function
9070 */
9071 static void
9072f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009073 typval_T *argvars;
9074 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009075{
9076 foldclosed_both(argvars, rettv, TRUE);
9077}
9078
9079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 * "foldlevel()" function
9081 */
9082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009083f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009084 typval_T *argvars;
9085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086{
9087#ifdef FEAT_FOLDING
9088 linenr_T lnum;
9089
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009092 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 else
9094#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009095 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096}
9097
9098/*
9099 * "foldtext()" function
9100 */
9101/*ARGSUSED*/
9102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103f_foldtext(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#ifdef FEAT_FOLDING
9108 linenr_T lnum;
9109 char_u *s;
9110 char_u *r;
9111 int len;
9112 char *txt;
9113#endif
9114
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 rettv->v_type = VAR_STRING;
9116 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009118 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9119 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9120 <= curbuf->b_ml.ml_line_count
9121 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 {
9123 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009124 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9125 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 {
9127 if (!linewhite(lnum))
9128 break;
9129 ++lnum;
9130 }
9131
9132 /* Find interesting text in this line. */
9133 s = skipwhite(ml_get(lnum));
9134 /* skip C comment-start */
9135 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009138 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009139 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009140 {
9141 s = skipwhite(ml_get(lnum + 1));
9142 if (*s == '*')
9143 s = skipwhite(s + 1);
9144 }
9145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 txt = _("+-%s%3ld lines: ");
9147 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009148 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 + 20 /* for %3ld */
9150 + STRLEN(s))); /* concatenated */
9151 if (r != NULL)
9152 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009153 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9154 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9155 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 len = (int)STRLEN(r);
9157 STRCAT(r, s);
9158 /* remove 'foldmarker' and 'commentstring' */
9159 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009160 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 }
9162 }
9163#endif
9164}
9165
9166/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009167 * "foldtextresult(lnum)" function
9168 */
9169/*ARGSUSED*/
9170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009171f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009172 typval_T *argvars;
9173 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009174{
9175#ifdef FEAT_FOLDING
9176 linenr_T lnum;
9177 char_u *text;
9178 char_u buf[51];
9179 foldinfo_T foldinfo;
9180 int fold_count;
9181#endif
9182
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183 rettv->v_type = VAR_STRING;
9184 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009185#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009187 /* treat illegal types and illegal string values for {lnum} the same */
9188 if (lnum < 0)
9189 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009190 fold_count = foldedCount(curwin, lnum, &foldinfo);
9191 if (fold_count > 0)
9192 {
9193 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9194 &foldinfo, buf);
9195 if (text == buf)
9196 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009198 }
9199#endif
9200}
9201
9202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 * "foreground()" function
9204 */
9205/*ARGSUSED*/
9206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009207f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009208 typval_T *argvars;
9209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009211 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212#ifdef FEAT_GUI
9213 if (gui.in_use)
9214 gui_mch_set_foreground();
9215#else
9216# ifdef WIN32
9217 win32_set_foreground();
9218# endif
9219#endif
9220}
9221
9222/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009223 * "function()" function
9224 */
9225/*ARGSUSED*/
9226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009227f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009228 typval_T *argvars;
9229 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009230{
9231 char_u *s;
9232
Bram Moolenaara7043832005-01-21 11:56:39 +00009233 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009234 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009235 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009236 EMSG2(_(e_invarg2), s);
9237 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009238 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009239 else
9240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009241 rettv->vval.v_string = vim_strsave(s);
9242 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009243 }
9244}
9245
9246/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009247 * "garbagecollect()" function
9248 */
9249/*ARGSUSED*/
9250 static void
9251f_garbagecollect(argvars, rettv)
9252 typval_T *argvars;
9253 typval_T *rettv;
9254{
9255 garbage_collect();
9256}
9257
9258/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009259 * "get()" function
9260 */
9261 static void
9262f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009263 typval_T *argvars;
9264 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009265{
Bram Moolenaar33570922005-01-25 22:26:29 +00009266 listitem_T *li;
9267 list_T *l;
9268 dictitem_T *di;
9269 dict_T *d;
9270 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009271
Bram Moolenaare9a41262005-01-15 22:18:47 +00009272 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009273 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009274 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009276 int error = FALSE;
9277
9278 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9279 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009280 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009281 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009282 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009283 else if (argvars[0].v_type == VAR_DICT)
9284 {
9285 if ((d = argvars[0].vval.v_dict) != NULL)
9286 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009287 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009288 if (di != NULL)
9289 tv = &di->di_tv;
9290 }
9291 }
9292 else
9293 EMSG2(_(e_listdictarg), "get()");
9294
9295 if (tv == NULL)
9296 {
9297 if (argvars[2].v_type == VAR_UNKNOWN)
9298 rettv->vval.v_number = 0;
9299 else
9300 copy_tv(&argvars[2], rettv);
9301 }
9302 else
9303 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009304}
9305
Bram Moolenaar342337a2005-07-21 21:11:17 +00009306static 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 +00009307
9308/*
9309 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009310 * Return a range (from start to end) of lines in rettv from the specified
9311 * buffer.
9312 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009313 */
9314 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009315get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009316 buf_T *buf;
9317 linenr_T start;
9318 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009319 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009320 typval_T *rettv;
9321{
9322 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009323 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009324
Bram Moolenaar342337a2005-07-21 21:11:17 +00009325 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009326 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009327 l = list_alloc();
9328 if (l == NULL)
9329 return;
9330
9331 rettv->vval.v_list = l;
9332 rettv->v_type = VAR_LIST;
9333 ++l->lv_refcount;
9334 }
9335 else
9336 rettv->vval.v_number = 0;
9337
9338 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9339 return;
9340
9341 if (!retlist)
9342 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009343 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9344 p = ml_get_buf(buf, start, FALSE);
9345 else
9346 p = (char_u *)"";
9347
9348 rettv->v_type = VAR_STRING;
9349 rettv->vval.v_string = vim_strsave(p);
9350 }
9351 else
9352 {
9353 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009354 return;
9355
9356 if (start < 1)
9357 start = 1;
9358 if (end > buf->b_ml.ml_line_count)
9359 end = buf->b_ml.ml_line_count;
9360 while (start <= end)
Bram Moolenaar4463f292005-09-25 22:20:24 +00009361 if (list_append_string(l, ml_get_buf(buf, start++, FALSE), -1)
9362 == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009363 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009364 }
9365}
9366
9367/*
9368 * "getbufline()" function
9369 */
9370 static void
9371f_getbufline(argvars, rettv)
9372 typval_T *argvars;
9373 typval_T *rettv;
9374{
9375 linenr_T lnum;
9376 linenr_T end;
9377 buf_T *buf;
9378
9379 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9380 ++emsg_off;
9381 buf = get_buf_tv(&argvars[0]);
9382 --emsg_off;
9383
Bram Moolenaar661b1822005-07-28 22:36:45 +00009384 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009385 if (argvars[2].v_type == VAR_UNKNOWN)
9386 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009387 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009388 end = get_tv_lnum_buf(&argvars[2], buf);
9389
Bram Moolenaar342337a2005-07-21 21:11:17 +00009390 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009391}
9392
Bram Moolenaar0d660222005-01-07 21:51:51 +00009393/*
9394 * "getbufvar()" function
9395 */
9396 static void
9397f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009398 typval_T *argvars;
9399 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009400{
9401 buf_T *buf;
9402 buf_T *save_curbuf;
9403 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009404 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009406 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9407 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009408 ++emsg_off;
9409 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009410
9411 rettv->v_type = VAR_STRING;
9412 rettv->vval.v_string = NULL;
9413
9414 if (buf != NULL && varname != NULL)
9415 {
9416 if (*varname == '&') /* buffer-local-option */
9417 {
9418 /* set curbuf to be our buf, temporarily */
9419 save_curbuf = curbuf;
9420 curbuf = buf;
9421
9422 get_option_tv(&varname, rettv, TRUE);
9423
9424 /* restore previous notion of curbuf */
9425 curbuf = save_curbuf;
9426 }
9427 else
9428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009429 if (*varname == NUL)
9430 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9431 * scope prefix before the NUL byte is required by
9432 * find_var_in_ht(). */
9433 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009434 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009435 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009436 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009437 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009438 }
9439 }
9440
9441 --emsg_off;
9442}
9443
9444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 * "getchar()" function
9446 */
9447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009448f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009449 typval_T *argvars;
9450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
9452 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009453 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454
9455 ++no_mapping;
9456 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009457 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 /* getchar(): blocking wait. */
9459 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009460 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 /* getchar(1): only check if char avail */
9462 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009463 else if (error || vpeekc() == NUL)
9464 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 n = 0;
9466 else
9467 /* getchar(0) and char avail: return char */
9468 n = safe_vgetc();
9469 --no_mapping;
9470 --allow_keys;
9471
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009472 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 if (IS_SPECIAL(n) || mod_mask != 0)
9474 {
9475 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9476 int i = 0;
9477
9478 /* Turn a special key into three bytes, plus modifier. */
9479 if (mod_mask != 0)
9480 {
9481 temp[i++] = K_SPECIAL;
9482 temp[i++] = KS_MODIFIER;
9483 temp[i++] = mod_mask;
9484 }
9485 if (IS_SPECIAL(n))
9486 {
9487 temp[i++] = K_SPECIAL;
9488 temp[i++] = K_SECOND(n);
9489 temp[i++] = K_THIRD(n);
9490 }
9491#ifdef FEAT_MBYTE
9492 else if (has_mbyte)
9493 i += (*mb_char2bytes)(n, temp + i);
9494#endif
9495 else
9496 temp[i++] = n;
9497 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498 rettv->v_type = VAR_STRING;
9499 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 }
9501}
9502
9503/*
9504 * "getcharmod()" function
9505 */
9506/*ARGSUSED*/
9507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009508f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009509 typval_T *argvars;
9510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513}
9514
9515/*
9516 * "getcmdline()" function
9517 */
9518/*ARGSUSED*/
9519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009520f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009521 typval_T *argvars;
9522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009524 rettv->v_type = VAR_STRING;
9525 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526}
9527
9528/*
9529 * "getcmdpos()" function
9530 */
9531/*ARGSUSED*/
9532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009534 typval_T *argvars;
9535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009537 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538}
9539
9540/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009541 * "getcmdtype()" function
9542 */
9543/*ARGSUSED*/
9544 static void
9545f_getcmdtype(argvars, rettv)
9546 typval_T *argvars;
9547 typval_T *rettv;
9548{
9549 rettv->v_type = VAR_STRING;
9550 rettv->vval.v_string = alloc(2);
9551 if (rettv->vval.v_string != NULL)
9552 {
9553 rettv->vval.v_string[0] = get_cmdline_type();
9554 rettv->vval.v_string[1] = NUL;
9555 }
9556}
9557
9558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 * "getcwd()" function
9560 */
9561/*ARGSUSED*/
9562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009563f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009564 typval_T *argvars;
9565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566{
9567 char_u cwd[MAXPATHL];
9568
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 else
9573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009576 if (rettv->vval.v_string != NULL)
9577 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578#endif
9579 }
9580}
9581
9582/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009583 * "getfontname()" function
9584 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009585/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009587f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009588 typval_T *argvars;
9589 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009590{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009591 rettv->v_type = VAR_STRING;
9592 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009593#ifdef FEAT_GUI
9594 if (gui.in_use)
9595 {
9596 GuiFont font;
9597 char_u *name = NULL;
9598
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009599 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009600 {
9601 /* Get the "Normal" font. Either the name saved by
9602 * hl_set_font_name() or from the font ID. */
9603 font = gui.norm_font;
9604 name = hl_get_font_name();
9605 }
9606 else
9607 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009609 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9610 return;
9611 font = gui_mch_get_font(name, FALSE);
9612 if (font == NOFONT)
9613 return; /* Invalid font name, return empty string. */
9614 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009615 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009616 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009617 gui_mch_free_font(font);
9618 }
9619#endif
9620}
9621
9622/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009623 * "getfperm({fname})" function
9624 */
9625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009627 typval_T *argvars;
9628 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009629{
9630 char_u *fname;
9631 struct stat st;
9632 char_u *perm = NULL;
9633 char_u flags[] = "rwx";
9634 int i;
9635
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009636 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009639 if (mch_stat((char *)fname, &st) >= 0)
9640 {
9641 perm = vim_strsave((char_u *)"---------");
9642 if (perm != NULL)
9643 {
9644 for (i = 0; i < 9; i++)
9645 {
9646 if (st.st_mode & (1 << (8 - i)))
9647 perm[i] = flags[i % 3];
9648 }
9649 }
9650 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009652}
9653
9654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 * "getfsize({fname})" function
9656 */
9657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009659 typval_T *argvars;
9660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
9662 char_u *fname;
9663 struct stat st;
9664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009665 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009667 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668
9669 if (mch_stat((char *)fname, &st) >= 0)
9670 {
9671 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 }
9676 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678}
9679
9680/*
9681 * "getftime({fname})" function
9682 */
9683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009685 typval_T *argvars;
9686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687{
9688 char_u *fname;
9689 struct stat st;
9690
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
9693 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697}
9698
9699/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009700 * "getftype({fname})" function
9701 */
9702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009704 typval_T *argvars;
9705 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009706{
9707 char_u *fname;
9708 struct stat st;
9709 char_u *type = NULL;
9710 char *t;
9711
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009713
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009715 if (mch_lstat((char *)fname, &st) >= 0)
9716 {
9717#ifdef S_ISREG
9718 if (S_ISREG(st.st_mode))
9719 t = "file";
9720 else if (S_ISDIR(st.st_mode))
9721 t = "dir";
9722# ifdef S_ISLNK
9723 else if (S_ISLNK(st.st_mode))
9724 t = "link";
9725# endif
9726# ifdef S_ISBLK
9727 else if (S_ISBLK(st.st_mode))
9728 t = "bdev";
9729# endif
9730# ifdef S_ISCHR
9731 else if (S_ISCHR(st.st_mode))
9732 t = "cdev";
9733# endif
9734# ifdef S_ISFIFO
9735 else if (S_ISFIFO(st.st_mode))
9736 t = "fifo";
9737# endif
9738# ifdef S_ISSOCK
9739 else if (S_ISSOCK(st.st_mode))
9740 t = "fifo";
9741# endif
9742 else
9743 t = "other";
9744#else
9745# ifdef S_IFMT
9746 switch (st.st_mode & S_IFMT)
9747 {
9748 case S_IFREG: t = "file"; break;
9749 case S_IFDIR: t = "dir"; break;
9750# ifdef S_IFLNK
9751 case S_IFLNK: t = "link"; break;
9752# endif
9753# ifdef S_IFBLK
9754 case S_IFBLK: t = "bdev"; break;
9755# endif
9756# ifdef S_IFCHR
9757 case S_IFCHR: t = "cdev"; break;
9758# endif
9759# ifdef S_IFIFO
9760 case S_IFIFO: t = "fifo"; break;
9761# endif
9762# ifdef S_IFSOCK
9763 case S_IFSOCK: t = "socket"; break;
9764# endif
9765 default: t = "other";
9766 }
9767# else
9768 if (mch_isdir(fname))
9769 t = "dir";
9770 else
9771 t = "file";
9772# endif
9773#endif
9774 type = vim_strsave((char_u *)t);
9775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009777}
9778
9779/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009780 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009781 */
9782 static void
9783f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009784 typval_T *argvars;
9785 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009786{
9787 linenr_T lnum;
9788 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009789 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009790
9791 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009792 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009793 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009794 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009795 retlist = FALSE;
9796 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009797 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009798 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009799 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009800 retlist = TRUE;
9801 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009802
Bram Moolenaar342337a2005-07-21 21:11:17 +00009803 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009804}
9805
9806/*
Bram Moolenaar280f1262006-01-30 00:14:18 +00009807 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +00009808 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00009809/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +00009810 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +00009811f_getqflist(argvars, rettv)
9812 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009813 typval_T *rettv;
9814{
9815#ifdef FEAT_QUICKFIX
9816 list_T *l;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009817 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009818#endif
9819
9820 rettv->vval.v_number = FALSE;
9821#ifdef FEAT_QUICKFIX
9822 l = list_alloc();
9823 if (l != NULL)
9824 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009825 rettv->vval.v_list = l;
9826 rettv->v_type = VAR_LIST;
9827 ++l->lv_refcount;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009828 wp = NULL;
9829 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
9830 {
9831 wp = find_win_by_nr(&argvars[0]);
9832 if (wp == NULL)
9833 return;
9834 }
9835
Bram Moolenaar17c7c012006-01-26 22:25:15 +00009836 (void)get_errorlist(wp, l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009837 }
9838#endif
9839}
9840
9841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 * "getreg()" function
9843 */
9844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009845f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009846 typval_T *argvars;
9847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848{
9849 char_u *strregname;
9850 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009851 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009852 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009854 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009856 strregname = get_tv_string_chk(&argvars[0]);
9857 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009858 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009859 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009862 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 regname = (strregname == NULL ? '"' : *strregname);
9864 if (regname == 0)
9865 regname = '"';
9866
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009867 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009868 rettv->vval.v_string = error ? NULL :
9869 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870}
9871
9872/*
9873 * "getregtype()" function
9874 */
9875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009877 typval_T *argvars;
9878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879{
9880 char_u *strregname;
9881 int regname;
9882 char_u buf[NUMBUFLEN + 2];
9883 long reglen = 0;
9884
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009885 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009886 {
9887 strregname = get_tv_string_chk(&argvars[0]);
9888 if (strregname == NULL) /* type error; errmsg already given */
9889 {
9890 rettv->v_type = VAR_STRING;
9891 rettv->vval.v_string = NULL;
9892 return;
9893 }
9894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 else
9896 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009897 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898
9899 regname = (strregname == NULL ? '"' : *strregname);
9900 if (regname == 0)
9901 regname = '"';
9902
9903 buf[0] = NUL;
9904 buf[1] = NUL;
9905 switch (get_reg_type(regname, &reglen))
9906 {
9907 case MLINE: buf[0] = 'V'; break;
9908 case MCHAR: buf[0] = 'v'; break;
9909#ifdef FEAT_VISUAL
9910 case MBLOCK:
9911 buf[0] = Ctrl_V;
9912 sprintf((char *)buf + 1, "%ld", reglen + 1);
9913 break;
9914#endif
9915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009916 rettv->v_type = VAR_STRING;
9917 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918}
9919
9920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 * "getwinposx()" function
9922 */
9923/*ARGSUSED*/
9924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009925f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009926 typval_T *argvars;
9927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009929 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930#ifdef FEAT_GUI
9931 if (gui.in_use)
9932 {
9933 int x, y;
9934
9935 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 }
9938#endif
9939}
9940
9941/*
9942 * "getwinposy()" function
9943 */
9944/*ARGSUSED*/
9945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009946f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009947 typval_T *argvars;
9948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009950 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951#ifdef FEAT_GUI
9952 if (gui.in_use)
9953 {
9954 int x, y;
9955
9956 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009957 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 }
9959#endif
9960}
9961
Bram Moolenaara40058a2005-07-11 22:42:07 +00009962 static win_T *
9963find_win_by_nr(vp)
9964 typval_T *vp;
9965{
9966#ifdef FEAT_WINDOWS
9967 win_T *wp;
9968#endif
9969 int nr;
9970
9971 nr = get_tv_number_chk(vp, NULL);
9972
9973#ifdef FEAT_WINDOWS
9974 if (nr < 0)
9975 return NULL;
9976 if (nr == 0)
9977 return curwin;
9978
9979 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9980 if (--nr <= 0)
9981 break;
9982 return wp;
9983#else
9984 if (nr == 0 || nr == 1)
9985 return curwin;
9986 return NULL;
9987#endif
9988}
9989
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990/*
9991 * "getwinvar()" function
9992 */
9993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009995 typval_T *argvars;
9996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997{
9998 win_T *win, *oldcurwin;
9999 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010000 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010003 varname = get_tv_string_chk(&argvars[1]);
10004 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 rettv->v_type = VAR_STRING;
10007 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008
10009 if (win != NULL && varname != NULL)
10010 {
10011 if (*varname == '&') /* window-local-option */
10012 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010013 /* Set curwin to be our win, temporarily. Also set curbuf, so
10014 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 oldcurwin = curwin;
10016 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010017 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010019 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020
10021 /* restore previous notion of curwin */
10022 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010023 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 }
10025 else
10026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010027 if (*varname == NUL)
10028 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10029 * scope prefix before the NUL byte is required by
10030 * find_var_in_ht(). */
10031 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010033 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010035 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 }
10037 }
10038
10039 --emsg_off;
10040}
10041
10042/*
10043 * "glob()" function
10044 */
10045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010047 typval_T *argvars;
10048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049{
10050 expand_T xpc;
10051
10052 ExpandInit(&xpc);
10053 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 rettv->v_type = VAR_STRING;
10055 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10057 ExpandCleanup(&xpc);
10058}
10059
10060/*
10061 * "globpath()" function
10062 */
10063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010064f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010065 typval_T *argvars;
10066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067{
10068 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010069 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010072 if (file == NULL)
10073 rettv->vval.v_string = NULL;
10074 else
10075 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076}
10077
10078/*
10079 * "has()" function
10080 */
10081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010082f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010083 typval_T *argvars;
10084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085{
10086 int i;
10087 char_u *name;
10088 int n = FALSE;
10089 static char *(has_list[]) =
10090 {
10091#ifdef AMIGA
10092 "amiga",
10093# ifdef FEAT_ARP
10094 "arp",
10095# endif
10096#endif
10097#ifdef __BEOS__
10098 "beos",
10099#endif
10100#ifdef MSDOS
10101# ifdef DJGPP
10102 "dos32",
10103# else
10104 "dos16",
10105# endif
10106#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010107#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 "mac",
10109#endif
10110#if defined(MACOS_X_UNIX)
10111 "macunix",
10112#endif
10113#ifdef OS2
10114 "os2",
10115#endif
10116#ifdef __QNX__
10117 "qnx",
10118#endif
10119#ifdef RISCOS
10120 "riscos",
10121#endif
10122#ifdef UNIX
10123 "unix",
10124#endif
10125#ifdef VMS
10126 "vms",
10127#endif
10128#ifdef WIN16
10129 "win16",
10130#endif
10131#ifdef WIN32
10132 "win32",
10133#endif
10134#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10135 "win32unix",
10136#endif
10137#ifdef WIN64
10138 "win64",
10139#endif
10140#ifdef EBCDIC
10141 "ebcdic",
10142#endif
10143#ifndef CASE_INSENSITIVE_FILENAME
10144 "fname_case",
10145#endif
10146#ifdef FEAT_ARABIC
10147 "arabic",
10148#endif
10149#ifdef FEAT_AUTOCMD
10150 "autocmd",
10151#endif
10152#ifdef FEAT_BEVAL
10153 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010154# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10155 "balloon_multiline",
10156# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157#endif
10158#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10159 "builtin_terms",
10160# ifdef ALL_BUILTIN_TCAPS
10161 "all_builtin_terms",
10162# endif
10163#endif
10164#ifdef FEAT_BYTEOFF
10165 "byte_offset",
10166#endif
10167#ifdef FEAT_CINDENT
10168 "cindent",
10169#endif
10170#ifdef FEAT_CLIENTSERVER
10171 "clientserver",
10172#endif
10173#ifdef FEAT_CLIPBOARD
10174 "clipboard",
10175#endif
10176#ifdef FEAT_CMDL_COMPL
10177 "cmdline_compl",
10178#endif
10179#ifdef FEAT_CMDHIST
10180 "cmdline_hist",
10181#endif
10182#ifdef FEAT_COMMENTS
10183 "comments",
10184#endif
10185#ifdef FEAT_CRYPT
10186 "cryptv",
10187#endif
10188#ifdef FEAT_CSCOPE
10189 "cscope",
10190#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010191#ifdef CURSOR_SHAPE
10192 "cursorshape",
10193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194#ifdef DEBUG
10195 "debug",
10196#endif
10197#ifdef FEAT_CON_DIALOG
10198 "dialog_con",
10199#endif
10200#ifdef FEAT_GUI_DIALOG
10201 "dialog_gui",
10202#endif
10203#ifdef FEAT_DIFF
10204 "diff",
10205#endif
10206#ifdef FEAT_DIGRAPHS
10207 "digraphs",
10208#endif
10209#ifdef FEAT_DND
10210 "dnd",
10211#endif
10212#ifdef FEAT_EMACS_TAGS
10213 "emacs_tags",
10214#endif
10215 "eval", /* always present, of course! */
10216#ifdef FEAT_EX_EXTRA
10217 "ex_extra",
10218#endif
10219#ifdef FEAT_SEARCH_EXTRA
10220 "extra_search",
10221#endif
10222#ifdef FEAT_FKMAP
10223 "farsi",
10224#endif
10225#ifdef FEAT_SEARCHPATH
10226 "file_in_path",
10227#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010228#if defined(UNIX) && !defined(USE_SYSTEM)
10229 "filterpipe",
10230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231#ifdef FEAT_FIND_ID
10232 "find_in_path",
10233#endif
10234#ifdef FEAT_FOLDING
10235 "folding",
10236#endif
10237#ifdef FEAT_FOOTER
10238 "footer",
10239#endif
10240#if !defined(USE_SYSTEM) && defined(UNIX)
10241 "fork",
10242#endif
10243#ifdef FEAT_GETTEXT
10244 "gettext",
10245#endif
10246#ifdef FEAT_GUI
10247 "gui",
10248#endif
10249#ifdef FEAT_GUI_ATHENA
10250# ifdef FEAT_GUI_NEXTAW
10251 "gui_neXtaw",
10252# else
10253 "gui_athena",
10254# endif
10255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256#ifdef FEAT_GUI_GTK
10257 "gui_gtk",
10258# ifdef HAVE_GTK2
10259 "gui_gtk2",
10260# endif
10261#endif
10262#ifdef FEAT_GUI_MAC
10263 "gui_mac",
10264#endif
10265#ifdef FEAT_GUI_MOTIF
10266 "gui_motif",
10267#endif
10268#ifdef FEAT_GUI_PHOTON
10269 "gui_photon",
10270#endif
10271#ifdef FEAT_GUI_W16
10272 "gui_win16",
10273#endif
10274#ifdef FEAT_GUI_W32
10275 "gui_win32",
10276#endif
10277#ifdef FEAT_HANGULIN
10278 "hangul_input",
10279#endif
10280#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10281 "iconv",
10282#endif
10283#ifdef FEAT_INS_EXPAND
10284 "insert_expand",
10285#endif
10286#ifdef FEAT_JUMPLIST
10287 "jumplist",
10288#endif
10289#ifdef FEAT_KEYMAP
10290 "keymap",
10291#endif
10292#ifdef FEAT_LANGMAP
10293 "langmap",
10294#endif
10295#ifdef FEAT_LIBCALL
10296 "libcall",
10297#endif
10298#ifdef FEAT_LINEBREAK
10299 "linebreak",
10300#endif
10301#ifdef FEAT_LISP
10302 "lispindent",
10303#endif
10304#ifdef FEAT_LISTCMDS
10305 "listcmds",
10306#endif
10307#ifdef FEAT_LOCALMAP
10308 "localmap",
10309#endif
10310#ifdef FEAT_MENU
10311 "menu",
10312#endif
10313#ifdef FEAT_SESSION
10314 "mksession",
10315#endif
10316#ifdef FEAT_MODIFY_FNAME
10317 "modify_fname",
10318#endif
10319#ifdef FEAT_MOUSE
10320 "mouse",
10321#endif
10322#ifdef FEAT_MOUSESHAPE
10323 "mouseshape",
10324#endif
10325#if defined(UNIX) || defined(VMS)
10326# ifdef FEAT_MOUSE_DEC
10327 "mouse_dec",
10328# endif
10329# ifdef FEAT_MOUSE_GPM
10330 "mouse_gpm",
10331# endif
10332# ifdef FEAT_MOUSE_JSB
10333 "mouse_jsbterm",
10334# endif
10335# ifdef FEAT_MOUSE_NET
10336 "mouse_netterm",
10337# endif
10338# ifdef FEAT_MOUSE_PTERM
10339 "mouse_pterm",
10340# endif
10341# ifdef FEAT_MOUSE_XTERM
10342 "mouse_xterm",
10343# endif
10344#endif
10345#ifdef FEAT_MBYTE
10346 "multi_byte",
10347#endif
10348#ifdef FEAT_MBYTE_IME
10349 "multi_byte_ime",
10350#endif
10351#ifdef FEAT_MULTI_LANG
10352 "multi_lang",
10353#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010354#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010355#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010356 "mzscheme",
10357#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359#ifdef FEAT_OLE
10360 "ole",
10361#endif
10362#ifdef FEAT_OSFILETYPE
10363 "osfiletype",
10364#endif
10365#ifdef FEAT_PATH_EXTRA
10366 "path_extra",
10367#endif
10368#ifdef FEAT_PERL
10369#ifndef DYNAMIC_PERL
10370 "perl",
10371#endif
10372#endif
10373#ifdef FEAT_PYTHON
10374#ifndef DYNAMIC_PYTHON
10375 "python",
10376#endif
10377#endif
10378#ifdef FEAT_POSTSCRIPT
10379 "postscript",
10380#endif
10381#ifdef FEAT_PRINTER
10382 "printer",
10383#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010384#ifdef FEAT_PROFILE
10385 "profile",
10386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387#ifdef FEAT_QUICKFIX
10388 "quickfix",
10389#endif
10390#ifdef FEAT_RIGHTLEFT
10391 "rightleft",
10392#endif
10393#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10394 "ruby",
10395#endif
10396#ifdef FEAT_SCROLLBIND
10397 "scrollbind",
10398#endif
10399#ifdef FEAT_CMDL_INFO
10400 "showcmd",
10401 "cmdline_info",
10402#endif
10403#ifdef FEAT_SIGNS
10404 "signs",
10405#endif
10406#ifdef FEAT_SMARTINDENT
10407 "smartindent",
10408#endif
10409#ifdef FEAT_SNIFF
10410 "sniff",
10411#endif
10412#ifdef FEAT_STL_OPT
10413 "statusline",
10414#endif
10415#ifdef FEAT_SUN_WORKSHOP
10416 "sun_workshop",
10417#endif
10418#ifdef FEAT_NETBEANS_INTG
10419 "netbeans_intg",
10420#endif
10421#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010422 "spell",
10423#endif
10424#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 "syntax",
10426#endif
10427#if defined(USE_SYSTEM) || !defined(UNIX)
10428 "system",
10429#endif
10430#ifdef FEAT_TAG_BINS
10431 "tag_binary",
10432#endif
10433#ifdef FEAT_TAG_OLDSTATIC
10434 "tag_old_static",
10435#endif
10436#ifdef FEAT_TAG_ANYWHITE
10437 "tag_any_white",
10438#endif
10439#ifdef FEAT_TCL
10440# ifndef DYNAMIC_TCL
10441 "tcl",
10442# endif
10443#endif
10444#ifdef TERMINFO
10445 "terminfo",
10446#endif
10447#ifdef FEAT_TERMRESPONSE
10448 "termresponse",
10449#endif
10450#ifdef FEAT_TEXTOBJ
10451 "textobjects",
10452#endif
10453#ifdef HAVE_TGETENT
10454 "tgetent",
10455#endif
10456#ifdef FEAT_TITLE
10457 "title",
10458#endif
10459#ifdef FEAT_TOOLBAR
10460 "toolbar",
10461#endif
10462#ifdef FEAT_USR_CMDS
10463 "user-commands", /* was accidentally included in 5.4 */
10464 "user_commands",
10465#endif
10466#ifdef FEAT_VIMINFO
10467 "viminfo",
10468#endif
10469#ifdef FEAT_VERTSPLIT
10470 "vertsplit",
10471#endif
10472#ifdef FEAT_VIRTUALEDIT
10473 "virtualedit",
10474#endif
10475#ifdef FEAT_VISUAL
10476 "visual",
10477#endif
10478#ifdef FEAT_VISUALEXTRA
10479 "visualextra",
10480#endif
10481#ifdef FEAT_VREPLACE
10482 "vreplace",
10483#endif
10484#ifdef FEAT_WILDIGN
10485 "wildignore",
10486#endif
10487#ifdef FEAT_WILDMENU
10488 "wildmenu",
10489#endif
10490#ifdef FEAT_WINDOWS
10491 "windows",
10492#endif
10493#ifdef FEAT_WAK
10494 "winaltkeys",
10495#endif
10496#ifdef FEAT_WRITEBACKUP
10497 "writebackup",
10498#endif
10499#ifdef FEAT_XIM
10500 "xim",
10501#endif
10502#ifdef FEAT_XFONTSET
10503 "xfontset",
10504#endif
10505#ifdef USE_XSMP
10506 "xsmp",
10507#endif
10508#ifdef USE_XSMP_INTERACT
10509 "xsmp_interact",
10510#endif
10511#ifdef FEAT_XCLIPBOARD
10512 "xterm_clipboard",
10513#endif
10514#ifdef FEAT_XTERM_SAVE
10515 "xterm_save",
10516#endif
10517#if defined(UNIX) && defined(FEAT_X11)
10518 "X11",
10519#endif
10520 NULL
10521 };
10522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010523 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 for (i = 0; has_list[i] != NULL; ++i)
10525 if (STRICMP(name, has_list[i]) == 0)
10526 {
10527 n = TRUE;
10528 break;
10529 }
10530
10531 if (n == FALSE)
10532 {
10533 if (STRNICMP(name, "patch", 5) == 0)
10534 n = has_patch(atoi((char *)name + 5));
10535 else if (STRICMP(name, "vim_starting") == 0)
10536 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010537#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10538 else if (STRICMP(name, "balloon_multiline") == 0)
10539 n = multiline_balloon_available();
10540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541#ifdef DYNAMIC_TCL
10542 else if (STRICMP(name, "tcl") == 0)
10543 n = tcl_enabled(FALSE);
10544#endif
10545#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10546 else if (STRICMP(name, "iconv") == 0)
10547 n = iconv_enabled(FALSE);
10548#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010549#ifdef DYNAMIC_MZSCHEME
10550 else if (STRICMP(name, "mzscheme") == 0)
10551 n = mzscheme_enabled(FALSE);
10552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553#ifdef DYNAMIC_RUBY
10554 else if (STRICMP(name, "ruby") == 0)
10555 n = ruby_enabled(FALSE);
10556#endif
10557#ifdef DYNAMIC_PYTHON
10558 else if (STRICMP(name, "python") == 0)
10559 n = python_enabled(FALSE);
10560#endif
10561#ifdef DYNAMIC_PERL
10562 else if (STRICMP(name, "perl") == 0)
10563 n = perl_enabled(FALSE);
10564#endif
10565#ifdef FEAT_GUI
10566 else if (STRICMP(name, "gui_running") == 0)
10567 n = (gui.in_use || gui.starting);
10568# ifdef FEAT_GUI_W32
10569 else if (STRICMP(name, "gui_win32s") == 0)
10570 n = gui_is_win32s();
10571# endif
10572# ifdef FEAT_BROWSE
10573 else if (STRICMP(name, "browse") == 0)
10574 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10575# endif
10576#endif
10577#ifdef FEAT_SYN_HL
10578 else if (STRICMP(name, "syntax_items") == 0)
10579 n = syntax_present(curbuf);
10580#endif
10581#if defined(WIN3264)
10582 else if (STRICMP(name, "win95") == 0)
10583 n = mch_windows95();
10584#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010585#ifdef FEAT_NETBEANS_INTG
10586 else if (STRICMP(name, "netbeans_enabled") == 0)
10587 n = usingNetbeans;
10588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 }
10590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010591 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592}
10593
10594/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010595 * "has_key()" function
10596 */
10597 static void
10598f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010599 typval_T *argvars;
10600 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010601{
10602 rettv->vval.v_number = 0;
10603 if (argvars[0].v_type != VAR_DICT)
10604 {
10605 EMSG(_(e_dictreq));
10606 return;
10607 }
10608 if (argvars[0].vval.v_dict == NULL)
10609 return;
10610
10611 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010612 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613}
10614
10615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 * "hasmapto()" function
10617 */
10618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010619f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010620 typval_T *argvars;
10621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622{
10623 char_u *name;
10624 char_u *mode;
10625 char_u buf[NUMBUFLEN];
10626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010627 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010628 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 mode = (char_u *)"nvo";
10630 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010631 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632
10633 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010636 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637}
10638
10639/*
10640 * "histadd()" function
10641 */
10642/*ARGSUSED*/
10643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010644f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010645 typval_T *argvars;
10646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647{
10648#ifdef FEAT_CMDHIST
10649 int histype;
10650 char_u *str;
10651 char_u buf[NUMBUFLEN];
10652#endif
10653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010654 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 if (check_restricted() || check_secure())
10656 return;
10657#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010658 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10659 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 if (histype >= 0)
10661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010662 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 if (*str != NUL)
10664 {
10665 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010666 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 return;
10668 }
10669 }
10670#endif
10671}
10672
10673/*
10674 * "histdel()" function
10675 */
10676/*ARGSUSED*/
10677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010678f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010679 typval_T *argvars;
10680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681{
10682#ifdef FEAT_CMDHIST
10683 int n;
10684 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010685 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010687 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10688 if (str == NULL)
10689 n = 0;
10690 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010692 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010693 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010695 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010696 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 else
10698 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010699 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010700 get_tv_string_buf(&argvars[1], buf));
10701 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010703 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704#endif
10705}
10706
10707/*
10708 * "histget()" function
10709 */
10710/*ARGSUSED*/
10711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010713 typval_T *argvars;
10714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715{
10716#ifdef FEAT_CMDHIST
10717 int type;
10718 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010719 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010721 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10722 if (str == NULL)
10723 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010725 {
10726 type = get_histtype(str);
10727 if (argvars[1].v_type == VAR_UNKNOWN)
10728 idx = get_history_idx(type);
10729 else
10730 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10731 /* -1 on type error */
10732 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010737 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738}
10739
10740/*
10741 * "histnr()" function
10742 */
10743/*ARGSUSED*/
10744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010745f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010746 typval_T *argvars;
10747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748{
10749 int i;
10750
10751#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010752 char_u *history = get_tv_string_chk(&argvars[0]);
10753
10754 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 if (i >= HIST_CMD && i < HIST_COUNT)
10756 i = get_history_idx(i);
10757 else
10758#endif
10759 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010760 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761}
10762
10763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 * "highlightID(name)" function
10765 */
10766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010767f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010768 typval_T *argvars;
10769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010771 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772}
10773
10774/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010775 * "highlight_exists()" function
10776 */
10777 static void
10778f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010779 typval_T *argvars;
10780 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010781{
10782 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10783}
10784
10785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 * "hostname()" function
10787 */
10788/*ARGSUSED*/
10789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010790f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010791 typval_T *argvars;
10792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793{
10794 char_u hostname[256];
10795
10796 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010797 rettv->v_type = VAR_STRING;
10798 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799}
10800
10801/*
10802 * iconv() function
10803 */
10804/*ARGSUSED*/
10805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010806f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010807 typval_T *argvars;
10808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809{
10810#ifdef FEAT_MBYTE
10811 char_u buf1[NUMBUFLEN];
10812 char_u buf2[NUMBUFLEN];
10813 char_u *from, *to, *str;
10814 vimconv_T vimconv;
10815#endif
10816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817 rettv->v_type = VAR_STRING;
10818 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819
10820#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821 str = get_tv_string(&argvars[0]);
10822 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10823 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 vimconv.vc_type = CONV_NONE;
10825 convert_setup(&vimconv, from, to);
10826
10827 /* If the encodings are equal, no conversion needed. */
10828 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010829 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832
10833 convert_setup(&vimconv, NULL, NULL);
10834 vim_free(from);
10835 vim_free(to);
10836#endif
10837}
10838
10839/*
10840 * "indent()" function
10841 */
10842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010844 typval_T *argvars;
10845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846{
10847 linenr_T lnum;
10848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010849 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010851 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010853 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854}
10855
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010856/*
10857 * "index()" function
10858 */
10859 static void
10860f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010861 typval_T *argvars;
10862 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010863{
Bram Moolenaar33570922005-01-25 22:26:29 +000010864 list_T *l;
10865 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010866 long idx = 0;
10867 int ic = FALSE;
10868
10869 rettv->vval.v_number = -1;
10870 if (argvars[0].v_type != VAR_LIST)
10871 {
10872 EMSG(_(e_listreq));
10873 return;
10874 }
10875 l = argvars[0].vval.v_list;
10876 if (l != NULL)
10877 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010878 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010879 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010881 int error = FALSE;
10882
Bram Moolenaar758711c2005-02-02 23:11:38 +000010883 /* Start at specified item. Use the cached index that list_find()
10884 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010885 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010886 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010887 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010888 ic = get_tv_number_chk(&argvars[3], &error);
10889 if (error)
10890 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010891 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010892
Bram Moolenaar758711c2005-02-02 23:11:38 +000010893 for ( ; item != NULL; item = item->li_next, ++idx)
10894 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010895 {
10896 rettv->vval.v_number = idx;
10897 break;
10898 }
10899 }
10900}
10901
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902static int inputsecret_flag = 0;
10903
10904/*
10905 * "input()" function
10906 * Also handles inputsecret() when inputsecret is set.
10907 */
10908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010909f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010910 typval_T *argvars;
10911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010913 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 char_u *p = NULL;
10915 int c;
10916 char_u buf[NUMBUFLEN];
10917 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010918 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010919 int xp_type = EXPAND_NOTHING;
10920 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923
10924#ifdef NO_CONSOLE_INPUT
10925 /* While starting up, there is no place to enter text. */
10926 if (no_console_input())
10927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010928 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 return;
10930 }
10931#endif
10932
10933 cmd_silent = FALSE; /* Want to see the prompt. */
10934 if (prompt != NULL)
10935 {
10936 /* Only the part of the message after the last NL is considered as
10937 * prompt for the command line */
10938 p = vim_strrchr(prompt, '\n');
10939 if (p == NULL)
10940 p = prompt;
10941 else
10942 {
10943 ++p;
10944 c = *p;
10945 *p = NUL;
10946 msg_start();
10947 msg_clr_eos();
10948 msg_puts_attr(prompt, echo_attr);
10949 msg_didout = FALSE;
10950 msg_starthere();
10951 *p = c;
10952 }
10953 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010955 if (argvars[1].v_type != VAR_UNKNOWN)
10956 {
10957 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10958 if (defstr != NULL)
10959 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960
Bram Moolenaar4463f292005-09-25 22:20:24 +000010961 if (argvars[2].v_type != VAR_UNKNOWN)
10962 {
10963 char_u *xp_name;
10964 int xp_namelen;
10965 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010966
Bram Moolenaar4463f292005-09-25 22:20:24 +000010967 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010968
Bram Moolenaar4463f292005-09-25 22:20:24 +000010969 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
10970 if (xp_name == NULL)
10971 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010972
Bram Moolenaar4463f292005-09-25 22:20:24 +000010973 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010974
Bram Moolenaar4463f292005-09-25 22:20:24 +000010975 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
10976 &xp_arg) == FAIL)
10977 return;
10978 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010979 }
10980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010981 if (defstr != NULL)
10982 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010983 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
10984 xp_type, xp_arg);
10985
10986 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010988 /* since the user typed this, no need to wait for return */
10989 need_wait_return = FALSE;
10990 msg_didout = FALSE;
10991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 cmd_silent = cmd_silent_save;
10993}
10994
10995/*
10996 * "inputdialog()" function
10997 */
10998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011000 typval_T *argvars;
11001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002{
11003#if defined(FEAT_GUI_TEXTDIALOG)
11004 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11005 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11006 {
11007 char_u *message;
11008 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011009 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011011 message = get_tv_string_chk(&argvars[0]);
11012 if (argvars[1].v_type != VAR_UNKNOWN
11013 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011014 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 else
11016 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011017 if (message != NULL && defstr != NULL
11018 && do_dialog(VIM_QUESTION, NULL, message,
11019 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011020 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 else
11022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011023 if (message != NULL && defstr != NULL
11024 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011025 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011026 rettv->vval.v_string = vim_strsave(
11027 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011029 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011031 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 }
11033 else
11034#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011035 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036}
11037
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011038/*
11039 * "inputlist()" function
11040 */
11041 static void
11042f_inputlist(argvars, rettv)
11043 typval_T *argvars;
11044 typval_T *rettv;
11045{
11046 listitem_T *li;
11047 int selected;
11048 int mouse_used;
11049
11050 rettv->vval.v_number = 0;
11051#ifdef NO_CONSOLE_INPUT
11052 /* While starting up, there is no place to enter text. */
11053 if (no_console_input())
11054 return;
11055#endif
11056 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11057 {
11058 EMSG2(_(e_listarg), "inputlist()");
11059 return;
11060 }
11061
11062 msg_start();
11063 lines_left = Rows; /* avoid more prompt */
11064 msg_scroll = TRUE;
11065 msg_clr_eos();
11066
11067 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11068 {
11069 msg_puts(get_tv_string(&li->li_tv));
11070 msg_putchar('\n');
11071 }
11072
11073 /* Ask for choice. */
11074 selected = prompt_for_number(&mouse_used);
11075 if (mouse_used)
11076 selected -= lines_left;
11077
11078 rettv->vval.v_number = selected;
11079}
11080
11081
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11083
11084/*
11085 * "inputrestore()" function
11086 */
11087/*ARGSUSED*/
11088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011090 typval_T *argvars;
11091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092{
11093 if (ga_userinput.ga_len > 0)
11094 {
11095 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11097 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 }
11100 else if (p_verbose > 1)
11101 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011102 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011103 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 }
11105}
11106
11107/*
11108 * "inputsave()" function
11109 */
11110/*ARGSUSED*/
11111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011113 typval_T *argvars;
11114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115{
11116 /* Add an entry to the stack of typehead storage. */
11117 if (ga_grow(&ga_userinput, 1) == OK)
11118 {
11119 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11120 + ga_userinput.ga_len);
11121 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011122 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 }
11124 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011125 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126}
11127
11128/*
11129 * "inputsecret()" function
11130 */
11131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011132f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011133 typval_T *argvars;
11134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135{
11136 ++cmdline_star;
11137 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011138 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 --cmdline_star;
11140 --inputsecret_flag;
11141}
11142
11143/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011144 * "insert()" function
11145 */
11146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011147f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011148 typval_T *argvars;
11149 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011150{
11151 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011152 listitem_T *item;
11153 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011154 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011155
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011156 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011157 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011158 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011159 else if ((l = argvars[0].vval.v_list) != NULL
11160 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011161 {
11162 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011163 before = get_tv_number_chk(&argvars[2], &error);
11164 if (error)
11165 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011166
Bram Moolenaar758711c2005-02-02 23:11:38 +000011167 if (before == l->lv_len)
11168 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011169 else
11170 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011171 item = list_find(l, before);
11172 if (item == NULL)
11173 {
11174 EMSGN(_(e_listidx), before);
11175 l = NULL;
11176 }
11177 }
11178 if (l != NULL)
11179 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011180 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011181 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011182 }
11183 }
11184}
11185
11186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 * "isdirectory()" function
11188 */
11189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011190f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011191 typval_T *argvars;
11192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011194 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195}
11196
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011197/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011198 * "islocked()" function
11199 */
11200 static void
11201f_islocked(argvars, rettv)
11202 typval_T *argvars;
11203 typval_T *rettv;
11204{
11205 lval_T lv;
11206 char_u *end;
11207 dictitem_T *di;
11208
11209 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011210 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11211 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011212 if (end != NULL && lv.ll_name != NULL)
11213 {
11214 if (*end != NUL)
11215 EMSG(_(e_trailing));
11216 else
11217 {
11218 if (lv.ll_tv == NULL)
11219 {
11220 if (check_changedtick(lv.ll_name))
11221 rettv->vval.v_number = 1; /* always locked */
11222 else
11223 {
11224 di = find_var(lv.ll_name, NULL);
11225 if (di != NULL)
11226 {
11227 /* Consider a variable locked when:
11228 * 1. the variable itself is locked
11229 * 2. the value of the variable is locked.
11230 * 3. the List or Dict value is locked.
11231 */
11232 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11233 || tv_islocked(&di->di_tv));
11234 }
11235 }
11236 }
11237 else if (lv.ll_range)
11238 EMSG(_("E745: Range not allowed"));
11239 else if (lv.ll_newkey != NULL)
11240 EMSG2(_(e_dictkey), lv.ll_newkey);
11241 else if (lv.ll_list != NULL)
11242 /* List item. */
11243 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11244 else
11245 /* Dictionary item. */
11246 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11247 }
11248 }
11249
11250 clear_lval(&lv);
11251}
11252
Bram Moolenaar33570922005-01-25 22:26:29 +000011253static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011254
11255/*
11256 * Turn a dict into a list:
11257 * "what" == 0: list of keys
11258 * "what" == 1: list of values
11259 * "what" == 2: list of items
11260 */
11261 static void
11262dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011263 typval_T *argvars;
11264 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011265 int what;
11266{
Bram Moolenaar33570922005-01-25 22:26:29 +000011267 list_T *l;
11268 list_T *l2;
11269 dictitem_T *di;
11270 hashitem_T *hi;
11271 listitem_T *li;
11272 listitem_T *li2;
11273 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011274 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011275
11276 rettv->vval.v_number = 0;
11277 if (argvars[0].v_type != VAR_DICT)
11278 {
11279 EMSG(_(e_dictreq));
11280 return;
11281 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011282 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011283 return;
11284
11285 l = list_alloc();
11286 if (l == NULL)
11287 return;
11288 rettv->v_type = VAR_LIST;
11289 rettv->vval.v_list = l;
11290 ++l->lv_refcount;
11291
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 todo = d->dv_hashtab.ht_used;
11293 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011294 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011295 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011296 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011297 --todo;
11298 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011299
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011300 li = listitem_alloc();
11301 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011302 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011303 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011304
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011305 if (what == 0)
11306 {
11307 /* keys() */
11308 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011309 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011310 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11311 }
11312 else if (what == 1)
11313 {
11314 /* values() */
11315 copy_tv(&di->di_tv, &li->li_tv);
11316 }
11317 else
11318 {
11319 /* items() */
11320 l2 = list_alloc();
11321 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011322 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011323 li->li_tv.vval.v_list = l2;
11324 if (l2 == NULL)
11325 break;
11326 ++l2->lv_refcount;
11327
11328 li2 = listitem_alloc();
11329 if (li2 == NULL)
11330 break;
11331 list_append(l2, li2);
11332 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011333 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011334 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11335
11336 li2 = listitem_alloc();
11337 if (li2 == NULL)
11338 break;
11339 list_append(l2, li2);
11340 copy_tv(&di->di_tv, &li2->li_tv);
11341 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011342 }
11343 }
11344}
11345
11346/*
11347 * "items(dict)" function
11348 */
11349 static void
11350f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011351 typval_T *argvars;
11352 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011353{
11354 dict_list(argvars, rettv, 2);
11355}
11356
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011358 * "join()" function
11359 */
11360 static void
11361f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011362 typval_T *argvars;
11363 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011364{
11365 garray_T ga;
11366 char_u *sep;
11367
11368 rettv->vval.v_number = 0;
11369 if (argvars[0].v_type != VAR_LIST)
11370 {
11371 EMSG(_(e_listreq));
11372 return;
11373 }
11374 if (argvars[0].vval.v_list == NULL)
11375 return;
11376 if (argvars[1].v_type == VAR_UNKNOWN)
11377 sep = (char_u *)" ";
11378 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011379 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011380
11381 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011382
11383 if (sep != NULL)
11384 {
11385 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011386 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011387 ga_append(&ga, NUL);
11388 rettv->vval.v_string = (char_u *)ga.ga_data;
11389 }
11390 else
11391 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011392}
11393
11394/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011395 * "keys()" function
11396 */
11397 static void
11398f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011399 typval_T *argvars;
11400 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011401{
11402 dict_list(argvars, rettv, 0);
11403}
11404
11405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 * "last_buffer_nr()" function.
11407 */
11408/*ARGSUSED*/
11409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011411 typval_T *argvars;
11412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413{
11414 int n = 0;
11415 buf_T *buf;
11416
11417 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11418 if (n < buf->b_fnum)
11419 n = buf->b_fnum;
11420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011422}
11423
11424/*
11425 * "len()" function
11426 */
11427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011428f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011429 typval_T *argvars;
11430 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011431{
11432 switch (argvars[0].v_type)
11433 {
11434 case VAR_STRING:
11435 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 rettv->vval.v_number = (varnumber_T)STRLEN(
11437 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011438 break;
11439 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011441 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011442 case VAR_DICT:
11443 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11444 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011445 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011446 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011447 break;
11448 }
11449}
11450
Bram Moolenaar33570922005-01-25 22:26:29 +000011451static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011452
11453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011455 typval_T *argvars;
11456 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011457 int type;
11458{
11459#ifdef FEAT_LIBCALL
11460 char_u *string_in;
11461 char_u **string_result;
11462 int nr_result;
11463#endif
11464
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011465 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011466 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011468 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011470
11471 if (check_restricted() || check_secure())
11472 return;
11473
11474#ifdef FEAT_LIBCALL
11475 /* The first two args must be strings, otherwise its meaningless */
11476 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11477 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011478 string_in = NULL;
11479 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011480 string_in = argvars[2].vval.v_string;
11481 if (type == VAR_NUMBER)
11482 string_result = NULL;
11483 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011485 if (mch_libcall(argvars[0].vval.v_string,
11486 argvars[1].vval.v_string,
11487 string_in,
11488 argvars[2].vval.v_number,
11489 string_result,
11490 &nr_result) == OK
11491 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011493 }
11494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495}
11496
11497/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011498 * "libcall()" function
11499 */
11500 static void
11501f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *argvars;
11503 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011504{
11505 libcall_common(argvars, rettv, VAR_STRING);
11506}
11507
11508/*
11509 * "libcallnr()" function
11510 */
11511 static void
11512f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011513 typval_T *argvars;
11514 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011515{
11516 libcall_common(argvars, rettv, VAR_NUMBER);
11517}
11518
11519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 * "line(string)" function
11521 */
11522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011523f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011524 typval_T *argvars;
11525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526{
11527 linenr_T lnum = 0;
11528 pos_T *fp;
11529
11530 fp = var2fpos(&argvars[0], TRUE);
11531 if (fp != NULL)
11532 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011533 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534}
11535
11536/*
11537 * "line2byte(lnum)" function
11538 */
11539/*ARGSUSED*/
11540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011542 typval_T *argvars;
11543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544{
11545#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547#else
11548 linenr_T lnum;
11549
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011550 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11555 if (rettv->vval.v_number >= 0)
11556 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557#endif
11558}
11559
11560/*
11561 * "lispindent(lnum)" function
11562 */
11563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011564f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011565 typval_T *argvars;
11566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567{
11568#ifdef FEAT_LISP
11569 pos_T pos;
11570 linenr_T lnum;
11571
11572 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011573 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11575 {
11576 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578 curwin->w_cursor = pos;
11579 }
11580 else
11581#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011582 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583}
11584
11585/*
11586 * "localtime()" function
11587 */
11588/*ARGSUSED*/
11589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011590f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011591 typval_T *argvars;
11592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011594 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595}
11596
Bram Moolenaar33570922005-01-25 22:26:29 +000011597static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598
11599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011600get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011601 typval_T *argvars;
11602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 int exact;
11604{
11605 char_u *keys;
11606 char_u *which;
11607 char_u buf[NUMBUFLEN];
11608 char_u *keys_buf = NULL;
11609 char_u *rhs;
11610 int mode;
11611 garray_T ga;
11612
11613 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614 rettv->v_type = VAR_STRING;
11615 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011617 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618 if (*keys == NUL)
11619 return;
11620
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011621 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011622 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623 else
11624 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011625 if (which == NULL)
11626 return;
11627
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 mode = get_map_mode(&which, 0);
11629
11630 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011631 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632 vim_free(keys_buf);
11633 if (rhs != NULL)
11634 {
11635 ga_init(&ga);
11636 ga.ga_itemsize = 1;
11637 ga.ga_growsize = 40;
11638
11639 while (*rhs != NUL)
11640 ga_concat(&ga, str2special(&rhs, FALSE));
11641
11642 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011643 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 }
11645}
11646
11647/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011648 * "map()" function
11649 */
11650 static void
11651f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011652 typval_T *argvars;
11653 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011654{
11655 filter_map(argvars, rettv, TRUE);
11656}
11657
11658/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011659 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 */
11661 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011663 typval_T *argvars;
11664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011666 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667}
11668
11669/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011670 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671 */
11672 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011673f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011674 typval_T *argvars;
11675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011677 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678}
11679
Bram Moolenaar33570922005-01-25 22:26:29 +000011680static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681
11682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011683find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011684 typval_T *argvars;
11685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686 int type;
11687{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011688 char_u *str = NULL;
11689 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690 char_u *pat;
11691 regmatch_T regmatch;
11692 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011693 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694 char_u *save_cpo;
11695 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011696 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011697 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011698 list_T *l = NULL;
11699 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011700 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011701 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702
11703 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11704 save_cpo = p_cpo;
11705 p_cpo = (char_u *)"";
11706
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011707 rettv->vval.v_number = -1;
11708 if (type == 3)
11709 {
11710 /* return empty list when there are no matches */
11711 if ((rettv->vval.v_list = list_alloc()) == NULL)
11712 goto theend;
11713 rettv->v_type = VAR_LIST;
11714 ++rettv->vval.v_list->lv_refcount;
11715 }
11716 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011718 rettv->v_type = VAR_STRING;
11719 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011722 if (argvars[0].v_type == VAR_LIST)
11723 {
11724 if ((l = argvars[0].vval.v_list) == NULL)
11725 goto theend;
11726 li = l->lv_first;
11727 }
11728 else
11729 expr = str = get_tv_string(&argvars[0]);
11730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011731 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11732 if (pat == NULL)
11733 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011734
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011735 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011737 int error = FALSE;
11738
11739 start = get_tv_number_chk(&argvars[2], &error);
11740 if (error)
11741 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011742 if (l != NULL)
11743 {
11744 li = list_find(l, start);
11745 if (li == NULL)
11746 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011747 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011748 }
11749 else
11750 {
11751 if (start < 0)
11752 start = 0;
11753 if (start > (long)STRLEN(str))
11754 goto theend;
11755 str += start;
11756 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011757
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011758 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011759 nth = get_tv_number_chk(&argvars[3], &error);
11760 if (error)
11761 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 }
11763
11764 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11765 if (regmatch.regprog != NULL)
11766 {
11767 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011768
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011769 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011770 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011771 if (l != NULL)
11772 {
11773 if (li == NULL)
11774 {
11775 match = FALSE;
11776 break;
11777 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011778 vim_free(tofree);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011779 str = echo_string(&li->li_tv, &tofree, strbuf,0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011780 if (str == NULL)
11781 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011782 }
11783
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011784 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011785
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011786 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011787 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011788 if (l == NULL && !match)
11789 break;
11790
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011791 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011792 if (l != NULL)
11793 {
11794 li = li->li_next;
11795 ++idx;
11796 }
11797 else
11798 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011799#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011800 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011801#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011802 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011803#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011804 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011805 }
11806
11807 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011809 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011810 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011811 int i;
11812
11813 /* return list with matched string and submatches */
11814 for (i = 0; i < NSUBEXP; ++i)
11815 {
11816 if (regmatch.endp[i] == NULL)
11817 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011818 if (list_append_string(rettv->vval.v_list,
11819 regmatch.startp[i],
11820 (int)(regmatch.endp[i] - regmatch.startp[i]))
11821 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011822 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011823 }
11824 }
11825 else if (type == 2)
11826 {
11827 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011828 if (l != NULL)
11829 copy_tv(&li->li_tv, rettv);
11830 else
11831 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011833 }
11834 else if (l != NULL)
11835 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 else
11837 {
11838 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011839 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 (varnumber_T)(regmatch.startp[0] - str);
11841 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011844 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 }
11846 }
11847 vim_free(regmatch.regprog);
11848 }
11849
11850theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011851 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 p_cpo = save_cpo;
11853}
11854
11855/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011856 * "match()" function
11857 */
11858 static void
11859f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011860 typval_T *argvars;
11861 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011862{
11863 find_some_match(argvars, rettv, 1);
11864}
11865
11866/*
11867 * "matchend()" function
11868 */
11869 static void
11870f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011871 typval_T *argvars;
11872 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011873{
11874 find_some_match(argvars, rettv, 0);
11875}
11876
11877/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011878 * "matchlist()" function
11879 */
11880 static void
11881f_matchlist(argvars, rettv)
11882 typval_T *argvars;
11883 typval_T *rettv;
11884{
11885 find_some_match(argvars, rettv, 3);
11886}
11887
11888/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011889 * "matchstr()" function
11890 */
11891 static void
11892f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011893 typval_T *argvars;
11894 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011895{
11896 find_some_match(argvars, rettv, 2);
11897}
11898
Bram Moolenaar33570922005-01-25 22:26:29 +000011899static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011900
11901 static void
11902max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011903 typval_T *argvars;
11904 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011905 int domax;
11906{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011907 long n = 0;
11908 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011909 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011910
11911 if (argvars[0].v_type == VAR_LIST)
11912 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011913 list_T *l;
11914 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011915
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011916 l = argvars[0].vval.v_list;
11917 if (l != NULL)
11918 {
11919 li = l->lv_first;
11920 if (li != NULL)
11921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011922 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011923 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011924 {
11925 li = li->li_next;
11926 if (li == NULL)
11927 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011928 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011929 if (domax ? i > n : i < n)
11930 n = i;
11931 }
11932 }
11933 }
11934 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011935 else if (argvars[0].v_type == VAR_DICT)
11936 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011937 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011938 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011939 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011940 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011941
11942 d = argvars[0].vval.v_dict;
11943 if (d != NULL)
11944 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011945 todo = d->dv_hashtab.ht_used;
11946 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011947 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011948 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011949 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011950 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011951 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011952 if (first)
11953 {
11954 n = i;
11955 first = FALSE;
11956 }
11957 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011958 n = i;
11959 }
11960 }
11961 }
11962 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011963 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011964 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011965 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011966}
11967
11968/*
11969 * "max()" function
11970 */
11971 static void
11972f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011973 typval_T *argvars;
11974 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011975{
11976 max_min(argvars, rettv, TRUE);
11977}
11978
11979/*
11980 * "min()" function
11981 */
11982 static void
11983f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011984 typval_T *argvars;
11985 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011986{
11987 max_min(argvars, rettv, FALSE);
11988}
11989
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011990static int mkdir_recurse __ARGS((char_u *dir, int prot));
11991
11992/*
11993 * Create the directory in which "dir" is located, and higher levels when
11994 * needed.
11995 */
11996 static int
11997mkdir_recurse(dir, prot)
11998 char_u *dir;
11999 int prot;
12000{
12001 char_u *p;
12002 char_u *updir;
12003 int r = FAIL;
12004
12005 /* Get end of directory name in "dir".
12006 * We're done when it's "/" or "c:/". */
12007 p = gettail_sep(dir);
12008 if (p <= get_past_head(dir))
12009 return OK;
12010
12011 /* If the directory exists we're done. Otherwise: create it.*/
12012 updir = vim_strnsave(dir, (int)(p - dir));
12013 if (updir == NULL)
12014 return FAIL;
12015 if (mch_isdir(updir))
12016 r = OK;
12017 else if (mkdir_recurse(updir, prot) == OK)
12018 r = vim_mkdir_emsg(updir, prot);
12019 vim_free(updir);
12020 return r;
12021}
12022
12023#ifdef vim_mkdir
12024/*
12025 * "mkdir()" function
12026 */
12027 static void
12028f_mkdir(argvars, rettv)
12029 typval_T *argvars;
12030 typval_T *rettv;
12031{
12032 char_u *dir;
12033 char_u buf[NUMBUFLEN];
12034 int prot = 0755;
12035
12036 rettv->vval.v_number = FAIL;
12037 if (check_restricted() || check_secure())
12038 return;
12039
12040 dir = get_tv_string_buf(&argvars[0], buf);
12041 if (argvars[1].v_type != VAR_UNKNOWN)
12042 {
12043 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012044 prot = get_tv_number_chk(&argvars[2], NULL);
12045 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012046 mkdir_recurse(dir, prot);
12047 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012048 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012049}
12050#endif
12051
Bram Moolenaar0d660222005-01-07 21:51:51 +000012052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 * "mode()" function
12054 */
12055/*ARGSUSED*/
12056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012058 typval_T *argvars;
12059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060{
12061 char_u buf[2];
12062
12063#ifdef FEAT_VISUAL
12064 if (VIsual_active)
12065 {
12066 if (VIsual_select)
12067 buf[0] = VIsual_mode + 's' - 'v';
12068 else
12069 buf[0] = VIsual_mode;
12070 }
12071 else
12072#endif
12073 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12074 buf[0] = 'r';
12075 else if (State & INSERT)
12076 {
12077 if (State & REPLACE_FLAG)
12078 buf[0] = 'R';
12079 else
12080 buf[0] = 'i';
12081 }
12082 else if (State & CMDLINE)
12083 buf[0] = 'c';
12084 else
12085 buf[0] = 'n';
12086
12087 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012088 rettv->vval.v_string = vim_strsave(buf);
12089 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090}
12091
12092/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012093 * "nextnonblank()" function
12094 */
12095 static void
12096f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012097 typval_T *argvars;
12098 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012099{
12100 linenr_T lnum;
12101
12102 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012104 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012105 {
12106 lnum = 0;
12107 break;
12108 }
12109 if (*skipwhite(ml_get(lnum)) != NUL)
12110 break;
12111 }
12112 rettv->vval.v_number = lnum;
12113}
12114
12115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 * "nr2char()" function
12117 */
12118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012119f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012120 typval_T *argvars;
12121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122{
12123 char_u buf[NUMBUFLEN];
12124
12125#ifdef FEAT_MBYTE
12126 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012127 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128 else
12129#endif
12130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012131 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 buf[1] = NUL;
12133 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134 rettv->v_type = VAR_STRING;
12135 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012136}
12137
12138/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012139 * "prevnonblank()" function
12140 */
12141 static void
12142f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012143 typval_T *argvars;
12144 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012145{
12146 linenr_T lnum;
12147
12148 lnum = get_tv_lnum(argvars);
12149 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12150 lnum = 0;
12151 else
12152 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12153 --lnum;
12154 rettv->vval.v_number = lnum;
12155}
12156
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012157#ifdef HAVE_STDARG_H
12158/* This dummy va_list is here because:
12159 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12160 * - locally in the function results in a "used before set" warning
12161 * - using va_start() to initialize it gives "function with fixed args" error */
12162static va_list ap;
12163#endif
12164
Bram Moolenaar8c711452005-01-14 21:53:12 +000012165/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012166 * "printf()" function
12167 */
12168 static void
12169f_printf(argvars, rettv)
12170 typval_T *argvars;
12171 typval_T *rettv;
12172{
12173 rettv->v_type = VAR_STRING;
12174 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012175#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012176 {
12177 char_u buf[NUMBUFLEN];
12178 int len;
12179 char_u *s;
12180 int saved_did_emsg = did_emsg;
12181 char *fmt;
12182
12183 /* Get the required length, allocate the buffer and do it for real. */
12184 did_emsg = FALSE;
12185 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012186 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012187 if (!did_emsg)
12188 {
12189 s = alloc(len + 1);
12190 if (s != NULL)
12191 {
12192 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012193 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012194 }
12195 }
12196 did_emsg |= saved_did_emsg;
12197 }
12198#endif
12199}
12200
12201/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012202 * "range()" function
12203 */
12204 static void
12205f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012206 typval_T *argvars;
12207 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012208{
12209 long start;
12210 long end;
12211 long stride = 1;
12212 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012213 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012214 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012216 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012217 if (argvars[1].v_type == VAR_UNKNOWN)
12218 {
12219 end = start - 1;
12220 start = 0;
12221 }
12222 else
12223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012224 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012225 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012226 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012227 }
12228
12229 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012230 if (error)
12231 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012232 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012233 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012234 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012235 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012236 else
12237 {
12238 l = list_alloc();
12239 if (l != NULL)
12240 {
12241 rettv->v_type = VAR_LIST;
12242 rettv->vval.v_list = l;
12243 ++l->lv_refcount;
12244
12245 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012246 if (list_append_number(l, (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012247 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012248 }
12249 }
12250}
12251
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012252/*
12253 * "readfile()" function
12254 */
12255 static void
12256f_readfile(argvars, rettv)
12257 typval_T *argvars;
12258 typval_T *rettv;
12259{
12260 int binary = FALSE;
12261 char_u *fname;
12262 FILE *fd;
12263 list_T *l;
12264 listitem_T *li;
12265#define FREAD_SIZE 200 /* optimized for text lines */
12266 char_u buf[FREAD_SIZE];
12267 int readlen; /* size of last fread() */
12268 int buflen; /* nr of valid chars in buf[] */
12269 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12270 int tolist; /* first byte in buf[] still to be put in list */
12271 int chop; /* how many CR to chop off */
12272 char_u *prev = NULL; /* previously read bytes, if any */
12273 int prevlen = 0; /* length of "prev" if not NULL */
12274 char_u *s;
12275 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012276 long maxline = MAXLNUM;
12277 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012278
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012279 if (argvars[1].v_type != VAR_UNKNOWN)
12280 {
12281 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12282 binary = TRUE;
12283 if (argvars[2].v_type != VAR_UNKNOWN)
12284 maxline = get_tv_number(&argvars[2]);
12285 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012286
12287 l = list_alloc();
12288 if (l == NULL)
12289 return;
12290 rettv->v_type = VAR_LIST;
12291 rettv->vval.v_list = l;
12292 l->lv_refcount = 1;
12293
12294 /* Always open the file in binary mode, library functions have a mind of
12295 * their own about CR-LF conversion. */
12296 fname = get_tv_string(&argvars[0]);
12297 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12298 {
12299 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12300 return;
12301 }
12302
12303 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012304 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012305 {
12306 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12307 buflen = filtd + readlen;
12308 tolist = 0;
12309 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12310 {
12311 if (buf[filtd] == '\n' || readlen <= 0)
12312 {
12313 /* Only when in binary mode add an empty list item when the
12314 * last line ends in a '\n'. */
12315 if (!binary && readlen == 0 && filtd == 0)
12316 break;
12317
12318 /* Found end-of-line or end-of-file: add a text line to the
12319 * list. */
12320 chop = 0;
12321 if (!binary)
12322 while (filtd - chop - 1 >= tolist
12323 && buf[filtd - chop - 1] == '\r')
12324 ++chop;
12325 len = filtd - tolist - chop;
12326 if (prev == NULL)
12327 s = vim_strnsave(buf + tolist, len);
12328 else
12329 {
12330 s = alloc((unsigned)(prevlen + len + 1));
12331 if (s != NULL)
12332 {
12333 mch_memmove(s, prev, prevlen);
12334 vim_free(prev);
12335 prev = NULL;
12336 mch_memmove(s + prevlen, buf + tolist, len);
12337 s[prevlen + len] = NUL;
12338 }
12339 }
12340 tolist = filtd + 1;
12341
12342 li = listitem_alloc();
12343 if (li == NULL)
12344 {
12345 vim_free(s);
12346 break;
12347 }
12348 li->li_tv.v_type = VAR_STRING;
12349 li->li_tv.v_lock = 0;
12350 li->li_tv.vval.v_string = s;
12351 list_append(l, li);
12352
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012353 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012354 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012355 if (readlen <= 0)
12356 break;
12357 }
12358 else if (buf[filtd] == NUL)
12359 buf[filtd] = '\n';
12360 }
12361 if (readlen <= 0)
12362 break;
12363
12364 if (tolist == 0)
12365 {
12366 /* "buf" is full, need to move text to an allocated buffer */
12367 if (prev == NULL)
12368 {
12369 prev = vim_strnsave(buf, buflen);
12370 prevlen = buflen;
12371 }
12372 else
12373 {
12374 s = alloc((unsigned)(prevlen + buflen));
12375 if (s != NULL)
12376 {
12377 mch_memmove(s, prev, prevlen);
12378 mch_memmove(s + prevlen, buf, buflen);
12379 vim_free(prev);
12380 prev = s;
12381 prevlen += buflen;
12382 }
12383 }
12384 filtd = 0;
12385 }
12386 else
12387 {
12388 mch_memmove(buf, buf + tolist, buflen - tolist);
12389 filtd -= tolist;
12390 }
12391 }
12392
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012393 /*
12394 * For a negative line count use only the lines at the end of the file,
12395 * free the rest.
12396 */
12397 if (maxline < 0)
12398 while (cnt > -maxline)
12399 {
12400 listitem_remove(l, l->lv_first);
12401 --cnt;
12402 }
12403
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012404 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012405 fclose(fd);
12406}
12407
12408
Bram Moolenaar0d660222005-01-07 21:51:51 +000012409#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12410static void make_connection __ARGS((void));
12411static int check_connection __ARGS((void));
12412
12413 static void
12414make_connection()
12415{
12416 if (X_DISPLAY == NULL
12417# ifdef FEAT_GUI
12418 && !gui.in_use
12419# endif
12420 )
12421 {
12422 x_force_connect = TRUE;
12423 setup_term_clip();
12424 x_force_connect = FALSE;
12425 }
12426}
12427
12428 static int
12429check_connection()
12430{
12431 make_connection();
12432 if (X_DISPLAY == NULL)
12433 {
12434 EMSG(_("E240: No connection to Vim server"));
12435 return FAIL;
12436 }
12437 return OK;
12438}
12439#endif
12440
12441#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012442static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012443
12444 static void
12445remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012446 typval_T *argvars;
12447 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012448 int expr;
12449{
12450 char_u *server_name;
12451 char_u *keys;
12452 char_u *r = NULL;
12453 char_u buf[NUMBUFLEN];
12454# ifdef WIN32
12455 HWND w;
12456# else
12457 Window w;
12458# endif
12459
12460 if (check_restricted() || check_secure())
12461 return;
12462
12463# ifdef FEAT_X11
12464 if (check_connection() == FAIL)
12465 return;
12466# endif
12467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012468 server_name = get_tv_string_chk(&argvars[0]);
12469 if (server_name == NULL)
12470 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012471 keys = get_tv_string_buf(&argvars[1], buf);
12472# ifdef WIN32
12473 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12474# else
12475 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12476 < 0)
12477# endif
12478 {
12479 if (r != NULL)
12480 EMSG(r); /* sending worked but evaluation failed */
12481 else
12482 EMSG2(_("E241: Unable to send to %s"), server_name);
12483 return;
12484 }
12485
12486 rettv->vval.v_string = r;
12487
12488 if (argvars[2].v_type != VAR_UNKNOWN)
12489 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012490 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012491 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012492 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012493
12494 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012495 v.di_tv.v_type = VAR_STRING;
12496 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012497 idvar = get_tv_string_chk(&argvars[2]);
12498 if (idvar != NULL)
12499 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012500 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012501 }
12502}
12503#endif
12504
12505/*
12506 * "remote_expr()" function
12507 */
12508/*ARGSUSED*/
12509 static void
12510f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012511 typval_T *argvars;
12512 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012513{
12514 rettv->v_type = VAR_STRING;
12515 rettv->vval.v_string = NULL;
12516#ifdef FEAT_CLIENTSERVER
12517 remote_common(argvars, rettv, TRUE);
12518#endif
12519}
12520
12521/*
12522 * "remote_foreground()" function
12523 */
12524/*ARGSUSED*/
12525 static void
12526f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012527 typval_T *argvars;
12528 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012529{
12530 rettv->vval.v_number = 0;
12531#ifdef FEAT_CLIENTSERVER
12532# ifdef WIN32
12533 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012534 {
12535 char_u *server_name = get_tv_string_chk(&argvars[0]);
12536
12537 if (server_name != NULL)
12538 serverForeground(server_name);
12539 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540# else
12541 /* Send a foreground() expression to the server. */
12542 argvars[1].v_type = VAR_STRING;
12543 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12544 argvars[2].v_type = VAR_UNKNOWN;
12545 remote_common(argvars, rettv, TRUE);
12546 vim_free(argvars[1].vval.v_string);
12547# endif
12548#endif
12549}
12550
12551/*ARGSUSED*/
12552 static void
12553f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012554 typval_T *argvars;
12555 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012556{
12557#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012558 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012559 char_u *s = NULL;
12560# ifdef WIN32
12561 int n = 0;
12562# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012563 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012564
12565 if (check_restricted() || check_secure())
12566 {
12567 rettv->vval.v_number = -1;
12568 return;
12569 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012570 serverid = get_tv_string_chk(&argvars[0]);
12571 if (serverid == NULL)
12572 {
12573 rettv->vval.v_number = -1;
12574 return; /* type error; errmsg already given */
12575 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012576# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012577 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012578 if (n == 0)
12579 rettv->vval.v_number = -1;
12580 else
12581 {
12582 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12583 rettv->vval.v_number = (s != NULL);
12584 }
12585# else
12586 rettv->vval.v_number = 0;
12587 if (check_connection() == FAIL)
12588 return;
12589
12590 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012591 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012592# endif
12593
12594 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12595 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012596 char_u *retvar;
12597
Bram Moolenaar33570922005-01-25 22:26:29 +000012598 v.di_tv.v_type = VAR_STRING;
12599 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012600 retvar = get_tv_string_chk(&argvars[1]);
12601 if (retvar != NULL)
12602 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012603 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012604 }
12605#else
12606 rettv->vval.v_number = -1;
12607#endif
12608}
12609
12610/*ARGSUSED*/
12611 static void
12612f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012613 typval_T *argvars;
12614 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012615{
12616 char_u *r = NULL;
12617
12618#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012619 char_u *serverid = get_tv_string_chk(&argvars[0]);
12620
12621 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012622 {
12623# ifdef WIN32
12624 /* The server's HWND is encoded in the 'id' parameter */
12625 int n = 0;
12626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012627 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012628 if (n != 0)
12629 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12630 if (r == NULL)
12631# else
12632 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012633 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012634# endif
12635 EMSG(_("E277: Unable to read a server reply"));
12636 }
12637#endif
12638 rettv->v_type = VAR_STRING;
12639 rettv->vval.v_string = r;
12640}
12641
12642/*
12643 * "remote_send()" function
12644 */
12645/*ARGSUSED*/
12646 static void
12647f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012648 typval_T *argvars;
12649 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012650{
12651 rettv->v_type = VAR_STRING;
12652 rettv->vval.v_string = NULL;
12653#ifdef FEAT_CLIENTSERVER
12654 remote_common(argvars, rettv, FALSE);
12655#endif
12656}
12657
12658/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012659 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012660 */
12661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012662f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012663 typval_T *argvars;
12664 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012665{
Bram Moolenaar33570922005-01-25 22:26:29 +000012666 list_T *l;
12667 listitem_T *item, *item2;
12668 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012669 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012670 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012671 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012672 dict_T *d;
12673 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012674
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012675 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012676 if (argvars[0].v_type == VAR_DICT)
12677 {
12678 if (argvars[2].v_type != VAR_UNKNOWN)
12679 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012680 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012681 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012683 key = get_tv_string_chk(&argvars[1]);
12684 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012686 di = dict_find(d, key, -1);
12687 if (di == NULL)
12688 EMSG2(_(e_dictkey), key);
12689 else
12690 {
12691 *rettv = di->di_tv;
12692 init_tv(&di->di_tv);
12693 dictitem_remove(d, di);
12694 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012696 }
12697 }
12698 else if (argvars[0].v_type != VAR_LIST)
12699 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012700 else if ((l = argvars[0].vval.v_list) != NULL
12701 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012702 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012703 int error = FALSE;
12704
12705 idx = get_tv_number_chk(&argvars[1], &error);
12706 if (error)
12707 ; /* type error: do nothing, errmsg already given */
12708 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012709 EMSGN(_(e_listidx), idx);
12710 else
12711 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012712 if (argvars[2].v_type == VAR_UNKNOWN)
12713 {
12714 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012715 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012716 *rettv = item->li_tv;
12717 vim_free(item);
12718 }
12719 else
12720 {
12721 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012722 end = get_tv_number_chk(&argvars[2], &error);
12723 if (error)
12724 ; /* type error: do nothing */
12725 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012726 EMSGN(_(e_listidx), end);
12727 else
12728 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012729 int cnt = 0;
12730
12731 for (li = item; li != NULL; li = li->li_next)
12732 {
12733 ++cnt;
12734 if (li == item2)
12735 break;
12736 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012737 if (li == NULL) /* didn't find "item2" after "item" */
12738 EMSG(_(e_invrange));
12739 else
12740 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012741 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012742 l = list_alloc();
12743 if (l != NULL)
12744 {
12745 rettv->v_type = VAR_LIST;
12746 rettv->vval.v_list = l;
12747 l->lv_first = item;
12748 l->lv_last = item2;
12749 l->lv_refcount = 1;
12750 item->li_prev = NULL;
12751 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012752 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012753 }
12754 }
12755 }
12756 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012757 }
12758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759}
12760
12761/*
12762 * "rename({from}, {to})" function
12763 */
12764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012765f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012766 typval_T *argvars;
12767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768{
12769 char_u buf[NUMBUFLEN];
12770
12771 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12775 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776}
12777
12778/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012779 * "repeat()" function
12780 */
12781/*ARGSUSED*/
12782 static void
12783f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012784 typval_T *argvars;
12785 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012786{
12787 char_u *p;
12788 int n;
12789 int slen;
12790 int len;
12791 char_u *r;
12792 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012793 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012794
12795 n = get_tv_number(&argvars[1]);
12796 if (argvars[0].v_type == VAR_LIST)
12797 {
12798 l = list_alloc();
12799 if (l != NULL && argvars[0].vval.v_list != NULL)
12800 {
12801 l->lv_refcount = 1;
12802 while (n-- > 0)
12803 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12804 break;
12805 }
12806 rettv->v_type = VAR_LIST;
12807 rettv->vval.v_list = l;
12808 }
12809 else
12810 {
12811 p = get_tv_string(&argvars[0]);
12812 rettv->v_type = VAR_STRING;
12813 rettv->vval.v_string = NULL;
12814
12815 slen = (int)STRLEN(p);
12816 len = slen * n;
12817 if (len <= 0)
12818 return;
12819
12820 r = alloc(len + 1);
12821 if (r != NULL)
12822 {
12823 for (i = 0; i < n; i++)
12824 mch_memmove(r + i * slen, p, (size_t)slen);
12825 r[len] = NUL;
12826 }
12827
12828 rettv->vval.v_string = r;
12829 }
12830}
12831
12832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833 * "resolve()" function
12834 */
12835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012836f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012837 typval_T *argvars;
12838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839{
12840 char_u *p;
12841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843#ifdef FEAT_SHORTCUT
12844 {
12845 char_u *v = NULL;
12846
12847 v = mch_resolve_shortcut(p);
12848 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012849 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 }
12853#else
12854# ifdef HAVE_READLINK
12855 {
12856 char_u buf[MAXPATHL + 1];
12857 char_u *cpy;
12858 int len;
12859 char_u *remain = NULL;
12860 char_u *q;
12861 int is_relative_to_current = FALSE;
12862 int has_trailing_pathsep = FALSE;
12863 int limit = 100;
12864
12865 p = vim_strsave(p);
12866
12867 if (p[0] == '.' && (vim_ispathsep(p[1])
12868 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12869 is_relative_to_current = TRUE;
12870
12871 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012872 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 has_trailing_pathsep = TRUE;
12874
12875 q = getnextcomp(p);
12876 if (*q != NUL)
12877 {
12878 /* Separate the first path component in "p", and keep the
12879 * remainder (beginning with the path separator). */
12880 remain = vim_strsave(q - 1);
12881 q[-1] = NUL;
12882 }
12883
12884 for (;;)
12885 {
12886 for (;;)
12887 {
12888 len = readlink((char *)p, (char *)buf, MAXPATHL);
12889 if (len <= 0)
12890 break;
12891 buf[len] = NUL;
12892
12893 if (limit-- == 0)
12894 {
12895 vim_free(p);
12896 vim_free(remain);
12897 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 goto fail;
12900 }
12901
12902 /* Ensure that the result will have a trailing path separator
12903 * if the argument has one. */
12904 if (remain == NULL && has_trailing_pathsep)
12905 add_pathsep(buf);
12906
12907 /* Separate the first path component in the link value and
12908 * concatenate the remainders. */
12909 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12910 if (*q != NUL)
12911 {
12912 if (remain == NULL)
12913 remain = vim_strsave(q - 1);
12914 else
12915 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000012916 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917 if (cpy != NULL)
12918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 vim_free(remain);
12920 remain = cpy;
12921 }
12922 }
12923 q[-1] = NUL;
12924 }
12925
12926 q = gettail(p);
12927 if (q > p && *q == NUL)
12928 {
12929 /* Ignore trailing path separator. */
12930 q[-1] = NUL;
12931 q = gettail(p);
12932 }
12933 if (q > p && !mch_isFullName(buf))
12934 {
12935 /* symlink is relative to directory of argument */
12936 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12937 if (cpy != NULL)
12938 {
12939 STRCPY(cpy, p);
12940 STRCPY(gettail(cpy), buf);
12941 vim_free(p);
12942 p = cpy;
12943 }
12944 }
12945 else
12946 {
12947 vim_free(p);
12948 p = vim_strsave(buf);
12949 }
12950 }
12951
12952 if (remain == NULL)
12953 break;
12954
12955 /* Append the first path component of "remain" to "p". */
12956 q = getnextcomp(remain + 1);
12957 len = q - remain - (*q != NUL);
12958 cpy = vim_strnsave(p, STRLEN(p) + len);
12959 if (cpy != NULL)
12960 {
12961 STRNCAT(cpy, remain, len);
12962 vim_free(p);
12963 p = cpy;
12964 }
12965 /* Shorten "remain". */
12966 if (*q != NUL)
12967 STRCPY(remain, q - 1);
12968 else
12969 {
12970 vim_free(remain);
12971 remain = NULL;
12972 }
12973 }
12974
12975 /* If the result is a relative path name, make it explicitly relative to
12976 * the current directory if and only if the argument had this form. */
12977 if (!vim_ispathsep(*p))
12978 {
12979 if (is_relative_to_current
12980 && *p != NUL
12981 && !(p[0] == '.'
12982 && (p[1] == NUL
12983 || vim_ispathsep(p[1])
12984 || (p[1] == '.'
12985 && (p[2] == NUL
12986 || vim_ispathsep(p[2]))))))
12987 {
12988 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012989 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990 if (cpy != NULL)
12991 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992 vim_free(p);
12993 p = cpy;
12994 }
12995 }
12996 else if (!is_relative_to_current)
12997 {
12998 /* Strip leading "./". */
12999 q = p;
13000 while (q[0] == '.' && vim_ispathsep(q[1]))
13001 q += 2;
13002 if (q > p)
13003 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13004 }
13005 }
13006
13007 /* Ensure that the result will have no trailing path separator
13008 * if the argument had none. But keep "/" or "//". */
13009 if (!has_trailing_pathsep)
13010 {
13011 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013012 if (after_pathsep(p, q))
13013 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014 }
13015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013016 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017 }
13018# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013019 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020# endif
13021#endif
13022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013023 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024
13025#ifdef HAVE_READLINK
13026fail:
13027#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013028 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029}
13030
13031/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013032 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033 */
13034 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013035f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013036 typval_T *argvars;
13037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038{
Bram Moolenaar33570922005-01-25 22:26:29 +000013039 list_T *l;
13040 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041
Bram Moolenaar0d660222005-01-07 21:51:51 +000013042 rettv->vval.v_number = 0;
13043 if (argvars[0].v_type != VAR_LIST)
13044 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013045 else if ((l = argvars[0].vval.v_list) != NULL
13046 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013047 {
13048 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013049 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013050 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013051 while (li != NULL)
13052 {
13053 ni = li->li_prev;
13054 list_append(l, li);
13055 li = ni;
13056 }
13057 rettv->vval.v_list = l;
13058 rettv->v_type = VAR_LIST;
13059 ++l->lv_refcount;
13060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061}
13062
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013063#define SP_NOMOVE 1 /* don't move cursor */
13064#define SP_REPEAT 2 /* repeat to find outer pair */
13065#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000013066#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013067
Bram Moolenaar33570922005-01-25 22:26:29 +000013068static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013069
13070/*
13071 * Get flags for a search function.
13072 * Possibly sets "p_ws".
13073 * Returns BACKWARD, FORWARD or zero (for an error).
13074 */
13075 static int
13076get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013077 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013078 int *flagsp;
13079{
13080 int dir = FORWARD;
13081 char_u *flags;
13082 char_u nbuf[NUMBUFLEN];
13083 int mask;
13084
13085 if (varp->v_type != VAR_UNKNOWN)
13086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013087 flags = get_tv_string_buf_chk(varp, nbuf);
13088 if (flags == NULL)
13089 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013090 while (*flags != NUL)
13091 {
13092 switch (*flags)
13093 {
13094 case 'b': dir = BACKWARD; break;
13095 case 'w': p_ws = TRUE; break;
13096 case 'W': p_ws = FALSE; break;
13097 default: mask = 0;
13098 if (flagsp != NULL)
13099 switch (*flags)
13100 {
13101 case 'n': mask = SP_NOMOVE; break;
13102 case 'r': mask = SP_REPEAT; break;
13103 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013104 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013105 }
13106 if (mask == 0)
13107 {
13108 EMSG2(_(e_invarg2), flags);
13109 dir = 0;
13110 }
13111 else
13112 *flagsp |= mask;
13113 }
13114 if (dir == 0)
13115 break;
13116 ++flags;
13117 }
13118 }
13119 return dir;
13120}
13121
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122/*
13123 * "search()" function
13124 */
13125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013126f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013127 typval_T *argvars;
13128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129{
13130 char_u *pat;
13131 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013132 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133 int save_p_ws = p_ws;
13134 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013135 int flags = 0;
13136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013137 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013140 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13141 if (dir == 0)
13142 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013143 /*
13144 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13145 * Check to make sure only those flags are set.
13146 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13147 * flags cannot be set. Check for that condition also.
13148 */
13149 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13150 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013152 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013153 goto theend;
13154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013156 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13158 SEARCH_KEEP, RE_SEARCH) != FAIL)
13159 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013160 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013161 if (flags & SP_SETPCMARK)
13162 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 curwin->w_cursor = pos;
13164 /* "/$" will put the cursor after the end of the line, may need to
13165 * correct that here */
13166 check_cursor();
13167 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013168
13169 /* If 'n' flag is used: restore cursor position. */
13170 if (flags & SP_NOMOVE)
13171 curwin->w_cursor = save_cursor;
13172theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 p_ws = save_p_ws;
13174}
13175
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013177 * "searchdecl()" function
13178 */
13179 static void
13180f_searchdecl(argvars, rettv)
13181 typval_T *argvars;
13182 typval_T *rettv;
13183{
13184 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013185 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013186 int error = FALSE;
13187 char_u *name;
13188
13189 rettv->vval.v_number = 1; /* default: FAIL */
13190
13191 name = get_tv_string_chk(&argvars[0]);
13192 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013193 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013194 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013195 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13196 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13197 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013198 if (!error && name != NULL)
13199 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013200 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013201}
13202
13203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 * "searchpair()" function
13205 */
13206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013208 typval_T *argvars;
13209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210{
13211 char_u *spat, *mpat, *epat;
13212 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 int dir;
13215 int flags = 0;
13216 char_u nbuf1[NUMBUFLEN];
13217 char_u nbuf2[NUMBUFLEN];
13218 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013223 spat = get_tv_string_chk(&argvars[0]);
13224 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13225 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13226 if (spat == NULL || mpat == NULL || epat == NULL)
13227 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 /* Handle the optional fourth argument: flags */
13230 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013231 if (dir == 0)
13232 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013233 /*
13234 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13235 */
13236 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13237 {
13238 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13239 goto theend;
13240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241
13242 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013243 if (argvars[3].v_type == VAR_UNKNOWN
13244 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245 skip = (char_u *)"";
13246 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013247 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13248 if (skip == NULL)
13249 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013251 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13252
13253theend:
13254 p_ws = save_p_ws;
13255}
13256
13257/*
13258 * Search for a start/middle/end thing.
13259 * Used by searchpair(), see its documentation for the details.
13260 * Returns 0 or -1 for no match,
13261 */
13262 long
13263do_searchpair(spat, mpat, epat, dir, skip, flags)
13264 char_u *spat; /* start pattern */
13265 char_u *mpat; /* middle pattern */
13266 char_u *epat; /* end pattern */
13267 int dir; /* BACKWARD or FORWARD */
13268 char_u *skip; /* skip expression */
13269 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13270{
13271 char_u *save_cpo;
13272 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13273 long retval = 0;
13274 pos_T pos;
13275 pos_T firstpos;
13276 pos_T foundpos;
13277 pos_T save_cursor;
13278 pos_T save_pos;
13279 int n;
13280 int r;
13281 int nest = 1;
13282 int err;
13283
13284 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13285 save_cpo = p_cpo;
13286 p_cpo = (char_u *)"";
13287
13288 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13289 * start/middle/end (pat3, for the top pair). */
13290 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13291 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13292 if (pat2 == NULL || pat3 == NULL)
13293 goto theend;
13294 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13295 if (*mpat == NUL)
13296 STRCPY(pat3, pat2);
13297 else
13298 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13299 spat, epat, mpat);
13300
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301 save_cursor = curwin->w_cursor;
13302 pos = curwin->w_cursor;
13303 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013304 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305 pat = pat3;
13306 for (;;)
13307 {
13308 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13309 SEARCH_KEEP, RE_SEARCH);
13310 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13311 /* didn't find it or found the first match again: FAIL */
13312 break;
13313
13314 if (firstpos.lnum == 0)
13315 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013316 if (equalpos(pos, foundpos))
13317 {
13318 /* Found the same position again. Can happen with a pattern that
13319 * has "\zs" at the end and searching backwards. Advance one
13320 * character and try again. */
13321 if (dir == BACKWARD)
13322 decl(&pos);
13323 else
13324 incl(&pos);
13325 }
13326 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327
13328 /* If the skip pattern matches, ignore this match. */
13329 if (*skip != NUL)
13330 {
13331 save_pos = curwin->w_cursor;
13332 curwin->w_cursor = pos;
13333 r = eval_to_bool(skip, &err, NULL, FALSE);
13334 curwin->w_cursor = save_pos;
13335 if (err)
13336 {
13337 /* Evaluating {skip} caused an error, break here. */
13338 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013339 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 break;
13341 }
13342 if (r)
13343 continue;
13344 }
13345
13346 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13347 {
13348 /* Found end when searching backwards or start when searching
13349 * forward: nested pair. */
13350 ++nest;
13351 pat = pat2; /* nested, don't search for middle */
13352 }
13353 else
13354 {
13355 /* Found end when searching forward or start when searching
13356 * backward: end of (nested) pair; or found middle in outer pair. */
13357 if (--nest == 1)
13358 pat = pat3; /* outer level, search for middle */
13359 }
13360
13361 if (nest == 0)
13362 {
13363 /* Found the match: return matchcount or line number. */
13364 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013365 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013367 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013368 if (flags & SP_SETPCMARK)
13369 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370 curwin->w_cursor = pos;
13371 if (!(flags & SP_REPEAT))
13372 break;
13373 nest = 1; /* search for next unmatched */
13374 }
13375 }
13376
13377 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013378 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 curwin->w_cursor = save_cursor;
13380
13381theend:
13382 vim_free(pat2);
13383 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013385
13386 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387}
13388
Bram Moolenaar0d660222005-01-07 21:51:51 +000013389/*ARGSUSED*/
13390 static void
13391f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013392 typval_T *argvars;
13393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013395#ifdef FEAT_CLIENTSERVER
13396 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013397 char_u *server = get_tv_string_chk(&argvars[0]);
13398 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399
Bram Moolenaar0d660222005-01-07 21:51:51 +000013400 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013401 if (server == NULL || reply == NULL)
13402 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013403 if (check_restricted() || check_secure())
13404 return;
13405# ifdef FEAT_X11
13406 if (check_connection() == FAIL)
13407 return;
13408# endif
13409
13410 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013412 EMSG(_("E258: Unable to send to client"));
13413 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013415 rettv->vval.v_number = 0;
13416#else
13417 rettv->vval.v_number = -1;
13418#endif
13419}
13420
13421/*ARGSUSED*/
13422 static void
13423f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013424 typval_T *argvars;
13425 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013426{
13427 char_u *r = NULL;
13428
13429#ifdef FEAT_CLIENTSERVER
13430# ifdef WIN32
13431 r = serverGetVimNames();
13432# else
13433 make_connection();
13434 if (X_DISPLAY != NULL)
13435 r = serverGetVimNames(X_DISPLAY);
13436# endif
13437#endif
13438 rettv->v_type = VAR_STRING;
13439 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440}
13441
13442/*
13443 * "setbufvar()" function
13444 */
13445/*ARGSUSED*/
13446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013447f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013448 typval_T *argvars;
13449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450{
13451 buf_T *buf;
13452#ifdef FEAT_AUTOCMD
13453 aco_save_T aco;
13454#else
13455 buf_T *save_curbuf;
13456#endif
13457 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013458 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459 char_u nbuf[NUMBUFLEN];
13460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013461 rettv->vval.v_number = 0;
13462
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463 if (check_restricted() || check_secure())
13464 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013465 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13466 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013467 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 varp = &argvars[2];
13469
13470 if (buf != NULL && varname != NULL && varp != NULL)
13471 {
13472 /* set curbuf to be our buf, temporarily */
13473#ifdef FEAT_AUTOCMD
13474 aucmd_prepbuf(&aco, buf);
13475#else
13476 save_curbuf = curbuf;
13477 curbuf = buf;
13478#endif
13479
13480 if (*varname == '&')
13481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013482 long numval;
13483 char_u *strval;
13484 int error = FALSE;
13485
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013487 numval = get_tv_number_chk(varp, &error);
13488 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013489 if (!error && strval != NULL)
13490 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491 }
13492 else
13493 {
13494 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13495 if (bufvarname != NULL)
13496 {
13497 STRCPY(bufvarname, "b:");
13498 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013499 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 vim_free(bufvarname);
13501 }
13502 }
13503
13504 /* reset notion of buffer */
13505#ifdef FEAT_AUTOCMD
13506 aucmd_restbuf(&aco);
13507#else
13508 curbuf = save_curbuf;
13509#endif
13510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511}
13512
13513/*
13514 * "setcmdpos()" function
13515 */
13516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013517f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013518 typval_T *argvars;
13519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013521 int pos = (int)get_tv_number(&argvars[0]) - 1;
13522
13523 if (pos >= 0)
13524 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525}
13526
13527/*
13528 * "setline()" function
13529 */
13530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013531f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013532 typval_T *argvars;
13533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534{
13535 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013536 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013537 list_T *l = NULL;
13538 listitem_T *li = NULL;
13539 long added = 0;
13540 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013542 lnum = get_tv_lnum(&argvars[0]);
13543 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013545 l = argvars[1].vval.v_list;
13546 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013548 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013549 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013550
13551 rettv->vval.v_number = 0; /* OK */
13552 for (;;)
13553 {
13554 if (l != NULL)
13555 {
13556 /* list argument, get next string */
13557 if (li == NULL)
13558 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013559 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013560 li = li->li_next;
13561 }
13562
13563 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013564 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013565 break;
13566 if (lnum <= curbuf->b_ml.ml_line_count)
13567 {
13568 /* existing line, replace it */
13569 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13570 {
13571 changed_bytes(lnum, 0);
13572 check_cursor_col();
13573 rettv->vval.v_number = 0; /* OK */
13574 }
13575 }
13576 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13577 {
13578 /* lnum is one past the last line, append the line */
13579 ++added;
13580 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13581 rettv->vval.v_number = 0; /* OK */
13582 }
13583
13584 if (l == NULL) /* only one string argument */
13585 break;
13586 ++lnum;
13587 }
13588
13589 if (added > 0)
13590 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591}
13592
13593/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013594 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013595 */
13596/*ARGSUSED*/
13597 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013598set_qf_ll_list(wp, list_arg, action_arg, rettv)
13599 win_T *wp;
13600 typval_T *list_arg;
13601 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013602 typval_T *rettv;
13603{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013604#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013605 char_u *act;
13606 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013607#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013608
Bram Moolenaar2641f772005-03-25 21:58:17 +000013609 rettv->vval.v_number = -1;
13610
13611#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013612 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013613 EMSG(_(e_listreq));
13614 else
13615 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013616 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013617
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013618 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013619 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013620 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 if (act == NULL)
13622 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013623 if (*act == 'a' || *act == 'r')
13624 action = *act;
13625 }
13626
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013627 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013628 rettv->vval.v_number = 0;
13629 }
13630#endif
13631}
13632
13633/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013634 * "setloclist()" function
13635 */
13636/*ARGSUSED*/
13637 static void
13638f_setloclist(argvars, rettv)
13639 typval_T *argvars;
13640 typval_T *rettv;
13641{
13642 win_T *win;
13643
13644 rettv->vval.v_number = -1;
13645
13646 win = find_win_by_nr(&argvars[0]);
13647 if (win != NULL)
13648 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13649}
13650
13651/*
13652 * "setqflist()" function
13653 */
13654/*ARGSUSED*/
13655 static void
13656f_setqflist(argvars, rettv)
13657 typval_T *argvars;
13658 typval_T *rettv;
13659{
13660 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
13661}
13662
13663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664 * "setreg()" function
13665 */
13666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013667f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013668 typval_T *argvars;
13669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670{
13671 int regname;
13672 char_u *strregname;
13673 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013674 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 int append;
13676 char_u yank_type;
13677 long block_len;
13678
13679 block_len = -1;
13680 yank_type = MAUTO;
13681 append = FALSE;
13682
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013683 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013684 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013686 if (strregname == NULL)
13687 return; /* type error; errmsg already given */
13688 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 if (regname == 0 || regname == '@')
13690 regname = '"';
13691 else if (regname == '=')
13692 return;
13693
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013694 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013696 stropt = get_tv_string_chk(&argvars[2]);
13697 if (stropt == NULL)
13698 return; /* type error */
13699 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700 switch (*stropt)
13701 {
13702 case 'a': case 'A': /* append */
13703 append = TRUE;
13704 break;
13705 case 'v': case 'c': /* character-wise selection */
13706 yank_type = MCHAR;
13707 break;
13708 case 'V': case 'l': /* line-wise selection */
13709 yank_type = MLINE;
13710 break;
13711#ifdef FEAT_VISUAL
13712 case 'b': case Ctrl_V: /* block-wise selection */
13713 yank_type = MBLOCK;
13714 if (VIM_ISDIGIT(stropt[1]))
13715 {
13716 ++stropt;
13717 block_len = getdigits(&stropt) - 1;
13718 --stropt;
13719 }
13720 break;
13721#endif
13722 }
13723 }
13724
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013725 strval = get_tv_string_chk(&argvars[1]);
13726 if (strval != NULL)
13727 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730}
13731
13732
13733/*
13734 * "setwinvar(expr)" function
13735 */
13736/*ARGSUSED*/
13737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013738f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013739 typval_T *argvars;
13740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741{
13742 win_T *win;
13743#ifdef FEAT_WINDOWS
13744 win_T *save_curwin;
13745#endif
13746 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013747 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 char_u nbuf[NUMBUFLEN];
13749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013750 rettv->vval.v_number = 0;
13751
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 if (check_restricted() || check_secure())
13753 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013755 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 varp = &argvars[2];
13757
13758 if (win != NULL && varname != NULL && varp != NULL)
13759 {
13760#ifdef FEAT_WINDOWS
13761 /* set curwin to be our win, temporarily */
13762 save_curwin = curwin;
13763 curwin = win;
13764 curbuf = curwin->w_buffer;
13765#endif
13766
13767 if (*varname == '&')
13768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769 long numval;
13770 char_u *strval;
13771 int error = FALSE;
13772
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013774 numval = get_tv_number_chk(varp, &error);
13775 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013776 if (!error && strval != NULL)
13777 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778 }
13779 else
13780 {
13781 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13782 if (winvarname != NULL)
13783 {
13784 STRCPY(winvarname, "w:");
13785 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013786 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787 vim_free(winvarname);
13788 }
13789 }
13790
13791#ifdef FEAT_WINDOWS
13792 /* Restore current window, if it's still valid (autocomands can make
13793 * it invalid). */
13794 if (win_valid(save_curwin))
13795 {
13796 curwin = save_curwin;
13797 curbuf = curwin->w_buffer;
13798 }
13799#endif
13800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801}
13802
13803/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013804 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805 */
13806 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013807f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013808 typval_T *argvars;
13809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013811 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812
Bram Moolenaar0d660222005-01-07 21:51:51 +000013813 p = get_tv_string(&argvars[0]);
13814 rettv->vval.v_string = vim_strsave(p);
13815 simplify_filename(rettv->vval.v_string); /* simplify in place */
13816 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817}
13818
Bram Moolenaar0d660222005-01-07 21:51:51 +000013819static int
13820#ifdef __BORLANDC__
13821 _RTLENTRYF
13822#endif
13823 item_compare __ARGS((const void *s1, const void *s2));
13824static int
13825#ifdef __BORLANDC__
13826 _RTLENTRYF
13827#endif
13828 item_compare2 __ARGS((const void *s1, const void *s2));
13829
13830static int item_compare_ic;
13831static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013832static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013833#define ITEM_COMPARE_FAIL 999
13834
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013836 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013838 static int
13839#ifdef __BORLANDC__
13840_RTLENTRYF
13841#endif
13842item_compare(s1, s2)
13843 const void *s1;
13844 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013846 char_u *p1, *p2;
13847 char_u *tofree1, *tofree2;
13848 int res;
13849 char_u numbuf1[NUMBUFLEN];
13850 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013852 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
13853 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013854 if (item_compare_ic)
13855 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013857 res = STRCMP(p1, p2);
13858 vim_free(tofree1);
13859 vim_free(tofree2);
13860 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861}
13862
13863 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013864#ifdef __BORLANDC__
13865_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013867item_compare2(s1, s2)
13868 const void *s1;
13869 const void *s2;
13870{
13871 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013872 typval_T rettv;
13873 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013874 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013876 /* shortcut after failure in previous call; compare all items equal */
13877 if (item_compare_func_err)
13878 return 0;
13879
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013880 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13881 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013882 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13883 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013884
13885 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13886 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013887 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013888 clear_tv(&argv[0]);
13889 clear_tv(&argv[1]);
13890
13891 if (res == FAIL)
13892 res = ITEM_COMPARE_FAIL;
13893 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013894 /* return value has wrong type */
13895 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13896 if (item_compare_func_err)
13897 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013898 clear_tv(&rettv);
13899 return res;
13900}
13901
13902/*
13903 * "sort({list})" function
13904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013906f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013907 typval_T *argvars;
13908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909{
Bram Moolenaar33570922005-01-25 22:26:29 +000013910 list_T *l;
13911 listitem_T *li;
13912 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013913 long len;
13914 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915
Bram Moolenaar0d660222005-01-07 21:51:51 +000013916 rettv->vval.v_number = 0;
13917 if (argvars[0].v_type != VAR_LIST)
13918 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 else
13920 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013921 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013922 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013923 return;
13924 rettv->vval.v_list = l;
13925 rettv->v_type = VAR_LIST;
13926 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927
Bram Moolenaar0d660222005-01-07 21:51:51 +000013928 len = list_len(l);
13929 if (len <= 1)
13930 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931
Bram Moolenaar0d660222005-01-07 21:51:51 +000013932 item_compare_ic = FALSE;
13933 item_compare_func = NULL;
13934 if (argvars[1].v_type != VAR_UNKNOWN)
13935 {
13936 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013937 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013938 else
13939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013940 int error = FALSE;
13941
13942 i = get_tv_number_chk(&argvars[1], &error);
13943 if (error)
13944 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013945 if (i == 1)
13946 item_compare_ic = TRUE;
13947 else
13948 item_compare_func = get_tv_string(&argvars[1]);
13949 }
13950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951
Bram Moolenaar0d660222005-01-07 21:51:51 +000013952 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013953 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013954 if (ptrs == NULL)
13955 return;
13956 i = 0;
13957 for (li = l->lv_first; li != NULL; li = li->li_next)
13958 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013960 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013961 /* test the compare function */
13962 if (item_compare_func != NULL
13963 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13964 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013965 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013967 {
13968 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013969 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013970 item_compare_func == NULL ? item_compare : item_compare2);
13971
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013972 if (!item_compare_func_err)
13973 {
13974 /* Clear the List and append the items in the sorted order. */
13975 l->lv_first = l->lv_last = NULL;
13976 l->lv_len = 0;
13977 for (i = 0; i < len; ++i)
13978 list_append(l, ptrs[i]);
13979 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013980 }
13981
13982 vim_free(ptrs);
13983 }
13984}
13985
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013986/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013987 * "soundfold({word})" function
13988 */
13989 static void
13990f_soundfold(argvars, rettv)
13991 typval_T *argvars;
13992 typval_T *rettv;
13993{
13994 char_u *s;
13995
13996 rettv->v_type = VAR_STRING;
13997 s = get_tv_string(&argvars[0]);
13998#ifdef FEAT_SYN_HL
13999 rettv->vval.v_string = eval_soundfold(s);
14000#else
14001 rettv->vval.v_string = vim_strsave(s);
14002#endif
14003}
14004
14005/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014006 * "spellbadword()" function
14007 */
14008/* ARGSUSED */
14009 static void
14010f_spellbadword(argvars, rettv)
14011 typval_T *argvars;
14012 typval_T *rettv;
14013{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014014 char_u *word = (char_u *)"";
14015#ifdef FEAT_SYN_HL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014016 int len = 0;
14017 hlf_T attr = HLF_COUNT;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014018 list_T *l;
14019#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014020
Bram Moolenaar4463f292005-09-25 22:20:24 +000014021 l = list_alloc();
14022 if (l == NULL)
14023 return;
14024 rettv->v_type = VAR_LIST;
14025 rettv->vval.v_list = l;
14026 ++l->lv_refcount;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014027
14028#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014029 if (argvars[0].v_type == VAR_UNKNOWN)
14030 {
14031 /* Find the start and length of the badly spelled word. */
14032 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14033 if (len != 0)
14034 word = ml_get_cursor();
14035 }
14036 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14037 {
14038 char_u *str = get_tv_string_chk(&argvars[0]);
14039 int capcol = -1;
14040
14041 if (str != NULL)
14042 {
14043 /* Check the argument for spelling. */
14044 while (*str != NUL)
14045 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014046 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014047 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014048 {
14049 word = str;
14050 break;
14051 }
14052 str += len;
14053 }
14054 }
14055 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014056#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014057
14058 list_append_string(l, word, len);
14059 list_append_string(l, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014060 attr == HLF_SPB ? "bad" :
14061 attr == HLF_SPR ? "rare" :
14062 attr == HLF_SPL ? "local" :
14063 attr == HLF_SPC ? "caps" :
14064 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014065}
14066
14067/*
14068 * "spellsuggest()" function
14069 */
14070 static void
14071f_spellsuggest(argvars, rettv)
14072 typval_T *argvars;
14073 typval_T *rettv;
14074{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014075 list_T *l;
14076#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014077 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014078 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014079 int maxcount;
14080 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014081 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014082 listitem_T *li;
14083 int need_capital = FALSE;
14084#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014085
14086 l = list_alloc();
14087 if (l == NULL)
14088 return;
14089 rettv->v_type = VAR_LIST;
14090 rettv->vval.v_list = l;
14091 ++l->lv_refcount;
14092
14093#ifdef FEAT_SYN_HL
14094 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14095 {
14096 str = get_tv_string(&argvars[0]);
14097 if (argvars[1].v_type != VAR_UNKNOWN)
14098 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014099 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014100 if (maxcount <= 0)
14101 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014102 if (argvars[2].v_type != VAR_UNKNOWN)
14103 {
14104 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14105 if (typeerr)
14106 return;
14107 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014108 }
14109 else
14110 maxcount = 25;
14111
Bram Moolenaar4770d092006-01-12 23:22:24 +000014112 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014113
14114 for (i = 0; i < ga.ga_len; ++i)
14115 {
14116 str = ((char_u **)ga.ga_data)[i];
14117
14118 li = listitem_alloc();
14119 if (li == NULL)
14120 vim_free(str);
14121 else
14122 {
14123 li->li_tv.v_type = VAR_STRING;
14124 li->li_tv.v_lock = 0;
14125 li->li_tv.vval.v_string = str;
14126 list_append(l, li);
14127 }
14128 }
14129 ga_clear(&ga);
14130 }
14131#endif
14132}
14133
Bram Moolenaar0d660222005-01-07 21:51:51 +000014134 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014135f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014136 typval_T *argvars;
14137 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014138{
14139 char_u *str;
14140 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014141 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014142 regmatch_T regmatch;
14143 char_u patbuf[NUMBUFLEN];
14144 char_u *save_cpo;
14145 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000014146 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014147 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014148 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014150
14151 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14152 save_cpo = p_cpo;
14153 p_cpo = (char_u *)"";
14154
14155 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014156 if (argvars[1].v_type != VAR_UNKNOWN)
14157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14159 if (pat == NULL)
14160 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014161 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014162 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014163 }
14164 if (pat == NULL || *pat == NUL)
14165 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014166
14167 l = list_alloc();
14168 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014170 rettv->v_type = VAR_LIST;
14171 rettv->vval.v_list = l;
14172 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 if (typeerr)
14174 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175
Bram Moolenaar0d660222005-01-07 21:51:51 +000014176 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14177 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014179 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014180 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014181 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014182 if (*str == NUL)
14183 match = FALSE; /* empty item at the end */
14184 else
14185 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014186 if (match)
14187 end = regmatch.startp[0];
14188 else
14189 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014190 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
14191 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014192 {
Bram Moolenaar4463f292005-09-25 22:20:24 +000014193 if (list_append_string(l, str, (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014194 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014195 }
14196 if (!match)
14197 break;
14198 /* Advance to just after the match. */
14199 if (regmatch.endp[0] > str)
14200 col = 0;
14201 else
14202 {
14203 /* Don't get stuck at the same match. */
14204#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014205 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014206#else
14207 col = 1;
14208#endif
14209 }
14210 str = regmatch.endp[0];
14211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212
Bram Moolenaar0d660222005-01-07 21:51:51 +000014213 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215
Bram Moolenaar0d660222005-01-07 21:51:51 +000014216 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217}
14218
14219#ifdef HAVE_STRFTIME
14220/*
14221 * "strftime({format}[, {time}])" function
14222 */
14223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014224f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014225 typval_T *argvars;
14226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227{
14228 char_u result_buf[256];
14229 struct tm *curtime;
14230 time_t seconds;
14231 char_u *p;
14232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014233 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014235 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014236 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237 seconds = time(NULL);
14238 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014239 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240 curtime = localtime(&seconds);
14241 /* MSVC returns NULL for an invalid value of seconds. */
14242 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014243 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244 else
14245 {
14246# ifdef FEAT_MBYTE
14247 vimconv_T conv;
14248 char_u *enc;
14249
14250 conv.vc_type = CONV_NONE;
14251 enc = enc_locale();
14252 convert_setup(&conv, p_enc, enc);
14253 if (conv.vc_type != CONV_NONE)
14254 p = string_convert(&conv, p, NULL);
14255# endif
14256 if (p != NULL)
14257 (void)strftime((char *)result_buf, sizeof(result_buf),
14258 (char *)p, curtime);
14259 else
14260 result_buf[0] = NUL;
14261
14262# ifdef FEAT_MBYTE
14263 if (conv.vc_type != CONV_NONE)
14264 vim_free(p);
14265 convert_setup(&conv, enc, p_enc);
14266 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014267 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268 else
14269# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014270 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271
14272# ifdef FEAT_MBYTE
14273 /* Release conversion descriptors */
14274 convert_setup(&conv, NULL, NULL);
14275 vim_free(enc);
14276# endif
14277 }
14278}
14279#endif
14280
14281/*
14282 * "stridx()" function
14283 */
14284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014285f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014286 typval_T *argvars;
14287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288{
14289 char_u buf[NUMBUFLEN];
14290 char_u *needle;
14291 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014292 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014294 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014296 needle = get_tv_string_chk(&argvars[1]);
14297 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014298 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014299 if (needle == NULL || haystack == NULL)
14300 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301
Bram Moolenaar33570922005-01-25 22:26:29 +000014302 if (argvars[2].v_type != VAR_UNKNOWN)
14303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014304 int error = FALSE;
14305
14306 start_idx = get_tv_number_chk(&argvars[2], &error);
14307 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014308 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014309 if (start_idx >= 0)
14310 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014311 }
14312
14313 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14314 if (pos != NULL)
14315 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316}
14317
14318/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014319 * "string()" function
14320 */
14321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014322f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014323 typval_T *argvars;
14324 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014325{
14326 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014327 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014328
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014329 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014330 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014331 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014332 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333}
14334
14335/*
14336 * "strlen()" function
14337 */
14338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014339f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014340 typval_T *argvars;
14341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014343 rettv->vval.v_number = (varnumber_T)(STRLEN(
14344 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345}
14346
14347/*
14348 * "strpart()" function
14349 */
14350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014351f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014352 typval_T *argvars;
14353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354{
14355 char_u *p;
14356 int n;
14357 int len;
14358 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014359 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014361 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 slen = (int)STRLEN(p);
14363
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014364 n = get_tv_number_chk(&argvars[1], &error);
14365 if (error)
14366 len = 0;
14367 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 else
14370 len = slen - n; /* default len: all bytes that are available. */
14371
14372 /*
14373 * Only return the overlap between the specified part and the actual
14374 * string.
14375 */
14376 if (n < 0)
14377 {
14378 len += n;
14379 n = 0;
14380 }
14381 else if (n > slen)
14382 n = slen;
14383 if (len < 0)
14384 len = 0;
14385 else if (n + len > slen)
14386 len = slen - n;
14387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014388 rettv->v_type = VAR_STRING;
14389 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390}
14391
14392/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014393 * "strridx()" function
14394 */
14395 static void
14396f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014397 typval_T *argvars;
14398 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014399{
14400 char_u buf[NUMBUFLEN];
14401 char_u *needle;
14402 char_u *haystack;
14403 char_u *rest;
14404 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014405 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014407 needle = get_tv_string_chk(&argvars[1]);
14408 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014409 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014410
14411 rettv->vval.v_number = -1;
14412 if (needle == NULL || haystack == NULL)
14413 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014414 if (argvars[2].v_type != VAR_UNKNOWN)
14415 {
14416 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014417 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014418 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014419 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014420 }
14421 else
14422 end_idx = haystack_len;
14423
Bram Moolenaar0d660222005-01-07 21:51:51 +000014424 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014425 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014426 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014427 lastmatch = haystack + end_idx;
14428 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014429 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014430 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014431 for (rest = haystack; *rest != '\0'; ++rest)
14432 {
14433 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014434 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014435 break;
14436 lastmatch = rest;
14437 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014438 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014439
14440 if (lastmatch == NULL)
14441 rettv->vval.v_number = -1;
14442 else
14443 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14444}
14445
14446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447 * "strtrans()" function
14448 */
14449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014450f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014451 typval_T *argvars;
14452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014454 rettv->v_type = VAR_STRING;
14455 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456}
14457
14458/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014459 * "submatch()" function
14460 */
14461 static void
14462f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014463 typval_T *argvars;
14464 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014465{
14466 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014467 rettv->vval.v_string =
14468 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014469}
14470
14471/*
14472 * "substitute()" function
14473 */
14474 static void
14475f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014476 typval_T *argvars;
14477 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014478{
14479 char_u patbuf[NUMBUFLEN];
14480 char_u subbuf[NUMBUFLEN];
14481 char_u flagsbuf[NUMBUFLEN];
14482
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014483 char_u *str = get_tv_string_chk(&argvars[0]);
14484 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14485 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14486 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14487
Bram Moolenaar0d660222005-01-07 21:51:51 +000014488 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014489 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14490 rettv->vval.v_string = NULL;
14491 else
14492 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014493}
14494
14495/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014496 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497 */
14498/*ARGSUSED*/
14499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014500f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014501 typval_T *argvars;
14502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503{
14504 int id = 0;
14505#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014506 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 long col;
14508 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014509 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014511 lnum = get_tv_lnum(argvars); /* -1 on type error */
14512 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14513 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014515 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014516 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014517 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518#endif
14519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014520 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521}
14522
14523/*
14524 * "synIDattr(id, what [, mode])" function
14525 */
14526/*ARGSUSED*/
14527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014528f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014529 typval_T *argvars;
14530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531{
14532 char_u *p = NULL;
14533#ifdef FEAT_SYN_HL
14534 int id;
14535 char_u *what;
14536 char_u *mode;
14537 char_u modebuf[NUMBUFLEN];
14538 int modec;
14539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014540 id = get_tv_number(&argvars[0]);
14541 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014542 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014544 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 modec = TOLOWER_ASC(mode[0]);
14546 if (modec != 't' && modec != 'c'
14547#ifdef FEAT_GUI
14548 && modec != 'g'
14549#endif
14550 )
14551 modec = 0; /* replace invalid with current */
14552 }
14553 else
14554 {
14555#ifdef FEAT_GUI
14556 if (gui.in_use)
14557 modec = 'g';
14558 else
14559#endif
14560 if (t_colors > 1)
14561 modec = 'c';
14562 else
14563 modec = 't';
14564 }
14565
14566
14567 switch (TOLOWER_ASC(what[0]))
14568 {
14569 case 'b':
14570 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14571 p = highlight_color(id, what, modec);
14572 else /* bold */
14573 p = highlight_has_attr(id, HL_BOLD, modec);
14574 break;
14575
14576 case 'f': /* fg[#] */
14577 p = highlight_color(id, what, modec);
14578 break;
14579
14580 case 'i':
14581 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14582 p = highlight_has_attr(id, HL_INVERSE, modec);
14583 else /* italic */
14584 p = highlight_has_attr(id, HL_ITALIC, modec);
14585 break;
14586
14587 case 'n': /* name */
14588 p = get_highlight_name(NULL, id - 1);
14589 break;
14590
14591 case 'r': /* reverse */
14592 p = highlight_has_attr(id, HL_INVERSE, modec);
14593 break;
14594
14595 case 's': /* standout */
14596 p = highlight_has_attr(id, HL_STANDOUT, modec);
14597 break;
14598
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014599 case 'u':
14600 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14601 /* underline */
14602 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14603 else
14604 /* undercurl */
14605 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606 break;
14607 }
14608
14609 if (p != NULL)
14610 p = vim_strsave(p);
14611#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014612 rettv->v_type = VAR_STRING;
14613 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614}
14615
14616/*
14617 * "synIDtrans(id)" function
14618 */
14619/*ARGSUSED*/
14620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014621f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014622 typval_T *argvars;
14623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624{
14625 int id;
14626
14627#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014628 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629
14630 if (id > 0)
14631 id = syn_get_final_id(id);
14632 else
14633#endif
14634 id = 0;
14635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014636 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637}
14638
14639/*
14640 * "system()" function
14641 */
14642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014643f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014644 typval_T *argvars;
14645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014647 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014649 char_u *infile = NULL;
14650 char_u buf[NUMBUFLEN];
14651 int err = FALSE;
14652 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014654 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014655 {
14656 /*
14657 * Write the string to a temp file, to be used for input of the shell
14658 * command.
14659 */
14660 if ((infile = vim_tempname('i')) == NULL)
14661 {
14662 EMSG(_(e_notmp));
14663 return;
14664 }
14665
14666 fd = mch_fopen((char *)infile, WRITEBIN);
14667 if (fd == NULL)
14668 {
14669 EMSG2(_(e_notopen), infile);
14670 goto done;
14671 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014672 p = get_tv_string_buf_chk(&argvars[1], buf);
14673 if (p == NULL)
14674 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014675 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14676 err = TRUE;
14677 if (fclose(fd) != 0)
14678 err = TRUE;
14679 if (err)
14680 {
14681 EMSG(_("E677: Error writing temp file"));
14682 goto done;
14683 }
14684 }
14685
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014686 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014687
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688#ifdef USE_CR
14689 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014690 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691 {
14692 char_u *s;
14693
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014694 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695 {
14696 if (*s == CAR)
14697 *s = NL;
14698 }
14699 }
14700#else
14701# ifdef USE_CRNL
14702 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014703 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704 {
14705 char_u *s, *d;
14706
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014707 d = res;
14708 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709 {
14710 if (s[0] == CAR && s[1] == NL)
14711 ++s;
14712 *d++ = *s;
14713 }
14714 *d = NUL;
14715 }
14716# endif
14717#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014718
14719done:
14720 if (infile != NULL)
14721 {
14722 mch_remove(infile);
14723 vim_free(infile);
14724 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014725 rettv->v_type = VAR_STRING;
14726 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727}
14728
14729/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014730 * "tagfiles()" function
14731 */
14732/*ARGSUSED*/
14733 static void
14734f_tagfiles(argvars, rettv)
14735 typval_T *argvars;
14736 typval_T *rettv;
14737{
14738 char_u fname[MAXPATHL + 1];
14739 list_T *l;
14740
14741 l = list_alloc();
14742 if (l == NULL)
14743 {
14744 rettv->vval.v_number = 0;
14745 return;
14746 }
14747 rettv->vval.v_list = l;
14748 rettv->v_type = VAR_LIST;
14749 ++l->lv_refcount;
14750
14751 get_tagfname(TRUE, NULL);
14752 for (;;)
14753 if (get_tagfname(FALSE, fname) == FAIL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014754 || list_append_string(l, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014755 break;
14756}
14757
14758/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014759 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014760 */
14761 static void
14762f_taglist(argvars, rettv)
14763 typval_T *argvars;
14764 typval_T *rettv;
14765{
14766 char_u *tag_pattern;
14767 list_T *l;
14768
14769 tag_pattern = get_tv_string(&argvars[0]);
14770
14771 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014772 if (*tag_pattern == NUL)
14773 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014774
14775 l = list_alloc();
14776 if (l != NULL)
14777 {
14778 if (get_tags(l, tag_pattern) != FAIL)
14779 {
14780 rettv->vval.v_list = l;
14781 rettv->v_type = VAR_LIST;
14782 ++l->lv_refcount;
14783 }
14784 else
14785 list_free(l);
14786 }
14787}
14788
14789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790 * "tempname()" function
14791 */
14792/*ARGSUSED*/
14793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014794f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014795 typval_T *argvars;
14796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797{
14798 static int x = 'A';
14799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014800 rettv->v_type = VAR_STRING;
14801 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014802
14803 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14804 * names. Skip 'I' and 'O', they are used for shell redirection. */
14805 do
14806 {
14807 if (x == 'Z')
14808 x = '0';
14809 else if (x == '9')
14810 x = 'A';
14811 else
14812 {
14813#ifdef EBCDIC
14814 if (x == 'I')
14815 x = 'J';
14816 else if (x == 'R')
14817 x = 'S';
14818 else
14819#endif
14820 ++x;
14821 }
14822 } while (x == 'I' || x == 'O');
14823}
14824
14825/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014826 * "test(list)" function: Just checking the walls...
14827 */
14828/*ARGSUSED*/
14829 static void
14830f_test(argvars, rettv)
14831 typval_T *argvars;
14832 typval_T *rettv;
14833{
14834 /* Used for unit testing. Change the code below to your liking. */
14835#if 0
14836 listitem_T *li;
14837 list_T *l;
14838 char_u *bad, *good;
14839
14840 if (argvars[0].v_type != VAR_LIST)
14841 return;
14842 l = argvars[0].vval.v_list;
14843 if (l == NULL)
14844 return;
14845 li = l->lv_first;
14846 if (li == NULL)
14847 return;
14848 bad = get_tv_string(&li->li_tv);
14849 li = li->li_next;
14850 if (li == NULL)
14851 return;
14852 good = get_tv_string(&li->li_tv);
14853 rettv->vval.v_number = test_edit_score(bad, good);
14854#endif
14855}
14856
14857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858 * "tolower(string)" function
14859 */
14860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014861f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014862 typval_T *argvars;
14863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864{
14865 char_u *p;
14866
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014867 p = vim_strsave(get_tv_string(&argvars[0]));
14868 rettv->v_type = VAR_STRING;
14869 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870
14871 if (p != NULL)
14872 while (*p != NUL)
14873 {
14874#ifdef FEAT_MBYTE
14875 int l;
14876
14877 if (enc_utf8)
14878 {
14879 int c, lc;
14880
14881 c = utf_ptr2char(p);
14882 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014883 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014884 /* TODO: reallocate string when byte count changes. */
14885 if (utf_char2len(lc) == l)
14886 utf_char2bytes(lc, p);
14887 p += l;
14888 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014889 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890 p += l; /* skip multi-byte character */
14891 else
14892#endif
14893 {
14894 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14895 ++p;
14896 }
14897 }
14898}
14899
14900/*
14901 * "toupper(string)" function
14902 */
14903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014904f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014905 typval_T *argvars;
14906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014908 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014909 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910}
14911
14912/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014913 * "tr(string, fromstr, tostr)" function
14914 */
14915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014916f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014917 typval_T *argvars;
14918 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014919{
14920 char_u *instr;
14921 char_u *fromstr;
14922 char_u *tostr;
14923 char_u *p;
14924#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014925 int inlen;
14926 int fromlen;
14927 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014928 int idx;
14929 char_u *cpstr;
14930 int cplen;
14931 int first = TRUE;
14932#endif
14933 char_u buf[NUMBUFLEN];
14934 char_u buf2[NUMBUFLEN];
14935 garray_T ga;
14936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014937 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014938 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14939 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014940
14941 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014942 rettv->v_type = VAR_STRING;
14943 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014944 if (fromstr == NULL || tostr == NULL)
14945 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014946 ga_init2(&ga, (int)sizeof(char), 80);
14947
14948#ifdef FEAT_MBYTE
14949 if (!has_mbyte)
14950#endif
14951 /* not multi-byte: fromstr and tostr must be the same length */
14952 if (STRLEN(fromstr) != STRLEN(tostr))
14953 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014954#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014955error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014956#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014957 EMSG2(_(e_invarg2), fromstr);
14958 ga_clear(&ga);
14959 return;
14960 }
14961
14962 /* fromstr and tostr have to contain the same number of chars */
14963 while (*instr != NUL)
14964 {
14965#ifdef FEAT_MBYTE
14966 if (has_mbyte)
14967 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014968 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014969 cpstr = instr;
14970 cplen = inlen;
14971 idx = 0;
14972 for (p = fromstr; *p != NUL; p += fromlen)
14973 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014974 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014975 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14976 {
14977 for (p = tostr; *p != NUL; p += tolen)
14978 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014979 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014980 if (idx-- == 0)
14981 {
14982 cplen = tolen;
14983 cpstr = p;
14984 break;
14985 }
14986 }
14987 if (*p == NUL) /* tostr is shorter than fromstr */
14988 goto error;
14989 break;
14990 }
14991 ++idx;
14992 }
14993
14994 if (first && cpstr == instr)
14995 {
14996 /* Check that fromstr and tostr have the same number of
14997 * (multi-byte) characters. Done only once when a character
14998 * of instr doesn't appear in fromstr. */
14999 first = FALSE;
15000 for (p = tostr; *p != NUL; p += tolen)
15001 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015002 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015003 --idx;
15004 }
15005 if (idx != 0)
15006 goto error;
15007 }
15008
15009 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015010 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015011 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015012
15013 instr += inlen;
15014 }
15015 else
15016#endif
15017 {
15018 /* When not using multi-byte chars we can do it faster. */
15019 p = vim_strchr(fromstr, *instr);
15020 if (p != NULL)
15021 ga_append(&ga, tostr[p - fromstr]);
15022 else
15023 ga_append(&ga, *instr);
15024 ++instr;
15025 }
15026 }
15027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015028 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015029}
15030
15031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032 * "type(expr)" function
15033 */
15034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015035f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015036 typval_T *argvars;
15037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015039 int n;
15040
15041 switch (argvars[0].v_type)
15042 {
15043 case VAR_NUMBER: n = 0; break;
15044 case VAR_STRING: n = 1; break;
15045 case VAR_FUNC: n = 2; break;
15046 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015047 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015048 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15049 }
15050 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051}
15052
15053/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015054 * "values(dict)" function
15055 */
15056 static void
15057f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015058 typval_T *argvars;
15059 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015060{
15061 dict_list(argvars, rettv, 1);
15062}
15063
15064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015065 * "virtcol(string)" function
15066 */
15067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015068f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015069 typval_T *argvars;
15070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071{
15072 colnr_T vcol = 0;
15073 pos_T *fp;
15074
15075 fp = var2fpos(&argvars[0], FALSE);
15076 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
15077 {
15078 getvvcol(curwin, fp, NULL, NULL, &vcol);
15079 ++vcol;
15080 }
15081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015082 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083}
15084
15085/*
15086 * "visualmode()" function
15087 */
15088/*ARGSUSED*/
15089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015090f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 typval_T *argvars;
15092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093{
15094#ifdef FEAT_VISUAL
15095 char_u str[2];
15096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015097 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098 str[0] = curbuf->b_visual_mode_eval;
15099 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015100 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101
15102 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015103 if ((argvars[0].v_type == VAR_NUMBER
15104 && argvars[0].vval.v_number != 0)
15105 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015106 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 curbuf->b_visual_mode_eval = NUL;
15108#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015109 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110#endif
15111}
15112
15113/*
15114 * "winbufnr(nr)" function
15115 */
15116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015117f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015118 typval_T *argvars;
15119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120{
15121 win_T *wp;
15122
15123 wp = find_win_by_nr(&argvars[0]);
15124 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015125 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015127 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128}
15129
15130/*
15131 * "wincol()" function
15132 */
15133/*ARGSUSED*/
15134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015135f_wincol(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 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015140 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141}
15142
15143/*
15144 * "winheight(nr)" function
15145 */
15146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015147f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015148 typval_T *argvars;
15149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150{
15151 win_T *wp;
15152
15153 wp = find_win_by_nr(&argvars[0]);
15154 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015155 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015157 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158}
15159
15160/*
15161 * "winline()" function
15162 */
15163/*ARGSUSED*/
15164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015165f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015166 typval_T *argvars;
15167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168{
15169 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015170 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171}
15172
15173/*
15174 * "winnr()" function
15175 */
15176/* ARGSUSED */
15177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015178f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015179 typval_T *argvars;
15180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181{
15182 int nr = 1;
15183#ifdef FEAT_WINDOWS
15184 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015185 win_T *twin = curwin;
15186 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015188 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015190 arg = get_tv_string_chk(&argvars[0]);
15191 if (arg == NULL)
15192 nr = 0; /* type error; errmsg already given */
15193 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015194 twin = lastwin;
15195 else if (STRCMP(arg, "#") == 0)
15196 {
15197 twin = prevwin;
15198 if (prevwin == NULL)
15199 nr = 0;
15200 }
15201 else
15202 {
15203 EMSG2(_(e_invexpr2), arg);
15204 nr = 0;
15205 }
15206 }
15207
15208 if (nr > 0)
15209 for (wp = firstwin; wp != twin; wp = wp->w_next)
15210 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015212 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015213}
15214
15215/*
15216 * "winrestcmd()" function
15217 */
15218/* ARGSUSED */
15219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015220f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015221 typval_T *argvars;
15222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223{
15224#ifdef FEAT_WINDOWS
15225 win_T *wp;
15226 int winnr = 1;
15227 garray_T ga;
15228 char_u buf[50];
15229
15230 ga_init2(&ga, (int)sizeof(char), 70);
15231 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15232 {
15233 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15234 ga_concat(&ga, buf);
15235# ifdef FEAT_VERTSPLIT
15236 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15237 ga_concat(&ga, buf);
15238# endif
15239 ++winnr;
15240 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015241 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015243 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015245 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015246#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015247 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015248}
15249
15250/*
15251 * "winwidth(nr)" function
15252 */
15253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015254f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015255 typval_T *argvars;
15256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257{
15258 win_T *wp;
15259
15260 wp = find_win_by_nr(&argvars[0]);
15261 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015262 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263 else
15264#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015265 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015267 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268#endif
15269}
15270
Bram Moolenaar071d4272004-06-13 20:20:40 +000015271/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015272 * "writefile()" function
15273 */
15274 static void
15275f_writefile(argvars, rettv)
15276 typval_T *argvars;
15277 typval_T *rettv;
15278{
15279 int binary = FALSE;
15280 char_u *fname;
15281 FILE *fd;
15282 listitem_T *li;
15283 char_u *s;
15284 int ret = 0;
15285 int c;
15286
15287 if (argvars[0].v_type != VAR_LIST)
15288 {
15289 EMSG2(_(e_listarg), "writefile()");
15290 return;
15291 }
15292 if (argvars[0].vval.v_list == NULL)
15293 return;
15294
15295 if (argvars[2].v_type != VAR_UNKNOWN
15296 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15297 binary = TRUE;
15298
15299 /* Always open the file in binary mode, library functions have a mind of
15300 * their own about CR-LF conversion. */
15301 fname = get_tv_string(&argvars[1]);
15302 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15303 {
15304 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15305 ret = -1;
15306 }
15307 else
15308 {
15309 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15310 li = li->li_next)
15311 {
15312 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15313 {
15314 if (*s == '\n')
15315 c = putc(NUL, fd);
15316 else
15317 c = putc(*s, fd);
15318 if (c == EOF)
15319 {
15320 ret = -1;
15321 break;
15322 }
15323 }
15324 if (!binary || li->li_next != NULL)
15325 if (putc('\n', fd) == EOF)
15326 {
15327 ret = -1;
15328 break;
15329 }
15330 if (ret < 0)
15331 {
15332 EMSG(_(e_write));
15333 break;
15334 }
15335 }
15336 fclose(fd);
15337 }
15338
15339 rettv->vval.v_number = ret;
15340}
15341
15342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343 * Translate a String variable into a position.
15344 */
15345 static pos_T *
15346var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015347 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348 int lnum; /* TRUE when $ is last line */
15349{
15350 char_u *name;
15351 static pos_T pos;
15352 pos_T *pp;
15353
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015354 name = get_tv_string_chk(varp);
15355 if (name == NULL)
15356 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357 if (name[0] == '.') /* cursor */
15358 return &curwin->w_cursor;
15359 if (name[0] == '\'') /* mark */
15360 {
15361 pp = getmark(name[1], FALSE);
15362 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15363 return NULL;
15364 return pp;
15365 }
15366 if (name[0] == '$') /* last column or line */
15367 {
15368 if (lnum)
15369 {
15370 pos.lnum = curbuf->b_ml.ml_line_count;
15371 pos.col = 0;
15372 }
15373 else
15374 {
15375 pos.lnum = curwin->w_cursor.lnum;
15376 pos.col = (colnr_T)STRLEN(ml_get_curline());
15377 }
15378 return &pos;
15379 }
15380 return NULL;
15381}
15382
15383/*
15384 * Get the length of an environment variable name.
15385 * Advance "arg" to the first character after the name.
15386 * Return 0 for error.
15387 */
15388 static int
15389get_env_len(arg)
15390 char_u **arg;
15391{
15392 char_u *p;
15393 int len;
15394
15395 for (p = *arg; vim_isIDc(*p); ++p)
15396 ;
15397 if (p == *arg) /* no name found */
15398 return 0;
15399
15400 len = (int)(p - *arg);
15401 *arg = p;
15402 return len;
15403}
15404
15405/*
15406 * Get the length of the name of a function or internal variable.
15407 * "arg" is advanced to the first non-white character after the name.
15408 * Return 0 if something is wrong.
15409 */
15410 static int
15411get_id_len(arg)
15412 char_u **arg;
15413{
15414 char_u *p;
15415 int len;
15416
15417 /* Find the end of the name. */
15418 for (p = *arg; eval_isnamec(*p); ++p)
15419 ;
15420 if (p == *arg) /* no name found */
15421 return 0;
15422
15423 len = (int)(p - *arg);
15424 *arg = skipwhite(p);
15425
15426 return len;
15427}
15428
15429/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015430 * Get the length of the name of a variable or function.
15431 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015433 * Return -1 if curly braces expansion failed.
15434 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435 * If the name contains 'magic' {}'s, expand them and return the
15436 * expanded name in an allocated string via 'alias' - caller must free.
15437 */
15438 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015439get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 char_u **arg;
15441 char_u **alias;
15442 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015443 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444{
15445 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446 char_u *p;
15447 char_u *expr_start;
15448 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449
15450 *alias = NULL; /* default to no alias */
15451
15452 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15453 && (*arg)[2] == (int)KE_SNR)
15454 {
15455 /* hard coded <SNR>, already translated */
15456 *arg += 3;
15457 return get_id_len(arg) + 3;
15458 }
15459 len = eval_fname_script(*arg);
15460 if (len > 0)
15461 {
15462 /* literal "<SID>", "s:" or "<SNR>" */
15463 *arg += len;
15464 }
15465
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015467 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015469 p = find_name_end(*arg, &expr_start, &expr_end,
15470 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471 if (expr_start != NULL)
15472 {
15473 char_u *temp_string;
15474
15475 if (!evaluate)
15476 {
15477 len += (int)(p - *arg);
15478 *arg = skipwhite(p);
15479 return len;
15480 }
15481
15482 /*
15483 * Include any <SID> etc in the expanded string:
15484 * Thus the -len here.
15485 */
15486 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15487 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015488 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489 *alias = temp_string;
15490 *arg = skipwhite(p);
15491 return (int)STRLEN(temp_string);
15492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015493
15494 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015495 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496 EMSG2(_(e_invexpr2), *arg);
15497
15498 return len;
15499}
15500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015501/*
15502 * Find the end of a variable or function name, taking care of magic braces.
15503 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15504 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015505 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015506 * Return a pointer to just after the name. Equal to "arg" if there is no
15507 * valid name.
15508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015510find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 char_u *arg;
15512 char_u **expr_start;
15513 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015514 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015516 int mb_nest = 0;
15517 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518 char_u *p;
15519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015520 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015522 *expr_start = NULL;
15523 *expr_end = NULL;
15524 }
15525
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015526 /* Quick check for valid starting character. */
15527 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15528 return arg;
15529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015530 for (p = arg; *p != NUL
15531 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015532 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015533 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015534 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015535 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015536 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015537 if (*p == '\'')
15538 {
15539 /* skip over 'string' to avoid counting [ and ] inside it. */
15540 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15541 ;
15542 if (*p == NUL)
15543 break;
15544 }
15545 else if (*p == '"')
15546 {
15547 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15548 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15549 if (*p == '\\' && p[1] != NUL)
15550 ++p;
15551 if (*p == NUL)
15552 break;
15553 }
15554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015555 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015557 if (*p == '[')
15558 ++br_nest;
15559 else if (*p == ']')
15560 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015563 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015565 if (*p == '{')
15566 {
15567 mb_nest++;
15568 if (expr_start != NULL && *expr_start == NULL)
15569 *expr_start = p;
15570 }
15571 else if (*p == '}')
15572 {
15573 mb_nest--;
15574 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15575 *expr_end = p;
15576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578 }
15579
15580 return p;
15581}
15582
15583/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015584 * Expands out the 'magic' {}'s in a variable/function name.
15585 * Note that this can call itself recursively, to deal with
15586 * constructs like foo{bar}{baz}{bam}
15587 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15588 * "in_start" ^
15589 * "expr_start" ^
15590 * "expr_end" ^
15591 * "in_end" ^
15592 *
15593 * Returns a new allocated string, which the caller must free.
15594 * Returns NULL for failure.
15595 */
15596 static char_u *
15597make_expanded_name(in_start, expr_start, expr_end, in_end)
15598 char_u *in_start;
15599 char_u *expr_start;
15600 char_u *expr_end;
15601 char_u *in_end;
15602{
15603 char_u c1;
15604 char_u *retval = NULL;
15605 char_u *temp_result;
15606 char_u *nextcmd = NULL;
15607
15608 if (expr_end == NULL || in_end == NULL)
15609 return NULL;
15610 *expr_start = NUL;
15611 *expr_end = NUL;
15612 c1 = *in_end;
15613 *in_end = NUL;
15614
15615 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15616 if (temp_result != NULL && nextcmd == NULL)
15617 {
15618 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15619 + (in_end - expr_end) + 1));
15620 if (retval != NULL)
15621 {
15622 STRCPY(retval, in_start);
15623 STRCAT(retval, temp_result);
15624 STRCAT(retval, expr_end + 1);
15625 }
15626 }
15627 vim_free(temp_result);
15628
15629 *in_end = c1; /* put char back for error messages */
15630 *expr_start = '{';
15631 *expr_end = '}';
15632
15633 if (retval != NULL)
15634 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015635 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015636 if (expr_start != NULL)
15637 {
15638 /* Further expansion! */
15639 temp_result = make_expanded_name(retval, expr_start,
15640 expr_end, temp_result);
15641 vim_free(retval);
15642 retval = temp_result;
15643 }
15644 }
15645
15646 return retval;
15647}
15648
15649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015651 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652 */
15653 static int
15654eval_isnamec(c)
15655 int c;
15656{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015657 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15658}
15659
15660/*
15661 * Return TRUE if character "c" can be used as the first character in a
15662 * variable or function name (excluding '{' and '}').
15663 */
15664 static int
15665eval_isnamec1(c)
15666 int c;
15667{
15668 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669}
15670
15671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 * Set number v: variable to "val".
15673 */
15674 void
15675set_vim_var_nr(idx, val)
15676 int idx;
15677 long val;
15678{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015679 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680}
15681
15682/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015683 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684 */
15685 long
15686get_vim_var_nr(idx)
15687 int idx;
15688{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015689 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690}
15691
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015692#if defined(FEAT_AUTOCMD) || defined(PROTO)
15693/*
15694 * Get string v: variable value. Uses a static buffer, can only be used once.
15695 */
15696 char_u *
15697get_vim_var_str(idx)
15698 int idx;
15699{
15700 return get_tv_string(&vimvars[idx].vv_tv);
15701}
15702#endif
15703
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704/*
15705 * Set v:count, v:count1 and v:prevcount.
15706 */
15707 void
15708set_vcount(count, count1)
15709 long count;
15710 long count1;
15711{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015712 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15713 vimvars[VV_COUNT].vv_nr = count;
15714 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715}
15716
15717/*
15718 * Set string v: variable to a copy of "val".
15719 */
15720 void
15721set_vim_var_string(idx, val, len)
15722 int idx;
15723 char_u *val;
15724 int len; /* length of "val" to use or -1 (whole string) */
15725{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015726 /* Need to do this (at least) once, since we can't initialize a union.
15727 * Will always be invoked when "v:progname" is set. */
15728 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15729
Bram Moolenaare9a41262005-01-15 22:18:47 +000015730 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015732 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015734 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015736 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737}
15738
15739/*
15740 * Set v:register if needed.
15741 */
15742 void
15743set_reg_var(c)
15744 int c;
15745{
15746 char_u regname;
15747
15748 if (c == 0 || c == ' ')
15749 regname = '"';
15750 else
15751 regname = c;
15752 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015753 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 set_vim_var_string(VV_REG, &regname, 1);
15755}
15756
15757/*
15758 * Get or set v:exception. If "oldval" == NULL, return the current value.
15759 * Otherwise, restore the value to "oldval" and return NULL.
15760 * Must always be called in pairs to save and restore v:exception! Does not
15761 * take care of memory allocations.
15762 */
15763 char_u *
15764v_exception(oldval)
15765 char_u *oldval;
15766{
15767 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015768 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769
Bram Moolenaare9a41262005-01-15 22:18:47 +000015770 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 return NULL;
15772}
15773
15774/*
15775 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15776 * Otherwise, restore the value to "oldval" and return NULL.
15777 * Must always be called in pairs to save and restore v:throwpoint! Does not
15778 * take care of memory allocations.
15779 */
15780 char_u *
15781v_throwpoint(oldval)
15782 char_u *oldval;
15783{
15784 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015785 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786
Bram Moolenaare9a41262005-01-15 22:18:47 +000015787 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 return NULL;
15789}
15790
15791#if defined(FEAT_AUTOCMD) || defined(PROTO)
15792/*
15793 * Set v:cmdarg.
15794 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15795 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15796 * Must always be called in pairs!
15797 */
15798 char_u *
15799set_cmdarg(eap, oldarg)
15800 exarg_T *eap;
15801 char_u *oldarg;
15802{
15803 char_u *oldval;
15804 char_u *newval;
15805 unsigned len;
15806
Bram Moolenaare9a41262005-01-15 22:18:47 +000015807 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015808 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015810 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015811 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015812 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813 }
15814
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015815 if (eap->force_bin == FORCE_BIN)
15816 len = 6;
15817 else if (eap->force_bin == FORCE_NOBIN)
15818 len = 8;
15819 else
15820 len = 0;
15821 if (eap->force_ff != 0)
15822 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15823# ifdef FEAT_MBYTE
15824 if (eap->force_enc != 0)
15825 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000015826 if (eap->bad_char != 0)
15827 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015828# endif
15829
15830 newval = alloc(len + 1);
15831 if (newval == NULL)
15832 return NULL;
15833
15834 if (eap->force_bin == FORCE_BIN)
15835 sprintf((char *)newval, " ++bin");
15836 else if (eap->force_bin == FORCE_NOBIN)
15837 sprintf((char *)newval, " ++nobin");
15838 else
15839 *newval = NUL;
15840 if (eap->force_ff != 0)
15841 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15842 eap->cmd + eap->force_ff);
15843# ifdef FEAT_MBYTE
15844 if (eap->force_enc != 0)
15845 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15846 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000015847 if (eap->bad_char != 0)
15848 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
15849 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015850# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015851 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015852 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853}
15854#endif
15855
15856/*
15857 * Get the value of internal variable "name".
15858 * Return OK or FAIL.
15859 */
15860 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015861get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862 char_u *name;
15863 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015864 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015865 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866{
15867 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015868 typval_T *tv = NULL;
15869 typval_T atv;
15870 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872
15873 /* truncate the name, so that we can use strcmp() */
15874 cc = name[len];
15875 name[len] = NUL;
15876
15877 /*
15878 * Check for "b:changedtick".
15879 */
15880 if (STRCMP(name, "b:changedtick") == 0)
15881 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015882 atv.v_type = VAR_NUMBER;
15883 atv.vval.v_number = curbuf->b_changedtick;
15884 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885 }
15886
15887 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 * Check for user-defined variables.
15889 */
15890 else
15891 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015892 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015894 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895 }
15896
Bram Moolenaare9a41262005-01-15 22:18:47 +000015897 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015899 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015900 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901 ret = FAIL;
15902 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015903 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015904 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905
15906 name[len] = cc;
15907
15908 return ret;
15909}
15910
15911/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015912 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15913 * Also handle function call with Funcref variable: func(expr)
15914 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15915 */
15916 static int
15917handle_subscript(arg, rettv, evaluate, verbose)
15918 char_u **arg;
15919 typval_T *rettv;
15920 int evaluate; /* do more than finding the end */
15921 int verbose; /* give error messages */
15922{
15923 int ret = OK;
15924 dict_T *selfdict = NULL;
15925 char_u *s;
15926 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015927 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015928
15929 while (ret == OK
15930 && (**arg == '['
15931 || (**arg == '.' && rettv->v_type == VAR_DICT)
15932 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15933 && !vim_iswhite(*(*arg - 1)))
15934 {
15935 if (**arg == '(')
15936 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015937 /* need to copy the funcref so that we can clear rettv */
15938 functv = *rettv;
15939 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015940
15941 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015942 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015943 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015944 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15945 &len, evaluate, selfdict);
15946
15947 /* Clear the funcref afterwards, so that deleting it while
15948 * evaluating the arguments is possible (see test55). */
15949 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015950
15951 /* Stop the expression evaluation when immediately aborting on
15952 * error, or when an interrupt occurred or an exception was thrown
15953 * but not caught. */
15954 if (aborting())
15955 {
15956 if (ret == OK)
15957 clear_tv(rettv);
15958 ret = FAIL;
15959 }
15960 dict_unref(selfdict);
15961 selfdict = NULL;
15962 }
15963 else /* **arg == '[' || **arg == '.' */
15964 {
15965 dict_unref(selfdict);
15966 if (rettv->v_type == VAR_DICT)
15967 {
15968 selfdict = rettv->vval.v_dict;
15969 if (selfdict != NULL)
15970 ++selfdict->dv_refcount;
15971 }
15972 else
15973 selfdict = NULL;
15974 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15975 {
15976 clear_tv(rettv);
15977 ret = FAIL;
15978 }
15979 }
15980 }
15981 dict_unref(selfdict);
15982 return ret;
15983}
15984
15985/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015986 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15987 * value).
15988 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015989 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015990alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015991{
Bram Moolenaar33570922005-01-25 22:26:29 +000015992 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015993}
15994
15995/*
15996 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997 * The string "s" must have been allocated, it is consumed.
15998 * Return NULL for out of memory, the variable otherwise.
15999 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016000 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016001alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002 char_u *s;
16003{
Bram Moolenaar33570922005-01-25 22:26:29 +000016004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016006 rettv = alloc_tv();
16007 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016009 rettv->v_type = VAR_STRING;
16010 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011 }
16012 else
16013 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016014 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015}
16016
16017/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016018 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016020 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016021free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016022 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023{
16024 if (varp != NULL)
16025 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016026 switch (varp->v_type)
16027 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016028 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016029 func_unref(varp->vval.v_string);
16030 /*FALLTHROUGH*/
16031 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016032 vim_free(varp->vval.v_string);
16033 break;
16034 case VAR_LIST:
16035 list_unref(varp->vval.v_list);
16036 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016037 case VAR_DICT:
16038 dict_unref(varp->vval.v_dict);
16039 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016040 case VAR_NUMBER:
16041 case VAR_UNKNOWN:
16042 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016043 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016044 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016045 break;
16046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 vim_free(varp);
16048 }
16049}
16050
16051/*
16052 * Free the memory for a variable value and set the value to NULL or 0.
16053 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016054 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016055clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016056 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057{
16058 if (varp != NULL)
16059 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016060 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016062 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016063 func_unref(varp->vval.v_string);
16064 /*FALLTHROUGH*/
16065 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016066 vim_free(varp->vval.v_string);
16067 varp->vval.v_string = NULL;
16068 break;
16069 case VAR_LIST:
16070 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016071 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016072 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016073 case VAR_DICT:
16074 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016075 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016076 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016077 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016078 varp->vval.v_number = 0;
16079 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016080 case VAR_UNKNOWN:
16081 break;
16082 default:
16083 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016085 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 }
16087}
16088
16089/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016090 * Set the value of a variable to NULL without freeing items.
16091 */
16092 static void
16093init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016094 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016095{
16096 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016097 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016098}
16099
16100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 * Get the number value of a variable.
16102 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016103 * For incompatible types, return 0.
16104 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16105 * caller of incompatible types: it sets *denote to TRUE if "denote"
16106 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107 */
16108 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016109get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016110 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016112 int error = FALSE;
16113
16114 return get_tv_number_chk(varp, &error); /* return 0L on error */
16115}
16116
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016117 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016118get_tv_number_chk(varp, denote)
16119 typval_T *varp;
16120 int *denote;
16121{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016122 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016124 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016126 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016127 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016128 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016129 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016130 break;
16131 case VAR_STRING:
16132 if (varp->vval.v_string != NULL)
16133 vim_str2nr(varp->vval.v_string, NULL, NULL,
16134 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016135 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016136 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016137 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016138 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016139 case VAR_DICT:
16140 EMSG(_("E728: Using a Dictionary as a number"));
16141 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016142 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016143 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016144 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016146 if (denote == NULL) /* useful for values that must be unsigned */
16147 n = -1;
16148 else
16149 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016150 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016151}
16152
16153/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016154 * Get the lnum from the first argument.
16155 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016156 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 */
16158 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016159get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016160 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161{
Bram Moolenaar33570922005-01-25 22:26:29 +000016162 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163 linenr_T lnum;
16164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016165 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 if (lnum == 0) /* no valid number, try using line() */
16167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016168 rettv.v_type = VAR_NUMBER;
16169 f_line(argvars, &rettv);
16170 lnum = rettv.vval.v_number;
16171 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172 }
16173 return lnum;
16174}
16175
16176/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016177 * Get the lnum from the first argument.
16178 * Also accepts "$", then "buf" is used.
16179 * Returns 0 on error.
16180 */
16181 static linenr_T
16182get_tv_lnum_buf(argvars, buf)
16183 typval_T *argvars;
16184 buf_T *buf;
16185{
16186 if (argvars[0].v_type == VAR_STRING
16187 && argvars[0].vval.v_string != NULL
16188 && argvars[0].vval.v_string[0] == '$'
16189 && buf != NULL)
16190 return buf->b_ml.ml_line_count;
16191 return get_tv_number_chk(&argvars[0], NULL);
16192}
16193
16194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 * Get the string value of a variable.
16196 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016197 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16198 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 * If the String variable has never been set, return an empty string.
16200 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016201 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16202 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203 */
16204 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016205get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016206 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207{
16208 static char_u mybuf[NUMBUFLEN];
16209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016210 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211}
16212
16213 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016214get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016215 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016216 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016218 char_u *res = get_tv_string_buf_chk(varp, buf);
16219
16220 return res != NULL ? res : (char_u *)"";
16221}
16222
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016223 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016224get_tv_string_chk(varp)
16225 typval_T *varp;
16226{
16227 static char_u mybuf[NUMBUFLEN];
16228
16229 return get_tv_string_buf_chk(varp, mybuf);
16230}
16231
16232 static char_u *
16233get_tv_string_buf_chk(varp, buf)
16234 typval_T *varp;
16235 char_u *buf;
16236{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016237 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016239 case VAR_NUMBER:
16240 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16241 return buf;
16242 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016243 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016244 break;
16245 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016246 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016247 break;
16248 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016249 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016250 break;
16251 case VAR_STRING:
16252 if (varp->vval.v_string != NULL)
16253 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016254 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016255 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016256 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016257 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016259 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260}
16261
16262/*
16263 * Find variable "name" in the list of variables.
16264 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016265 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016266 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016267 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016270find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016272 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016275 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276
Bram Moolenaara7043832005-01-21 11:56:39 +000016277 ht = find_var_ht(name, &varname);
16278 if (htp != NULL)
16279 *htp = ht;
16280 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016282 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016283}
16284
16285/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016286 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016287 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016289 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016290find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016291 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016292 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016293 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016294{
Bram Moolenaar33570922005-01-25 22:26:29 +000016295 hashitem_T *hi;
16296
16297 if (*varname == NUL)
16298 {
16299 /* Must be something like "s:", otherwise "ht" would be NULL. */
16300 switch (varname[-2])
16301 {
16302 case 's': return &SCRIPT_SV(current_SID).sv_var;
16303 case 'g': return &globvars_var;
16304 case 'v': return &vimvars_var;
16305 case 'b': return &curbuf->b_bufvar;
16306 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016307 case 'l': return current_funccal == NULL
16308 ? NULL : &current_funccal->l_vars_var;
16309 case 'a': return current_funccal == NULL
16310 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016311 }
16312 return NULL;
16313 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016314
16315 hi = hash_find(ht, varname);
16316 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016317 {
16318 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016319 * worked find the variable again. Don't auto-load a script if it was
16320 * loaded already, otherwise it would be loaded every time when
16321 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016322 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016323 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016324 hi = hash_find(ht, varname);
16325 if (HASHITEM_EMPTY(hi))
16326 return NULL;
16327 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016328 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016329}
16330
16331/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016332 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016333 * Set "varname" to the start of name without ':'.
16334 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016335 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016336find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 char_u *name;
16338 char_u **varname;
16339{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016340 hashitem_T *hi;
16341
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342 if (name[1] != ':')
16343 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016344 /* The name must not start with a colon or #. */
16345 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346 return NULL;
16347 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016348
16349 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016350 hi = hash_find(&compat_hashtab, name);
16351 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016352 return &compat_hashtab;
16353
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016355 return &globvarht; /* global variable */
16356 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357 }
16358 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016359 if (*name == 'g') /* global variable */
16360 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016361 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16362 */
16363 if (vim_strchr(name + 2, ':') != NULL
16364 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016365 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016367 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016369 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016370 if (*name == 'v') /* v: variable */
16371 return &vimvarht;
16372 if (*name == 'a' && current_funccal != NULL) /* function argument */
16373 return &current_funccal->l_avars.dv_hashtab;
16374 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16375 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376 if (*name == 's' /* script variable */
16377 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16378 return &SCRIPT_VARS(current_SID);
16379 return NULL;
16380}
16381
16382/*
16383 * Get the string value of a (global/local) variable.
16384 * Returns NULL when it doesn't exist.
16385 */
16386 char_u *
16387get_var_value(name)
16388 char_u *name;
16389{
Bram Moolenaar33570922005-01-25 22:26:29 +000016390 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391
Bram Moolenaara7043832005-01-21 11:56:39 +000016392 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 if (v == NULL)
16394 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016395 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396}
16397
16398/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016399 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400 * sourcing this script and when executing functions defined in the script.
16401 */
16402 void
16403new_script_vars(id)
16404 scid_T id;
16405{
Bram Moolenaara7043832005-01-21 11:56:39 +000016406 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016407 hashtab_T *ht;
16408 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016409
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16411 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016412 /* Re-allocating ga_data means that an ht_array pointing to
16413 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016414 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016415 for (i = 1; i <= ga_scripts.ga_len; ++i)
16416 {
16417 ht = &SCRIPT_VARS(i);
16418 if (ht->ht_mask == HT_INIT_SIZE - 1)
16419 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016420 sv = &SCRIPT_SV(i);
16421 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016422 }
16423
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424 while (ga_scripts.ga_len < id)
16425 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016426 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16427 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429 }
16430 }
16431}
16432
16433/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016434 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16435 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436 */
16437 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016438init_var_dict(dict, dict_var)
16439 dict_T *dict;
16440 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441{
Bram Moolenaar33570922005-01-25 22:26:29 +000016442 hash_init(&dict->dv_hashtab);
16443 dict->dv_refcount = 99999;
16444 dict_var->di_tv.vval.v_dict = dict;
16445 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016446 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016447 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16448 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449}
16450
16451/*
16452 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016453 * Frees all allocated variables and the value they contain.
16454 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455 */
16456 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016457vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016458 hashtab_T *ht;
16459{
16460 vars_clear_ext(ht, TRUE);
16461}
16462
16463/*
16464 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16465 */
16466 static void
16467vars_clear_ext(ht, free_val)
16468 hashtab_T *ht;
16469 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470{
Bram Moolenaara7043832005-01-21 11:56:39 +000016471 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016472 hashitem_T *hi;
16473 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474
Bram Moolenaar33570922005-01-25 22:26:29 +000016475 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016476 todo = ht->ht_used;
16477 for (hi = ht->ht_array; todo > 0; ++hi)
16478 {
16479 if (!HASHITEM_EMPTY(hi))
16480 {
16481 --todo;
16482
Bram Moolenaar33570922005-01-25 22:26:29 +000016483 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016484 * ht_array might change then. hash_clear() takes care of it
16485 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016486 v = HI2DI(hi);
16487 if (free_val)
16488 clear_tv(&v->di_tv);
16489 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16490 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016491 }
16492 }
16493 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016494 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495}
16496
Bram Moolenaara7043832005-01-21 11:56:39 +000016497/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016498 * Delete a variable from hashtab "ht" at item "hi".
16499 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016502delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016503 hashtab_T *ht;
16504 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505{
Bram Moolenaar33570922005-01-25 22:26:29 +000016506 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016507
16508 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016509 clear_tv(&di->di_tv);
16510 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511}
16512
16513/*
16514 * List the value of one internal variable.
16515 */
16516 static void
16517list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016518 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519 char_u *prefix;
16520{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016521 char_u *tofree;
16522 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016523 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016524
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016525 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000016526 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016527 s == NULL ? (char_u *)"" : s);
16528 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529}
16530
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 static void
16532list_one_var_a(prefix, name, type, string)
16533 char_u *prefix;
16534 char_u *name;
16535 int type;
16536 char_u *string;
16537{
16538 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16539 if (name != NULL) /* "a:" vars don't have a name stored */
16540 msg_puts(name);
16541 msg_putchar(' ');
16542 msg_advance(22);
16543 if (type == VAR_NUMBER)
16544 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016545 else if (type == VAR_FUNC)
16546 msg_putchar('*');
16547 else if (type == VAR_LIST)
16548 {
16549 msg_putchar('[');
16550 if (*string == '[')
16551 ++string;
16552 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016553 else if (type == VAR_DICT)
16554 {
16555 msg_putchar('{');
16556 if (*string == '{')
16557 ++string;
16558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 else
16560 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016561
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016563
16564 if (type == VAR_FUNC)
16565 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566}
16567
16568/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016569 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 * If the variable already exists, the value is updated.
16571 * Otherwise the variable is created.
16572 */
16573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016574set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016576 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016577 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578{
Bram Moolenaar33570922005-01-25 22:26:29 +000016579 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016581 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016582 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016584 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016585 {
16586 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16587 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16588 ? name[2] : name[0]))
16589 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016590 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016591 return;
16592 }
16593 if (function_exists(name))
16594 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016595 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016596 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016597 return;
16598 }
16599 }
16600
Bram Moolenaara7043832005-01-21 11:56:39 +000016601 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016602 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016603 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016604 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016605 return;
16606 }
16607
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016608 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016609 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016610 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016611 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016612 if (var_check_ro(v->di_flags, name)
16613 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016614 return;
16615 if (v->di_tv.v_type != tv->v_type
16616 && !((v->di_tv.v_type == VAR_STRING
16617 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016618 && (tv->v_type == VAR_STRING
16619 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016620 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016621 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016622 return;
16623 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016624
16625 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016626 * Handle setting internal v: variables separately: we don't change
16627 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016628 */
16629 if (ht == &vimvarht)
16630 {
16631 if (v->di_tv.v_type == VAR_STRING)
16632 {
16633 vim_free(v->di_tv.vval.v_string);
16634 if (copy || tv->v_type != VAR_STRING)
16635 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16636 else
16637 {
16638 /* Take over the string to avoid an extra alloc/free. */
16639 v->di_tv.vval.v_string = tv->vval.v_string;
16640 tv->vval.v_string = NULL;
16641 }
16642 }
16643 else if (v->di_tv.v_type != VAR_NUMBER)
16644 EMSG2(_(e_intern2), "set_var()");
16645 else
16646 v->di_tv.vval.v_number = get_tv_number(tv);
16647 return;
16648 }
16649
16650 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016651 }
16652 else /* add a new variable */
16653 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016654 /* Make sure the variable name is valid. */
16655 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000016656 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
16657 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000016658 {
16659 EMSG2(_(e_illvar), varname);
16660 return;
16661 }
16662
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016663 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16664 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016665 if (v == NULL)
16666 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016667 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016668 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016670 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671 return;
16672 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016673 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016676 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016677 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016678 else
16679 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016680 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016681 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016682 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684}
16685
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016686/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016687 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16688 * Also give an error message.
16689 */
16690 static int
16691var_check_ro(flags, name)
16692 int flags;
16693 char_u *name;
16694{
16695 if (flags & DI_FLAGS_RO)
16696 {
16697 EMSG2(_(e_readonlyvar), name);
16698 return TRUE;
16699 }
16700 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16701 {
16702 EMSG2(_(e_readonlysbx), name);
16703 return TRUE;
16704 }
16705 return FALSE;
16706}
16707
16708/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016709 * Return TRUE if typeval "tv" is set to be locked (immutable).
16710 * Also give an error message, using "name".
16711 */
16712 static int
16713tv_check_lock(lock, name)
16714 int lock;
16715 char_u *name;
16716{
16717 if (lock & VAR_LOCKED)
16718 {
16719 EMSG2(_("E741: Value is locked: %s"),
16720 name == NULL ? (char_u *)_("Unknown") : name);
16721 return TRUE;
16722 }
16723 if (lock & VAR_FIXED)
16724 {
16725 EMSG2(_("E742: Cannot change value of %s"),
16726 name == NULL ? (char_u *)_("Unknown") : name);
16727 return TRUE;
16728 }
16729 return FALSE;
16730}
16731
16732/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016733 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016734 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016735 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016738copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016739 typval_T *from;
16740 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016742 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016743 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016744 switch (from->v_type)
16745 {
16746 case VAR_NUMBER:
16747 to->vval.v_number = from->vval.v_number;
16748 break;
16749 case VAR_STRING:
16750 case VAR_FUNC:
16751 if (from->vval.v_string == NULL)
16752 to->vval.v_string = NULL;
16753 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016754 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016755 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016756 if (from->v_type == VAR_FUNC)
16757 func_ref(to->vval.v_string);
16758 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016759 break;
16760 case VAR_LIST:
16761 if (from->vval.v_list == NULL)
16762 to->vval.v_list = NULL;
16763 else
16764 {
16765 to->vval.v_list = from->vval.v_list;
16766 ++to->vval.v_list->lv_refcount;
16767 }
16768 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016769 case VAR_DICT:
16770 if (from->vval.v_dict == NULL)
16771 to->vval.v_dict = NULL;
16772 else
16773 {
16774 to->vval.v_dict = from->vval.v_dict;
16775 ++to->vval.v_dict->dv_refcount;
16776 }
16777 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016778 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016779 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016780 break;
16781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016782}
16783
16784/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016785 * Make a copy of an item.
16786 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016787 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16788 * reference to an already copied list/dict can be used.
16789 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016790 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016791 static int
16792item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016793 typval_T *from;
16794 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016795 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016796 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016797{
16798 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016799 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016800
Bram Moolenaar33570922005-01-25 22:26:29 +000016801 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016802 {
16803 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016804 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016805 }
16806 ++recurse;
16807
16808 switch (from->v_type)
16809 {
16810 case VAR_NUMBER:
16811 case VAR_STRING:
16812 case VAR_FUNC:
16813 copy_tv(from, to);
16814 break;
16815 case VAR_LIST:
16816 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016817 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016818 if (from->vval.v_list == NULL)
16819 to->vval.v_list = NULL;
16820 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16821 {
16822 /* use the copy made earlier */
16823 to->vval.v_list = from->vval.v_list->lv_copylist;
16824 ++to->vval.v_list->lv_refcount;
16825 }
16826 else
16827 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16828 if (to->vval.v_list == NULL)
16829 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016830 break;
16831 case VAR_DICT:
16832 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016833 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016834 if (from->vval.v_dict == NULL)
16835 to->vval.v_dict = NULL;
16836 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16837 {
16838 /* use the copy made earlier */
16839 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16840 ++to->vval.v_dict->dv_refcount;
16841 }
16842 else
16843 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16844 if (to->vval.v_dict == NULL)
16845 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016846 break;
16847 default:
16848 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016849 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016850 }
16851 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016852 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016853}
16854
16855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856 * ":echo expr1 ..." print each argument separated with a space, add a
16857 * newline at the end.
16858 * ":echon expr1 ..." print each argument plain.
16859 */
16860 void
16861ex_echo(eap)
16862 exarg_T *eap;
16863{
16864 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016865 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016866 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 char_u *p;
16868 int needclr = TRUE;
16869 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016870 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871
16872 if (eap->skip)
16873 ++emsg_skip;
16874 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16875 {
16876 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016877 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 {
16879 /*
16880 * Report the invalid expression unless the expression evaluation
16881 * has been cancelled due to an aborting error, an interrupt, or an
16882 * exception.
16883 */
16884 if (!aborting())
16885 EMSG2(_(e_invexpr2), p);
16886 break;
16887 }
16888 if (!eap->skip)
16889 {
16890 if (atstart)
16891 {
16892 atstart = FALSE;
16893 /* Call msg_start() after eval1(), evaluating the expression
16894 * may cause a message to appear. */
16895 if (eap->cmdidx == CMD_echo)
16896 msg_start();
16897 }
16898 else if (eap->cmdidx == CMD_echo)
16899 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016900 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016901 if (p != NULL)
16902 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016904 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016906 if (*p != TAB && needclr)
16907 {
16908 /* remove any text still there from the command */
16909 msg_clr_eos();
16910 needclr = FALSE;
16911 }
16912 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 }
16914 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016915 {
16916#ifdef FEAT_MBYTE
16917 if (has_mbyte)
16918 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016919 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016920
16921 (void)msg_outtrans_len_attr(p, i, echo_attr);
16922 p += i - 1;
16923 }
16924 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016926 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016929 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016931 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 arg = skipwhite(arg);
16933 }
16934 eap->nextcmd = check_nextcmd(arg);
16935
16936 if (eap->skip)
16937 --emsg_skip;
16938 else
16939 {
16940 /* remove text that may still be there from the command */
16941 if (needclr)
16942 msg_clr_eos();
16943 if (eap->cmdidx == CMD_echo)
16944 msg_end();
16945 }
16946}
16947
16948/*
16949 * ":echohl {name}".
16950 */
16951 void
16952ex_echohl(eap)
16953 exarg_T *eap;
16954{
16955 int id;
16956
16957 id = syn_name2id(eap->arg);
16958 if (id == 0)
16959 echo_attr = 0;
16960 else
16961 echo_attr = syn_id2attr(id);
16962}
16963
16964/*
16965 * ":execute expr1 ..." execute the result of an expression.
16966 * ":echomsg expr1 ..." Print a message
16967 * ":echoerr expr1 ..." Print an error
16968 * Each gets spaces around each argument and a newline at the end for
16969 * echo commands
16970 */
16971 void
16972ex_execute(eap)
16973 exarg_T *eap;
16974{
16975 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016976 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977 int ret = OK;
16978 char_u *p;
16979 garray_T ga;
16980 int len;
16981 int save_did_emsg;
16982
16983 ga_init2(&ga, 1, 80);
16984
16985 if (eap->skip)
16986 ++emsg_skip;
16987 while (*arg != NUL && *arg != '|' && *arg != '\n')
16988 {
16989 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016990 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 {
16992 /*
16993 * Report the invalid expression unless the expression evaluation
16994 * has been cancelled due to an aborting error, an interrupt, or an
16995 * exception.
16996 */
16997 if (!aborting())
16998 EMSG2(_(e_invexpr2), p);
16999 ret = FAIL;
17000 break;
17001 }
17002
17003 if (!eap->skip)
17004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017005 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 len = (int)STRLEN(p);
17007 if (ga_grow(&ga, len + 2) == FAIL)
17008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017009 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010 ret = FAIL;
17011 break;
17012 }
17013 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 ga.ga_len += len;
17017 }
17018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017019 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 arg = skipwhite(arg);
17021 }
17022
17023 if (ret != FAIL && ga.ga_data != NULL)
17024 {
17025 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017028 out_flush();
17029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 else if (eap->cmdidx == CMD_echoerr)
17031 {
17032 /* We don't want to abort following commands, restore did_emsg. */
17033 save_did_emsg = did_emsg;
17034 EMSG((char_u *)ga.ga_data);
17035 if (!force_abort)
17036 did_emsg = save_did_emsg;
17037 }
17038 else if (eap->cmdidx == CMD_execute)
17039 do_cmdline((char_u *)ga.ga_data,
17040 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17041 }
17042
17043 ga_clear(&ga);
17044
17045 if (eap->skip)
17046 --emsg_skip;
17047
17048 eap->nextcmd = check_nextcmd(arg);
17049}
17050
17051/*
17052 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17053 * "arg" points to the "&" or '+' when called, to "option" when returning.
17054 * Returns NULL when no option name found. Otherwise pointer to the char
17055 * after the option name.
17056 */
17057 static char_u *
17058find_option_end(arg, opt_flags)
17059 char_u **arg;
17060 int *opt_flags;
17061{
17062 char_u *p = *arg;
17063
17064 ++p;
17065 if (*p == 'g' && p[1] == ':')
17066 {
17067 *opt_flags = OPT_GLOBAL;
17068 p += 2;
17069 }
17070 else if (*p == 'l' && p[1] == ':')
17071 {
17072 *opt_flags = OPT_LOCAL;
17073 p += 2;
17074 }
17075 else
17076 *opt_flags = 0;
17077
17078 if (!ASCII_ISALPHA(*p))
17079 return NULL;
17080 *arg = p;
17081
17082 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17083 p += 4; /* termcap option */
17084 else
17085 while (ASCII_ISALPHA(*p))
17086 ++p;
17087 return p;
17088}
17089
17090/*
17091 * ":function"
17092 */
17093 void
17094ex_function(eap)
17095 exarg_T *eap;
17096{
17097 char_u *theline;
17098 int j;
17099 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101 char_u *name = NULL;
17102 char_u *p;
17103 char_u *arg;
17104 garray_T newargs;
17105 garray_T newlines;
17106 int varargs = FALSE;
17107 int mustend = FALSE;
17108 int flags = 0;
17109 ufunc_T *fp;
17110 int indent;
17111 int nesting;
17112 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017113 dictitem_T *v;
17114 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017115 static int func_nr = 0; /* number for nameless function */
17116 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017117 hashtab_T *ht;
17118 int todo;
17119 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120
17121 /*
17122 * ":function" without argument: list functions.
17123 */
17124 if (ends_excmd(*eap->arg))
17125 {
17126 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017127 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017128 todo = func_hashtab.ht_used;
17129 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017130 {
17131 if (!HASHITEM_EMPTY(hi))
17132 {
17133 --todo;
17134 fp = HI2UF(hi);
17135 if (!isdigit(*fp->uf_name))
17136 list_func_head(fp, FALSE);
17137 }
17138 }
17139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 eap->nextcmd = check_nextcmd(eap->arg);
17141 return;
17142 }
17143
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017144 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017145 * ":function /pat": list functions matching pattern.
17146 */
17147 if (*eap->arg == '/')
17148 {
17149 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17150 if (!eap->skip)
17151 {
17152 regmatch_T regmatch;
17153
17154 c = *p;
17155 *p = NUL;
17156 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17157 *p = c;
17158 if (regmatch.regprog != NULL)
17159 {
17160 regmatch.rm_ic = p_ic;
17161
17162 todo = func_hashtab.ht_used;
17163 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17164 {
17165 if (!HASHITEM_EMPTY(hi))
17166 {
17167 --todo;
17168 fp = HI2UF(hi);
17169 if (!isdigit(*fp->uf_name)
17170 && vim_regexec(&regmatch, fp->uf_name, 0))
17171 list_func_head(fp, FALSE);
17172 }
17173 }
17174 }
17175 }
17176 if (*p == '/')
17177 ++p;
17178 eap->nextcmd = check_nextcmd(p);
17179 return;
17180 }
17181
17182 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017183 * Get the function name. There are these situations:
17184 * func normal function name
17185 * "name" == func, "fudi.fd_dict" == NULL
17186 * dict.func new dictionary entry
17187 * "name" == NULL, "fudi.fd_dict" set,
17188 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17189 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017190 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017191 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17192 * dict.func existing dict entry that's not a Funcref
17193 * "name" == NULL, "fudi.fd_dict" set,
17194 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017197 name = trans_function_name(&p, eap->skip, 0, &fudi);
17198 paren = (vim_strchr(p, '(') != NULL);
17199 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 {
17201 /*
17202 * Return on an invalid expression in braces, unless the expression
17203 * evaluation has been cancelled due to an aborting error, an
17204 * interrupt, or an exception.
17205 */
17206 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017207 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017208 if (!eap->skip && fudi.fd_newkey != NULL)
17209 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017210 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 else
17214 eap->skip = TRUE;
17215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017216 /* An error in a function call during evaluation of an expression in magic
17217 * braces should not cause the function not to be defined. */
17218 saved_did_emsg = did_emsg;
17219 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220
17221 /*
17222 * ":function func" with only function name: list function.
17223 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017224 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225 {
17226 if (!ends_excmd(*skipwhite(p)))
17227 {
17228 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017229 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230 }
17231 eap->nextcmd = check_nextcmd(p);
17232 if (eap->nextcmd != NULL)
17233 *p = NUL;
17234 if (!eap->skip && !got_int)
17235 {
17236 fp = find_func(name);
17237 if (fp != NULL)
17238 {
17239 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017240 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 {
17242 msg_putchar('\n');
17243 msg_outnum((long)(j + 1));
17244 if (j < 9)
17245 msg_putchar(' ');
17246 if (j < 99)
17247 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017248 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 out_flush(); /* show a line at a time */
17250 ui_breakcheck();
17251 }
17252 if (!got_int)
17253 {
17254 msg_putchar('\n');
17255 msg_puts((char_u *)" endfunction");
17256 }
17257 }
17258 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017259 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017261 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262 }
17263
17264 /*
17265 * ":function name(arg1, arg2)" Define function.
17266 */
17267 p = skipwhite(p);
17268 if (*p != '(')
17269 {
17270 if (!eap->skip)
17271 {
17272 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017273 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 }
17275 /* attempt to continue by skipping some text */
17276 if (vim_strchr(p, '(') != NULL)
17277 p = vim_strchr(p, '(');
17278 }
17279 p = skipwhite(p + 1);
17280
17281 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17282 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17283
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017284 if (!eap->skip)
17285 {
17286 /* Check the name of the function. */
17287 if (name != NULL)
17288 arg = name;
17289 else
17290 arg = fudi.fd_newkey;
17291 if (arg != NULL)
17292 {
17293 if (*arg == K_SPECIAL)
17294 j = 3;
17295 else
17296 j = 0;
17297 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17298 : eval_isnamec(arg[j])))
17299 ++j;
17300 if (arg[j] != NUL)
17301 emsg_funcname(_(e_invarg2), arg);
17302 }
17303 }
17304
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305 /*
17306 * Isolate the arguments: "arg1, arg2, ...)"
17307 */
17308 while (*p != ')')
17309 {
17310 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17311 {
17312 varargs = TRUE;
17313 p += 3;
17314 mustend = TRUE;
17315 }
17316 else
17317 {
17318 arg = p;
17319 while (ASCII_ISALNUM(*p) || *p == '_')
17320 ++p;
17321 if (arg == p || isdigit(*arg)
17322 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17323 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17324 {
17325 if (!eap->skip)
17326 EMSG2(_("E125: Illegal argument: %s"), arg);
17327 break;
17328 }
17329 if (ga_grow(&newargs, 1) == FAIL)
17330 goto erret;
17331 c = *p;
17332 *p = NUL;
17333 arg = vim_strsave(arg);
17334 if (arg == NULL)
17335 goto erret;
17336 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17337 *p = c;
17338 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 if (*p == ',')
17340 ++p;
17341 else
17342 mustend = TRUE;
17343 }
17344 p = skipwhite(p);
17345 if (mustend && *p != ')')
17346 {
17347 if (!eap->skip)
17348 EMSG2(_(e_invarg2), eap->arg);
17349 break;
17350 }
17351 }
17352 ++p; /* skip the ')' */
17353
Bram Moolenaare9a41262005-01-15 22:18:47 +000017354 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355 for (;;)
17356 {
17357 p = skipwhite(p);
17358 if (STRNCMP(p, "range", 5) == 0)
17359 {
17360 flags |= FC_RANGE;
17361 p += 5;
17362 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017363 else if (STRNCMP(p, "dict", 4) == 0)
17364 {
17365 flags |= FC_DICT;
17366 p += 4;
17367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368 else if (STRNCMP(p, "abort", 5) == 0)
17369 {
17370 flags |= FC_ABORT;
17371 p += 5;
17372 }
17373 else
17374 break;
17375 }
17376
17377 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
17378 EMSG(_(e_trailing));
17379
17380 /*
17381 * Read the body of the function, until ":endfunction" is found.
17382 */
17383 if (KeyTyped)
17384 {
17385 /* Check if the function already exists, don't let the user type the
17386 * whole function before telling him it doesn't work! For a script we
17387 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017388 if (!eap->skip && !eap->forceit)
17389 {
17390 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17391 EMSG(_(e_funcdict));
17392 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017393 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017396 if (!eap->skip && did_emsg)
17397 goto erret;
17398
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 msg_putchar('\n'); /* don't overwrite the function name */
17400 cmdline_row = msg_row;
17401 }
17402
17403 indent = 2;
17404 nesting = 0;
17405 for (;;)
17406 {
17407 msg_scroll = TRUE;
17408 need_wait_return = FALSE;
17409 if (eap->getline == NULL)
17410 theline = getcmdline(':', 0L, indent);
17411 else
17412 theline = eap->getline(':', eap->cookie, indent);
17413 if (KeyTyped)
17414 lines_left = Rows - 1;
17415 if (theline == NULL)
17416 {
17417 EMSG(_("E126: Missing :endfunction"));
17418 goto erret;
17419 }
17420
17421 if (skip_until != NULL)
17422 {
17423 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17424 * don't check for ":endfunc". */
17425 if (STRCMP(theline, skip_until) == 0)
17426 {
17427 vim_free(skip_until);
17428 skip_until = NULL;
17429 }
17430 }
17431 else
17432 {
17433 /* skip ':' and blanks*/
17434 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17435 ;
17436
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017437 /* Check for "endfunction". */
17438 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439 {
17440 vim_free(theline);
17441 break;
17442 }
17443
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017444 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445 * at "end". */
17446 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17447 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017448 else if (STRNCMP(p, "if", 2) == 0
17449 || STRNCMP(p, "wh", 2) == 0
17450 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 || STRNCMP(p, "try", 3) == 0)
17452 indent += 2;
17453
17454 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017455 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017457 if (*p == '!')
17458 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459 p += eval_fname_script(p);
17460 if (ASCII_ISALPHA(*p))
17461 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017462 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463 if (*skipwhite(p) == '(')
17464 {
17465 ++nesting;
17466 indent += 2;
17467 }
17468 }
17469 }
17470
17471 /* Check for ":append" or ":insert". */
17472 p = skip_range(p, NULL);
17473 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17474 || (p[0] == 'i'
17475 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17476 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17477 skip_until = vim_strsave((char_u *)".");
17478
17479 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17480 arg = skipwhite(skiptowhite(p));
17481 if (arg[0] == '<' && arg[1] =='<'
17482 && ((p[0] == 'p' && p[1] == 'y'
17483 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17484 || (p[0] == 'p' && p[1] == 'e'
17485 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17486 || (p[0] == 't' && p[1] == 'c'
17487 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17488 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17489 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017490 || (p[0] == 'm' && p[1] == 'z'
17491 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 ))
17493 {
17494 /* ":python <<" continues until a dot, like ":append" */
17495 p = skipwhite(arg + 2);
17496 if (*p == NUL)
17497 skip_until = vim_strsave((char_u *)".");
17498 else
17499 skip_until = vim_strsave(p);
17500 }
17501 }
17502
17503 /* Add the line to the function. */
17504 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017505 {
17506 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017508 }
17509
17510 /* Copy the line to newly allocated memory. get_one_sourceline()
17511 * allocates 250 bytes per line, this saves 80% on average. The cost
17512 * is an extra alloc/free. */
17513 p = vim_strsave(theline);
17514 if (p != NULL)
17515 {
17516 vim_free(theline);
17517 theline = p;
17518 }
17519
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17521 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 }
17523
17524 /* Don't define the function when skipping commands or when an error was
17525 * detected. */
17526 if (eap->skip || did_emsg)
17527 goto erret;
17528
17529 /*
17530 * If there are no errors, add the function
17531 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017532 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017533 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017534 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017535 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017536 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017537 emsg_funcname("E707: Function name conflicts with variable: %s",
17538 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017539 goto erret;
17540 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017541
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017542 fp = find_func(name);
17543 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017545 if (!eap->forceit)
17546 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017547 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017548 goto erret;
17549 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017550 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017551 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017552 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017553 name);
17554 goto erret;
17555 }
17556 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017557 ga_clear_strings(&(fp->uf_args));
17558 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017559 vim_free(name);
17560 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 }
17563 else
17564 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017565 char numbuf[20];
17566
17567 fp = NULL;
17568 if (fudi.fd_newkey == NULL && !eap->forceit)
17569 {
17570 EMSG(_(e_funcdict));
17571 goto erret;
17572 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017573 if (fudi.fd_di == NULL)
17574 {
17575 /* Can't add a function to a locked dictionary */
17576 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17577 goto erret;
17578 }
17579 /* Can't change an existing function if it is locked */
17580 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17581 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017582
17583 /* Give the function a sequential number. Can only be used with a
17584 * Funcref! */
17585 vim_free(name);
17586 sprintf(numbuf, "%d", ++func_nr);
17587 name = vim_strsave((char_u *)numbuf);
17588 if (name == NULL)
17589 goto erret;
17590 }
17591
17592 if (fp == NULL)
17593 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017594 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017595 {
17596 int slen, plen;
17597 char_u *scriptname;
17598
17599 /* Check that the autoload name matches the script name. */
17600 j = FAIL;
17601 if (sourcing_name != NULL)
17602 {
17603 scriptname = autoload_name(name);
17604 if (scriptname != NULL)
17605 {
17606 p = vim_strchr(scriptname, '/');
17607 plen = STRLEN(p);
17608 slen = STRLEN(sourcing_name);
17609 if (slen > plen && fnamecmp(p,
17610 sourcing_name + slen - plen) == 0)
17611 j = OK;
17612 vim_free(scriptname);
17613 }
17614 }
17615 if (j == FAIL)
17616 {
17617 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17618 goto erret;
17619 }
17620 }
17621
17622 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 if (fp == NULL)
17624 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017625
17626 if (fudi.fd_dict != NULL)
17627 {
17628 if (fudi.fd_di == NULL)
17629 {
17630 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017631 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017632 if (fudi.fd_di == NULL)
17633 {
17634 vim_free(fp);
17635 goto erret;
17636 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017637 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17638 {
17639 vim_free(fudi.fd_di);
17640 goto erret;
17641 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017642 }
17643 else
17644 /* overwrite existing dict entry */
17645 clear_tv(&fudi.fd_di->di_tv);
17646 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017647 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017648 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017649 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017650 }
17651
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017653 STRCPY(fp->uf_name, name);
17654 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017656 fp->uf_args = newargs;
17657 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017658#ifdef FEAT_PROFILE
17659 fp->uf_tml_count = NULL;
17660 fp->uf_tml_total = NULL;
17661 fp->uf_tml_self = NULL;
17662 fp->uf_profiling = FALSE;
17663 if (prof_def_func())
17664 func_do_profile(fp);
17665#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017666 fp->uf_varargs = varargs;
17667 fp->uf_flags = flags;
17668 fp->uf_calls = 0;
17669 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017670 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671
17672erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673 ga_clear_strings(&newargs);
17674 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017675ret_free:
17676 vim_free(skip_until);
17677 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680}
17681
17682/*
17683 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017684 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017686 * flags:
17687 * TFN_INT: internal function name OK
17688 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017689 * Advances "pp" to just after the function name (if no error).
17690 */
17691 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017692trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693 char_u **pp;
17694 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017695 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017696 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017698 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 char_u *start;
17700 char_u *end;
17701 int lead;
17702 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017704 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017705
17706 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017707 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017709
17710 /* Check for hard coded <SNR>: already translated function ID (from a user
17711 * command). */
17712 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17713 && (*pp)[2] == (int)KE_SNR)
17714 {
17715 *pp += 3;
17716 len = get_id_len(pp) + 3;
17717 return vim_strnsave(start, len);
17718 }
17719
17720 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17721 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017723 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017725
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017726 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17727 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017728 if (end == start)
17729 {
17730 if (!skip)
17731 EMSG(_("E129: Function name required"));
17732 goto theend;
17733 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017734 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017735 {
17736 /*
17737 * Report an invalid expression in braces, unless the expression
17738 * evaluation has been cancelled due to an aborting error, an
17739 * interrupt, or an exception.
17740 */
17741 if (!aborting())
17742 {
17743 if (end != NULL)
17744 EMSG2(_(e_invarg2), start);
17745 }
17746 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017747 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017748 goto theend;
17749 }
17750
17751 if (lv.ll_tv != NULL)
17752 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017753 if (fdp != NULL)
17754 {
17755 fdp->fd_dict = lv.ll_dict;
17756 fdp->fd_newkey = lv.ll_newkey;
17757 lv.ll_newkey = NULL;
17758 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017759 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017760 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17761 {
17762 name = vim_strsave(lv.ll_tv->vval.v_string);
17763 *pp = end;
17764 }
17765 else
17766 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017767 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17768 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017769 EMSG(_(e_funcref));
17770 else
17771 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017772 name = NULL;
17773 }
17774 goto theend;
17775 }
17776
17777 if (lv.ll_name == NULL)
17778 {
17779 /* Error found, but continue after the function name. */
17780 *pp = end;
17781 goto theend;
17782 }
17783
17784 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000017785 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017786 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000017787 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
17788 && STRNCMP(lv.ll_name, "s:", 2) == 0)
17789 {
17790 /* When there was "s:" already or the name expanded to get a
17791 * leading "s:" then remove it. */
17792 lv.ll_name += 2;
17793 len -= 2;
17794 lead = 2;
17795 }
17796 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017797 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017798 {
17799 if (lead == 2) /* skip over "s:" */
17800 lv.ll_name += 2;
17801 len = (int)(end - lv.ll_name);
17802 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017803
17804 /*
17805 * Copy the function name to allocated memory.
17806 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17807 * Accept <SNR>123_name() outside a script.
17808 */
17809 if (skip)
17810 lead = 0; /* do nothing */
17811 else if (lead > 0)
17812 {
17813 lead = 3;
17814 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17815 {
17816 if (current_SID <= 0)
17817 {
17818 EMSG(_(e_usingsid));
17819 goto theend;
17820 }
17821 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17822 lead += (int)STRLEN(sid_buf);
17823 }
17824 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017825 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017826 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017827 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017828 goto theend;
17829 }
17830 name = alloc((unsigned)(len + lead + 1));
17831 if (name != NULL)
17832 {
17833 if (lead > 0)
17834 {
17835 name[0] = K_SPECIAL;
17836 name[1] = KS_EXTRA;
17837 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017838 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017839 STRCPY(name + 3, sid_buf);
17840 }
17841 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17842 name[len + lead] = NUL;
17843 }
17844 *pp = end;
17845
17846theend:
17847 clear_lval(&lv);
17848 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849}
17850
17851/*
17852 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17853 * Return 2 if "p" starts with "s:".
17854 * Return 0 otherwise.
17855 */
17856 static int
17857eval_fname_script(p)
17858 char_u *p;
17859{
17860 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17861 || STRNICMP(p + 1, "SNR>", 4) == 0))
17862 return 5;
17863 if (p[0] == 's' && p[1] == ':')
17864 return 2;
17865 return 0;
17866}
17867
17868/*
17869 * Return TRUE if "p" starts with "<SID>" or "s:".
17870 * Only works if eval_fname_script() returned non-zero for "p"!
17871 */
17872 static int
17873eval_fname_sid(p)
17874 char_u *p;
17875{
17876 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17877}
17878
17879/*
17880 * List the head of the function: "name(arg1, arg2)".
17881 */
17882 static void
17883list_func_head(fp, indent)
17884 ufunc_T *fp;
17885 int indent;
17886{
17887 int j;
17888
17889 msg_start();
17890 if (indent)
17891 MSG_PUTS(" ");
17892 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017893 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894 {
17895 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017896 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 }
17898 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017899 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017900 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017901 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 {
17903 if (j)
17904 MSG_PUTS(", ");
17905 msg_puts(FUNCARG(fp, j));
17906 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017907 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908 {
17909 if (j)
17910 MSG_PUTS(", ");
17911 MSG_PUTS("...");
17912 }
17913 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000017914 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000017915 if (p_verbose > 0)
17916 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917}
17918
17919/*
17920 * Find a function by name, return pointer to it in ufuncs.
17921 * Return NULL for unknown function.
17922 */
17923 static ufunc_T *
17924find_func(name)
17925 char_u *name;
17926{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017927 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017929 hi = hash_find(&func_hashtab, name);
17930 if (!HASHITEM_EMPTY(hi))
17931 return HI2UF(hi);
17932 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933}
17934
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017935#if defined(EXITFREE) || defined(PROTO)
17936 void
17937free_all_functions()
17938{
17939 hashitem_T *hi;
17940
17941 /* Need to start all over every time, because func_free() may change the
17942 * hash table. */
17943 while (func_hashtab.ht_used > 0)
17944 for (hi = func_hashtab.ht_array; ; ++hi)
17945 if (!HASHITEM_EMPTY(hi))
17946 {
17947 func_free(HI2UF(hi));
17948 break;
17949 }
17950}
17951#endif
17952
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017953/*
17954 * Return TRUE if a function "name" exists.
17955 */
17956 static int
17957function_exists(name)
17958 char_u *name;
17959{
17960 char_u *p = name;
17961 int n = FALSE;
17962
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017963 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017964 if (p != NULL)
17965 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017966 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017967 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017968 else
17969 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017970 vim_free(p);
17971 }
17972 return n;
17973}
17974
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017975/*
17976 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017977 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017978 */
17979 static int
17980builtin_function(name)
17981 char_u *name;
17982{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017983 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17984 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017985}
17986
Bram Moolenaar05159a02005-02-26 23:04:13 +000017987#if defined(FEAT_PROFILE) || defined(PROTO)
17988/*
17989 * Start profiling function "fp".
17990 */
17991 static void
17992func_do_profile(fp)
17993 ufunc_T *fp;
17994{
17995 fp->uf_tm_count = 0;
17996 profile_zero(&fp->uf_tm_self);
17997 profile_zero(&fp->uf_tm_total);
17998 if (fp->uf_tml_count == NULL)
17999 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18000 (sizeof(int) * fp->uf_lines.ga_len));
18001 if (fp->uf_tml_total == NULL)
18002 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18003 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18004 if (fp->uf_tml_self == NULL)
18005 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18006 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18007 fp->uf_tml_idx = -1;
18008 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18009 || fp->uf_tml_self == NULL)
18010 return; /* out of memory */
18011
18012 fp->uf_profiling = TRUE;
18013}
18014
18015/*
18016 * Dump the profiling results for all functions in file "fd".
18017 */
18018 void
18019func_dump_profile(fd)
18020 FILE *fd;
18021{
18022 hashitem_T *hi;
18023 int todo;
18024 ufunc_T *fp;
18025 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018026 ufunc_T **sorttab;
18027 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018028
18029 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018030 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18031
Bram Moolenaar05159a02005-02-26 23:04:13 +000018032 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18033 {
18034 if (!HASHITEM_EMPTY(hi))
18035 {
18036 --todo;
18037 fp = HI2UF(hi);
18038 if (fp->uf_profiling)
18039 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018040 if (sorttab != NULL)
18041 sorttab[st_len++] = fp;
18042
Bram Moolenaar05159a02005-02-26 23:04:13 +000018043 if (fp->uf_name[0] == K_SPECIAL)
18044 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18045 else
18046 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18047 if (fp->uf_tm_count == 1)
18048 fprintf(fd, "Called 1 time\n");
18049 else
18050 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18051 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18052 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18053 fprintf(fd, "\n");
18054 fprintf(fd, "count total (s) self (s)\n");
18055
18056 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18057 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018058 prof_func_line(fd, fp->uf_tml_count[i],
18059 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018060 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18061 }
18062 fprintf(fd, "\n");
18063 }
18064 }
18065 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018066
18067 if (sorttab != NULL && st_len > 0)
18068 {
18069 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18070 prof_total_cmp);
18071 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18072 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18073 prof_self_cmp);
18074 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18075 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018076}
Bram Moolenaar73830342005-02-28 22:48:19 +000018077
18078 static void
18079prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18080 FILE *fd;
18081 ufunc_T **sorttab;
18082 int st_len;
18083 char *title;
18084 int prefer_self; /* when equal print only self time */
18085{
18086 int i;
18087 ufunc_T *fp;
18088
18089 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18090 fprintf(fd, "count total (s) self (s) function\n");
18091 for (i = 0; i < 20 && i < st_len; ++i)
18092 {
18093 fp = sorttab[i];
18094 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18095 prefer_self);
18096 if (fp->uf_name[0] == K_SPECIAL)
18097 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18098 else
18099 fprintf(fd, " %s()\n", fp->uf_name);
18100 }
18101 fprintf(fd, "\n");
18102}
18103
18104/*
18105 * Print the count and times for one function or function line.
18106 */
18107 static void
18108prof_func_line(fd, count, total, self, prefer_self)
18109 FILE *fd;
18110 int count;
18111 proftime_T *total;
18112 proftime_T *self;
18113 int prefer_self; /* when equal print only self time */
18114{
18115 if (count > 0)
18116 {
18117 fprintf(fd, "%5d ", count);
18118 if (prefer_self && profile_equal(total, self))
18119 fprintf(fd, " ");
18120 else
18121 fprintf(fd, "%s ", profile_msg(total));
18122 if (!prefer_self && profile_equal(total, self))
18123 fprintf(fd, " ");
18124 else
18125 fprintf(fd, "%s ", profile_msg(self));
18126 }
18127 else
18128 fprintf(fd, " ");
18129}
18130
18131/*
18132 * Compare function for total time sorting.
18133 */
18134 static int
18135#ifdef __BORLANDC__
18136_RTLENTRYF
18137#endif
18138prof_total_cmp(s1, s2)
18139 const void *s1;
18140 const void *s2;
18141{
18142 ufunc_T *p1, *p2;
18143
18144 p1 = *(ufunc_T **)s1;
18145 p2 = *(ufunc_T **)s2;
18146 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18147}
18148
18149/*
18150 * Compare function for self time sorting.
18151 */
18152 static int
18153#ifdef __BORLANDC__
18154_RTLENTRYF
18155#endif
18156prof_self_cmp(s1, s2)
18157 const void *s1;
18158 const void *s2;
18159{
18160 ufunc_T *p1, *p2;
18161
18162 p1 = *(ufunc_T **)s1;
18163 p2 = *(ufunc_T **)s2;
18164 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18165}
18166
Bram Moolenaar05159a02005-02-26 23:04:13 +000018167#endif
18168
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018169/* The names of packages that once were loaded is remembered. */
18170static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18171
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018172/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018173 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018174 * Return TRUE if a package was loaded.
18175 */
18176 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018177script_autoload(name, reload)
18178 char_u *name;
18179 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018180{
18181 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018182 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018183 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018184 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018185
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018186 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018187 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018188 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018189 return FALSE;
18190
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018191 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018192
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018193 /* Find the name in the list of previously loaded package names. Skip
18194 * "autoload/", it's always the same. */
18195 for (i = 0; i < ga_loaded.ga_len; ++i)
18196 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18197 break;
18198 if (!reload && i < ga_loaded.ga_len)
18199 ret = FALSE; /* was loaded already */
18200 else
18201 {
18202 /* Remember the name if it wasn't loaded already. */
18203 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18204 {
18205 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18206 tofree = NULL;
18207 }
18208
18209 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018210 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018211 ret = TRUE;
18212 }
18213
18214 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018215 return ret;
18216}
18217
18218/*
18219 * Return the autoload script name for a function or variable name.
18220 * Returns NULL when out of memory.
18221 */
18222 static char_u *
18223autoload_name(name)
18224 char_u *name;
18225{
18226 char_u *p;
18227 char_u *scriptname;
18228
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018229 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018230 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18231 if (scriptname == NULL)
18232 return FALSE;
18233 STRCPY(scriptname, "autoload/");
18234 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018235 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018236 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018237 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018238 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018239 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018240}
18241
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18243
18244/*
18245 * Function given to ExpandGeneric() to obtain the list of user defined
18246 * function names.
18247 */
18248 char_u *
18249get_user_func_name(xp, idx)
18250 expand_T *xp;
18251 int idx;
18252{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018253 static long_u done;
18254 static hashitem_T *hi;
18255 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256
18257 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018259 done = 0;
18260 hi = func_hashtab.ht_array;
18261 }
18262 if (done < func_hashtab.ht_used)
18263 {
18264 if (done++ > 0)
18265 ++hi;
18266 while (HASHITEM_EMPTY(hi))
18267 ++hi;
18268 fp = HI2UF(hi);
18269
18270 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18271 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272
18273 cat_func_name(IObuff, fp);
18274 if (xp->xp_context != EXPAND_USER_FUNC)
18275 {
18276 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018277 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278 STRCAT(IObuff, ")");
18279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280 return IObuff;
18281 }
18282 return NULL;
18283}
18284
18285#endif /* FEAT_CMDL_COMPL */
18286
18287/*
18288 * Copy the function name of "fp" to buffer "buf".
18289 * "buf" must be able to hold the function name plus three bytes.
18290 * Takes care of script-local function names.
18291 */
18292 static void
18293cat_func_name(buf, fp)
18294 char_u *buf;
18295 ufunc_T *fp;
18296{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018297 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298 {
18299 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018300 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 }
18302 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018303 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304}
18305
18306/*
18307 * ":delfunction {name}"
18308 */
18309 void
18310ex_delfunction(eap)
18311 exarg_T *eap;
18312{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018313 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 char_u *p;
18315 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018316 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317
18318 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018319 name = trans_function_name(&p, eap->skip, 0, &fudi);
18320 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018322 {
18323 if (fudi.fd_dict != NULL && !eap->skip)
18324 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 if (!ends_excmd(*skipwhite(p)))
18328 {
18329 vim_free(name);
18330 EMSG(_(e_trailing));
18331 return;
18332 }
18333 eap->nextcmd = check_nextcmd(p);
18334 if (eap->nextcmd != NULL)
18335 *p = NUL;
18336
18337 if (!eap->skip)
18338 fp = find_func(name);
18339 vim_free(name);
18340
18341 if (!eap->skip)
18342 {
18343 if (fp == NULL)
18344 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018345 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346 return;
18347 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018348 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349 {
18350 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18351 return;
18352 }
18353
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018354 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018356 /* Delete the dict item that refers to the function, it will
18357 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018358 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018360 else
18361 func_free(fp);
18362 }
18363}
18364
18365/*
18366 * Free a function and remove it from the list of functions.
18367 */
18368 static void
18369func_free(fp)
18370 ufunc_T *fp;
18371{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018372 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018373
18374 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018375 ga_clear_strings(&(fp->uf_args));
18376 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018377#ifdef FEAT_PROFILE
18378 vim_free(fp->uf_tml_count);
18379 vim_free(fp->uf_tml_total);
18380 vim_free(fp->uf_tml_self);
18381#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018382
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018383 /* remove the function from the function hashtable */
18384 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18385 if (HASHITEM_EMPTY(hi))
18386 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018387 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018388 hash_remove(&func_hashtab, hi);
18389
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018390 vim_free(fp);
18391}
18392
18393/*
18394 * Unreference a Function: decrement the reference count and free it when it
18395 * becomes zero. Only for numbered functions.
18396 */
18397 static void
18398func_unref(name)
18399 char_u *name;
18400{
18401 ufunc_T *fp;
18402
18403 if (name != NULL && isdigit(*name))
18404 {
18405 fp = find_func(name);
18406 if (fp == NULL)
18407 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018408 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018409 {
18410 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018411 * when "uf_calls" becomes zero. */
18412 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018413 func_free(fp);
18414 }
18415 }
18416}
18417
18418/*
18419 * Count a reference to a Function.
18420 */
18421 static void
18422func_ref(name)
18423 char_u *name;
18424{
18425 ufunc_T *fp;
18426
18427 if (name != NULL && isdigit(*name))
18428 {
18429 fp = find_func(name);
18430 if (fp == NULL)
18431 EMSG2(_(e_intern2), "func_ref()");
18432 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018433 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434 }
18435}
18436
18437/*
18438 * Call a user function.
18439 */
18440 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018441call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442 ufunc_T *fp; /* pointer to function */
18443 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018444 typval_T *argvars; /* arguments */
18445 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446 linenr_T firstline; /* first line of range */
18447 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018448 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449{
Bram Moolenaar33570922005-01-25 22:26:29 +000018450 char_u *save_sourcing_name;
18451 linenr_T save_sourcing_lnum;
18452 scid_T save_current_SID;
18453 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018454 int save_did_emsg;
18455 static int depth = 0;
18456 dictitem_T *v;
18457 int fixvar_idx = 0; /* index in fixvar[] */
18458 int i;
18459 int ai;
18460 char_u numbuf[NUMBUFLEN];
18461 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018462#ifdef FEAT_PROFILE
18463 proftime_T wait_start;
18464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465
18466 /* If depth of calling is getting too high, don't execute the function */
18467 if (depth >= p_mfd)
18468 {
18469 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018470 rettv->v_type = VAR_NUMBER;
18471 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472 return;
18473 }
18474 ++depth;
18475
18476 line_breakcheck(); /* check for CTRL-C hit */
18477
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018478 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018479 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018481 fc.rettv = rettv;
18482 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 fc.linenr = 0;
18484 fc.returned = FALSE;
18485 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018487 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488 fc.dbg_tick = debug_tick;
18489
Bram Moolenaar33570922005-01-25 22:26:29 +000018490 /*
18491 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18492 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18493 * each argument variable and saves a lot of time.
18494 */
18495 /*
18496 * Init l: variables.
18497 */
18498 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018499 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018500 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018501 /* Set l:self to "selfdict". */
18502 v = &fc.fixvar[fixvar_idx++].var;
18503 STRCPY(v->di_key, "self");
18504 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18505 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18506 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018507 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018508 v->di_tv.vval.v_dict = selfdict;
18509 ++selfdict->dv_refcount;
18510 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018511
Bram Moolenaar33570922005-01-25 22:26:29 +000018512 /*
18513 * Init a: variables.
18514 * Set a:0 to "argcount".
18515 * Set a:000 to a list with room for the "..." arguments.
18516 */
18517 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18518 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018519 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018520 v = &fc.fixvar[fixvar_idx++].var;
18521 STRCPY(v->di_key, "000");
18522 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18523 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18524 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018525 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018526 v->di_tv.vval.v_list = &fc.l_varlist;
18527 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18528 fc.l_varlist.lv_refcount = 99999;
18529
18530 /*
18531 * Set a:firstline to "firstline" and a:lastline to "lastline".
18532 * Set a:name to named arguments.
18533 * Set a:N to the "..." arguments.
18534 */
18535 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18536 (varnumber_T)firstline);
18537 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18538 (varnumber_T)lastline);
18539 for (i = 0; i < argcount; ++i)
18540 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018541 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018542 if (ai < 0)
18543 /* named argument a:name */
18544 name = FUNCARG(fp, i);
18545 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018546 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018547 /* "..." argument a:1, a:2, etc. */
18548 sprintf((char *)numbuf, "%d", ai + 1);
18549 name = numbuf;
18550 }
18551 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18552 {
18553 v = &fc.fixvar[fixvar_idx++].var;
18554 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18555 }
18556 else
18557 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018558 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18559 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018560 if (v == NULL)
18561 break;
18562 v->di_flags = DI_FLAGS_RO;
18563 }
18564 STRCPY(v->di_key, name);
18565 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18566
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018567 /* Note: the values are copied directly to avoid alloc/free.
18568 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018569 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018570 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018571
18572 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18573 {
18574 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18575 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018576 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018577 }
18578 }
18579
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 /* Don't redraw while executing the function. */
18581 ++RedrawingDisabled;
18582 save_sourcing_name = sourcing_name;
18583 save_sourcing_lnum = sourcing_lnum;
18584 sourcing_lnum = 1;
18585 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018586 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 if (sourcing_name != NULL)
18588 {
18589 if (save_sourcing_name != NULL
18590 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18591 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18592 else
18593 STRCPY(sourcing_name, "function ");
18594 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18595
18596 if (p_verbose >= 12)
18597 {
18598 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018599 verbose_enter_scroll();
18600
Bram Moolenaar555b2802005-05-19 21:08:39 +000018601 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 if (p_verbose >= 14)
18603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018605 char_u numbuf[NUMBUFLEN];
18606 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607
18608 msg_puts((char_u *)"(");
18609 for (i = 0; i < argcount; ++i)
18610 {
18611 if (i > 0)
18612 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018613 if (argvars[i].v_type == VAR_NUMBER)
18614 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 else
18616 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018617 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018618 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018620 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 }
18622 }
18623 msg_puts((char_u *)")");
18624 }
18625 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018626
18627 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 --no_wait_return;
18629 }
18630 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018631#ifdef FEAT_PROFILE
18632 if (do_profiling)
18633 {
18634 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18635 func_do_profile(fp);
18636 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018637 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018638 {
18639 ++fp->uf_tm_count;
18640 profile_start(&fp->uf_tm_start);
18641 profile_zero(&fp->uf_tm_children);
18642 }
18643 script_prof_save(&wait_start);
18644 }
18645#endif
18646
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018648 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649 save_did_emsg = did_emsg;
18650 did_emsg = FALSE;
18651
18652 /* call do_cmdline() to execute the lines */
18653 do_cmdline(NULL, get_func_line, (void *)&fc,
18654 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18655
18656 --RedrawingDisabled;
18657
18658 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018659 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018661 clear_tv(rettv);
18662 rettv->v_type = VAR_NUMBER;
18663 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 }
18665
Bram Moolenaar05159a02005-02-26 23:04:13 +000018666#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018667 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018668 {
18669 profile_end(&fp->uf_tm_start);
18670 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18671 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18672 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18673 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018674 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018675 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018676 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18677 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018678 }
18679 }
18680#endif
18681
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682 /* when being verbose, mention the return value */
18683 if (p_verbose >= 12)
18684 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018686 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018689 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018690 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018691 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18692 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018693 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018695 char_u buf[MSG_BUF_LEN];
18696 char_u numbuf[NUMBUFLEN];
18697 char_u *tofree;
18698
Bram Moolenaar555b2802005-05-19 21:08:39 +000018699 /* The value may be very long. Skip the middle part, so that we
18700 * have some idea how it starts and ends. smsg() would always
18701 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018702 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018703 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018704 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018705 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706 }
18707 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018708
18709 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018710 --no_wait_return;
18711 }
18712
18713 vim_free(sourcing_name);
18714 sourcing_name = save_sourcing_name;
18715 sourcing_lnum = save_sourcing_lnum;
18716 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018717#ifdef FEAT_PROFILE
18718 if (do_profiling)
18719 script_prof_restore(&wait_start);
18720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721
18722 if (p_verbose >= 12 && sourcing_name != NULL)
18723 {
18724 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018725 verbose_enter_scroll();
18726
Bram Moolenaar555b2802005-05-19 21:08:39 +000018727 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018729
18730 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 --no_wait_return;
18732 }
18733
18734 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018735 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736
Bram Moolenaar33570922005-01-25 22:26:29 +000018737 /* The a: variables typevals were not alloced, only free the allocated
18738 * variables. */
18739 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18740
18741 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 --depth;
18743}
18744
18745/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018746 * Add a number variable "name" to dict "dp" with value "nr".
18747 */
18748 static void
18749add_nr_var(dp, v, name, nr)
18750 dict_T *dp;
18751 dictitem_T *v;
18752 char *name;
18753 varnumber_T nr;
18754{
18755 STRCPY(v->di_key, name);
18756 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18757 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18758 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018759 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018760 v->di_tv.vval.v_number = nr;
18761}
18762
18763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764 * ":return [expr]"
18765 */
18766 void
18767ex_return(eap)
18768 exarg_T *eap;
18769{
18770 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018771 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 int returning = FALSE;
18773
18774 if (current_funccal == NULL)
18775 {
18776 EMSG(_("E133: :return not inside a function"));
18777 return;
18778 }
18779
18780 if (eap->skip)
18781 ++emsg_skip;
18782
18783 eap->nextcmd = NULL;
18784 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018785 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 {
18787 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018788 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018790 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 }
18792 /* It's safer to return also on error. */
18793 else if (!eap->skip)
18794 {
18795 /*
18796 * Return unless the expression evaluation has been cancelled due to an
18797 * aborting error, an interrupt, or an exception.
18798 */
18799 if (!aborting())
18800 returning = do_return(eap, FALSE, TRUE, NULL);
18801 }
18802
18803 /* When skipping or the return gets pending, advance to the next command
18804 * in this line (!returning). Otherwise, ignore the rest of the line.
18805 * Following lines will be ignored by get_func_line(). */
18806 if (returning)
18807 eap->nextcmd = NULL;
18808 else if (eap->nextcmd == NULL) /* no argument */
18809 eap->nextcmd = check_nextcmd(arg);
18810
18811 if (eap->skip)
18812 --emsg_skip;
18813}
18814
18815/*
18816 * Return from a function. Possibly makes the return pending. Also called
18817 * for a pending return at the ":endtry" or after returning from an extra
18818 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018819 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018820 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 * FALSE when the return gets pending.
18822 */
18823 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018824do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 exarg_T *eap;
18826 int reanimate;
18827 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018828 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829{
18830 int idx;
18831 struct condstack *cstack = eap->cstack;
18832
18833 if (reanimate)
18834 /* Undo the return. */
18835 current_funccal->returned = FALSE;
18836
18837 /*
18838 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18839 * not in its finally clause (which then is to be executed next) is found.
18840 * In this case, make the ":return" pending for execution at the ":endtry".
18841 * Otherwise, return normally.
18842 */
18843 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18844 if (idx >= 0)
18845 {
18846 cstack->cs_pending[idx] = CSTP_RETURN;
18847
18848 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018849 /* A pending return again gets pending. "rettv" points to an
18850 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018852 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853 else
18854 {
18855 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018856 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018858 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018860 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861 {
18862 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018863 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018864 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 else
18866 EMSG(_(e_outofmem));
18867 }
18868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018869 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870
18871 if (reanimate)
18872 {
18873 /* The pending return value could be overwritten by a ":return"
18874 * without argument in a finally clause; reset the default
18875 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018876 current_funccal->rettv->v_type = VAR_NUMBER;
18877 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 }
18879 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018880 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 }
18882 else
18883 {
18884 current_funccal->returned = TRUE;
18885
18886 /* If the return is carried out now, store the return value. For
18887 * a return immediately after reanimation, the value is already
18888 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018889 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018891 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018892 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018894 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895 }
18896 }
18897
18898 return idx < 0;
18899}
18900
18901/*
18902 * Free the variable with a pending return value.
18903 */
18904 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018905discard_pending_return(rettv)
18906 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907{
Bram Moolenaar33570922005-01-25 22:26:29 +000018908 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909}
18910
18911/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018912 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913 * is an allocated string. Used by report_pending() for verbose messages.
18914 */
18915 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018916get_return_cmd(rettv)
18917 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018919 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018920 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018921 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018923 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018924 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018925 if (s == NULL)
18926 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018927
18928 STRCPY(IObuff, ":return ");
18929 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18930 if (STRLEN(s) + 8 >= IOSIZE)
18931 STRCPY(IObuff + IOSIZE - 4, "...");
18932 vim_free(tofree);
18933 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934}
18935
18936/*
18937 * Get next function line.
18938 * Called by do_cmdline() to get the next line.
18939 * Returns allocated string, or NULL for end of function.
18940 */
18941/* ARGSUSED */
18942 char_u *
18943get_func_line(c, cookie, indent)
18944 int c; /* not used */
18945 void *cookie;
18946 int indent; /* not used */
18947{
Bram Moolenaar33570922005-01-25 22:26:29 +000018948 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018949 ufunc_T *fp = fcp->func;
18950 char_u *retval;
18951 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952
18953 /* If breakpoints have been added/deleted need to check for it. */
18954 if (fcp->dbg_tick != debug_tick)
18955 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018956 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957 sourcing_lnum);
18958 fcp->dbg_tick = debug_tick;
18959 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018960#ifdef FEAT_PROFILE
18961 if (do_profiling)
18962 func_line_end(cookie);
18963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964
Bram Moolenaar05159a02005-02-26 23:04:13 +000018965 gap = &fp->uf_lines;
18966 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 retval = NULL;
18968 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18969 retval = NULL;
18970 else
18971 {
18972 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18973 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018974#ifdef FEAT_PROFILE
18975 if (do_profiling)
18976 func_line_start(cookie);
18977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978 }
18979
18980 /* Did we encounter a breakpoint? */
18981 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18982 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018983 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018985 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 sourcing_lnum);
18987 fcp->dbg_tick = debug_tick;
18988 }
18989
18990 return retval;
18991}
18992
Bram Moolenaar05159a02005-02-26 23:04:13 +000018993#if defined(FEAT_PROFILE) || defined(PROTO)
18994/*
18995 * Called when starting to read a function line.
18996 * "sourcing_lnum" must be correct!
18997 * When skipping lines it may not actually be executed, but we won't find out
18998 * until later and we need to store the time now.
18999 */
19000 void
19001func_line_start(cookie)
19002 void *cookie;
19003{
19004 funccall_T *fcp = (funccall_T *)cookie;
19005 ufunc_T *fp = fcp->func;
19006
19007 if (fp->uf_profiling && sourcing_lnum >= 1
19008 && sourcing_lnum <= fp->uf_lines.ga_len)
19009 {
19010 fp->uf_tml_idx = sourcing_lnum - 1;
19011 fp->uf_tml_execed = FALSE;
19012 profile_start(&fp->uf_tml_start);
19013 profile_zero(&fp->uf_tml_children);
19014 profile_get_wait(&fp->uf_tml_wait);
19015 }
19016}
19017
19018/*
19019 * Called when actually executing a function line.
19020 */
19021 void
19022func_line_exec(cookie)
19023 void *cookie;
19024{
19025 funccall_T *fcp = (funccall_T *)cookie;
19026 ufunc_T *fp = fcp->func;
19027
19028 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19029 fp->uf_tml_execed = TRUE;
19030}
19031
19032/*
19033 * Called when done with a function line.
19034 */
19035 void
19036func_line_end(cookie)
19037 void *cookie;
19038{
19039 funccall_T *fcp = (funccall_T *)cookie;
19040 ufunc_T *fp = fcp->func;
19041
19042 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19043 {
19044 if (fp->uf_tml_execed)
19045 {
19046 ++fp->uf_tml_count[fp->uf_tml_idx];
19047 profile_end(&fp->uf_tml_start);
19048 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
19049 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
19050 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
19051 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
19052 }
19053 fp->uf_tml_idx = -1;
19054 }
19055}
19056#endif
19057
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058/*
19059 * Return TRUE if the currently active function should be ended, because a
19060 * return was encountered or an error occured. Used inside a ":while".
19061 */
19062 int
19063func_has_ended(cookie)
19064 void *cookie;
19065{
Bram Moolenaar33570922005-01-25 22:26:29 +000019066 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067
19068 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19069 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019070 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 || fcp->returned);
19072}
19073
19074/*
19075 * return TRUE if cookie indicates a function which "abort"s on errors.
19076 */
19077 int
19078func_has_abort(cookie)
19079 void *cookie;
19080{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019081 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082}
19083
19084#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19085typedef enum
19086{
19087 VAR_FLAVOUR_DEFAULT,
19088 VAR_FLAVOUR_SESSION,
19089 VAR_FLAVOUR_VIMINFO
19090} var_flavour_T;
19091
19092static var_flavour_T var_flavour __ARGS((char_u *varname));
19093
19094 static var_flavour_T
19095var_flavour(varname)
19096 char_u *varname;
19097{
19098 char_u *p = varname;
19099
19100 if (ASCII_ISUPPER(*p))
19101 {
19102 while (*(++p))
19103 if (ASCII_ISLOWER(*p))
19104 return VAR_FLAVOUR_SESSION;
19105 return VAR_FLAVOUR_VIMINFO;
19106 }
19107 else
19108 return VAR_FLAVOUR_DEFAULT;
19109}
19110#endif
19111
19112#if defined(FEAT_VIMINFO) || defined(PROTO)
19113/*
19114 * Restore global vars that start with a capital from the viminfo file
19115 */
19116 int
19117read_viminfo_varlist(virp, writing)
19118 vir_T *virp;
19119 int writing;
19120{
19121 char_u *tab;
19122 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019123 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124
19125 if (!writing && (find_viminfo_parameter('!') != NULL))
19126 {
19127 tab = vim_strchr(virp->vir_line + 1, '\t');
19128 if (tab != NULL)
19129 {
19130 *tab++ = '\0'; /* isolate the variable name */
19131 if (*tab == 'S') /* string var */
19132 is_string = TRUE;
19133
19134 tab = vim_strchr(tab, '\t');
19135 if (tab != NULL)
19136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137 if (is_string)
19138 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019139 tv.v_type = VAR_STRING;
19140 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 }
19143 else
19144 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019145 tv.v_type = VAR_NUMBER;
19146 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019148 set_var(virp->vir_line + 1, &tv, FALSE);
19149 if (is_string)
19150 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151 }
19152 }
19153 }
19154
19155 return viminfo_readline(virp);
19156}
19157
19158/*
19159 * Write global vars that start with a capital to the viminfo file
19160 */
19161 void
19162write_viminfo_varlist(fp)
19163 FILE *fp;
19164{
Bram Moolenaar33570922005-01-25 22:26:29 +000019165 hashitem_T *hi;
19166 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019167 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019168 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019169 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019170 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019171 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172
19173 if (find_viminfo_parameter('!') == NULL)
19174 return;
19175
19176 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019177
Bram Moolenaar33570922005-01-25 22:26:29 +000019178 todo = globvarht.ht_used;
19179 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019181 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019183 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019184 this_var = HI2DI(hi);
19185 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019186 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019187 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019188 {
19189 case VAR_STRING: s = "STR"; break;
19190 case VAR_NUMBER: s = "NUM"; break;
19191 default: continue;
19192 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019193 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019194 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019195 if (p != NULL)
19196 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019197 vim_free(tofree);
19198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199 }
19200 }
19201}
19202#endif
19203
19204#if defined(FEAT_SESSION) || defined(PROTO)
19205 int
19206store_session_globals(fd)
19207 FILE *fd;
19208{
Bram Moolenaar33570922005-01-25 22:26:29 +000019209 hashitem_T *hi;
19210 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019211 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019212 char_u *p, *t;
19213
Bram Moolenaar33570922005-01-25 22:26:29 +000019214 todo = globvarht.ht_used;
19215 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019217 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019219 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019220 this_var = HI2DI(hi);
19221 if ((this_var->di_tv.v_type == VAR_NUMBER
19222 || this_var->di_tv.v_type == VAR_STRING)
19223 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019224 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019225 /* Escape special characters with a backslash. Turn a LF and
19226 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019227 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019228 (char_u *)"\\\"\n\r");
19229 if (p == NULL) /* out of memory */
19230 break;
19231 for (t = p; *t != NUL; ++t)
19232 if (*t == '\n')
19233 *t = 'n';
19234 else if (*t == '\r')
19235 *t = 'r';
19236 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019237 this_var->di_key,
19238 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19239 : ' ',
19240 p,
19241 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19242 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019243 || put_eol(fd) == FAIL)
19244 {
19245 vim_free(p);
19246 return FAIL;
19247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248 vim_free(p);
19249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250 }
19251 }
19252 return OK;
19253}
19254#endif
19255
Bram Moolenaar661b1822005-07-28 22:36:45 +000019256/*
19257 * Display script name where an item was last set.
19258 * Should only be invoked when 'verbose' is non-zero.
19259 */
19260 void
19261last_set_msg(scriptID)
19262 scid_T scriptID;
19263{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019264 char_u *p;
19265
Bram Moolenaar661b1822005-07-28 22:36:45 +000019266 if (scriptID != 0)
19267 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019268 p = home_replace_save(NULL, get_scriptname(scriptID));
19269 if (p != NULL)
19270 {
19271 verbose_enter();
19272 MSG_PUTS(_("\n\tLast set from "));
19273 MSG_PUTS(p);
19274 vim_free(p);
19275 verbose_leave();
19276 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019277 }
19278}
19279
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280#endif /* FEAT_EVAL */
19281
19282#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19283
19284
19285#ifdef WIN3264
19286/*
19287 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19288 */
19289static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19290static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19291static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19292
19293/*
19294 * Get the short pathname of a file.
19295 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19296 */
19297 static int
19298get_short_pathname(fnamep, bufp, fnamelen)
19299 char_u **fnamep;
19300 char_u **bufp;
19301 int *fnamelen;
19302{
19303 int l,len;
19304 char_u *newbuf;
19305
19306 len = *fnamelen;
19307
19308 l = GetShortPathName(*fnamep, *fnamep, len);
19309 if (l > len - 1)
19310 {
19311 /* If that doesn't work (not enough space), then save the string
19312 * and try again with a new buffer big enough
19313 */
19314 newbuf = vim_strnsave(*fnamep, l);
19315 if (newbuf == NULL)
19316 return 0;
19317
19318 vim_free(*bufp);
19319 *fnamep = *bufp = newbuf;
19320
19321 l = GetShortPathName(*fnamep,*fnamep,l+1);
19322
19323 /* Really should always succeed, as the buffer is big enough */
19324 }
19325
19326 *fnamelen = l;
19327 return 1;
19328}
19329
19330/*
19331 * Create a short path name. Returns the length of the buffer it needs.
19332 * Doesn't copy over the end of the buffer passed in.
19333 */
19334 static int
19335shortpath_for_invalid_fname(fname, bufp, fnamelen)
19336 char_u **fname;
19337 char_u **bufp;
19338 int *fnamelen;
19339{
19340 char_u *s, *p, *pbuf2, *pbuf3;
19341 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019342 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343
19344 /* Make a copy */
19345 len2 = *fnamelen;
19346 pbuf2 = vim_strnsave(*fname, len2);
19347 pbuf3 = NULL;
19348
19349 s = pbuf2 + len2 - 1; /* Find the end */
19350 slen = 1;
19351 plen = len2;
19352
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019353 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354 {
19355 --s;
19356 ++slen;
19357 --plen;
19358 }
19359
19360 do
19361 {
19362 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019363 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 {
19365 --s;
19366 ++slen;
19367 --plen;
19368 }
19369 if (s <= pbuf2)
19370 break;
19371
19372 /* Remeber the character that is about to be blatted */
19373 ch = *s;
19374 *s = 0; /* get_short_pathname requires a null-terminated string */
19375
19376 /* Try it in situ */
19377 p = pbuf2;
19378 if (!get_short_pathname(&p, &pbuf3, &plen))
19379 {
19380 vim_free(pbuf2);
19381 return -1;
19382 }
19383 *s = ch; /* Preserve the string */
19384 } while (plen == 0);
19385
19386 if (plen > 0)
19387 {
19388 /* Remeber the length of the new string. */
19389 *fnamelen = len = plen + slen;
19390 vim_free(*bufp);
19391 if (len > len2)
19392 {
19393 /* If there's not enough space in the currently allocated string,
19394 * then copy it to a buffer big enough.
19395 */
19396 *fname= *bufp = vim_strnsave(p, len);
19397 if (*fname == NULL)
19398 return -1;
19399 }
19400 else
19401 {
19402 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19403 *fname = *bufp = pbuf2;
19404 if (p != pbuf2)
19405 strncpy(*fname, p, plen);
19406 pbuf2 = NULL;
19407 }
19408 /* Concat the next bit */
19409 strncpy(*fname + plen, s, slen);
19410 (*fname)[len] = '\0';
19411 }
19412 vim_free(pbuf3);
19413 vim_free(pbuf2);
19414 return 0;
19415}
19416
19417/*
19418 * Get a pathname for a partial path.
19419 */
19420 static int
19421shortpath_for_partial(fnamep, bufp, fnamelen)
19422 char_u **fnamep;
19423 char_u **bufp;
19424 int *fnamelen;
19425{
19426 int sepcount, len, tflen;
19427 char_u *p;
19428 char_u *pbuf, *tfname;
19429 int hasTilde;
19430
19431 /* Count up the path seperators from the RHS.. so we know which part
19432 * of the path to return.
19433 */
19434 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019435 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 if (vim_ispathsep(*p))
19437 ++sepcount;
19438
19439 /* Need full path first (use expand_env() to remove a "~/") */
19440 hasTilde = (**fnamep == '~');
19441 if (hasTilde)
19442 pbuf = tfname = expand_env_save(*fnamep);
19443 else
19444 pbuf = tfname = FullName_save(*fnamep, FALSE);
19445
19446 len = tflen = STRLEN(tfname);
19447
19448 if (!get_short_pathname(&tfname, &pbuf, &len))
19449 return -1;
19450
19451 if (len == 0)
19452 {
19453 /* Don't have a valid filename, so shorten the rest of the
19454 * path if we can. This CAN give us invalid 8.3 filenames, but
19455 * there's not a lot of point in guessing what it might be.
19456 */
19457 len = tflen;
19458 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19459 return -1;
19460 }
19461
19462 /* Count the paths backward to find the beginning of the desired string. */
19463 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019464 {
19465#ifdef FEAT_MBYTE
19466 if (has_mbyte)
19467 p -= mb_head_off(tfname, p);
19468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 if (vim_ispathsep(*p))
19470 {
19471 if (sepcount == 0 || (hasTilde && sepcount == 1))
19472 break;
19473 else
19474 sepcount --;
19475 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 if (hasTilde)
19478 {
19479 --p;
19480 if (p >= tfname)
19481 *p = '~';
19482 else
19483 return -1;
19484 }
19485 else
19486 ++p;
19487
19488 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19489 vim_free(*bufp);
19490 *fnamelen = (int)STRLEN(p);
19491 *bufp = pbuf;
19492 *fnamep = p;
19493
19494 return 0;
19495}
19496#endif /* WIN3264 */
19497
19498/*
19499 * Adjust a filename, according to a string of modifiers.
19500 * *fnamep must be NUL terminated when called. When returning, the length is
19501 * determined by *fnamelen.
19502 * Returns valid flags.
19503 * When there is an error, *fnamep is set to NULL.
19504 */
19505 int
19506modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19507 char_u *src; /* string with modifiers */
19508 int *usedlen; /* characters after src that are used */
19509 char_u **fnamep; /* file name so far */
19510 char_u **bufp; /* buffer for allocated file name or NULL */
19511 int *fnamelen; /* length of fnamep */
19512{
19513 int valid = 0;
19514 char_u *tail;
19515 char_u *s, *p, *pbuf;
19516 char_u dirname[MAXPATHL];
19517 int c;
19518 int has_fullname = 0;
19519#ifdef WIN3264
19520 int has_shortname = 0;
19521#endif
19522
19523repeat:
19524 /* ":p" - full path/file_name */
19525 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19526 {
19527 has_fullname = 1;
19528
19529 valid |= VALID_PATH;
19530 *usedlen += 2;
19531
19532 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19533 if ((*fnamep)[0] == '~'
19534#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19535 && ((*fnamep)[1] == '/'
19536# ifdef BACKSLASH_IN_FILENAME
19537 || (*fnamep)[1] == '\\'
19538# endif
19539 || (*fnamep)[1] == NUL)
19540
19541#endif
19542 )
19543 {
19544 *fnamep = expand_env_save(*fnamep);
19545 vim_free(*bufp); /* free any allocated file name */
19546 *bufp = *fnamep;
19547 if (*fnamep == NULL)
19548 return -1;
19549 }
19550
19551 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019552 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553 {
19554 if (vim_ispathsep(*p)
19555 && p[1] == '.'
19556 && (p[2] == NUL
19557 || vim_ispathsep(p[2])
19558 || (p[2] == '.'
19559 && (p[3] == NUL || vim_ispathsep(p[3])))))
19560 break;
19561 }
19562
19563 /* FullName_save() is slow, don't use it when not needed. */
19564 if (*p != NUL || !vim_isAbsName(*fnamep))
19565 {
19566 *fnamep = FullName_save(*fnamep, *p != NUL);
19567 vim_free(*bufp); /* free any allocated file name */
19568 *bufp = *fnamep;
19569 if (*fnamep == NULL)
19570 return -1;
19571 }
19572
19573 /* Append a path separator to a directory. */
19574 if (mch_isdir(*fnamep))
19575 {
19576 /* Make room for one or two extra characters. */
19577 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19578 vim_free(*bufp); /* free any allocated file name */
19579 *bufp = *fnamep;
19580 if (*fnamep == NULL)
19581 return -1;
19582 add_pathsep(*fnamep);
19583 }
19584 }
19585
19586 /* ":." - path relative to the current directory */
19587 /* ":~" - path relative to the home directory */
19588 /* ":8" - shortname path - postponed till after */
19589 while (src[*usedlen] == ':'
19590 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19591 {
19592 *usedlen += 2;
19593 if (c == '8')
19594 {
19595#ifdef WIN3264
19596 has_shortname = 1; /* Postpone this. */
19597#endif
19598 continue;
19599 }
19600 pbuf = NULL;
19601 /* Need full path first (use expand_env() to remove a "~/") */
19602 if (!has_fullname)
19603 {
19604 if (c == '.' && **fnamep == '~')
19605 p = pbuf = expand_env_save(*fnamep);
19606 else
19607 p = pbuf = FullName_save(*fnamep, FALSE);
19608 }
19609 else
19610 p = *fnamep;
19611
19612 has_fullname = 0;
19613
19614 if (p != NULL)
19615 {
19616 if (c == '.')
19617 {
19618 mch_dirname(dirname, MAXPATHL);
19619 s = shorten_fname(p, dirname);
19620 if (s != NULL)
19621 {
19622 *fnamep = s;
19623 if (pbuf != NULL)
19624 {
19625 vim_free(*bufp); /* free any allocated file name */
19626 *bufp = pbuf;
19627 pbuf = NULL;
19628 }
19629 }
19630 }
19631 else
19632 {
19633 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19634 /* Only replace it when it starts with '~' */
19635 if (*dirname == '~')
19636 {
19637 s = vim_strsave(dirname);
19638 if (s != NULL)
19639 {
19640 *fnamep = s;
19641 vim_free(*bufp);
19642 *bufp = s;
19643 }
19644 }
19645 }
19646 vim_free(pbuf);
19647 }
19648 }
19649
19650 tail = gettail(*fnamep);
19651 *fnamelen = (int)STRLEN(*fnamep);
19652
19653 /* ":h" - head, remove "/file_name", can be repeated */
19654 /* Don't remove the first "/" or "c:\" */
19655 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19656 {
19657 valid |= VALID_HEAD;
19658 *usedlen += 2;
19659 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019660 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661 --tail;
19662 *fnamelen = (int)(tail - *fnamep);
19663#ifdef VMS
19664 if (*fnamelen > 0)
19665 *fnamelen += 1; /* the path separator is part of the path */
19666#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019667 while (tail > s && !after_pathsep(s, tail))
19668 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669 }
19670
19671 /* ":8" - shortname */
19672 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19673 {
19674 *usedlen += 2;
19675#ifdef WIN3264
19676 has_shortname = 1;
19677#endif
19678 }
19679
19680#ifdef WIN3264
19681 /* Check shortname after we have done 'heads' and before we do 'tails'
19682 */
19683 if (has_shortname)
19684 {
19685 pbuf = NULL;
19686 /* Copy the string if it is shortened by :h */
19687 if (*fnamelen < (int)STRLEN(*fnamep))
19688 {
19689 p = vim_strnsave(*fnamep, *fnamelen);
19690 if (p == 0)
19691 return -1;
19692 vim_free(*bufp);
19693 *bufp = *fnamep = p;
19694 }
19695
19696 /* Split into two implementations - makes it easier. First is where
19697 * there isn't a full name already, second is where there is.
19698 */
19699 if (!has_fullname && !vim_isAbsName(*fnamep))
19700 {
19701 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19702 return -1;
19703 }
19704 else
19705 {
19706 int l;
19707
19708 /* Simple case, already have the full-name
19709 * Nearly always shorter, so try first time. */
19710 l = *fnamelen;
19711 if (!get_short_pathname(fnamep, bufp, &l))
19712 return -1;
19713
19714 if (l == 0)
19715 {
19716 /* Couldn't find the filename.. search the paths.
19717 */
19718 l = *fnamelen;
19719 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19720 return -1;
19721 }
19722 *fnamelen = l;
19723 }
19724 }
19725#endif /* WIN3264 */
19726
19727 /* ":t" - tail, just the basename */
19728 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19729 {
19730 *usedlen += 2;
19731 *fnamelen -= (int)(tail - *fnamep);
19732 *fnamep = tail;
19733 }
19734
19735 /* ":e" - extension, can be repeated */
19736 /* ":r" - root, without extension, can be repeated */
19737 while (src[*usedlen] == ':'
19738 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19739 {
19740 /* find a '.' in the tail:
19741 * - for second :e: before the current fname
19742 * - otherwise: The last '.'
19743 */
19744 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19745 s = *fnamep - 2;
19746 else
19747 s = *fnamep + *fnamelen - 1;
19748 for ( ; s > tail; --s)
19749 if (s[0] == '.')
19750 break;
19751 if (src[*usedlen + 1] == 'e') /* :e */
19752 {
19753 if (s > tail)
19754 {
19755 *fnamelen += (int)(*fnamep - (s + 1));
19756 *fnamep = s + 1;
19757#ifdef VMS
19758 /* cut version from the extension */
19759 s = *fnamep + *fnamelen - 1;
19760 for ( ; s > *fnamep; --s)
19761 if (s[0] == ';')
19762 break;
19763 if (s > *fnamep)
19764 *fnamelen = s - *fnamep;
19765#endif
19766 }
19767 else if (*fnamep <= tail)
19768 *fnamelen = 0;
19769 }
19770 else /* :r */
19771 {
19772 if (s > tail) /* remove one extension */
19773 *fnamelen = (int)(s - *fnamep);
19774 }
19775 *usedlen += 2;
19776 }
19777
19778 /* ":s?pat?foo?" - substitute */
19779 /* ":gs?pat?foo?" - global substitute */
19780 if (src[*usedlen] == ':'
19781 && (src[*usedlen + 1] == 's'
19782 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19783 {
19784 char_u *str;
19785 char_u *pat;
19786 char_u *sub;
19787 int sep;
19788 char_u *flags;
19789 int didit = FALSE;
19790
19791 flags = (char_u *)"";
19792 s = src + *usedlen + 2;
19793 if (src[*usedlen + 1] == 'g')
19794 {
19795 flags = (char_u *)"g";
19796 ++s;
19797 }
19798
19799 sep = *s++;
19800 if (sep)
19801 {
19802 /* find end of pattern */
19803 p = vim_strchr(s, sep);
19804 if (p != NULL)
19805 {
19806 pat = vim_strnsave(s, (int)(p - s));
19807 if (pat != NULL)
19808 {
19809 s = p + 1;
19810 /* find end of substitution */
19811 p = vim_strchr(s, sep);
19812 if (p != NULL)
19813 {
19814 sub = vim_strnsave(s, (int)(p - s));
19815 str = vim_strnsave(*fnamep, *fnamelen);
19816 if (sub != NULL && str != NULL)
19817 {
19818 *usedlen = (int)(p + 1 - src);
19819 s = do_string_sub(str, pat, sub, flags);
19820 if (s != NULL)
19821 {
19822 *fnamep = s;
19823 *fnamelen = (int)STRLEN(s);
19824 vim_free(*bufp);
19825 *bufp = s;
19826 didit = TRUE;
19827 }
19828 }
19829 vim_free(sub);
19830 vim_free(str);
19831 }
19832 vim_free(pat);
19833 }
19834 }
19835 /* after using ":s", repeat all the modifiers */
19836 if (didit)
19837 goto repeat;
19838 }
19839 }
19840
19841 return valid;
19842}
19843
19844/*
19845 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19846 * "flags" can be "g" to do a global substitute.
19847 * Returns an allocated string, NULL for error.
19848 */
19849 char_u *
19850do_string_sub(str, pat, sub, flags)
19851 char_u *str;
19852 char_u *pat;
19853 char_u *sub;
19854 char_u *flags;
19855{
19856 int sublen;
19857 regmatch_T regmatch;
19858 int i;
19859 int do_all;
19860 char_u *tail;
19861 garray_T ga;
19862 char_u *ret;
19863 char_u *save_cpo;
19864
19865 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19866 save_cpo = p_cpo;
19867 p_cpo = (char_u *)"";
19868
19869 ga_init2(&ga, 1, 200);
19870
19871 do_all = (flags[0] == 'g');
19872
19873 regmatch.rm_ic = p_ic;
19874 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19875 if (regmatch.regprog != NULL)
19876 {
19877 tail = str;
19878 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19879 {
19880 /*
19881 * Get some space for a temporary buffer to do the substitution
19882 * into. It will contain:
19883 * - The text up to where the match is.
19884 * - The substituted text.
19885 * - The text after the match.
19886 */
19887 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19888 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19889 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19890 {
19891 ga_clear(&ga);
19892 break;
19893 }
19894
19895 /* copy the text up to where the match is */
19896 i = (int)(regmatch.startp[0] - tail);
19897 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19898 /* add the substituted text */
19899 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19900 + ga.ga_len + i, TRUE, TRUE, FALSE);
19901 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902 /* avoid getting stuck on a match with an empty string */
19903 if (tail == regmatch.endp[0])
19904 {
19905 if (*tail == NUL)
19906 break;
19907 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19908 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909 }
19910 else
19911 {
19912 tail = regmatch.endp[0];
19913 if (*tail == NUL)
19914 break;
19915 }
19916 if (!do_all)
19917 break;
19918 }
19919
19920 if (ga.ga_data != NULL)
19921 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19922
19923 vim_free(regmatch.regprog);
19924 }
19925
19926 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19927 ga_clear(&ga);
19928 p_cpo = save_cpo;
19929
19930 return ret;
19931}
19932
19933#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */