blob: cfcc9d4aeac42ef723af17291b0f0e38b1b744bb [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));
403static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000404static void list_free __ARGS((list_T *l));
405static listitem_T *listitem_alloc __ARGS((void));
406static void listitem_free __ARGS((listitem_T *item));
407static void listitem_remove __ARGS((list_T *l, listitem_T *item));
408static long list_len __ARGS((list_T *l));
409static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
410static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
411static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static listitem_T *list_find __ARGS((list_T *l, long n));
413static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static void list_append __ARGS((list_T *l, listitem_T *item));
415static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000416static int list_append_string __ARGS((list_T *l, char_u *str, int len));
417static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
419static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
420static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000421static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000423static char_u *list2string __ARGS((typval_T *tv, int copyID));
424static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000425static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
426static void set_ref_in_list __ARGS((list_T *l, int copyID));
427static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static void dict_unref __ARGS((dict_T *d));
429static void dict_free __ARGS((dict_T *d));
430static dictitem_T *dictitem_alloc __ARGS((char_u *key));
431static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
432static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
433static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int dict_add __ARGS((dict_T *d, dictitem_T *item));
436static long dict_len __ARGS((dict_T *d));
437static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
441static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static char_u *string_quote __ARGS((char_u *str, int function));
443static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
444static int find_internal_func __ARGS((char_u *name));
445static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
446static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
447static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000448static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449
450static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000469#if defined(FEAT_INS_EXPAND)
470static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
472#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000473static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
478static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000504static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000506static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000512static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000513static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000520static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000543static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000544static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000549static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000550static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000566static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000570#ifdef vim_mkdir
571static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
572#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000577static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000579static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000591static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000598static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000599static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000604static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000605static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
608#ifdef HAVE_STRFTIME
609static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
610#endif
611static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000623static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000624static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000626static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000641static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000642
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
644static int get_env_len __ARGS((char_u **arg));
645static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000646static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000647static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
648#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
649#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
650 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000651static 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 +0000652static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000653static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000654static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
655static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static typval_T *alloc_tv __ARGS((void));
657static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void init_tv __ARGS((typval_T *varp));
659static long get_tv_number __ARGS((typval_T *varp));
660static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000661static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static char_u *get_tv_string __ARGS((typval_T *varp));
663static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000664static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000666static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
668static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
669static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
670static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
671static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
672static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
673static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000674static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000676static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000677static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
678static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
679static int eval_fname_script __ARGS((char_u *p));
680static int eval_fname_sid __ARGS((char_u *p));
681static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static ufunc_T *find_func __ARGS((char_u *name));
683static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000684static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000685#ifdef FEAT_PROFILE
686static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000687static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
688static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
689static int
690# ifdef __BORLANDC__
691 _RTLENTRYF
692# endif
693 prof_total_cmp __ARGS((const void *s1, const void *s2));
694static int
695# ifdef __BORLANDC__
696 _RTLENTRYF
697# endif
698 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000699#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000700static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000701static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000702static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void func_free __ARGS((ufunc_T *fp));
704static void func_unref __ARGS((char_u *name));
705static void func_ref __ARGS((char_u *name));
706static 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));
707static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000708static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000710/* Character used as separated in autoload function/variable names. */
711#define AUTOLOAD_CHAR '#'
712
Bram Moolenaar33570922005-01-25 22:26:29 +0000713/*
714 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000715 */
716 void
717eval_init()
718{
Bram Moolenaar33570922005-01-25 22:26:29 +0000719 int i;
720 struct vimvar *p;
721
722 init_var_dict(&globvardict, &globvars_var);
723 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000724 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000725 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000726
727 for (i = 0; i < VV_LEN; ++i)
728 {
729 p = &vimvars[i];
730 STRCPY(p->vv_di.di_key, p->vv_name);
731 if (p->vv_flags & VV_RO)
732 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
733 else if (p->vv_flags & VV_RO_SBX)
734 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
735 else
736 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000737
738 /* add to v: scope dict, unless the value is not always available */
739 if (p->vv_type != VAR_UNKNOWN)
740 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000741 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000742 /* add to compat scope dict */
743 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000744 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000745}
746
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000747#if defined(EXITFREE) || defined(PROTO)
748 void
749eval_clear()
750{
751 int i;
752 struct vimvar *p;
753
754 for (i = 0; i < VV_LEN; ++i)
755 {
756 p = &vimvars[i];
757 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000758 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000759 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000760 p->vv_di.di_tv.vval.v_string = NULL;
761 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000762 }
763 hash_clear(&vimvarht);
764 hash_clear(&compat_hashtab);
765
766 /* script-local variables */
767 for (i = 1; i <= ga_scripts.ga_len; ++i)
768 vars_clear(&SCRIPT_VARS(i));
769 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000770 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000771
772 /* global variables */
773 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000774
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000775 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000776 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000777 hash_clear(&func_hashtab);
778
779 /* unreferenced lists and dicts */
780 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000781}
782#endif
783
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 * Return the name of the executed function.
786 */
787 char_u *
788func_name(cookie)
789 void *cookie;
790{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000791 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792}
793
794/*
795 * Return the address holding the next breakpoint line for a funccall cookie.
796 */
797 linenr_T *
798func_breakpoint(cookie)
799 void *cookie;
800{
Bram Moolenaar33570922005-01-25 22:26:29 +0000801 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802}
803
804/*
805 * Return the address holding the debug tick for a funccall cookie.
806 */
807 int *
808func_dbg_tick(cookie)
809 void *cookie;
810{
Bram Moolenaar33570922005-01-25 22:26:29 +0000811 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812}
813
814/*
815 * Return the nesting level for a funccall cookie.
816 */
817 int
818func_level(cookie)
819 void *cookie;
820{
Bram Moolenaar33570922005-01-25 22:26:29 +0000821 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822}
823
824/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000825funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827/*
828 * Return TRUE when a function was ended by a ":return" command.
829 */
830 int
831current_func_returned()
832{
833 return current_funccal->returned;
834}
835
836
837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 * Set an internal variable to a string value. Creates the variable if it does
839 * not already exist.
840 */
841 void
842set_internal_string_var(name, value)
843 char_u *name;
844 char_u *value;
845{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000846 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000847 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
849 val = vim_strsave(value);
850 if (val != NULL)
851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000852 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000853 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000855 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000856 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
858 }
859}
860
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000861static lval_T *redir_lval = NULL;
862static char_u *redir_endp = NULL;
863static char_u *redir_varname = NULL;
864
865/*
866 * Start recording command output to a variable
867 * Returns OK if successfully completed the setup. FAIL otherwise.
868 */
869 int
870var_redir_start(name, append)
871 char_u *name;
872 int append; /* append to an existing variable */
873{
874 int save_emsg;
875 int err;
876 typval_T tv;
877
878 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000879 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000880 {
881 EMSG(_(e_invarg));
882 return FAIL;
883 }
884
885 redir_varname = vim_strsave(name);
886 if (redir_varname == NULL)
887 return FAIL;
888
889 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
890 if (redir_lval == NULL)
891 {
892 var_redir_stop();
893 return FAIL;
894 }
895
896 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000897 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
898 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000899 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
900 {
901 if (redir_endp != NULL && *redir_endp != NUL)
902 /* Trailing characters are present after the variable name */
903 EMSG(_(e_trailing));
904 else
905 EMSG(_(e_invarg));
906 var_redir_stop();
907 return FAIL;
908 }
909
910 /* check if we can write to the variable: set it to or append an empty
911 * string */
912 save_emsg = did_emsg;
913 did_emsg = FALSE;
914 tv.v_type = VAR_STRING;
915 tv.vval.v_string = (char_u *)"";
916 if (append)
917 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
918 else
919 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
920 err = did_emsg;
921 did_emsg += save_emsg;
922 if (err)
923 {
924 var_redir_stop();
925 return FAIL;
926 }
927 if (redir_lval->ll_newkey != NULL)
928 {
929 /* Dictionary item was created, don't do it again. */
930 vim_free(redir_lval->ll_newkey);
931 redir_lval->ll_newkey = NULL;
932 }
933
934 return OK;
935}
936
937/*
938 * Append "value[len]" to the variable set by var_redir_start().
939 */
940 void
941var_redir_str(value, len)
942 char_u *value;
943 int len;
944{
945 char_u *val;
946 typval_T tv;
947 int save_emsg;
948 int err;
949
950 if (redir_lval == NULL)
951 return;
952
953 if (len == -1)
954 /* Append the entire string */
955 val = vim_strsave(value);
956 else
957 /* Append only the specified number of characters */
958 val = vim_strnsave(value, len);
959 if (val == NULL)
960 return;
961
962 tv.v_type = VAR_STRING;
963 tv.vval.v_string = val;
964
965 save_emsg = did_emsg;
966 did_emsg = FALSE;
967 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
968 err = did_emsg;
969 did_emsg += save_emsg;
970 if (err)
971 var_redir_stop();
972
973 vim_free(tv.vval.v_string);
974}
975
976/*
977 * Stop redirecting command output to a variable.
978 */
979 void
980var_redir_stop()
981{
982 if (redir_lval != NULL)
983 {
984 clear_lval(redir_lval);
985 vim_free(redir_lval);
986 redir_lval = NULL;
987 }
988 vim_free(redir_varname);
989 redir_varname = NULL;
990}
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992# if defined(FEAT_MBYTE) || defined(PROTO)
993 int
994eval_charconvert(enc_from, enc_to, fname_from, fname_to)
995 char_u *enc_from;
996 char_u *enc_to;
997 char_u *fname_from;
998 char_u *fname_to;
999{
1000 int err = FALSE;
1001
1002 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1003 set_vim_var_string(VV_CC_TO, enc_to, -1);
1004 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1005 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1006 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1007 err = TRUE;
1008 set_vim_var_string(VV_CC_FROM, NULL, -1);
1009 set_vim_var_string(VV_CC_TO, NULL, -1);
1010 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1011 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1012
1013 if (err)
1014 return FAIL;
1015 return OK;
1016}
1017# endif
1018
1019# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1020 int
1021eval_printexpr(fname, args)
1022 char_u *fname;
1023 char_u *args;
1024{
1025 int err = FALSE;
1026
1027 set_vim_var_string(VV_FNAME_IN, fname, -1);
1028 set_vim_var_string(VV_CMDARG, args, -1);
1029 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1030 err = TRUE;
1031 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1032 set_vim_var_string(VV_CMDARG, NULL, -1);
1033
1034 if (err)
1035 {
1036 mch_remove(fname);
1037 return FAIL;
1038 }
1039 return OK;
1040}
1041# endif
1042
1043# if defined(FEAT_DIFF) || defined(PROTO)
1044 void
1045eval_diff(origfile, newfile, outfile)
1046 char_u *origfile;
1047 char_u *newfile;
1048 char_u *outfile;
1049{
1050 int err = FALSE;
1051
1052 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1053 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1054 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1055 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1056 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1057 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1058 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1059}
1060
1061 void
1062eval_patch(origfile, difffile, outfile)
1063 char_u *origfile;
1064 char_u *difffile;
1065 char_u *outfile;
1066{
1067 int err;
1068
1069 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1070 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1071 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1072 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1073 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1074 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1075 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1076}
1077# endif
1078
1079/*
1080 * Top level evaluation function, returning a boolean.
1081 * Sets "error" to TRUE if there was an error.
1082 * Return TRUE or FALSE.
1083 */
1084 int
1085eval_to_bool(arg, error, nextcmd, skip)
1086 char_u *arg;
1087 int *error;
1088 char_u **nextcmd;
1089 int skip; /* only parse, don't execute */
1090{
Bram Moolenaar33570922005-01-25 22:26:29 +00001091 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 int retval = FALSE;
1093
1094 if (skip)
1095 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001096 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 else
1099 {
1100 *error = FALSE;
1101 if (!skip)
1102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001103 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001104 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 }
1106 }
1107 if (skip)
1108 --emsg_skip;
1109
1110 return retval;
1111}
1112
1113/*
1114 * Top level evaluation function, returning a string. If "skip" is TRUE,
1115 * only parsing to "nextcmd" is done, without reporting errors. Return
1116 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1117 */
1118 char_u *
1119eval_to_string_skip(arg, nextcmd, skip)
1120 char_u *arg;
1121 char_u **nextcmd;
1122 int skip; /* only parse, don't execute */
1123{
Bram Moolenaar33570922005-01-25 22:26:29 +00001124 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 char_u *retval;
1126
1127 if (skip)
1128 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001129 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 retval = NULL;
1131 else
1132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001133 retval = vim_strsave(get_tv_string(&tv));
1134 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 }
1136 if (skip)
1137 --emsg_skip;
1138
1139 return retval;
1140}
1141
1142/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001143 * Skip over an expression at "*pp".
1144 * Return FAIL for an error, OK otherwise.
1145 */
1146 int
1147skip_expr(pp)
1148 char_u **pp;
1149{
Bram Moolenaar33570922005-01-25 22:26:29 +00001150 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001151
1152 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001154}
1155
1156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 * Top level evaluation function, returning a string.
1158 * Return pointer to allocated memory, or NULL for failure.
1159 */
1160 char_u *
1161eval_to_string(arg, nextcmd)
1162 char_u *arg;
1163 char_u **nextcmd;
1164{
Bram Moolenaar33570922005-01-25 22:26:29 +00001165 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 char_u *retval;
1167
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001168 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 retval = NULL;
1170 else
1171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001172 retval = vim_strsave(get_tv_string(&tv));
1173 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
1175
1176 return retval;
1177}
1178
1179/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001180 * Call eval_to_string() without using current local variables and using
1181 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 */
1183 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001184eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 char_u *arg;
1186 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001187 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188{
1189 char_u *retval;
1190 void *save_funccalp;
1191
1192 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001193 if (use_sandbox)
1194 ++sandbox;
1195 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 retval = eval_to_string(arg, nextcmd);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001197 if (use_sandbox)
1198 --sandbox;
1199 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 restore_funccal(save_funccalp);
1201 return retval;
1202}
1203
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204/*
1205 * Top level evaluation function, returning a number.
1206 * Evaluates "expr" silently.
1207 * Returns -1 for an error.
1208 */
1209 int
1210eval_to_number(expr)
1211 char_u *expr;
1212{
Bram Moolenaar33570922005-01-25 22:26:29 +00001213 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001215 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
1217 ++emsg_off;
1218
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001219 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 retval = -1;
1221 else
1222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001223 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001224 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 }
1226 --emsg_off;
1227
1228 return retval;
1229}
1230
Bram Moolenaara40058a2005-07-11 22:42:07 +00001231/*
1232 * Prepare v: variable "idx" to be used.
1233 * Save the current typeval in "save_tv".
1234 * When not used yet add the variable to the v: hashtable.
1235 */
1236 static void
1237prepare_vimvar(idx, save_tv)
1238 int idx;
1239 typval_T *save_tv;
1240{
1241 *save_tv = vimvars[idx].vv_tv;
1242 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1243 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1244}
1245
1246/*
1247 * Restore v: variable "idx" to typeval "save_tv".
1248 * When no longer defined, remove the variable from the v: hashtable.
1249 */
1250 static void
1251restore_vimvar(idx, save_tv)
1252 int idx;
1253 typval_T *save_tv;
1254{
1255 hashitem_T *hi;
1256
1257 clear_tv(&vimvars[idx].vv_tv);
1258 vimvars[idx].vv_tv = *save_tv;
1259 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1260 {
1261 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1262 if (HASHITEM_EMPTY(hi))
1263 EMSG2(_(e_intern2), "restore_vimvar()");
1264 else
1265 hash_remove(&vimvarht, hi);
1266 }
1267}
1268
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001269#if defined(FEAT_SYN_HL) || defined(PROTO)
1270/*
1271 * Evaluate an expression to a list with suggestions.
1272 * For the "expr:" part of 'spellsuggest'.
1273 */
1274 list_T *
1275eval_spell_expr(badword, expr)
1276 char_u *badword;
1277 char_u *expr;
1278{
1279 typval_T save_val;
1280 typval_T rettv;
1281 list_T *list = NULL;
1282 char_u *p = skipwhite(expr);
1283
1284 /* Set "v:val" to the bad word. */
1285 prepare_vimvar(VV_VAL, &save_val);
1286 vimvars[VV_VAL].vv_type = VAR_STRING;
1287 vimvars[VV_VAL].vv_str = badword;
1288 if (p_verbose == 0)
1289 ++emsg_off;
1290
1291 if (eval1(&p, &rettv, TRUE) == OK)
1292 {
1293 if (rettv.v_type != VAR_LIST)
1294 clear_tv(&rettv);
1295 else
1296 list = rettv.vval.v_list;
1297 }
1298
1299 if (p_verbose == 0)
1300 --emsg_off;
1301 vimvars[VV_VAL].vv_str = NULL;
1302 restore_vimvar(VV_VAL, &save_val);
1303
1304 return list;
1305}
1306
1307/*
1308 * "list" is supposed to contain two items: a word and a number. Return the
1309 * word in "pp" and the number as the return value.
1310 * Return -1 if anything isn't right.
1311 * Used to get the good word and score from the eval_spell_expr() result.
1312 */
1313 int
1314get_spellword(list, pp)
1315 list_T *list;
1316 char_u **pp;
1317{
1318 listitem_T *li;
1319
1320 li = list->lv_first;
1321 if (li == NULL)
1322 return -1;
1323 *pp = get_tv_string(&li->li_tv);
1324
1325 li = li->li_next;
1326 if (li == NULL)
1327 return -1;
1328 return get_tv_number(&li->li_tv);
1329}
1330#endif
1331
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001332/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001333 * Top level evaluation function.
1334 * Returns an allocated typval_T with the result.
1335 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001336 */
1337 typval_T *
1338eval_expr(arg, nextcmd)
1339 char_u *arg;
1340 char_u **nextcmd;
1341{
1342 typval_T *tv;
1343
1344 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001345 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001346 {
1347 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001348 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001349 }
1350
1351 return tv;
1352}
1353
1354
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1356/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001357 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001359 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001361 static int
1362call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 char_u *func;
1364 int argc;
1365 char_u **argv;
1366 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
Bram Moolenaar33570922005-01-25 22:26:29 +00001369 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 long n;
1371 int len;
1372 int i;
1373 int doesrange;
1374 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001375 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
Bram Moolenaar33570922005-01-25 22:26:29 +00001377 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001379 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
1381 for (i = 0; i < argc; i++)
1382 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001383 /* Pass a NULL or empty argument as an empty string */
1384 if (argv[i] == NULL || *argv[i] == NUL)
1385 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001386 argvars[i].v_type = VAR_STRING;
1387 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001388 continue;
1389 }
1390
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 /* Recognize a number argument, the others must be strings. */
1392 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1393 if (len != 0 && len == (int)STRLEN(argv[i]))
1394 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001395 argvars[i].v_type = VAR_NUMBER;
1396 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398 else
1399 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001400 argvars[i].v_type = VAR_STRING;
1401 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403 }
1404
1405 if (safe)
1406 {
1407 save_funccalp = save_funccal();
1408 ++sandbox;
1409 }
1410
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001411 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1412 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001414 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (safe)
1416 {
1417 --sandbox;
1418 restore_funccal(save_funccalp);
1419 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001420 vim_free(argvars);
1421
1422 if (ret == FAIL)
1423 clear_tv(rettv);
1424
1425 return ret;
1426}
1427
1428/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001429 * Call vimL function "func" and return the result as a string.
1430 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001431 * Uses argv[argc] for the function arguments.
1432 */
1433 void *
1434call_func_retstr(func, argc, argv, safe)
1435 char_u *func;
1436 int argc;
1437 char_u **argv;
1438 int safe; /* use the sandbox */
1439{
1440 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001441 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001442
1443 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1444 return NULL;
1445
1446 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001447 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 return retval;
1449}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001450
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001451#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001452/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001453 * Call vimL function "func" and return the result as a number.
1454 * Returns -1 when calling the function fails.
1455 * Uses argv[argc] for the function arguments.
1456 */
1457 long
1458call_func_retnr(func, argc, argv, safe)
1459 char_u *func;
1460 int argc;
1461 char_u **argv;
1462 int safe; /* use the sandbox */
1463{
1464 typval_T rettv;
1465 long retval;
1466
1467 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1468 return -1;
1469
1470 retval = get_tv_number_chk(&rettv, NULL);
1471 clear_tv(&rettv);
1472 return retval;
1473}
1474#endif
1475
1476/*
1477 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001478 * Uses argv[argc] for the function arguments.
1479 */
1480 void *
1481call_func_retlist(func, argc, argv, safe)
1482 char_u *func;
1483 int argc;
1484 char_u **argv;
1485 int safe; /* use the sandbox */
1486{
1487 typval_T rettv;
1488
1489 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1490 return NULL;
1491
1492 if (rettv.v_type != VAR_LIST)
1493 {
1494 clear_tv(&rettv);
1495 return NULL;
1496 }
1497
1498 return rettv.vval.v_list;
1499}
1500
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501#endif
1502
1503/*
1504 * Save the current function call pointer, and set it to NULL.
1505 * Used when executing autocommands and for ":source".
1506 */
1507 void *
1508save_funccal()
1509{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001510 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 current_funccal = NULL;
1513 return (void *)fc;
1514}
1515
1516 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001517restore_funccal(vfc)
1518 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001520 funccall_T *fc = (funccall_T *)vfc;
1521
1522 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523}
1524
Bram Moolenaar05159a02005-02-26 23:04:13 +00001525#if defined(FEAT_PROFILE) || defined(PROTO)
1526/*
1527 * Prepare profiling for entering a child or something else that is not
1528 * counted for the script/function itself.
1529 * Should always be called in pair with prof_child_exit().
1530 */
1531 void
1532prof_child_enter(tm)
1533 proftime_T *tm; /* place to store waittime */
1534{
1535 funccall_T *fc = current_funccal;
1536
1537 if (fc != NULL && fc->func->uf_profiling)
1538 profile_start(&fc->prof_child);
1539 script_prof_save(tm);
1540}
1541
1542/*
1543 * Take care of time spent in a child.
1544 * Should always be called after prof_child_enter().
1545 */
1546 void
1547prof_child_exit(tm)
1548 proftime_T *tm; /* where waittime was stored */
1549{
1550 funccall_T *fc = current_funccal;
1551
1552 if (fc != NULL && fc->func->uf_profiling)
1553 {
1554 profile_end(&fc->prof_child);
1555 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1556 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1557 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1558 }
1559 script_prof_restore(tm);
1560}
1561#endif
1562
1563
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564#ifdef FEAT_FOLDING
1565/*
1566 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1567 * it in "*cp". Doesn't give error messages.
1568 */
1569 int
1570eval_foldexpr(arg, cp)
1571 char_u *arg;
1572 int *cp;
1573{
Bram Moolenaar33570922005-01-25 22:26:29 +00001574 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 int retval;
1576 char_u *s;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001577 int use_sandbox = was_set_insecurely((char_u *)"foldexpr");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
1579 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001580 if (use_sandbox)
1581 ++sandbox;
1582 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001584 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 retval = 0;
1586 else
1587 {
1588 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001589 if (tv.v_type == VAR_NUMBER)
1590 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001591 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 retval = 0;
1593 else
1594 {
1595 /* If the result is a string, check if there is a non-digit before
1596 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001597 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (!VIM_ISDIGIT(*s) && *s != '-')
1599 *cp = *s++;
1600 retval = atol((char *)s);
1601 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001602 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
1604 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001605 if (use_sandbox)
1606 --sandbox;
1607 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
1609 return retval;
1610}
1611#endif
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001614 * ":let" list all variable values
1615 * ":let var1 var2" list variable values
1616 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001617 * ":let var += expr" assignment command.
1618 * ":let var -= expr" assignment command.
1619 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001620 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 */
1622 void
1623ex_let(eap)
1624 exarg_T *eap;
1625{
1626 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001627 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001628 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001630 int var_count = 0;
1631 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001632 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001634 expr = skip_var_list(arg, &var_count, &semicolon);
1635 if (expr == NULL)
1636 return;
1637 expr = vim_strchr(expr, '=');
1638 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001640 /*
1641 * ":let" without "=": list variables
1642 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643 if (*arg == '[')
1644 EMSG(_(e_invarg));
1645 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001646 /* ":let var1 var2" */
1647 arg = list_arg_vars(eap, arg);
1648 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001650 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001651 list_glob_vars();
1652 list_buf_vars();
1653 list_win_vars();
1654 list_vim_vars();
1655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 eap->nextcmd = check_nextcmd(arg);
1657 }
1658 else
1659 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001660 op[0] = '=';
1661 op[1] = NUL;
1662 if (expr > arg)
1663 {
1664 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1665 op[0] = expr[-1]; /* +=, -= or .= */
1666 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001668
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 if (eap->skip)
1670 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if (eap->skip)
1673 {
1674 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 --emsg_skip;
1677 }
1678 else if (i != FAIL)
1679 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001681 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
1684 }
1685}
1686
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687/*
1688 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1689 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001690 * When "nextchars" is not NULL it points to a string with characters that
1691 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1692 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001693 * Returns OK or FAIL;
1694 */
1695 static int
1696ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1697 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001698 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001699 int copy; /* copy values from "tv", don't move */
1700 int semicolon; /* from skip_var_list() */
1701 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703{
1704 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001705 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001707 listitem_T *item;
1708 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709
1710 if (*arg != '[')
1711 {
1712 /*
1713 * ":let var = expr" or ":for var in list"
1714 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001715 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 return FAIL;
1717 return OK;
1718 }
1719
1720 /*
1721 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1722 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001723 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 {
1725 EMSG(_(e_listreq));
1726 return FAIL;
1727 }
1728
1729 i = list_len(l);
1730 if (semicolon == 0 && var_count < i)
1731 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001732 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001733 return FAIL;
1734 }
1735 if (var_count - semicolon > i)
1736 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001737 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001738 return FAIL;
1739 }
1740
1741 item = l->lv_first;
1742 while (*arg != ']')
1743 {
1744 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001745 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746 item = item->li_next;
1747 if (arg == NULL)
1748 return FAIL;
1749
1750 arg = skipwhite(arg);
1751 if (*arg == ';')
1752 {
1753 /* Put the rest of the list (may be empty) in the var after ';'.
1754 * Create a new list for this. */
1755 l = list_alloc();
1756 if (l == NULL)
1757 return FAIL;
1758 while (item != NULL)
1759 {
1760 list_append_tv(l, &item->li_tv);
1761 item = item->li_next;
1762 }
1763
1764 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001765 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001766 ltv.vval.v_list = l;
1767 l->lv_refcount = 1;
1768
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001769 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1770 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 clear_tv(&ltv);
1772 if (arg == NULL)
1773 return FAIL;
1774 break;
1775 }
1776 else if (*arg != ',' && *arg != ']')
1777 {
1778 EMSG2(_(e_intern2), "ex_let_vars()");
1779 return FAIL;
1780 }
1781 }
1782
1783 return OK;
1784}
1785
1786/*
1787 * Skip over assignable variable "var" or list of variables "[var, var]".
1788 * Used for ":let varvar = expr" and ":for varvar in expr".
1789 * For "[var, var]" increment "*var_count" for each variable.
1790 * for "[var, var; var]" set "semicolon".
1791 * Return NULL for an error.
1792 */
1793 static char_u *
1794skip_var_list(arg, var_count, semicolon)
1795 char_u *arg;
1796 int *var_count;
1797 int *semicolon;
1798{
1799 char_u *p, *s;
1800
1801 if (*arg == '[')
1802 {
1803 /* "[var, var]": find the matching ']'. */
1804 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001805 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001806 {
1807 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1808 s = skip_var_one(p);
1809 if (s == p)
1810 {
1811 EMSG2(_(e_invarg2), p);
1812 return NULL;
1813 }
1814 ++*var_count;
1815
1816 p = skipwhite(s);
1817 if (*p == ']')
1818 break;
1819 else if (*p == ';')
1820 {
1821 if (*semicolon == 1)
1822 {
1823 EMSG(_("Double ; in list of variables"));
1824 return NULL;
1825 }
1826 *semicolon = 1;
1827 }
1828 else if (*p != ',')
1829 {
1830 EMSG2(_(e_invarg2), p);
1831 return NULL;
1832 }
1833 }
1834 return p + 1;
1835 }
1836 else
1837 return skip_var_one(arg);
1838}
1839
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001840/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001841 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1842 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001843 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001844 static char_u *
1845skip_var_one(arg)
1846 char_u *arg;
1847{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001848 if (*arg == '@' && arg[1] != NUL)
1849 return arg + 2;
1850 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1851 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852}
1853
Bram Moolenaara7043832005-01-21 11:56:39 +00001854/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001855 * List variables for hashtab "ht" with prefix "prefix".
1856 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001857 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001859list_hashtable_vars(ht, prefix, empty)
1860 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001861 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001862 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001863{
Bram Moolenaar33570922005-01-25 22:26:29 +00001864 hashitem_T *hi;
1865 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001866 int todo;
1867
1868 todo = ht->ht_used;
1869 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1870 {
1871 if (!HASHITEM_EMPTY(hi))
1872 {
1873 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001874 di = HI2DI(hi);
1875 if (empty || di->di_tv.v_type != VAR_STRING
1876 || di->di_tv.vval.v_string != NULL)
1877 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001878 }
1879 }
1880}
1881
1882/*
1883 * List global variables.
1884 */
1885 static void
1886list_glob_vars()
1887{
Bram Moolenaar33570922005-01-25 22:26:29 +00001888 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001889}
1890
1891/*
1892 * List buffer variables.
1893 */
1894 static void
1895list_buf_vars()
1896{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001897 char_u numbuf[NUMBUFLEN];
1898
Bram Moolenaar33570922005-01-25 22:26:29 +00001899 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001900
1901 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1902 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001903}
1904
1905/*
1906 * List window variables.
1907 */
1908 static void
1909list_win_vars()
1910{
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001912}
1913
1914/*
1915 * List Vim variables.
1916 */
1917 static void
1918list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919{
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921}
1922
1923/*
1924 * List variables in "arg".
1925 */
1926 static char_u *
1927list_arg_vars(eap, arg)
1928 exarg_T *eap;
1929 char_u *arg;
1930{
1931 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001932 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001934 char_u *name_start;
1935 char_u *arg_subsc;
1936 char_u *tofree;
1937 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938
1939 while (!ends_excmd(*arg) && !got_int)
1940 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001941 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001943 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001944 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1945 {
1946 emsg_severe = TRUE;
1947 EMSG(_(e_trailing));
1948 break;
1949 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001951 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001953 /* get_name_len() takes care of expanding curly braces */
1954 name_start = name = arg;
1955 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1956 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001958 /* This is mainly to keep test 49 working: when expanding
1959 * curly braces fails overrule the exception error message. */
1960 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001962 emsg_severe = TRUE;
1963 EMSG2(_(e_invarg2), arg);
1964 break;
1965 }
1966 error = TRUE;
1967 }
1968 else
1969 {
1970 if (tofree != NULL)
1971 name = tofree;
1972 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001973 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974 else
1975 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001976 /* handle d.key, l[idx], f(expr) */
1977 arg_subsc = arg;
1978 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001979 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001980 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001981 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001982 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001983 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001984 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001985 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001986 case 'g': list_glob_vars(); break;
1987 case 'b': list_buf_vars(); break;
1988 case 'w': list_win_vars(); break;
1989 case 'v': list_vim_vars(); break;
1990 default:
1991 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001992 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001993 }
1994 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001995 {
1996 char_u numbuf[NUMBUFLEN];
1997 char_u *tf;
1998 int c;
1999 char_u *s;
2000
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002001 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002002 c = *arg;
2003 *arg = NUL;
2004 list_one_var_a((char_u *)"",
2005 arg == arg_subsc ? name : name_start,
2006 tv.v_type, s == NULL ? (char_u *)"" : s);
2007 *arg = c;
2008 vim_free(tf);
2009 }
2010 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002011 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 }
2013 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002014
2015 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002016 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002017
2018 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019 }
2020
2021 return arg;
2022}
2023
2024/*
2025 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2026 * Returns a pointer to the char just after the var name.
2027 * Returns NULL if there is an error.
2028 */
2029 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002030ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002032 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002035 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002036{
2037 int c1;
2038 char_u *name;
2039 char_u *p;
2040 char_u *arg_end = NULL;
2041 int len;
2042 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044
2045 /*
2046 * ":let $VAR = expr": Set environment variable.
2047 */
2048 if (*arg == '$')
2049 {
2050 /* Find the end of the name. */
2051 ++arg;
2052 name = arg;
2053 len = get_env_len(&arg);
2054 if (len == 0)
2055 EMSG2(_(e_invarg2), name - 1);
2056 else
2057 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002058 if (op != NULL && (*op == '+' || *op == '-'))
2059 EMSG2(_(e_letwrong), op);
2060 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062 EMSG(_(e_letunexp));
2063 else
2064 {
2065 c1 = name[len];
2066 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002067 p = get_tv_string_chk(tv);
2068 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002069 {
2070 int mustfree = FALSE;
2071 char_u *s = vim_getenv(name, &mustfree);
2072
2073 if (s != NULL)
2074 {
2075 p = tofree = concat_str(s, p);
2076 if (mustfree)
2077 vim_free(s);
2078 }
2079 }
2080 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002081 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002082 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002083 if (STRICMP(name, "HOME") == 0)
2084 init_homedir();
2085 else if (didset_vim && STRICMP(name, "VIM") == 0)
2086 didset_vim = FALSE;
2087 else if (didset_vimruntime
2088 && STRICMP(name, "VIMRUNTIME") == 0)
2089 didset_vimruntime = FALSE;
2090 arg_end = arg;
2091 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002092 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002093 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002094 }
2095 }
2096 }
2097
2098 /*
2099 * ":let &option = expr": Set option value.
2100 * ":let &l:option = expr": Set local option value.
2101 * ":let &g:option = expr": Set global option value.
2102 */
2103 else if (*arg == '&')
2104 {
2105 /* Find the end of the name. */
2106 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002107 if (p == NULL || (endchars != NULL
2108 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002109 EMSG(_(e_letunexp));
2110 else
2111 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002112 long n;
2113 int opt_type;
2114 long numval;
2115 char_u *stringval = NULL;
2116 char_u *s;
2117
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002118 c1 = *p;
2119 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002120
2121 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002122 s = get_tv_string_chk(tv); /* != NULL if number or string */
2123 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002124 {
2125 opt_type = get_option_value(arg, &numval,
2126 &stringval, opt_flags);
2127 if ((opt_type == 1 && *op == '.')
2128 || (opt_type == 0 && *op != '.'))
2129 EMSG2(_(e_letwrong), op);
2130 else
2131 {
2132 if (opt_type == 1) /* number */
2133 {
2134 if (*op == '+')
2135 n = numval + n;
2136 else
2137 n = numval - n;
2138 }
2139 else if (opt_type == 0 && stringval != NULL) /* string */
2140 {
2141 s = concat_str(stringval, s);
2142 vim_free(stringval);
2143 stringval = s;
2144 }
2145 }
2146 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002147 if (s != NULL)
2148 {
2149 set_option_value(arg, n, s, opt_flags);
2150 arg_end = p;
2151 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002153 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154 }
2155 }
2156
2157 /*
2158 * ":let @r = expr": Set register contents.
2159 */
2160 else if (*arg == '@')
2161 {
2162 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 if (op != NULL && (*op == '+' || *op == '-'))
2164 EMSG2(_(e_letwrong), op);
2165 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002167 EMSG(_(e_letunexp));
2168 else
2169 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002170 char_u *tofree = NULL;
2171 char_u *s;
2172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002173 p = get_tv_string_chk(tv);
2174 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002175 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002176 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002177 if (s != NULL)
2178 {
2179 p = tofree = concat_str(s, p);
2180 vim_free(s);
2181 }
2182 }
2183 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002184 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002185 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002186 arg_end = arg + 1;
2187 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002188 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 }
2190 }
2191
2192 /*
2193 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002196 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002198 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002200 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002203 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2204 EMSG(_(e_letunexp));
2205 else
2206 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002207 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002208 arg_end = p;
2209 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 }
2213
2214 else
2215 EMSG2(_(e_invarg2), arg);
2216
2217 return arg_end;
2218}
2219
2220/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002221 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2222 */
2223 static int
2224check_changedtick(arg)
2225 char_u *arg;
2226{
2227 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2228 {
2229 EMSG2(_(e_readonlyvar), arg);
2230 return TRUE;
2231 }
2232 return FALSE;
2233}
2234
2235/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236 * Get an lval: variable, Dict item or List item that can be assigned a value
2237 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2238 * "name.key", "name.key[expr]" etc.
2239 * Indexing only works if "name" is an existing List or Dictionary.
2240 * "name" points to the start of the name.
2241 * If "rettv" is not NULL it points to the value to be assigned.
2242 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2243 * wrong; must end in space or cmd separator.
2244 *
2245 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002246 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 */
2249 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002250get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002252 typval_T *rettv;
2253 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 int unlet;
2255 int skip;
2256 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002257 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 char_u *expr_start, *expr_end;
2261 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002262 dictitem_T *v;
2263 typval_T var1;
2264 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002266 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002267 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002268 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002269 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002271 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002272 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273
2274 if (skip)
2275 {
2276 /* When skipping just find the end of the name. */
2277 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002278 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 }
2280
2281 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002282 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002283 if (expr_start != NULL)
2284 {
2285 /* Don't expand the name when we already know there is an error. */
2286 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2287 && *p != '[' && *p != '.')
2288 {
2289 EMSG(_(e_trailing));
2290 return NULL;
2291 }
2292
2293 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2294 if (lp->ll_exp_name == NULL)
2295 {
2296 /* Report an invalid expression in braces, unless the
2297 * expression evaluation has been cancelled due to an
2298 * aborting error, an interrupt, or an exception. */
2299 if (!aborting() && !quiet)
2300 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002301 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002302 EMSG2(_(e_invarg2), name);
2303 return NULL;
2304 }
2305 }
2306 lp->ll_name = lp->ll_exp_name;
2307 }
2308 else
2309 lp->ll_name = name;
2310
2311 /* Without [idx] or .key we are done. */
2312 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2313 return p;
2314
2315 cc = *p;
2316 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002317 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002318 if (v == NULL && !quiet)
2319 EMSG2(_(e_undefvar), lp->ll_name);
2320 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 if (v == NULL)
2322 return NULL;
2323
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 /*
2325 * Loop until no more [idx] or .key is following.
2326 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002328 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2331 && !(lp->ll_tv->v_type == VAR_DICT
2332 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002334 if (!quiet)
2335 EMSG(_("E689: Can only index a List or Dictionary"));
2336 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 if (!quiet)
2341 EMSG(_("E708: [:] must come last"));
2342 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002344
Bram Moolenaar8c711452005-01-14 21:53:12 +00002345 len = -1;
2346 if (*p == '.')
2347 {
2348 key = p + 1;
2349 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2350 ;
2351 if (len == 0)
2352 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 if (!quiet)
2354 EMSG(_(e_emptykey));
2355 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002356 }
2357 p = key + len;
2358 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002359 else
2360 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002361 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002362 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002363 if (*p == ':')
2364 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002365 else
2366 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002367 empty1 = FALSE;
2368 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002370 if (get_tv_string_chk(&var1) == NULL)
2371 {
2372 /* not a number or string */
2373 clear_tv(&var1);
2374 return NULL;
2375 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002376 }
2377
2378 /* Optionally get the second index [ :expr]. */
2379 if (*p == ':')
2380 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002381 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002382 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002385 if (!empty1)
2386 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002388 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 if (rettv != NULL && (rettv->v_type != VAR_LIST
2390 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002391 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002392 if (!quiet)
2393 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002394 if (!empty1)
2395 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002397 }
2398 p = skipwhite(p + 1);
2399 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002400 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002401 else
2402 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002404 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2405 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002406 if (!empty1)
2407 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002410 if (get_tv_string_chk(&var2) == NULL)
2411 {
2412 /* not a number or string */
2413 if (!empty1)
2414 clear_tv(&var1);
2415 clear_tv(&var2);
2416 return NULL;
2417 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002418 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002419 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002420 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002421 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002423
Bram Moolenaar8c711452005-01-14 21:53:12 +00002424 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002425 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002426 if (!quiet)
2427 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002428 if (!empty1)
2429 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002433 }
2434
2435 /* Skip to past ']'. */
2436 ++p;
2437 }
2438
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 {
2441 if (len == -1)
2442 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 if (*key == NUL)
2446 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 if (!quiet)
2448 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 }
2452 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 lp->ll_list = NULL;
2454 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002455 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002458 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 if (len == -1)
2464 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002466 }
2467 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002471 if (len == -1)
2472 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 p = NULL;
2475 break;
2476 }
2477 if (len == -1)
2478 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002480 }
2481 else
2482 {
2483 /*
2484 * Get the number and item for the only or first index of the List.
2485 */
2486 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 else
2489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002490 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 clear_tv(&var1);
2492 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 lp->ll_list = lp->ll_tv->vval.v_list;
2495 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2496 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (!quiet)
2499 EMSGN(_(e_listidx), lp->ll_n1);
2500 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 }
2504
2505 /*
2506 * May need to find the item or absolute index for the second
2507 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 * When no index given: "lp->ll_empty2" is TRUE.
2509 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002512 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002513 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002518 if (ni == NULL)
2519 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (!quiet)
2521 EMSGN(_(e_listidx), lp->ll_n2);
2522 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 }
2526
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2528 if (lp->ll_n1 < 0)
2529 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2530 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002531 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 if (!quiet)
2533 EMSGN(_(e_listidx), lp->ll_n2);
2534 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002535 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002536 }
2537
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 }
2541
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 return p;
2543}
2544
2545/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002546 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 */
2548 static void
2549clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002550 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551{
2552 vim_free(lp->ll_exp_name);
2553 vim_free(lp->ll_newkey);
2554}
2555
2556/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002557 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002559 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 */
2561 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002562set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002563 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002567 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568{
2569 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002570 listitem_T *ri;
2571 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572
2573 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 cc = *endp;
2578 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002579 if (op != NULL && *op != '=')
2580 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002581 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002582
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002583 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002584 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2585 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002586 {
2587 if (tv_op(&tv, rettv, op) == OK)
2588 set_var(lp->ll_name, &tv, FALSE);
2589 clear_tv(&tv);
2590 }
2591 }
2592 else
2593 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002597 else if (tv_check_lock(lp->ll_newkey == NULL
2598 ? lp->ll_tv->v_lock
2599 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2600 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 else if (lp->ll_range)
2602 {
2603 /*
2604 * Assign the List values to the list items.
2605 */
2606 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002607 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002608 if (op != NULL && *op != '=')
2609 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2610 else
2611 {
2612 clear_tv(&lp->ll_li->li_tv);
2613 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2614 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 ri = ri->li_next;
2616 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2617 break;
2618 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002621 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 ri = NULL;
2624 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002626 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 lp->ll_li = lp->ll_li->li_next;
2628 ++lp->ll_n1;
2629 }
2630 if (ri != NULL)
2631 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002632 else if (lp->ll_empty2
2633 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 : lp->ll_n1 != lp->ll_n2)
2635 EMSG(_("E711: List value has not enough items"));
2636 }
2637 else
2638 {
2639 /*
2640 * Assign to a List or Dictionary item.
2641 */
2642 if (lp->ll_newkey != NULL)
2643 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002644 if (op != NULL && *op != '=')
2645 {
2646 EMSG2(_(e_letwrong), op);
2647 return;
2648 }
2649
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002651 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (di == NULL)
2653 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002654 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2655 {
2656 vim_free(di);
2657 return;
2658 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002661 else if (op != NULL && *op != '=')
2662 {
2663 tv_op(lp->ll_tv, rettv, op);
2664 return;
2665 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 /*
2670 * Assign the value to the variable or list item.
2671 */
2672 if (copy)
2673 copy_tv(rettv, lp->ll_tv);
2674 else
2675 {
2676 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002677 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002679 }
2680 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002681}
2682
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002683/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002684 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2685 * Returns OK or FAIL.
2686 */
2687 static int
2688tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002689 typval_T *tv1;
2690 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002691 char_u *op;
2692{
2693 long n;
2694 char_u numbuf[NUMBUFLEN];
2695 char_u *s;
2696
2697 /* Can't do anything with a Funcref or a Dict on the right. */
2698 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2699 {
2700 switch (tv1->v_type)
2701 {
2702 case VAR_DICT:
2703 case VAR_FUNC:
2704 break;
2705
2706 case VAR_LIST:
2707 if (*op != '+' || tv2->v_type != VAR_LIST)
2708 break;
2709 /* List += List */
2710 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2711 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2712 return OK;
2713
2714 case VAR_NUMBER:
2715 case VAR_STRING:
2716 if (tv2->v_type == VAR_LIST)
2717 break;
2718 if (*op == '+' || *op == '-')
2719 {
2720 /* nr += nr or nr -= nr*/
2721 n = get_tv_number(tv1);
2722 if (*op == '+')
2723 n += get_tv_number(tv2);
2724 else
2725 n -= get_tv_number(tv2);
2726 clear_tv(tv1);
2727 tv1->v_type = VAR_NUMBER;
2728 tv1->vval.v_number = n;
2729 }
2730 else
2731 {
2732 /* str .= str */
2733 s = get_tv_string(tv1);
2734 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2735 clear_tv(tv1);
2736 tv1->v_type = VAR_STRING;
2737 tv1->vval.v_string = s;
2738 }
2739 return OK;
2740 }
2741 }
2742
2743 EMSG2(_(e_letwrong), op);
2744 return FAIL;
2745}
2746
2747/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002748 * Add a watcher to a list.
2749 */
2750 static void
2751list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002752 list_T *l;
2753 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002754{
2755 lw->lw_next = l->lv_watch;
2756 l->lv_watch = lw;
2757}
2758
2759/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002760 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002761 * No warning when it isn't found...
2762 */
2763 static void
2764list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002765 list_T *l;
2766 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002767{
Bram Moolenaar33570922005-01-25 22:26:29 +00002768 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002769
2770 lwp = &l->lv_watch;
2771 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2772 {
2773 if (lw == lwrem)
2774 {
2775 *lwp = lw->lw_next;
2776 break;
2777 }
2778 lwp = &lw->lw_next;
2779 }
2780}
2781
2782/*
2783 * Just before removing an item from a list: advance watchers to the next
2784 * item.
2785 */
2786 static void
2787list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002788 list_T *l;
2789 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002790{
Bram Moolenaar33570922005-01-25 22:26:29 +00002791 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002792
2793 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2794 if (lw->lw_item == item)
2795 lw->lw_item = item->li_next;
2796}
2797
2798/*
2799 * Evaluate the expression used in a ":for var in expr" command.
2800 * "arg" points to "var".
2801 * Set "*errp" to TRUE for an error, FALSE otherwise;
2802 * Return a pointer that holds the info. Null when there is an error.
2803 */
2804 void *
2805eval_for_line(arg, errp, nextcmdp, skip)
2806 char_u *arg;
2807 int *errp;
2808 char_u **nextcmdp;
2809 int skip;
2810{
Bram Moolenaar33570922005-01-25 22:26:29 +00002811 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002812 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002813 typval_T tv;
2814 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002815
2816 *errp = TRUE; /* default: there is an error */
2817
Bram Moolenaar33570922005-01-25 22:26:29 +00002818 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002819 if (fi == NULL)
2820 return NULL;
2821
2822 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2823 if (expr == NULL)
2824 return fi;
2825
2826 expr = skipwhite(expr);
2827 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2828 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002829 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002830 return fi;
2831 }
2832
2833 if (skip)
2834 ++emsg_skip;
2835 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2836 {
2837 *errp = FALSE;
2838 if (!skip)
2839 {
2840 l = tv.vval.v_list;
2841 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002842 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002843 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002844 clear_tv(&tv);
2845 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002846 else
2847 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002848 /* No need to increment the refcount, it's already set for the
2849 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002850 fi->fi_list = l;
2851 list_add_watch(l, &fi->fi_lw);
2852 fi->fi_lw.lw_item = l->lv_first;
2853 }
2854 }
2855 }
2856 if (skip)
2857 --emsg_skip;
2858
2859 return fi;
2860}
2861
2862/*
2863 * Use the first item in a ":for" list. Advance to the next.
2864 * Assign the values to the variable (list). "arg" points to the first one.
2865 * Return TRUE when a valid item was found, FALSE when at end of list or
2866 * something wrong.
2867 */
2868 int
2869next_for_item(fi_void, arg)
2870 void *fi_void;
2871 char_u *arg;
2872{
Bram Moolenaar33570922005-01-25 22:26:29 +00002873 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002874 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002875 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002876
2877 item = fi->fi_lw.lw_item;
2878 if (item == NULL)
2879 result = FALSE;
2880 else
2881 {
2882 fi->fi_lw.lw_item = item->li_next;
2883 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2884 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2885 }
2886 return result;
2887}
2888
2889/*
2890 * Free the structure used to store info used by ":for".
2891 */
2892 void
2893free_for_info(fi_void)
2894 void *fi_void;
2895{
Bram Moolenaar33570922005-01-25 22:26:29 +00002896 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002897
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002898 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002899 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002900 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002901 list_unref(fi->fi_list);
2902 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002903 vim_free(fi);
2904}
2905
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2907
2908 void
2909set_context_for_expression(xp, arg, cmdidx)
2910 expand_T *xp;
2911 char_u *arg;
2912 cmdidx_T cmdidx;
2913{
2914 int got_eq = FALSE;
2915 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002916 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002918 if (cmdidx == CMD_let)
2919 {
2920 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002921 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002922 {
2923 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002924 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002925 {
2926 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002927 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002928 if (vim_iswhite(*p))
2929 break;
2930 }
2931 return;
2932 }
2933 }
2934 else
2935 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2936 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 while ((xp->xp_pattern = vim_strpbrk(arg,
2938 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2939 {
2940 c = *xp->xp_pattern;
2941 if (c == '&')
2942 {
2943 c = xp->xp_pattern[1];
2944 if (c == '&')
2945 {
2946 ++xp->xp_pattern;
2947 xp->xp_context = cmdidx != CMD_let || got_eq
2948 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2949 }
2950 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002953 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2954 xp->xp_pattern += 2;
2955
2956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 }
2958 else if (c == '$')
2959 {
2960 /* environment variable */
2961 xp->xp_context = EXPAND_ENV_VARS;
2962 }
2963 else if (c == '=')
2964 {
2965 got_eq = TRUE;
2966 xp->xp_context = EXPAND_EXPRESSION;
2967 }
2968 else if (c == '<'
2969 && xp->xp_context == EXPAND_FUNCTIONS
2970 && vim_strchr(xp->xp_pattern, '(') == NULL)
2971 {
2972 /* Function name can start with "<SNR>" */
2973 break;
2974 }
2975 else if (cmdidx != CMD_let || got_eq)
2976 {
2977 if (c == '"') /* string */
2978 {
2979 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2980 if (c == '\\' && xp->xp_pattern[1] != NUL)
2981 ++xp->xp_pattern;
2982 xp->xp_context = EXPAND_NOTHING;
2983 }
2984 else if (c == '\'') /* literal string */
2985 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002986 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2988 /* skip */ ;
2989 xp->xp_context = EXPAND_NOTHING;
2990 }
2991 else if (c == '|')
2992 {
2993 if (xp->xp_pattern[1] == '|')
2994 {
2995 ++xp->xp_pattern;
2996 xp->xp_context = EXPAND_EXPRESSION;
2997 }
2998 else
2999 xp->xp_context = EXPAND_COMMANDS;
3000 }
3001 else
3002 xp->xp_context = EXPAND_EXPRESSION;
3003 }
3004 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003005 /* Doesn't look like something valid, expand as an expression
3006 * anyway. */
3007 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 arg = xp->xp_pattern;
3009 if (*arg != NUL)
3010 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3011 /* skip */ ;
3012 }
3013 xp->xp_pattern = arg;
3014}
3015
3016#endif /* FEAT_CMDL_COMPL */
3017
3018/*
3019 * ":1,25call func(arg1, arg2)" function call.
3020 */
3021 void
3022ex_call(eap)
3023 exarg_T *eap;
3024{
3025 char_u *arg = eap->arg;
3026 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003028 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003030 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 linenr_T lnum;
3032 int doesrange;
3033 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003034 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003036 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3037 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003038 if (tofree == NULL)
3039 return;
3040
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003041 /* Increase refcount on dictionary, it could get deleted when evaluating
3042 * the arguments. */
3043 if (fudi.fd_dict != NULL)
3044 ++fudi.fd_dict->dv_refcount;
3045
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003046 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3047 len = STRLEN(tofree);
3048 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049
Bram Moolenaar532c7802005-01-27 14:44:31 +00003050 /* Skip white space to allow ":call func ()". Not good, but required for
3051 * backward compatibility. */
3052 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003053 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
3055 if (*startarg != '(')
3056 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003057 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 goto end;
3059 }
3060
3061 /*
3062 * When skipping, evaluate the function once, to find the end of the
3063 * arguments.
3064 * When the function takes a range, this is discovered after the first
3065 * call, and the loop is broken.
3066 */
3067 if (eap->skip)
3068 {
3069 ++emsg_skip;
3070 lnum = eap->line2; /* do it once, also with an invalid range */
3071 }
3072 else
3073 lnum = eap->line1;
3074 for ( ; lnum <= eap->line2; ++lnum)
3075 {
3076 if (!eap->skip && eap->addr_count > 0)
3077 {
3078 curwin->w_cursor.lnum = lnum;
3079 curwin->w_cursor.col = 0;
3080 }
3081 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003083 eap->line1, eap->line2, &doesrange,
3084 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 {
3086 failed = TRUE;
3087 break;
3088 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003089 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 if (doesrange || eap->skip)
3091 break;
3092 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003093 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003094 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003095 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 if (aborting())
3097 break;
3098 }
3099 if (eap->skip)
3100 --emsg_skip;
3101
3102 if (!failed)
3103 {
3104 /* Check for trailing illegal characters and a following command. */
3105 if (!ends_excmd(*arg))
3106 {
3107 emsg_severe = TRUE;
3108 EMSG(_(e_trailing));
3109 }
3110 else
3111 eap->nextcmd = check_nextcmd(arg);
3112 }
3113
3114end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003115 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003116 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117}
3118
3119/*
3120 * ":unlet[!] var1 ... " command.
3121 */
3122 void
3123ex_unlet(eap)
3124 exarg_T *eap;
3125{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003126 ex_unletlock(eap, eap->arg, 0);
3127}
3128
3129/*
3130 * ":lockvar" and ":unlockvar" commands
3131 */
3132 void
3133ex_lockvar(eap)
3134 exarg_T *eap;
3135{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003137 int deep = 2;
3138
3139 if (eap->forceit)
3140 deep = -1;
3141 else if (vim_isdigit(*arg))
3142 {
3143 deep = getdigits(&arg);
3144 arg = skipwhite(arg);
3145 }
3146
3147 ex_unletlock(eap, arg, deep);
3148}
3149
3150/*
3151 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3152 */
3153 static void
3154ex_unletlock(eap, argstart, deep)
3155 exarg_T *eap;
3156 char_u *argstart;
3157 int deep;
3158{
3159 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003162 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
3164 do
3165 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003166 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003167 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3168 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003169 if (lv.ll_name == NULL)
3170 error = TRUE; /* error but continue parsing */
3171 if (name_end == NULL || (!vim_iswhite(*name_end)
3172 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003174 if (name_end != NULL)
3175 {
3176 emsg_severe = TRUE;
3177 EMSG(_(e_trailing));
3178 }
3179 if (!(eap->skip || error))
3180 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 break;
3182 }
3183
3184 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003185 {
3186 if (eap->cmdidx == CMD_unlet)
3187 {
3188 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3189 error = TRUE;
3190 }
3191 else
3192 {
3193 if (do_lock_var(&lv, name_end, deep,
3194 eap->cmdidx == CMD_lockvar) == FAIL)
3195 error = TRUE;
3196 }
3197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003199 if (!eap->skip)
3200 clear_lval(&lv);
3201
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 arg = skipwhite(name_end);
3203 } while (!ends_excmd(*arg));
3204
3205 eap->nextcmd = check_nextcmd(arg);
3206}
3207
Bram Moolenaar8c711452005-01-14 21:53:12 +00003208 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003209do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003210 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003211 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003212 int forceit;
3213{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003214 int ret = OK;
3215 int cc;
3216
3217 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003218 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003219 cc = *name_end;
3220 *name_end = NUL;
3221
3222 /* Normal name or expanded name. */
3223 if (check_changedtick(lp->ll_name))
3224 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003225 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003226 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003227 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003228 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003229 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3230 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003231 else if (lp->ll_range)
3232 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003234
3235 /* Delete a range of List items. */
3236 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3237 {
3238 li = lp->ll_li->li_next;
3239 listitem_remove(lp->ll_list, lp->ll_li);
3240 lp->ll_li = li;
3241 ++lp->ll_n1;
3242 }
3243 }
3244 else
3245 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003246 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003247 /* unlet a List item. */
3248 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003249 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003250 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003251 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003252 }
3253
3254 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003255}
3256
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257/*
3258 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003259 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 */
3261 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003262do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003264 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
Bram Moolenaar33570922005-01-25 22:26:29 +00003266 hashtab_T *ht;
3267 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003268 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
Bram Moolenaar33570922005-01-25 22:26:29 +00003270 ht = find_var_ht(name, &varname);
3271 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003273 hi = hash_find(ht, varname);
3274 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003275 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003276 if (var_check_ro(HI2DI(hi)->di_flags, name))
3277 return FAIL;
3278 delete_var(ht, hi);
3279 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003282 if (forceit)
3283 return OK;
3284 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 return FAIL;
3286}
3287
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003288/*
3289 * Lock or unlock variable indicated by "lp".
3290 * "deep" is the levels to go (-1 for unlimited);
3291 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3292 */
3293 static int
3294do_lock_var(lp, name_end, deep, lock)
3295 lval_T *lp;
3296 char_u *name_end;
3297 int deep;
3298 int lock;
3299{
3300 int ret = OK;
3301 int cc;
3302 dictitem_T *di;
3303
3304 if (deep == 0) /* nothing to do */
3305 return OK;
3306
3307 if (lp->ll_tv == NULL)
3308 {
3309 cc = *name_end;
3310 *name_end = NUL;
3311
3312 /* Normal name or expanded name. */
3313 if (check_changedtick(lp->ll_name))
3314 ret = FAIL;
3315 else
3316 {
3317 di = find_var(lp->ll_name, NULL);
3318 if (di == NULL)
3319 ret = FAIL;
3320 else
3321 {
3322 if (lock)
3323 di->di_flags |= DI_FLAGS_LOCK;
3324 else
3325 di->di_flags &= ~DI_FLAGS_LOCK;
3326 item_lock(&di->di_tv, deep, lock);
3327 }
3328 }
3329 *name_end = cc;
3330 }
3331 else if (lp->ll_range)
3332 {
3333 listitem_T *li = lp->ll_li;
3334
3335 /* (un)lock a range of List items. */
3336 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3337 {
3338 item_lock(&li->li_tv, deep, lock);
3339 li = li->li_next;
3340 ++lp->ll_n1;
3341 }
3342 }
3343 else if (lp->ll_list != NULL)
3344 /* (un)lock a List item. */
3345 item_lock(&lp->ll_li->li_tv, deep, lock);
3346 else
3347 /* un(lock) a Dictionary item. */
3348 item_lock(&lp->ll_di->di_tv, deep, lock);
3349
3350 return ret;
3351}
3352
3353/*
3354 * Lock or unlock an item. "deep" is nr of levels to go.
3355 */
3356 static void
3357item_lock(tv, deep, lock)
3358 typval_T *tv;
3359 int deep;
3360 int lock;
3361{
3362 static int recurse = 0;
3363 list_T *l;
3364 listitem_T *li;
3365 dict_T *d;
3366 hashitem_T *hi;
3367 int todo;
3368
3369 if (recurse >= DICT_MAXNEST)
3370 {
3371 EMSG(_("E743: variable nested too deep for (un)lock"));
3372 return;
3373 }
3374 if (deep == 0)
3375 return;
3376 ++recurse;
3377
3378 /* lock/unlock the item itself */
3379 if (lock)
3380 tv->v_lock |= VAR_LOCKED;
3381 else
3382 tv->v_lock &= ~VAR_LOCKED;
3383
3384 switch (tv->v_type)
3385 {
3386 case VAR_LIST:
3387 if ((l = tv->vval.v_list) != NULL)
3388 {
3389 if (lock)
3390 l->lv_lock |= VAR_LOCKED;
3391 else
3392 l->lv_lock &= ~VAR_LOCKED;
3393 if (deep < 0 || deep > 1)
3394 /* recursive: lock/unlock the items the List contains */
3395 for (li = l->lv_first; li != NULL; li = li->li_next)
3396 item_lock(&li->li_tv, deep - 1, lock);
3397 }
3398 break;
3399 case VAR_DICT:
3400 if ((d = tv->vval.v_dict) != NULL)
3401 {
3402 if (lock)
3403 d->dv_lock |= VAR_LOCKED;
3404 else
3405 d->dv_lock &= ~VAR_LOCKED;
3406 if (deep < 0 || deep > 1)
3407 {
3408 /* recursive: lock/unlock the items the List contains */
3409 todo = d->dv_hashtab.ht_used;
3410 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3411 {
3412 if (!HASHITEM_EMPTY(hi))
3413 {
3414 --todo;
3415 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3416 }
3417 }
3418 }
3419 }
3420 }
3421 --recurse;
3422}
3423
Bram Moolenaara40058a2005-07-11 22:42:07 +00003424/*
3425 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3426 * it refers to a List or Dictionary that is locked.
3427 */
3428 static int
3429tv_islocked(tv)
3430 typval_T *tv;
3431{
3432 return (tv->v_lock & VAR_LOCKED)
3433 || (tv->v_type == VAR_LIST
3434 && tv->vval.v_list != NULL
3435 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3436 || (tv->v_type == VAR_DICT
3437 && tv->vval.v_dict != NULL
3438 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3439}
3440
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3442/*
3443 * Delete all "menutrans_" variables.
3444 */
3445 void
3446del_menutrans_vars()
3447{
Bram Moolenaar33570922005-01-25 22:26:29 +00003448 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003449 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
Bram Moolenaar33570922005-01-25 22:26:29 +00003451 hash_lock(&globvarht);
3452 todo = globvarht.ht_used;
3453 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003454 {
3455 if (!HASHITEM_EMPTY(hi))
3456 {
3457 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003458 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3459 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003460 }
3461 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003462 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463}
3464#endif
3465
3466#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3467
3468/*
3469 * Local string buffer for the next two functions to store a variable name
3470 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3471 * get_user_var_name().
3472 */
3473
3474static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3475
3476static char_u *varnamebuf = NULL;
3477static int varnamebuflen = 0;
3478
3479/*
3480 * Function to concatenate a prefix and a variable name.
3481 */
3482 static char_u *
3483cat_prefix_varname(prefix, name)
3484 int prefix;
3485 char_u *name;
3486{
3487 int len;
3488
3489 len = (int)STRLEN(name) + 3;
3490 if (len > varnamebuflen)
3491 {
3492 vim_free(varnamebuf);
3493 len += 10; /* some additional space */
3494 varnamebuf = alloc(len);
3495 if (varnamebuf == NULL)
3496 {
3497 varnamebuflen = 0;
3498 return NULL;
3499 }
3500 varnamebuflen = len;
3501 }
3502 *varnamebuf = prefix;
3503 varnamebuf[1] = ':';
3504 STRCPY(varnamebuf + 2, name);
3505 return varnamebuf;
3506}
3507
3508/*
3509 * Function given to ExpandGeneric() to obtain the list of user defined
3510 * (global/buffer/window/built-in) variable names.
3511 */
3512/*ARGSUSED*/
3513 char_u *
3514get_user_var_name(xp, idx)
3515 expand_T *xp;
3516 int idx;
3517{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003518 static long_u gdone;
3519 static long_u bdone;
3520 static long_u wdone;
3521 static int vidx;
3522 static hashitem_T *hi;
3523 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
3525 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003526 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003527
3528 /* Global variables */
3529 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003531 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003532 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003533 else
3534 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003535 while (HASHITEM_EMPTY(hi))
3536 ++hi;
3537 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3538 return cat_prefix_varname('g', hi->hi_key);
3539 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003541
3542 /* b: variables */
3543 ht = &curbuf->b_vars.dv_hashtab;
3544 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003546 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003547 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003548 else
3549 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003550 while (HASHITEM_EMPTY(hi))
3551 ++hi;
3552 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003554 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003556 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 return (char_u *)"b:changedtick";
3558 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003559
3560 /* w: variables */
3561 ht = &curwin->w_vars.dv_hashtab;
3562 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003564 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003565 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003566 else
3567 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003568 while (HASHITEM_EMPTY(hi))
3569 ++hi;
3570 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003572
3573 /* v: variables */
3574 if (vidx < VV_LEN)
3575 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
3577 vim_free(varnamebuf);
3578 varnamebuf = NULL;
3579 varnamebuflen = 0;
3580 return NULL;
3581}
3582
3583#endif /* FEAT_CMDL_COMPL */
3584
3585/*
3586 * types for expressions.
3587 */
3588typedef enum
3589{
3590 TYPE_UNKNOWN = 0
3591 , TYPE_EQUAL /* == */
3592 , TYPE_NEQUAL /* != */
3593 , TYPE_GREATER /* > */
3594 , TYPE_GEQUAL /* >= */
3595 , TYPE_SMALLER /* < */
3596 , TYPE_SEQUAL /* <= */
3597 , TYPE_MATCH /* =~ */
3598 , TYPE_NOMATCH /* !~ */
3599} exptype_T;
3600
3601/*
3602 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003603 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3605 */
3606
3607/*
3608 * Handle zero level expression.
3609 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003610 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003611 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 * Return OK or FAIL.
3613 */
3614 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003615eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 char_u **nextcmd;
3619 int evaluate;
3620{
3621 int ret;
3622 char_u *p;
3623
3624 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003625 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 if (ret == FAIL || !ends_excmd(*p))
3627 {
3628 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003629 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 /*
3631 * Report the invalid expression unless the expression evaluation has
3632 * been cancelled due to an aborting error, an interrupt, or an
3633 * exception.
3634 */
3635 if (!aborting())
3636 EMSG2(_(e_invexpr2), arg);
3637 ret = FAIL;
3638 }
3639 if (nextcmd != NULL)
3640 *nextcmd = check_nextcmd(p);
3641
3642 return ret;
3643}
3644
3645/*
3646 * Handle top level expression:
3647 * expr1 ? expr0 : expr0
3648 *
3649 * "arg" must point to the first non-white of the expression.
3650 * "arg" is advanced to the next non-white after the recognized expression.
3651 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003652 * Note: "rettv.v_lock" is not set.
3653 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 * Return OK or FAIL.
3655 */
3656 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003657eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 int evaluate;
3661{
3662 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003663 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
3665 /*
3666 * Get the first variable.
3667 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003668 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 return FAIL;
3670
3671 if ((*arg)[0] == '?')
3672 {
3673 result = FALSE;
3674 if (evaluate)
3675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003676 int error = FALSE;
3677
3678 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003680 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003681 if (error)
3682 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684
3685 /*
3686 * Get the second variable.
3687 */
3688 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003689 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 return FAIL;
3691
3692 /*
3693 * Check for the ":".
3694 */
3695 if ((*arg)[0] != ':')
3696 {
3697 EMSG(_("E109: Missing ':' after '?'"));
3698 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003699 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 return FAIL;
3701 }
3702
3703 /*
3704 * Get the third variable.
3705 */
3706 *arg = skipwhite(*arg + 1);
3707 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3708 {
3709 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003710 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 return FAIL;
3712 }
3713 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003714 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716
3717 return OK;
3718}
3719
3720/*
3721 * Handle first level expression:
3722 * expr2 || expr2 || expr2 logical OR
3723 *
3724 * "arg" must point to the first non-white of the expression.
3725 * "arg" is advanced to the next non-white after the recognized expression.
3726 *
3727 * Return OK or FAIL.
3728 */
3729 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003730eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 int evaluate;
3734{
Bram Moolenaar33570922005-01-25 22:26:29 +00003735 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 long result;
3737 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003738 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739
3740 /*
3741 * Get the first variable.
3742 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003743 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 return FAIL;
3745
3746 /*
3747 * Repeat until there is no following "||".
3748 */
3749 first = TRUE;
3750 result = FALSE;
3751 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3752 {
3753 if (evaluate && first)
3754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003755 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003757 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003758 if (error)
3759 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 first = FALSE;
3761 }
3762
3763 /*
3764 * Get the second variable.
3765 */
3766 *arg = skipwhite(*arg + 2);
3767 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3768 return FAIL;
3769
3770 /*
3771 * Compute the result.
3772 */
3773 if (evaluate && !result)
3774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003775 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003777 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003778 if (error)
3779 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 }
3781 if (evaluate)
3782 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003783 rettv->v_type = VAR_NUMBER;
3784 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
3786 }
3787
3788 return OK;
3789}
3790
3791/*
3792 * Handle second level expression:
3793 * expr3 && expr3 && expr3 logical AND
3794 *
3795 * "arg" must point to the first non-white of the expression.
3796 * "arg" is advanced to the next non-white after the recognized expression.
3797 *
3798 * Return OK or FAIL.
3799 */
3800 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003801eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 int evaluate;
3805{
Bram Moolenaar33570922005-01-25 22:26:29 +00003806 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 long result;
3808 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003809 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
3811 /*
3812 * Get the first variable.
3813 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003814 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 return FAIL;
3816
3817 /*
3818 * Repeat until there is no following "&&".
3819 */
3820 first = TRUE;
3821 result = TRUE;
3822 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3823 {
3824 if (evaluate && first)
3825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003826 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003828 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003829 if (error)
3830 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 first = FALSE;
3832 }
3833
3834 /*
3835 * Get the second variable.
3836 */
3837 *arg = skipwhite(*arg + 2);
3838 if (eval4(arg, &var2, evaluate && result) == FAIL)
3839 return FAIL;
3840
3841 /*
3842 * Compute the result.
3843 */
3844 if (evaluate && result)
3845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003846 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003848 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003849 if (error)
3850 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852 if (evaluate)
3853 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003854 rettv->v_type = VAR_NUMBER;
3855 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
3857 }
3858
3859 return OK;
3860}
3861
3862/*
3863 * Handle third level expression:
3864 * var1 == var2
3865 * var1 =~ var2
3866 * var1 != var2
3867 * var1 !~ var2
3868 * var1 > var2
3869 * var1 >= var2
3870 * var1 < var2
3871 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003872 * var1 is var2
3873 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 *
3875 * "arg" must point to the first non-white of the expression.
3876 * "arg" is advanced to the next non-white after the recognized expression.
3877 *
3878 * Return OK or FAIL.
3879 */
3880 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003881eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 int evaluate;
3885{
Bram Moolenaar33570922005-01-25 22:26:29 +00003886 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 char_u *p;
3888 int i;
3889 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003890 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 int len = 2;
3892 long n1, n2;
3893 char_u *s1, *s2;
3894 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3895 regmatch_T regmatch;
3896 int ic;
3897 char_u *save_cpo;
3898
3899 /*
3900 * Get the first variable.
3901 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003902 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 return FAIL;
3904
3905 p = *arg;
3906 switch (p[0])
3907 {
3908 case '=': if (p[1] == '=')
3909 type = TYPE_EQUAL;
3910 else if (p[1] == '~')
3911 type = TYPE_MATCH;
3912 break;
3913 case '!': if (p[1] == '=')
3914 type = TYPE_NEQUAL;
3915 else if (p[1] == '~')
3916 type = TYPE_NOMATCH;
3917 break;
3918 case '>': if (p[1] != '=')
3919 {
3920 type = TYPE_GREATER;
3921 len = 1;
3922 }
3923 else
3924 type = TYPE_GEQUAL;
3925 break;
3926 case '<': if (p[1] != '=')
3927 {
3928 type = TYPE_SMALLER;
3929 len = 1;
3930 }
3931 else
3932 type = TYPE_SEQUAL;
3933 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003934 case 'i': if (p[1] == 's')
3935 {
3936 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3937 len = 5;
3938 if (!vim_isIDc(p[len]))
3939 {
3940 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3941 type_is = TRUE;
3942 }
3943 }
3944 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
3946
3947 /*
3948 * If there is a comparitive operator, use it.
3949 */
3950 if (type != TYPE_UNKNOWN)
3951 {
3952 /* extra question mark appended: ignore case */
3953 if (p[len] == '?')
3954 {
3955 ic = TRUE;
3956 ++len;
3957 }
3958 /* extra '#' appended: match case */
3959 else if (p[len] == '#')
3960 {
3961 ic = FALSE;
3962 ++len;
3963 }
3964 /* nothing appened: use 'ignorecase' */
3965 else
3966 ic = p_ic;
3967
3968 /*
3969 * Get the second variable.
3970 */
3971 *arg = skipwhite(p + len);
3972 if (eval5(arg, &var2, evaluate) == FAIL)
3973 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003974 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 return FAIL;
3976 }
3977
3978 if (evaluate)
3979 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003980 if (type_is && rettv->v_type != var2.v_type)
3981 {
3982 /* For "is" a different type always means FALSE, for "notis"
3983 * it means TRUE. */
3984 n1 = (type == TYPE_NEQUAL);
3985 }
3986 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3987 {
3988 if (type_is)
3989 {
3990 n1 = (rettv->v_type == var2.v_type
3991 && rettv->vval.v_list == var2.vval.v_list);
3992 if (type == TYPE_NEQUAL)
3993 n1 = !n1;
3994 }
3995 else if (rettv->v_type != var2.v_type
3996 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3997 {
3998 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003999 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004000 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004001 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004002 clear_tv(rettv);
4003 clear_tv(&var2);
4004 return FAIL;
4005 }
4006 else
4007 {
4008 /* Compare two Lists for being equal or unequal. */
4009 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4010 if (type == TYPE_NEQUAL)
4011 n1 = !n1;
4012 }
4013 }
4014
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004015 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4016 {
4017 if (type_is)
4018 {
4019 n1 = (rettv->v_type == var2.v_type
4020 && rettv->vval.v_dict == var2.vval.v_dict);
4021 if (type == TYPE_NEQUAL)
4022 n1 = !n1;
4023 }
4024 else if (rettv->v_type != var2.v_type
4025 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4026 {
4027 if (rettv->v_type != var2.v_type)
4028 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4029 else
4030 EMSG(_("E736: Invalid operation for Dictionary"));
4031 clear_tv(rettv);
4032 clear_tv(&var2);
4033 return FAIL;
4034 }
4035 else
4036 {
4037 /* Compare two Dictionaries for being equal or unequal. */
4038 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4039 if (type == TYPE_NEQUAL)
4040 n1 = !n1;
4041 }
4042 }
4043
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004044 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4045 {
4046 if (rettv->v_type != var2.v_type
4047 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4048 {
4049 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004050 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004051 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004052 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004053 clear_tv(rettv);
4054 clear_tv(&var2);
4055 return FAIL;
4056 }
4057 else
4058 {
4059 /* Compare two Funcrefs for being equal or unequal. */
4060 if (rettv->vval.v_string == NULL
4061 || var2.vval.v_string == NULL)
4062 n1 = FALSE;
4063 else
4064 n1 = STRCMP(rettv->vval.v_string,
4065 var2.vval.v_string) == 0;
4066 if (type == TYPE_NEQUAL)
4067 n1 = !n1;
4068 }
4069 }
4070
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 /*
4072 * If one of the two variables is a number, compare as a number.
4073 * When using "=~" or "!~", always compare as string.
4074 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004075 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004078 n1 = get_tv_number(rettv);
4079 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 switch (type)
4081 {
4082 case TYPE_EQUAL: n1 = (n1 == n2); break;
4083 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4084 case TYPE_GREATER: n1 = (n1 > n2); break;
4085 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4086 case TYPE_SMALLER: n1 = (n1 < n2); break;
4087 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4088 case TYPE_UNKNOWN:
4089 case TYPE_MATCH:
4090 case TYPE_NOMATCH: break; /* avoid gcc warning */
4091 }
4092 }
4093 else
4094 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004095 s1 = get_tv_string_buf(rettv, buf1);
4096 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4098 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4099 else
4100 i = 0;
4101 n1 = FALSE;
4102 switch (type)
4103 {
4104 case TYPE_EQUAL: n1 = (i == 0); break;
4105 case TYPE_NEQUAL: n1 = (i != 0); break;
4106 case TYPE_GREATER: n1 = (i > 0); break;
4107 case TYPE_GEQUAL: n1 = (i >= 0); break;
4108 case TYPE_SMALLER: n1 = (i < 0); break;
4109 case TYPE_SEQUAL: n1 = (i <= 0); break;
4110
4111 case TYPE_MATCH:
4112 case TYPE_NOMATCH:
4113 /* avoid 'l' flag in 'cpoptions' */
4114 save_cpo = p_cpo;
4115 p_cpo = (char_u *)"";
4116 regmatch.regprog = vim_regcomp(s2,
4117 RE_MAGIC + RE_STRING);
4118 regmatch.rm_ic = ic;
4119 if (regmatch.regprog != NULL)
4120 {
4121 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4122 vim_free(regmatch.regprog);
4123 if (type == TYPE_NOMATCH)
4124 n1 = !n1;
4125 }
4126 p_cpo = save_cpo;
4127 break;
4128
4129 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4130 }
4131 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 clear_tv(rettv);
4133 clear_tv(&var2);
4134 rettv->v_type = VAR_NUMBER;
4135 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
4137 }
4138
4139 return OK;
4140}
4141
4142/*
4143 * Handle fourth level expression:
4144 * + number addition
4145 * - number subtraction
4146 * . string concatenation
4147 *
4148 * "arg" must point to the first non-white of the expression.
4149 * "arg" is advanced to the next non-white after the recognized expression.
4150 *
4151 * Return OK or FAIL.
4152 */
4153 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 int evaluate;
4158{
Bram Moolenaar33570922005-01-25 22:26:29 +00004159 typval_T var2;
4160 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 int op;
4162 long n1, n2;
4163 char_u *s1, *s2;
4164 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4165 char_u *p;
4166
4167 /*
4168 * Get the first variable.
4169 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 return FAIL;
4172
4173 /*
4174 * Repeat computing, until no '+', '-' or '.' is following.
4175 */
4176 for (;;)
4177 {
4178 op = **arg;
4179 if (op != '+' && op != '-' && op != '.')
4180 break;
4181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 if (op != '+' || rettv->v_type != VAR_LIST)
4183 {
4184 /* For "list + ...", an illegal use of the first operand as
4185 * a number cannot be determined before evaluating the 2nd
4186 * operand: if this is also a list, all is ok.
4187 * For "something . ...", "something - ..." or "non-list + ...",
4188 * we know that the first operand needs to be a string or number
4189 * without evaluating the 2nd operand. So check before to avoid
4190 * side effects after an error. */
4191 if (evaluate && get_tv_string_chk(rettv) == NULL)
4192 {
4193 clear_tv(rettv);
4194 return FAIL;
4195 }
4196 }
4197
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 /*
4199 * Get the second variable.
4200 */
4201 *arg = skipwhite(*arg + 1);
4202 if (eval6(arg, &var2, evaluate) == FAIL)
4203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 return FAIL;
4206 }
4207
4208 if (evaluate)
4209 {
4210 /*
4211 * Compute the result.
4212 */
4213 if (op == '.')
4214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004215 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4216 s2 = get_tv_string_buf_chk(&var2, buf2);
4217 if (s2 == NULL) /* type error ? */
4218 {
4219 clear_tv(rettv);
4220 clear_tv(&var2);
4221 return FAIL;
4222 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004223 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 clear_tv(rettv);
4225 rettv->v_type = VAR_STRING;
4226 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004228 else if (op == '+' && rettv->v_type == VAR_LIST
4229 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004230 {
4231 /* concatenate Lists */
4232 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4233 &var3) == FAIL)
4234 {
4235 clear_tv(rettv);
4236 clear_tv(&var2);
4237 return FAIL;
4238 }
4239 clear_tv(rettv);
4240 *rettv = var3;
4241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 else
4243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 int error = FALSE;
4245
4246 n1 = get_tv_number_chk(rettv, &error);
4247 if (error)
4248 {
4249 /* This can only happen for "list + non-list".
4250 * For "non-list + ..." or "something - ...", we returned
4251 * before evaluating the 2nd operand. */
4252 clear_tv(rettv);
4253 return FAIL;
4254 }
4255 n2 = get_tv_number_chk(&var2, &error);
4256 if (error)
4257 {
4258 clear_tv(rettv);
4259 clear_tv(&var2);
4260 return FAIL;
4261 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (op == '+')
4264 n1 = n1 + n2;
4265 else
4266 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267 rettv->v_type = VAR_NUMBER;
4268 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272 }
4273 return OK;
4274}
4275
4276/*
4277 * Handle fifth level expression:
4278 * * number multiplication
4279 * / number division
4280 * % number modulo
4281 *
4282 * "arg" must point to the first non-white of the expression.
4283 * "arg" is advanced to the next non-white after the recognized expression.
4284 *
4285 * Return OK or FAIL.
4286 */
4287 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 int evaluate;
4292{
Bram Moolenaar33570922005-01-25 22:26:29 +00004293 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 int op;
4295 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004296 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297
4298 /*
4299 * Get the first variable.
4300 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004301 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 return FAIL;
4303
4304 /*
4305 * Repeat computing, until no '*', '/' or '%' is following.
4306 */
4307 for (;;)
4308 {
4309 op = **arg;
4310 if (op != '*' && op != '/' && op != '%')
4311 break;
4312
4313 if (evaluate)
4314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004315 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 if (error)
4318 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 }
4320 else
4321 n1 = 0;
4322
4323 /*
4324 * Get the second variable.
4325 */
4326 *arg = skipwhite(*arg + 1);
4327 if (eval7(arg, &var2, evaluate) == FAIL)
4328 return FAIL;
4329
4330 if (evaluate)
4331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004333 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004334 if (error)
4335 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336
4337 /*
4338 * Compute the result.
4339 */
4340 if (op == '*')
4341 n1 = n1 * n2;
4342 else if (op == '/')
4343 {
4344 if (n2 == 0) /* give an error message? */
4345 n1 = 0x7fffffffL;
4346 else
4347 n1 = n1 / n2;
4348 }
4349 else
4350 {
4351 if (n2 == 0) /* give an error message? */
4352 n1 = 0;
4353 else
4354 n1 = n1 % n2;
4355 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 rettv->v_type = VAR_NUMBER;
4357 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359 }
4360
4361 return OK;
4362}
4363
4364/*
4365 * Handle sixth level expression:
4366 * number number constant
4367 * "string" string contstant
4368 * 'string' literal string contstant
4369 * &option-name option value
4370 * @r register contents
4371 * identifier variable value
4372 * function() function call
4373 * $VAR environment variable
4374 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004375 * [expr, expr] List
4376 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 *
4378 * Also handle:
4379 * ! in front logical NOT
4380 * - in front unary minus
4381 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004382 * trailing [] subscript in String or List
4383 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 *
4385 * "arg" must point to the first non-white of the expression.
4386 * "arg" is advanced to the next non-white after the recognized expression.
4387 *
4388 * Return OK or FAIL.
4389 */
4390 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 int evaluate;
4395{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 long n;
4397 int len;
4398 char_u *s;
4399 int val;
4400 char_u *start_leader, *end_leader;
4401 int ret = OK;
4402 char_u *alias;
4403
4404 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004405 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004406 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
4410 /*
4411 * Skip '!' and '-' characters. They are handled later.
4412 */
4413 start_leader = *arg;
4414 while (**arg == '!' || **arg == '-' || **arg == '+')
4415 *arg = skipwhite(*arg + 1);
4416 end_leader = *arg;
4417
4418 switch (**arg)
4419 {
4420 /*
4421 * Number constant.
4422 */
4423 case '0':
4424 case '1':
4425 case '2':
4426 case '3':
4427 case '4':
4428 case '5':
4429 case '6':
4430 case '7':
4431 case '8':
4432 case '9':
4433 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4434 *arg += len;
4435 if (evaluate)
4436 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 rettv->v_type = VAR_NUMBER;
4438 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440 break;
4441
4442 /*
4443 * String constant: "string".
4444 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004445 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 break;
4447
4448 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004449 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004451 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004452 break;
4453
4454 /*
4455 * List: [expr, expr]
4456 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004457 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 break;
4459
4460 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004461 * Dictionary: {key: val, key: val}
4462 */
4463 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4464 break;
4465
4466 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004467 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004469 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 break;
4471
4472 /*
4473 * Environment variable: $VAR.
4474 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 break;
4477
4478 /*
4479 * Register contents: @r.
4480 */
4481 case '@': ++*arg;
4482 if (evaluate)
4483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004484 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004485 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487 if (**arg != NUL)
4488 ++*arg;
4489 break;
4490
4491 /*
4492 * nested expression: (expression).
4493 */
4494 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004495 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 if (**arg == ')')
4497 ++*arg;
4498 else if (ret == OK)
4499 {
4500 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004501 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 ret = FAIL;
4503 }
4504 break;
4505
Bram Moolenaar8c711452005-01-14 21:53:12 +00004506 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 break;
4508 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004509
4510 if (ret == NOTDONE)
4511 {
4512 /*
4513 * Must be a variable or function name.
4514 * Can also be a curly-braces kind of name: {expr}.
4515 */
4516 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004517 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004518 if (alias != NULL)
4519 s = alias;
4520
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004521 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004522 ret = FAIL;
4523 else
4524 {
4525 if (**arg == '(') /* recursive! */
4526 {
4527 /* If "s" is the name of a variable of type VAR_FUNC
4528 * use its contents. */
4529 s = deref_func_name(s, &len);
4530
4531 /* Invoke the function. */
4532 ret = get_func_tv(s, len, rettv, arg,
4533 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004534 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004535 /* Stop the expression evaluation when immediately
4536 * aborting on error, or when an interrupt occurred or
4537 * an exception was thrown but not caught. */
4538 if (aborting())
4539 {
4540 if (ret == OK)
4541 clear_tv(rettv);
4542 ret = FAIL;
4543 }
4544 }
4545 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004546 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004547 else
4548 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004549 }
4550
4551 if (alias != NULL)
4552 vim_free(alias);
4553 }
4554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 *arg = skipwhite(*arg);
4556
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004557 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4558 * expr(expr). */
4559 if (ret == OK)
4560 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
4562 /*
4563 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4564 */
4565 if (ret == OK && evaluate && end_leader > start_leader)
4566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004567 int error = FALSE;
4568
4569 val = get_tv_number_chk(rettv, &error);
4570 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004572 clear_tv(rettv);
4573 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004575 else
4576 {
4577 while (end_leader > start_leader)
4578 {
4579 --end_leader;
4580 if (*end_leader == '!')
4581 val = !val;
4582 else if (*end_leader == '-')
4583 val = -val;
4584 }
4585 clear_tv(rettv);
4586 rettv->v_type = VAR_NUMBER;
4587 rettv->vval.v_number = val;
4588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
4590
4591 return ret;
4592}
4593
4594/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004595 * Evaluate an "[expr]" or "[expr:expr]" index.
4596 * "*arg" points to the '['.
4597 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4598 */
4599 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004600eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004601 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004602 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004603 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004604 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004605{
4606 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004607 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004608 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004609 long len = -1;
4610 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004611 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004612 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004613
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004616 if (verbose)
4617 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004618 return FAIL;
4619 }
4620
Bram Moolenaar8c711452005-01-14 21:53:12 +00004621 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004623 /*
4624 * dict.name
4625 */
4626 key = *arg + 1;
4627 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4628 ;
4629 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004630 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004631 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632 }
4633 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004634 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004635 /*
4636 * something[idx]
4637 *
4638 * Get the (first) variable from inside the [].
4639 */
4640 *arg = skipwhite(*arg + 1);
4641 if (**arg == ':')
4642 empty1 = TRUE;
4643 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4644 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004645 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4646 {
4647 /* not a number or string */
4648 clear_tv(&var1);
4649 return FAIL;
4650 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004651
4652 /*
4653 * Get the second variable from inside the [:].
4654 */
4655 if (**arg == ':')
4656 {
4657 range = TRUE;
4658 *arg = skipwhite(*arg + 1);
4659 if (**arg == ']')
4660 empty2 = TRUE;
4661 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004663 if (!empty1)
4664 clear_tv(&var1);
4665 return FAIL;
4666 }
4667 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4668 {
4669 /* not a number or string */
4670 if (!empty1)
4671 clear_tv(&var1);
4672 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004673 return FAIL;
4674 }
4675 }
4676
4677 /* Check for the ']'. */
4678 if (**arg != ']')
4679 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004680 if (verbose)
4681 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004682 clear_tv(&var1);
4683 if (range)
4684 clear_tv(&var2);
4685 return FAIL;
4686 }
4687 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004688 }
4689
4690 if (evaluate)
4691 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004692 n1 = 0;
4693 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004695 n1 = get_tv_number(&var1);
4696 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004697 }
4698 if (range)
4699 {
4700 if (empty2)
4701 n2 = -1;
4702 else
4703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004704 n2 = get_tv_number(&var2);
4705 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004706 }
4707 }
4708
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004709 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004710 {
4711 case VAR_NUMBER:
4712 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004713 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714 len = (long)STRLEN(s);
4715 if (range)
4716 {
4717 /* The resulting variable is a substring. If the indexes
4718 * are out of range the result is empty. */
4719 if (n1 < 0)
4720 {
4721 n1 = len + n1;
4722 if (n1 < 0)
4723 n1 = 0;
4724 }
4725 if (n2 < 0)
4726 n2 = len + n2;
4727 else if (n2 >= len)
4728 n2 = len;
4729 if (n1 >= len || n2 < 0 || n1 > n2)
4730 s = NULL;
4731 else
4732 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4733 }
4734 else
4735 {
4736 /* The resulting variable is a string of a single
4737 * character. If the index is too big or negative the
4738 * result is empty. */
4739 if (n1 >= len || n1 < 0)
4740 s = NULL;
4741 else
4742 s = vim_strnsave(s + n1, 1);
4743 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 clear_tv(rettv);
4745 rettv->v_type = VAR_STRING;
4746 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 break;
4748
4749 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004750 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004751 if (n1 < 0)
4752 n1 = len + n1;
4753 if (!empty1 && (n1 < 0 || n1 >= len))
4754 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004755 if (verbose)
4756 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004757 return FAIL;
4758 }
4759 if (range)
4760 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004761 list_T *l;
4762 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004763
4764 if (n2 < 0)
4765 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004766 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004767 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004768 if (verbose)
4769 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770 return FAIL;
4771 }
4772 l = list_alloc();
4773 if (l == NULL)
4774 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004775 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 n1 <= n2; ++n1)
4777 {
4778 if (list_append_tv(l, &item->li_tv) == FAIL)
4779 {
4780 list_free(l);
4781 return FAIL;
4782 }
4783 item = item->li_next;
4784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004785 clear_tv(rettv);
4786 rettv->v_type = VAR_LIST;
4787 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004788 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004789 }
4790 else
4791 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004792 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004793 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004794 clear_tv(rettv);
4795 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004796 }
4797 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004798
4799 case VAR_DICT:
4800 if (range)
4801 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004802 if (verbose)
4803 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004804 if (len == -1)
4805 clear_tv(&var1);
4806 return FAIL;
4807 }
4808 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004809 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004810
4811 if (len == -1)
4812 {
4813 key = get_tv_string(&var1);
4814 if (*key == NUL)
4815 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004816 if (verbose)
4817 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004818 clear_tv(&var1);
4819 return FAIL;
4820 }
4821 }
4822
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004823 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004824
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004825 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004826 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004827 if (len == -1)
4828 clear_tv(&var1);
4829 if (item == NULL)
4830 return FAIL;
4831
4832 copy_tv(&item->di_tv, &var1);
4833 clear_tv(rettv);
4834 *rettv = var1;
4835 }
4836 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004837 }
4838 }
4839
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004840 return OK;
4841}
4842
4843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 * Get an option value.
4845 * "arg" points to the '&' or '+' before the option name.
4846 * "arg" is advanced to character after the option name.
4847 * Return OK or FAIL.
4848 */
4849 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004850get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004852 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 int evaluate;
4854{
4855 char_u *option_end;
4856 long numval;
4857 char_u *stringval;
4858 int opt_type;
4859 int c;
4860 int working = (**arg == '+'); /* has("+option") */
4861 int ret = OK;
4862 int opt_flags;
4863
4864 /*
4865 * Isolate the option name and find its value.
4866 */
4867 option_end = find_option_end(arg, &opt_flags);
4868 if (option_end == NULL)
4869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 EMSG2(_("E112: Option name missing: %s"), *arg);
4872 return FAIL;
4873 }
4874
4875 if (!evaluate)
4876 {
4877 *arg = option_end;
4878 return OK;
4879 }
4880
4881 c = *option_end;
4882 *option_end = NUL;
4883 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004884 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885
4886 if (opt_type == -3) /* invalid name */
4887 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004888 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 EMSG2(_("E113: Unknown option: %s"), *arg);
4890 ret = FAIL;
4891 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004892 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
4894 if (opt_type == -2) /* hidden string option */
4895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004896 rettv->v_type = VAR_STRING;
4897 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899 else if (opt_type == -1) /* hidden number option */
4900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004901 rettv->v_type = VAR_NUMBER;
4902 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904 else if (opt_type == 1) /* number option */
4905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004906 rettv->v_type = VAR_NUMBER;
4907 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
4909 else /* string option */
4910 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 rettv->v_type = VAR_STRING;
4912 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 }
4914 }
4915 else if (working && (opt_type == -2 || opt_type == -1))
4916 ret = FAIL;
4917
4918 *option_end = c; /* put back for error messages */
4919 *arg = option_end;
4920
4921 return ret;
4922}
4923
4924/*
4925 * Allocate a variable for a string constant.
4926 * Return OK or FAIL.
4927 */
4928 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 int evaluate;
4933{
4934 char_u *p;
4935 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 int extra = 0;
4937
4938 /*
4939 * Find the end of the string, skipping backslashed characters.
4940 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004941 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
4943 if (*p == '\\' && p[1] != NUL)
4944 {
4945 ++p;
4946 /* A "\<x>" form occupies at least 4 characters, and produces up
4947 * to 6 characters: reserve space for 2 extra */
4948 if (*p == '<')
4949 extra += 2;
4950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952
4953 if (*p != '"')
4954 {
4955 EMSG2(_("E114: Missing quote: %s"), *arg);
4956 return FAIL;
4957 }
4958
4959 /* If only parsing, set *arg and return here */
4960 if (!evaluate)
4961 {
4962 *arg = p + 1;
4963 return OK;
4964 }
4965
4966 /*
4967 * Copy the string into allocated memory, handling backslashed
4968 * characters.
4969 */
4970 name = alloc((unsigned)(p - *arg + extra));
4971 if (name == NULL)
4972 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004973 rettv->v_type = VAR_STRING;
4974 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 {
4978 if (*p == '\\')
4979 {
4980 switch (*++p)
4981 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 case 'b': *name++ = BS; ++p; break;
4983 case 'e': *name++ = ESC; ++p; break;
4984 case 'f': *name++ = FF; ++p; break;
4985 case 'n': *name++ = NL; ++p; break;
4986 case 'r': *name++ = CAR; ++p; break;
4987 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988
4989 case 'X': /* hex: "\x1", "\x12" */
4990 case 'x':
4991 case 'u': /* Unicode: "\u0023" */
4992 case 'U':
4993 if (vim_isxdigit(p[1]))
4994 {
4995 int n, nr;
4996 int c = toupper(*p);
4997
4998 if (c == 'X')
4999 n = 2;
5000 else
5001 n = 4;
5002 nr = 0;
5003 while (--n >= 0 && vim_isxdigit(p[1]))
5004 {
5005 ++p;
5006 nr = (nr << 4) + hex2nr(*p);
5007 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005008 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009#ifdef FEAT_MBYTE
5010 /* For "\u" store the number according to
5011 * 'encoding'. */
5012 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005013 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 else
5015#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 break;
5019
5020 /* octal: "\1", "\12", "\123" */
5021 case '0':
5022 case '1':
5023 case '2':
5024 case '3':
5025 case '4':
5026 case '5':
5027 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005028 case '7': *name = *p++ - '0';
5029 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 *name = (*name << 3) + *p++ - '0';
5032 if (*p >= '0' && *p <= '7')
5033 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 break;
5037
5038 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 if (extra != 0)
5041 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 break;
5044 }
5045 /* FALLTHROUGH */
5046
Bram Moolenaar8c711452005-01-14 21:53:12 +00005047 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 break;
5049 }
5050 }
5051 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005052 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 *arg = p + 1;
5057
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 return OK;
5059}
5060
5061/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 * Return OK or FAIL.
5064 */
5065 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005066get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 int evaluate;
5070{
5071 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005072 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005073 int reduce = 0;
5074
5075 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005076 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005077 */
5078 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5079 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005081 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005083 break;
5084 ++reduce;
5085 ++p;
5086 }
5087 }
5088
Bram Moolenaar8c711452005-01-14 21:53:12 +00005089 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005090 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005091 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005092 return FAIL;
5093 }
5094
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005096 if (!evaluate)
5097 {
5098 *arg = p + 1;
5099 return OK;
5100 }
5101
5102 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005104 */
5105 str = alloc((unsigned)((p - *arg) - reduce));
5106 if (str == NULL)
5107 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 rettv->v_type = VAR_STRING;
5109 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005112 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005113 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005114 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005115 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005116 break;
5117 ++p;
5118 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005120 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005122 *arg = p + 1;
5123
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005124 return OK;
5125}
5126
5127/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005128 * Allocate a variable for a List and fill it from "*arg".
5129 * Return OK or FAIL.
5130 */
5131 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005132get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005133 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005134 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005135 int evaluate;
5136{
Bram Moolenaar33570922005-01-25 22:26:29 +00005137 list_T *l = NULL;
5138 typval_T tv;
5139 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005140
5141 if (evaluate)
5142 {
5143 l = list_alloc();
5144 if (l == NULL)
5145 return FAIL;
5146 }
5147
5148 *arg = skipwhite(*arg + 1);
5149 while (**arg != ']' && **arg != NUL)
5150 {
5151 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5152 goto failret;
5153 if (evaluate)
5154 {
5155 item = listitem_alloc();
5156 if (item != NULL)
5157 {
5158 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005159 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005160 list_append(l, item);
5161 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005162 else
5163 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164 }
5165
5166 if (**arg == ']')
5167 break;
5168 if (**arg != ',')
5169 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005170 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005171 goto failret;
5172 }
5173 *arg = skipwhite(*arg + 1);
5174 }
5175
5176 if (**arg != ']')
5177 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179failret:
5180 if (evaluate)
5181 list_free(l);
5182 return FAIL;
5183 }
5184
5185 *arg = skipwhite(*arg + 1);
5186 if (evaluate)
5187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005188 rettv->v_type = VAR_LIST;
5189 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005190 ++l->lv_refcount;
5191 }
5192
5193 return OK;
5194}
5195
5196/*
5197 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005198 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005199 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005200 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005201list_alloc()
5202{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005203 list_T *l;
5204
5205 l = (list_T *)alloc_clear(sizeof(list_T));
5206 if (l != NULL)
5207 {
5208 /* Prepend the list to the list of lists for garbage collection. */
5209 if (first_list != NULL)
5210 first_list->lv_used_prev = l;
5211 l->lv_used_prev = NULL;
5212 l->lv_used_next = first_list;
5213 first_list = l;
5214 }
5215 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005216}
5217
5218/*
5219 * Unreference a list: decrement the reference count and free it when it
5220 * becomes zero.
5221 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005222 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005223list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005224 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005226 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5227 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228}
5229
5230/*
5231 * Free a list, including all items it points to.
5232 * Ignores the reference count.
5233 */
5234 static void
5235list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005236 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237{
Bram Moolenaar33570922005-01-25 22:26:29 +00005238 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239
Bram Moolenaard9fba312005-06-26 22:34:35 +00005240 /* Avoid that recursive reference to the list frees us again. */
5241 l->lv_refcount = DEL_REFCOUNT;
5242
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005243 /* Remove the list from the list of lists for garbage collection. */
5244 if (l->lv_used_prev == NULL)
5245 first_list = l->lv_used_next;
5246 else
5247 l->lv_used_prev->lv_used_next = l->lv_used_next;
5248 if (l->lv_used_next != NULL)
5249 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5250
Bram Moolenaard9fba312005-06-26 22:34:35 +00005251 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005253 /* Remove the item before deleting it. */
5254 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 listitem_free(item);
5256 }
5257 vim_free(l);
5258}
5259
5260/*
5261 * Allocate a list item.
5262 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264listitem_alloc()
5265{
Bram Moolenaar33570922005-01-25 22:26:29 +00005266 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267}
5268
5269/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005270 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 */
5272 static void
5273listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005274 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005276 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 vim_free(item);
5278}
5279
5280/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005281 * Remove a list item from a List and free it. Also clears the value.
5282 */
5283 static void
5284listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005285 list_T *l;
5286 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005287{
5288 list_remove(l, item, item);
5289 listitem_free(item);
5290}
5291
5292/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 * Get the number of items in a list.
5294 */
5295 static long
5296list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005297 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 if (l == NULL)
5300 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005301 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302}
5303
5304/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005305 * Return TRUE when two lists have exactly the same values.
5306 */
5307 static int
5308list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005309 list_T *l1;
5310 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005311 int ic; /* ignore case for strings */
5312{
Bram Moolenaar33570922005-01-25 22:26:29 +00005313 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005314
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005315 if (list_len(l1) != list_len(l2))
5316 return FALSE;
5317
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005318 for (item1 = l1->lv_first, item2 = l2->lv_first;
5319 item1 != NULL && item2 != NULL;
5320 item1 = item1->li_next, item2 = item2->li_next)
5321 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5322 return FALSE;
5323 return item1 == NULL && item2 == NULL;
5324}
5325
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005326#if defined(FEAT_PYTHON) || defined(PROTO)
5327/*
5328 * Return the dictitem that an entry in a hashtable points to.
5329 */
5330 dictitem_T *
5331dict_lookup(hi)
5332 hashitem_T *hi;
5333{
5334 return HI2DI(hi);
5335}
5336#endif
5337
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005338/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005339 * Return TRUE when two dictionaries have exactly the same key/values.
5340 */
5341 static int
5342dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005343 dict_T *d1;
5344 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005345 int ic; /* ignore case for strings */
5346{
Bram Moolenaar33570922005-01-25 22:26:29 +00005347 hashitem_T *hi;
5348 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005349 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005350
5351 if (dict_len(d1) != dict_len(d2))
5352 return FALSE;
5353
Bram Moolenaar33570922005-01-25 22:26:29 +00005354 todo = d1->dv_hashtab.ht_used;
5355 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005356 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005357 if (!HASHITEM_EMPTY(hi))
5358 {
5359 item2 = dict_find(d2, hi->hi_key, -1);
5360 if (item2 == NULL)
5361 return FALSE;
5362 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5363 return FALSE;
5364 --todo;
5365 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005366 }
5367 return TRUE;
5368}
5369
5370/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005371 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005372 * Compares the items just like "==" would compare them, but strings and
5373 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005374 */
5375 static int
5376tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005377 typval_T *tv1;
5378 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005379 int ic; /* ignore case */
5380{
5381 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005382 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005383
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005384 if (tv1->v_type != tv2->v_type)
5385 return FALSE;
5386
5387 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005388 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005389 case VAR_LIST:
5390 /* recursive! */
5391 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5392
5393 case VAR_DICT:
5394 /* recursive! */
5395 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5396
5397 case VAR_FUNC:
5398 return (tv1->vval.v_string != NULL
5399 && tv2->vval.v_string != NULL
5400 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5401
5402 case VAR_NUMBER:
5403 return tv1->vval.v_number == tv2->vval.v_number;
5404
5405 case VAR_STRING:
5406 s1 = get_tv_string_buf(tv1, buf1);
5407 s2 = get_tv_string_buf(tv2, buf2);
5408 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005409 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005410
5411 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005412 return TRUE;
5413}
5414
5415/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 * Locate item with index "n" in list "l" and return it.
5417 * A negative index is counted from the end; -1 is the last item.
5418 * Returns NULL when "n" is out of range.
5419 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005420 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005422 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423 long n;
5424{
Bram Moolenaar33570922005-01-25 22:26:29 +00005425 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 long idx;
5427
5428 if (l == NULL)
5429 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005430
5431 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005433 n = l->lv_len + n;
5434
5435 /* Check for index out of range. */
5436 if (n < 0 || n >= l->lv_len)
5437 return NULL;
5438
5439 /* When there is a cached index may start search from there. */
5440 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005442 if (n < l->lv_idx / 2)
5443 {
5444 /* closest to the start of the list */
5445 item = l->lv_first;
5446 idx = 0;
5447 }
5448 else if (n > (l->lv_idx + l->lv_len) / 2)
5449 {
5450 /* closest to the end of the list */
5451 item = l->lv_last;
5452 idx = l->lv_len - 1;
5453 }
5454 else
5455 {
5456 /* closest to the cached index */
5457 item = l->lv_idx_item;
5458 idx = l->lv_idx;
5459 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461 else
5462 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005463 if (n < l->lv_len / 2)
5464 {
5465 /* closest to the start of the list */
5466 item = l->lv_first;
5467 idx = 0;
5468 }
5469 else
5470 {
5471 /* closest to the end of the list */
5472 item = l->lv_last;
5473 idx = l->lv_len - 1;
5474 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005476
5477 while (n > idx)
5478 {
5479 /* search forward */
5480 item = item->li_next;
5481 ++idx;
5482 }
5483 while (n < idx)
5484 {
5485 /* search backward */
5486 item = item->li_prev;
5487 --idx;
5488 }
5489
5490 /* cache the used index */
5491 l->lv_idx = idx;
5492 l->lv_idx_item = item;
5493
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 return item;
5495}
5496
5497/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005498 * Locate "item" list "l" and return its index.
5499 * Returns -1 when "item" is not in the list.
5500 */
5501 static long
5502list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005503 list_T *l;
5504 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005505{
5506 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005507 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005508
5509 if (l == NULL)
5510 return -1;
5511 idx = 0;
5512 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5513 ++idx;
5514 if (li == NULL)
5515 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005516 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005517}
5518
5519/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 * Append item "item" to the end of list "l".
5521 */
5522 static void
5523list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005524 list_T *l;
5525 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526{
5527 if (l->lv_last == NULL)
5528 {
5529 /* empty list */
5530 l->lv_first = item;
5531 l->lv_last = item;
5532 item->li_prev = NULL;
5533 }
5534 else
5535 {
5536 l->lv_last->li_next = item;
5537 item->li_prev = l->lv_last;
5538 l->lv_last = item;
5539 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005540 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 item->li_next = NULL;
5542}
5543
5544/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005545 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005546 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 */
5548 static int
5549list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005550 list_T *l;
5551 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005553 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554
Bram Moolenaar05159a02005-02-26 23:04:13 +00005555 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005557 copy_tv(tv, &li->li_tv);
5558 list_append(l, li);
5559 return OK;
5560}
5561
5562/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005563 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005564 * Return FAIL when out of memory.
5565 */
5566 int
5567list_append_dict(list, dict)
5568 list_T *list;
5569 dict_T *dict;
5570{
5571 listitem_T *li = listitem_alloc();
5572
5573 if (li == NULL)
5574 return FAIL;
5575 li->li_tv.v_type = VAR_DICT;
5576 li->li_tv.v_lock = 0;
5577 li->li_tv.vval.v_dict = dict;
5578 list_append(list, li);
5579 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 return OK;
5581}
5582
5583/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005584 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005585 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005586 * Returns FAIL when out of memory.
5587 */
5588 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005589list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005590 list_T *l;
5591 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005592 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005593{
5594 listitem_T *li = listitem_alloc();
5595
5596 if (li == NULL)
5597 return FAIL;
5598 list_append(l, li);
5599 li->li_tv.v_type = VAR_STRING;
5600 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005601 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5602 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005603 return FAIL;
5604 return OK;
5605}
5606
5607/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005608 * Append "n" to list "l".
5609 * Returns FAIL when out of memory.
5610 */
5611 static int
5612list_append_number(l, n)
5613 list_T *l;
5614 varnumber_T n;
5615{
5616 listitem_T *li;
5617
5618 li = listitem_alloc();
5619 if (li == NULL)
5620 return FAIL;
5621 li->li_tv.v_type = VAR_NUMBER;
5622 li->li_tv.v_lock = 0;
5623 li->li_tv.vval.v_number = n;
5624 list_append(l, li);
5625 return OK;
5626}
5627
5628/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005629 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005630 * If "item" is NULL append at the end.
5631 * Return FAIL when out of memory.
5632 */
5633 static int
5634list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005635 list_T *l;
5636 typval_T *tv;
5637 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005638{
Bram Moolenaar33570922005-01-25 22:26:29 +00005639 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005640
5641 if (ni == NULL)
5642 return FAIL;
5643 copy_tv(tv, &ni->li_tv);
5644 if (item == NULL)
5645 /* Append new item at end of list. */
5646 list_append(l, ni);
5647 else
5648 {
5649 /* Insert new item before existing item. */
5650 ni->li_prev = item->li_prev;
5651 ni->li_next = item;
5652 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005653 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005654 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005655 ++l->lv_idx;
5656 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005657 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005658 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005659 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005660 l->lv_idx_item = NULL;
5661 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005662 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005663 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005664 }
5665 return OK;
5666}
5667
5668/*
5669 * Extend "l1" with "l2".
5670 * If "bef" is NULL append at the end, otherwise insert before this item.
5671 * Returns FAIL when out of memory.
5672 */
5673 static int
5674list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005675 list_T *l1;
5676 list_T *l2;
5677 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005678{
Bram Moolenaar33570922005-01-25 22:26:29 +00005679 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005680
5681 for (item = l2->lv_first; item != NULL; item = item->li_next)
5682 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5683 return FAIL;
5684 return OK;
5685}
5686
5687/*
5688 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5689 * Return FAIL when out of memory.
5690 */
5691 static int
5692list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005693 list_T *l1;
5694 list_T *l2;
5695 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005696{
Bram Moolenaar33570922005-01-25 22:26:29 +00005697 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005698
5699 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005700 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005701 if (l == NULL)
5702 return FAIL;
5703 tv->v_type = VAR_LIST;
5704 tv->vval.v_list = l;
5705
5706 /* append all items from the second list */
5707 return list_extend(l, l2, NULL);
5708}
5709
5710/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005711 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005712 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005713 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005714 * Returns NULL when out of memory.
5715 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005716 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005717list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005718 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005720 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721{
Bram Moolenaar33570922005-01-25 22:26:29 +00005722 list_T *copy;
5723 listitem_T *item;
5724 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005725
5726 if (orig == NULL)
5727 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728
5729 copy = list_alloc();
5730 if (copy != NULL)
5731 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005732 if (copyID != 0)
5733 {
5734 /* Do this before adding the items, because one of the items may
5735 * refer back to this list. */
5736 orig->lv_copyID = copyID;
5737 orig->lv_copylist = copy;
5738 }
5739 for (item = orig->lv_first; item != NULL && !got_int;
5740 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005741 {
5742 ni = listitem_alloc();
5743 if (ni == NULL)
5744 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005745 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005746 {
5747 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5748 {
5749 vim_free(ni);
5750 break;
5751 }
5752 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005754 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755 list_append(copy, ni);
5756 }
5757 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005758 if (item != NULL)
5759 {
5760 list_unref(copy);
5761 copy = NULL;
5762 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005763 }
5764
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005765 return copy;
5766}
5767
5768/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005769 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005770 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005772 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005773list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005774 list_T *l;
5775 listitem_T *item;
5776 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005777{
Bram Moolenaar33570922005-01-25 22:26:29 +00005778 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005780 /* notify watchers */
5781 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005783 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005784 list_fix_watch(l, ip);
5785 if (ip == item2)
5786 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005787 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005788
5789 if (item2->li_next == NULL)
5790 l->lv_last = item->li_prev;
5791 else
5792 item2->li_next->li_prev = item->li_prev;
5793 if (item->li_prev == NULL)
5794 l->lv_first = item2->li_next;
5795 else
5796 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005797 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798}
5799
5800/*
5801 * Return an allocated string with the string representation of a list.
5802 * May return NULL.
5803 */
5804 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005805list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005806 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005807 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808{
5809 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005810
5811 if (tv->vval.v_list == NULL)
5812 return NULL;
5813 ga_init2(&ga, (int)sizeof(char), 80);
5814 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005815 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005816 {
5817 vim_free(ga.ga_data);
5818 return NULL;
5819 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005820 ga_append(&ga, ']');
5821 ga_append(&ga, NUL);
5822 return (char_u *)ga.ga_data;
5823}
5824
5825/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005826 * Join list "l" into a string in "*gap", using separator "sep".
5827 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005828 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005829 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005830 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005831list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005832 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005833 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005834 char_u *sep;
5835 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005836 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005837{
5838 int first = TRUE;
5839 char_u *tofree;
5840 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005841 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005842 char_u *s;
5843
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005844 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005845 {
5846 if (first)
5847 first = FALSE;
5848 else
5849 ga_concat(gap, sep);
5850
5851 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005852 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005854 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005855 if (s != NULL)
5856 ga_concat(gap, s);
5857 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005858 if (s == NULL)
5859 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005861 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862}
5863
5864/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005865 * Garbage collection for lists and dictionaries.
5866 *
5867 * We use reference counts to be able to free most items right away when they
5868 * are no longer used. But for composite items it's possible that it becomes
5869 * unused while the reference count is > 0: When there is a recursive
5870 * reference. Example:
5871 * :let l = [1, 2, 3]
5872 * :let d = {9: l}
5873 * :let l[1] = d
5874 *
5875 * Since this is quite unusual we handle this with garbage collection: every
5876 * once in a while find out which lists and dicts are not referenced from any
5877 * variable.
5878 *
5879 * Here is a good reference text about garbage collection (refers to Python
5880 * but it applies to all reference-counting mechanisms):
5881 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005882 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005883
5884/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005885 * Do garbage collection for lists and dicts.
5886 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005887 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005888 int
5889garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005890{
5891 dict_T *dd;
5892 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005893 int copyID = ++current_copyID;
5894 buf_T *buf;
5895 win_T *wp;
5896 int i;
5897 funccall_T *fc;
5898 int did_free = FALSE;
5899
5900 /*
5901 * 1. Go through all accessible variables and mark all lists and dicts
5902 * with copyID.
5903 */
5904 /* script-local variables */
5905 for (i = 1; i <= ga_scripts.ga_len; ++i)
5906 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5907
5908 /* buffer-local variables */
5909 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5910 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5911
5912 /* window-local variables */
5913 FOR_ALL_WINDOWS(wp)
5914 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5915
5916 /* global variables */
5917 set_ref_in_ht(&globvarht, copyID);
5918
5919 /* function-local variables */
5920 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5921 {
5922 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5923 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5924 }
5925
5926 /*
5927 * 2. Go through the list of dicts and free items without the copyID.
5928 */
5929 for (dd = first_dict; dd != NULL; )
5930 if (dd->dv_copyID != copyID)
5931 {
5932 dict_free(dd);
5933 did_free = TRUE;
5934
5935 /* restart, next dict may also have been freed */
5936 dd = first_dict;
5937 }
5938 else
5939 dd = dd->dv_used_next;
5940
5941 /*
5942 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005943 * But don't free a list that has a watcher (used in a for loop), these
5944 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005945 */
5946 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005947 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005948 {
5949 list_free(ll);
5950 did_free = TRUE;
5951
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005952 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005953 ll = first_list;
5954 }
5955 else
5956 ll = ll->lv_used_next;
5957
5958 return did_free;
5959}
5960
5961/*
5962 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5963 */
5964 static void
5965set_ref_in_ht(ht, copyID)
5966 hashtab_T *ht;
5967 int copyID;
5968{
5969 int todo;
5970 hashitem_T *hi;
5971
5972 todo = ht->ht_used;
5973 for (hi = ht->ht_array; todo > 0; ++hi)
5974 if (!HASHITEM_EMPTY(hi))
5975 {
5976 --todo;
5977 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5978 }
5979}
5980
5981/*
5982 * Mark all lists and dicts referenced through list "l" with "copyID".
5983 */
5984 static void
5985set_ref_in_list(l, copyID)
5986 list_T *l;
5987 int copyID;
5988{
5989 listitem_T *li;
5990
5991 for (li = l->lv_first; li != NULL; li = li->li_next)
5992 set_ref_in_item(&li->li_tv, copyID);
5993}
5994
5995/*
5996 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5997 */
5998 static void
5999set_ref_in_item(tv, copyID)
6000 typval_T *tv;
6001 int copyID;
6002{
6003 dict_T *dd;
6004 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006005
6006 switch (tv->v_type)
6007 {
6008 case VAR_DICT:
6009 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006010 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006011 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006012 /* Didn't see this dict yet. */
6013 dd->dv_copyID = copyID;
6014 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006015 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006016 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006017
6018 case VAR_LIST:
6019 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006020 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006021 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006022 /* Didn't see this list yet. */
6023 ll->lv_copyID = copyID;
6024 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006025 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006026 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006027 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006028 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006029}
6030
6031/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006032 * Allocate an empty header for a dictionary.
6033 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006034 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006035dict_alloc()
6036{
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006038
Bram Moolenaar33570922005-01-25 22:26:29 +00006039 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006040 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006041 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006042 /* Add the list to the hashtable for garbage collection. */
6043 if (first_dict != NULL)
6044 first_dict->dv_used_prev = d;
6045 d->dv_used_next = first_dict;
6046 d->dv_used_prev = NULL;
6047
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006049 d->dv_lock = 0;
6050 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006051 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006052 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006053 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006054}
6055
6056/*
6057 * Unreference a Dictionary: decrement the reference count and free it when it
6058 * becomes zero.
6059 */
6060 static void
6061dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006063{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006064 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6065 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006066}
6067
6068/*
6069 * Free a Dictionary, including all items it contains.
6070 * Ignores the reference count.
6071 */
6072 static void
6073dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006074 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006075{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006076 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006077 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006078 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006079
Bram Moolenaard9fba312005-06-26 22:34:35 +00006080 /* Avoid that recursive reference to the dict frees us again. */
6081 d->dv_refcount = DEL_REFCOUNT;
6082
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006083 /* Remove the dict from the list of dicts for garbage collection. */
6084 if (d->dv_used_prev == NULL)
6085 first_dict = d->dv_used_next;
6086 else
6087 d->dv_used_prev->dv_used_next = d->dv_used_next;
6088 if (d->dv_used_next != NULL)
6089 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6090
6091 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006092 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 todo = d->dv_hashtab.ht_used;
6094 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006095 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006096 if (!HASHITEM_EMPTY(hi))
6097 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006098 /* Remove the item before deleting it, just in case there is
6099 * something recursive causing trouble. */
6100 di = HI2DI(hi);
6101 hash_remove(&d->dv_hashtab, hi);
6102 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006103 --todo;
6104 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006105 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006106 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006107 vim_free(d);
6108}
6109
6110/*
6111 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006112 * The "key" is copied to the new item.
6113 * Note that the value of the item "di_tv" still needs to be initialized!
6114 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006115 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006116 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006117dictitem_alloc(key)
6118 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006119{
Bram Moolenaar33570922005-01-25 22:26:29 +00006120 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006121
Bram Moolenaar33570922005-01-25 22:26:29 +00006122 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006123 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006124 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006125 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006126 di->di_flags = 0;
6127 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006128 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006129}
6130
6131/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006132 * Make a copy of a Dictionary item.
6133 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006134 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006135dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006137{
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006139
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006141 if (di != NULL)
6142 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006143 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006144 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006145 copy_tv(&org->di_tv, &di->di_tv);
6146 }
6147 return di;
6148}
6149
6150/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006151 * Remove item "item" from Dictionary "dict" and free it.
6152 */
6153 static void
6154dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 dict_T *dict;
6156 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006157{
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006159
Bram Moolenaar33570922005-01-25 22:26:29 +00006160 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006161 if (HASHITEM_EMPTY(hi))
6162 EMSG2(_(e_intern2), "dictitem_remove()");
6163 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006165 dictitem_free(item);
6166}
6167
6168/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006169 * Free a dict item. Also clears the value.
6170 */
6171 static void
6172dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006173 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006174{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006175 clear_tv(&item->di_tv);
6176 vim_free(item);
6177}
6178
6179/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006180 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6181 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006182 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006183 * Returns NULL when out of memory.
6184 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006185 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006186dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006187 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006188 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006189 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006190{
Bram Moolenaar33570922005-01-25 22:26:29 +00006191 dict_T *copy;
6192 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006193 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006194 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006195
6196 if (orig == NULL)
6197 return NULL;
6198
6199 copy = dict_alloc();
6200 if (copy != NULL)
6201 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006202 if (copyID != 0)
6203 {
6204 orig->dv_copyID = copyID;
6205 orig->dv_copydict = copy;
6206 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006207 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006208 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006209 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006210 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006211 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006212 --todo;
6213
6214 di = dictitem_alloc(hi->hi_key);
6215 if (di == NULL)
6216 break;
6217 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006218 {
6219 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6220 copyID) == FAIL)
6221 {
6222 vim_free(di);
6223 break;
6224 }
6225 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006226 else
6227 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6228 if (dict_add(copy, di) == FAIL)
6229 {
6230 dictitem_free(di);
6231 break;
6232 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006233 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006234 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006235
Bram Moolenaare9a41262005-01-15 22:18:47 +00006236 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006237 if (todo > 0)
6238 {
6239 dict_unref(copy);
6240 copy = NULL;
6241 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006242 }
6243
6244 return copy;
6245}
6246
6247/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006248 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006249 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006250 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006251 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006252dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006253 dict_T *d;
6254 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006255{
Bram Moolenaar33570922005-01-25 22:26:29 +00006256 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006257}
6258
Bram Moolenaar8c711452005-01-14 21:53:12 +00006259/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006260 * Add a number or string entry to dictionary "d".
6261 * When "str" is NULL use number "nr", otherwise use "str".
6262 * Returns FAIL when out of memory and when key already exists.
6263 */
6264 int
6265dict_add_nr_str(d, key, nr, str)
6266 dict_T *d;
6267 char *key;
6268 long nr;
6269 char_u *str;
6270{
6271 dictitem_T *item;
6272
6273 item = dictitem_alloc((char_u *)key);
6274 if (item == NULL)
6275 return FAIL;
6276 item->di_tv.v_lock = 0;
6277 if (str == NULL)
6278 {
6279 item->di_tv.v_type = VAR_NUMBER;
6280 item->di_tv.vval.v_number = nr;
6281 }
6282 else
6283 {
6284 item->di_tv.v_type = VAR_STRING;
6285 item->di_tv.vval.v_string = vim_strsave(str);
6286 }
6287 if (dict_add(d, item) == FAIL)
6288 {
6289 dictitem_free(item);
6290 return FAIL;
6291 }
6292 return OK;
6293}
6294
6295/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006296 * Get the number of items in a Dictionary.
6297 */
6298 static long
6299dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006300 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006301{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006302 if (d == NULL)
6303 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006305}
6306
6307/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006308 * Find item "key[len]" in Dictionary "d".
6309 * If "len" is negative use strlen(key).
6310 * Returns NULL when not found.
6311 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006313dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006314 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006315 char_u *key;
6316 int len;
6317{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006318#define AKEYLEN 200
6319 char_u buf[AKEYLEN];
6320 char_u *akey;
6321 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006322 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006323
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006324 if (len < 0)
6325 akey = key;
6326 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006327 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006328 tofree = akey = vim_strnsave(key, len);
6329 if (akey == NULL)
6330 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006331 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006332 else
6333 {
6334 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006335 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006336 akey = buf;
6337 }
6338
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006340 vim_free(tofree);
6341 if (HASHITEM_EMPTY(hi))
6342 return NULL;
6343 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006344}
6345
6346/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006347 * Get a string item from a dictionary in allocated memory.
6348 * Returns NULL if the entry doesn't exist or out of memory.
6349 */
6350 char_u *
6351get_dict_string(d, key)
6352 dict_T *d;
6353 char_u *key;
6354{
6355 dictitem_T *di;
6356
6357 di = dict_find(d, key, -1);
6358 if (di == NULL)
6359 return NULL;
6360 return vim_strsave(get_tv_string(&di->di_tv));
6361}
6362
6363/*
6364 * Get a number item from a dictionary.
6365 * Returns 0 if the entry doesn't exist or out of memory.
6366 */
6367 long
6368get_dict_number(d, key)
6369 dict_T *d;
6370 char_u *key;
6371{
6372 dictitem_T *di;
6373
6374 di = dict_find(d, key, -1);
6375 if (di == NULL)
6376 return 0;
6377 return get_tv_number(&di->di_tv);
6378}
6379
6380/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006381 * Return an allocated string with the string representation of a Dictionary.
6382 * May return NULL.
6383 */
6384 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006385dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006387 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006388{
6389 garray_T ga;
6390 int first = TRUE;
6391 char_u *tofree;
6392 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006393 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006394 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006395 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006396 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006397
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006398 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006399 return NULL;
6400 ga_init2(&ga, (int)sizeof(char), 80);
6401 ga_append(&ga, '{');
6402
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006404 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006405 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006406 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006407 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408 --todo;
6409
6410 if (first)
6411 first = FALSE;
6412 else
6413 ga_concat(&ga, (char_u *)", ");
6414
6415 tofree = string_quote(hi->hi_key, FALSE);
6416 if (tofree != NULL)
6417 {
6418 ga_concat(&ga, tofree);
6419 vim_free(tofree);
6420 }
6421 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006422 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006423 if (s != NULL)
6424 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006425 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006426 if (s == NULL)
6427 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006428 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006429 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006430 if (todo > 0)
6431 {
6432 vim_free(ga.ga_data);
6433 return NULL;
6434 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006435
6436 ga_append(&ga, '}');
6437 ga_append(&ga, NUL);
6438 return (char_u *)ga.ga_data;
6439}
6440
6441/*
6442 * Allocate a variable for a Dictionary and fill it from "*arg".
6443 * Return OK or FAIL. Returns NOTDONE for {expr}.
6444 */
6445 static int
6446get_dict_tv(arg, rettv, evaluate)
6447 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006448 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006449 int evaluate;
6450{
Bram Moolenaar33570922005-01-25 22:26:29 +00006451 dict_T *d = NULL;
6452 typval_T tvkey;
6453 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006454 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006455 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006456 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006457 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006458
6459 /*
6460 * First check if it's not a curly-braces thing: {expr}.
6461 * Must do this without evaluating, otherwise a function may be called
6462 * twice. Unfortunately this means we need to call eval1() twice for the
6463 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006464 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006465 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006466 if (*start != '}')
6467 {
6468 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6469 return FAIL;
6470 if (*start == '}')
6471 return NOTDONE;
6472 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006473
6474 if (evaluate)
6475 {
6476 d = dict_alloc();
6477 if (d == NULL)
6478 return FAIL;
6479 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006480 tvkey.v_type = VAR_UNKNOWN;
6481 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006482
6483 *arg = skipwhite(*arg + 1);
6484 while (**arg != '}' && **arg != NUL)
6485 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006486 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006487 goto failret;
6488 if (**arg != ':')
6489 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006490 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006491 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006492 goto failret;
6493 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006494 key = get_tv_string_buf_chk(&tvkey, buf);
6495 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006497 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6498 if (key != NULL)
6499 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006500 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006501 goto failret;
6502 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006503
6504 *arg = skipwhite(*arg + 1);
6505 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6506 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006507 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006508 goto failret;
6509 }
6510 if (evaluate)
6511 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006512 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006513 if (item != NULL)
6514 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006515 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006516 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006517 clear_tv(&tv);
6518 goto failret;
6519 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006520 item = dictitem_alloc(key);
6521 clear_tv(&tvkey);
6522 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006523 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006524 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006525 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006526 if (dict_add(d, item) == FAIL)
6527 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006528 }
6529 }
6530
6531 if (**arg == '}')
6532 break;
6533 if (**arg != ',')
6534 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006535 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006536 goto failret;
6537 }
6538 *arg = skipwhite(*arg + 1);
6539 }
6540
6541 if (**arg != '}')
6542 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006543 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006544failret:
6545 if (evaluate)
6546 dict_free(d);
6547 return FAIL;
6548 }
6549
6550 *arg = skipwhite(*arg + 1);
6551 if (evaluate)
6552 {
6553 rettv->v_type = VAR_DICT;
6554 rettv->vval.v_dict = d;
6555 ++d->dv_refcount;
6556 }
6557
6558 return OK;
6559}
6560
Bram Moolenaar8c711452005-01-14 21:53:12 +00006561/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 * Return a string with the string representation of a variable.
6563 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006564 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006565 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006566 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567 * May return NULL;
6568 */
6569 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006570echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006571 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006574 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006576 static int recurse = 0;
6577 char_u *r = NULL;
6578
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006580 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006581 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006582 *tofree = NULL;
6583 return NULL;
6584 }
6585 ++recurse;
6586
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 switch (tv->v_type)
6588 {
6589 case VAR_FUNC:
6590 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006591 r = tv->vval.v_string;
6592 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006593
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006595 if (tv->vval.v_list == NULL)
6596 {
6597 *tofree = NULL;
6598 r = NULL;
6599 }
6600 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6601 {
6602 *tofree = NULL;
6603 r = (char_u *)"[...]";
6604 }
6605 else
6606 {
6607 tv->vval.v_list->lv_copyID = copyID;
6608 *tofree = list2string(tv, copyID);
6609 r = *tofree;
6610 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006611 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006612
Bram Moolenaar8c711452005-01-14 21:53:12 +00006613 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006614 if (tv->vval.v_dict == NULL)
6615 {
6616 *tofree = NULL;
6617 r = NULL;
6618 }
6619 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6620 {
6621 *tofree = NULL;
6622 r = (char_u *)"{...}";
6623 }
6624 else
6625 {
6626 tv->vval.v_dict->dv_copyID = copyID;
6627 *tofree = dict2string(tv, copyID);
6628 r = *tofree;
6629 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006630 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006631
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006632 case VAR_STRING:
6633 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006634 *tofree = NULL;
6635 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006638 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006639 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006640 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006642
6643 --recurse;
6644 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006645}
6646
6647/*
6648 * Return a string with the string representation of a variable.
6649 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6650 * "numbuf" is used for a number.
6651 * Puts quotes around strings, so that they can be parsed back by eval().
6652 * May return NULL;
6653 */
6654 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006655tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006656 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006657 char_u **tofree;
6658 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006659 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006660{
6661 switch (tv->v_type)
6662 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006663 case VAR_FUNC:
6664 *tofree = string_quote(tv->vval.v_string, TRUE);
6665 return *tofree;
6666 case VAR_STRING:
6667 *tofree = string_quote(tv->vval.v_string, FALSE);
6668 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006669 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006670 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006671 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006672 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006673 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006674 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006676 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677}
6678
6679/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006680 * Return string "str" in ' quotes, doubling ' characters.
6681 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006682 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006683 */
6684 static char_u *
6685string_quote(str, function)
6686 char_u *str;
6687 int function;
6688{
Bram Moolenaar33570922005-01-25 22:26:29 +00006689 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006690 char_u *p, *r, *s;
6691
Bram Moolenaar33570922005-01-25 22:26:29 +00006692 len = (function ? 13 : 3);
6693 if (str != NULL)
6694 {
6695 len += STRLEN(str);
6696 for (p = str; *p != NUL; mb_ptr_adv(p))
6697 if (*p == '\'')
6698 ++len;
6699 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006700 s = r = alloc(len);
6701 if (r != NULL)
6702 {
6703 if (function)
6704 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006705 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006706 r += 10;
6707 }
6708 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006709 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006710 if (str != NULL)
6711 for (p = str; *p != NUL; )
6712 {
6713 if (*p == '\'')
6714 *r++ = '\'';
6715 MB_COPY_CHAR(p, r);
6716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006717 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006718 if (function)
6719 *r++ = ')';
6720 *r++ = NUL;
6721 }
6722 return s;
6723}
6724
6725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 * Get the value of an environment variable.
6727 * "arg" is pointing to the '$'. It is advanced to after the name.
6728 * If the environment variable was not set, silently assume it is empty.
6729 * Always return OK.
6730 */
6731 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006732get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 int evaluate;
6736{
6737 char_u *string = NULL;
6738 int len;
6739 int cc;
6740 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006741 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742
6743 ++*arg;
6744 name = *arg;
6745 len = get_env_len(arg);
6746 if (evaluate)
6747 {
6748 if (len != 0)
6749 {
6750 cc = name[len];
6751 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006752 /* first try vim_getenv(), fast for normal environment vars */
6753 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006755 {
6756 if (!mustfree)
6757 string = vim_strsave(string);
6758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 else
6760 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006761 if (mustfree)
6762 vim_free(string);
6763
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 /* next try expanding things like $VIM and ${HOME} */
6765 string = expand_env_save(name - 1);
6766 if (string != NULL && *string == '$')
6767 {
6768 vim_free(string);
6769 string = NULL;
6770 }
6771 }
6772 name[len] = cc;
6773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006774 rettv->v_type = VAR_STRING;
6775 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 }
6777
6778 return OK;
6779}
6780
6781/*
6782 * Array with names and number of arguments of all internal functions
6783 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6784 */
6785static struct fst
6786{
6787 char *f_name; /* function name */
6788 char f_min_argc; /* minimal number of arguments */
6789 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006790 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006791 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792} functions[] =
6793{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006794 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 {"append", 2, 2, f_append},
6796 {"argc", 0, 0, f_argc},
6797 {"argidx", 0, 0, f_argidx},
6798 {"argv", 1, 1, f_argv},
6799 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006800 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {"bufexists", 1, 1, f_bufexists},
6802 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6803 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6804 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6805 {"buflisted", 1, 1, f_buflisted},
6806 {"bufloaded", 1, 1, f_bufloaded},
6807 {"bufname", 1, 1, f_bufname},
6808 {"bufnr", 1, 1, f_bufnr},
6809 {"bufwinnr", 1, 1, f_bufwinnr},
6810 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006811 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006812 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {"char2nr", 1, 1, f_char2nr},
6814 {"cindent", 1, 1, f_cindent},
6815 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006816#if defined(FEAT_INS_EXPAND)
6817 {"complete_add", 1, 1, f_complete_add},
6818 {"complete_check", 0, 0, f_complete_check},
6819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006821 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006822 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 {"cscope_connection",0,3, f_cscope_connection},
6824 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006825 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 {"delete", 1, 1, f_delete},
6827 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006828 {"diff_filler", 1, 1, f_diff_filler},
6829 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006830 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006832 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 {"eventhandler", 0, 0, f_eventhandler},
6834 {"executable", 1, 1, f_executable},
6835 {"exists", 1, 1, f_exists},
6836 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006837 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6839 {"filereadable", 1, 1, f_filereadable},
6840 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006841 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006842 {"finddir", 1, 3, f_finddir},
6843 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 {"fnamemodify", 2, 2, f_fnamemodify},
6845 {"foldclosed", 1, 1, f_foldclosed},
6846 {"foldclosedend", 1, 1, f_foldclosedend},
6847 {"foldlevel", 1, 1, f_foldlevel},
6848 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006849 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006851 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006852 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006853 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006854 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 {"getbufvar", 2, 2, f_getbufvar},
6856 {"getchar", 0, 1, f_getchar},
6857 {"getcharmod", 0, 0, f_getcharmod},
6858 {"getcmdline", 0, 0, f_getcmdline},
6859 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006860 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006862 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006863 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 {"getfsize", 1, 1, f_getfsize},
6865 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006866 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006867 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006868 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006869 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006870 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {"getregtype", 0, 1, f_getregtype},
6872 {"getwinposx", 0, 0, f_getwinposx},
6873 {"getwinposy", 0, 0, f_getwinposy},
6874 {"getwinvar", 2, 2, f_getwinvar},
6875 {"glob", 1, 1, f_glob},
6876 {"globpath", 2, 2, f_globpath},
6877 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006878 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {"hasmapto", 1, 2, f_hasmapto},
6880 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6881 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6882 {"histadd", 2, 2, f_histadd},
6883 {"histdel", 1, 2, f_histdel},
6884 {"histget", 1, 2, f_histget},
6885 {"histnr", 1, 1, f_histnr},
6886 {"hlID", 1, 1, f_hlID},
6887 {"hlexists", 1, 1, f_hlexists},
6888 {"hostname", 0, 0, f_hostname},
6889 {"iconv", 3, 3, f_iconv},
6890 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006891 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006892 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006894 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 {"inputrestore", 0, 0, f_inputrestore},
6896 {"inputsave", 0, 0, f_inputsave},
6897 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006898 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006900 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006901 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006902 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006903 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006905 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 {"libcall", 3, 3, f_libcall},
6907 {"libcallnr", 3, 3, f_libcallnr},
6908 {"line", 1, 1, f_line},
6909 {"line2byte", 1, 1, f_line2byte},
6910 {"lispindent", 1, 1, f_lispindent},
6911 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006912 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {"maparg", 1, 2, f_maparg},
6914 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006915 {"match", 2, 4, f_match},
6916 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006917 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006918 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006919 {"max", 1, 1, f_max},
6920 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006921#ifdef vim_mkdir
6922 {"mkdir", 1, 3, f_mkdir},
6923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 {"mode", 0, 0, f_mode},
6925 {"nextnonblank", 1, 1, f_nextnonblank},
6926 {"nr2char", 1, 1, f_nr2char},
6927 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006928 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006929 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006930 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {"remote_expr", 2, 3, f_remote_expr},
6932 {"remote_foreground", 1, 1, f_remote_foreground},
6933 {"remote_peek", 1, 2, f_remote_peek},
6934 {"remote_read", 1, 1, f_remote_read},
6935 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006936 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006938 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006940 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006942 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 {"searchpair", 3, 5, f_searchpair},
6944 {"server2client", 2, 2, f_server2client},
6945 {"serverlist", 0, 0, f_serverlist},
6946 {"setbufvar", 3, 3, f_setbufvar},
6947 {"setcmdpos", 1, 1, f_setcmdpos},
6948 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006949 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006950 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {"setreg", 2, 3, f_setreg},
6952 {"setwinvar", 3, 3, f_setwinvar},
6953 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006954 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006955 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006956 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006957 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006958 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959#ifdef HAVE_STRFTIME
6960 {"strftime", 1, 2, f_strftime},
6961#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006962 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006963 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {"strlen", 1, 1, f_strlen},
6965 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006966 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 {"strtrans", 1, 1, f_strtrans},
6968 {"submatch", 1, 1, f_submatch},
6969 {"substitute", 4, 4, f_substitute},
6970 {"synID", 3, 3, f_synID},
6971 {"synIDattr", 2, 3, f_synIDattr},
6972 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006973 {"system", 1, 2, f_system},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006974 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006975 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006977 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 {"tolower", 1, 1, f_tolower},
6979 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006980 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006982 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {"virtcol", 1, 1, f_virtcol},
6984 {"visualmode", 0, 1, f_visualmode},
6985 {"winbufnr", 1, 1, f_winbufnr},
6986 {"wincol", 0, 0, f_wincol},
6987 {"winheight", 1, 1, f_winheight},
6988 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006989 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {"winrestcmd", 0, 0, f_winrestcmd},
6991 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006992 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993};
6994
6995#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6996
6997/*
6998 * Function given to ExpandGeneric() to obtain the list of internal
6999 * or user defined function names.
7000 */
7001 char_u *
7002get_function_name(xp, idx)
7003 expand_T *xp;
7004 int idx;
7005{
7006 static int intidx = -1;
7007 char_u *name;
7008
7009 if (idx == 0)
7010 intidx = -1;
7011 if (intidx < 0)
7012 {
7013 name = get_user_func_name(xp, idx);
7014 if (name != NULL)
7015 return name;
7016 }
7017 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7018 {
7019 STRCPY(IObuff, functions[intidx].f_name);
7020 STRCAT(IObuff, "(");
7021 if (functions[intidx].f_max_argc == 0)
7022 STRCAT(IObuff, ")");
7023 return IObuff;
7024 }
7025
7026 return NULL;
7027}
7028
7029/*
7030 * Function given to ExpandGeneric() to obtain the list of internal or
7031 * user defined variable or function names.
7032 */
7033/*ARGSUSED*/
7034 char_u *
7035get_expr_name(xp, idx)
7036 expand_T *xp;
7037 int idx;
7038{
7039 static int intidx = -1;
7040 char_u *name;
7041
7042 if (idx == 0)
7043 intidx = -1;
7044 if (intidx < 0)
7045 {
7046 name = get_function_name(xp, idx);
7047 if (name != NULL)
7048 return name;
7049 }
7050 return get_user_var_name(xp, ++intidx);
7051}
7052
7053#endif /* FEAT_CMDL_COMPL */
7054
7055/*
7056 * Find internal function in table above.
7057 * Return index, or -1 if not found
7058 */
7059 static int
7060find_internal_func(name)
7061 char_u *name; /* name of the function */
7062{
7063 int first = 0;
7064 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7065 int cmp;
7066 int x;
7067
7068 /*
7069 * Find the function name in the table. Binary search.
7070 */
7071 while (first <= last)
7072 {
7073 x = first + ((unsigned)(last - first) >> 1);
7074 cmp = STRCMP(name, functions[x].f_name);
7075 if (cmp < 0)
7076 last = x - 1;
7077 else if (cmp > 0)
7078 first = x + 1;
7079 else
7080 return x;
7081 }
7082 return -1;
7083}
7084
7085/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007086 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7087 * name it contains, otherwise return "name".
7088 */
7089 static char_u *
7090deref_func_name(name, lenp)
7091 char_u *name;
7092 int *lenp;
7093{
Bram Moolenaar33570922005-01-25 22:26:29 +00007094 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007095 int cc;
7096
7097 cc = name[*lenp];
7098 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007099 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007100 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007102 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007103 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007104 {
7105 *lenp = 0;
7106 return (char_u *)""; /* just in case */
7107 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007108 *lenp = STRLEN(v->di_tv.vval.v_string);
7109 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007110 }
7111
7112 return name;
7113}
7114
7115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 * Allocate a variable for the result of a function.
7117 * Return OK or FAIL.
7118 */
7119 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007120get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7121 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 char_u *name; /* name of the function */
7123 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 char_u **arg; /* argument, pointing to the '(' */
7126 linenr_T firstline; /* first line of range */
7127 linenr_T lastline; /* last line of range */
7128 int *doesrange; /* return: function handled range */
7129 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007130 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131{
7132 char_u *argp;
7133 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007134 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 int argcount = 0; /* number of arguments found */
7136
7137 /*
7138 * Get the arguments.
7139 */
7140 argp = *arg;
7141 while (argcount < MAX_FUNC_ARGS)
7142 {
7143 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7144 if (*argp == ')' || *argp == ',' || *argp == NUL)
7145 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7147 {
7148 ret = FAIL;
7149 break;
7150 }
7151 ++argcount;
7152 if (*argp != ',')
7153 break;
7154 }
7155 if (*argp == ')')
7156 ++argp;
7157 else
7158 ret = FAIL;
7159
7160 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007161 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007162 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007164 {
7165 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007166 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007167 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007168 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170
7171 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007172 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173
7174 *arg = skipwhite(argp);
7175 return ret;
7176}
7177
7178
7179/*
7180 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007181 * Return OK when the function can't be called, FAIL otherwise.
7182 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 */
7184 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007185call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007186 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 char_u *name; /* name of the function */
7188 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007191 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 linenr_T firstline; /* first line of range */
7193 linenr_T lastline; /* last line of range */
7194 int *doesrange; /* return: function handled range */
7195 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007196 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199#define ERROR_UNKNOWN 0
7200#define ERROR_TOOMANY 1
7201#define ERROR_TOOFEW 2
7202#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203#define ERROR_DICT 4
7204#define ERROR_NONE 5
7205#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 int error = ERROR_NONE;
7207 int i;
7208 int llen;
7209 ufunc_T *fp;
7210 int cc;
7211#define FLEN_FIXED 40
7212 char_u fname_buf[FLEN_FIXED + 1];
7213 char_u *fname;
7214
7215 /*
7216 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7217 * Change <SNR>123_name() to K_SNR 123_name().
7218 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7219 */
7220 cc = name[len];
7221 name[len] = NUL;
7222 llen = eval_fname_script(name);
7223 if (llen > 0)
7224 {
7225 fname_buf[0] = K_SPECIAL;
7226 fname_buf[1] = KS_EXTRA;
7227 fname_buf[2] = (int)KE_SNR;
7228 i = 3;
7229 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7230 {
7231 if (current_SID <= 0)
7232 error = ERROR_SCRIPT;
7233 else
7234 {
7235 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7236 i = (int)STRLEN(fname_buf);
7237 }
7238 }
7239 if (i + STRLEN(name + llen) < FLEN_FIXED)
7240 {
7241 STRCPY(fname_buf + i, name + llen);
7242 fname = fname_buf;
7243 }
7244 else
7245 {
7246 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7247 if (fname == NULL)
7248 error = ERROR_OTHER;
7249 else
7250 {
7251 mch_memmove(fname, fname_buf, (size_t)i);
7252 STRCPY(fname + i, name + llen);
7253 }
7254 }
7255 }
7256 else
7257 fname = name;
7258
7259 *doesrange = FALSE;
7260
7261
7262 /* execute the function if no errors detected and executing */
7263 if (evaluate && error == ERROR_NONE)
7264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007265 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 error = ERROR_UNKNOWN;
7267
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007268 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
7270 /*
7271 * User defined function.
7272 */
7273 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007274
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007276 /* Trigger FuncUndefined event, may load the function. */
7277 if (fp == NULL
7278 && apply_autocmds(EVENT_FUNCUNDEFINED,
7279 fname, fname, TRUE, NULL)
7280 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007282 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 fp = find_func(fname);
7284 }
7285#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007286 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007287 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007288 {
7289 /* loaded a package, search for the function again */
7290 fp = find_func(fname);
7291 }
7292
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 if (fp != NULL)
7294 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007295 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007297 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007299 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007301 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 else
7304 {
7305 /*
7306 * Call the user function.
7307 * Save and restore search patterns, script variables and
7308 * redo buffer.
7309 */
7310 save_search_patterns();
7311 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007312 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007313 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007314 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007315 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7316 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7317 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007318 /* Function was unreferenced while being used, free it
7319 * now. */
7320 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 restoreRedobuff();
7322 restore_search_patterns();
7323 error = ERROR_NONE;
7324 }
7325 }
7326 }
7327 else
7328 {
7329 /*
7330 * Find the function name in the table, call its implementation.
7331 */
7332 i = find_internal_func(fname);
7333 if (i >= 0)
7334 {
7335 if (argcount < functions[i].f_min_argc)
7336 error = ERROR_TOOFEW;
7337 else if (argcount > functions[i].f_max_argc)
7338 error = ERROR_TOOMANY;
7339 else
7340 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007341 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007342 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 error = ERROR_NONE;
7344 }
7345 }
7346 }
7347 /*
7348 * The function call (or "FuncUndefined" autocommand sequence) might
7349 * have been aborted by an error, an interrupt, or an explicitly thrown
7350 * exception that has not been caught so far. This situation can be
7351 * tested for by calling aborting(). For an error in an internal
7352 * function or for the "E132" error in call_user_func(), however, the
7353 * throw point at which the "force_abort" flag (temporarily reset by
7354 * emsg()) is normally updated has not been reached yet. We need to
7355 * update that flag first to make aborting() reliable.
7356 */
7357 update_force_abort();
7358 }
7359 if (error == ERROR_NONE)
7360 ret = OK;
7361
7362 /*
7363 * Report an error unless the argument evaluation or function call has been
7364 * cancelled due to an aborting error, an interrupt, or an exception.
7365 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366 if (!aborting())
7367 {
7368 switch (error)
7369 {
7370 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007371 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007372 break;
7373 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007375 break;
7376 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007377 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007378 name);
7379 break;
7380 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007381 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382 name);
7383 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007384 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007385 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007386 name);
7387 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007388 }
7389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390
7391 name[len] = cc;
7392 if (fname != name && fname != fname_buf)
7393 vim_free(fname);
7394
7395 return ret;
7396}
7397
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007398/*
7399 * Give an error message with a function name. Handle <SNR> things.
7400 */
7401 static void
7402emsg_funcname(msg, name)
7403 char *msg;
7404 char_u *name;
7405{
7406 char_u *p;
7407
7408 if (*name == K_SPECIAL)
7409 p = concat_str((char_u *)"<SNR>", name + 3);
7410 else
7411 p = name;
7412 EMSG2(_(msg), p);
7413 if (p != name)
7414 vim_free(p);
7415}
7416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417/*********************************************
7418 * Implementation of the built-in functions
7419 */
7420
7421/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007422 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 */
7424 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007425f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 typval_T *argvars;
7427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007431 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007432 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007434 if ((l = argvars[0].vval.v_list) != NULL
7435 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7436 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007437 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007438 }
7439 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007440 EMSG(_(e_listreq));
7441}
7442
7443/*
7444 * "append(lnum, string/list)" function
7445 */
7446 static void
7447f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 typval_T *argvars;
7449 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007450{
7451 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007452 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 list_T *l = NULL;
7454 listitem_T *li = NULL;
7455 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007456 long added = 0;
7457
Bram Moolenaar0d660222005-01-07 21:51:51 +00007458 lnum = get_tv_lnum(argvars);
7459 if (lnum >= 0
7460 && lnum <= curbuf->b_ml.ml_line_count
7461 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007462 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007463 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007464 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007465 l = argvars[1].vval.v_list;
7466 if (l == NULL)
7467 return;
7468 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007469 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007470 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007471 for (;;)
7472 {
7473 if (l == NULL)
7474 tv = &argvars[1]; /* append a string */
7475 else if (li == NULL)
7476 break; /* end of list */
7477 else
7478 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007479 line = get_tv_string_chk(tv);
7480 if (line == NULL) /* type error */
7481 {
7482 rettv->vval.v_number = 1; /* Failed */
7483 break;
7484 }
7485 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007486 ++added;
7487 if (l == NULL)
7488 break;
7489 li = li->li_next;
7490 }
7491
7492 appended_lines_mark(lnum, added);
7493 if (curwin->w_cursor.lnum > lnum)
7494 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007496 else
7497 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498}
7499
7500/*
7501 * "argc()" function
7502 */
7503/* ARGSUSED */
7504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007505f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007506 typval_T *argvars;
7507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007509 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510}
7511
7512/*
7513 * "argidx()" function
7514 */
7515/* ARGSUSED */
7516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007517f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007518 typval_T *argvars;
7519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007521 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522}
7523
7524/*
7525 * "argv(nr)" function
7526 */
7527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007528f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 typval_T *argvars;
7530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531{
7532 int idx;
7533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007534 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007536 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007538 rettv->vval.v_string = NULL;
7539 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540}
7541
7542/*
7543 * "browse(save, title, initdir, default)" function
7544 */
7545/* ARGSUSED */
7546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007547f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 typval_T *argvars;
7549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550{
7551#ifdef FEAT_BROWSE
7552 int save;
7553 char_u *title;
7554 char_u *initdir;
7555 char_u *defname;
7556 char_u buf[NUMBUFLEN];
7557 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007558 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007560 save = get_tv_number_chk(&argvars[0], &error);
7561 title = get_tv_string_chk(&argvars[1]);
7562 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7563 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007565 if (error || title == NULL || initdir == NULL || defname == NULL)
7566 rettv->vval.v_string = NULL;
7567 else
7568 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007569 do_browse(save ? BROWSE_SAVE : 0,
7570 title, defname, NULL, initdir, NULL, curbuf);
7571#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007572 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007573#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007574 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007575}
7576
7577/*
7578 * "browsedir(title, initdir)" function
7579 */
7580/* ARGSUSED */
7581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007582f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 typval_T *argvars;
7584 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007585{
7586#ifdef FEAT_BROWSE
7587 char_u *title;
7588 char_u *initdir;
7589 char_u buf[NUMBUFLEN];
7590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007591 title = get_tv_string_chk(&argvars[0]);
7592 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007594 if (title == NULL || initdir == NULL)
7595 rettv->vval.v_string = NULL;
7596 else
7597 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007598 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007602 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603}
7604
Bram Moolenaar33570922005-01-25 22:26:29 +00007605static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007606
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607/*
7608 * Find a buffer by number or exact name.
7609 */
7610 static buf_T *
7611find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613{
7614 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007616 if (avar->v_type == VAR_NUMBER)
7617 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007618 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007620 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007621 if (buf == NULL)
7622 {
7623 /* No full path name match, try a match with a URL or a "nofile"
7624 * buffer, these don't use the full path. */
7625 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7626 if (buf->b_fname != NULL
7627 && (path_with_url(buf->b_fname)
7628#ifdef FEAT_QUICKFIX
7629 || bt_nofile(buf)
7630#endif
7631 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007632 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007633 break;
7634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 }
7636 return buf;
7637}
7638
7639/*
7640 * "bufexists(expr)" function
7641 */
7642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007643f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007644 typval_T *argvars;
7645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007647 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648}
7649
7650/*
7651 * "buflisted(expr)" function
7652 */
7653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007654f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007655 typval_T *argvars;
7656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657{
7658 buf_T *buf;
7659
7660 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007661 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662}
7663
7664/*
7665 * "bufloaded(expr)" function
7666 */
7667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007668f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007669 typval_T *argvars;
7670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
7672 buf_T *buf;
7673
7674 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007675 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676}
7677
Bram Moolenaar33570922005-01-25 22:26:29 +00007678static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007679
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680/*
7681 * Get buffer by number or pattern.
7682 */
7683 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007684get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 int save_magic;
7689 char_u *save_cpo;
7690 buf_T *buf;
7691
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007692 if (tv->v_type == VAR_NUMBER)
7693 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007694 if (tv->v_type != VAR_STRING)
7695 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 if (name == NULL || *name == NUL)
7697 return curbuf;
7698 if (name[0] == '$' && name[1] == NUL)
7699 return lastbuf;
7700
7701 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7702 save_magic = p_magic;
7703 p_magic = TRUE;
7704 save_cpo = p_cpo;
7705 p_cpo = (char_u *)"";
7706
7707 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7708 TRUE, FALSE));
7709
7710 p_magic = save_magic;
7711 p_cpo = save_cpo;
7712
7713 /* If not found, try expanding the name, like done for bufexists(). */
7714 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007715 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716
7717 return buf;
7718}
7719
7720/*
7721 * "bufname(expr)" function
7722 */
7723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007724f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 typval_T *argvars;
7726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
7728 buf_T *buf;
7729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007730 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007732 buf = get_buf_tv(&argvars[0]);
7733 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007735 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007737 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 --emsg_off;
7739}
7740
7741/*
7742 * "bufnr(expr)" function
7743 */
7744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007745f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007746 typval_T *argvars;
7747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748{
7749 buf_T *buf;
7750
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007751 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007753 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007755 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007757 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 --emsg_off;
7759}
7760
7761/*
7762 * "bufwinnr(nr)" function
7763 */
7764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 typval_T *argvars;
7767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
7769#ifdef FEAT_WINDOWS
7770 win_T *wp;
7771 int winnr = 0;
7772#endif
7773 buf_T *buf;
7774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007775 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007777 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778#ifdef FEAT_WINDOWS
7779 for (wp = firstwin; wp; wp = wp->w_next)
7780 {
7781 ++winnr;
7782 if (wp->w_buffer == buf)
7783 break;
7784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007785 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007787 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788#endif
7789 --emsg_off;
7790}
7791
7792/*
7793 * "byte2line(byte)" function
7794 */
7795/*ARGSUSED*/
7796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007797f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 typval_T *argvars;
7799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
7801#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803#else
7804 long boff = 0;
7805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007806 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007808 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007810 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 (linenr_T)0, &boff);
7812#endif
7813}
7814
7815/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007816 * "byteidx()" function
7817 */
7818/*ARGSUSED*/
7819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007820f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007821 typval_T *argvars;
7822 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007823{
7824#ifdef FEAT_MBYTE
7825 char_u *t;
7826#endif
7827 char_u *str;
7828 long idx;
7829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007830 str = get_tv_string_chk(&argvars[0]);
7831 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007832 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007833 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007834 return;
7835
7836#ifdef FEAT_MBYTE
7837 t = str;
7838 for ( ; idx > 0; idx--)
7839 {
7840 if (*t == NUL) /* EOL reached */
7841 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007842 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007845#else
7846 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007847 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007848#endif
7849}
7850
7851/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007852 * "call(func, arglist)" function
7853 */
7854 static void
7855f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007856 typval_T *argvars;
7857 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007858{
7859 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007861 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007863 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007865
7866 rettv->vval.v_number = 0;
7867 if (argvars[1].v_type != VAR_LIST)
7868 {
7869 EMSG(_(e_listreq));
7870 return;
7871 }
7872 if (argvars[1].vval.v_list == NULL)
7873 return;
7874
7875 if (argvars[0].v_type == VAR_FUNC)
7876 func = argvars[0].vval.v_string;
7877 else
7878 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007879 if (*func == NUL)
7880 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007881
Bram Moolenaare9a41262005-01-15 22:18:47 +00007882 if (argvars[2].v_type != VAR_UNKNOWN)
7883 {
7884 if (argvars[2].v_type != VAR_DICT)
7885 {
7886 EMSG(_(e_dictreq));
7887 return;
7888 }
7889 selfdict = argvars[2].vval.v_dict;
7890 }
7891
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007892 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7893 item = item->li_next)
7894 {
7895 if (argc == MAX_FUNC_ARGS)
7896 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007897 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007898 break;
7899 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007900 /* Make a copy of each argument. This is needed to be able to set
7901 * v_lock to VAR_FIXED in the copy without changing the original list.
7902 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007903 copy_tv(&item->li_tv, &argv[argc++]);
7904 }
7905
7906 if (item == NULL)
7907 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007908 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7909 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007910
7911 /* Free the arguments. */
7912 while (argc > 0)
7913 clear_tv(&argv[--argc]);
7914}
7915
7916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 * "char2nr(string)" function
7918 */
7919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007920f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007921 typval_T *argvars;
7922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923{
7924#ifdef FEAT_MBYTE
7925 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007926 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 else
7928#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007929 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930}
7931
7932/*
7933 * "cindent(lnum)" function
7934 */
7935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007936f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007937 typval_T *argvars;
7938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939{
7940#ifdef FEAT_CINDENT
7941 pos_T pos;
7942 linenr_T lnum;
7943
7944 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007945 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7947 {
7948 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007949 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 curwin->w_cursor = pos;
7951 }
7952 else
7953#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007954 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955}
7956
7957/*
7958 * "col(string)" function
7959 */
7960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007961f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007962 typval_T *argvars;
7963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
7965 colnr_T col = 0;
7966 pos_T *fp;
7967
7968 fp = var2fpos(&argvars[0], FALSE);
7969 if (fp != NULL)
7970 {
7971 if (fp->col == MAXCOL)
7972 {
7973 /* '> can be MAXCOL, get the length of the line then */
7974 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7975 col = STRLEN(ml_get(fp->lnum)) + 1;
7976 else
7977 col = MAXCOL;
7978 }
7979 else
7980 {
7981 col = fp->col + 1;
7982#ifdef FEAT_VIRTUALEDIT
7983 /* col(".") when the cursor is on the NUL at the end of the line
7984 * because of "coladd" can be seen as an extra column. */
7985 if (virtual_active() && fp == &curwin->w_cursor)
7986 {
7987 char_u *p = ml_get_cursor();
7988
7989 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7990 curwin->w_virtcol - curwin->w_cursor.coladd))
7991 {
7992# ifdef FEAT_MBYTE
7993 int l;
7994
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007995 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 col += l;
7997# else
7998 if (*p != NUL && p[1] == NUL)
7999 ++col;
8000# endif
8001 }
8002 }
8003#endif
8004 }
8005 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008006 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007}
8008
Bram Moolenaar572cb562005-08-05 21:35:02 +00008009#if defined(FEAT_INS_EXPAND)
8010/*
8011 * "complete_add()" function
8012 */
8013/*ARGSUSED*/
8014 static void
8015f_complete_add(argvars, rettv)
8016 typval_T *argvars;
8017 typval_T *rettv;
8018{
8019 char_u *s;
8020
8021 s = get_tv_string_chk(&argvars[0]);
8022 if (s != NULL)
8023 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
8024}
8025
8026/*
8027 * "complete_check()" function
8028 */
8029/*ARGSUSED*/
8030 static void
8031f_complete_check(argvars, rettv)
8032 typval_T *argvars;
8033 typval_T *rettv;
8034{
8035 int saved = RedrawingDisabled;
8036
8037 RedrawingDisabled = 0;
8038 ins_compl_check_keys(0);
8039 rettv->vval.v_number = compl_interrupted;
8040 RedrawingDisabled = saved;
8041}
8042#endif
8043
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044/*
8045 * "confirm(message, buttons[, default [, type]])" function
8046 */
8047/*ARGSUSED*/
8048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008049f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008050 typval_T *argvars;
8051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052{
8053#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8054 char_u *message;
8055 char_u *buttons = NULL;
8056 char_u buf[NUMBUFLEN];
8057 char_u buf2[NUMBUFLEN];
8058 int def = 1;
8059 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008060 char_u *typestr;
8061 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008063 message = get_tv_string_chk(&argvars[0]);
8064 if (message == NULL)
8065 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008066 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008068 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8069 if (buttons == NULL)
8070 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008071 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008073 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008074 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008076 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8077 if (typestr == NULL)
8078 error = TRUE;
8079 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008081 switch (TOUPPER_ASC(*typestr))
8082 {
8083 case 'E': type = VIM_ERROR; break;
8084 case 'Q': type = VIM_QUESTION; break;
8085 case 'I': type = VIM_INFO; break;
8086 case 'W': type = VIM_WARNING; break;
8087 case 'G': type = VIM_GENERIC; break;
8088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 }
8090 }
8091 }
8092 }
8093
8094 if (buttons == NULL || *buttons == NUL)
8095 buttons = (char_u *)_("&Ok");
8096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008097 if (error)
8098 rettv->vval.v_number = 0;
8099 else
8100 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 def, NULL);
8102#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008103 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104#endif
8105}
8106
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008107/*
8108 * "copy()" function
8109 */
8110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008111f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008112 typval_T *argvars;
8113 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008114{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008115 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008116}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117
8118/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008119 * "count()" function
8120 */
8121 static void
8122f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008123 typval_T *argvars;
8124 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008125{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008126 long n = 0;
8127 int ic = FALSE;
8128
Bram Moolenaare9a41262005-01-15 22:18:47 +00008129 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008130 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008131 listitem_T *li;
8132 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008133 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008134
Bram Moolenaare9a41262005-01-15 22:18:47 +00008135 if ((l = argvars[0].vval.v_list) != NULL)
8136 {
8137 li = l->lv_first;
8138 if (argvars[2].v_type != VAR_UNKNOWN)
8139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008140 int error = FALSE;
8141
8142 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008143 if (argvars[3].v_type != VAR_UNKNOWN)
8144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008145 idx = get_tv_number_chk(&argvars[3], &error);
8146 if (!error)
8147 {
8148 li = list_find(l, idx);
8149 if (li == NULL)
8150 EMSGN(_(e_listidx), idx);
8151 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008152 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008153 if (error)
8154 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008155 }
8156
8157 for ( ; li != NULL; li = li->li_next)
8158 if (tv_equal(&li->li_tv, &argvars[1], ic))
8159 ++n;
8160 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008161 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008162 else if (argvars[0].v_type == VAR_DICT)
8163 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008164 int todo;
8165 dict_T *d;
8166 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008167
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008168 if ((d = argvars[0].vval.v_dict) != NULL)
8169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008170 int error = FALSE;
8171
Bram Moolenaare9a41262005-01-15 22:18:47 +00008172 if (argvars[2].v_type != VAR_UNKNOWN)
8173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008174 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008175 if (argvars[3].v_type != VAR_UNKNOWN)
8176 EMSG(_(e_invarg));
8177 }
8178
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008179 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008180 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008181 {
8182 if (!HASHITEM_EMPTY(hi))
8183 {
8184 --todo;
8185 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8186 ++n;
8187 }
8188 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008189 }
8190 }
8191 else
8192 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008193 rettv->vval.v_number = n;
8194}
8195
8196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8198 *
8199 * Checks the existence of a cscope connection.
8200 */
8201/*ARGSUSED*/
8202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008204 typval_T *argvars;
8205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
8207#ifdef FEAT_CSCOPE
8208 int num = 0;
8209 char_u *dbpath = NULL;
8210 char_u *prepend = NULL;
8211 char_u buf[NUMBUFLEN];
8212
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008213 if (argvars[0].v_type != VAR_UNKNOWN
8214 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008216 num = (int)get_tv_number(&argvars[0]);
8217 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008218 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008219 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 }
8221
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008222 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008224 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225#endif
8226}
8227
8228/*
8229 * "cursor(lnum, col)" function
8230 *
8231 * Moves the cursor to the specified line and column
8232 */
8233/*ARGSUSED*/
8234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008235f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008236 typval_T *argvars;
8237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238{
8239 long line, col;
8240
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008241 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008242 col = get_tv_number_chk(&argvars[1], NULL);
8243 if (line < 0 || col < 0)
8244 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 if (line > 0)
8246 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 if (col > 0)
8248 curwin->w_cursor.col = col - 1;
8249#ifdef FEAT_VIRTUALEDIT
8250 curwin->w_cursor.coladd = 0;
8251#endif
8252
8253 /* Make sure the cursor is in a valid position. */
8254 check_cursor();
8255#ifdef FEAT_MBYTE
8256 /* Correct cursor for multi-byte character. */
8257 if (has_mbyte)
8258 mb_adjust_cursor();
8259#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008260
8261 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262}
8263
8264/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008265 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 */
8267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008268f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008269 typval_T *argvars;
8270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008272 int noref = 0;
8273
8274 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008275 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008276 if (noref < 0 || noref > 1)
8277 EMSG(_(e_invarg));
8278 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008279 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280}
8281
8282/*
8283 * "delete()" function
8284 */
8285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008286f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008287 typval_T *argvars;
8288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289{
8290 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008291 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008293 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294}
8295
8296/*
8297 * "did_filetype()" function
8298 */
8299/*ARGSUSED*/
8300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008301f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008302 typval_T *argvars;
8303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304{
8305#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008306 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008308 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309#endif
8310}
8311
8312/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008313 * "diff_filler()" function
8314 */
8315/*ARGSUSED*/
8316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008317f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008318 typval_T *argvars;
8319 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008320{
8321#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008322 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008323#endif
8324}
8325
8326/*
8327 * "diff_hlID()" function
8328 */
8329/*ARGSUSED*/
8330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008331f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008332 typval_T *argvars;
8333 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008334{
8335#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008336 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008337 static linenr_T prev_lnum = 0;
8338 static int changedtick = 0;
8339 static int fnum = 0;
8340 static int change_start = 0;
8341 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008342 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008343 int filler_lines;
8344 int col;
8345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008346 if (lnum < 0) /* ignore type error in {lnum} arg */
8347 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008348 if (lnum != prev_lnum
8349 || changedtick != curbuf->b_changedtick
8350 || fnum != curbuf->b_fnum)
8351 {
8352 /* New line, buffer, change: need to get the values. */
8353 filler_lines = diff_check(curwin, lnum);
8354 if (filler_lines < 0)
8355 {
8356 if (filler_lines == -1)
8357 {
8358 change_start = MAXCOL;
8359 change_end = -1;
8360 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8361 hlID = HLF_ADD; /* added line */
8362 else
8363 hlID = HLF_CHD; /* changed line */
8364 }
8365 else
8366 hlID = HLF_ADD; /* added line */
8367 }
8368 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008369 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008370 prev_lnum = lnum;
8371 changedtick = curbuf->b_changedtick;
8372 fnum = curbuf->b_fnum;
8373 }
8374
8375 if (hlID == HLF_CHD || hlID == HLF_TXD)
8376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008377 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008378 if (col >= change_start && col <= change_end)
8379 hlID = HLF_TXD; /* changed text */
8380 else
8381 hlID = HLF_CHD; /* changed line */
8382 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008383 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008384#endif
8385}
8386
8387/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008388 * "empty({expr})" function
8389 */
8390 static void
8391f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008392 typval_T *argvars;
8393 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008394{
8395 int n;
8396
8397 switch (argvars[0].v_type)
8398 {
8399 case VAR_STRING:
8400 case VAR_FUNC:
8401 n = argvars[0].vval.v_string == NULL
8402 || *argvars[0].vval.v_string == NUL;
8403 break;
8404 case VAR_NUMBER:
8405 n = argvars[0].vval.v_number == 0;
8406 break;
8407 case VAR_LIST:
8408 n = argvars[0].vval.v_list == NULL
8409 || argvars[0].vval.v_list->lv_first == NULL;
8410 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008411 case VAR_DICT:
8412 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008413 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008414 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008415 default:
8416 EMSG2(_(e_intern2), "f_empty()");
8417 n = 0;
8418 }
8419
8420 rettv->vval.v_number = n;
8421}
8422
8423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 * "escape({string}, {chars})" function
8425 */
8426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008427f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008428 typval_T *argvars;
8429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430{
8431 char_u buf[NUMBUFLEN];
8432
Bram Moolenaar758711c2005-02-02 23:11:38 +00008433 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8434 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008435 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436}
8437
8438/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008439 * "eval()" function
8440 */
8441/*ARGSUSED*/
8442 static void
8443f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008444 typval_T *argvars;
8445 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008446{
8447 char_u *s;
8448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008449 s = get_tv_string_chk(&argvars[0]);
8450 if (s != NULL)
8451 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008453 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8454 {
8455 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008456 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008457 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008458 else if (*s != NUL)
8459 EMSG(_(e_trailing));
8460}
8461
8462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 * "eventhandler()" function
8464 */
8465/*ARGSUSED*/
8466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008467f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008468 typval_T *argvars;
8469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008471 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472}
8473
8474/*
8475 * "executable()" function
8476 */
8477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008478f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008479 typval_T *argvars;
8480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008482 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483}
8484
8485/*
8486 * "exists()" function
8487 */
8488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008489f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008490 typval_T *argvars;
8491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
8493 char_u *p;
8494 char_u *name;
8495 int n = FALSE;
8496 int len = 0;
8497
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 if (*p == '$') /* environment variable */
8500 {
8501 /* first try "normal" environment variables (fast) */
8502 if (mch_getenv(p + 1) != NULL)
8503 n = TRUE;
8504 else
8505 {
8506 /* try expanding things like $VIM and ${HOME} */
8507 p = expand_env_save(p);
8508 if (p != NULL && *p != '$')
8509 n = TRUE;
8510 vim_free(p);
8511 }
8512 }
8513 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008514 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 else if (*p == '*') /* internal or user defined function */
8516 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008517 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 }
8519 else if (*p == ':')
8520 {
8521 n = cmd_exists(p + 1);
8522 }
8523 else if (*p == '#')
8524 {
8525#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008526 if (p[1] == '#')
8527 n = autocmd_supported(p + 2);
8528 else
8529 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530#endif
8531 }
8532 else /* internal variable */
8533 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008534 char_u *tofree;
8535 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008537 /* get_name_len() takes care of expanding curly braces */
8538 name = p;
8539 len = get_name_len(&p, &tofree, TRUE, FALSE);
8540 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008542 if (tofree != NULL)
8543 name = tofree;
8544 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8545 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008547 /* handle d.key, l[idx], f(expr) */
8548 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8549 if (n)
8550 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 }
8552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008554 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 }
8556
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008557 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558}
8559
8560/*
8561 * "expand()" function
8562 */
8563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008564f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008565 typval_T *argvars;
8566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567{
8568 char_u *s;
8569 int len;
8570 char_u *errormsg;
8571 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8572 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008573 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008575 rettv->v_type = VAR_STRING;
8576 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 if (*s == '%' || *s == '#' || *s == '<')
8578 {
8579 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 --emsg_off;
8582 }
8583 else
8584 {
8585 /* When the optional second argument is non-zero, don't remove matches
8586 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008587 if (argvars[1].v_type != VAR_UNKNOWN
8588 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008590 if (!error)
8591 {
8592 ExpandInit(&xpc);
8593 xpc.xp_context = EXPAND_FILES;
8594 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8595 ExpandCleanup(&xpc);
8596 }
8597 else
8598 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 }
8600}
8601
8602/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008603 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008604 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008605 */
8606 static void
8607f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008608 typval_T *argvars;
8609 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008610{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008611 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008612 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008613 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008614 list_T *l1, *l2;
8615 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008616 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008617 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008618
Bram Moolenaare9a41262005-01-15 22:18:47 +00008619 l1 = argvars[0].vval.v_list;
8620 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008621 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8622 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008623 {
8624 if (argvars[2].v_type != VAR_UNKNOWN)
8625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008626 before = get_tv_number_chk(&argvars[2], &error);
8627 if (error)
8628 return; /* type error; errmsg already given */
8629
Bram Moolenaar758711c2005-02-02 23:11:38 +00008630 if (before == l1->lv_len)
8631 item = NULL;
8632 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008634 item = list_find(l1, before);
8635 if (item == NULL)
8636 {
8637 EMSGN(_(e_listidx), before);
8638 return;
8639 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008640 }
8641 }
8642 else
8643 item = NULL;
8644 list_extend(l1, l2, item);
8645
Bram Moolenaare9a41262005-01-15 22:18:47 +00008646 copy_tv(&argvars[0], rettv);
8647 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008648 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008649 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8650 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008651 dict_T *d1, *d2;
8652 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008653 char_u *action;
8654 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008655 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008656 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008657
8658 d1 = argvars[0].vval.v_dict;
8659 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008660 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8661 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008662 {
8663 /* Check the third argument. */
8664 if (argvars[2].v_type != VAR_UNKNOWN)
8665 {
8666 static char *(av[]) = {"keep", "force", "error"};
8667
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008668 action = get_tv_string_chk(&argvars[2]);
8669 if (action == NULL)
8670 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008671 for (i = 0; i < 3; ++i)
8672 if (STRCMP(action, av[i]) == 0)
8673 break;
8674 if (i == 3)
8675 {
8676 EMSGN(_(e_invarg2), action);
8677 return;
8678 }
8679 }
8680 else
8681 action = (char_u *)"force";
8682
8683 /* Go over all entries in the second dict and add them to the
8684 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008685 todo = d2->dv_hashtab.ht_used;
8686 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008687 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008688 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008689 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008690 --todo;
8691 di1 = dict_find(d1, hi2->hi_key, -1);
8692 if (di1 == NULL)
8693 {
8694 di1 = dictitem_copy(HI2DI(hi2));
8695 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8696 dictitem_free(di1);
8697 }
8698 else if (*action == 'e')
8699 {
8700 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8701 break;
8702 }
8703 else if (*action == 'f')
8704 {
8705 clear_tv(&di1->di_tv);
8706 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8707 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008708 }
8709 }
8710
Bram Moolenaare9a41262005-01-15 22:18:47 +00008711 copy_tv(&argvars[0], rettv);
8712 }
8713 }
8714 else
8715 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008716}
8717
8718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 * "filereadable()" function
8720 */
8721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008722f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008723 typval_T *argvars;
8724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725{
8726 FILE *fd;
8727 char_u *p;
8728 int n;
8729
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8732 {
8733 n = TRUE;
8734 fclose(fd);
8735 }
8736 else
8737 n = FALSE;
8738
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008739 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740}
8741
8742/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008743 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 * rights to write into.
8745 */
8746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008748 typval_T *argvars;
8749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008751 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752}
8753
Bram Moolenaar33570922005-01-25 22:26:29 +00008754static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008755
8756 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008757findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008758 typval_T *argvars;
8759 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008760 int dir;
8761{
8762#ifdef FEAT_SEARCHPATH
8763 char_u *fname;
8764 char_u *fresult = NULL;
8765 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8766 char_u *p;
8767 char_u pathbuf[NUMBUFLEN];
8768 int count = 1;
8769 int first = TRUE;
8770
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008771 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008772
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008773 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008775 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8776 if (p == NULL)
8777 count = -1; /* error */
8778 else
8779 {
8780 if (*p != NUL)
8781 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008783 if (argvars[2].v_type != VAR_UNKNOWN)
8784 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8785 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008786 }
8787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008788 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008789 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008790 do
8791 {
8792 vim_free(fresult);
8793 fresult = find_file_in_path_option(first ? fname : NULL,
8794 first ? (int)STRLEN(fname) : 0,
8795 0, first, path, dir, NULL);
8796 first = FALSE;
8797 } while (--count > 0 && fresult != NULL);
8798 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008799
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008800 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008801#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008803#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008804 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008805}
8806
Bram Moolenaar33570922005-01-25 22:26:29 +00008807static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8808static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008809
8810/*
8811 * Implementation of map() and filter().
8812 */
8813 static void
8814filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008815 typval_T *argvars;
8816 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008817 int map;
8818{
8819 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008820 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008821 listitem_T *li, *nli;
8822 list_T *l = NULL;
8823 dictitem_T *di;
8824 hashtab_T *ht;
8825 hashitem_T *hi;
8826 dict_T *d = NULL;
8827 typval_T save_val;
8828 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008829 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008830 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008831 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar280f1262006-01-30 00:14:18 +00008832 int save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008833
8834 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008835 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008836 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008837 if ((l = argvars[0].vval.v_list) == NULL
8838 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008839 return;
8840 }
8841 else if (argvars[0].v_type == VAR_DICT)
8842 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008843 if ((d = argvars[0].vval.v_dict) == NULL
8844 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008845 return;
8846 }
8847 else
8848 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008849 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008850 return;
8851 }
8852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008853 expr = get_tv_string_buf_chk(&argvars[1], buf);
8854 /* On type errors, the preceding call has already displayed an error
8855 * message. Avoid a misleading error message for an empty string that
8856 * was not passed as argument. */
8857 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008858 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008859 prepare_vimvar(VV_VAL, &save_val);
8860 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008861
Bram Moolenaar280f1262006-01-30 00:14:18 +00008862 /* We reset "called_emsg" to be able to detect whether an error
8863 * occurred during evaluation of the expression. "did_emsg" can't be
8864 * used, because it is reset when calling a function. */
8865 save_called_emsg = called_emsg;
8866 called_emsg = FALSE;
8867
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008868 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008870 prepare_vimvar(VV_KEY, &save_key);
8871 vimvars[VV_KEY].vv_type = VAR_STRING;
8872
8873 ht = &d->dv_hashtab;
8874 hash_lock(ht);
8875 todo = ht->ht_used;
8876 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008877 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008878 if (!HASHITEM_EMPTY(hi))
8879 {
8880 --todo;
8881 di = HI2DI(hi);
8882 if (tv_check_lock(di->di_tv.v_lock, msg))
8883 break;
8884 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008885 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
8886 || called_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008887 break;
8888 if (!map && rem)
8889 dictitem_remove(d, di);
8890 clear_tv(&vimvars[VV_KEY].vv_tv);
8891 }
8892 }
8893 hash_unlock(ht);
8894
8895 restore_vimvar(VV_KEY, &save_key);
8896 }
8897 else
8898 {
8899 for (li = l->lv_first; li != NULL; li = nli)
8900 {
8901 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008902 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008903 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008904 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
8905 || called_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008906 break;
8907 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008908 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008909 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008910 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008911
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008912 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008913
8914 called_emsg |= save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008915 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008916
8917 copy_tv(&argvars[0], rettv);
8918}
8919
8920 static int
8921filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008922 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008923 char_u *expr;
8924 int map;
8925 int *remp;
8926{
Bram Moolenaar33570922005-01-25 22:26:29 +00008927 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008928 char_u *s;
8929
Bram Moolenaar33570922005-01-25 22:26:29 +00008930 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008931 s = expr;
8932 if (eval1(&s, &rettv, TRUE) == FAIL)
8933 return FAIL;
8934 if (*s != NUL) /* check for trailing chars after expr */
8935 {
8936 EMSG2(_(e_invexpr2), s);
8937 return FAIL;
8938 }
8939 if (map)
8940 {
8941 /* map(): replace the list item value */
8942 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008943 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008944 *tv = rettv;
8945 }
8946 else
8947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008948 int error = FALSE;
8949
Bram Moolenaare9a41262005-01-15 22:18:47 +00008950 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008951 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008952 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008953 /* On type error, nothing has been removed; return FAIL to stop the
8954 * loop. The error message was given by get_tv_number_chk(). */
8955 if (error)
8956 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008957 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008958 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008959 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008960}
8961
8962/*
8963 * "filter()" function
8964 */
8965 static void
8966f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008967 typval_T *argvars;
8968 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008969{
8970 filter_map(argvars, rettv, FALSE);
8971}
8972
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008973/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008974 * "finddir({fname}[, {path}[, {count}]])" function
8975 */
8976 static void
8977f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 typval_T *argvars;
8979 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008980{
8981 findfilendir(argvars, rettv, TRUE);
8982}
8983
8984/*
8985 * "findfile({fname}[, {path}[, {count}]])" function
8986 */
8987 static void
8988f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008989 typval_T *argvars;
8990 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008991{
8992 findfilendir(argvars, rettv, FALSE);
8993}
8994
8995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 * "fnamemodify({fname}, {mods})" function
8997 */
8998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008999f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009000 typval_T *argvars;
9001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002{
9003 char_u *fname;
9004 char_u *mods;
9005 int usedlen = 0;
9006 int len;
9007 char_u *fbuf = NULL;
9008 char_u buf[NUMBUFLEN];
9009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009010 fname = get_tv_string_chk(&argvars[0]);
9011 mods = get_tv_string_buf_chk(&argvars[1], buf);
9012 if (fname == NULL || mods == NULL)
9013 fname = NULL;
9014 else
9015 {
9016 len = (int)STRLEN(fname);
9017 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009020 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009022 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009024 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 vim_free(fbuf);
9026}
9027
Bram Moolenaar33570922005-01-25 22:26:29 +00009028static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029
9030/*
9031 * "foldclosed()" function
9032 */
9033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009034foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009035 typval_T *argvars;
9036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 int end;
9038{
9039#ifdef FEAT_FOLDING
9040 linenr_T lnum;
9041 linenr_T first, last;
9042
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009043 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9045 {
9046 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9047 {
9048 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009051 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 return;
9053 }
9054 }
9055#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009056 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057}
9058
9059/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009060 * "foldclosed()" function
9061 */
9062 static void
9063f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009064 typval_T *argvars;
9065 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009066{
9067 foldclosed_both(argvars, rettv, FALSE);
9068}
9069
9070/*
9071 * "foldclosedend()" function
9072 */
9073 static void
9074f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009075 typval_T *argvars;
9076 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009077{
9078 foldclosed_both(argvars, rettv, TRUE);
9079}
9080
9081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 * "foldlevel()" function
9083 */
9084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009086 typval_T *argvars;
9087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088{
9089#ifdef FEAT_FOLDING
9090 linenr_T lnum;
9091
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009092 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 else
9096#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098}
9099
9100/*
9101 * "foldtext()" function
9102 */
9103/*ARGSUSED*/
9104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009105f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009106 typval_T *argvars;
9107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
9109#ifdef FEAT_FOLDING
9110 linenr_T lnum;
9111 char_u *s;
9112 char_u *r;
9113 int len;
9114 char *txt;
9115#endif
9116
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009117 rettv->v_type = VAR_STRING;
9118 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009120 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9121 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9122 <= curbuf->b_ml.ml_line_count
9123 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 {
9125 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009126 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9127 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 {
9129 if (!linewhite(lnum))
9130 break;
9131 ++lnum;
9132 }
9133
9134 /* Find interesting text in this line. */
9135 s = skipwhite(ml_get(lnum));
9136 /* skip C comment-start */
9137 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009140 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009141 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009142 {
9143 s = skipwhite(ml_get(lnum + 1));
9144 if (*s == '*')
9145 s = skipwhite(s + 1);
9146 }
9147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 txt = _("+-%s%3ld lines: ");
9149 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009150 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 + 20 /* for %3ld */
9152 + STRLEN(s))); /* concatenated */
9153 if (r != NULL)
9154 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009155 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9156 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9157 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 len = (int)STRLEN(r);
9159 STRCAT(r, s);
9160 /* remove 'foldmarker' and 'commentstring' */
9161 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009162 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 }
9164 }
9165#endif
9166}
9167
9168/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009169 * "foldtextresult(lnum)" function
9170 */
9171/*ARGSUSED*/
9172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009174 typval_T *argvars;
9175 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009176{
9177#ifdef FEAT_FOLDING
9178 linenr_T lnum;
9179 char_u *text;
9180 char_u buf[51];
9181 foldinfo_T foldinfo;
9182 int fold_count;
9183#endif
9184
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009185 rettv->v_type = VAR_STRING;
9186 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009187#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009189 /* treat illegal types and illegal string values for {lnum} the same */
9190 if (lnum < 0)
9191 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009192 fold_count = foldedCount(curwin, lnum, &foldinfo);
9193 if (fold_count > 0)
9194 {
9195 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9196 &foldinfo, buf);
9197 if (text == buf)
9198 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009199 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009200 }
9201#endif
9202}
9203
9204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 * "foreground()" function
9206 */
9207/*ARGSUSED*/
9208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009209f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009210 typval_T *argvars;
9211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009213 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214#ifdef FEAT_GUI
9215 if (gui.in_use)
9216 gui_mch_set_foreground();
9217#else
9218# ifdef WIN32
9219 win32_set_foreground();
9220# endif
9221#endif
9222}
9223
9224/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009225 * "function()" function
9226 */
9227/*ARGSUSED*/
9228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009230 typval_T *argvars;
9231 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009232{
9233 char_u *s;
9234
Bram Moolenaara7043832005-01-21 11:56:39 +00009235 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009236 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009237 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009238 EMSG2(_(e_invarg2), s);
9239 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009240 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009241 else
9242 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009243 rettv->vval.v_string = vim_strsave(s);
9244 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009245 }
9246}
9247
9248/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009249 * "garbagecollect()" function
9250 */
9251/*ARGSUSED*/
9252 static void
9253f_garbagecollect(argvars, rettv)
9254 typval_T *argvars;
9255 typval_T *rettv;
9256{
9257 garbage_collect();
9258}
9259
9260/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009261 * "get()" function
9262 */
9263 static void
9264f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009265 typval_T *argvars;
9266 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009267{
Bram Moolenaar33570922005-01-25 22:26:29 +00009268 listitem_T *li;
9269 list_T *l;
9270 dictitem_T *di;
9271 dict_T *d;
9272 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009273
Bram Moolenaare9a41262005-01-15 22:18:47 +00009274 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009275 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009276 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009278 int error = FALSE;
9279
9280 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9281 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009282 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009283 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009284 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009285 else if (argvars[0].v_type == VAR_DICT)
9286 {
9287 if ((d = argvars[0].vval.v_dict) != NULL)
9288 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009289 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009290 if (di != NULL)
9291 tv = &di->di_tv;
9292 }
9293 }
9294 else
9295 EMSG2(_(e_listdictarg), "get()");
9296
9297 if (tv == NULL)
9298 {
9299 if (argvars[2].v_type == VAR_UNKNOWN)
9300 rettv->vval.v_number = 0;
9301 else
9302 copy_tv(&argvars[2], rettv);
9303 }
9304 else
9305 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009306}
9307
Bram Moolenaar342337a2005-07-21 21:11:17 +00009308static 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 +00009309
9310/*
9311 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009312 * Return a range (from start to end) of lines in rettv from the specified
9313 * buffer.
9314 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009315 */
9316 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009317get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009318 buf_T *buf;
9319 linenr_T start;
9320 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009321 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009322 typval_T *rettv;
9323{
9324 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009325 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009326
Bram Moolenaar342337a2005-07-21 21:11:17 +00009327 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009328 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009329 l = list_alloc();
9330 if (l == NULL)
9331 return;
9332
9333 rettv->vval.v_list = l;
9334 rettv->v_type = VAR_LIST;
9335 ++l->lv_refcount;
9336 }
9337 else
9338 rettv->vval.v_number = 0;
9339
9340 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9341 return;
9342
9343 if (!retlist)
9344 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009345 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9346 p = ml_get_buf(buf, start, FALSE);
9347 else
9348 p = (char_u *)"";
9349
9350 rettv->v_type = VAR_STRING;
9351 rettv->vval.v_string = vim_strsave(p);
9352 }
9353 else
9354 {
9355 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009356 return;
9357
9358 if (start < 1)
9359 start = 1;
9360 if (end > buf->b_ml.ml_line_count)
9361 end = buf->b_ml.ml_line_count;
9362 while (start <= end)
Bram Moolenaar4463f292005-09-25 22:20:24 +00009363 if (list_append_string(l, ml_get_buf(buf, start++, FALSE), -1)
9364 == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009365 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009366 }
9367}
9368
9369/*
9370 * "getbufline()" function
9371 */
9372 static void
9373f_getbufline(argvars, rettv)
9374 typval_T *argvars;
9375 typval_T *rettv;
9376{
9377 linenr_T lnum;
9378 linenr_T end;
9379 buf_T *buf;
9380
9381 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9382 ++emsg_off;
9383 buf = get_buf_tv(&argvars[0]);
9384 --emsg_off;
9385
Bram Moolenaar661b1822005-07-28 22:36:45 +00009386 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009387 if (argvars[2].v_type == VAR_UNKNOWN)
9388 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009389 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009390 end = get_tv_lnum_buf(&argvars[2], buf);
9391
Bram Moolenaar342337a2005-07-21 21:11:17 +00009392 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009393}
9394
Bram Moolenaar0d660222005-01-07 21:51:51 +00009395/*
9396 * "getbufvar()" function
9397 */
9398 static void
9399f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009400 typval_T *argvars;
9401 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009402{
9403 buf_T *buf;
9404 buf_T *save_curbuf;
9405 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009406 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009407
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009408 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9409 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009410 ++emsg_off;
9411 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009412
9413 rettv->v_type = VAR_STRING;
9414 rettv->vval.v_string = NULL;
9415
9416 if (buf != NULL && varname != NULL)
9417 {
9418 if (*varname == '&') /* buffer-local-option */
9419 {
9420 /* set curbuf to be our buf, temporarily */
9421 save_curbuf = curbuf;
9422 curbuf = buf;
9423
9424 get_option_tv(&varname, rettv, TRUE);
9425
9426 /* restore previous notion of curbuf */
9427 curbuf = save_curbuf;
9428 }
9429 else
9430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009431 if (*varname == NUL)
9432 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9433 * scope prefix before the NUL byte is required by
9434 * find_var_in_ht(). */
9435 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009436 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009437 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009438 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009439 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009440 }
9441 }
9442
9443 --emsg_off;
9444}
9445
9446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 * "getchar()" function
9448 */
9449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009450f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009451 typval_T *argvars;
9452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453{
9454 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009455 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456
9457 ++no_mapping;
9458 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009459 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 /* getchar(): blocking wait. */
9461 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009462 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 /* getchar(1): only check if char avail */
9464 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009465 else if (error || vpeekc() == NUL)
9466 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 n = 0;
9468 else
9469 /* getchar(0) and char avail: return char */
9470 n = safe_vgetc();
9471 --no_mapping;
9472 --allow_keys;
9473
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 if (IS_SPECIAL(n) || mod_mask != 0)
9476 {
9477 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9478 int i = 0;
9479
9480 /* Turn a special key into three bytes, plus modifier. */
9481 if (mod_mask != 0)
9482 {
9483 temp[i++] = K_SPECIAL;
9484 temp[i++] = KS_MODIFIER;
9485 temp[i++] = mod_mask;
9486 }
9487 if (IS_SPECIAL(n))
9488 {
9489 temp[i++] = K_SPECIAL;
9490 temp[i++] = K_SECOND(n);
9491 temp[i++] = K_THIRD(n);
9492 }
9493#ifdef FEAT_MBYTE
9494 else if (has_mbyte)
9495 i += (*mb_char2bytes)(n, temp + i);
9496#endif
9497 else
9498 temp[i++] = n;
9499 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009500 rettv->v_type = VAR_STRING;
9501 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 }
9503}
9504
9505/*
9506 * "getcharmod()" function
9507 */
9508/*ARGSUSED*/
9509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009510f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009511 typval_T *argvars;
9512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515}
9516
9517/*
9518 * "getcmdline()" function
9519 */
9520/*ARGSUSED*/
9521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009522f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009523 typval_T *argvars;
9524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009526 rettv->v_type = VAR_STRING;
9527 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528}
9529
9530/*
9531 * "getcmdpos()" function
9532 */
9533/*ARGSUSED*/
9534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009536 typval_T *argvars;
9537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009539 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540}
9541
9542/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009543 * "getcmdtype()" function
9544 */
9545/*ARGSUSED*/
9546 static void
9547f_getcmdtype(argvars, rettv)
9548 typval_T *argvars;
9549 typval_T *rettv;
9550{
9551 rettv->v_type = VAR_STRING;
9552 rettv->vval.v_string = alloc(2);
9553 if (rettv->vval.v_string != NULL)
9554 {
9555 rettv->vval.v_string[0] = get_cmdline_type();
9556 rettv->vval.v_string[1] = NUL;
9557 }
9558}
9559
9560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 * "getcwd()" function
9562 */
9563/*ARGSUSED*/
9564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009566 typval_T *argvars;
9567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
9569 char_u cwd[MAXPATHL];
9570
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 else
9575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009578 if (rettv->vval.v_string != NULL)
9579 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580#endif
9581 }
9582}
9583
9584/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009585 * "getfontname()" function
9586 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009587/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009590 typval_T *argvars;
9591 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009592{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 rettv->v_type = VAR_STRING;
9594 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009595#ifdef FEAT_GUI
9596 if (gui.in_use)
9597 {
9598 GuiFont font;
9599 char_u *name = NULL;
9600
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009601 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009602 {
9603 /* Get the "Normal" font. Either the name saved by
9604 * hl_set_font_name() or from the font ID. */
9605 font = gui.norm_font;
9606 name = hl_get_font_name();
9607 }
9608 else
9609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009611 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9612 return;
9613 font = gui_mch_get_font(name, FALSE);
9614 if (font == NOFONT)
9615 return; /* Invalid font name, return empty string. */
9616 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009617 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009618 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009619 gui_mch_free_font(font);
9620 }
9621#endif
9622}
9623
9624/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009625 * "getfperm({fname})" function
9626 */
9627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009629 typval_T *argvars;
9630 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009631{
9632 char_u *fname;
9633 struct stat st;
9634 char_u *perm = NULL;
9635 char_u flags[] = "rwx";
9636 int i;
9637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009639
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009641 if (mch_stat((char *)fname, &st) >= 0)
9642 {
9643 perm = vim_strsave((char_u *)"---------");
9644 if (perm != NULL)
9645 {
9646 for (i = 0; i < 9; i++)
9647 {
9648 if (st.st_mode & (1 << (8 - i)))
9649 perm[i] = flags[i % 3];
9650 }
9651 }
9652 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009654}
9655
9656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 * "getfsize({fname})" function
9658 */
9659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009661 typval_T *argvars;
9662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663{
9664 char_u *fname;
9665 struct stat st;
9666
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009667 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670
9671 if (mch_stat((char *)fname, &st) >= 0)
9672 {
9673 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 }
9678 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680}
9681
9682/*
9683 * "getftime({fname})" function
9684 */
9685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009687 typval_T *argvars;
9688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689{
9690 char_u *fname;
9691 struct stat st;
9692
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009693 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694
9695 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009698 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699}
9700
9701/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009702 * "getftype({fname})" function
9703 */
9704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009706 typval_T *argvars;
9707 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009708{
9709 char_u *fname;
9710 struct stat st;
9711 char_u *type = NULL;
9712 char *t;
9713
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009715
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009717 if (mch_lstat((char *)fname, &st) >= 0)
9718 {
9719#ifdef S_ISREG
9720 if (S_ISREG(st.st_mode))
9721 t = "file";
9722 else if (S_ISDIR(st.st_mode))
9723 t = "dir";
9724# ifdef S_ISLNK
9725 else if (S_ISLNK(st.st_mode))
9726 t = "link";
9727# endif
9728# ifdef S_ISBLK
9729 else if (S_ISBLK(st.st_mode))
9730 t = "bdev";
9731# endif
9732# ifdef S_ISCHR
9733 else if (S_ISCHR(st.st_mode))
9734 t = "cdev";
9735# endif
9736# ifdef S_ISFIFO
9737 else if (S_ISFIFO(st.st_mode))
9738 t = "fifo";
9739# endif
9740# ifdef S_ISSOCK
9741 else if (S_ISSOCK(st.st_mode))
9742 t = "fifo";
9743# endif
9744 else
9745 t = "other";
9746#else
9747# ifdef S_IFMT
9748 switch (st.st_mode & S_IFMT)
9749 {
9750 case S_IFREG: t = "file"; break;
9751 case S_IFDIR: t = "dir"; break;
9752# ifdef S_IFLNK
9753 case S_IFLNK: t = "link"; break;
9754# endif
9755# ifdef S_IFBLK
9756 case S_IFBLK: t = "bdev"; break;
9757# endif
9758# ifdef S_IFCHR
9759 case S_IFCHR: t = "cdev"; break;
9760# endif
9761# ifdef S_IFIFO
9762 case S_IFIFO: t = "fifo"; break;
9763# endif
9764# ifdef S_IFSOCK
9765 case S_IFSOCK: t = "socket"; break;
9766# endif
9767 default: t = "other";
9768 }
9769# else
9770 if (mch_isdir(fname))
9771 t = "dir";
9772 else
9773 t = "file";
9774# endif
9775#endif
9776 type = vim_strsave((char_u *)t);
9777 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009778 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009779}
9780
9781/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009782 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009783 */
9784 static void
9785f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009786 typval_T *argvars;
9787 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009788{
9789 linenr_T lnum;
9790 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009791 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009792
9793 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009794 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009795 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009796 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009797 retlist = FALSE;
9798 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009799 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009800 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009801 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009802 retlist = TRUE;
9803 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009804
Bram Moolenaar342337a2005-07-21 21:11:17 +00009805 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009806}
9807
9808/*
Bram Moolenaar280f1262006-01-30 00:14:18 +00009809 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +00009810 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00009811/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +00009812 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +00009813f_getqflist(argvars, rettv)
9814 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009815 typval_T *rettv;
9816{
9817#ifdef FEAT_QUICKFIX
9818 list_T *l;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009819 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009820#endif
9821
9822 rettv->vval.v_number = FALSE;
9823#ifdef FEAT_QUICKFIX
9824 l = list_alloc();
9825 if (l != NULL)
9826 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009827 rettv->vval.v_list = l;
9828 rettv->v_type = VAR_LIST;
9829 ++l->lv_refcount;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009830 wp = NULL;
9831 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
9832 {
9833 wp = find_win_by_nr(&argvars[0]);
9834 if (wp == NULL)
9835 return;
9836 }
9837
Bram Moolenaar17c7c012006-01-26 22:25:15 +00009838 (void)get_errorlist(wp, l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009839 }
9840#endif
9841}
9842
9843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 * "getreg()" function
9845 */
9846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009848 typval_T *argvars;
9849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850{
9851 char_u *strregname;
9852 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009853 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009854 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009856 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009858 strregname = get_tv_string_chk(&argvars[0]);
9859 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009860 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009861 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009864 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 regname = (strregname == NULL ? '"' : *strregname);
9866 if (regname == 0)
9867 regname = '"';
9868
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009869 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009870 rettv->vval.v_string = error ? NULL :
9871 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872}
9873
9874/*
9875 * "getregtype()" function
9876 */
9877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009878f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009879 typval_T *argvars;
9880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881{
9882 char_u *strregname;
9883 int regname;
9884 char_u buf[NUMBUFLEN + 2];
9885 long reglen = 0;
9886
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009887 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009888 {
9889 strregname = get_tv_string_chk(&argvars[0]);
9890 if (strregname == NULL) /* type error; errmsg already given */
9891 {
9892 rettv->v_type = VAR_STRING;
9893 rettv->vval.v_string = NULL;
9894 return;
9895 }
9896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 else
9898 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009899 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900
9901 regname = (strregname == NULL ? '"' : *strregname);
9902 if (regname == 0)
9903 regname = '"';
9904
9905 buf[0] = NUL;
9906 buf[1] = NUL;
9907 switch (get_reg_type(regname, &reglen))
9908 {
9909 case MLINE: buf[0] = 'V'; break;
9910 case MCHAR: buf[0] = 'v'; break;
9911#ifdef FEAT_VISUAL
9912 case MBLOCK:
9913 buf[0] = Ctrl_V;
9914 sprintf((char *)buf + 1, "%ld", reglen + 1);
9915 break;
9916#endif
9917 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918 rettv->v_type = VAR_STRING;
9919 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920}
9921
9922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 * "getwinposx()" function
9924 */
9925/*ARGSUSED*/
9926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009927f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009928 typval_T *argvars;
9929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009931 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932#ifdef FEAT_GUI
9933 if (gui.in_use)
9934 {
9935 int x, y;
9936
9937 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009938 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 }
9940#endif
9941}
9942
9943/*
9944 * "getwinposy()" function
9945 */
9946/*ARGSUSED*/
9947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009948f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009949 typval_T *argvars;
9950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009952 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953#ifdef FEAT_GUI
9954 if (gui.in_use)
9955 {
9956 int x, y;
9957
9958 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009959 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 }
9961#endif
9962}
9963
Bram Moolenaara40058a2005-07-11 22:42:07 +00009964 static win_T *
9965find_win_by_nr(vp)
9966 typval_T *vp;
9967{
9968#ifdef FEAT_WINDOWS
9969 win_T *wp;
9970#endif
9971 int nr;
9972
9973 nr = get_tv_number_chk(vp, NULL);
9974
9975#ifdef FEAT_WINDOWS
9976 if (nr < 0)
9977 return NULL;
9978 if (nr == 0)
9979 return curwin;
9980
9981 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9982 if (--nr <= 0)
9983 break;
9984 return wp;
9985#else
9986 if (nr == 0 || nr == 1)
9987 return curwin;
9988 return NULL;
9989#endif
9990}
9991
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992/*
9993 * "getwinvar()" function
9994 */
9995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009997 typval_T *argvars;
9998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999{
10000 win_T *win, *oldcurwin;
10001 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010002 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010005 varname = get_tv_string_chk(&argvars[1]);
10006 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010008 rettv->v_type = VAR_STRING;
10009 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010
10011 if (win != NULL && varname != NULL)
10012 {
10013 if (*varname == '&') /* window-local-option */
10014 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010015 /* Set curwin to be our win, temporarily. Also set curbuf, so
10016 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 oldcurwin = curwin;
10018 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010019 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010021 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022
10023 /* restore previous notion of curwin */
10024 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010025 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 }
10027 else
10028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010029 if (*varname == NUL)
10030 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10031 * scope prefix before the NUL byte is required by
10032 * find_var_in_ht(). */
10033 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010035 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010037 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 }
10039 }
10040
10041 --emsg_off;
10042}
10043
10044/*
10045 * "glob()" function
10046 */
10047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010049 typval_T *argvars;
10050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051{
10052 expand_T xpc;
10053
10054 ExpandInit(&xpc);
10055 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056 rettv->v_type = VAR_STRING;
10057 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10059 ExpandCleanup(&xpc);
10060}
10061
10062/*
10063 * "globpath()" function
10064 */
10065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010067 typval_T *argvars;
10068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069{
10070 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010071 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010074 if (file == NULL)
10075 rettv->vval.v_string = NULL;
10076 else
10077 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078}
10079
10080/*
10081 * "has()" function
10082 */
10083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010085 typval_T *argvars;
10086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087{
10088 int i;
10089 char_u *name;
10090 int n = FALSE;
10091 static char *(has_list[]) =
10092 {
10093#ifdef AMIGA
10094 "amiga",
10095# ifdef FEAT_ARP
10096 "arp",
10097# endif
10098#endif
10099#ifdef __BEOS__
10100 "beos",
10101#endif
10102#ifdef MSDOS
10103# ifdef DJGPP
10104 "dos32",
10105# else
10106 "dos16",
10107# endif
10108#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010109#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 "mac",
10111#endif
10112#if defined(MACOS_X_UNIX)
10113 "macunix",
10114#endif
10115#ifdef OS2
10116 "os2",
10117#endif
10118#ifdef __QNX__
10119 "qnx",
10120#endif
10121#ifdef RISCOS
10122 "riscos",
10123#endif
10124#ifdef UNIX
10125 "unix",
10126#endif
10127#ifdef VMS
10128 "vms",
10129#endif
10130#ifdef WIN16
10131 "win16",
10132#endif
10133#ifdef WIN32
10134 "win32",
10135#endif
10136#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10137 "win32unix",
10138#endif
10139#ifdef WIN64
10140 "win64",
10141#endif
10142#ifdef EBCDIC
10143 "ebcdic",
10144#endif
10145#ifndef CASE_INSENSITIVE_FILENAME
10146 "fname_case",
10147#endif
10148#ifdef FEAT_ARABIC
10149 "arabic",
10150#endif
10151#ifdef FEAT_AUTOCMD
10152 "autocmd",
10153#endif
10154#ifdef FEAT_BEVAL
10155 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010156# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10157 "balloon_multiline",
10158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159#endif
10160#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10161 "builtin_terms",
10162# ifdef ALL_BUILTIN_TCAPS
10163 "all_builtin_terms",
10164# endif
10165#endif
10166#ifdef FEAT_BYTEOFF
10167 "byte_offset",
10168#endif
10169#ifdef FEAT_CINDENT
10170 "cindent",
10171#endif
10172#ifdef FEAT_CLIENTSERVER
10173 "clientserver",
10174#endif
10175#ifdef FEAT_CLIPBOARD
10176 "clipboard",
10177#endif
10178#ifdef FEAT_CMDL_COMPL
10179 "cmdline_compl",
10180#endif
10181#ifdef FEAT_CMDHIST
10182 "cmdline_hist",
10183#endif
10184#ifdef FEAT_COMMENTS
10185 "comments",
10186#endif
10187#ifdef FEAT_CRYPT
10188 "cryptv",
10189#endif
10190#ifdef FEAT_CSCOPE
10191 "cscope",
10192#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010193#ifdef CURSOR_SHAPE
10194 "cursorshape",
10195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196#ifdef DEBUG
10197 "debug",
10198#endif
10199#ifdef FEAT_CON_DIALOG
10200 "dialog_con",
10201#endif
10202#ifdef FEAT_GUI_DIALOG
10203 "dialog_gui",
10204#endif
10205#ifdef FEAT_DIFF
10206 "diff",
10207#endif
10208#ifdef FEAT_DIGRAPHS
10209 "digraphs",
10210#endif
10211#ifdef FEAT_DND
10212 "dnd",
10213#endif
10214#ifdef FEAT_EMACS_TAGS
10215 "emacs_tags",
10216#endif
10217 "eval", /* always present, of course! */
10218#ifdef FEAT_EX_EXTRA
10219 "ex_extra",
10220#endif
10221#ifdef FEAT_SEARCH_EXTRA
10222 "extra_search",
10223#endif
10224#ifdef FEAT_FKMAP
10225 "farsi",
10226#endif
10227#ifdef FEAT_SEARCHPATH
10228 "file_in_path",
10229#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010230#if defined(UNIX) && !defined(USE_SYSTEM)
10231 "filterpipe",
10232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233#ifdef FEAT_FIND_ID
10234 "find_in_path",
10235#endif
10236#ifdef FEAT_FOLDING
10237 "folding",
10238#endif
10239#ifdef FEAT_FOOTER
10240 "footer",
10241#endif
10242#if !defined(USE_SYSTEM) && defined(UNIX)
10243 "fork",
10244#endif
10245#ifdef FEAT_GETTEXT
10246 "gettext",
10247#endif
10248#ifdef FEAT_GUI
10249 "gui",
10250#endif
10251#ifdef FEAT_GUI_ATHENA
10252# ifdef FEAT_GUI_NEXTAW
10253 "gui_neXtaw",
10254# else
10255 "gui_athena",
10256# endif
10257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258#ifdef FEAT_GUI_GTK
10259 "gui_gtk",
10260# ifdef HAVE_GTK2
10261 "gui_gtk2",
10262# endif
10263#endif
10264#ifdef FEAT_GUI_MAC
10265 "gui_mac",
10266#endif
10267#ifdef FEAT_GUI_MOTIF
10268 "gui_motif",
10269#endif
10270#ifdef FEAT_GUI_PHOTON
10271 "gui_photon",
10272#endif
10273#ifdef FEAT_GUI_W16
10274 "gui_win16",
10275#endif
10276#ifdef FEAT_GUI_W32
10277 "gui_win32",
10278#endif
10279#ifdef FEAT_HANGULIN
10280 "hangul_input",
10281#endif
10282#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10283 "iconv",
10284#endif
10285#ifdef FEAT_INS_EXPAND
10286 "insert_expand",
10287#endif
10288#ifdef FEAT_JUMPLIST
10289 "jumplist",
10290#endif
10291#ifdef FEAT_KEYMAP
10292 "keymap",
10293#endif
10294#ifdef FEAT_LANGMAP
10295 "langmap",
10296#endif
10297#ifdef FEAT_LIBCALL
10298 "libcall",
10299#endif
10300#ifdef FEAT_LINEBREAK
10301 "linebreak",
10302#endif
10303#ifdef FEAT_LISP
10304 "lispindent",
10305#endif
10306#ifdef FEAT_LISTCMDS
10307 "listcmds",
10308#endif
10309#ifdef FEAT_LOCALMAP
10310 "localmap",
10311#endif
10312#ifdef FEAT_MENU
10313 "menu",
10314#endif
10315#ifdef FEAT_SESSION
10316 "mksession",
10317#endif
10318#ifdef FEAT_MODIFY_FNAME
10319 "modify_fname",
10320#endif
10321#ifdef FEAT_MOUSE
10322 "mouse",
10323#endif
10324#ifdef FEAT_MOUSESHAPE
10325 "mouseshape",
10326#endif
10327#if defined(UNIX) || defined(VMS)
10328# ifdef FEAT_MOUSE_DEC
10329 "mouse_dec",
10330# endif
10331# ifdef FEAT_MOUSE_GPM
10332 "mouse_gpm",
10333# endif
10334# ifdef FEAT_MOUSE_JSB
10335 "mouse_jsbterm",
10336# endif
10337# ifdef FEAT_MOUSE_NET
10338 "mouse_netterm",
10339# endif
10340# ifdef FEAT_MOUSE_PTERM
10341 "mouse_pterm",
10342# endif
10343# ifdef FEAT_MOUSE_XTERM
10344 "mouse_xterm",
10345# endif
10346#endif
10347#ifdef FEAT_MBYTE
10348 "multi_byte",
10349#endif
10350#ifdef FEAT_MBYTE_IME
10351 "multi_byte_ime",
10352#endif
10353#ifdef FEAT_MULTI_LANG
10354 "multi_lang",
10355#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010356#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010357#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010358 "mzscheme",
10359#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361#ifdef FEAT_OLE
10362 "ole",
10363#endif
10364#ifdef FEAT_OSFILETYPE
10365 "osfiletype",
10366#endif
10367#ifdef FEAT_PATH_EXTRA
10368 "path_extra",
10369#endif
10370#ifdef FEAT_PERL
10371#ifndef DYNAMIC_PERL
10372 "perl",
10373#endif
10374#endif
10375#ifdef FEAT_PYTHON
10376#ifndef DYNAMIC_PYTHON
10377 "python",
10378#endif
10379#endif
10380#ifdef FEAT_POSTSCRIPT
10381 "postscript",
10382#endif
10383#ifdef FEAT_PRINTER
10384 "printer",
10385#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010386#ifdef FEAT_PROFILE
10387 "profile",
10388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389#ifdef FEAT_QUICKFIX
10390 "quickfix",
10391#endif
10392#ifdef FEAT_RIGHTLEFT
10393 "rightleft",
10394#endif
10395#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10396 "ruby",
10397#endif
10398#ifdef FEAT_SCROLLBIND
10399 "scrollbind",
10400#endif
10401#ifdef FEAT_CMDL_INFO
10402 "showcmd",
10403 "cmdline_info",
10404#endif
10405#ifdef FEAT_SIGNS
10406 "signs",
10407#endif
10408#ifdef FEAT_SMARTINDENT
10409 "smartindent",
10410#endif
10411#ifdef FEAT_SNIFF
10412 "sniff",
10413#endif
10414#ifdef FEAT_STL_OPT
10415 "statusline",
10416#endif
10417#ifdef FEAT_SUN_WORKSHOP
10418 "sun_workshop",
10419#endif
10420#ifdef FEAT_NETBEANS_INTG
10421 "netbeans_intg",
10422#endif
10423#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010424 "spell",
10425#endif
10426#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 "syntax",
10428#endif
10429#if defined(USE_SYSTEM) || !defined(UNIX)
10430 "system",
10431#endif
10432#ifdef FEAT_TAG_BINS
10433 "tag_binary",
10434#endif
10435#ifdef FEAT_TAG_OLDSTATIC
10436 "tag_old_static",
10437#endif
10438#ifdef FEAT_TAG_ANYWHITE
10439 "tag_any_white",
10440#endif
10441#ifdef FEAT_TCL
10442# ifndef DYNAMIC_TCL
10443 "tcl",
10444# endif
10445#endif
10446#ifdef TERMINFO
10447 "terminfo",
10448#endif
10449#ifdef FEAT_TERMRESPONSE
10450 "termresponse",
10451#endif
10452#ifdef FEAT_TEXTOBJ
10453 "textobjects",
10454#endif
10455#ifdef HAVE_TGETENT
10456 "tgetent",
10457#endif
10458#ifdef FEAT_TITLE
10459 "title",
10460#endif
10461#ifdef FEAT_TOOLBAR
10462 "toolbar",
10463#endif
10464#ifdef FEAT_USR_CMDS
10465 "user-commands", /* was accidentally included in 5.4 */
10466 "user_commands",
10467#endif
10468#ifdef FEAT_VIMINFO
10469 "viminfo",
10470#endif
10471#ifdef FEAT_VERTSPLIT
10472 "vertsplit",
10473#endif
10474#ifdef FEAT_VIRTUALEDIT
10475 "virtualedit",
10476#endif
10477#ifdef FEAT_VISUAL
10478 "visual",
10479#endif
10480#ifdef FEAT_VISUALEXTRA
10481 "visualextra",
10482#endif
10483#ifdef FEAT_VREPLACE
10484 "vreplace",
10485#endif
10486#ifdef FEAT_WILDIGN
10487 "wildignore",
10488#endif
10489#ifdef FEAT_WILDMENU
10490 "wildmenu",
10491#endif
10492#ifdef FEAT_WINDOWS
10493 "windows",
10494#endif
10495#ifdef FEAT_WAK
10496 "winaltkeys",
10497#endif
10498#ifdef FEAT_WRITEBACKUP
10499 "writebackup",
10500#endif
10501#ifdef FEAT_XIM
10502 "xim",
10503#endif
10504#ifdef FEAT_XFONTSET
10505 "xfontset",
10506#endif
10507#ifdef USE_XSMP
10508 "xsmp",
10509#endif
10510#ifdef USE_XSMP_INTERACT
10511 "xsmp_interact",
10512#endif
10513#ifdef FEAT_XCLIPBOARD
10514 "xterm_clipboard",
10515#endif
10516#ifdef FEAT_XTERM_SAVE
10517 "xterm_save",
10518#endif
10519#if defined(UNIX) && defined(FEAT_X11)
10520 "X11",
10521#endif
10522 NULL
10523 };
10524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010525 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 for (i = 0; has_list[i] != NULL; ++i)
10527 if (STRICMP(name, has_list[i]) == 0)
10528 {
10529 n = TRUE;
10530 break;
10531 }
10532
10533 if (n == FALSE)
10534 {
10535 if (STRNICMP(name, "patch", 5) == 0)
10536 n = has_patch(atoi((char *)name + 5));
10537 else if (STRICMP(name, "vim_starting") == 0)
10538 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010539#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10540 else if (STRICMP(name, "balloon_multiline") == 0)
10541 n = multiline_balloon_available();
10542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543#ifdef DYNAMIC_TCL
10544 else if (STRICMP(name, "tcl") == 0)
10545 n = tcl_enabled(FALSE);
10546#endif
10547#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10548 else if (STRICMP(name, "iconv") == 0)
10549 n = iconv_enabled(FALSE);
10550#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010551#ifdef DYNAMIC_MZSCHEME
10552 else if (STRICMP(name, "mzscheme") == 0)
10553 n = mzscheme_enabled(FALSE);
10554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555#ifdef DYNAMIC_RUBY
10556 else if (STRICMP(name, "ruby") == 0)
10557 n = ruby_enabled(FALSE);
10558#endif
10559#ifdef DYNAMIC_PYTHON
10560 else if (STRICMP(name, "python") == 0)
10561 n = python_enabled(FALSE);
10562#endif
10563#ifdef DYNAMIC_PERL
10564 else if (STRICMP(name, "perl") == 0)
10565 n = perl_enabled(FALSE);
10566#endif
10567#ifdef FEAT_GUI
10568 else if (STRICMP(name, "gui_running") == 0)
10569 n = (gui.in_use || gui.starting);
10570# ifdef FEAT_GUI_W32
10571 else if (STRICMP(name, "gui_win32s") == 0)
10572 n = gui_is_win32s();
10573# endif
10574# ifdef FEAT_BROWSE
10575 else if (STRICMP(name, "browse") == 0)
10576 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10577# endif
10578#endif
10579#ifdef FEAT_SYN_HL
10580 else if (STRICMP(name, "syntax_items") == 0)
10581 n = syntax_present(curbuf);
10582#endif
10583#if defined(WIN3264)
10584 else if (STRICMP(name, "win95") == 0)
10585 n = mch_windows95();
10586#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010587#ifdef FEAT_NETBEANS_INTG
10588 else if (STRICMP(name, "netbeans_enabled") == 0)
10589 n = usingNetbeans;
10590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 }
10592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594}
10595
10596/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597 * "has_key()" function
10598 */
10599 static void
10600f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010601 typval_T *argvars;
10602 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603{
10604 rettv->vval.v_number = 0;
10605 if (argvars[0].v_type != VAR_DICT)
10606 {
10607 EMSG(_(e_dictreq));
10608 return;
10609 }
10610 if (argvars[0].vval.v_dict == NULL)
10611 return;
10612
10613 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010614 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010615}
10616
10617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 * "hasmapto()" function
10619 */
10620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010621f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010622 typval_T *argvars;
10623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624{
10625 char_u *name;
10626 char_u *mode;
10627 char_u buf[NUMBUFLEN];
10628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010630 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 mode = (char_u *)"nvo";
10632 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010633 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634
10635 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010636 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010638 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639}
10640
10641/*
10642 * "histadd()" function
10643 */
10644/*ARGSUSED*/
10645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010646f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010647 typval_T *argvars;
10648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649{
10650#ifdef FEAT_CMDHIST
10651 int histype;
10652 char_u *str;
10653 char_u buf[NUMBUFLEN];
10654#endif
10655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 if (check_restricted() || check_secure())
10658 return;
10659#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010660 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10661 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 if (histype >= 0)
10663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010664 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 if (*str != NUL)
10666 {
10667 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010668 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 return;
10670 }
10671 }
10672#endif
10673}
10674
10675/*
10676 * "histdel()" function
10677 */
10678/*ARGSUSED*/
10679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010680f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010681 typval_T *argvars;
10682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683{
10684#ifdef FEAT_CMDHIST
10685 int n;
10686 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010687 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10690 if (str == NULL)
10691 n = 0;
10692 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010694 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010695 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010697 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010698 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 else
10700 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010701 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010702 get_tv_string_buf(&argvars[1], buf));
10703 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010705 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706#endif
10707}
10708
10709/*
10710 * "histget()" function
10711 */
10712/*ARGSUSED*/
10713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010714f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010715 typval_T *argvars;
10716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717{
10718#ifdef FEAT_CMDHIST
10719 int type;
10720 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010721 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010723 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10724 if (str == NULL)
10725 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010727 {
10728 type = get_histtype(str);
10729 if (argvars[1].v_type == VAR_UNKNOWN)
10730 idx = get_history_idx(type);
10731 else
10732 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10733 /* -1 on type error */
10734 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010737 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010739 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740}
10741
10742/*
10743 * "histnr()" function
10744 */
10745/*ARGSUSED*/
10746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010747f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010748 typval_T *argvars;
10749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750{
10751 int i;
10752
10753#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010754 char_u *history = get_tv_string_chk(&argvars[0]);
10755
10756 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 if (i >= HIST_CMD && i < HIST_COUNT)
10758 i = get_history_idx(i);
10759 else
10760#endif
10761 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010762 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763}
10764
10765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 * "highlightID(name)" function
10767 */
10768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010770 typval_T *argvars;
10771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010773 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774}
10775
10776/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010777 * "highlight_exists()" function
10778 */
10779 static void
10780f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010781 typval_T *argvars;
10782 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010783{
10784 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10785}
10786
10787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 * "hostname()" function
10789 */
10790/*ARGSUSED*/
10791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010792f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010793 typval_T *argvars;
10794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795{
10796 char_u hostname[256];
10797
10798 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010799 rettv->v_type = VAR_STRING;
10800 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801}
10802
10803/*
10804 * iconv() function
10805 */
10806/*ARGSUSED*/
10807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010808f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010809 typval_T *argvars;
10810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811{
10812#ifdef FEAT_MBYTE
10813 char_u buf1[NUMBUFLEN];
10814 char_u buf2[NUMBUFLEN];
10815 char_u *from, *to, *str;
10816 vimconv_T vimconv;
10817#endif
10818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819 rettv->v_type = VAR_STRING;
10820 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821
10822#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010823 str = get_tv_string(&argvars[0]);
10824 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10825 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 vimconv.vc_type = CONV_NONE;
10827 convert_setup(&vimconv, from, to);
10828
10829 /* If the encodings are equal, no conversion needed. */
10830 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010833 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834
10835 convert_setup(&vimconv, NULL, NULL);
10836 vim_free(from);
10837 vim_free(to);
10838#endif
10839}
10840
10841/*
10842 * "indent()" function
10843 */
10844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010846 typval_T *argvars;
10847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848{
10849 linenr_T lnum;
10850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010851 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010853 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010855 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856}
10857
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010858/*
10859 * "index()" function
10860 */
10861 static void
10862f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010863 typval_T *argvars;
10864 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010865{
Bram Moolenaar33570922005-01-25 22:26:29 +000010866 list_T *l;
10867 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010868 long idx = 0;
10869 int ic = FALSE;
10870
10871 rettv->vval.v_number = -1;
10872 if (argvars[0].v_type != VAR_LIST)
10873 {
10874 EMSG(_(e_listreq));
10875 return;
10876 }
10877 l = argvars[0].vval.v_list;
10878 if (l != NULL)
10879 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010880 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010881 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010883 int error = FALSE;
10884
Bram Moolenaar758711c2005-02-02 23:11:38 +000010885 /* Start at specified item. Use the cached index that list_find()
10886 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010887 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010888 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010889 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010890 ic = get_tv_number_chk(&argvars[3], &error);
10891 if (error)
10892 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010893 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010894
Bram Moolenaar758711c2005-02-02 23:11:38 +000010895 for ( ; item != NULL; item = item->li_next, ++idx)
10896 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010897 {
10898 rettv->vval.v_number = idx;
10899 break;
10900 }
10901 }
10902}
10903
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904static int inputsecret_flag = 0;
10905
10906/*
10907 * "input()" function
10908 * Also handles inputsecret() when inputsecret is set.
10909 */
10910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010911f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010912 typval_T *argvars;
10913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 char_u *p = NULL;
10917 int c;
10918 char_u buf[NUMBUFLEN];
10919 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010920 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010921 int xp_type = EXPAND_NOTHING;
10922 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010924 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925
10926#ifdef NO_CONSOLE_INPUT
10927 /* While starting up, there is no place to enter text. */
10928 if (no_console_input())
10929 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010930 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 return;
10932 }
10933#endif
10934
10935 cmd_silent = FALSE; /* Want to see the prompt. */
10936 if (prompt != NULL)
10937 {
10938 /* Only the part of the message after the last NL is considered as
10939 * prompt for the command line */
10940 p = vim_strrchr(prompt, '\n');
10941 if (p == NULL)
10942 p = prompt;
10943 else
10944 {
10945 ++p;
10946 c = *p;
10947 *p = NUL;
10948 msg_start();
10949 msg_clr_eos();
10950 msg_puts_attr(prompt, echo_attr);
10951 msg_didout = FALSE;
10952 msg_starthere();
10953 *p = c;
10954 }
10955 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010957 if (argvars[1].v_type != VAR_UNKNOWN)
10958 {
10959 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10960 if (defstr != NULL)
10961 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962
Bram Moolenaar4463f292005-09-25 22:20:24 +000010963 if (argvars[2].v_type != VAR_UNKNOWN)
10964 {
10965 char_u *xp_name;
10966 int xp_namelen;
10967 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010968
Bram Moolenaar4463f292005-09-25 22:20:24 +000010969 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010970
Bram Moolenaar4463f292005-09-25 22:20:24 +000010971 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
10972 if (xp_name == NULL)
10973 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010974
Bram Moolenaar4463f292005-09-25 22:20:24 +000010975 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010976
Bram Moolenaar4463f292005-09-25 22:20:24 +000010977 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
10978 &xp_arg) == FAIL)
10979 return;
10980 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010981 }
10982
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010983 if (defstr != NULL)
10984 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010985 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
10986 xp_type, xp_arg);
10987
10988 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010990 /* since the user typed this, no need to wait for return */
10991 need_wait_return = FALSE;
10992 msg_didout = FALSE;
10993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 cmd_silent = cmd_silent_save;
10995}
10996
10997/*
10998 * "inputdialog()" function
10999 */
11000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011001f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011002 typval_T *argvars;
11003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004{
11005#if defined(FEAT_GUI_TEXTDIALOG)
11006 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11007 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11008 {
11009 char_u *message;
11010 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011011 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011013 message = get_tv_string_chk(&argvars[0]);
11014 if (argvars[1].v_type != VAR_UNKNOWN
11015 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011016 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 else
11018 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011019 if (message != NULL && defstr != NULL
11020 && do_dialog(VIM_QUESTION, NULL, message,
11021 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 else
11024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011025 if (message != NULL && defstr != NULL
11026 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011027 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011028 rettv->vval.v_string = vim_strsave(
11029 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011031 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 }
11035 else
11036#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011037 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038}
11039
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011040/*
11041 * "inputlist()" function
11042 */
11043 static void
11044f_inputlist(argvars, rettv)
11045 typval_T *argvars;
11046 typval_T *rettv;
11047{
11048 listitem_T *li;
11049 int selected;
11050 int mouse_used;
11051
11052 rettv->vval.v_number = 0;
11053#ifdef NO_CONSOLE_INPUT
11054 /* While starting up, there is no place to enter text. */
11055 if (no_console_input())
11056 return;
11057#endif
11058 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11059 {
11060 EMSG2(_(e_listarg), "inputlist()");
11061 return;
11062 }
11063
11064 msg_start();
11065 lines_left = Rows; /* avoid more prompt */
11066 msg_scroll = TRUE;
11067 msg_clr_eos();
11068
11069 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11070 {
11071 msg_puts(get_tv_string(&li->li_tv));
11072 msg_putchar('\n');
11073 }
11074
11075 /* Ask for choice. */
11076 selected = prompt_for_number(&mouse_used);
11077 if (mouse_used)
11078 selected -= lines_left;
11079
11080 rettv->vval.v_number = selected;
11081}
11082
11083
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11085
11086/*
11087 * "inputrestore()" function
11088 */
11089/*ARGSUSED*/
11090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011091f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011092 typval_T *argvars;
11093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094{
11095 if (ga_userinput.ga_len > 0)
11096 {
11097 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11099 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011100 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 }
11102 else if (p_verbose > 1)
11103 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011104 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011105 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 }
11107}
11108
11109/*
11110 * "inputsave()" function
11111 */
11112/*ARGSUSED*/
11113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011114f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011115 typval_T *argvars;
11116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117{
11118 /* Add an entry to the stack of typehead storage. */
11119 if (ga_grow(&ga_userinput, 1) == OK)
11120 {
11121 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11122 + ga_userinput.ga_len);
11123 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011124 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 }
11126 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128}
11129
11130/*
11131 * "inputsecret()" function
11132 */
11133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011134f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011135 typval_T *argvars;
11136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137{
11138 ++cmdline_star;
11139 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 --cmdline_star;
11142 --inputsecret_flag;
11143}
11144
11145/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011146 * "insert()" function
11147 */
11148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011149f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011150 typval_T *argvars;
11151 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011152{
11153 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011154 listitem_T *item;
11155 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011156 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011157
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011158 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011159 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011160 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011161 else if ((l = argvars[0].vval.v_list) != NULL
11162 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011163 {
11164 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011165 before = get_tv_number_chk(&argvars[2], &error);
11166 if (error)
11167 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011168
Bram Moolenaar758711c2005-02-02 23:11:38 +000011169 if (before == l->lv_len)
11170 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011171 else
11172 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011173 item = list_find(l, before);
11174 if (item == NULL)
11175 {
11176 EMSGN(_(e_listidx), before);
11177 l = NULL;
11178 }
11179 }
11180 if (l != NULL)
11181 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011182 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011183 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011184 }
11185 }
11186}
11187
11188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 * "isdirectory()" function
11190 */
11191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011192f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011193 typval_T *argvars;
11194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011196 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197}
11198
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011199/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011200 * "islocked()" function
11201 */
11202 static void
11203f_islocked(argvars, rettv)
11204 typval_T *argvars;
11205 typval_T *rettv;
11206{
11207 lval_T lv;
11208 char_u *end;
11209 dictitem_T *di;
11210
11211 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011212 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11213 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011214 if (end != NULL && lv.ll_name != NULL)
11215 {
11216 if (*end != NUL)
11217 EMSG(_(e_trailing));
11218 else
11219 {
11220 if (lv.ll_tv == NULL)
11221 {
11222 if (check_changedtick(lv.ll_name))
11223 rettv->vval.v_number = 1; /* always locked */
11224 else
11225 {
11226 di = find_var(lv.ll_name, NULL);
11227 if (di != NULL)
11228 {
11229 /* Consider a variable locked when:
11230 * 1. the variable itself is locked
11231 * 2. the value of the variable is locked.
11232 * 3. the List or Dict value is locked.
11233 */
11234 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11235 || tv_islocked(&di->di_tv));
11236 }
11237 }
11238 }
11239 else if (lv.ll_range)
11240 EMSG(_("E745: Range not allowed"));
11241 else if (lv.ll_newkey != NULL)
11242 EMSG2(_(e_dictkey), lv.ll_newkey);
11243 else if (lv.ll_list != NULL)
11244 /* List item. */
11245 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11246 else
11247 /* Dictionary item. */
11248 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11249 }
11250 }
11251
11252 clear_lval(&lv);
11253}
11254
Bram Moolenaar33570922005-01-25 22:26:29 +000011255static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011256
11257/*
11258 * Turn a dict into a list:
11259 * "what" == 0: list of keys
11260 * "what" == 1: list of values
11261 * "what" == 2: list of items
11262 */
11263 static void
11264dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011265 typval_T *argvars;
11266 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011267 int what;
11268{
Bram Moolenaar33570922005-01-25 22:26:29 +000011269 list_T *l;
11270 list_T *l2;
11271 dictitem_T *di;
11272 hashitem_T *hi;
11273 listitem_T *li;
11274 listitem_T *li2;
11275 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011276 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011277
11278 rettv->vval.v_number = 0;
11279 if (argvars[0].v_type != VAR_DICT)
11280 {
11281 EMSG(_(e_dictreq));
11282 return;
11283 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011284 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011285 return;
11286
11287 l = list_alloc();
11288 if (l == NULL)
11289 return;
11290 rettv->v_type = VAR_LIST;
11291 rettv->vval.v_list = l;
11292 ++l->lv_refcount;
11293
Bram Moolenaar33570922005-01-25 22:26:29 +000011294 todo = d->dv_hashtab.ht_used;
11295 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011296 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011297 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011298 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011299 --todo;
11300 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011301
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011302 li = listitem_alloc();
11303 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011304 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011305 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011306
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011307 if (what == 0)
11308 {
11309 /* keys() */
11310 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011311 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011312 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11313 }
11314 else if (what == 1)
11315 {
11316 /* values() */
11317 copy_tv(&di->di_tv, &li->li_tv);
11318 }
11319 else
11320 {
11321 /* items() */
11322 l2 = list_alloc();
11323 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011324 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011325 li->li_tv.vval.v_list = l2;
11326 if (l2 == NULL)
11327 break;
11328 ++l2->lv_refcount;
11329
11330 li2 = listitem_alloc();
11331 if (li2 == NULL)
11332 break;
11333 list_append(l2, li2);
11334 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011335 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011336 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11337
11338 li2 = listitem_alloc();
11339 if (li2 == NULL)
11340 break;
11341 list_append(l2, li2);
11342 copy_tv(&di->di_tv, &li2->li_tv);
11343 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011344 }
11345 }
11346}
11347
11348/*
11349 * "items(dict)" function
11350 */
11351 static void
11352f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011353 typval_T *argvars;
11354 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011355{
11356 dict_list(argvars, rettv, 2);
11357}
11358
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011360 * "join()" function
11361 */
11362 static void
11363f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011364 typval_T *argvars;
11365 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011366{
11367 garray_T ga;
11368 char_u *sep;
11369
11370 rettv->vval.v_number = 0;
11371 if (argvars[0].v_type != VAR_LIST)
11372 {
11373 EMSG(_(e_listreq));
11374 return;
11375 }
11376 if (argvars[0].vval.v_list == NULL)
11377 return;
11378 if (argvars[1].v_type == VAR_UNKNOWN)
11379 sep = (char_u *)" ";
11380 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011381 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011382
11383 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011384
11385 if (sep != NULL)
11386 {
11387 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011388 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011389 ga_append(&ga, NUL);
11390 rettv->vval.v_string = (char_u *)ga.ga_data;
11391 }
11392 else
11393 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011394}
11395
11396/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011397 * "keys()" function
11398 */
11399 static void
11400f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011401 typval_T *argvars;
11402 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011403{
11404 dict_list(argvars, rettv, 0);
11405}
11406
11407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 * "last_buffer_nr()" function.
11409 */
11410/*ARGSUSED*/
11411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011413 typval_T *argvars;
11414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415{
11416 int n = 0;
11417 buf_T *buf;
11418
11419 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11420 if (n < buf->b_fnum)
11421 n = buf->b_fnum;
11422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011423 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011424}
11425
11426/*
11427 * "len()" function
11428 */
11429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011430f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011431 typval_T *argvars;
11432 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011433{
11434 switch (argvars[0].v_type)
11435 {
11436 case VAR_STRING:
11437 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011438 rettv->vval.v_number = (varnumber_T)STRLEN(
11439 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011440 break;
11441 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011443 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011444 case VAR_DICT:
11445 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11446 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011447 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011448 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011449 break;
11450 }
11451}
11452
Bram Moolenaar33570922005-01-25 22:26:29 +000011453static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011454
11455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011456libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011457 typval_T *argvars;
11458 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011459 int type;
11460{
11461#ifdef FEAT_LIBCALL
11462 char_u *string_in;
11463 char_u **string_result;
11464 int nr_result;
11465#endif
11466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011468 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011470 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011472
11473 if (check_restricted() || check_secure())
11474 return;
11475
11476#ifdef FEAT_LIBCALL
11477 /* The first two args must be strings, otherwise its meaningless */
11478 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11479 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011480 string_in = NULL;
11481 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011482 string_in = argvars[2].vval.v_string;
11483 if (type == VAR_NUMBER)
11484 string_result = NULL;
11485 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011487 if (mch_libcall(argvars[0].vval.v_string,
11488 argvars[1].vval.v_string,
11489 string_in,
11490 argvars[2].vval.v_number,
11491 string_result,
11492 &nr_result) == OK
11493 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011495 }
11496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497}
11498
11499/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011500 * "libcall()" function
11501 */
11502 static void
11503f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011504 typval_T *argvars;
11505 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011506{
11507 libcall_common(argvars, rettv, VAR_STRING);
11508}
11509
11510/*
11511 * "libcallnr()" function
11512 */
11513 static void
11514f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011515 typval_T *argvars;
11516 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011517{
11518 libcall_common(argvars, rettv, VAR_NUMBER);
11519}
11520
11521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 * "line(string)" function
11523 */
11524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011525f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011526 typval_T *argvars;
11527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528{
11529 linenr_T lnum = 0;
11530 pos_T *fp;
11531
11532 fp = var2fpos(&argvars[0], TRUE);
11533 if (fp != NULL)
11534 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011535 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536}
11537
11538/*
11539 * "line2byte(lnum)" function
11540 */
11541/*ARGSUSED*/
11542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011543f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011544 typval_T *argvars;
11545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546{
11547#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549#else
11550 linenr_T lnum;
11551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011556 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11557 if (rettv->vval.v_number >= 0)
11558 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559#endif
11560}
11561
11562/*
11563 * "lispindent(lnum)" function
11564 */
11565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011567 typval_T *argvars;
11568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569{
11570#ifdef FEAT_LISP
11571 pos_T pos;
11572 linenr_T lnum;
11573
11574 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011575 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11577 {
11578 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 curwin->w_cursor = pos;
11581 }
11582 else
11583#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585}
11586
11587/*
11588 * "localtime()" function
11589 */
11590/*ARGSUSED*/
11591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011593 typval_T *argvars;
11594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597}
11598
Bram Moolenaar33570922005-01-25 22:26:29 +000011599static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600
11601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011603 typval_T *argvars;
11604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 int exact;
11606{
11607 char_u *keys;
11608 char_u *which;
11609 char_u buf[NUMBUFLEN];
11610 char_u *keys_buf = NULL;
11611 char_u *rhs;
11612 int mode;
11613 garray_T ga;
11614
11615 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011616 rettv->v_type = VAR_STRING;
11617 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620 if (*keys == NUL)
11621 return;
11622
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011623 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011624 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 else
11626 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011627 if (which == NULL)
11628 return;
11629
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 mode = get_map_mode(&which, 0);
11631
11632 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011633 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634 vim_free(keys_buf);
11635 if (rhs != NULL)
11636 {
11637 ga_init(&ga);
11638 ga.ga_itemsize = 1;
11639 ga.ga_growsize = 40;
11640
11641 while (*rhs != NUL)
11642 ga_concat(&ga, str2special(&rhs, FALSE));
11643
11644 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011645 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646 }
11647}
11648
11649/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011650 * "map()" function
11651 */
11652 static void
11653f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011654 typval_T *argvars;
11655 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011656{
11657 filter_map(argvars, rettv, TRUE);
11658}
11659
11660/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011661 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 */
11663 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011664f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011665 typval_T *argvars;
11666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011668 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669}
11670
11671/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011672 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673 */
11674 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011675f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011676 typval_T *argvars;
11677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011679 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680}
11681
Bram Moolenaar33570922005-01-25 22:26:29 +000011682static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683
11684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011685find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011686 typval_T *argvars;
11687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 int type;
11689{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011690 char_u *str = NULL;
11691 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692 char_u *pat;
11693 regmatch_T regmatch;
11694 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011695 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 char_u *save_cpo;
11697 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011698 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011699 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011700 list_T *l = NULL;
11701 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011702 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011703 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704
11705 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11706 save_cpo = p_cpo;
11707 p_cpo = (char_u *)"";
11708
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011709 rettv->vval.v_number = -1;
11710 if (type == 3)
11711 {
11712 /* return empty list when there are no matches */
11713 if ((rettv->vval.v_list = list_alloc()) == NULL)
11714 goto theend;
11715 rettv->v_type = VAR_LIST;
11716 ++rettv->vval.v_list->lv_refcount;
11717 }
11718 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011720 rettv->v_type = VAR_STRING;
11721 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011724 if (argvars[0].v_type == VAR_LIST)
11725 {
11726 if ((l = argvars[0].vval.v_list) == NULL)
11727 goto theend;
11728 li = l->lv_first;
11729 }
11730 else
11731 expr = str = get_tv_string(&argvars[0]);
11732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011733 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11734 if (pat == NULL)
11735 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011736
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011737 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011739 int error = FALSE;
11740
11741 start = get_tv_number_chk(&argvars[2], &error);
11742 if (error)
11743 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011744 if (l != NULL)
11745 {
11746 li = list_find(l, start);
11747 if (li == NULL)
11748 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011749 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011750 }
11751 else
11752 {
11753 if (start < 0)
11754 start = 0;
11755 if (start > (long)STRLEN(str))
11756 goto theend;
11757 str += start;
11758 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011759
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011760 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011761 nth = get_tv_number_chk(&argvars[3], &error);
11762 if (error)
11763 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 }
11765
11766 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11767 if (regmatch.regprog != NULL)
11768 {
11769 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011770
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011771 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011772 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011773 if (l != NULL)
11774 {
11775 if (li == NULL)
11776 {
11777 match = FALSE;
11778 break;
11779 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011780 vim_free(tofree);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011781 str = echo_string(&li->li_tv, &tofree, strbuf,0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011782 if (str == NULL)
11783 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011784 }
11785
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011786 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011787
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011788 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011789 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011790 if (l == NULL && !match)
11791 break;
11792
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011793 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011794 if (l != NULL)
11795 {
11796 li = li->li_next;
11797 ++idx;
11798 }
11799 else
11800 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011801#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011802 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011803#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011804 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011805#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011806 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011807 }
11808
11809 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011811 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011812 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011813 int i;
11814
11815 /* return list with matched string and submatches */
11816 for (i = 0; i < NSUBEXP; ++i)
11817 {
11818 if (regmatch.endp[i] == NULL)
11819 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011820 if (list_append_string(rettv->vval.v_list,
11821 regmatch.startp[i],
11822 (int)(regmatch.endp[i] - regmatch.startp[i]))
11823 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011824 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011825 }
11826 }
11827 else if (type == 2)
11828 {
11829 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011830 if (l != NULL)
11831 copy_tv(&li->li_tv, rettv);
11832 else
11833 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011835 }
11836 else if (l != NULL)
11837 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838 else
11839 {
11840 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011841 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 (varnumber_T)(regmatch.startp[0] - str);
11843 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011844 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 }
11848 }
11849 vim_free(regmatch.regprog);
11850 }
11851
11852theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011853 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 p_cpo = save_cpo;
11855}
11856
11857/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011858 * "match()" function
11859 */
11860 static void
11861f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011862 typval_T *argvars;
11863 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011864{
11865 find_some_match(argvars, rettv, 1);
11866}
11867
11868/*
11869 * "matchend()" function
11870 */
11871 static void
11872f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011873 typval_T *argvars;
11874 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011875{
11876 find_some_match(argvars, rettv, 0);
11877}
11878
11879/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011880 * "matchlist()" function
11881 */
11882 static void
11883f_matchlist(argvars, rettv)
11884 typval_T *argvars;
11885 typval_T *rettv;
11886{
11887 find_some_match(argvars, rettv, 3);
11888}
11889
11890/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011891 * "matchstr()" function
11892 */
11893 static void
11894f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011895 typval_T *argvars;
11896 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011897{
11898 find_some_match(argvars, rettv, 2);
11899}
11900
Bram Moolenaar33570922005-01-25 22:26:29 +000011901static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011902
11903 static void
11904max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011905 typval_T *argvars;
11906 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011907 int domax;
11908{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011909 long n = 0;
11910 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011911 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011912
11913 if (argvars[0].v_type == VAR_LIST)
11914 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011915 list_T *l;
11916 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011917
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011918 l = argvars[0].vval.v_list;
11919 if (l != NULL)
11920 {
11921 li = l->lv_first;
11922 if (li != NULL)
11923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011924 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011925 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011926 {
11927 li = li->li_next;
11928 if (li == NULL)
11929 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011930 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011931 if (domax ? i > n : i < n)
11932 n = i;
11933 }
11934 }
11935 }
11936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011937 else if (argvars[0].v_type == VAR_DICT)
11938 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011939 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011940 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011941 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011942 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011943
11944 d = argvars[0].vval.v_dict;
11945 if (d != NULL)
11946 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011947 todo = d->dv_hashtab.ht_used;
11948 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011949 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011950 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011951 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011952 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011953 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011954 if (first)
11955 {
11956 n = i;
11957 first = FALSE;
11958 }
11959 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011960 n = i;
11961 }
11962 }
11963 }
11964 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011965 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011966 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011967 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011968}
11969
11970/*
11971 * "max()" function
11972 */
11973 static void
11974f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011975 typval_T *argvars;
11976 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011977{
11978 max_min(argvars, rettv, TRUE);
11979}
11980
11981/*
11982 * "min()" function
11983 */
11984 static void
11985f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011986 typval_T *argvars;
11987 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011988{
11989 max_min(argvars, rettv, FALSE);
11990}
11991
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011992static int mkdir_recurse __ARGS((char_u *dir, int prot));
11993
11994/*
11995 * Create the directory in which "dir" is located, and higher levels when
11996 * needed.
11997 */
11998 static int
11999mkdir_recurse(dir, prot)
12000 char_u *dir;
12001 int prot;
12002{
12003 char_u *p;
12004 char_u *updir;
12005 int r = FAIL;
12006
12007 /* Get end of directory name in "dir".
12008 * We're done when it's "/" or "c:/". */
12009 p = gettail_sep(dir);
12010 if (p <= get_past_head(dir))
12011 return OK;
12012
12013 /* If the directory exists we're done. Otherwise: create it.*/
12014 updir = vim_strnsave(dir, (int)(p - dir));
12015 if (updir == NULL)
12016 return FAIL;
12017 if (mch_isdir(updir))
12018 r = OK;
12019 else if (mkdir_recurse(updir, prot) == OK)
12020 r = vim_mkdir_emsg(updir, prot);
12021 vim_free(updir);
12022 return r;
12023}
12024
12025#ifdef vim_mkdir
12026/*
12027 * "mkdir()" function
12028 */
12029 static void
12030f_mkdir(argvars, rettv)
12031 typval_T *argvars;
12032 typval_T *rettv;
12033{
12034 char_u *dir;
12035 char_u buf[NUMBUFLEN];
12036 int prot = 0755;
12037
12038 rettv->vval.v_number = FAIL;
12039 if (check_restricted() || check_secure())
12040 return;
12041
12042 dir = get_tv_string_buf(&argvars[0], buf);
12043 if (argvars[1].v_type != VAR_UNKNOWN)
12044 {
12045 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012046 prot = get_tv_number_chk(&argvars[2], NULL);
12047 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012048 mkdir_recurse(dir, prot);
12049 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012050 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012051}
12052#endif
12053
Bram Moolenaar0d660222005-01-07 21:51:51 +000012054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 * "mode()" function
12056 */
12057/*ARGSUSED*/
12058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012059f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012060 typval_T *argvars;
12061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062{
12063 char_u buf[2];
12064
12065#ifdef FEAT_VISUAL
12066 if (VIsual_active)
12067 {
12068 if (VIsual_select)
12069 buf[0] = VIsual_mode + 's' - 'v';
12070 else
12071 buf[0] = VIsual_mode;
12072 }
12073 else
12074#endif
12075 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12076 buf[0] = 'r';
12077 else if (State & INSERT)
12078 {
12079 if (State & REPLACE_FLAG)
12080 buf[0] = 'R';
12081 else
12082 buf[0] = 'i';
12083 }
12084 else if (State & CMDLINE)
12085 buf[0] = 'c';
12086 else
12087 buf[0] = 'n';
12088
12089 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 rettv->vval.v_string = vim_strsave(buf);
12091 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092}
12093
12094/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012095 * "nextnonblank()" function
12096 */
12097 static void
12098f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012099 typval_T *argvars;
12100 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012101{
12102 linenr_T lnum;
12103
12104 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012106 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012107 {
12108 lnum = 0;
12109 break;
12110 }
12111 if (*skipwhite(ml_get(lnum)) != NUL)
12112 break;
12113 }
12114 rettv->vval.v_number = lnum;
12115}
12116
12117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 * "nr2char()" function
12119 */
12120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012122 typval_T *argvars;
12123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124{
12125 char_u buf[NUMBUFLEN];
12126
12127#ifdef FEAT_MBYTE
12128 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012129 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130 else
12131#endif
12132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134 buf[1] = NUL;
12135 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012136 rettv->v_type = VAR_STRING;
12137 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012138}
12139
12140/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012141 * "prevnonblank()" function
12142 */
12143 static void
12144f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012145 typval_T *argvars;
12146 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012147{
12148 linenr_T lnum;
12149
12150 lnum = get_tv_lnum(argvars);
12151 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12152 lnum = 0;
12153 else
12154 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12155 --lnum;
12156 rettv->vval.v_number = lnum;
12157}
12158
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012159#ifdef HAVE_STDARG_H
12160/* This dummy va_list is here because:
12161 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12162 * - locally in the function results in a "used before set" warning
12163 * - using va_start() to initialize it gives "function with fixed args" error */
12164static va_list ap;
12165#endif
12166
Bram Moolenaar8c711452005-01-14 21:53:12 +000012167/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012168 * "printf()" function
12169 */
12170 static void
12171f_printf(argvars, rettv)
12172 typval_T *argvars;
12173 typval_T *rettv;
12174{
12175 rettv->v_type = VAR_STRING;
12176 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012177#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012178 {
12179 char_u buf[NUMBUFLEN];
12180 int len;
12181 char_u *s;
12182 int saved_did_emsg = did_emsg;
12183 char *fmt;
12184
12185 /* Get the required length, allocate the buffer and do it for real. */
12186 did_emsg = FALSE;
12187 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012188 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012189 if (!did_emsg)
12190 {
12191 s = alloc(len + 1);
12192 if (s != NULL)
12193 {
12194 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012195 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012196 }
12197 }
12198 did_emsg |= saved_did_emsg;
12199 }
12200#endif
12201}
12202
12203/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012204 * "range()" function
12205 */
12206 static void
12207f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012208 typval_T *argvars;
12209 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012210{
12211 long start;
12212 long end;
12213 long stride = 1;
12214 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012215 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012216 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012217
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012218 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012219 if (argvars[1].v_type == VAR_UNKNOWN)
12220 {
12221 end = start - 1;
12222 start = 0;
12223 }
12224 else
12225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012226 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012227 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012228 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012229 }
12230
12231 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012232 if (error)
12233 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012234 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012235 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012236 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012237 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012238 else
12239 {
12240 l = list_alloc();
12241 if (l != NULL)
12242 {
12243 rettv->v_type = VAR_LIST;
12244 rettv->vval.v_list = l;
12245 ++l->lv_refcount;
12246
12247 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012248 if (list_append_number(l, (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012249 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012250 }
12251 }
12252}
12253
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012254/*
12255 * "readfile()" function
12256 */
12257 static void
12258f_readfile(argvars, rettv)
12259 typval_T *argvars;
12260 typval_T *rettv;
12261{
12262 int binary = FALSE;
12263 char_u *fname;
12264 FILE *fd;
12265 list_T *l;
12266 listitem_T *li;
12267#define FREAD_SIZE 200 /* optimized for text lines */
12268 char_u buf[FREAD_SIZE];
12269 int readlen; /* size of last fread() */
12270 int buflen; /* nr of valid chars in buf[] */
12271 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12272 int tolist; /* first byte in buf[] still to be put in list */
12273 int chop; /* how many CR to chop off */
12274 char_u *prev = NULL; /* previously read bytes, if any */
12275 int prevlen = 0; /* length of "prev" if not NULL */
12276 char_u *s;
12277 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012278 long maxline = MAXLNUM;
12279 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012280
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012281 if (argvars[1].v_type != VAR_UNKNOWN)
12282 {
12283 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12284 binary = TRUE;
12285 if (argvars[2].v_type != VAR_UNKNOWN)
12286 maxline = get_tv_number(&argvars[2]);
12287 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012288
12289 l = list_alloc();
12290 if (l == NULL)
12291 return;
12292 rettv->v_type = VAR_LIST;
12293 rettv->vval.v_list = l;
12294 l->lv_refcount = 1;
12295
12296 /* Always open the file in binary mode, library functions have a mind of
12297 * their own about CR-LF conversion. */
12298 fname = get_tv_string(&argvars[0]);
12299 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12300 {
12301 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12302 return;
12303 }
12304
12305 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012306 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012307 {
12308 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12309 buflen = filtd + readlen;
12310 tolist = 0;
12311 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12312 {
12313 if (buf[filtd] == '\n' || readlen <= 0)
12314 {
12315 /* Only when in binary mode add an empty list item when the
12316 * last line ends in a '\n'. */
12317 if (!binary && readlen == 0 && filtd == 0)
12318 break;
12319
12320 /* Found end-of-line or end-of-file: add a text line to the
12321 * list. */
12322 chop = 0;
12323 if (!binary)
12324 while (filtd - chop - 1 >= tolist
12325 && buf[filtd - chop - 1] == '\r')
12326 ++chop;
12327 len = filtd - tolist - chop;
12328 if (prev == NULL)
12329 s = vim_strnsave(buf + tolist, len);
12330 else
12331 {
12332 s = alloc((unsigned)(prevlen + len + 1));
12333 if (s != NULL)
12334 {
12335 mch_memmove(s, prev, prevlen);
12336 vim_free(prev);
12337 prev = NULL;
12338 mch_memmove(s + prevlen, buf + tolist, len);
12339 s[prevlen + len] = NUL;
12340 }
12341 }
12342 tolist = filtd + 1;
12343
12344 li = listitem_alloc();
12345 if (li == NULL)
12346 {
12347 vim_free(s);
12348 break;
12349 }
12350 li->li_tv.v_type = VAR_STRING;
12351 li->li_tv.v_lock = 0;
12352 li->li_tv.vval.v_string = s;
12353 list_append(l, li);
12354
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012355 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012356 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012357 if (readlen <= 0)
12358 break;
12359 }
12360 else if (buf[filtd] == NUL)
12361 buf[filtd] = '\n';
12362 }
12363 if (readlen <= 0)
12364 break;
12365
12366 if (tolist == 0)
12367 {
12368 /* "buf" is full, need to move text to an allocated buffer */
12369 if (prev == NULL)
12370 {
12371 prev = vim_strnsave(buf, buflen);
12372 prevlen = buflen;
12373 }
12374 else
12375 {
12376 s = alloc((unsigned)(prevlen + buflen));
12377 if (s != NULL)
12378 {
12379 mch_memmove(s, prev, prevlen);
12380 mch_memmove(s + prevlen, buf, buflen);
12381 vim_free(prev);
12382 prev = s;
12383 prevlen += buflen;
12384 }
12385 }
12386 filtd = 0;
12387 }
12388 else
12389 {
12390 mch_memmove(buf, buf + tolist, buflen - tolist);
12391 filtd -= tolist;
12392 }
12393 }
12394
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012395 /*
12396 * For a negative line count use only the lines at the end of the file,
12397 * free the rest.
12398 */
12399 if (maxline < 0)
12400 while (cnt > -maxline)
12401 {
12402 listitem_remove(l, l->lv_first);
12403 --cnt;
12404 }
12405
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012406 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012407 fclose(fd);
12408}
12409
12410
Bram Moolenaar0d660222005-01-07 21:51:51 +000012411#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12412static void make_connection __ARGS((void));
12413static int check_connection __ARGS((void));
12414
12415 static void
12416make_connection()
12417{
12418 if (X_DISPLAY == NULL
12419# ifdef FEAT_GUI
12420 && !gui.in_use
12421# endif
12422 )
12423 {
12424 x_force_connect = TRUE;
12425 setup_term_clip();
12426 x_force_connect = FALSE;
12427 }
12428}
12429
12430 static int
12431check_connection()
12432{
12433 make_connection();
12434 if (X_DISPLAY == NULL)
12435 {
12436 EMSG(_("E240: No connection to Vim server"));
12437 return FAIL;
12438 }
12439 return OK;
12440}
12441#endif
12442
12443#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012444static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012445
12446 static void
12447remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012448 typval_T *argvars;
12449 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012450 int expr;
12451{
12452 char_u *server_name;
12453 char_u *keys;
12454 char_u *r = NULL;
12455 char_u buf[NUMBUFLEN];
12456# ifdef WIN32
12457 HWND w;
12458# else
12459 Window w;
12460# endif
12461
12462 if (check_restricted() || check_secure())
12463 return;
12464
12465# ifdef FEAT_X11
12466 if (check_connection() == FAIL)
12467 return;
12468# endif
12469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012470 server_name = get_tv_string_chk(&argvars[0]);
12471 if (server_name == NULL)
12472 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012473 keys = get_tv_string_buf(&argvars[1], buf);
12474# ifdef WIN32
12475 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12476# else
12477 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12478 < 0)
12479# endif
12480 {
12481 if (r != NULL)
12482 EMSG(r); /* sending worked but evaluation failed */
12483 else
12484 EMSG2(_("E241: Unable to send to %s"), server_name);
12485 return;
12486 }
12487
12488 rettv->vval.v_string = r;
12489
12490 if (argvars[2].v_type != VAR_UNKNOWN)
12491 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012492 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012493 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012494 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012495
12496 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012497 v.di_tv.v_type = VAR_STRING;
12498 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012499 idvar = get_tv_string_chk(&argvars[2]);
12500 if (idvar != NULL)
12501 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012502 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012503 }
12504}
12505#endif
12506
12507/*
12508 * "remote_expr()" function
12509 */
12510/*ARGSUSED*/
12511 static void
12512f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012513 typval_T *argvars;
12514 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012515{
12516 rettv->v_type = VAR_STRING;
12517 rettv->vval.v_string = NULL;
12518#ifdef FEAT_CLIENTSERVER
12519 remote_common(argvars, rettv, TRUE);
12520#endif
12521}
12522
12523/*
12524 * "remote_foreground()" function
12525 */
12526/*ARGSUSED*/
12527 static void
12528f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012529 typval_T *argvars;
12530 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012531{
12532 rettv->vval.v_number = 0;
12533#ifdef FEAT_CLIENTSERVER
12534# ifdef WIN32
12535 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012536 {
12537 char_u *server_name = get_tv_string_chk(&argvars[0]);
12538
12539 if (server_name != NULL)
12540 serverForeground(server_name);
12541 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012542# else
12543 /* Send a foreground() expression to the server. */
12544 argvars[1].v_type = VAR_STRING;
12545 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12546 argvars[2].v_type = VAR_UNKNOWN;
12547 remote_common(argvars, rettv, TRUE);
12548 vim_free(argvars[1].vval.v_string);
12549# endif
12550#endif
12551}
12552
12553/*ARGSUSED*/
12554 static void
12555f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012556 typval_T *argvars;
12557 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012558{
12559#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012560 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012561 char_u *s = NULL;
12562# ifdef WIN32
12563 int n = 0;
12564# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012565 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012566
12567 if (check_restricted() || check_secure())
12568 {
12569 rettv->vval.v_number = -1;
12570 return;
12571 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012572 serverid = get_tv_string_chk(&argvars[0]);
12573 if (serverid == NULL)
12574 {
12575 rettv->vval.v_number = -1;
12576 return; /* type error; errmsg already given */
12577 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012578# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012579 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012580 if (n == 0)
12581 rettv->vval.v_number = -1;
12582 else
12583 {
12584 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12585 rettv->vval.v_number = (s != NULL);
12586 }
12587# else
12588 rettv->vval.v_number = 0;
12589 if (check_connection() == FAIL)
12590 return;
12591
12592 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012593 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012594# endif
12595
12596 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012598 char_u *retvar;
12599
Bram Moolenaar33570922005-01-25 22:26:29 +000012600 v.di_tv.v_type = VAR_STRING;
12601 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012602 retvar = get_tv_string_chk(&argvars[1]);
12603 if (retvar != NULL)
12604 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012605 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012606 }
12607#else
12608 rettv->vval.v_number = -1;
12609#endif
12610}
12611
12612/*ARGSUSED*/
12613 static void
12614f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012615 typval_T *argvars;
12616 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012617{
12618 char_u *r = NULL;
12619
12620#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012621 char_u *serverid = get_tv_string_chk(&argvars[0]);
12622
12623 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012624 {
12625# ifdef WIN32
12626 /* The server's HWND is encoded in the 'id' parameter */
12627 int n = 0;
12628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012629 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012630 if (n != 0)
12631 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12632 if (r == NULL)
12633# else
12634 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012635 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012636# endif
12637 EMSG(_("E277: Unable to read a server reply"));
12638 }
12639#endif
12640 rettv->v_type = VAR_STRING;
12641 rettv->vval.v_string = r;
12642}
12643
12644/*
12645 * "remote_send()" function
12646 */
12647/*ARGSUSED*/
12648 static void
12649f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012650 typval_T *argvars;
12651 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012652{
12653 rettv->v_type = VAR_STRING;
12654 rettv->vval.v_string = NULL;
12655#ifdef FEAT_CLIENTSERVER
12656 remote_common(argvars, rettv, FALSE);
12657#endif
12658}
12659
12660/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012661 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012662 */
12663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012664f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012665 typval_T *argvars;
12666 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012667{
Bram Moolenaar33570922005-01-25 22:26:29 +000012668 list_T *l;
12669 listitem_T *item, *item2;
12670 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012671 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012672 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012673 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012674 dict_T *d;
12675 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012676
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012677 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012678 if (argvars[0].v_type == VAR_DICT)
12679 {
12680 if (argvars[2].v_type != VAR_UNKNOWN)
12681 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012682 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012683 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012685 key = get_tv_string_chk(&argvars[1]);
12686 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012688 di = dict_find(d, key, -1);
12689 if (di == NULL)
12690 EMSG2(_(e_dictkey), key);
12691 else
12692 {
12693 *rettv = di->di_tv;
12694 init_tv(&di->di_tv);
12695 dictitem_remove(d, di);
12696 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012697 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012698 }
12699 }
12700 else if (argvars[0].v_type != VAR_LIST)
12701 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012702 else if ((l = argvars[0].vval.v_list) != NULL
12703 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012705 int error = FALSE;
12706
12707 idx = get_tv_number_chk(&argvars[1], &error);
12708 if (error)
12709 ; /* type error: do nothing, errmsg already given */
12710 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012711 EMSGN(_(e_listidx), idx);
12712 else
12713 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012714 if (argvars[2].v_type == VAR_UNKNOWN)
12715 {
12716 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012717 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012718 *rettv = item->li_tv;
12719 vim_free(item);
12720 }
12721 else
12722 {
12723 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012724 end = get_tv_number_chk(&argvars[2], &error);
12725 if (error)
12726 ; /* type error: do nothing */
12727 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012728 EMSGN(_(e_listidx), end);
12729 else
12730 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012731 int cnt = 0;
12732
12733 for (li = item; li != NULL; li = li->li_next)
12734 {
12735 ++cnt;
12736 if (li == item2)
12737 break;
12738 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012739 if (li == NULL) /* didn't find "item2" after "item" */
12740 EMSG(_(e_invrange));
12741 else
12742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012743 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012744 l = list_alloc();
12745 if (l != NULL)
12746 {
12747 rettv->v_type = VAR_LIST;
12748 rettv->vval.v_list = l;
12749 l->lv_first = item;
12750 l->lv_last = item2;
12751 l->lv_refcount = 1;
12752 item->li_prev = NULL;
12753 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012754 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012755 }
12756 }
12757 }
12758 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012759 }
12760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761}
12762
12763/*
12764 * "rename({from}, {to})" function
12765 */
12766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012767f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012768 typval_T *argvars;
12769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770{
12771 char_u buf[NUMBUFLEN];
12772
12773 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12777 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778}
12779
12780/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012781 * "repeat()" function
12782 */
12783/*ARGSUSED*/
12784 static void
12785f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012786 typval_T *argvars;
12787 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012788{
12789 char_u *p;
12790 int n;
12791 int slen;
12792 int len;
12793 char_u *r;
12794 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012795 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012796
12797 n = get_tv_number(&argvars[1]);
12798 if (argvars[0].v_type == VAR_LIST)
12799 {
12800 l = list_alloc();
12801 if (l != NULL && argvars[0].vval.v_list != NULL)
12802 {
12803 l->lv_refcount = 1;
12804 while (n-- > 0)
12805 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12806 break;
12807 }
12808 rettv->v_type = VAR_LIST;
12809 rettv->vval.v_list = l;
12810 }
12811 else
12812 {
12813 p = get_tv_string(&argvars[0]);
12814 rettv->v_type = VAR_STRING;
12815 rettv->vval.v_string = NULL;
12816
12817 slen = (int)STRLEN(p);
12818 len = slen * n;
12819 if (len <= 0)
12820 return;
12821
12822 r = alloc(len + 1);
12823 if (r != NULL)
12824 {
12825 for (i = 0; i < n; i++)
12826 mch_memmove(r + i * slen, p, (size_t)slen);
12827 r[len] = NUL;
12828 }
12829
12830 rettv->vval.v_string = r;
12831 }
12832}
12833
12834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835 * "resolve()" function
12836 */
12837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012838f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012839 typval_T *argvars;
12840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841{
12842 char_u *p;
12843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845#ifdef FEAT_SHORTCUT
12846 {
12847 char_u *v = NULL;
12848
12849 v = mch_resolve_shortcut(p);
12850 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 }
12855#else
12856# ifdef HAVE_READLINK
12857 {
12858 char_u buf[MAXPATHL + 1];
12859 char_u *cpy;
12860 int len;
12861 char_u *remain = NULL;
12862 char_u *q;
12863 int is_relative_to_current = FALSE;
12864 int has_trailing_pathsep = FALSE;
12865 int limit = 100;
12866
12867 p = vim_strsave(p);
12868
12869 if (p[0] == '.' && (vim_ispathsep(p[1])
12870 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12871 is_relative_to_current = TRUE;
12872
12873 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012874 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875 has_trailing_pathsep = TRUE;
12876
12877 q = getnextcomp(p);
12878 if (*q != NUL)
12879 {
12880 /* Separate the first path component in "p", and keep the
12881 * remainder (beginning with the path separator). */
12882 remain = vim_strsave(q - 1);
12883 q[-1] = NUL;
12884 }
12885
12886 for (;;)
12887 {
12888 for (;;)
12889 {
12890 len = readlink((char *)p, (char *)buf, MAXPATHL);
12891 if (len <= 0)
12892 break;
12893 buf[len] = NUL;
12894
12895 if (limit-- == 0)
12896 {
12897 vim_free(p);
12898 vim_free(remain);
12899 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901 goto fail;
12902 }
12903
12904 /* Ensure that the result will have a trailing path separator
12905 * if the argument has one. */
12906 if (remain == NULL && has_trailing_pathsep)
12907 add_pathsep(buf);
12908
12909 /* Separate the first path component in the link value and
12910 * concatenate the remainders. */
12911 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12912 if (*q != NUL)
12913 {
12914 if (remain == NULL)
12915 remain = vim_strsave(q - 1);
12916 else
12917 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000012918 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 if (cpy != NULL)
12920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921 vim_free(remain);
12922 remain = cpy;
12923 }
12924 }
12925 q[-1] = NUL;
12926 }
12927
12928 q = gettail(p);
12929 if (q > p && *q == NUL)
12930 {
12931 /* Ignore trailing path separator. */
12932 q[-1] = NUL;
12933 q = gettail(p);
12934 }
12935 if (q > p && !mch_isFullName(buf))
12936 {
12937 /* symlink is relative to directory of argument */
12938 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12939 if (cpy != NULL)
12940 {
12941 STRCPY(cpy, p);
12942 STRCPY(gettail(cpy), buf);
12943 vim_free(p);
12944 p = cpy;
12945 }
12946 }
12947 else
12948 {
12949 vim_free(p);
12950 p = vim_strsave(buf);
12951 }
12952 }
12953
12954 if (remain == NULL)
12955 break;
12956
12957 /* Append the first path component of "remain" to "p". */
12958 q = getnextcomp(remain + 1);
12959 len = q - remain - (*q != NUL);
12960 cpy = vim_strnsave(p, STRLEN(p) + len);
12961 if (cpy != NULL)
12962 {
12963 STRNCAT(cpy, remain, len);
12964 vim_free(p);
12965 p = cpy;
12966 }
12967 /* Shorten "remain". */
12968 if (*q != NUL)
12969 STRCPY(remain, q - 1);
12970 else
12971 {
12972 vim_free(remain);
12973 remain = NULL;
12974 }
12975 }
12976
12977 /* If the result is a relative path name, make it explicitly relative to
12978 * the current directory if and only if the argument had this form. */
12979 if (!vim_ispathsep(*p))
12980 {
12981 if (is_relative_to_current
12982 && *p != NUL
12983 && !(p[0] == '.'
12984 && (p[1] == NUL
12985 || vim_ispathsep(p[1])
12986 || (p[1] == '.'
12987 && (p[2] == NUL
12988 || vim_ispathsep(p[2]))))))
12989 {
12990 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012991 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992 if (cpy != NULL)
12993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994 vim_free(p);
12995 p = cpy;
12996 }
12997 }
12998 else if (!is_relative_to_current)
12999 {
13000 /* Strip leading "./". */
13001 q = p;
13002 while (q[0] == '.' && vim_ispathsep(q[1]))
13003 q += 2;
13004 if (q > p)
13005 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13006 }
13007 }
13008
13009 /* Ensure that the result will have no trailing path separator
13010 * if the argument had none. But keep "/" or "//". */
13011 if (!has_trailing_pathsep)
13012 {
13013 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013014 if (after_pathsep(p, q))
13015 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016 }
13017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013018 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019 }
13020# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013021 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022# endif
13023#endif
13024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013025 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026
13027#ifdef HAVE_READLINK
13028fail:
13029#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013030 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031}
13032
13033/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013034 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035 */
13036 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013037f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013038 typval_T *argvars;
13039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040{
Bram Moolenaar33570922005-01-25 22:26:29 +000013041 list_T *l;
13042 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043
Bram Moolenaar0d660222005-01-07 21:51:51 +000013044 rettv->vval.v_number = 0;
13045 if (argvars[0].v_type != VAR_LIST)
13046 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013047 else if ((l = argvars[0].vval.v_list) != NULL
13048 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013049 {
13050 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013051 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013052 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013053 while (li != NULL)
13054 {
13055 ni = li->li_prev;
13056 list_append(l, li);
13057 li = ni;
13058 }
13059 rettv->vval.v_list = l;
13060 rettv->v_type = VAR_LIST;
13061 ++l->lv_refcount;
13062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063}
13064
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013065#define SP_NOMOVE 1 /* don't move cursor */
13066#define SP_REPEAT 2 /* repeat to find outer pair */
13067#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000013068#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013069
Bram Moolenaar33570922005-01-25 22:26:29 +000013070static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013071
13072/*
13073 * Get flags for a search function.
13074 * Possibly sets "p_ws".
13075 * Returns BACKWARD, FORWARD or zero (for an error).
13076 */
13077 static int
13078get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013079 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013080 int *flagsp;
13081{
13082 int dir = FORWARD;
13083 char_u *flags;
13084 char_u nbuf[NUMBUFLEN];
13085 int mask;
13086
13087 if (varp->v_type != VAR_UNKNOWN)
13088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013089 flags = get_tv_string_buf_chk(varp, nbuf);
13090 if (flags == NULL)
13091 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013092 while (*flags != NUL)
13093 {
13094 switch (*flags)
13095 {
13096 case 'b': dir = BACKWARD; break;
13097 case 'w': p_ws = TRUE; break;
13098 case 'W': p_ws = FALSE; break;
13099 default: mask = 0;
13100 if (flagsp != NULL)
13101 switch (*flags)
13102 {
13103 case 'n': mask = SP_NOMOVE; break;
13104 case 'r': mask = SP_REPEAT; break;
13105 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013106 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013107 }
13108 if (mask == 0)
13109 {
13110 EMSG2(_(e_invarg2), flags);
13111 dir = 0;
13112 }
13113 else
13114 *flagsp |= mask;
13115 }
13116 if (dir == 0)
13117 break;
13118 ++flags;
13119 }
13120 }
13121 return dir;
13122}
13123
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124/*
13125 * "search()" function
13126 */
13127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013129 typval_T *argvars;
13130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131{
13132 char_u *pat;
13133 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013134 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135 int save_p_ws = p_ws;
13136 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013137 int flags = 0;
13138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013142 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13143 if (dir == 0)
13144 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013145 /*
13146 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13147 * Check to make sure only those flags are set.
13148 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13149 * flags cannot be set. Check for that condition also.
13150 */
13151 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13152 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013153 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013154 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013155 goto theend;
13156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013158 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13160 SEARCH_KEEP, RE_SEARCH) != FAIL)
13161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013162 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013163 if (flags & SP_SETPCMARK)
13164 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165 curwin->w_cursor = pos;
13166 /* "/$" will put the cursor after the end of the line, may need to
13167 * correct that here */
13168 check_cursor();
13169 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013170
13171 /* If 'n' flag is used: restore cursor position. */
13172 if (flags & SP_NOMOVE)
13173 curwin->w_cursor = save_cursor;
13174theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175 p_ws = save_p_ws;
13176}
13177
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013179 * "searchdecl()" function
13180 */
13181 static void
13182f_searchdecl(argvars, rettv)
13183 typval_T *argvars;
13184 typval_T *rettv;
13185{
13186 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013187 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013188 int error = FALSE;
13189 char_u *name;
13190
13191 rettv->vval.v_number = 1; /* default: FAIL */
13192
13193 name = get_tv_string_chk(&argvars[0]);
13194 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013195 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013196 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013197 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13198 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13199 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013200 if (!error && name != NULL)
13201 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013202 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013203}
13204
13205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206 * "searchpair()" function
13207 */
13208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013209f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013210 typval_T *argvars;
13211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212{
13213 char_u *spat, *mpat, *epat;
13214 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216 int dir;
13217 int flags = 0;
13218 char_u nbuf1[NUMBUFLEN];
13219 char_u nbuf2[NUMBUFLEN];
13220 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013222 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013225 spat = get_tv_string_chk(&argvars[0]);
13226 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13227 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13228 if (spat == NULL || mpat == NULL || epat == NULL)
13229 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 /* Handle the optional fourth argument: flags */
13232 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013233 if (dir == 0)
13234 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013235 /*
13236 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13237 */
13238 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13239 {
13240 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13241 goto theend;
13242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243
13244 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013245 if (argvars[3].v_type == VAR_UNKNOWN
13246 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 skip = (char_u *)"";
13248 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013249 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13250 if (skip == NULL)
13251 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013253 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13254
13255theend:
13256 p_ws = save_p_ws;
13257}
13258
13259/*
13260 * Search for a start/middle/end thing.
13261 * Used by searchpair(), see its documentation for the details.
13262 * Returns 0 or -1 for no match,
13263 */
13264 long
13265do_searchpair(spat, mpat, epat, dir, skip, flags)
13266 char_u *spat; /* start pattern */
13267 char_u *mpat; /* middle pattern */
13268 char_u *epat; /* end pattern */
13269 int dir; /* BACKWARD or FORWARD */
13270 char_u *skip; /* skip expression */
13271 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13272{
13273 char_u *save_cpo;
13274 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13275 long retval = 0;
13276 pos_T pos;
13277 pos_T firstpos;
13278 pos_T foundpos;
13279 pos_T save_cursor;
13280 pos_T save_pos;
13281 int n;
13282 int r;
13283 int nest = 1;
13284 int err;
13285
13286 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13287 save_cpo = p_cpo;
13288 p_cpo = (char_u *)"";
13289
13290 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13291 * start/middle/end (pat3, for the top pair). */
13292 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13293 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13294 if (pat2 == NULL || pat3 == NULL)
13295 goto theend;
13296 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13297 if (*mpat == NUL)
13298 STRCPY(pat3, pat2);
13299 else
13300 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13301 spat, epat, mpat);
13302
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303 save_cursor = curwin->w_cursor;
13304 pos = curwin->w_cursor;
13305 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013306 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307 pat = pat3;
13308 for (;;)
13309 {
13310 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13311 SEARCH_KEEP, RE_SEARCH);
13312 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13313 /* didn't find it or found the first match again: FAIL */
13314 break;
13315
13316 if (firstpos.lnum == 0)
13317 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013318 if (equalpos(pos, foundpos))
13319 {
13320 /* Found the same position again. Can happen with a pattern that
13321 * has "\zs" at the end and searching backwards. Advance one
13322 * character and try again. */
13323 if (dir == BACKWARD)
13324 decl(&pos);
13325 else
13326 incl(&pos);
13327 }
13328 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329
13330 /* If the skip pattern matches, ignore this match. */
13331 if (*skip != NUL)
13332 {
13333 save_pos = curwin->w_cursor;
13334 curwin->w_cursor = pos;
13335 r = eval_to_bool(skip, &err, NULL, FALSE);
13336 curwin->w_cursor = save_pos;
13337 if (err)
13338 {
13339 /* Evaluating {skip} caused an error, break here. */
13340 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013341 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342 break;
13343 }
13344 if (r)
13345 continue;
13346 }
13347
13348 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13349 {
13350 /* Found end when searching backwards or start when searching
13351 * forward: nested pair. */
13352 ++nest;
13353 pat = pat2; /* nested, don't search for middle */
13354 }
13355 else
13356 {
13357 /* Found end when searching forward or start when searching
13358 * backward: end of (nested) pair; or found middle in outer pair. */
13359 if (--nest == 1)
13360 pat = pat3; /* outer level, search for middle */
13361 }
13362
13363 if (nest == 0)
13364 {
13365 /* Found the match: return matchcount or line number. */
13366 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013367 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013369 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013370 if (flags & SP_SETPCMARK)
13371 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372 curwin->w_cursor = pos;
13373 if (!(flags & SP_REPEAT))
13374 break;
13375 nest = 1; /* search for next unmatched */
13376 }
13377 }
13378
13379 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013380 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381 curwin->w_cursor = save_cursor;
13382
13383theend:
13384 vim_free(pat2);
13385 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013387
13388 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389}
13390
Bram Moolenaar0d660222005-01-07 21:51:51 +000013391/*ARGSUSED*/
13392 static void
13393f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013394 typval_T *argvars;
13395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013397#ifdef FEAT_CLIENTSERVER
13398 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013399 char_u *server = get_tv_string_chk(&argvars[0]);
13400 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401
Bram Moolenaar0d660222005-01-07 21:51:51 +000013402 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013403 if (server == NULL || reply == NULL)
13404 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013405 if (check_restricted() || check_secure())
13406 return;
13407# ifdef FEAT_X11
13408 if (check_connection() == FAIL)
13409 return;
13410# endif
13411
13412 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013414 EMSG(_("E258: Unable to send to client"));
13415 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013417 rettv->vval.v_number = 0;
13418#else
13419 rettv->vval.v_number = -1;
13420#endif
13421}
13422
13423/*ARGSUSED*/
13424 static void
13425f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013426 typval_T *argvars;
13427 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013428{
13429 char_u *r = NULL;
13430
13431#ifdef FEAT_CLIENTSERVER
13432# ifdef WIN32
13433 r = serverGetVimNames();
13434# else
13435 make_connection();
13436 if (X_DISPLAY != NULL)
13437 r = serverGetVimNames(X_DISPLAY);
13438# endif
13439#endif
13440 rettv->v_type = VAR_STRING;
13441 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442}
13443
13444/*
13445 * "setbufvar()" function
13446 */
13447/*ARGSUSED*/
13448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013450 typval_T *argvars;
13451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452{
13453 buf_T *buf;
13454#ifdef FEAT_AUTOCMD
13455 aco_save_T aco;
13456#else
13457 buf_T *save_curbuf;
13458#endif
13459 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013460 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461 char_u nbuf[NUMBUFLEN];
13462
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013463 rettv->vval.v_number = 0;
13464
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465 if (check_restricted() || check_secure())
13466 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013467 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13468 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013469 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470 varp = &argvars[2];
13471
13472 if (buf != NULL && varname != NULL && varp != NULL)
13473 {
13474 /* set curbuf to be our buf, temporarily */
13475#ifdef FEAT_AUTOCMD
13476 aucmd_prepbuf(&aco, buf);
13477#else
13478 save_curbuf = curbuf;
13479 curbuf = buf;
13480#endif
13481
13482 if (*varname == '&')
13483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013484 long numval;
13485 char_u *strval;
13486 int error = FALSE;
13487
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013489 numval = get_tv_number_chk(varp, &error);
13490 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013491 if (!error && strval != NULL)
13492 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493 }
13494 else
13495 {
13496 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13497 if (bufvarname != NULL)
13498 {
13499 STRCPY(bufvarname, "b:");
13500 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013501 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502 vim_free(bufvarname);
13503 }
13504 }
13505
13506 /* reset notion of buffer */
13507#ifdef FEAT_AUTOCMD
13508 aucmd_restbuf(&aco);
13509#else
13510 curbuf = save_curbuf;
13511#endif
13512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513}
13514
13515/*
13516 * "setcmdpos()" function
13517 */
13518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013520 typval_T *argvars;
13521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013523 int pos = (int)get_tv_number(&argvars[0]) - 1;
13524
13525 if (pos >= 0)
13526 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527}
13528
13529/*
13530 * "setline()" function
13531 */
13532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013533f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013534 typval_T *argvars;
13535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536{
13537 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013538 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013539 list_T *l = NULL;
13540 listitem_T *li = NULL;
13541 long added = 0;
13542 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013544 lnum = get_tv_lnum(&argvars[0]);
13545 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013547 l = argvars[1].vval.v_list;
13548 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013550 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013551 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013552
13553 rettv->vval.v_number = 0; /* OK */
13554 for (;;)
13555 {
13556 if (l != NULL)
13557 {
13558 /* list argument, get next string */
13559 if (li == NULL)
13560 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013561 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013562 li = li->li_next;
13563 }
13564
13565 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013566 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013567 break;
13568 if (lnum <= curbuf->b_ml.ml_line_count)
13569 {
13570 /* existing line, replace it */
13571 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13572 {
13573 changed_bytes(lnum, 0);
13574 check_cursor_col();
13575 rettv->vval.v_number = 0; /* OK */
13576 }
13577 }
13578 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13579 {
13580 /* lnum is one past the last line, append the line */
13581 ++added;
13582 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13583 rettv->vval.v_number = 0; /* OK */
13584 }
13585
13586 if (l == NULL) /* only one string argument */
13587 break;
13588 ++lnum;
13589 }
13590
13591 if (added > 0)
13592 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593}
13594
13595/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013596 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013597 */
13598/*ARGSUSED*/
13599 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013600set_qf_ll_list(wp, list_arg, action_arg, rettv)
13601 win_T *wp;
13602 typval_T *list_arg;
13603 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013604 typval_T *rettv;
13605{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013606#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013607 char_u *act;
13608 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013609#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013610
Bram Moolenaar2641f772005-03-25 21:58:17 +000013611 rettv->vval.v_number = -1;
13612
13613#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013614 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013615 EMSG(_(e_listreq));
13616 else
13617 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013618 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013619
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013620 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013621 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013622 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013623 if (act == NULL)
13624 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013625 if (*act == 'a' || *act == 'r')
13626 action = *act;
13627 }
13628
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013629 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013630 rettv->vval.v_number = 0;
13631 }
13632#endif
13633}
13634
13635/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013636 * "setloclist()" function
13637 */
13638/*ARGSUSED*/
13639 static void
13640f_setloclist(argvars, rettv)
13641 typval_T *argvars;
13642 typval_T *rettv;
13643{
13644 win_T *win;
13645
13646 rettv->vval.v_number = -1;
13647
13648 win = find_win_by_nr(&argvars[0]);
13649 if (win != NULL)
13650 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13651}
13652
13653/*
13654 * "setqflist()" function
13655 */
13656/*ARGSUSED*/
13657 static void
13658f_setqflist(argvars, rettv)
13659 typval_T *argvars;
13660 typval_T *rettv;
13661{
13662 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
13663}
13664
13665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666 * "setreg()" function
13667 */
13668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013669f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013670 typval_T *argvars;
13671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672{
13673 int regname;
13674 char_u *strregname;
13675 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013676 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 int append;
13678 char_u yank_type;
13679 long block_len;
13680
13681 block_len = -1;
13682 yank_type = MAUTO;
13683 append = FALSE;
13684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013685 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013686 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013688 if (strregname == NULL)
13689 return; /* type error; errmsg already given */
13690 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 if (regname == 0 || regname == '@')
13692 regname = '"';
13693 else if (regname == '=')
13694 return;
13695
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013696 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013698 stropt = get_tv_string_chk(&argvars[2]);
13699 if (stropt == NULL)
13700 return; /* type error */
13701 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702 switch (*stropt)
13703 {
13704 case 'a': case 'A': /* append */
13705 append = TRUE;
13706 break;
13707 case 'v': case 'c': /* character-wise selection */
13708 yank_type = MCHAR;
13709 break;
13710 case 'V': case 'l': /* line-wise selection */
13711 yank_type = MLINE;
13712 break;
13713#ifdef FEAT_VISUAL
13714 case 'b': case Ctrl_V: /* block-wise selection */
13715 yank_type = MBLOCK;
13716 if (VIM_ISDIGIT(stropt[1]))
13717 {
13718 ++stropt;
13719 block_len = getdigits(&stropt) - 1;
13720 --stropt;
13721 }
13722 break;
13723#endif
13724 }
13725 }
13726
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013727 strval = get_tv_string_chk(&argvars[1]);
13728 if (strval != NULL)
13729 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013731 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732}
13733
13734
13735/*
13736 * "setwinvar(expr)" function
13737 */
13738/*ARGSUSED*/
13739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013740f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013741 typval_T *argvars;
13742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743{
13744 win_T *win;
13745#ifdef FEAT_WINDOWS
13746 win_T *save_curwin;
13747#endif
13748 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013749 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 char_u nbuf[NUMBUFLEN];
13751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013752 rettv->vval.v_number = 0;
13753
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754 if (check_restricted() || check_secure())
13755 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013757 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 varp = &argvars[2];
13759
13760 if (win != NULL && varname != NULL && varp != NULL)
13761 {
13762#ifdef FEAT_WINDOWS
13763 /* set curwin to be our win, temporarily */
13764 save_curwin = curwin;
13765 curwin = win;
13766 curbuf = curwin->w_buffer;
13767#endif
13768
13769 if (*varname == '&')
13770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013771 long numval;
13772 char_u *strval;
13773 int error = FALSE;
13774
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013776 numval = get_tv_number_chk(varp, &error);
13777 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013778 if (!error && strval != NULL)
13779 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780 }
13781 else
13782 {
13783 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13784 if (winvarname != NULL)
13785 {
13786 STRCPY(winvarname, "w:");
13787 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013788 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789 vim_free(winvarname);
13790 }
13791 }
13792
13793#ifdef FEAT_WINDOWS
13794 /* Restore current window, if it's still valid (autocomands can make
13795 * it invalid). */
13796 if (win_valid(save_curwin))
13797 {
13798 curwin = save_curwin;
13799 curbuf = curwin->w_buffer;
13800 }
13801#endif
13802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803}
13804
13805/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013806 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 */
13808 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013809f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013810 typval_T *argvars;
13811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013813 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814
Bram Moolenaar0d660222005-01-07 21:51:51 +000013815 p = get_tv_string(&argvars[0]);
13816 rettv->vval.v_string = vim_strsave(p);
13817 simplify_filename(rettv->vval.v_string); /* simplify in place */
13818 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819}
13820
Bram Moolenaar0d660222005-01-07 21:51:51 +000013821static int
13822#ifdef __BORLANDC__
13823 _RTLENTRYF
13824#endif
13825 item_compare __ARGS((const void *s1, const void *s2));
13826static int
13827#ifdef __BORLANDC__
13828 _RTLENTRYF
13829#endif
13830 item_compare2 __ARGS((const void *s1, const void *s2));
13831
13832static int item_compare_ic;
13833static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013834static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013835#define ITEM_COMPARE_FAIL 999
13836
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013838 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013840 static int
13841#ifdef __BORLANDC__
13842_RTLENTRYF
13843#endif
13844item_compare(s1, s2)
13845 const void *s1;
13846 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013848 char_u *p1, *p2;
13849 char_u *tofree1, *tofree2;
13850 int res;
13851 char_u numbuf1[NUMBUFLEN];
13852 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013854 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
13855 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013856 if (item_compare_ic)
13857 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013859 res = STRCMP(p1, p2);
13860 vim_free(tofree1);
13861 vim_free(tofree2);
13862 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863}
13864
13865 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013866#ifdef __BORLANDC__
13867_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013869item_compare2(s1, s2)
13870 const void *s1;
13871 const void *s2;
13872{
13873 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013874 typval_T rettv;
13875 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013876 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013878 /* shortcut after failure in previous call; compare all items equal */
13879 if (item_compare_func_err)
13880 return 0;
13881
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013882 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13883 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013884 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13885 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013886
13887 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13888 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013889 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013890 clear_tv(&argv[0]);
13891 clear_tv(&argv[1]);
13892
13893 if (res == FAIL)
13894 res = ITEM_COMPARE_FAIL;
13895 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013896 /* return value has wrong type */
13897 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13898 if (item_compare_func_err)
13899 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013900 clear_tv(&rettv);
13901 return res;
13902}
13903
13904/*
13905 * "sort({list})" function
13906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013908f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013909 typval_T *argvars;
13910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911{
Bram Moolenaar33570922005-01-25 22:26:29 +000013912 list_T *l;
13913 listitem_T *li;
13914 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013915 long len;
13916 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917
Bram Moolenaar0d660222005-01-07 21:51:51 +000013918 rettv->vval.v_number = 0;
13919 if (argvars[0].v_type != VAR_LIST)
13920 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 else
13922 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013923 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013924 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013925 return;
13926 rettv->vval.v_list = l;
13927 rettv->v_type = VAR_LIST;
13928 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929
Bram Moolenaar0d660222005-01-07 21:51:51 +000013930 len = list_len(l);
13931 if (len <= 1)
13932 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933
Bram Moolenaar0d660222005-01-07 21:51:51 +000013934 item_compare_ic = FALSE;
13935 item_compare_func = NULL;
13936 if (argvars[1].v_type != VAR_UNKNOWN)
13937 {
13938 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013939 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013940 else
13941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013942 int error = FALSE;
13943
13944 i = get_tv_number_chk(&argvars[1], &error);
13945 if (error)
13946 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013947 if (i == 1)
13948 item_compare_ic = TRUE;
13949 else
13950 item_compare_func = get_tv_string(&argvars[1]);
13951 }
13952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953
Bram Moolenaar0d660222005-01-07 21:51:51 +000013954 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013955 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013956 if (ptrs == NULL)
13957 return;
13958 i = 0;
13959 for (li = l->lv_first; li != NULL; li = li->li_next)
13960 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013962 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013963 /* test the compare function */
13964 if (item_compare_func != NULL
13965 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13966 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013967 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013969 {
13970 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013971 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013972 item_compare_func == NULL ? item_compare : item_compare2);
13973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013974 if (!item_compare_func_err)
13975 {
13976 /* Clear the List and append the items in the sorted order. */
13977 l->lv_first = l->lv_last = NULL;
13978 l->lv_len = 0;
13979 for (i = 0; i < len; ++i)
13980 list_append(l, ptrs[i]);
13981 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013982 }
13983
13984 vim_free(ptrs);
13985 }
13986}
13987
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013988/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013989 * "soundfold({word})" function
13990 */
13991 static void
13992f_soundfold(argvars, rettv)
13993 typval_T *argvars;
13994 typval_T *rettv;
13995{
13996 char_u *s;
13997
13998 rettv->v_type = VAR_STRING;
13999 s = get_tv_string(&argvars[0]);
14000#ifdef FEAT_SYN_HL
14001 rettv->vval.v_string = eval_soundfold(s);
14002#else
14003 rettv->vval.v_string = vim_strsave(s);
14004#endif
14005}
14006
14007/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014008 * "spellbadword()" function
14009 */
14010/* ARGSUSED */
14011 static void
14012f_spellbadword(argvars, rettv)
14013 typval_T *argvars;
14014 typval_T *rettv;
14015{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014016 char_u *word = (char_u *)"";
14017#ifdef FEAT_SYN_HL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014018 int len = 0;
14019 hlf_T attr = HLF_COUNT;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014020 list_T *l;
14021#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014022
Bram Moolenaar4463f292005-09-25 22:20:24 +000014023 l = list_alloc();
14024 if (l == NULL)
14025 return;
14026 rettv->v_type = VAR_LIST;
14027 rettv->vval.v_list = l;
14028 ++l->lv_refcount;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014029
14030#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014031 if (argvars[0].v_type == VAR_UNKNOWN)
14032 {
14033 /* Find the start and length of the badly spelled word. */
14034 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14035 if (len != 0)
14036 word = ml_get_cursor();
14037 }
14038 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14039 {
14040 char_u *str = get_tv_string_chk(&argvars[0]);
14041 int capcol = -1;
14042
14043 if (str != NULL)
14044 {
14045 /* Check the argument for spelling. */
14046 while (*str != NUL)
14047 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014048 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014049 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014050 {
14051 word = str;
14052 break;
14053 }
14054 str += len;
14055 }
14056 }
14057 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014058#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014059
14060 list_append_string(l, word, len);
14061 list_append_string(l, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014062 attr == HLF_SPB ? "bad" :
14063 attr == HLF_SPR ? "rare" :
14064 attr == HLF_SPL ? "local" :
14065 attr == HLF_SPC ? "caps" :
14066 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014067}
14068
14069/*
14070 * "spellsuggest()" function
14071 */
14072 static void
14073f_spellsuggest(argvars, rettv)
14074 typval_T *argvars;
14075 typval_T *rettv;
14076{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014077 list_T *l;
14078#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014079 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014080 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014081 int maxcount;
14082 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014083 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014084 listitem_T *li;
14085 int need_capital = FALSE;
14086#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014087
14088 l = list_alloc();
14089 if (l == NULL)
14090 return;
14091 rettv->v_type = VAR_LIST;
14092 rettv->vval.v_list = l;
14093 ++l->lv_refcount;
14094
14095#ifdef FEAT_SYN_HL
14096 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14097 {
14098 str = get_tv_string(&argvars[0]);
14099 if (argvars[1].v_type != VAR_UNKNOWN)
14100 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014101 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014102 if (maxcount <= 0)
14103 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014104 if (argvars[2].v_type != VAR_UNKNOWN)
14105 {
14106 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14107 if (typeerr)
14108 return;
14109 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014110 }
14111 else
14112 maxcount = 25;
14113
Bram Moolenaar4770d092006-01-12 23:22:24 +000014114 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014115
14116 for (i = 0; i < ga.ga_len; ++i)
14117 {
14118 str = ((char_u **)ga.ga_data)[i];
14119
14120 li = listitem_alloc();
14121 if (li == NULL)
14122 vim_free(str);
14123 else
14124 {
14125 li->li_tv.v_type = VAR_STRING;
14126 li->li_tv.v_lock = 0;
14127 li->li_tv.vval.v_string = str;
14128 list_append(l, li);
14129 }
14130 }
14131 ga_clear(&ga);
14132 }
14133#endif
14134}
14135
Bram Moolenaar0d660222005-01-07 21:51:51 +000014136 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014137f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014138 typval_T *argvars;
14139 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014140{
14141 char_u *str;
14142 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014143 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014144 regmatch_T regmatch;
14145 char_u patbuf[NUMBUFLEN];
14146 char_u *save_cpo;
14147 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000014148 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014149 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014150 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014152
14153 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14154 save_cpo = p_cpo;
14155 p_cpo = (char_u *)"";
14156
14157 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014158 if (argvars[1].v_type != VAR_UNKNOWN)
14159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014160 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14161 if (pat == NULL)
14162 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014163 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014165 }
14166 if (pat == NULL || *pat == NUL)
14167 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014168
14169 l = list_alloc();
14170 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014172 rettv->v_type = VAR_LIST;
14173 rettv->vval.v_list = l;
14174 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 if (typeerr)
14176 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177
Bram Moolenaar0d660222005-01-07 21:51:51 +000014178 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14179 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014181 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014182 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014183 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014184 if (*str == NUL)
14185 match = FALSE; /* empty item at the end */
14186 else
14187 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014188 if (match)
14189 end = regmatch.startp[0];
14190 else
14191 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014192 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
14193 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014194 {
Bram Moolenaar4463f292005-09-25 22:20:24 +000014195 if (list_append_string(l, str, (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014196 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014197 }
14198 if (!match)
14199 break;
14200 /* Advance to just after the match. */
14201 if (regmatch.endp[0] > str)
14202 col = 0;
14203 else
14204 {
14205 /* Don't get stuck at the same match. */
14206#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014207 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014208#else
14209 col = 1;
14210#endif
14211 }
14212 str = regmatch.endp[0];
14213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214
Bram Moolenaar0d660222005-01-07 21:51:51 +000014215 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217
Bram Moolenaar0d660222005-01-07 21:51:51 +000014218 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219}
14220
14221#ifdef HAVE_STRFTIME
14222/*
14223 * "strftime({format}[, {time}])" function
14224 */
14225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014226f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014227 typval_T *argvars;
14228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229{
14230 char_u result_buf[256];
14231 struct tm *curtime;
14232 time_t seconds;
14233 char_u *p;
14234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014235 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014237 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014238 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 seconds = time(NULL);
14240 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014241 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 curtime = localtime(&seconds);
14243 /* MSVC returns NULL for an invalid value of seconds. */
14244 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014245 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246 else
14247 {
14248# ifdef FEAT_MBYTE
14249 vimconv_T conv;
14250 char_u *enc;
14251
14252 conv.vc_type = CONV_NONE;
14253 enc = enc_locale();
14254 convert_setup(&conv, p_enc, enc);
14255 if (conv.vc_type != CONV_NONE)
14256 p = string_convert(&conv, p, NULL);
14257# endif
14258 if (p != NULL)
14259 (void)strftime((char *)result_buf, sizeof(result_buf),
14260 (char *)p, curtime);
14261 else
14262 result_buf[0] = NUL;
14263
14264# ifdef FEAT_MBYTE
14265 if (conv.vc_type != CONV_NONE)
14266 vim_free(p);
14267 convert_setup(&conv, enc, p_enc);
14268 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014269 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270 else
14271# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014272 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273
14274# ifdef FEAT_MBYTE
14275 /* Release conversion descriptors */
14276 convert_setup(&conv, NULL, NULL);
14277 vim_free(enc);
14278# endif
14279 }
14280}
14281#endif
14282
14283/*
14284 * "stridx()" function
14285 */
14286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014287f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014288 typval_T *argvars;
14289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290{
14291 char_u buf[NUMBUFLEN];
14292 char_u *needle;
14293 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014294 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014296 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014298 needle = get_tv_string_chk(&argvars[1]);
14299 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014300 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014301 if (needle == NULL || haystack == NULL)
14302 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303
Bram Moolenaar33570922005-01-25 22:26:29 +000014304 if (argvars[2].v_type != VAR_UNKNOWN)
14305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014306 int error = FALSE;
14307
14308 start_idx = get_tv_number_chk(&argvars[2], &error);
14309 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014310 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014311 if (start_idx >= 0)
14312 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014313 }
14314
14315 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14316 if (pos != NULL)
14317 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318}
14319
14320/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014321 * "string()" function
14322 */
14323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014324f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014325 typval_T *argvars;
14326 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014327{
14328 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014329 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014330
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014331 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014332 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014333 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014334 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335}
14336
14337/*
14338 * "strlen()" function
14339 */
14340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014341f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014342 typval_T *argvars;
14343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014345 rettv->vval.v_number = (varnumber_T)(STRLEN(
14346 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347}
14348
14349/*
14350 * "strpart()" function
14351 */
14352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014353f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014354 typval_T *argvars;
14355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356{
14357 char_u *p;
14358 int n;
14359 int len;
14360 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014361 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364 slen = (int)STRLEN(p);
14365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366 n = get_tv_number_chk(&argvars[1], &error);
14367 if (error)
14368 len = 0;
14369 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014370 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 else
14372 len = slen - n; /* default len: all bytes that are available. */
14373
14374 /*
14375 * Only return the overlap between the specified part and the actual
14376 * string.
14377 */
14378 if (n < 0)
14379 {
14380 len += n;
14381 n = 0;
14382 }
14383 else if (n > slen)
14384 n = slen;
14385 if (len < 0)
14386 len = 0;
14387 else if (n + len > slen)
14388 len = slen - n;
14389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014390 rettv->v_type = VAR_STRING;
14391 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392}
14393
14394/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014395 * "strridx()" function
14396 */
14397 static void
14398f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014399 typval_T *argvars;
14400 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014401{
14402 char_u buf[NUMBUFLEN];
14403 char_u *needle;
14404 char_u *haystack;
14405 char_u *rest;
14406 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014407 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014409 needle = get_tv_string_chk(&argvars[1]);
14410 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014411 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014412
14413 rettv->vval.v_number = -1;
14414 if (needle == NULL || haystack == NULL)
14415 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014416 if (argvars[2].v_type != VAR_UNKNOWN)
14417 {
14418 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014419 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014420 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014421 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014422 }
14423 else
14424 end_idx = haystack_len;
14425
Bram Moolenaar0d660222005-01-07 21:51:51 +000014426 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014427 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014428 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014429 lastmatch = haystack + end_idx;
14430 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014431 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014432 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014433 for (rest = haystack; *rest != '\0'; ++rest)
14434 {
14435 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014436 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014437 break;
14438 lastmatch = rest;
14439 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014440 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014441
14442 if (lastmatch == NULL)
14443 rettv->vval.v_number = -1;
14444 else
14445 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14446}
14447
14448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449 * "strtrans()" function
14450 */
14451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014452f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014453 typval_T *argvars;
14454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456 rettv->v_type = VAR_STRING;
14457 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458}
14459
14460/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014461 * "submatch()" function
14462 */
14463 static void
14464f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014465 typval_T *argvars;
14466 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014467{
14468 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014469 rettv->vval.v_string =
14470 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014471}
14472
14473/*
14474 * "substitute()" function
14475 */
14476 static void
14477f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014478 typval_T *argvars;
14479 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014480{
14481 char_u patbuf[NUMBUFLEN];
14482 char_u subbuf[NUMBUFLEN];
14483 char_u flagsbuf[NUMBUFLEN];
14484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014485 char_u *str = get_tv_string_chk(&argvars[0]);
14486 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14487 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14488 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14489
Bram Moolenaar0d660222005-01-07 21:51:51 +000014490 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014491 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14492 rettv->vval.v_string = NULL;
14493 else
14494 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014495}
14496
14497/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014498 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 */
14500/*ARGSUSED*/
14501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014502f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014503 typval_T *argvars;
14504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505{
14506 int id = 0;
14507#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014508 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 long col;
14510 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014511 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014513 lnum = get_tv_lnum(argvars); /* -1 on type error */
14514 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14515 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014517 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014518 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014519 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520#endif
14521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014522 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523}
14524
14525/*
14526 * "synIDattr(id, what [, mode])" function
14527 */
14528/*ARGSUSED*/
14529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014530f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014531 typval_T *argvars;
14532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533{
14534 char_u *p = NULL;
14535#ifdef FEAT_SYN_HL
14536 int id;
14537 char_u *what;
14538 char_u *mode;
14539 char_u modebuf[NUMBUFLEN];
14540 int modec;
14541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014542 id = get_tv_number(&argvars[0]);
14543 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014544 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014546 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 modec = TOLOWER_ASC(mode[0]);
14548 if (modec != 't' && modec != 'c'
14549#ifdef FEAT_GUI
14550 && modec != 'g'
14551#endif
14552 )
14553 modec = 0; /* replace invalid with current */
14554 }
14555 else
14556 {
14557#ifdef FEAT_GUI
14558 if (gui.in_use)
14559 modec = 'g';
14560 else
14561#endif
14562 if (t_colors > 1)
14563 modec = 'c';
14564 else
14565 modec = 't';
14566 }
14567
14568
14569 switch (TOLOWER_ASC(what[0]))
14570 {
14571 case 'b':
14572 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14573 p = highlight_color(id, what, modec);
14574 else /* bold */
14575 p = highlight_has_attr(id, HL_BOLD, modec);
14576 break;
14577
14578 case 'f': /* fg[#] */
14579 p = highlight_color(id, what, modec);
14580 break;
14581
14582 case 'i':
14583 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14584 p = highlight_has_attr(id, HL_INVERSE, modec);
14585 else /* italic */
14586 p = highlight_has_attr(id, HL_ITALIC, modec);
14587 break;
14588
14589 case 'n': /* name */
14590 p = get_highlight_name(NULL, id - 1);
14591 break;
14592
14593 case 'r': /* reverse */
14594 p = highlight_has_attr(id, HL_INVERSE, modec);
14595 break;
14596
14597 case 's': /* standout */
14598 p = highlight_has_attr(id, HL_STANDOUT, modec);
14599 break;
14600
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014601 case 'u':
14602 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14603 /* underline */
14604 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14605 else
14606 /* undercurl */
14607 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608 break;
14609 }
14610
14611 if (p != NULL)
14612 p = vim_strsave(p);
14613#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014614 rettv->v_type = VAR_STRING;
14615 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616}
14617
14618/*
14619 * "synIDtrans(id)" function
14620 */
14621/*ARGSUSED*/
14622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014623f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014624 typval_T *argvars;
14625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626{
14627 int id;
14628
14629#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631
14632 if (id > 0)
14633 id = syn_get_final_id(id);
14634 else
14635#endif
14636 id = 0;
14637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014638 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639}
14640
14641/*
14642 * "system()" function
14643 */
14644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014645f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014646 typval_T *argvars;
14647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014649 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014651 char_u *infile = NULL;
14652 char_u buf[NUMBUFLEN];
14653 int err = FALSE;
14654 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014656 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014657 {
14658 /*
14659 * Write the string to a temp file, to be used for input of the shell
14660 * command.
14661 */
14662 if ((infile = vim_tempname('i')) == NULL)
14663 {
14664 EMSG(_(e_notmp));
14665 return;
14666 }
14667
14668 fd = mch_fopen((char *)infile, WRITEBIN);
14669 if (fd == NULL)
14670 {
14671 EMSG2(_(e_notopen), infile);
14672 goto done;
14673 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014674 p = get_tv_string_buf_chk(&argvars[1], buf);
14675 if (p == NULL)
14676 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014677 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14678 err = TRUE;
14679 if (fclose(fd) != 0)
14680 err = TRUE;
14681 if (err)
14682 {
14683 EMSG(_("E677: Error writing temp file"));
14684 goto done;
14685 }
14686 }
14687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014688 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014689
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690#ifdef USE_CR
14691 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014692 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693 {
14694 char_u *s;
14695
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014696 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697 {
14698 if (*s == CAR)
14699 *s = NL;
14700 }
14701 }
14702#else
14703# ifdef USE_CRNL
14704 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014705 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706 {
14707 char_u *s, *d;
14708
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014709 d = res;
14710 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711 {
14712 if (s[0] == CAR && s[1] == NL)
14713 ++s;
14714 *d++ = *s;
14715 }
14716 *d = NUL;
14717 }
14718# endif
14719#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014720
14721done:
14722 if (infile != NULL)
14723 {
14724 mch_remove(infile);
14725 vim_free(infile);
14726 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014727 rettv->v_type = VAR_STRING;
14728 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729}
14730
14731/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014732 * "tagfiles()" function
14733 */
14734/*ARGSUSED*/
14735 static void
14736f_tagfiles(argvars, rettv)
14737 typval_T *argvars;
14738 typval_T *rettv;
14739{
14740 char_u fname[MAXPATHL + 1];
14741 list_T *l;
14742
14743 l = list_alloc();
14744 if (l == NULL)
14745 {
14746 rettv->vval.v_number = 0;
14747 return;
14748 }
14749 rettv->vval.v_list = l;
14750 rettv->v_type = VAR_LIST;
14751 ++l->lv_refcount;
14752
14753 get_tagfname(TRUE, NULL);
14754 for (;;)
14755 if (get_tagfname(FALSE, fname) == FAIL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014756 || list_append_string(l, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014757 break;
14758}
14759
14760/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014761 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014762 */
14763 static void
14764f_taglist(argvars, rettv)
14765 typval_T *argvars;
14766 typval_T *rettv;
14767{
14768 char_u *tag_pattern;
14769 list_T *l;
14770
14771 tag_pattern = get_tv_string(&argvars[0]);
14772
14773 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014774 if (*tag_pattern == NUL)
14775 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014776
14777 l = list_alloc();
14778 if (l != NULL)
14779 {
14780 if (get_tags(l, tag_pattern) != FAIL)
14781 {
14782 rettv->vval.v_list = l;
14783 rettv->v_type = VAR_LIST;
14784 ++l->lv_refcount;
14785 }
14786 else
14787 list_free(l);
14788 }
14789}
14790
14791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792 * "tempname()" function
14793 */
14794/*ARGSUSED*/
14795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014796f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014797 typval_T *argvars;
14798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014799{
14800 static int x = 'A';
14801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014802 rettv->v_type = VAR_STRING;
14803 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804
14805 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14806 * names. Skip 'I' and 'O', they are used for shell redirection. */
14807 do
14808 {
14809 if (x == 'Z')
14810 x = '0';
14811 else if (x == '9')
14812 x = 'A';
14813 else
14814 {
14815#ifdef EBCDIC
14816 if (x == 'I')
14817 x = 'J';
14818 else if (x == 'R')
14819 x = 'S';
14820 else
14821#endif
14822 ++x;
14823 }
14824 } while (x == 'I' || x == 'O');
14825}
14826
14827/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014828 * "test(list)" function: Just checking the walls...
14829 */
14830/*ARGSUSED*/
14831 static void
14832f_test(argvars, rettv)
14833 typval_T *argvars;
14834 typval_T *rettv;
14835{
14836 /* Used for unit testing. Change the code below to your liking. */
14837#if 0
14838 listitem_T *li;
14839 list_T *l;
14840 char_u *bad, *good;
14841
14842 if (argvars[0].v_type != VAR_LIST)
14843 return;
14844 l = argvars[0].vval.v_list;
14845 if (l == NULL)
14846 return;
14847 li = l->lv_first;
14848 if (li == NULL)
14849 return;
14850 bad = get_tv_string(&li->li_tv);
14851 li = li->li_next;
14852 if (li == NULL)
14853 return;
14854 good = get_tv_string(&li->li_tv);
14855 rettv->vval.v_number = test_edit_score(bad, good);
14856#endif
14857}
14858
14859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860 * "tolower(string)" function
14861 */
14862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014863f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014864 typval_T *argvars;
14865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866{
14867 char_u *p;
14868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014869 p = vim_strsave(get_tv_string(&argvars[0]));
14870 rettv->v_type = VAR_STRING;
14871 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872
14873 if (p != NULL)
14874 while (*p != NUL)
14875 {
14876#ifdef FEAT_MBYTE
14877 int l;
14878
14879 if (enc_utf8)
14880 {
14881 int c, lc;
14882
14883 c = utf_ptr2char(p);
14884 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014885 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886 /* TODO: reallocate string when byte count changes. */
14887 if (utf_char2len(lc) == l)
14888 utf_char2bytes(lc, p);
14889 p += l;
14890 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014891 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892 p += l; /* skip multi-byte character */
14893 else
14894#endif
14895 {
14896 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14897 ++p;
14898 }
14899 }
14900}
14901
14902/*
14903 * "toupper(string)" function
14904 */
14905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014906f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014907 typval_T *argvars;
14908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014910 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014911 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912}
14913
14914/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014915 * "tr(string, fromstr, tostr)" function
14916 */
14917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014918f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014919 typval_T *argvars;
14920 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014921{
14922 char_u *instr;
14923 char_u *fromstr;
14924 char_u *tostr;
14925 char_u *p;
14926#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014927 int inlen;
14928 int fromlen;
14929 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014930 int idx;
14931 char_u *cpstr;
14932 int cplen;
14933 int first = TRUE;
14934#endif
14935 char_u buf[NUMBUFLEN];
14936 char_u buf2[NUMBUFLEN];
14937 garray_T ga;
14938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014939 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014940 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14941 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014942
14943 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014944 rettv->v_type = VAR_STRING;
14945 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014946 if (fromstr == NULL || tostr == NULL)
14947 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014948 ga_init2(&ga, (int)sizeof(char), 80);
14949
14950#ifdef FEAT_MBYTE
14951 if (!has_mbyte)
14952#endif
14953 /* not multi-byte: fromstr and tostr must be the same length */
14954 if (STRLEN(fromstr) != STRLEN(tostr))
14955 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014956#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014957error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014958#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014959 EMSG2(_(e_invarg2), fromstr);
14960 ga_clear(&ga);
14961 return;
14962 }
14963
14964 /* fromstr and tostr have to contain the same number of chars */
14965 while (*instr != NUL)
14966 {
14967#ifdef FEAT_MBYTE
14968 if (has_mbyte)
14969 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014970 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014971 cpstr = instr;
14972 cplen = inlen;
14973 idx = 0;
14974 for (p = fromstr; *p != NUL; p += fromlen)
14975 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014976 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014977 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14978 {
14979 for (p = tostr; *p != NUL; p += tolen)
14980 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014981 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014982 if (idx-- == 0)
14983 {
14984 cplen = tolen;
14985 cpstr = p;
14986 break;
14987 }
14988 }
14989 if (*p == NUL) /* tostr is shorter than fromstr */
14990 goto error;
14991 break;
14992 }
14993 ++idx;
14994 }
14995
14996 if (first && cpstr == instr)
14997 {
14998 /* Check that fromstr and tostr have the same number of
14999 * (multi-byte) characters. Done only once when a character
15000 * of instr doesn't appear in fromstr. */
15001 first = FALSE;
15002 for (p = tostr; *p != NUL; p += tolen)
15003 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015004 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015005 --idx;
15006 }
15007 if (idx != 0)
15008 goto error;
15009 }
15010
15011 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015012 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015013 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015014
15015 instr += inlen;
15016 }
15017 else
15018#endif
15019 {
15020 /* When not using multi-byte chars we can do it faster. */
15021 p = vim_strchr(fromstr, *instr);
15022 if (p != NULL)
15023 ga_append(&ga, tostr[p - fromstr]);
15024 else
15025 ga_append(&ga, *instr);
15026 ++instr;
15027 }
15028 }
15029
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015030 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015031}
15032
15033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034 * "type(expr)" function
15035 */
15036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015037f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015038 typval_T *argvars;
15039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015041 int n;
15042
15043 switch (argvars[0].v_type)
15044 {
15045 case VAR_NUMBER: n = 0; break;
15046 case VAR_STRING: n = 1; break;
15047 case VAR_FUNC: n = 2; break;
15048 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015049 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015050 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15051 }
15052 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053}
15054
15055/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015056 * "values(dict)" function
15057 */
15058 static void
15059f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015060 typval_T *argvars;
15061 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015062{
15063 dict_list(argvars, rettv, 1);
15064}
15065
15066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015067 * "virtcol(string)" function
15068 */
15069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015070f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015071 typval_T *argvars;
15072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073{
15074 colnr_T vcol = 0;
15075 pos_T *fp;
15076
15077 fp = var2fpos(&argvars[0], FALSE);
15078 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
15079 {
15080 getvvcol(curwin, fp, NULL, NULL, &vcol);
15081 ++vcol;
15082 }
15083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015084 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015085}
15086
15087/*
15088 * "visualmode()" function
15089 */
15090/*ARGSUSED*/
15091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015092f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015093 typval_T *argvars;
15094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095{
15096#ifdef FEAT_VISUAL
15097 char_u str[2];
15098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015099 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100 str[0] = curbuf->b_visual_mode_eval;
15101 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015102 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103
15104 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015105 if ((argvars[0].v_type == VAR_NUMBER
15106 && argvars[0].vval.v_number != 0)
15107 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015108 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 curbuf->b_visual_mode_eval = NUL;
15110#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015111 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112#endif
15113}
15114
15115/*
15116 * "winbufnr(nr)" function
15117 */
15118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015119f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015120 typval_T *argvars;
15121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122{
15123 win_T *wp;
15124
15125 wp = find_win_by_nr(&argvars[0]);
15126 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015127 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015129 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130}
15131
15132/*
15133 * "wincol()" function
15134 */
15135/*ARGSUSED*/
15136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015137f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015138 typval_T *argvars;
15139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140{
15141 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015142 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143}
15144
15145/*
15146 * "winheight(nr)" function
15147 */
15148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015149f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015150 typval_T *argvars;
15151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152{
15153 win_T *wp;
15154
15155 wp = find_win_by_nr(&argvars[0]);
15156 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015157 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015159 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160}
15161
15162/*
15163 * "winline()" function
15164 */
15165/*ARGSUSED*/
15166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015167f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015168 typval_T *argvars;
15169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170{
15171 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015172 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173}
15174
15175/*
15176 * "winnr()" function
15177 */
15178/* ARGSUSED */
15179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015180f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015181 typval_T *argvars;
15182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183{
15184 int nr = 1;
15185#ifdef FEAT_WINDOWS
15186 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015187 win_T *twin = curwin;
15188 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015190 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015192 arg = get_tv_string_chk(&argvars[0]);
15193 if (arg == NULL)
15194 nr = 0; /* type error; errmsg already given */
15195 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015196 twin = lastwin;
15197 else if (STRCMP(arg, "#") == 0)
15198 {
15199 twin = prevwin;
15200 if (prevwin == NULL)
15201 nr = 0;
15202 }
15203 else
15204 {
15205 EMSG2(_(e_invexpr2), arg);
15206 nr = 0;
15207 }
15208 }
15209
15210 if (nr > 0)
15211 for (wp = firstwin; wp != twin; wp = wp->w_next)
15212 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015213#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015214 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215}
15216
15217/*
15218 * "winrestcmd()" function
15219 */
15220/* ARGSUSED */
15221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015222f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015223 typval_T *argvars;
15224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225{
15226#ifdef FEAT_WINDOWS
15227 win_T *wp;
15228 int winnr = 1;
15229 garray_T ga;
15230 char_u buf[50];
15231
15232 ga_init2(&ga, (int)sizeof(char), 70);
15233 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15234 {
15235 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15236 ga_concat(&ga, buf);
15237# ifdef FEAT_VERTSPLIT
15238 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15239 ga_concat(&ga, buf);
15240# endif
15241 ++winnr;
15242 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015243 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015245 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015246#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015247 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015248#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015249 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015250}
15251
15252/*
15253 * "winwidth(nr)" function
15254 */
15255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015256f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015257 typval_T *argvars;
15258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259{
15260 win_T *wp;
15261
15262 wp = find_win_by_nr(&argvars[0]);
15263 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015264 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265 else
15266#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015267 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015269 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270#endif
15271}
15272
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015274 * "writefile()" function
15275 */
15276 static void
15277f_writefile(argvars, rettv)
15278 typval_T *argvars;
15279 typval_T *rettv;
15280{
15281 int binary = FALSE;
15282 char_u *fname;
15283 FILE *fd;
15284 listitem_T *li;
15285 char_u *s;
15286 int ret = 0;
15287 int c;
15288
15289 if (argvars[0].v_type != VAR_LIST)
15290 {
15291 EMSG2(_(e_listarg), "writefile()");
15292 return;
15293 }
15294 if (argvars[0].vval.v_list == NULL)
15295 return;
15296
15297 if (argvars[2].v_type != VAR_UNKNOWN
15298 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15299 binary = TRUE;
15300
15301 /* Always open the file in binary mode, library functions have a mind of
15302 * their own about CR-LF conversion. */
15303 fname = get_tv_string(&argvars[1]);
15304 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15305 {
15306 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15307 ret = -1;
15308 }
15309 else
15310 {
15311 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15312 li = li->li_next)
15313 {
15314 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15315 {
15316 if (*s == '\n')
15317 c = putc(NUL, fd);
15318 else
15319 c = putc(*s, fd);
15320 if (c == EOF)
15321 {
15322 ret = -1;
15323 break;
15324 }
15325 }
15326 if (!binary || li->li_next != NULL)
15327 if (putc('\n', fd) == EOF)
15328 {
15329 ret = -1;
15330 break;
15331 }
15332 if (ret < 0)
15333 {
15334 EMSG(_(e_write));
15335 break;
15336 }
15337 }
15338 fclose(fd);
15339 }
15340
15341 rettv->vval.v_number = ret;
15342}
15343
15344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 * Translate a String variable into a position.
15346 */
15347 static pos_T *
15348var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015349 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 int lnum; /* TRUE when $ is last line */
15351{
15352 char_u *name;
15353 static pos_T pos;
15354 pos_T *pp;
15355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015356 name = get_tv_string_chk(varp);
15357 if (name == NULL)
15358 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359 if (name[0] == '.') /* cursor */
15360 return &curwin->w_cursor;
15361 if (name[0] == '\'') /* mark */
15362 {
15363 pp = getmark(name[1], FALSE);
15364 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15365 return NULL;
15366 return pp;
15367 }
15368 if (name[0] == '$') /* last column or line */
15369 {
15370 if (lnum)
15371 {
15372 pos.lnum = curbuf->b_ml.ml_line_count;
15373 pos.col = 0;
15374 }
15375 else
15376 {
15377 pos.lnum = curwin->w_cursor.lnum;
15378 pos.col = (colnr_T)STRLEN(ml_get_curline());
15379 }
15380 return &pos;
15381 }
15382 return NULL;
15383}
15384
15385/*
15386 * Get the length of an environment variable name.
15387 * Advance "arg" to the first character after the name.
15388 * Return 0 for error.
15389 */
15390 static int
15391get_env_len(arg)
15392 char_u **arg;
15393{
15394 char_u *p;
15395 int len;
15396
15397 for (p = *arg; vim_isIDc(*p); ++p)
15398 ;
15399 if (p == *arg) /* no name found */
15400 return 0;
15401
15402 len = (int)(p - *arg);
15403 *arg = p;
15404 return len;
15405}
15406
15407/*
15408 * Get the length of the name of a function or internal variable.
15409 * "arg" is advanced to the first non-white character after the name.
15410 * Return 0 if something is wrong.
15411 */
15412 static int
15413get_id_len(arg)
15414 char_u **arg;
15415{
15416 char_u *p;
15417 int len;
15418
15419 /* Find the end of the name. */
15420 for (p = *arg; eval_isnamec(*p); ++p)
15421 ;
15422 if (p == *arg) /* no name found */
15423 return 0;
15424
15425 len = (int)(p - *arg);
15426 *arg = skipwhite(p);
15427
15428 return len;
15429}
15430
15431/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015432 * Get the length of the name of a variable or function.
15433 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015435 * Return -1 if curly braces expansion failed.
15436 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437 * If the name contains 'magic' {}'s, expand them and return the
15438 * expanded name in an allocated string via 'alias' - caller must free.
15439 */
15440 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015441get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 char_u **arg;
15443 char_u **alias;
15444 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015445 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446{
15447 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 char_u *p;
15449 char_u *expr_start;
15450 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451
15452 *alias = NULL; /* default to no alias */
15453
15454 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15455 && (*arg)[2] == (int)KE_SNR)
15456 {
15457 /* hard coded <SNR>, already translated */
15458 *arg += 3;
15459 return get_id_len(arg) + 3;
15460 }
15461 len = eval_fname_script(*arg);
15462 if (len > 0)
15463 {
15464 /* literal "<SID>", "s:" or "<SNR>" */
15465 *arg += len;
15466 }
15467
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015469 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015471 p = find_name_end(*arg, &expr_start, &expr_end,
15472 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473 if (expr_start != NULL)
15474 {
15475 char_u *temp_string;
15476
15477 if (!evaluate)
15478 {
15479 len += (int)(p - *arg);
15480 *arg = skipwhite(p);
15481 return len;
15482 }
15483
15484 /*
15485 * Include any <SID> etc in the expanded string:
15486 * Thus the -len here.
15487 */
15488 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15489 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015490 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491 *alias = temp_string;
15492 *arg = skipwhite(p);
15493 return (int)STRLEN(temp_string);
15494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495
15496 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015497 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498 EMSG2(_(e_invexpr2), *arg);
15499
15500 return len;
15501}
15502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015503/*
15504 * Find the end of a variable or function name, taking care of magic braces.
15505 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15506 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015507 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015508 * Return a pointer to just after the name. Equal to "arg" if there is no
15509 * valid name.
15510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015512find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 char_u *arg;
15514 char_u **expr_start;
15515 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015516 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015518 int mb_nest = 0;
15519 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015520 char_u *p;
15521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015522 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015524 *expr_start = NULL;
15525 *expr_end = NULL;
15526 }
15527
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015528 /* Quick check for valid starting character. */
15529 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15530 return arg;
15531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015532 for (p = arg; *p != NUL
15533 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015534 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015535 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015536 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015537 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015538 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015539 if (*p == '\'')
15540 {
15541 /* skip over 'string' to avoid counting [ and ] inside it. */
15542 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15543 ;
15544 if (*p == NUL)
15545 break;
15546 }
15547 else if (*p == '"')
15548 {
15549 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15550 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15551 if (*p == '\\' && p[1] != NUL)
15552 ++p;
15553 if (*p == NUL)
15554 break;
15555 }
15556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015557 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015559 if (*p == '[')
15560 ++br_nest;
15561 else if (*p == ']')
15562 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015565 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015567 if (*p == '{')
15568 {
15569 mb_nest++;
15570 if (expr_start != NULL && *expr_start == NULL)
15571 *expr_start = p;
15572 }
15573 else if (*p == '}')
15574 {
15575 mb_nest--;
15576 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15577 *expr_end = p;
15578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580 }
15581
15582 return p;
15583}
15584
15585/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015586 * Expands out the 'magic' {}'s in a variable/function name.
15587 * Note that this can call itself recursively, to deal with
15588 * constructs like foo{bar}{baz}{bam}
15589 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15590 * "in_start" ^
15591 * "expr_start" ^
15592 * "expr_end" ^
15593 * "in_end" ^
15594 *
15595 * Returns a new allocated string, which the caller must free.
15596 * Returns NULL for failure.
15597 */
15598 static char_u *
15599make_expanded_name(in_start, expr_start, expr_end, in_end)
15600 char_u *in_start;
15601 char_u *expr_start;
15602 char_u *expr_end;
15603 char_u *in_end;
15604{
15605 char_u c1;
15606 char_u *retval = NULL;
15607 char_u *temp_result;
15608 char_u *nextcmd = NULL;
15609
15610 if (expr_end == NULL || in_end == NULL)
15611 return NULL;
15612 *expr_start = NUL;
15613 *expr_end = NUL;
15614 c1 = *in_end;
15615 *in_end = NUL;
15616
15617 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15618 if (temp_result != NULL && nextcmd == NULL)
15619 {
15620 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15621 + (in_end - expr_end) + 1));
15622 if (retval != NULL)
15623 {
15624 STRCPY(retval, in_start);
15625 STRCAT(retval, temp_result);
15626 STRCAT(retval, expr_end + 1);
15627 }
15628 }
15629 vim_free(temp_result);
15630
15631 *in_end = c1; /* put char back for error messages */
15632 *expr_start = '{';
15633 *expr_end = '}';
15634
15635 if (retval != NULL)
15636 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015637 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015638 if (expr_start != NULL)
15639 {
15640 /* Further expansion! */
15641 temp_result = make_expanded_name(retval, expr_start,
15642 expr_end, temp_result);
15643 vim_free(retval);
15644 retval = temp_result;
15645 }
15646 }
15647
15648 return retval;
15649}
15650
15651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015653 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654 */
15655 static int
15656eval_isnamec(c)
15657 int c;
15658{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015659 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15660}
15661
15662/*
15663 * Return TRUE if character "c" can be used as the first character in a
15664 * variable or function name (excluding '{' and '}').
15665 */
15666 static int
15667eval_isnamec1(c)
15668 int c;
15669{
15670 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671}
15672
15673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674 * Set number v: variable to "val".
15675 */
15676 void
15677set_vim_var_nr(idx, val)
15678 int idx;
15679 long val;
15680{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015681 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682}
15683
15684/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015685 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015686 */
15687 long
15688get_vim_var_nr(idx)
15689 int idx;
15690{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015691 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692}
15693
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015694#if defined(FEAT_AUTOCMD) || defined(PROTO)
15695/*
15696 * Get string v: variable value. Uses a static buffer, can only be used once.
15697 */
15698 char_u *
15699get_vim_var_str(idx)
15700 int idx;
15701{
15702 return get_tv_string(&vimvars[idx].vv_tv);
15703}
15704#endif
15705
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706/*
15707 * Set v:count, v:count1 and v:prevcount.
15708 */
15709 void
15710set_vcount(count, count1)
15711 long count;
15712 long count1;
15713{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015714 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15715 vimvars[VV_COUNT].vv_nr = count;
15716 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717}
15718
15719/*
15720 * Set string v: variable to a copy of "val".
15721 */
15722 void
15723set_vim_var_string(idx, val, len)
15724 int idx;
15725 char_u *val;
15726 int len; /* length of "val" to use or -1 (whole string) */
15727{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015728 /* Need to do this (at least) once, since we can't initialize a union.
15729 * Will always be invoked when "v:progname" is set. */
15730 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15731
Bram Moolenaare9a41262005-01-15 22:18:47 +000015732 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015734 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015736 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015738 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739}
15740
15741/*
15742 * Set v:register if needed.
15743 */
15744 void
15745set_reg_var(c)
15746 int c;
15747{
15748 char_u regname;
15749
15750 if (c == 0 || c == ' ')
15751 regname = '"';
15752 else
15753 regname = c;
15754 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015755 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756 set_vim_var_string(VV_REG, &regname, 1);
15757}
15758
15759/*
15760 * Get or set v:exception. If "oldval" == NULL, return the current value.
15761 * Otherwise, restore the value to "oldval" and return NULL.
15762 * Must always be called in pairs to save and restore v:exception! Does not
15763 * take care of memory allocations.
15764 */
15765 char_u *
15766v_exception(oldval)
15767 char_u *oldval;
15768{
15769 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015770 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771
Bram Moolenaare9a41262005-01-15 22:18:47 +000015772 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773 return NULL;
15774}
15775
15776/*
15777 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15778 * Otherwise, restore the value to "oldval" and return NULL.
15779 * Must always be called in pairs to save and restore v:throwpoint! Does not
15780 * take care of memory allocations.
15781 */
15782 char_u *
15783v_throwpoint(oldval)
15784 char_u *oldval;
15785{
15786 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015787 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788
Bram Moolenaare9a41262005-01-15 22:18:47 +000015789 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790 return NULL;
15791}
15792
15793#if defined(FEAT_AUTOCMD) || defined(PROTO)
15794/*
15795 * Set v:cmdarg.
15796 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15797 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15798 * Must always be called in pairs!
15799 */
15800 char_u *
15801set_cmdarg(eap, oldarg)
15802 exarg_T *eap;
15803 char_u *oldarg;
15804{
15805 char_u *oldval;
15806 char_u *newval;
15807 unsigned len;
15808
Bram Moolenaare9a41262005-01-15 22:18:47 +000015809 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015810 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015811 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015812 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015813 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015814 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815 }
15816
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015817 if (eap->force_bin == FORCE_BIN)
15818 len = 6;
15819 else if (eap->force_bin == FORCE_NOBIN)
15820 len = 8;
15821 else
15822 len = 0;
15823 if (eap->force_ff != 0)
15824 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15825# ifdef FEAT_MBYTE
15826 if (eap->force_enc != 0)
15827 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000015828 if (eap->bad_char != 0)
15829 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015830# endif
15831
15832 newval = alloc(len + 1);
15833 if (newval == NULL)
15834 return NULL;
15835
15836 if (eap->force_bin == FORCE_BIN)
15837 sprintf((char *)newval, " ++bin");
15838 else if (eap->force_bin == FORCE_NOBIN)
15839 sprintf((char *)newval, " ++nobin");
15840 else
15841 *newval = NUL;
15842 if (eap->force_ff != 0)
15843 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15844 eap->cmd + eap->force_ff);
15845# ifdef FEAT_MBYTE
15846 if (eap->force_enc != 0)
15847 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15848 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000015849 if (eap->bad_char != 0)
15850 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
15851 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015852# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015853 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015854 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855}
15856#endif
15857
15858/*
15859 * Get the value of internal variable "name".
15860 * Return OK or FAIL.
15861 */
15862 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015863get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864 char_u *name;
15865 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015866 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015867 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868{
15869 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015870 typval_T *tv = NULL;
15871 typval_T atv;
15872 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874
15875 /* truncate the name, so that we can use strcmp() */
15876 cc = name[len];
15877 name[len] = NUL;
15878
15879 /*
15880 * Check for "b:changedtick".
15881 */
15882 if (STRCMP(name, "b:changedtick") == 0)
15883 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015884 atv.v_type = VAR_NUMBER;
15885 atv.vval.v_number = curbuf->b_changedtick;
15886 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887 }
15888
15889 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890 * Check for user-defined variables.
15891 */
15892 else
15893 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015894 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015896 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897 }
15898
Bram Moolenaare9a41262005-01-15 22:18:47 +000015899 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015901 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015902 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903 ret = FAIL;
15904 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015905 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015906 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907
15908 name[len] = cc;
15909
15910 return ret;
15911}
15912
15913/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015914 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15915 * Also handle function call with Funcref variable: func(expr)
15916 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15917 */
15918 static int
15919handle_subscript(arg, rettv, evaluate, verbose)
15920 char_u **arg;
15921 typval_T *rettv;
15922 int evaluate; /* do more than finding the end */
15923 int verbose; /* give error messages */
15924{
15925 int ret = OK;
15926 dict_T *selfdict = NULL;
15927 char_u *s;
15928 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015929 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015930
15931 while (ret == OK
15932 && (**arg == '['
15933 || (**arg == '.' && rettv->v_type == VAR_DICT)
15934 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15935 && !vim_iswhite(*(*arg - 1)))
15936 {
15937 if (**arg == '(')
15938 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015939 /* need to copy the funcref so that we can clear rettv */
15940 functv = *rettv;
15941 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015942
15943 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015944 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015945 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015946 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15947 &len, evaluate, selfdict);
15948
15949 /* Clear the funcref afterwards, so that deleting it while
15950 * evaluating the arguments is possible (see test55). */
15951 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015952
15953 /* Stop the expression evaluation when immediately aborting on
15954 * error, or when an interrupt occurred or an exception was thrown
15955 * but not caught. */
15956 if (aborting())
15957 {
15958 if (ret == OK)
15959 clear_tv(rettv);
15960 ret = FAIL;
15961 }
15962 dict_unref(selfdict);
15963 selfdict = NULL;
15964 }
15965 else /* **arg == '[' || **arg == '.' */
15966 {
15967 dict_unref(selfdict);
15968 if (rettv->v_type == VAR_DICT)
15969 {
15970 selfdict = rettv->vval.v_dict;
15971 if (selfdict != NULL)
15972 ++selfdict->dv_refcount;
15973 }
15974 else
15975 selfdict = NULL;
15976 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15977 {
15978 clear_tv(rettv);
15979 ret = FAIL;
15980 }
15981 }
15982 }
15983 dict_unref(selfdict);
15984 return ret;
15985}
15986
15987/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015988 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15989 * value).
15990 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015991 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015992alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015993{
Bram Moolenaar33570922005-01-25 22:26:29 +000015994 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015995}
15996
15997/*
15998 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999 * The string "s" must have been allocated, it is consumed.
16000 * Return NULL for out of memory, the variable otherwise.
16001 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016002 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016003alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004 char_u *s;
16005{
Bram Moolenaar33570922005-01-25 22:26:29 +000016006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016008 rettv = alloc_tv();
16009 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016011 rettv->v_type = VAR_STRING;
16012 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 }
16014 else
16015 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016016 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017}
16018
16019/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016020 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016022 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016023free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016024 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025{
16026 if (varp != NULL)
16027 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016028 switch (varp->v_type)
16029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016030 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016031 func_unref(varp->vval.v_string);
16032 /*FALLTHROUGH*/
16033 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016034 vim_free(varp->vval.v_string);
16035 break;
16036 case VAR_LIST:
16037 list_unref(varp->vval.v_list);
16038 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016039 case VAR_DICT:
16040 dict_unref(varp->vval.v_dict);
16041 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016042 case VAR_NUMBER:
16043 case VAR_UNKNOWN:
16044 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016045 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016046 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016047 break;
16048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 vim_free(varp);
16050 }
16051}
16052
16053/*
16054 * Free the memory for a variable value and set the value to NULL or 0.
16055 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016056 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016057clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016058 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059{
16060 if (varp != NULL)
16061 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016062 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016064 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016065 func_unref(varp->vval.v_string);
16066 /*FALLTHROUGH*/
16067 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016068 vim_free(varp->vval.v_string);
16069 varp->vval.v_string = NULL;
16070 break;
16071 case VAR_LIST:
16072 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016073 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016074 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016075 case VAR_DICT:
16076 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016077 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016078 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016079 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016080 varp->vval.v_number = 0;
16081 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016082 case VAR_UNKNOWN:
16083 break;
16084 default:
16085 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016087 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 }
16089}
16090
16091/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016092 * Set the value of a variable to NULL without freeing items.
16093 */
16094 static void
16095init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016096 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016097{
16098 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016099 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016100}
16101
16102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103 * Get the number value of a variable.
16104 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016105 * For incompatible types, return 0.
16106 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16107 * caller of incompatible types: it sets *denote to TRUE if "denote"
16108 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 */
16110 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016111get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016112 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016114 int error = FALSE;
16115
16116 return get_tv_number_chk(varp, &error); /* return 0L on error */
16117}
16118
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016119 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016120get_tv_number_chk(varp, denote)
16121 typval_T *varp;
16122 int *denote;
16123{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016124 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016126 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016127 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016128 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016129 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016130 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016131 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016132 break;
16133 case VAR_STRING:
16134 if (varp->vval.v_string != NULL)
16135 vim_str2nr(varp->vval.v_string, NULL, NULL,
16136 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016137 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016138 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016139 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016140 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016141 case VAR_DICT:
16142 EMSG(_("E728: Using a Dictionary as a number"));
16143 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016144 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016145 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016146 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016148 if (denote == NULL) /* useful for values that must be unsigned */
16149 n = -1;
16150 else
16151 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016152 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153}
16154
16155/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016156 * Get the lnum from the first argument.
16157 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016158 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159 */
16160 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016161get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016162 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163{
Bram Moolenaar33570922005-01-25 22:26:29 +000016164 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165 linenr_T lnum;
16166
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016167 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 if (lnum == 0) /* no valid number, try using line() */
16169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016170 rettv.v_type = VAR_NUMBER;
16171 f_line(argvars, &rettv);
16172 lnum = rettv.vval.v_number;
16173 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 }
16175 return lnum;
16176}
16177
16178/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016179 * Get the lnum from the first argument.
16180 * Also accepts "$", then "buf" is used.
16181 * Returns 0 on error.
16182 */
16183 static linenr_T
16184get_tv_lnum_buf(argvars, buf)
16185 typval_T *argvars;
16186 buf_T *buf;
16187{
16188 if (argvars[0].v_type == VAR_STRING
16189 && argvars[0].vval.v_string != NULL
16190 && argvars[0].vval.v_string[0] == '$'
16191 && buf != NULL)
16192 return buf->b_ml.ml_line_count;
16193 return get_tv_number_chk(&argvars[0], NULL);
16194}
16195
16196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197 * Get the string value of a variable.
16198 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016199 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16200 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 * If the String variable has never been set, return an empty string.
16202 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016203 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16204 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205 */
16206 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016207get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016208 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016209{
16210 static char_u mybuf[NUMBUFLEN];
16211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016212 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213}
16214
16215 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016216get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016217 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016218 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016220 char_u *res = get_tv_string_buf_chk(varp, buf);
16221
16222 return res != NULL ? res : (char_u *)"";
16223}
16224
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016225 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016226get_tv_string_chk(varp)
16227 typval_T *varp;
16228{
16229 static char_u mybuf[NUMBUFLEN];
16230
16231 return get_tv_string_buf_chk(varp, mybuf);
16232}
16233
16234 static char_u *
16235get_tv_string_buf_chk(varp, buf)
16236 typval_T *varp;
16237 char_u *buf;
16238{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016239 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016241 case VAR_NUMBER:
16242 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16243 return buf;
16244 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016245 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016246 break;
16247 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016248 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016249 break;
16250 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016251 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016252 break;
16253 case VAR_STRING:
16254 if (varp->vval.v_string != NULL)
16255 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016256 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016257 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016258 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016259 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016261 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262}
16263
16264/*
16265 * Find variable "name" in the list of variables.
16266 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016267 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016268 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016271 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016272find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016274 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016277 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278
Bram Moolenaara7043832005-01-21 11:56:39 +000016279 ht = find_var_ht(name, &varname);
16280 if (htp != NULL)
16281 *htp = ht;
16282 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016283 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016284 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285}
16286
16287/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016288 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016289 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016291 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016292find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016293 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016294 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016295 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016296{
Bram Moolenaar33570922005-01-25 22:26:29 +000016297 hashitem_T *hi;
16298
16299 if (*varname == NUL)
16300 {
16301 /* Must be something like "s:", otherwise "ht" would be NULL. */
16302 switch (varname[-2])
16303 {
16304 case 's': return &SCRIPT_SV(current_SID).sv_var;
16305 case 'g': return &globvars_var;
16306 case 'v': return &vimvars_var;
16307 case 'b': return &curbuf->b_bufvar;
16308 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016309 case 'l': return current_funccal == NULL
16310 ? NULL : &current_funccal->l_vars_var;
16311 case 'a': return current_funccal == NULL
16312 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016313 }
16314 return NULL;
16315 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016316
16317 hi = hash_find(ht, varname);
16318 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016319 {
16320 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016321 * worked find the variable again. Don't auto-load a script if it was
16322 * loaded already, otherwise it would be loaded every time when
16323 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016324 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016325 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016326 hi = hash_find(ht, varname);
16327 if (HASHITEM_EMPTY(hi))
16328 return NULL;
16329 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016330 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016331}
16332
16333/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016334 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016335 * Set "varname" to the start of name without ':'.
16336 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016337 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016338find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339 char_u *name;
16340 char_u **varname;
16341{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016342 hashitem_T *hi;
16343
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344 if (name[1] != ':')
16345 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016346 /* The name must not start with a colon or #. */
16347 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 return NULL;
16349 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016350
16351 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016352 hi = hash_find(&compat_hashtab, name);
16353 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016354 return &compat_hashtab;
16355
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016357 return &globvarht; /* global variable */
16358 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359 }
16360 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016361 if (*name == 'g') /* global variable */
16362 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016363 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16364 */
16365 if (vim_strchr(name + 2, ':') != NULL
16366 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016367 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016369 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016371 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016372 if (*name == 'v') /* v: variable */
16373 return &vimvarht;
16374 if (*name == 'a' && current_funccal != NULL) /* function argument */
16375 return &current_funccal->l_avars.dv_hashtab;
16376 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16377 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 if (*name == 's' /* script variable */
16379 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16380 return &SCRIPT_VARS(current_SID);
16381 return NULL;
16382}
16383
16384/*
16385 * Get the string value of a (global/local) variable.
16386 * Returns NULL when it doesn't exist.
16387 */
16388 char_u *
16389get_var_value(name)
16390 char_u *name;
16391{
Bram Moolenaar33570922005-01-25 22:26:29 +000016392 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393
Bram Moolenaara7043832005-01-21 11:56:39 +000016394 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395 if (v == NULL)
16396 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016397 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398}
16399
16400/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016401 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402 * sourcing this script and when executing functions defined in the script.
16403 */
16404 void
16405new_script_vars(id)
16406 scid_T id;
16407{
Bram Moolenaara7043832005-01-21 11:56:39 +000016408 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016409 hashtab_T *ht;
16410 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016411
Bram Moolenaar071d4272004-06-13 20:20:40 +000016412 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16413 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016414 /* Re-allocating ga_data means that an ht_array pointing to
16415 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016416 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016417 for (i = 1; i <= ga_scripts.ga_len; ++i)
16418 {
16419 ht = &SCRIPT_VARS(i);
16420 if (ht->ht_mask == HT_INIT_SIZE - 1)
16421 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016422 sv = &SCRIPT_SV(i);
16423 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016424 }
16425
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426 while (ga_scripts.ga_len < id)
16427 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016428 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16429 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431 }
16432 }
16433}
16434
16435/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016436 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16437 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 */
16439 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016440init_var_dict(dict, dict_var)
16441 dict_T *dict;
16442 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443{
Bram Moolenaar33570922005-01-25 22:26:29 +000016444 hash_init(&dict->dv_hashtab);
16445 dict->dv_refcount = 99999;
16446 dict_var->di_tv.vval.v_dict = dict;
16447 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016448 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016449 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16450 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451}
16452
16453/*
16454 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016455 * Frees all allocated variables and the value they contain.
16456 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 */
16458 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016459vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016460 hashtab_T *ht;
16461{
16462 vars_clear_ext(ht, TRUE);
16463}
16464
16465/*
16466 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16467 */
16468 static void
16469vars_clear_ext(ht, free_val)
16470 hashtab_T *ht;
16471 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472{
Bram Moolenaara7043832005-01-21 11:56:39 +000016473 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016474 hashitem_T *hi;
16475 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476
Bram Moolenaar33570922005-01-25 22:26:29 +000016477 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016478 todo = ht->ht_used;
16479 for (hi = ht->ht_array; todo > 0; ++hi)
16480 {
16481 if (!HASHITEM_EMPTY(hi))
16482 {
16483 --todo;
16484
Bram Moolenaar33570922005-01-25 22:26:29 +000016485 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016486 * ht_array might change then. hash_clear() takes care of it
16487 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016488 v = HI2DI(hi);
16489 if (free_val)
16490 clear_tv(&v->di_tv);
16491 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16492 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016493 }
16494 }
16495 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016496 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497}
16498
Bram Moolenaara7043832005-01-21 11:56:39 +000016499/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016500 * Delete a variable from hashtab "ht" at item "hi".
16501 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016504delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016505 hashtab_T *ht;
16506 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507{
Bram Moolenaar33570922005-01-25 22:26:29 +000016508 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016509
16510 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016511 clear_tv(&di->di_tv);
16512 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513}
16514
16515/*
16516 * List the value of one internal variable.
16517 */
16518 static void
16519list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016520 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521 char_u *prefix;
16522{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016523 char_u *tofree;
16524 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016525 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016526
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016527 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000016528 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016529 s == NULL ? (char_u *)"" : s);
16530 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531}
16532
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533 static void
16534list_one_var_a(prefix, name, type, string)
16535 char_u *prefix;
16536 char_u *name;
16537 int type;
16538 char_u *string;
16539{
16540 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16541 if (name != NULL) /* "a:" vars don't have a name stored */
16542 msg_puts(name);
16543 msg_putchar(' ');
16544 msg_advance(22);
16545 if (type == VAR_NUMBER)
16546 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016547 else if (type == VAR_FUNC)
16548 msg_putchar('*');
16549 else if (type == VAR_LIST)
16550 {
16551 msg_putchar('[');
16552 if (*string == '[')
16553 ++string;
16554 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016555 else if (type == VAR_DICT)
16556 {
16557 msg_putchar('{');
16558 if (*string == '{')
16559 ++string;
16560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561 else
16562 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016563
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016565
16566 if (type == VAR_FUNC)
16567 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568}
16569
16570/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016571 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 * If the variable already exists, the value is updated.
16573 * Otherwise the variable is created.
16574 */
16575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016576set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016577 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016578 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016579 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580{
Bram Moolenaar33570922005-01-25 22:26:29 +000016581 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016583 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016584 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016586 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016587 {
16588 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16589 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16590 ? name[2] : name[0]))
16591 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016592 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016593 return;
16594 }
16595 if (function_exists(name))
16596 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016597 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016598 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016599 return;
16600 }
16601 }
16602
Bram Moolenaara7043832005-01-21 11:56:39 +000016603 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016604 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016605 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016606 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016607 return;
16608 }
16609
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016610 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016611 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016612 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016613 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016614 if (var_check_ro(v->di_flags, name)
16615 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016616 return;
16617 if (v->di_tv.v_type != tv->v_type
16618 && !((v->di_tv.v_type == VAR_STRING
16619 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016620 && (tv->v_type == VAR_STRING
16621 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016622 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016623 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016624 return;
16625 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016626
16627 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016628 * Handle setting internal v: variables separately: we don't change
16629 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016630 */
16631 if (ht == &vimvarht)
16632 {
16633 if (v->di_tv.v_type == VAR_STRING)
16634 {
16635 vim_free(v->di_tv.vval.v_string);
16636 if (copy || tv->v_type != VAR_STRING)
16637 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16638 else
16639 {
16640 /* Take over the string to avoid an extra alloc/free. */
16641 v->di_tv.vval.v_string = tv->vval.v_string;
16642 tv->vval.v_string = NULL;
16643 }
16644 }
16645 else if (v->di_tv.v_type != VAR_NUMBER)
16646 EMSG2(_(e_intern2), "set_var()");
16647 else
16648 v->di_tv.vval.v_number = get_tv_number(tv);
16649 return;
16650 }
16651
16652 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 }
16654 else /* add a new variable */
16655 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016656 /* Make sure the variable name is valid. */
16657 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000016658 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
16659 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000016660 {
16661 EMSG2(_(e_illvar), varname);
16662 return;
16663 }
16664
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016665 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16666 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016667 if (v == NULL)
16668 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016669 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016670 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016672 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 return;
16674 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016675 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016678 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016679 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016680 else
16681 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016682 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016683 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016684 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016686}
16687
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016688/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016689 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16690 * Also give an error message.
16691 */
16692 static int
16693var_check_ro(flags, name)
16694 int flags;
16695 char_u *name;
16696{
16697 if (flags & DI_FLAGS_RO)
16698 {
16699 EMSG2(_(e_readonlyvar), name);
16700 return TRUE;
16701 }
16702 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16703 {
16704 EMSG2(_(e_readonlysbx), name);
16705 return TRUE;
16706 }
16707 return FALSE;
16708}
16709
16710/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016711 * Return TRUE if typeval "tv" is set to be locked (immutable).
16712 * Also give an error message, using "name".
16713 */
16714 static int
16715tv_check_lock(lock, name)
16716 int lock;
16717 char_u *name;
16718{
16719 if (lock & VAR_LOCKED)
16720 {
16721 EMSG2(_("E741: Value is locked: %s"),
16722 name == NULL ? (char_u *)_("Unknown") : name);
16723 return TRUE;
16724 }
16725 if (lock & VAR_FIXED)
16726 {
16727 EMSG2(_("E742: Cannot change value of %s"),
16728 name == NULL ? (char_u *)_("Unknown") : name);
16729 return TRUE;
16730 }
16731 return FALSE;
16732}
16733
16734/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016735 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016736 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016737 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016740copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016741 typval_T *from;
16742 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016744 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016745 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016746 switch (from->v_type)
16747 {
16748 case VAR_NUMBER:
16749 to->vval.v_number = from->vval.v_number;
16750 break;
16751 case VAR_STRING:
16752 case VAR_FUNC:
16753 if (from->vval.v_string == NULL)
16754 to->vval.v_string = NULL;
16755 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016756 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016757 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016758 if (from->v_type == VAR_FUNC)
16759 func_ref(to->vval.v_string);
16760 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016761 break;
16762 case VAR_LIST:
16763 if (from->vval.v_list == NULL)
16764 to->vval.v_list = NULL;
16765 else
16766 {
16767 to->vval.v_list = from->vval.v_list;
16768 ++to->vval.v_list->lv_refcount;
16769 }
16770 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016771 case VAR_DICT:
16772 if (from->vval.v_dict == NULL)
16773 to->vval.v_dict = NULL;
16774 else
16775 {
16776 to->vval.v_dict = from->vval.v_dict;
16777 ++to->vval.v_dict->dv_refcount;
16778 }
16779 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016780 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016781 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016782 break;
16783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784}
16785
16786/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016787 * Make a copy of an item.
16788 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016789 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16790 * reference to an already copied list/dict can be used.
16791 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016792 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016793 static int
16794item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016795 typval_T *from;
16796 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016797 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016798 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016799{
16800 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016801 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016802
Bram Moolenaar33570922005-01-25 22:26:29 +000016803 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016804 {
16805 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016806 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016807 }
16808 ++recurse;
16809
16810 switch (from->v_type)
16811 {
16812 case VAR_NUMBER:
16813 case VAR_STRING:
16814 case VAR_FUNC:
16815 copy_tv(from, to);
16816 break;
16817 case VAR_LIST:
16818 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016819 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016820 if (from->vval.v_list == NULL)
16821 to->vval.v_list = NULL;
16822 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16823 {
16824 /* use the copy made earlier */
16825 to->vval.v_list = from->vval.v_list->lv_copylist;
16826 ++to->vval.v_list->lv_refcount;
16827 }
16828 else
16829 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16830 if (to->vval.v_list == NULL)
16831 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016832 break;
16833 case VAR_DICT:
16834 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016835 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016836 if (from->vval.v_dict == NULL)
16837 to->vval.v_dict = NULL;
16838 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16839 {
16840 /* use the copy made earlier */
16841 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16842 ++to->vval.v_dict->dv_refcount;
16843 }
16844 else
16845 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16846 if (to->vval.v_dict == NULL)
16847 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016848 break;
16849 default:
16850 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016851 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016852 }
16853 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016854 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016855}
16856
16857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858 * ":echo expr1 ..." print each argument separated with a space, add a
16859 * newline at the end.
16860 * ":echon expr1 ..." print each argument plain.
16861 */
16862 void
16863ex_echo(eap)
16864 exarg_T *eap;
16865{
16866 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016867 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016868 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869 char_u *p;
16870 int needclr = TRUE;
16871 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016872 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873
16874 if (eap->skip)
16875 ++emsg_skip;
16876 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16877 {
16878 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016879 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880 {
16881 /*
16882 * Report the invalid expression unless the expression evaluation
16883 * has been cancelled due to an aborting error, an interrupt, or an
16884 * exception.
16885 */
16886 if (!aborting())
16887 EMSG2(_(e_invexpr2), p);
16888 break;
16889 }
16890 if (!eap->skip)
16891 {
16892 if (atstart)
16893 {
16894 atstart = FALSE;
16895 /* Call msg_start() after eval1(), evaluating the expression
16896 * may cause a message to appear. */
16897 if (eap->cmdidx == CMD_echo)
16898 msg_start();
16899 }
16900 else if (eap->cmdidx == CMD_echo)
16901 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016902 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016903 if (p != NULL)
16904 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016906 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016908 if (*p != TAB && needclr)
16909 {
16910 /* remove any text still there from the command */
16911 msg_clr_eos();
16912 needclr = FALSE;
16913 }
16914 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 }
16916 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016917 {
16918#ifdef FEAT_MBYTE
16919 if (has_mbyte)
16920 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016921 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016922
16923 (void)msg_outtrans_len_attr(p, i, echo_attr);
16924 p += i - 1;
16925 }
16926 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016928 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016931 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016933 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934 arg = skipwhite(arg);
16935 }
16936 eap->nextcmd = check_nextcmd(arg);
16937
16938 if (eap->skip)
16939 --emsg_skip;
16940 else
16941 {
16942 /* remove text that may still be there from the command */
16943 if (needclr)
16944 msg_clr_eos();
16945 if (eap->cmdidx == CMD_echo)
16946 msg_end();
16947 }
16948}
16949
16950/*
16951 * ":echohl {name}".
16952 */
16953 void
16954ex_echohl(eap)
16955 exarg_T *eap;
16956{
16957 int id;
16958
16959 id = syn_name2id(eap->arg);
16960 if (id == 0)
16961 echo_attr = 0;
16962 else
16963 echo_attr = syn_id2attr(id);
16964}
16965
16966/*
16967 * ":execute expr1 ..." execute the result of an expression.
16968 * ":echomsg expr1 ..." Print a message
16969 * ":echoerr expr1 ..." Print an error
16970 * Each gets spaces around each argument and a newline at the end for
16971 * echo commands
16972 */
16973 void
16974ex_execute(eap)
16975 exarg_T *eap;
16976{
16977 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016978 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 int ret = OK;
16980 char_u *p;
16981 garray_T ga;
16982 int len;
16983 int save_did_emsg;
16984
16985 ga_init2(&ga, 1, 80);
16986
16987 if (eap->skip)
16988 ++emsg_skip;
16989 while (*arg != NUL && *arg != '|' && *arg != '\n')
16990 {
16991 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016992 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993 {
16994 /*
16995 * Report the invalid expression unless the expression evaluation
16996 * has been cancelled due to an aborting error, an interrupt, or an
16997 * exception.
16998 */
16999 if (!aborting())
17000 EMSG2(_(e_invexpr2), p);
17001 ret = FAIL;
17002 break;
17003 }
17004
17005 if (!eap->skip)
17006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017007 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008 len = (int)STRLEN(p);
17009 if (ga_grow(&ga, len + 2) == FAIL)
17010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017011 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012 ret = FAIL;
17013 break;
17014 }
17015 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 ga.ga_len += len;
17019 }
17020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017021 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022 arg = skipwhite(arg);
17023 }
17024
17025 if (ret != FAIL && ga.ga_data != NULL)
17026 {
17027 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017030 out_flush();
17031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032 else if (eap->cmdidx == CMD_echoerr)
17033 {
17034 /* We don't want to abort following commands, restore did_emsg. */
17035 save_did_emsg = did_emsg;
17036 EMSG((char_u *)ga.ga_data);
17037 if (!force_abort)
17038 did_emsg = save_did_emsg;
17039 }
17040 else if (eap->cmdidx == CMD_execute)
17041 do_cmdline((char_u *)ga.ga_data,
17042 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17043 }
17044
17045 ga_clear(&ga);
17046
17047 if (eap->skip)
17048 --emsg_skip;
17049
17050 eap->nextcmd = check_nextcmd(arg);
17051}
17052
17053/*
17054 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17055 * "arg" points to the "&" or '+' when called, to "option" when returning.
17056 * Returns NULL when no option name found. Otherwise pointer to the char
17057 * after the option name.
17058 */
17059 static char_u *
17060find_option_end(arg, opt_flags)
17061 char_u **arg;
17062 int *opt_flags;
17063{
17064 char_u *p = *arg;
17065
17066 ++p;
17067 if (*p == 'g' && p[1] == ':')
17068 {
17069 *opt_flags = OPT_GLOBAL;
17070 p += 2;
17071 }
17072 else if (*p == 'l' && p[1] == ':')
17073 {
17074 *opt_flags = OPT_LOCAL;
17075 p += 2;
17076 }
17077 else
17078 *opt_flags = 0;
17079
17080 if (!ASCII_ISALPHA(*p))
17081 return NULL;
17082 *arg = p;
17083
17084 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17085 p += 4; /* termcap option */
17086 else
17087 while (ASCII_ISALPHA(*p))
17088 ++p;
17089 return p;
17090}
17091
17092/*
17093 * ":function"
17094 */
17095 void
17096ex_function(eap)
17097 exarg_T *eap;
17098{
17099 char_u *theline;
17100 int j;
17101 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103 char_u *name = NULL;
17104 char_u *p;
17105 char_u *arg;
17106 garray_T newargs;
17107 garray_T newlines;
17108 int varargs = FALSE;
17109 int mustend = FALSE;
17110 int flags = 0;
17111 ufunc_T *fp;
17112 int indent;
17113 int nesting;
17114 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017115 dictitem_T *v;
17116 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017117 static int func_nr = 0; /* number for nameless function */
17118 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017119 hashtab_T *ht;
17120 int todo;
17121 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122
17123 /*
17124 * ":function" without argument: list functions.
17125 */
17126 if (ends_excmd(*eap->arg))
17127 {
17128 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017129 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017130 todo = func_hashtab.ht_used;
17131 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017132 {
17133 if (!HASHITEM_EMPTY(hi))
17134 {
17135 --todo;
17136 fp = HI2UF(hi);
17137 if (!isdigit(*fp->uf_name))
17138 list_func_head(fp, FALSE);
17139 }
17140 }
17141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142 eap->nextcmd = check_nextcmd(eap->arg);
17143 return;
17144 }
17145
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017146 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017147 * ":function /pat": list functions matching pattern.
17148 */
17149 if (*eap->arg == '/')
17150 {
17151 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17152 if (!eap->skip)
17153 {
17154 regmatch_T regmatch;
17155
17156 c = *p;
17157 *p = NUL;
17158 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17159 *p = c;
17160 if (regmatch.regprog != NULL)
17161 {
17162 regmatch.rm_ic = p_ic;
17163
17164 todo = func_hashtab.ht_used;
17165 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17166 {
17167 if (!HASHITEM_EMPTY(hi))
17168 {
17169 --todo;
17170 fp = HI2UF(hi);
17171 if (!isdigit(*fp->uf_name)
17172 && vim_regexec(&regmatch, fp->uf_name, 0))
17173 list_func_head(fp, FALSE);
17174 }
17175 }
17176 }
17177 }
17178 if (*p == '/')
17179 ++p;
17180 eap->nextcmd = check_nextcmd(p);
17181 return;
17182 }
17183
17184 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017185 * Get the function name. There are these situations:
17186 * func normal function name
17187 * "name" == func, "fudi.fd_dict" == NULL
17188 * dict.func new dictionary entry
17189 * "name" == NULL, "fudi.fd_dict" set,
17190 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17191 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017192 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017193 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17194 * dict.func existing dict entry that's not a Funcref
17195 * "name" == NULL, "fudi.fd_dict" set,
17196 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017199 name = trans_function_name(&p, eap->skip, 0, &fudi);
17200 paren = (vim_strchr(p, '(') != NULL);
17201 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202 {
17203 /*
17204 * Return on an invalid expression in braces, unless the expression
17205 * evaluation has been cancelled due to an aborting error, an
17206 * interrupt, or an exception.
17207 */
17208 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017209 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017210 if (!eap->skip && fudi.fd_newkey != NULL)
17211 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017212 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 else
17216 eap->skip = TRUE;
17217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218 /* An error in a function call during evaluation of an expression in magic
17219 * braces should not cause the function not to be defined. */
17220 saved_did_emsg = did_emsg;
17221 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222
17223 /*
17224 * ":function func" with only function name: list function.
17225 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017226 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227 {
17228 if (!ends_excmd(*skipwhite(p)))
17229 {
17230 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017231 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 }
17233 eap->nextcmd = check_nextcmd(p);
17234 if (eap->nextcmd != NULL)
17235 *p = NUL;
17236 if (!eap->skip && !got_int)
17237 {
17238 fp = find_func(name);
17239 if (fp != NULL)
17240 {
17241 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017242 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243 {
17244 msg_putchar('\n');
17245 msg_outnum((long)(j + 1));
17246 if (j < 9)
17247 msg_putchar(' ');
17248 if (j < 99)
17249 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017250 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 out_flush(); /* show a line at a time */
17252 ui_breakcheck();
17253 }
17254 if (!got_int)
17255 {
17256 msg_putchar('\n');
17257 msg_puts((char_u *)" endfunction");
17258 }
17259 }
17260 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017261 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017263 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 }
17265
17266 /*
17267 * ":function name(arg1, arg2)" Define function.
17268 */
17269 p = skipwhite(p);
17270 if (*p != '(')
17271 {
17272 if (!eap->skip)
17273 {
17274 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017275 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 }
17277 /* attempt to continue by skipping some text */
17278 if (vim_strchr(p, '(') != NULL)
17279 p = vim_strchr(p, '(');
17280 }
17281 p = skipwhite(p + 1);
17282
17283 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17284 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17285
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017286 if (!eap->skip)
17287 {
17288 /* Check the name of the function. */
17289 if (name != NULL)
17290 arg = name;
17291 else
17292 arg = fudi.fd_newkey;
17293 if (arg != NULL)
17294 {
17295 if (*arg == K_SPECIAL)
17296 j = 3;
17297 else
17298 j = 0;
17299 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17300 : eval_isnamec(arg[j])))
17301 ++j;
17302 if (arg[j] != NUL)
17303 emsg_funcname(_(e_invarg2), arg);
17304 }
17305 }
17306
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 /*
17308 * Isolate the arguments: "arg1, arg2, ...)"
17309 */
17310 while (*p != ')')
17311 {
17312 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17313 {
17314 varargs = TRUE;
17315 p += 3;
17316 mustend = TRUE;
17317 }
17318 else
17319 {
17320 arg = p;
17321 while (ASCII_ISALNUM(*p) || *p == '_')
17322 ++p;
17323 if (arg == p || isdigit(*arg)
17324 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17325 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17326 {
17327 if (!eap->skip)
17328 EMSG2(_("E125: Illegal argument: %s"), arg);
17329 break;
17330 }
17331 if (ga_grow(&newargs, 1) == FAIL)
17332 goto erret;
17333 c = *p;
17334 *p = NUL;
17335 arg = vim_strsave(arg);
17336 if (arg == NULL)
17337 goto erret;
17338 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17339 *p = c;
17340 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341 if (*p == ',')
17342 ++p;
17343 else
17344 mustend = TRUE;
17345 }
17346 p = skipwhite(p);
17347 if (mustend && *p != ')')
17348 {
17349 if (!eap->skip)
17350 EMSG2(_(e_invarg2), eap->arg);
17351 break;
17352 }
17353 }
17354 ++p; /* skip the ')' */
17355
Bram Moolenaare9a41262005-01-15 22:18:47 +000017356 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 for (;;)
17358 {
17359 p = skipwhite(p);
17360 if (STRNCMP(p, "range", 5) == 0)
17361 {
17362 flags |= FC_RANGE;
17363 p += 5;
17364 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017365 else if (STRNCMP(p, "dict", 4) == 0)
17366 {
17367 flags |= FC_DICT;
17368 p += 4;
17369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 else if (STRNCMP(p, "abort", 5) == 0)
17371 {
17372 flags |= FC_ABORT;
17373 p += 5;
17374 }
17375 else
17376 break;
17377 }
17378
17379 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
17380 EMSG(_(e_trailing));
17381
17382 /*
17383 * Read the body of the function, until ":endfunction" is found.
17384 */
17385 if (KeyTyped)
17386 {
17387 /* Check if the function already exists, don't let the user type the
17388 * whole function before telling him it doesn't work! For a script we
17389 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017390 if (!eap->skip && !eap->forceit)
17391 {
17392 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17393 EMSG(_(e_funcdict));
17394 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017395 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017398 if (!eap->skip && did_emsg)
17399 goto erret;
17400
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 msg_putchar('\n'); /* don't overwrite the function name */
17402 cmdline_row = msg_row;
17403 }
17404
17405 indent = 2;
17406 nesting = 0;
17407 for (;;)
17408 {
17409 msg_scroll = TRUE;
17410 need_wait_return = FALSE;
17411 if (eap->getline == NULL)
17412 theline = getcmdline(':', 0L, indent);
17413 else
17414 theline = eap->getline(':', eap->cookie, indent);
17415 if (KeyTyped)
17416 lines_left = Rows - 1;
17417 if (theline == NULL)
17418 {
17419 EMSG(_("E126: Missing :endfunction"));
17420 goto erret;
17421 }
17422
17423 if (skip_until != NULL)
17424 {
17425 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17426 * don't check for ":endfunc". */
17427 if (STRCMP(theline, skip_until) == 0)
17428 {
17429 vim_free(skip_until);
17430 skip_until = NULL;
17431 }
17432 }
17433 else
17434 {
17435 /* skip ':' and blanks*/
17436 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17437 ;
17438
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017439 /* Check for "endfunction". */
17440 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441 {
17442 vim_free(theline);
17443 break;
17444 }
17445
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017446 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447 * at "end". */
17448 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17449 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017450 else if (STRNCMP(p, "if", 2) == 0
17451 || STRNCMP(p, "wh", 2) == 0
17452 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 || STRNCMP(p, "try", 3) == 0)
17454 indent += 2;
17455
17456 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017457 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017459 if (*p == '!')
17460 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461 p += eval_fname_script(p);
17462 if (ASCII_ISALPHA(*p))
17463 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017464 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465 if (*skipwhite(p) == '(')
17466 {
17467 ++nesting;
17468 indent += 2;
17469 }
17470 }
17471 }
17472
17473 /* Check for ":append" or ":insert". */
17474 p = skip_range(p, NULL);
17475 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17476 || (p[0] == 'i'
17477 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17478 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17479 skip_until = vim_strsave((char_u *)".");
17480
17481 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17482 arg = skipwhite(skiptowhite(p));
17483 if (arg[0] == '<' && arg[1] =='<'
17484 && ((p[0] == 'p' && p[1] == 'y'
17485 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17486 || (p[0] == 'p' && p[1] == 'e'
17487 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17488 || (p[0] == 't' && p[1] == 'c'
17489 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17490 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17491 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017492 || (p[0] == 'm' && p[1] == 'z'
17493 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 ))
17495 {
17496 /* ":python <<" continues until a dot, like ":append" */
17497 p = skipwhite(arg + 2);
17498 if (*p == NUL)
17499 skip_until = vim_strsave((char_u *)".");
17500 else
17501 skip_until = vim_strsave(p);
17502 }
17503 }
17504
17505 /* Add the line to the function. */
17506 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017507 {
17508 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017510 }
17511
17512 /* Copy the line to newly allocated memory. get_one_sourceline()
17513 * allocates 250 bytes per line, this saves 80% on average. The cost
17514 * is an extra alloc/free. */
17515 p = vim_strsave(theline);
17516 if (p != NULL)
17517 {
17518 vim_free(theline);
17519 theline = p;
17520 }
17521
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17523 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524 }
17525
17526 /* Don't define the function when skipping commands or when an error was
17527 * detected. */
17528 if (eap->skip || did_emsg)
17529 goto erret;
17530
17531 /*
17532 * If there are no errors, add the function
17533 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017534 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017535 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017536 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017537 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017538 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017539 emsg_funcname("E707: Function name conflicts with variable: %s",
17540 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017541 goto erret;
17542 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017543
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017544 fp = find_func(name);
17545 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017547 if (!eap->forceit)
17548 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017549 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017550 goto erret;
17551 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017552 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017553 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017554 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017555 name);
17556 goto erret;
17557 }
17558 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017559 ga_clear_strings(&(fp->uf_args));
17560 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017561 vim_free(name);
17562 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 }
17565 else
17566 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017567 char numbuf[20];
17568
17569 fp = NULL;
17570 if (fudi.fd_newkey == NULL && !eap->forceit)
17571 {
17572 EMSG(_(e_funcdict));
17573 goto erret;
17574 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017575 if (fudi.fd_di == NULL)
17576 {
17577 /* Can't add a function to a locked dictionary */
17578 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17579 goto erret;
17580 }
17581 /* Can't change an existing function if it is locked */
17582 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17583 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017584
17585 /* Give the function a sequential number. Can only be used with a
17586 * Funcref! */
17587 vim_free(name);
17588 sprintf(numbuf, "%d", ++func_nr);
17589 name = vim_strsave((char_u *)numbuf);
17590 if (name == NULL)
17591 goto erret;
17592 }
17593
17594 if (fp == NULL)
17595 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017596 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017597 {
17598 int slen, plen;
17599 char_u *scriptname;
17600
17601 /* Check that the autoload name matches the script name. */
17602 j = FAIL;
17603 if (sourcing_name != NULL)
17604 {
17605 scriptname = autoload_name(name);
17606 if (scriptname != NULL)
17607 {
17608 p = vim_strchr(scriptname, '/');
17609 plen = STRLEN(p);
17610 slen = STRLEN(sourcing_name);
17611 if (slen > plen && fnamecmp(p,
17612 sourcing_name + slen - plen) == 0)
17613 j = OK;
17614 vim_free(scriptname);
17615 }
17616 }
17617 if (j == FAIL)
17618 {
17619 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17620 goto erret;
17621 }
17622 }
17623
17624 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 if (fp == NULL)
17626 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017627
17628 if (fudi.fd_dict != NULL)
17629 {
17630 if (fudi.fd_di == NULL)
17631 {
17632 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017633 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017634 if (fudi.fd_di == NULL)
17635 {
17636 vim_free(fp);
17637 goto erret;
17638 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017639 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17640 {
17641 vim_free(fudi.fd_di);
17642 goto erret;
17643 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017644 }
17645 else
17646 /* overwrite existing dict entry */
17647 clear_tv(&fudi.fd_di->di_tv);
17648 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017649 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017650 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017651 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017652 }
17653
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017655 STRCPY(fp->uf_name, name);
17656 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017658 fp->uf_args = newargs;
17659 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017660#ifdef FEAT_PROFILE
17661 fp->uf_tml_count = NULL;
17662 fp->uf_tml_total = NULL;
17663 fp->uf_tml_self = NULL;
17664 fp->uf_profiling = FALSE;
17665 if (prof_def_func())
17666 func_do_profile(fp);
17667#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017668 fp->uf_varargs = varargs;
17669 fp->uf_flags = flags;
17670 fp->uf_calls = 0;
17671 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017672 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673
17674erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 ga_clear_strings(&newargs);
17676 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017677ret_free:
17678 vim_free(skip_until);
17679 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682}
17683
17684/*
17685 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017686 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017688 * flags:
17689 * TFN_INT: internal function name OK
17690 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691 * Advances "pp" to just after the function name (if no error).
17692 */
17693 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017694trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 char_u **pp;
17696 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017697 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017698 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017700 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 char_u *start;
17702 char_u *end;
17703 int lead;
17704 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017706 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017707
17708 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017709 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017711
17712 /* Check for hard coded <SNR>: already translated function ID (from a user
17713 * command). */
17714 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17715 && (*pp)[2] == (int)KE_SNR)
17716 {
17717 *pp += 3;
17718 len = get_id_len(pp) + 3;
17719 return vim_strnsave(start, len);
17720 }
17721
17722 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17723 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017725 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017727
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017728 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17729 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017730 if (end == start)
17731 {
17732 if (!skip)
17733 EMSG(_("E129: Function name required"));
17734 goto theend;
17735 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017736 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017737 {
17738 /*
17739 * Report an invalid expression in braces, unless the expression
17740 * evaluation has been cancelled due to an aborting error, an
17741 * interrupt, or an exception.
17742 */
17743 if (!aborting())
17744 {
17745 if (end != NULL)
17746 EMSG2(_(e_invarg2), start);
17747 }
17748 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017749 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017750 goto theend;
17751 }
17752
17753 if (lv.ll_tv != NULL)
17754 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017755 if (fdp != NULL)
17756 {
17757 fdp->fd_dict = lv.ll_dict;
17758 fdp->fd_newkey = lv.ll_newkey;
17759 lv.ll_newkey = NULL;
17760 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017761 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017762 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17763 {
17764 name = vim_strsave(lv.ll_tv->vval.v_string);
17765 *pp = end;
17766 }
17767 else
17768 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017769 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17770 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017771 EMSG(_(e_funcref));
17772 else
17773 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017774 name = NULL;
17775 }
17776 goto theend;
17777 }
17778
17779 if (lv.ll_name == NULL)
17780 {
17781 /* Error found, but continue after the function name. */
17782 *pp = end;
17783 goto theend;
17784 }
17785
17786 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000017787 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017788 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000017789 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
17790 && STRNCMP(lv.ll_name, "s:", 2) == 0)
17791 {
17792 /* When there was "s:" already or the name expanded to get a
17793 * leading "s:" then remove it. */
17794 lv.ll_name += 2;
17795 len -= 2;
17796 lead = 2;
17797 }
17798 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017799 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017800 {
17801 if (lead == 2) /* skip over "s:" */
17802 lv.ll_name += 2;
17803 len = (int)(end - lv.ll_name);
17804 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017805
17806 /*
17807 * Copy the function name to allocated memory.
17808 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17809 * Accept <SNR>123_name() outside a script.
17810 */
17811 if (skip)
17812 lead = 0; /* do nothing */
17813 else if (lead > 0)
17814 {
17815 lead = 3;
17816 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17817 {
17818 if (current_SID <= 0)
17819 {
17820 EMSG(_(e_usingsid));
17821 goto theend;
17822 }
17823 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17824 lead += (int)STRLEN(sid_buf);
17825 }
17826 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017827 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017828 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017829 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017830 goto theend;
17831 }
17832 name = alloc((unsigned)(len + lead + 1));
17833 if (name != NULL)
17834 {
17835 if (lead > 0)
17836 {
17837 name[0] = K_SPECIAL;
17838 name[1] = KS_EXTRA;
17839 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017840 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017841 STRCPY(name + 3, sid_buf);
17842 }
17843 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17844 name[len + lead] = NUL;
17845 }
17846 *pp = end;
17847
17848theend:
17849 clear_lval(&lv);
17850 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851}
17852
17853/*
17854 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17855 * Return 2 if "p" starts with "s:".
17856 * Return 0 otherwise.
17857 */
17858 static int
17859eval_fname_script(p)
17860 char_u *p;
17861{
17862 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17863 || STRNICMP(p + 1, "SNR>", 4) == 0))
17864 return 5;
17865 if (p[0] == 's' && p[1] == ':')
17866 return 2;
17867 return 0;
17868}
17869
17870/*
17871 * Return TRUE if "p" starts with "<SID>" or "s:".
17872 * Only works if eval_fname_script() returned non-zero for "p"!
17873 */
17874 static int
17875eval_fname_sid(p)
17876 char_u *p;
17877{
17878 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17879}
17880
17881/*
17882 * List the head of the function: "name(arg1, arg2)".
17883 */
17884 static void
17885list_func_head(fp, indent)
17886 ufunc_T *fp;
17887 int indent;
17888{
17889 int j;
17890
17891 msg_start();
17892 if (indent)
17893 MSG_PUTS(" ");
17894 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017895 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 {
17897 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017898 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899 }
17900 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017901 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017903 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904 {
17905 if (j)
17906 MSG_PUTS(", ");
17907 msg_puts(FUNCARG(fp, j));
17908 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017909 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910 {
17911 if (j)
17912 MSG_PUTS(", ");
17913 MSG_PUTS("...");
17914 }
17915 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000017916 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000017917 if (p_verbose > 0)
17918 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919}
17920
17921/*
17922 * Find a function by name, return pointer to it in ufuncs.
17923 * Return NULL for unknown function.
17924 */
17925 static ufunc_T *
17926find_func(name)
17927 char_u *name;
17928{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017929 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017931 hi = hash_find(&func_hashtab, name);
17932 if (!HASHITEM_EMPTY(hi))
17933 return HI2UF(hi);
17934 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935}
17936
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017937#if defined(EXITFREE) || defined(PROTO)
17938 void
17939free_all_functions()
17940{
17941 hashitem_T *hi;
17942
17943 /* Need to start all over every time, because func_free() may change the
17944 * hash table. */
17945 while (func_hashtab.ht_used > 0)
17946 for (hi = func_hashtab.ht_array; ; ++hi)
17947 if (!HASHITEM_EMPTY(hi))
17948 {
17949 func_free(HI2UF(hi));
17950 break;
17951 }
17952}
17953#endif
17954
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017955/*
17956 * Return TRUE if a function "name" exists.
17957 */
17958 static int
17959function_exists(name)
17960 char_u *name;
17961{
17962 char_u *p = name;
17963 int n = FALSE;
17964
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017965 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017966 if (p != NULL)
17967 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017968 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017969 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017970 else
17971 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017972 vim_free(p);
17973 }
17974 return n;
17975}
17976
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017977/*
17978 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017979 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017980 */
17981 static int
17982builtin_function(name)
17983 char_u *name;
17984{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017985 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17986 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017987}
17988
Bram Moolenaar05159a02005-02-26 23:04:13 +000017989#if defined(FEAT_PROFILE) || defined(PROTO)
17990/*
17991 * Start profiling function "fp".
17992 */
17993 static void
17994func_do_profile(fp)
17995 ufunc_T *fp;
17996{
17997 fp->uf_tm_count = 0;
17998 profile_zero(&fp->uf_tm_self);
17999 profile_zero(&fp->uf_tm_total);
18000 if (fp->uf_tml_count == NULL)
18001 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18002 (sizeof(int) * fp->uf_lines.ga_len));
18003 if (fp->uf_tml_total == NULL)
18004 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18005 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18006 if (fp->uf_tml_self == NULL)
18007 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18008 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18009 fp->uf_tml_idx = -1;
18010 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18011 || fp->uf_tml_self == NULL)
18012 return; /* out of memory */
18013
18014 fp->uf_profiling = TRUE;
18015}
18016
18017/*
18018 * Dump the profiling results for all functions in file "fd".
18019 */
18020 void
18021func_dump_profile(fd)
18022 FILE *fd;
18023{
18024 hashitem_T *hi;
18025 int todo;
18026 ufunc_T *fp;
18027 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018028 ufunc_T **sorttab;
18029 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018030
18031 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018032 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18033
Bram Moolenaar05159a02005-02-26 23:04:13 +000018034 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18035 {
18036 if (!HASHITEM_EMPTY(hi))
18037 {
18038 --todo;
18039 fp = HI2UF(hi);
18040 if (fp->uf_profiling)
18041 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018042 if (sorttab != NULL)
18043 sorttab[st_len++] = fp;
18044
Bram Moolenaar05159a02005-02-26 23:04:13 +000018045 if (fp->uf_name[0] == K_SPECIAL)
18046 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18047 else
18048 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18049 if (fp->uf_tm_count == 1)
18050 fprintf(fd, "Called 1 time\n");
18051 else
18052 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18053 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18054 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18055 fprintf(fd, "\n");
18056 fprintf(fd, "count total (s) self (s)\n");
18057
18058 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18059 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018060 prof_func_line(fd, fp->uf_tml_count[i],
18061 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018062 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18063 }
18064 fprintf(fd, "\n");
18065 }
18066 }
18067 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018068
18069 if (sorttab != NULL && st_len > 0)
18070 {
18071 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18072 prof_total_cmp);
18073 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18074 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18075 prof_self_cmp);
18076 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18077 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018078}
Bram Moolenaar73830342005-02-28 22:48:19 +000018079
18080 static void
18081prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18082 FILE *fd;
18083 ufunc_T **sorttab;
18084 int st_len;
18085 char *title;
18086 int prefer_self; /* when equal print only self time */
18087{
18088 int i;
18089 ufunc_T *fp;
18090
18091 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18092 fprintf(fd, "count total (s) self (s) function\n");
18093 for (i = 0; i < 20 && i < st_len; ++i)
18094 {
18095 fp = sorttab[i];
18096 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18097 prefer_self);
18098 if (fp->uf_name[0] == K_SPECIAL)
18099 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18100 else
18101 fprintf(fd, " %s()\n", fp->uf_name);
18102 }
18103 fprintf(fd, "\n");
18104}
18105
18106/*
18107 * Print the count and times for one function or function line.
18108 */
18109 static void
18110prof_func_line(fd, count, total, self, prefer_self)
18111 FILE *fd;
18112 int count;
18113 proftime_T *total;
18114 proftime_T *self;
18115 int prefer_self; /* when equal print only self time */
18116{
18117 if (count > 0)
18118 {
18119 fprintf(fd, "%5d ", count);
18120 if (prefer_self && profile_equal(total, self))
18121 fprintf(fd, " ");
18122 else
18123 fprintf(fd, "%s ", profile_msg(total));
18124 if (!prefer_self && profile_equal(total, self))
18125 fprintf(fd, " ");
18126 else
18127 fprintf(fd, "%s ", profile_msg(self));
18128 }
18129 else
18130 fprintf(fd, " ");
18131}
18132
18133/*
18134 * Compare function for total time sorting.
18135 */
18136 static int
18137#ifdef __BORLANDC__
18138_RTLENTRYF
18139#endif
18140prof_total_cmp(s1, s2)
18141 const void *s1;
18142 const void *s2;
18143{
18144 ufunc_T *p1, *p2;
18145
18146 p1 = *(ufunc_T **)s1;
18147 p2 = *(ufunc_T **)s2;
18148 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18149}
18150
18151/*
18152 * Compare function for self time sorting.
18153 */
18154 static int
18155#ifdef __BORLANDC__
18156_RTLENTRYF
18157#endif
18158prof_self_cmp(s1, s2)
18159 const void *s1;
18160 const void *s2;
18161{
18162 ufunc_T *p1, *p2;
18163
18164 p1 = *(ufunc_T **)s1;
18165 p2 = *(ufunc_T **)s2;
18166 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18167}
18168
Bram Moolenaar05159a02005-02-26 23:04:13 +000018169#endif
18170
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018171/* The names of packages that once were loaded is remembered. */
18172static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18173
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018174/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018175 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018176 * Return TRUE if a package was loaded.
18177 */
18178 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018179script_autoload(name, reload)
18180 char_u *name;
18181 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018182{
18183 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018184 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018185 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018186 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018187
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018188 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018189 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018190 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018191 return FALSE;
18192
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018193 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018194
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018195 /* Find the name in the list of previously loaded package names. Skip
18196 * "autoload/", it's always the same. */
18197 for (i = 0; i < ga_loaded.ga_len; ++i)
18198 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18199 break;
18200 if (!reload && i < ga_loaded.ga_len)
18201 ret = FALSE; /* was loaded already */
18202 else
18203 {
18204 /* Remember the name if it wasn't loaded already. */
18205 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18206 {
18207 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18208 tofree = NULL;
18209 }
18210
18211 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018212 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018213 ret = TRUE;
18214 }
18215
18216 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018217 return ret;
18218}
18219
18220/*
18221 * Return the autoload script name for a function or variable name.
18222 * Returns NULL when out of memory.
18223 */
18224 static char_u *
18225autoload_name(name)
18226 char_u *name;
18227{
18228 char_u *p;
18229 char_u *scriptname;
18230
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018231 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018232 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18233 if (scriptname == NULL)
18234 return FALSE;
18235 STRCPY(scriptname, "autoload/");
18236 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018237 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018238 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018239 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018240 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018241 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018242}
18243
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18245
18246/*
18247 * Function given to ExpandGeneric() to obtain the list of user defined
18248 * function names.
18249 */
18250 char_u *
18251get_user_func_name(xp, idx)
18252 expand_T *xp;
18253 int idx;
18254{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018255 static long_u done;
18256 static hashitem_T *hi;
18257 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258
18259 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018261 done = 0;
18262 hi = func_hashtab.ht_array;
18263 }
18264 if (done < func_hashtab.ht_used)
18265 {
18266 if (done++ > 0)
18267 ++hi;
18268 while (HASHITEM_EMPTY(hi))
18269 ++hi;
18270 fp = HI2UF(hi);
18271
18272 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18273 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274
18275 cat_func_name(IObuff, fp);
18276 if (xp->xp_context != EXPAND_USER_FUNC)
18277 {
18278 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018279 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280 STRCAT(IObuff, ")");
18281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282 return IObuff;
18283 }
18284 return NULL;
18285}
18286
18287#endif /* FEAT_CMDL_COMPL */
18288
18289/*
18290 * Copy the function name of "fp" to buffer "buf".
18291 * "buf" must be able to hold the function name plus three bytes.
18292 * Takes care of script-local function names.
18293 */
18294 static void
18295cat_func_name(buf, fp)
18296 char_u *buf;
18297 ufunc_T *fp;
18298{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018299 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300 {
18301 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018302 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 }
18304 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018305 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306}
18307
18308/*
18309 * ":delfunction {name}"
18310 */
18311 void
18312ex_delfunction(eap)
18313 exarg_T *eap;
18314{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018315 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 char_u *p;
18317 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018318 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319
18320 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018321 name = trans_function_name(&p, eap->skip, 0, &fudi);
18322 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018324 {
18325 if (fudi.fd_dict != NULL && !eap->skip)
18326 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329 if (!ends_excmd(*skipwhite(p)))
18330 {
18331 vim_free(name);
18332 EMSG(_(e_trailing));
18333 return;
18334 }
18335 eap->nextcmd = check_nextcmd(p);
18336 if (eap->nextcmd != NULL)
18337 *p = NUL;
18338
18339 if (!eap->skip)
18340 fp = find_func(name);
18341 vim_free(name);
18342
18343 if (!eap->skip)
18344 {
18345 if (fp == NULL)
18346 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018347 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348 return;
18349 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018350 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351 {
18352 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18353 return;
18354 }
18355
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018356 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018358 /* Delete the dict item that refers to the function, it will
18359 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018360 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018362 else
18363 func_free(fp);
18364 }
18365}
18366
18367/*
18368 * Free a function and remove it from the list of functions.
18369 */
18370 static void
18371func_free(fp)
18372 ufunc_T *fp;
18373{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018374 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018375
18376 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018377 ga_clear_strings(&(fp->uf_args));
18378 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018379#ifdef FEAT_PROFILE
18380 vim_free(fp->uf_tml_count);
18381 vim_free(fp->uf_tml_total);
18382 vim_free(fp->uf_tml_self);
18383#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018384
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018385 /* remove the function from the function hashtable */
18386 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18387 if (HASHITEM_EMPTY(hi))
18388 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018389 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018390 hash_remove(&func_hashtab, hi);
18391
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018392 vim_free(fp);
18393}
18394
18395/*
18396 * Unreference a Function: decrement the reference count and free it when it
18397 * becomes zero. Only for numbered functions.
18398 */
18399 static void
18400func_unref(name)
18401 char_u *name;
18402{
18403 ufunc_T *fp;
18404
18405 if (name != NULL && isdigit(*name))
18406 {
18407 fp = find_func(name);
18408 if (fp == NULL)
18409 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018410 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018411 {
18412 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018413 * when "uf_calls" becomes zero. */
18414 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018415 func_free(fp);
18416 }
18417 }
18418}
18419
18420/*
18421 * Count a reference to a Function.
18422 */
18423 static void
18424func_ref(name)
18425 char_u *name;
18426{
18427 ufunc_T *fp;
18428
18429 if (name != NULL && isdigit(*name))
18430 {
18431 fp = find_func(name);
18432 if (fp == NULL)
18433 EMSG2(_(e_intern2), "func_ref()");
18434 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018435 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436 }
18437}
18438
18439/*
18440 * Call a user function.
18441 */
18442 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018443call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444 ufunc_T *fp; /* pointer to function */
18445 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018446 typval_T *argvars; /* arguments */
18447 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 linenr_T firstline; /* first line of range */
18449 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018450 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451{
Bram Moolenaar33570922005-01-25 22:26:29 +000018452 char_u *save_sourcing_name;
18453 linenr_T save_sourcing_lnum;
18454 scid_T save_current_SID;
18455 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018456 int save_did_emsg;
18457 static int depth = 0;
18458 dictitem_T *v;
18459 int fixvar_idx = 0; /* index in fixvar[] */
18460 int i;
18461 int ai;
18462 char_u numbuf[NUMBUFLEN];
18463 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018464#ifdef FEAT_PROFILE
18465 proftime_T wait_start;
18466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467
18468 /* If depth of calling is getting too high, don't execute the function */
18469 if (depth >= p_mfd)
18470 {
18471 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018472 rettv->v_type = VAR_NUMBER;
18473 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474 return;
18475 }
18476 ++depth;
18477
18478 line_breakcheck(); /* check for CTRL-C hit */
18479
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018480 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018481 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018483 fc.rettv = rettv;
18484 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 fc.linenr = 0;
18486 fc.returned = FALSE;
18487 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018489 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490 fc.dbg_tick = debug_tick;
18491
Bram Moolenaar33570922005-01-25 22:26:29 +000018492 /*
18493 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18494 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18495 * each argument variable and saves a lot of time.
18496 */
18497 /*
18498 * Init l: variables.
18499 */
18500 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018501 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018502 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018503 /* Set l:self to "selfdict". */
18504 v = &fc.fixvar[fixvar_idx++].var;
18505 STRCPY(v->di_key, "self");
18506 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18507 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18508 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018509 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018510 v->di_tv.vval.v_dict = selfdict;
18511 ++selfdict->dv_refcount;
18512 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018513
Bram Moolenaar33570922005-01-25 22:26:29 +000018514 /*
18515 * Init a: variables.
18516 * Set a:0 to "argcount".
18517 * Set a:000 to a list with room for the "..." arguments.
18518 */
18519 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18520 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018521 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018522 v = &fc.fixvar[fixvar_idx++].var;
18523 STRCPY(v->di_key, "000");
18524 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18525 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18526 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018527 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018528 v->di_tv.vval.v_list = &fc.l_varlist;
18529 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18530 fc.l_varlist.lv_refcount = 99999;
18531
18532 /*
18533 * Set a:firstline to "firstline" and a:lastline to "lastline".
18534 * Set a:name to named arguments.
18535 * Set a:N to the "..." arguments.
18536 */
18537 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18538 (varnumber_T)firstline);
18539 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18540 (varnumber_T)lastline);
18541 for (i = 0; i < argcount; ++i)
18542 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018543 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018544 if (ai < 0)
18545 /* named argument a:name */
18546 name = FUNCARG(fp, i);
18547 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018548 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018549 /* "..." argument a:1, a:2, etc. */
18550 sprintf((char *)numbuf, "%d", ai + 1);
18551 name = numbuf;
18552 }
18553 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18554 {
18555 v = &fc.fixvar[fixvar_idx++].var;
18556 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18557 }
18558 else
18559 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018560 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18561 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018562 if (v == NULL)
18563 break;
18564 v->di_flags = DI_FLAGS_RO;
18565 }
18566 STRCPY(v->di_key, name);
18567 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18568
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018569 /* Note: the values are copied directly to avoid alloc/free.
18570 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018571 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018572 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018573
18574 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18575 {
18576 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18577 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018578 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018579 }
18580 }
18581
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 /* Don't redraw while executing the function. */
18583 ++RedrawingDisabled;
18584 save_sourcing_name = sourcing_name;
18585 save_sourcing_lnum = sourcing_lnum;
18586 sourcing_lnum = 1;
18587 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018588 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589 if (sourcing_name != NULL)
18590 {
18591 if (save_sourcing_name != NULL
18592 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18593 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18594 else
18595 STRCPY(sourcing_name, "function ");
18596 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18597
18598 if (p_verbose >= 12)
18599 {
18600 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018601 verbose_enter_scroll();
18602
Bram Moolenaar555b2802005-05-19 21:08:39 +000018603 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604 if (p_verbose >= 14)
18605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018607 char_u numbuf[NUMBUFLEN];
18608 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609
18610 msg_puts((char_u *)"(");
18611 for (i = 0; i < argcount; ++i)
18612 {
18613 if (i > 0)
18614 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018615 if (argvars[i].v_type == VAR_NUMBER)
18616 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018617 else
18618 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018619 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018620 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018622 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 }
18624 }
18625 msg_puts((char_u *)")");
18626 }
18627 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018628
18629 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 --no_wait_return;
18631 }
18632 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018633#ifdef FEAT_PROFILE
18634 if (do_profiling)
18635 {
18636 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18637 func_do_profile(fp);
18638 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018639 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018640 {
18641 ++fp->uf_tm_count;
18642 profile_start(&fp->uf_tm_start);
18643 profile_zero(&fp->uf_tm_children);
18644 }
18645 script_prof_save(&wait_start);
18646 }
18647#endif
18648
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018650 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651 save_did_emsg = did_emsg;
18652 did_emsg = FALSE;
18653
18654 /* call do_cmdline() to execute the lines */
18655 do_cmdline(NULL, get_func_line, (void *)&fc,
18656 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18657
18658 --RedrawingDisabled;
18659
18660 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018661 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018663 clear_tv(rettv);
18664 rettv->v_type = VAR_NUMBER;
18665 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666 }
18667
Bram Moolenaar05159a02005-02-26 23:04:13 +000018668#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018669 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018670 {
18671 profile_end(&fp->uf_tm_start);
18672 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18673 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18674 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18675 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018676 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018677 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018678 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18679 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018680 }
18681 }
18682#endif
18683
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 /* when being verbose, mention the return value */
18685 if (p_verbose >= 12)
18686 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018688 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018691 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018692 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018693 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18694 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018695 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018697 char_u buf[MSG_BUF_LEN];
18698 char_u numbuf[NUMBUFLEN];
18699 char_u *tofree;
18700
Bram Moolenaar555b2802005-05-19 21:08:39 +000018701 /* The value may be very long. Skip the middle part, so that we
18702 * have some idea how it starts and ends. smsg() would always
18703 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018704 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018705 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018706 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018707 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708 }
18709 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018710
18711 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712 --no_wait_return;
18713 }
18714
18715 vim_free(sourcing_name);
18716 sourcing_name = save_sourcing_name;
18717 sourcing_lnum = save_sourcing_lnum;
18718 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018719#ifdef FEAT_PROFILE
18720 if (do_profiling)
18721 script_prof_restore(&wait_start);
18722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723
18724 if (p_verbose >= 12 && sourcing_name != NULL)
18725 {
18726 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018727 verbose_enter_scroll();
18728
Bram Moolenaar555b2802005-05-19 21:08:39 +000018729 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018731
18732 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733 --no_wait_return;
18734 }
18735
18736 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018737 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738
Bram Moolenaar33570922005-01-25 22:26:29 +000018739 /* The a: variables typevals were not alloced, only free the allocated
18740 * variables. */
18741 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18742
18743 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 --depth;
18745}
18746
18747/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018748 * Add a number variable "name" to dict "dp" with value "nr".
18749 */
18750 static void
18751add_nr_var(dp, v, name, nr)
18752 dict_T *dp;
18753 dictitem_T *v;
18754 char *name;
18755 varnumber_T nr;
18756{
18757 STRCPY(v->di_key, name);
18758 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18759 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18760 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018761 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018762 v->di_tv.vval.v_number = nr;
18763}
18764
18765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 * ":return [expr]"
18767 */
18768 void
18769ex_return(eap)
18770 exarg_T *eap;
18771{
18772 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018773 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774 int returning = FALSE;
18775
18776 if (current_funccal == NULL)
18777 {
18778 EMSG(_("E133: :return not inside a function"));
18779 return;
18780 }
18781
18782 if (eap->skip)
18783 ++emsg_skip;
18784
18785 eap->nextcmd = NULL;
18786 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018787 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 {
18789 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018790 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018792 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793 }
18794 /* It's safer to return also on error. */
18795 else if (!eap->skip)
18796 {
18797 /*
18798 * Return unless the expression evaluation has been cancelled due to an
18799 * aborting error, an interrupt, or an exception.
18800 */
18801 if (!aborting())
18802 returning = do_return(eap, FALSE, TRUE, NULL);
18803 }
18804
18805 /* When skipping or the return gets pending, advance to the next command
18806 * in this line (!returning). Otherwise, ignore the rest of the line.
18807 * Following lines will be ignored by get_func_line(). */
18808 if (returning)
18809 eap->nextcmd = NULL;
18810 else if (eap->nextcmd == NULL) /* no argument */
18811 eap->nextcmd = check_nextcmd(arg);
18812
18813 if (eap->skip)
18814 --emsg_skip;
18815}
18816
18817/*
18818 * Return from a function. Possibly makes the return pending. Also called
18819 * for a pending return at the ":endtry" or after returning from an extra
18820 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018821 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018822 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 * FALSE when the return gets pending.
18824 */
18825 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018826do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827 exarg_T *eap;
18828 int reanimate;
18829 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018830 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831{
18832 int idx;
18833 struct condstack *cstack = eap->cstack;
18834
18835 if (reanimate)
18836 /* Undo the return. */
18837 current_funccal->returned = FALSE;
18838
18839 /*
18840 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18841 * not in its finally clause (which then is to be executed next) is found.
18842 * In this case, make the ":return" pending for execution at the ":endtry".
18843 * Otherwise, return normally.
18844 */
18845 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18846 if (idx >= 0)
18847 {
18848 cstack->cs_pending[idx] = CSTP_RETURN;
18849
18850 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018851 /* A pending return again gets pending. "rettv" points to an
18852 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018854 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855 else
18856 {
18857 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018858 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018860 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018862 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863 {
18864 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018865 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018866 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867 else
18868 EMSG(_(e_outofmem));
18869 }
18870 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018871 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872
18873 if (reanimate)
18874 {
18875 /* The pending return value could be overwritten by a ":return"
18876 * without argument in a finally clause; reset the default
18877 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018878 current_funccal->rettv->v_type = VAR_NUMBER;
18879 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 }
18881 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018882 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883 }
18884 else
18885 {
18886 current_funccal->returned = TRUE;
18887
18888 /* If the return is carried out now, store the return value. For
18889 * a return immediately after reanimation, the value is already
18890 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018891 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018893 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018894 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018896 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897 }
18898 }
18899
18900 return idx < 0;
18901}
18902
18903/*
18904 * Free the variable with a pending return value.
18905 */
18906 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018907discard_pending_return(rettv)
18908 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909{
Bram Moolenaar33570922005-01-25 22:26:29 +000018910 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911}
18912
18913/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018914 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 * is an allocated string. Used by report_pending() for verbose messages.
18916 */
18917 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018918get_return_cmd(rettv)
18919 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018921 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018922 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018923 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018925 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018926 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018927 if (s == NULL)
18928 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018929
18930 STRCPY(IObuff, ":return ");
18931 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18932 if (STRLEN(s) + 8 >= IOSIZE)
18933 STRCPY(IObuff + IOSIZE - 4, "...");
18934 vim_free(tofree);
18935 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936}
18937
18938/*
18939 * Get next function line.
18940 * Called by do_cmdline() to get the next line.
18941 * Returns allocated string, or NULL for end of function.
18942 */
18943/* ARGSUSED */
18944 char_u *
18945get_func_line(c, cookie, indent)
18946 int c; /* not used */
18947 void *cookie;
18948 int indent; /* not used */
18949{
Bram Moolenaar33570922005-01-25 22:26:29 +000018950 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018951 ufunc_T *fp = fcp->func;
18952 char_u *retval;
18953 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954
18955 /* If breakpoints have been added/deleted need to check for it. */
18956 if (fcp->dbg_tick != debug_tick)
18957 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018958 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 sourcing_lnum);
18960 fcp->dbg_tick = debug_tick;
18961 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018962#ifdef FEAT_PROFILE
18963 if (do_profiling)
18964 func_line_end(cookie);
18965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966
Bram Moolenaar05159a02005-02-26 23:04:13 +000018967 gap = &fp->uf_lines;
18968 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 retval = NULL;
18970 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18971 retval = NULL;
18972 else
18973 {
18974 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18975 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018976#ifdef FEAT_PROFILE
18977 if (do_profiling)
18978 func_line_start(cookie);
18979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 }
18981
18982 /* Did we encounter a breakpoint? */
18983 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18984 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018985 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018987 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988 sourcing_lnum);
18989 fcp->dbg_tick = debug_tick;
18990 }
18991
18992 return retval;
18993}
18994
Bram Moolenaar05159a02005-02-26 23:04:13 +000018995#if defined(FEAT_PROFILE) || defined(PROTO)
18996/*
18997 * Called when starting to read a function line.
18998 * "sourcing_lnum" must be correct!
18999 * When skipping lines it may not actually be executed, but we won't find out
19000 * until later and we need to store the time now.
19001 */
19002 void
19003func_line_start(cookie)
19004 void *cookie;
19005{
19006 funccall_T *fcp = (funccall_T *)cookie;
19007 ufunc_T *fp = fcp->func;
19008
19009 if (fp->uf_profiling && sourcing_lnum >= 1
19010 && sourcing_lnum <= fp->uf_lines.ga_len)
19011 {
19012 fp->uf_tml_idx = sourcing_lnum - 1;
19013 fp->uf_tml_execed = FALSE;
19014 profile_start(&fp->uf_tml_start);
19015 profile_zero(&fp->uf_tml_children);
19016 profile_get_wait(&fp->uf_tml_wait);
19017 }
19018}
19019
19020/*
19021 * Called when actually executing a function line.
19022 */
19023 void
19024func_line_exec(cookie)
19025 void *cookie;
19026{
19027 funccall_T *fcp = (funccall_T *)cookie;
19028 ufunc_T *fp = fcp->func;
19029
19030 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19031 fp->uf_tml_execed = TRUE;
19032}
19033
19034/*
19035 * Called when done with a function line.
19036 */
19037 void
19038func_line_end(cookie)
19039 void *cookie;
19040{
19041 funccall_T *fcp = (funccall_T *)cookie;
19042 ufunc_T *fp = fcp->func;
19043
19044 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19045 {
19046 if (fp->uf_tml_execed)
19047 {
19048 ++fp->uf_tml_count[fp->uf_tml_idx];
19049 profile_end(&fp->uf_tml_start);
19050 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
19051 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
19052 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
19053 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
19054 }
19055 fp->uf_tml_idx = -1;
19056 }
19057}
19058#endif
19059
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060/*
19061 * Return TRUE if the currently active function should be ended, because a
19062 * return was encountered or an error occured. Used inside a ":while".
19063 */
19064 int
19065func_has_ended(cookie)
19066 void *cookie;
19067{
Bram Moolenaar33570922005-01-25 22:26:29 +000019068 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069
19070 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19071 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019072 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073 || fcp->returned);
19074}
19075
19076/*
19077 * return TRUE if cookie indicates a function which "abort"s on errors.
19078 */
19079 int
19080func_has_abort(cookie)
19081 void *cookie;
19082{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019083 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084}
19085
19086#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19087typedef enum
19088{
19089 VAR_FLAVOUR_DEFAULT,
19090 VAR_FLAVOUR_SESSION,
19091 VAR_FLAVOUR_VIMINFO
19092} var_flavour_T;
19093
19094static var_flavour_T var_flavour __ARGS((char_u *varname));
19095
19096 static var_flavour_T
19097var_flavour(varname)
19098 char_u *varname;
19099{
19100 char_u *p = varname;
19101
19102 if (ASCII_ISUPPER(*p))
19103 {
19104 while (*(++p))
19105 if (ASCII_ISLOWER(*p))
19106 return VAR_FLAVOUR_SESSION;
19107 return VAR_FLAVOUR_VIMINFO;
19108 }
19109 else
19110 return VAR_FLAVOUR_DEFAULT;
19111}
19112#endif
19113
19114#if defined(FEAT_VIMINFO) || defined(PROTO)
19115/*
19116 * Restore global vars that start with a capital from the viminfo file
19117 */
19118 int
19119read_viminfo_varlist(virp, writing)
19120 vir_T *virp;
19121 int writing;
19122{
19123 char_u *tab;
19124 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019125 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126
19127 if (!writing && (find_viminfo_parameter('!') != NULL))
19128 {
19129 tab = vim_strchr(virp->vir_line + 1, '\t');
19130 if (tab != NULL)
19131 {
19132 *tab++ = '\0'; /* isolate the variable name */
19133 if (*tab == 'S') /* string var */
19134 is_string = TRUE;
19135
19136 tab = vim_strchr(tab, '\t');
19137 if (tab != NULL)
19138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139 if (is_string)
19140 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019141 tv.v_type = VAR_STRING;
19142 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 }
19145 else
19146 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019147 tv.v_type = VAR_NUMBER;
19148 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019149 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019150 set_var(virp->vir_line + 1, &tv, FALSE);
19151 if (is_string)
19152 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 }
19154 }
19155 }
19156
19157 return viminfo_readline(virp);
19158}
19159
19160/*
19161 * Write global vars that start with a capital to the viminfo file
19162 */
19163 void
19164write_viminfo_varlist(fp)
19165 FILE *fp;
19166{
Bram Moolenaar33570922005-01-25 22:26:29 +000019167 hashitem_T *hi;
19168 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019169 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019170 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019171 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019172 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019173 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174
19175 if (find_viminfo_parameter('!') == NULL)
19176 return;
19177
19178 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019179
Bram Moolenaar33570922005-01-25 22:26:29 +000019180 todo = globvarht.ht_used;
19181 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019183 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019185 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019186 this_var = HI2DI(hi);
19187 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019188 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019189 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019190 {
19191 case VAR_STRING: s = "STR"; break;
19192 case VAR_NUMBER: s = "NUM"; break;
19193 default: continue;
19194 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019195 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019196 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019197 if (p != NULL)
19198 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019199 vim_free(tofree);
19200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201 }
19202 }
19203}
19204#endif
19205
19206#if defined(FEAT_SESSION) || defined(PROTO)
19207 int
19208store_session_globals(fd)
19209 FILE *fd;
19210{
Bram Moolenaar33570922005-01-25 22:26:29 +000019211 hashitem_T *hi;
19212 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019213 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214 char_u *p, *t;
19215
Bram Moolenaar33570922005-01-25 22:26:29 +000019216 todo = globvarht.ht_used;
19217 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019219 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019221 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019222 this_var = HI2DI(hi);
19223 if ((this_var->di_tv.v_type == VAR_NUMBER
19224 || this_var->di_tv.v_type == VAR_STRING)
19225 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019226 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019227 /* Escape special characters with a backslash. Turn a LF and
19228 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019229 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019230 (char_u *)"\\\"\n\r");
19231 if (p == NULL) /* out of memory */
19232 break;
19233 for (t = p; *t != NUL; ++t)
19234 if (*t == '\n')
19235 *t = 'n';
19236 else if (*t == '\r')
19237 *t = 'r';
19238 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019239 this_var->di_key,
19240 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19241 : ' ',
19242 p,
19243 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19244 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019245 || put_eol(fd) == FAIL)
19246 {
19247 vim_free(p);
19248 return FAIL;
19249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250 vim_free(p);
19251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019252 }
19253 }
19254 return OK;
19255}
19256#endif
19257
Bram Moolenaar661b1822005-07-28 22:36:45 +000019258/*
19259 * Display script name where an item was last set.
19260 * Should only be invoked when 'verbose' is non-zero.
19261 */
19262 void
19263last_set_msg(scriptID)
19264 scid_T scriptID;
19265{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019266 char_u *p;
19267
Bram Moolenaar661b1822005-07-28 22:36:45 +000019268 if (scriptID != 0)
19269 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019270 p = home_replace_save(NULL, get_scriptname(scriptID));
19271 if (p != NULL)
19272 {
19273 verbose_enter();
19274 MSG_PUTS(_("\n\tLast set from "));
19275 MSG_PUTS(p);
19276 vim_free(p);
19277 verbose_leave();
19278 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019279 }
19280}
19281
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282#endif /* FEAT_EVAL */
19283
19284#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19285
19286
19287#ifdef WIN3264
19288/*
19289 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19290 */
19291static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19292static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19293static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19294
19295/*
19296 * Get the short pathname of a file.
19297 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19298 */
19299 static int
19300get_short_pathname(fnamep, bufp, fnamelen)
19301 char_u **fnamep;
19302 char_u **bufp;
19303 int *fnamelen;
19304{
19305 int l,len;
19306 char_u *newbuf;
19307
19308 len = *fnamelen;
19309
19310 l = GetShortPathName(*fnamep, *fnamep, len);
19311 if (l > len - 1)
19312 {
19313 /* If that doesn't work (not enough space), then save the string
19314 * and try again with a new buffer big enough
19315 */
19316 newbuf = vim_strnsave(*fnamep, l);
19317 if (newbuf == NULL)
19318 return 0;
19319
19320 vim_free(*bufp);
19321 *fnamep = *bufp = newbuf;
19322
19323 l = GetShortPathName(*fnamep,*fnamep,l+1);
19324
19325 /* Really should always succeed, as the buffer is big enough */
19326 }
19327
19328 *fnamelen = l;
19329 return 1;
19330}
19331
19332/*
19333 * Create a short path name. Returns the length of the buffer it needs.
19334 * Doesn't copy over the end of the buffer passed in.
19335 */
19336 static int
19337shortpath_for_invalid_fname(fname, bufp, fnamelen)
19338 char_u **fname;
19339 char_u **bufp;
19340 int *fnamelen;
19341{
19342 char_u *s, *p, *pbuf2, *pbuf3;
19343 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019344 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345
19346 /* Make a copy */
19347 len2 = *fnamelen;
19348 pbuf2 = vim_strnsave(*fname, len2);
19349 pbuf3 = NULL;
19350
19351 s = pbuf2 + len2 - 1; /* Find the end */
19352 slen = 1;
19353 plen = len2;
19354
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019355 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019356 {
19357 --s;
19358 ++slen;
19359 --plen;
19360 }
19361
19362 do
19363 {
19364 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019365 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366 {
19367 --s;
19368 ++slen;
19369 --plen;
19370 }
19371 if (s <= pbuf2)
19372 break;
19373
19374 /* Remeber the character that is about to be blatted */
19375 ch = *s;
19376 *s = 0; /* get_short_pathname requires a null-terminated string */
19377
19378 /* Try it in situ */
19379 p = pbuf2;
19380 if (!get_short_pathname(&p, &pbuf3, &plen))
19381 {
19382 vim_free(pbuf2);
19383 return -1;
19384 }
19385 *s = ch; /* Preserve the string */
19386 } while (plen == 0);
19387
19388 if (plen > 0)
19389 {
19390 /* Remeber the length of the new string. */
19391 *fnamelen = len = plen + slen;
19392 vim_free(*bufp);
19393 if (len > len2)
19394 {
19395 /* If there's not enough space in the currently allocated string,
19396 * then copy it to a buffer big enough.
19397 */
19398 *fname= *bufp = vim_strnsave(p, len);
19399 if (*fname == NULL)
19400 return -1;
19401 }
19402 else
19403 {
19404 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19405 *fname = *bufp = pbuf2;
19406 if (p != pbuf2)
19407 strncpy(*fname, p, plen);
19408 pbuf2 = NULL;
19409 }
19410 /* Concat the next bit */
19411 strncpy(*fname + plen, s, slen);
19412 (*fname)[len] = '\0';
19413 }
19414 vim_free(pbuf3);
19415 vim_free(pbuf2);
19416 return 0;
19417}
19418
19419/*
19420 * Get a pathname for a partial path.
19421 */
19422 static int
19423shortpath_for_partial(fnamep, bufp, fnamelen)
19424 char_u **fnamep;
19425 char_u **bufp;
19426 int *fnamelen;
19427{
19428 int sepcount, len, tflen;
19429 char_u *p;
19430 char_u *pbuf, *tfname;
19431 int hasTilde;
19432
19433 /* Count up the path seperators from the RHS.. so we know which part
19434 * of the path to return.
19435 */
19436 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019437 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438 if (vim_ispathsep(*p))
19439 ++sepcount;
19440
19441 /* Need full path first (use expand_env() to remove a "~/") */
19442 hasTilde = (**fnamep == '~');
19443 if (hasTilde)
19444 pbuf = tfname = expand_env_save(*fnamep);
19445 else
19446 pbuf = tfname = FullName_save(*fnamep, FALSE);
19447
19448 len = tflen = STRLEN(tfname);
19449
19450 if (!get_short_pathname(&tfname, &pbuf, &len))
19451 return -1;
19452
19453 if (len == 0)
19454 {
19455 /* Don't have a valid filename, so shorten the rest of the
19456 * path if we can. This CAN give us invalid 8.3 filenames, but
19457 * there's not a lot of point in guessing what it might be.
19458 */
19459 len = tflen;
19460 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19461 return -1;
19462 }
19463
19464 /* Count the paths backward to find the beginning of the desired string. */
19465 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019466 {
19467#ifdef FEAT_MBYTE
19468 if (has_mbyte)
19469 p -= mb_head_off(tfname, p);
19470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471 if (vim_ispathsep(*p))
19472 {
19473 if (sepcount == 0 || (hasTilde && sepcount == 1))
19474 break;
19475 else
19476 sepcount --;
19477 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479 if (hasTilde)
19480 {
19481 --p;
19482 if (p >= tfname)
19483 *p = '~';
19484 else
19485 return -1;
19486 }
19487 else
19488 ++p;
19489
19490 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19491 vim_free(*bufp);
19492 *fnamelen = (int)STRLEN(p);
19493 *bufp = pbuf;
19494 *fnamep = p;
19495
19496 return 0;
19497}
19498#endif /* WIN3264 */
19499
19500/*
19501 * Adjust a filename, according to a string of modifiers.
19502 * *fnamep must be NUL terminated when called. When returning, the length is
19503 * determined by *fnamelen.
19504 * Returns valid flags.
19505 * When there is an error, *fnamep is set to NULL.
19506 */
19507 int
19508modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19509 char_u *src; /* string with modifiers */
19510 int *usedlen; /* characters after src that are used */
19511 char_u **fnamep; /* file name so far */
19512 char_u **bufp; /* buffer for allocated file name or NULL */
19513 int *fnamelen; /* length of fnamep */
19514{
19515 int valid = 0;
19516 char_u *tail;
19517 char_u *s, *p, *pbuf;
19518 char_u dirname[MAXPATHL];
19519 int c;
19520 int has_fullname = 0;
19521#ifdef WIN3264
19522 int has_shortname = 0;
19523#endif
19524
19525repeat:
19526 /* ":p" - full path/file_name */
19527 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19528 {
19529 has_fullname = 1;
19530
19531 valid |= VALID_PATH;
19532 *usedlen += 2;
19533
19534 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19535 if ((*fnamep)[0] == '~'
19536#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19537 && ((*fnamep)[1] == '/'
19538# ifdef BACKSLASH_IN_FILENAME
19539 || (*fnamep)[1] == '\\'
19540# endif
19541 || (*fnamep)[1] == NUL)
19542
19543#endif
19544 )
19545 {
19546 *fnamep = expand_env_save(*fnamep);
19547 vim_free(*bufp); /* free any allocated file name */
19548 *bufp = *fnamep;
19549 if (*fnamep == NULL)
19550 return -1;
19551 }
19552
19553 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019554 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 {
19556 if (vim_ispathsep(*p)
19557 && p[1] == '.'
19558 && (p[2] == NUL
19559 || vim_ispathsep(p[2])
19560 || (p[2] == '.'
19561 && (p[3] == NUL || vim_ispathsep(p[3])))))
19562 break;
19563 }
19564
19565 /* FullName_save() is slow, don't use it when not needed. */
19566 if (*p != NUL || !vim_isAbsName(*fnamep))
19567 {
19568 *fnamep = FullName_save(*fnamep, *p != NUL);
19569 vim_free(*bufp); /* free any allocated file name */
19570 *bufp = *fnamep;
19571 if (*fnamep == NULL)
19572 return -1;
19573 }
19574
19575 /* Append a path separator to a directory. */
19576 if (mch_isdir(*fnamep))
19577 {
19578 /* Make room for one or two extra characters. */
19579 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19580 vim_free(*bufp); /* free any allocated file name */
19581 *bufp = *fnamep;
19582 if (*fnamep == NULL)
19583 return -1;
19584 add_pathsep(*fnamep);
19585 }
19586 }
19587
19588 /* ":." - path relative to the current directory */
19589 /* ":~" - path relative to the home directory */
19590 /* ":8" - shortname path - postponed till after */
19591 while (src[*usedlen] == ':'
19592 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19593 {
19594 *usedlen += 2;
19595 if (c == '8')
19596 {
19597#ifdef WIN3264
19598 has_shortname = 1; /* Postpone this. */
19599#endif
19600 continue;
19601 }
19602 pbuf = NULL;
19603 /* Need full path first (use expand_env() to remove a "~/") */
19604 if (!has_fullname)
19605 {
19606 if (c == '.' && **fnamep == '~')
19607 p = pbuf = expand_env_save(*fnamep);
19608 else
19609 p = pbuf = FullName_save(*fnamep, FALSE);
19610 }
19611 else
19612 p = *fnamep;
19613
19614 has_fullname = 0;
19615
19616 if (p != NULL)
19617 {
19618 if (c == '.')
19619 {
19620 mch_dirname(dirname, MAXPATHL);
19621 s = shorten_fname(p, dirname);
19622 if (s != NULL)
19623 {
19624 *fnamep = s;
19625 if (pbuf != NULL)
19626 {
19627 vim_free(*bufp); /* free any allocated file name */
19628 *bufp = pbuf;
19629 pbuf = NULL;
19630 }
19631 }
19632 }
19633 else
19634 {
19635 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19636 /* Only replace it when it starts with '~' */
19637 if (*dirname == '~')
19638 {
19639 s = vim_strsave(dirname);
19640 if (s != NULL)
19641 {
19642 *fnamep = s;
19643 vim_free(*bufp);
19644 *bufp = s;
19645 }
19646 }
19647 }
19648 vim_free(pbuf);
19649 }
19650 }
19651
19652 tail = gettail(*fnamep);
19653 *fnamelen = (int)STRLEN(*fnamep);
19654
19655 /* ":h" - head, remove "/file_name", can be repeated */
19656 /* Don't remove the first "/" or "c:\" */
19657 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19658 {
19659 valid |= VALID_HEAD;
19660 *usedlen += 2;
19661 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019662 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663 --tail;
19664 *fnamelen = (int)(tail - *fnamep);
19665#ifdef VMS
19666 if (*fnamelen > 0)
19667 *fnamelen += 1; /* the path separator is part of the path */
19668#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019669 while (tail > s && !after_pathsep(s, tail))
19670 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 }
19672
19673 /* ":8" - shortname */
19674 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19675 {
19676 *usedlen += 2;
19677#ifdef WIN3264
19678 has_shortname = 1;
19679#endif
19680 }
19681
19682#ifdef WIN3264
19683 /* Check shortname after we have done 'heads' and before we do 'tails'
19684 */
19685 if (has_shortname)
19686 {
19687 pbuf = NULL;
19688 /* Copy the string if it is shortened by :h */
19689 if (*fnamelen < (int)STRLEN(*fnamep))
19690 {
19691 p = vim_strnsave(*fnamep, *fnamelen);
19692 if (p == 0)
19693 return -1;
19694 vim_free(*bufp);
19695 *bufp = *fnamep = p;
19696 }
19697
19698 /* Split into two implementations - makes it easier. First is where
19699 * there isn't a full name already, second is where there is.
19700 */
19701 if (!has_fullname && !vim_isAbsName(*fnamep))
19702 {
19703 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19704 return -1;
19705 }
19706 else
19707 {
19708 int l;
19709
19710 /* Simple case, already have the full-name
19711 * Nearly always shorter, so try first time. */
19712 l = *fnamelen;
19713 if (!get_short_pathname(fnamep, bufp, &l))
19714 return -1;
19715
19716 if (l == 0)
19717 {
19718 /* Couldn't find the filename.. search the paths.
19719 */
19720 l = *fnamelen;
19721 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19722 return -1;
19723 }
19724 *fnamelen = l;
19725 }
19726 }
19727#endif /* WIN3264 */
19728
19729 /* ":t" - tail, just the basename */
19730 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19731 {
19732 *usedlen += 2;
19733 *fnamelen -= (int)(tail - *fnamep);
19734 *fnamep = tail;
19735 }
19736
19737 /* ":e" - extension, can be repeated */
19738 /* ":r" - root, without extension, can be repeated */
19739 while (src[*usedlen] == ':'
19740 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19741 {
19742 /* find a '.' in the tail:
19743 * - for second :e: before the current fname
19744 * - otherwise: The last '.'
19745 */
19746 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19747 s = *fnamep - 2;
19748 else
19749 s = *fnamep + *fnamelen - 1;
19750 for ( ; s > tail; --s)
19751 if (s[0] == '.')
19752 break;
19753 if (src[*usedlen + 1] == 'e') /* :e */
19754 {
19755 if (s > tail)
19756 {
19757 *fnamelen += (int)(*fnamep - (s + 1));
19758 *fnamep = s + 1;
19759#ifdef VMS
19760 /* cut version from the extension */
19761 s = *fnamep + *fnamelen - 1;
19762 for ( ; s > *fnamep; --s)
19763 if (s[0] == ';')
19764 break;
19765 if (s > *fnamep)
19766 *fnamelen = s - *fnamep;
19767#endif
19768 }
19769 else if (*fnamep <= tail)
19770 *fnamelen = 0;
19771 }
19772 else /* :r */
19773 {
19774 if (s > tail) /* remove one extension */
19775 *fnamelen = (int)(s - *fnamep);
19776 }
19777 *usedlen += 2;
19778 }
19779
19780 /* ":s?pat?foo?" - substitute */
19781 /* ":gs?pat?foo?" - global substitute */
19782 if (src[*usedlen] == ':'
19783 && (src[*usedlen + 1] == 's'
19784 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19785 {
19786 char_u *str;
19787 char_u *pat;
19788 char_u *sub;
19789 int sep;
19790 char_u *flags;
19791 int didit = FALSE;
19792
19793 flags = (char_u *)"";
19794 s = src + *usedlen + 2;
19795 if (src[*usedlen + 1] == 'g')
19796 {
19797 flags = (char_u *)"g";
19798 ++s;
19799 }
19800
19801 sep = *s++;
19802 if (sep)
19803 {
19804 /* find end of pattern */
19805 p = vim_strchr(s, sep);
19806 if (p != NULL)
19807 {
19808 pat = vim_strnsave(s, (int)(p - s));
19809 if (pat != NULL)
19810 {
19811 s = p + 1;
19812 /* find end of substitution */
19813 p = vim_strchr(s, sep);
19814 if (p != NULL)
19815 {
19816 sub = vim_strnsave(s, (int)(p - s));
19817 str = vim_strnsave(*fnamep, *fnamelen);
19818 if (sub != NULL && str != NULL)
19819 {
19820 *usedlen = (int)(p + 1 - src);
19821 s = do_string_sub(str, pat, sub, flags);
19822 if (s != NULL)
19823 {
19824 *fnamep = s;
19825 *fnamelen = (int)STRLEN(s);
19826 vim_free(*bufp);
19827 *bufp = s;
19828 didit = TRUE;
19829 }
19830 }
19831 vim_free(sub);
19832 vim_free(str);
19833 }
19834 vim_free(pat);
19835 }
19836 }
19837 /* after using ":s", repeat all the modifiers */
19838 if (didit)
19839 goto repeat;
19840 }
19841 }
19842
19843 return valid;
19844}
19845
19846/*
19847 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19848 * "flags" can be "g" to do a global substitute.
19849 * Returns an allocated string, NULL for error.
19850 */
19851 char_u *
19852do_string_sub(str, pat, sub, flags)
19853 char_u *str;
19854 char_u *pat;
19855 char_u *sub;
19856 char_u *flags;
19857{
19858 int sublen;
19859 regmatch_T regmatch;
19860 int i;
19861 int do_all;
19862 char_u *tail;
19863 garray_T ga;
19864 char_u *ret;
19865 char_u *save_cpo;
19866
19867 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19868 save_cpo = p_cpo;
19869 p_cpo = (char_u *)"";
19870
19871 ga_init2(&ga, 1, 200);
19872
19873 do_all = (flags[0] == 'g');
19874
19875 regmatch.rm_ic = p_ic;
19876 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19877 if (regmatch.regprog != NULL)
19878 {
19879 tail = str;
19880 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19881 {
19882 /*
19883 * Get some space for a temporary buffer to do the substitution
19884 * into. It will contain:
19885 * - The text up to where the match is.
19886 * - The substituted text.
19887 * - The text after the match.
19888 */
19889 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19890 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19891 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19892 {
19893 ga_clear(&ga);
19894 break;
19895 }
19896
19897 /* copy the text up to where the match is */
19898 i = (int)(regmatch.startp[0] - tail);
19899 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19900 /* add the substituted text */
19901 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19902 + ga.ga_len + i, TRUE, TRUE, FALSE);
19903 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 /* avoid getting stuck on a match with an empty string */
19905 if (tail == regmatch.endp[0])
19906 {
19907 if (*tail == NUL)
19908 break;
19909 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19910 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911 }
19912 else
19913 {
19914 tail = regmatch.endp[0];
19915 if (*tail == NUL)
19916 break;
19917 }
19918 if (!do_all)
19919 break;
19920 }
19921
19922 if (ga.ga_data != NULL)
19923 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19924
19925 vim_free(regmatch.regprog);
19926 }
19927
19928 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19929 ga_clear(&ga);
19930 p_cpo = save_cpo;
19931
19932 return ret;
19933}
19934
19935#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */