blob: 8c7049be30f6f32f930e5b639d6b13013abbd963 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000344};
345
346/* shorthand */
347#define vv_type vv_di.di_tv.v_type
348#define vv_nr vv_di.di_tv.vval.v_number
349#define vv_str vv_di.di_tv.vval.v_string
350#define vv_tv vv_di.di_tv
351
352/*
353 * The v: variables are stored in dictionary "vimvardict".
354 * "vimvars_var" is the variable that is used for the "l:" scope.
355 */
356static dict_T vimvardict;
357static dictitem_T vimvars_var;
358#define vimvarht vimvardict.dv_hashtab
359
Bram Moolenaara40058a2005-07-11 22:42:07 +0000360static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
361static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
362#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
363static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
364#endif
365static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
366static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
367static char_u *skip_var_one __ARGS((char_u *arg));
368static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
369static void list_glob_vars __ARGS((void));
370static void list_buf_vars __ARGS((void));
371static void list_win_vars __ARGS((void));
372static void list_vim_vars __ARGS((void));
373static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
374static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
375static int check_changedtick __ARGS((char_u *arg));
376static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
377static void clear_lval __ARGS((lval_T *lp));
378static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
379static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
380static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
381static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
382static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
383static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
384static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
385static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
386static void item_lock __ARGS((typval_T *tv, int deep, int lock));
387static int tv_islocked __ARGS((typval_T *tv));
388
Bram Moolenaar33570922005-01-25 22:26:29 +0000389static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
390static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000398static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000403static listitem_T *listitem_alloc __ARGS((void));
404static void listitem_free __ARGS((listitem_T *item));
405static void listitem_remove __ARGS((list_T *l, listitem_T *item));
406static long list_len __ARGS((list_T *l));
407static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
408static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
409static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static listitem_T *list_find __ARGS((list_T *l, long n));
411static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static void list_append __ARGS((list_T *l, listitem_T *item));
413static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000414static int list_append_string __ARGS((list_T *l, char_u *str, int len));
415static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
417static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
418static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000419static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000421static char_u *list2string __ARGS((typval_T *tv, int copyID));
422static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000423static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
424static void set_ref_in_list __ARGS((list_T *l, int copyID));
425static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static void dict_unref __ARGS((dict_T *d));
427static void dict_free __ARGS((dict_T *d));
428static dictitem_T *dictitem_alloc __ARGS((char_u *key));
429static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
430static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
431static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000432static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static int dict_add __ARGS((dict_T *d, dictitem_T *item));
434static long dict_len __ARGS((dict_T *d));
435static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
439static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static char_u *string_quote __ARGS((char_u *str, int function));
441static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
442static int find_internal_func __ARGS((char_u *name));
443static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
444static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
445static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447
448static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000467#if defined(FEAT_INS_EXPAND)
468static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
476static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000502static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000504static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000510static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000518static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000541static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000547static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000564static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000568#ifdef vim_mkdir
569static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
570#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000571static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000575static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000576static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000578static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000590static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000592static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000599static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000600static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000605static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000606static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
609#ifdef HAVE_STRFTIME
610static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
611#endif
612static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000624static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000625static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000627static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000642static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000643
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
645static int get_env_len __ARGS((char_u **arg));
646static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000647static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000648static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
649#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
650#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
651 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000652static 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 +0000653static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000654static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
656static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static typval_T *alloc_tv __ARGS((void));
658static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void init_tv __ARGS((typval_T *varp));
660static long get_tv_number __ARGS((typval_T *varp));
661static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000662static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static char_u *get_tv_string __ARGS((typval_T *varp));
664static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000665static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000667static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
669static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
670static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
671static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
672static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
673static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
674static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000675static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000677static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
679static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
680static int eval_fname_script __ARGS((char_u *p));
681static int eval_fname_sid __ARGS((char_u *p));
682static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static ufunc_T *find_func __ARGS((char_u *name));
684static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000685static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000686#ifdef FEAT_PROFILE
687static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000688static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
689static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
690static int
691# ifdef __BORLANDC__
692 _RTLENTRYF
693# endif
694 prof_total_cmp __ARGS((const void *s1, const void *s2));
695static int
696# ifdef __BORLANDC__
697 _RTLENTRYF
698# endif
699 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000700#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000701static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000702static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000703static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static void func_free __ARGS((ufunc_T *fp));
705static void func_unref __ARGS((char_u *name));
706static void func_ref __ARGS((char_u *name));
707static 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));
708static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000709static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000710static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
711static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000713/* Character used as separated in autoload function/variable names. */
714#define AUTOLOAD_CHAR '#'
715
Bram Moolenaar33570922005-01-25 22:26:29 +0000716/*
717 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000718 */
719 void
720eval_init()
721{
Bram Moolenaar33570922005-01-25 22:26:29 +0000722 int i;
723 struct vimvar *p;
724
725 init_var_dict(&globvardict, &globvars_var);
726 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000727 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000728 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000729
730 for (i = 0; i < VV_LEN; ++i)
731 {
732 p = &vimvars[i];
733 STRCPY(p->vv_di.di_key, p->vv_name);
734 if (p->vv_flags & VV_RO)
735 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
736 else if (p->vv_flags & VV_RO_SBX)
737 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
738 else
739 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000740
741 /* add to v: scope dict, unless the value is not always available */
742 if (p->vv_type != VAR_UNKNOWN)
743 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000744 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000745 /* add to compat scope dict */
746 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000747 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000748}
749
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000750#if defined(EXITFREE) || defined(PROTO)
751 void
752eval_clear()
753{
754 int i;
755 struct vimvar *p;
756
757 for (i = 0; i < VV_LEN; ++i)
758 {
759 p = &vimvars[i];
760 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000761 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000762 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000763 p->vv_di.di_tv.vval.v_string = NULL;
764 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000765 }
766 hash_clear(&vimvarht);
767 hash_clear(&compat_hashtab);
768
769 /* script-local variables */
770 for (i = 1; i <= ga_scripts.ga_len; ++i)
771 vars_clear(&SCRIPT_VARS(i));
772 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000773 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000774
775 /* global variables */
776 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000777
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000778 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000779 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000780 hash_clear(&func_hashtab);
781
782 /* unreferenced lists and dicts */
783 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000784}
785#endif
786
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 * Return the name of the executed function.
789 */
790 char_u *
791func_name(cookie)
792 void *cookie;
793{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000794 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795}
796
797/*
798 * Return the address holding the next breakpoint line for a funccall cookie.
799 */
800 linenr_T *
801func_breakpoint(cookie)
802 void *cookie;
803{
Bram Moolenaar33570922005-01-25 22:26:29 +0000804 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805}
806
807/*
808 * Return the address holding the debug tick for a funccall cookie.
809 */
810 int *
811func_dbg_tick(cookie)
812 void *cookie;
813{
Bram Moolenaar33570922005-01-25 22:26:29 +0000814 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815}
816
817/*
818 * Return the nesting level for a funccall cookie.
819 */
820 int
821func_level(cookie)
822 void *cookie;
823{
Bram Moolenaar33570922005-01-25 22:26:29 +0000824 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825}
826
827/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000828funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
830/*
831 * Return TRUE when a function was ended by a ":return" command.
832 */
833 int
834current_func_returned()
835{
836 return current_funccal->returned;
837}
838
839
840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 * Set an internal variable to a string value. Creates the variable if it does
842 * not already exist.
843 */
844 void
845set_internal_string_var(name, value)
846 char_u *name;
847 char_u *value;
848{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000849 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000850 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
852 val = vim_strsave(value);
853 if (val != NULL)
854 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000855 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000856 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000858 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000859 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
861 }
862}
863
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000864static lval_T *redir_lval = NULL;
865static char_u *redir_endp = NULL;
866static char_u *redir_varname = NULL;
867
868/*
869 * Start recording command output to a variable
870 * Returns OK if successfully completed the setup. FAIL otherwise.
871 */
872 int
873var_redir_start(name, append)
874 char_u *name;
875 int append; /* append to an existing variable */
876{
877 int save_emsg;
878 int err;
879 typval_T tv;
880
881 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000882 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000883 {
884 EMSG(_(e_invarg));
885 return FAIL;
886 }
887
888 redir_varname = vim_strsave(name);
889 if (redir_varname == NULL)
890 return FAIL;
891
892 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
893 if (redir_lval == NULL)
894 {
895 var_redir_stop();
896 return FAIL;
897 }
898
899 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000900 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
901 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000902 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
903 {
904 if (redir_endp != NULL && *redir_endp != NUL)
905 /* Trailing characters are present after the variable name */
906 EMSG(_(e_trailing));
907 else
908 EMSG(_(e_invarg));
909 var_redir_stop();
910 return FAIL;
911 }
912
913 /* check if we can write to the variable: set it to or append an empty
914 * string */
915 save_emsg = did_emsg;
916 did_emsg = FALSE;
917 tv.v_type = VAR_STRING;
918 tv.vval.v_string = (char_u *)"";
919 if (append)
920 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
921 else
922 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
923 err = did_emsg;
924 did_emsg += save_emsg;
925 if (err)
926 {
927 var_redir_stop();
928 return FAIL;
929 }
930 if (redir_lval->ll_newkey != NULL)
931 {
932 /* Dictionary item was created, don't do it again. */
933 vim_free(redir_lval->ll_newkey);
934 redir_lval->ll_newkey = NULL;
935 }
936
937 return OK;
938}
939
940/*
941 * Append "value[len]" to the variable set by var_redir_start().
942 */
943 void
944var_redir_str(value, len)
945 char_u *value;
946 int len;
947{
948 char_u *val;
949 typval_T tv;
950 int save_emsg;
951 int err;
952
953 if (redir_lval == NULL)
954 return;
955
956 if (len == -1)
957 /* Append the entire string */
958 val = vim_strsave(value);
959 else
960 /* Append only the specified number of characters */
961 val = vim_strnsave(value, len);
962 if (val == NULL)
963 return;
964
965 tv.v_type = VAR_STRING;
966 tv.vval.v_string = val;
967
968 save_emsg = did_emsg;
969 did_emsg = FALSE;
970 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
971 err = did_emsg;
972 did_emsg += save_emsg;
973 if (err)
974 var_redir_stop();
975
976 vim_free(tv.vval.v_string);
977}
978
979/*
980 * Stop redirecting command output to a variable.
981 */
982 void
983var_redir_stop()
984{
985 if (redir_lval != NULL)
986 {
987 clear_lval(redir_lval);
988 vim_free(redir_lval);
989 redir_lval = NULL;
990 }
991 vim_free(redir_varname);
992 redir_varname = NULL;
993}
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995# if defined(FEAT_MBYTE) || defined(PROTO)
996 int
997eval_charconvert(enc_from, enc_to, fname_from, fname_to)
998 char_u *enc_from;
999 char_u *enc_to;
1000 char_u *fname_from;
1001 char_u *fname_to;
1002{
1003 int err = FALSE;
1004
1005 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1006 set_vim_var_string(VV_CC_TO, enc_to, -1);
1007 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1008 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1009 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1010 err = TRUE;
1011 set_vim_var_string(VV_CC_FROM, NULL, -1);
1012 set_vim_var_string(VV_CC_TO, NULL, -1);
1013 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1014 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1015
1016 if (err)
1017 return FAIL;
1018 return OK;
1019}
1020# endif
1021
1022# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1023 int
1024eval_printexpr(fname, args)
1025 char_u *fname;
1026 char_u *args;
1027{
1028 int err = FALSE;
1029
1030 set_vim_var_string(VV_FNAME_IN, fname, -1);
1031 set_vim_var_string(VV_CMDARG, args, -1);
1032 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1033 err = TRUE;
1034 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1035 set_vim_var_string(VV_CMDARG, NULL, -1);
1036
1037 if (err)
1038 {
1039 mch_remove(fname);
1040 return FAIL;
1041 }
1042 return OK;
1043}
1044# endif
1045
1046# if defined(FEAT_DIFF) || defined(PROTO)
1047 void
1048eval_diff(origfile, newfile, outfile)
1049 char_u *origfile;
1050 char_u *newfile;
1051 char_u *outfile;
1052{
1053 int err = FALSE;
1054
1055 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1056 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1057 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1058 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1059 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1060 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1061 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1062}
1063
1064 void
1065eval_patch(origfile, difffile, outfile)
1066 char_u *origfile;
1067 char_u *difffile;
1068 char_u *outfile;
1069{
1070 int err;
1071
1072 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1073 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1074 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1075 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1076 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1077 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1078 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1079}
1080# endif
1081
1082/*
1083 * Top level evaluation function, returning a boolean.
1084 * Sets "error" to TRUE if there was an error.
1085 * Return TRUE or FALSE.
1086 */
1087 int
1088eval_to_bool(arg, error, nextcmd, skip)
1089 char_u *arg;
1090 int *error;
1091 char_u **nextcmd;
1092 int skip; /* only parse, don't execute */
1093{
Bram Moolenaar33570922005-01-25 22:26:29 +00001094 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 int retval = FALSE;
1096
1097 if (skip)
1098 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001099 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 else
1102 {
1103 *error = FALSE;
1104 if (!skip)
1105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001106 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001107 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 }
1109 }
1110 if (skip)
1111 --emsg_skip;
1112
1113 return retval;
1114}
1115
1116/*
1117 * Top level evaluation function, returning a string. If "skip" is TRUE,
1118 * only parsing to "nextcmd" is done, without reporting errors. Return
1119 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1120 */
1121 char_u *
1122eval_to_string_skip(arg, nextcmd, skip)
1123 char_u *arg;
1124 char_u **nextcmd;
1125 int skip; /* only parse, don't execute */
1126{
Bram Moolenaar33570922005-01-25 22:26:29 +00001127 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 char_u *retval;
1129
1130 if (skip)
1131 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001132 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 retval = NULL;
1134 else
1135 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001136 retval = vim_strsave(get_tv_string(&tv));
1137 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139 if (skip)
1140 --emsg_skip;
1141
1142 return retval;
1143}
1144
1145/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001146 * Skip over an expression at "*pp".
1147 * Return FAIL for an error, OK otherwise.
1148 */
1149 int
1150skip_expr(pp)
1151 char_u **pp;
1152{
Bram Moolenaar33570922005-01-25 22:26:29 +00001153 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001154
1155 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001156 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001157}
1158
1159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 * Top level evaluation function, returning a string.
1161 * Return pointer to allocated memory, or NULL for failure.
1162 */
1163 char_u *
1164eval_to_string(arg, nextcmd)
1165 char_u *arg;
1166 char_u **nextcmd;
1167{
Bram Moolenaar33570922005-01-25 22:26:29 +00001168 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 char_u *retval;
1170
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 retval = NULL;
1173 else
1174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001175 retval = vim_strsave(get_tv_string(&tv));
1176 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 }
1178
1179 return retval;
1180}
1181
1182/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001183 * Call eval_to_string() without using current local variables and using
1184 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 */
1186 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001187eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 char_u *arg;
1189 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001190 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191{
1192 char_u *retval;
1193 void *save_funccalp;
1194
1195 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001196 if (use_sandbox)
1197 ++sandbox;
1198 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 retval = eval_to_string(arg, nextcmd);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001200 if (use_sandbox)
1201 --sandbox;
1202 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 restore_funccal(save_funccalp);
1204 return retval;
1205}
1206
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207/*
1208 * Top level evaluation function, returning a number.
1209 * Evaluates "expr" silently.
1210 * Returns -1 for an error.
1211 */
1212 int
1213eval_to_number(expr)
1214 char_u *expr;
1215{
Bram Moolenaar33570922005-01-25 22:26:29 +00001216 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001218 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
1220 ++emsg_off;
1221
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001222 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 retval = -1;
1224 else
1225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001226 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001227 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 }
1229 --emsg_off;
1230
1231 return retval;
1232}
1233
Bram Moolenaara40058a2005-07-11 22:42:07 +00001234/*
1235 * Prepare v: variable "idx" to be used.
1236 * Save the current typeval in "save_tv".
1237 * When not used yet add the variable to the v: hashtable.
1238 */
1239 static void
1240prepare_vimvar(idx, save_tv)
1241 int idx;
1242 typval_T *save_tv;
1243{
1244 *save_tv = vimvars[idx].vv_tv;
1245 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1246 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1247}
1248
1249/*
1250 * Restore v: variable "idx" to typeval "save_tv".
1251 * When no longer defined, remove the variable from the v: hashtable.
1252 */
1253 static void
1254restore_vimvar(idx, save_tv)
1255 int idx;
1256 typval_T *save_tv;
1257{
1258 hashitem_T *hi;
1259
1260 clear_tv(&vimvars[idx].vv_tv);
1261 vimvars[idx].vv_tv = *save_tv;
1262 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1263 {
1264 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1265 if (HASHITEM_EMPTY(hi))
1266 EMSG2(_(e_intern2), "restore_vimvar()");
1267 else
1268 hash_remove(&vimvarht, hi);
1269 }
1270}
1271
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001272#if defined(FEAT_SYN_HL) || defined(PROTO)
1273/*
1274 * Evaluate an expression to a list with suggestions.
1275 * For the "expr:" part of 'spellsuggest'.
1276 */
1277 list_T *
1278eval_spell_expr(badword, expr)
1279 char_u *badword;
1280 char_u *expr;
1281{
1282 typval_T save_val;
1283 typval_T rettv;
1284 list_T *list = NULL;
1285 char_u *p = skipwhite(expr);
1286
1287 /* Set "v:val" to the bad word. */
1288 prepare_vimvar(VV_VAL, &save_val);
1289 vimvars[VV_VAL].vv_type = VAR_STRING;
1290 vimvars[VV_VAL].vv_str = badword;
1291 if (p_verbose == 0)
1292 ++emsg_off;
1293
1294 if (eval1(&p, &rettv, TRUE) == OK)
1295 {
1296 if (rettv.v_type != VAR_LIST)
1297 clear_tv(&rettv);
1298 else
1299 list = rettv.vval.v_list;
1300 }
1301
1302 if (p_verbose == 0)
1303 --emsg_off;
1304 vimvars[VV_VAL].vv_str = NULL;
1305 restore_vimvar(VV_VAL, &save_val);
1306
1307 return list;
1308}
1309
1310/*
1311 * "list" is supposed to contain two items: a word and a number. Return the
1312 * word in "pp" and the number as the return value.
1313 * Return -1 if anything isn't right.
1314 * Used to get the good word and score from the eval_spell_expr() result.
1315 */
1316 int
1317get_spellword(list, pp)
1318 list_T *list;
1319 char_u **pp;
1320{
1321 listitem_T *li;
1322
1323 li = list->lv_first;
1324 if (li == NULL)
1325 return -1;
1326 *pp = get_tv_string(&li->li_tv);
1327
1328 li = li->li_next;
1329 if (li == NULL)
1330 return -1;
1331 return get_tv_number(&li->li_tv);
1332}
1333#endif
1334
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001335/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001336 * Top level evaluation function.
1337 * Returns an allocated typval_T with the result.
1338 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001339 */
1340 typval_T *
1341eval_expr(arg, nextcmd)
1342 char_u *arg;
1343 char_u **nextcmd;
1344{
1345 typval_T *tv;
1346
1347 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001348 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001349 {
1350 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001351 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001352 }
1353
1354 return tv;
1355}
1356
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1359/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001360 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001362 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001364 static int
1365call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 char_u *func;
1367 int argc;
1368 char_u **argv;
1369 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
Bram Moolenaar33570922005-01-25 22:26:29 +00001372 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 long n;
1374 int len;
1375 int i;
1376 int doesrange;
1377 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001378 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379
Bram Moolenaar33570922005-01-25 22:26:29 +00001380 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001382 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
1384 for (i = 0; i < argc; i++)
1385 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001386 /* Pass a NULL or empty argument as an empty string */
1387 if (argv[i] == NULL || *argv[i] == NUL)
1388 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001389 argvars[i].v_type = VAR_STRING;
1390 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001391 continue;
1392 }
1393
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 /* Recognize a number argument, the others must be strings. */
1395 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1396 if (len != 0 && len == (int)STRLEN(argv[i]))
1397 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001398 argvars[i].v_type = VAR_NUMBER;
1399 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
1401 else
1402 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001403 argvars[i].v_type = VAR_STRING;
1404 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 }
1406 }
1407
1408 if (safe)
1409 {
1410 save_funccalp = save_funccal();
1411 ++sandbox;
1412 }
1413
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001414 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1415 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001417 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 if (safe)
1419 {
1420 --sandbox;
1421 restore_funccal(save_funccalp);
1422 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001423 vim_free(argvars);
1424
1425 if (ret == FAIL)
1426 clear_tv(rettv);
1427
1428 return ret;
1429}
1430
1431/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001432 * Call vimL function "func" and return the result as a string.
1433 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001434 * Uses argv[argc] for the function arguments.
1435 */
1436 void *
1437call_func_retstr(func, argc, argv, safe)
1438 char_u *func;
1439 int argc;
1440 char_u **argv;
1441 int safe; /* use the sandbox */
1442{
1443 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001444 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001445
1446 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1447 return NULL;
1448
1449 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001450 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 return retval;
1452}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001453
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001454#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001455/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001456 * Call vimL function "func" and return the result as a number.
1457 * Returns -1 when calling the function fails.
1458 * Uses argv[argc] for the function arguments.
1459 */
1460 long
1461call_func_retnr(func, argc, argv, safe)
1462 char_u *func;
1463 int argc;
1464 char_u **argv;
1465 int safe; /* use the sandbox */
1466{
1467 typval_T rettv;
1468 long retval;
1469
1470 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1471 return -1;
1472
1473 retval = get_tv_number_chk(&rettv, NULL);
1474 clear_tv(&rettv);
1475 return retval;
1476}
1477#endif
1478
1479/*
1480 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001481 * Uses argv[argc] for the function arguments.
1482 */
1483 void *
1484call_func_retlist(func, argc, argv, safe)
1485 char_u *func;
1486 int argc;
1487 char_u **argv;
1488 int safe; /* use the sandbox */
1489{
1490 typval_T rettv;
1491
1492 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1493 return NULL;
1494
1495 if (rettv.v_type != VAR_LIST)
1496 {
1497 clear_tv(&rettv);
1498 return NULL;
1499 }
1500
1501 return rettv.vval.v_list;
1502}
1503
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504#endif
1505
1506/*
1507 * Save the current function call pointer, and set it to NULL.
1508 * Used when executing autocommands and for ":source".
1509 */
1510 void *
1511save_funccal()
1512{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001513 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 current_funccal = NULL;
1516 return (void *)fc;
1517}
1518
1519 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001520restore_funccal(vfc)
1521 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001523 funccall_T *fc = (funccall_T *)vfc;
1524
1525 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526}
1527
Bram Moolenaar05159a02005-02-26 23:04:13 +00001528#if defined(FEAT_PROFILE) || defined(PROTO)
1529/*
1530 * Prepare profiling for entering a child or something else that is not
1531 * counted for the script/function itself.
1532 * Should always be called in pair with prof_child_exit().
1533 */
1534 void
1535prof_child_enter(tm)
1536 proftime_T *tm; /* place to store waittime */
1537{
1538 funccall_T *fc = current_funccal;
1539
1540 if (fc != NULL && fc->func->uf_profiling)
1541 profile_start(&fc->prof_child);
1542 script_prof_save(tm);
1543}
1544
1545/*
1546 * Take care of time spent in a child.
1547 * Should always be called after prof_child_enter().
1548 */
1549 void
1550prof_child_exit(tm)
1551 proftime_T *tm; /* where waittime was stored */
1552{
1553 funccall_T *fc = current_funccal;
1554
1555 if (fc != NULL && fc->func->uf_profiling)
1556 {
1557 profile_end(&fc->prof_child);
1558 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1559 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1560 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1561 }
1562 script_prof_restore(tm);
1563}
1564#endif
1565
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567#ifdef FEAT_FOLDING
1568/*
1569 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1570 * it in "*cp". Doesn't give error messages.
1571 */
1572 int
1573eval_foldexpr(arg, cp)
1574 char_u *arg;
1575 int *cp;
1576{
Bram Moolenaar33570922005-01-25 22:26:29 +00001577 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 int retval;
1579 char_u *s;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001580 int use_sandbox = was_set_insecurely((char_u *)"foldexpr");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001583 if (use_sandbox)
1584 ++sandbox;
1585 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001587 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 retval = 0;
1589 else
1590 {
1591 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001592 if (tv.v_type == VAR_NUMBER)
1593 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001594 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 retval = 0;
1596 else
1597 {
1598 /* If the result is a string, check if there is a non-digit before
1599 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001600 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 if (!VIM_ISDIGIT(*s) && *s != '-')
1602 *cp = *s++;
1603 retval = atol((char *)s);
1604 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001605 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001608 if (use_sandbox)
1609 --sandbox;
1610 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
1612 return retval;
1613}
1614#endif
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001617 * ":let" list all variable values
1618 * ":let var1 var2" list variable values
1619 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001620 * ":let var += expr" assignment command.
1621 * ":let var -= expr" assignment command.
1622 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001623 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 */
1625 void
1626ex_let(eap)
1627 exarg_T *eap;
1628{
1629 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001630 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001631 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001633 int var_count = 0;
1634 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001635 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001637 expr = skip_var_list(arg, &var_count, &semicolon);
1638 if (expr == NULL)
1639 return;
1640 expr = vim_strchr(expr, '=');
1641 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001643 /*
1644 * ":let" without "=": list variables
1645 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001646 if (*arg == '[')
1647 EMSG(_(e_invarg));
1648 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001649 /* ":let var1 var2" */
1650 arg = list_arg_vars(eap, arg);
1651 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001653 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001654 list_glob_vars();
1655 list_buf_vars();
1656 list_win_vars();
1657 list_vim_vars();
1658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 eap->nextcmd = check_nextcmd(arg);
1660 }
1661 else
1662 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001663 op[0] = '=';
1664 op[1] = NUL;
1665 if (expr > arg)
1666 {
1667 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1668 op[0] = expr[-1]; /* +=, -= or .= */
1669 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if (eap->skip)
1673 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (eap->skip)
1676 {
1677 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001678 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 --emsg_skip;
1680 }
1681 else if (i != FAIL)
1682 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001684 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001685 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 }
1687 }
1688}
1689
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690/*
1691 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1692 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001693 * When "nextchars" is not NULL it points to a string with characters that
1694 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1695 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 * Returns OK or FAIL;
1697 */
1698 static int
1699ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1700 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001701 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 int copy; /* copy values from "tv", don't move */
1703 int semicolon; /* from skip_var_list() */
1704 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001705 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706{
1707 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001708 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001710 listitem_T *item;
1711 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712
1713 if (*arg != '[')
1714 {
1715 /*
1716 * ":let var = expr" or ":for var in list"
1717 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001718 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 return FAIL;
1720 return OK;
1721 }
1722
1723 /*
1724 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1725 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001726 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727 {
1728 EMSG(_(e_listreq));
1729 return FAIL;
1730 }
1731
1732 i = list_len(l);
1733 if (semicolon == 0 && var_count < i)
1734 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001735 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 return FAIL;
1737 }
1738 if (var_count - semicolon > i)
1739 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001740 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741 return FAIL;
1742 }
1743
1744 item = l->lv_first;
1745 while (*arg != ']')
1746 {
1747 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001748 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749 item = item->li_next;
1750 if (arg == NULL)
1751 return FAIL;
1752
1753 arg = skipwhite(arg);
1754 if (*arg == ';')
1755 {
1756 /* Put the rest of the list (may be empty) in the var after ';'.
1757 * Create a new list for this. */
1758 l = list_alloc();
1759 if (l == NULL)
1760 return FAIL;
1761 while (item != NULL)
1762 {
1763 list_append_tv(l, &item->li_tv);
1764 item = item->li_next;
1765 }
1766
1767 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001768 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 ltv.vval.v_list = l;
1770 l->lv_refcount = 1;
1771
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001772 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1773 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 clear_tv(&ltv);
1775 if (arg == NULL)
1776 return FAIL;
1777 break;
1778 }
1779 else if (*arg != ',' && *arg != ']')
1780 {
1781 EMSG2(_(e_intern2), "ex_let_vars()");
1782 return FAIL;
1783 }
1784 }
1785
1786 return OK;
1787}
1788
1789/*
1790 * Skip over assignable variable "var" or list of variables "[var, var]".
1791 * Used for ":let varvar = expr" and ":for varvar in expr".
1792 * For "[var, var]" increment "*var_count" for each variable.
1793 * for "[var, var; var]" set "semicolon".
1794 * Return NULL for an error.
1795 */
1796 static char_u *
1797skip_var_list(arg, var_count, semicolon)
1798 char_u *arg;
1799 int *var_count;
1800 int *semicolon;
1801{
1802 char_u *p, *s;
1803
1804 if (*arg == '[')
1805 {
1806 /* "[var, var]": find the matching ']'. */
1807 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001808 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809 {
1810 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1811 s = skip_var_one(p);
1812 if (s == p)
1813 {
1814 EMSG2(_(e_invarg2), p);
1815 return NULL;
1816 }
1817 ++*var_count;
1818
1819 p = skipwhite(s);
1820 if (*p == ']')
1821 break;
1822 else if (*p == ';')
1823 {
1824 if (*semicolon == 1)
1825 {
1826 EMSG(_("Double ; in list of variables"));
1827 return NULL;
1828 }
1829 *semicolon = 1;
1830 }
1831 else if (*p != ',')
1832 {
1833 EMSG2(_(e_invarg2), p);
1834 return NULL;
1835 }
1836 }
1837 return p + 1;
1838 }
1839 else
1840 return skip_var_one(arg);
1841}
1842
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001843/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001844 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1845 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001846 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 static char_u *
1848skip_var_one(arg)
1849 char_u *arg;
1850{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001851 if (*arg == '@' && arg[1] != NUL)
1852 return arg + 2;
1853 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1854 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855}
1856
Bram Moolenaara7043832005-01-21 11:56:39 +00001857/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001858 * List variables for hashtab "ht" with prefix "prefix".
1859 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001860 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001862list_hashtable_vars(ht, prefix, empty)
1863 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001864 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001865 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001866{
Bram Moolenaar33570922005-01-25 22:26:29 +00001867 hashitem_T *hi;
1868 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001869 int todo;
1870
1871 todo = ht->ht_used;
1872 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1873 {
1874 if (!HASHITEM_EMPTY(hi))
1875 {
1876 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001877 di = HI2DI(hi);
1878 if (empty || di->di_tv.v_type != VAR_STRING
1879 || di->di_tv.vval.v_string != NULL)
1880 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001881 }
1882 }
1883}
1884
1885/*
1886 * List global variables.
1887 */
1888 static void
1889list_glob_vars()
1890{
Bram Moolenaar33570922005-01-25 22:26:29 +00001891 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001892}
1893
1894/*
1895 * List buffer variables.
1896 */
1897 static void
1898list_buf_vars()
1899{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001900 char_u numbuf[NUMBUFLEN];
1901
Bram Moolenaar33570922005-01-25 22:26:29 +00001902 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001903
1904 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1905 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001906}
1907
1908/*
1909 * List window variables.
1910 */
1911 static void
1912list_win_vars()
1913{
Bram Moolenaar33570922005-01-25 22:26:29 +00001914 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001915}
1916
1917/*
1918 * List Vim variables.
1919 */
1920 static void
1921list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922{
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924}
1925
1926/*
1927 * List variables in "arg".
1928 */
1929 static char_u *
1930list_arg_vars(eap, arg)
1931 exarg_T *eap;
1932 char_u *arg;
1933{
1934 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001935 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001937 char_u *name_start;
1938 char_u *arg_subsc;
1939 char_u *tofree;
1940 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941
1942 while (!ends_excmd(*arg) && !got_int)
1943 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001944 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001946 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001947 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1948 {
1949 emsg_severe = TRUE;
1950 EMSG(_(e_trailing));
1951 break;
1952 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001954 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001956 /* get_name_len() takes care of expanding curly braces */
1957 name_start = name = arg;
1958 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1959 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001961 /* This is mainly to keep test 49 working: when expanding
1962 * curly braces fails overrule the exception error message. */
1963 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001964 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001965 emsg_severe = TRUE;
1966 EMSG2(_(e_invarg2), arg);
1967 break;
1968 }
1969 error = TRUE;
1970 }
1971 else
1972 {
1973 if (tofree != NULL)
1974 name = tofree;
1975 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001976 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001977 else
1978 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001979 /* handle d.key, l[idx], f(expr) */
1980 arg_subsc = arg;
1981 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001982 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001984 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001985 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001986 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001987 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001988 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001989 case 'g': list_glob_vars(); break;
1990 case 'b': list_buf_vars(); break;
1991 case 'w': list_win_vars(); break;
1992 case 'v': list_vim_vars(); break;
1993 default:
1994 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001995 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001996 }
1997 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001998 {
1999 char_u numbuf[NUMBUFLEN];
2000 char_u *tf;
2001 int c;
2002 char_u *s;
2003
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002004 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002005 c = *arg;
2006 *arg = NUL;
2007 list_one_var_a((char_u *)"",
2008 arg == arg_subsc ? name : name_start,
2009 tv.v_type, s == NULL ? (char_u *)"" : s);
2010 *arg = c;
2011 vim_free(tf);
2012 }
2013 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002014 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002015 }
2016 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002017
2018 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002020
2021 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002022 }
2023
2024 return arg;
2025}
2026
2027/*
2028 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2029 * Returns a pointer to the char just after the var name.
2030 * Returns NULL if there is an error.
2031 */
2032 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002033ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002034 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002035 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002036 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002038 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002039{
2040 int c1;
2041 char_u *name;
2042 char_u *p;
2043 char_u *arg_end = NULL;
2044 int len;
2045 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002046 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002047
2048 /*
2049 * ":let $VAR = expr": Set environment variable.
2050 */
2051 if (*arg == '$')
2052 {
2053 /* Find the end of the name. */
2054 ++arg;
2055 name = arg;
2056 len = get_env_len(&arg);
2057 if (len == 0)
2058 EMSG2(_(e_invarg2), name - 1);
2059 else
2060 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002061 if (op != NULL && (*op == '+' || *op == '-'))
2062 EMSG2(_(e_letwrong), op);
2063 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002065 EMSG(_(e_letunexp));
2066 else
2067 {
2068 c1 = name[len];
2069 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002070 p = get_tv_string_chk(tv);
2071 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002072 {
2073 int mustfree = FALSE;
2074 char_u *s = vim_getenv(name, &mustfree);
2075
2076 if (s != NULL)
2077 {
2078 p = tofree = concat_str(s, p);
2079 if (mustfree)
2080 vim_free(s);
2081 }
2082 }
2083 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002084 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002085 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002086 if (STRICMP(name, "HOME") == 0)
2087 init_homedir();
2088 else if (didset_vim && STRICMP(name, "VIM") == 0)
2089 didset_vim = FALSE;
2090 else if (didset_vimruntime
2091 && STRICMP(name, "VIMRUNTIME") == 0)
2092 didset_vimruntime = FALSE;
2093 arg_end = arg;
2094 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002096 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 }
2098 }
2099 }
2100
2101 /*
2102 * ":let &option = expr": Set option value.
2103 * ":let &l:option = expr": Set local option value.
2104 * ":let &g:option = expr": Set global option value.
2105 */
2106 else if (*arg == '&')
2107 {
2108 /* Find the end of the name. */
2109 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002110 if (p == NULL || (endchars != NULL
2111 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002112 EMSG(_(e_letunexp));
2113 else
2114 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002115 long n;
2116 int opt_type;
2117 long numval;
2118 char_u *stringval = NULL;
2119 char_u *s;
2120
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 c1 = *p;
2122 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002123
2124 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002125 s = get_tv_string_chk(tv); /* != NULL if number or string */
2126 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002127 {
2128 opt_type = get_option_value(arg, &numval,
2129 &stringval, opt_flags);
2130 if ((opt_type == 1 && *op == '.')
2131 || (opt_type == 0 && *op != '.'))
2132 EMSG2(_(e_letwrong), op);
2133 else
2134 {
2135 if (opt_type == 1) /* number */
2136 {
2137 if (*op == '+')
2138 n = numval + n;
2139 else
2140 n = numval - n;
2141 }
2142 else if (opt_type == 0 && stringval != NULL) /* string */
2143 {
2144 s = concat_str(stringval, s);
2145 vim_free(stringval);
2146 stringval = s;
2147 }
2148 }
2149 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002150 if (s != NULL)
2151 {
2152 set_option_value(arg, n, s, opt_flags);
2153 arg_end = p;
2154 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002156 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157 }
2158 }
2159
2160 /*
2161 * ":let @r = expr": Set register contents.
2162 */
2163 else if (*arg == '@')
2164 {
2165 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002166 if (op != NULL && (*op == '+' || *op == '-'))
2167 EMSG2(_(e_letwrong), op);
2168 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002169 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002170 EMSG(_(e_letunexp));
2171 else
2172 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002173 char_u *tofree = NULL;
2174 char_u *s;
2175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002176 p = get_tv_string_chk(tv);
2177 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002178 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002179 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002180 if (s != NULL)
2181 {
2182 p = tofree = concat_str(s, p);
2183 vim_free(s);
2184 }
2185 }
2186 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002187 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002188 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002189 arg_end = arg + 1;
2190 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002191 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 }
2193 }
2194
2195 /*
2196 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002199 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002201 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002203 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002204 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2207 EMSG(_(e_letunexp));
2208 else
2209 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002210 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 arg_end = p;
2212 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002214 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 }
2216
2217 else
2218 EMSG2(_(e_invarg2), arg);
2219
2220 return arg_end;
2221}
2222
2223/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002224 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2225 */
2226 static int
2227check_changedtick(arg)
2228 char_u *arg;
2229{
2230 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2231 {
2232 EMSG2(_(e_readonlyvar), arg);
2233 return TRUE;
2234 }
2235 return FALSE;
2236}
2237
2238/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 * Get an lval: variable, Dict item or List item that can be assigned a value
2240 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2241 * "name.key", "name.key[expr]" etc.
2242 * Indexing only works if "name" is an existing List or Dictionary.
2243 * "name" points to the start of the name.
2244 * If "rettv" is not NULL it points to the value to be assigned.
2245 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2246 * wrong; must end in space or cmd separator.
2247 *
2248 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002250 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 */
2252 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002253get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002255 typval_T *rettv;
2256 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 int unlet;
2258 int skip;
2259 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002260 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002263 char_u *expr_start, *expr_end;
2264 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002265 dictitem_T *v;
2266 typval_T var1;
2267 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002269 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002270 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002271 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002272 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002274 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002275 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002276
2277 if (skip)
2278 {
2279 /* When skipping just find the end of the name. */
2280 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002281 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002282 }
2283
2284 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002285 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286 if (expr_start != NULL)
2287 {
2288 /* Don't expand the name when we already know there is an error. */
2289 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2290 && *p != '[' && *p != '.')
2291 {
2292 EMSG(_(e_trailing));
2293 return NULL;
2294 }
2295
2296 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2297 if (lp->ll_exp_name == NULL)
2298 {
2299 /* Report an invalid expression in braces, unless the
2300 * expression evaluation has been cancelled due to an
2301 * aborting error, an interrupt, or an exception. */
2302 if (!aborting() && !quiet)
2303 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002304 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 EMSG2(_(e_invarg2), name);
2306 return NULL;
2307 }
2308 }
2309 lp->ll_name = lp->ll_exp_name;
2310 }
2311 else
2312 lp->ll_name = name;
2313
2314 /* Without [idx] or .key we are done. */
2315 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2316 return p;
2317
2318 cc = *p;
2319 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002320 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002321 if (v == NULL && !quiet)
2322 EMSG2(_(e_undefvar), lp->ll_name);
2323 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 if (v == NULL)
2325 return NULL;
2326
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002327 /*
2328 * Loop until no more [idx] or .key is following.
2329 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002330 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2334 && !(lp->ll_tv->v_type == VAR_DICT
2335 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 if (!quiet)
2338 EMSG(_("E689: Can only index a List or Dictionary"));
2339 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002341 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002343 if (!quiet)
2344 EMSG(_("E708: [:] must come last"));
2345 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002347
Bram Moolenaar8c711452005-01-14 21:53:12 +00002348 len = -1;
2349 if (*p == '.')
2350 {
2351 key = p + 1;
2352 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2353 ;
2354 if (len == 0)
2355 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002356 if (!quiet)
2357 EMSG(_(e_emptykey));
2358 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002359 }
2360 p = key + len;
2361 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002362 else
2363 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002364 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002365 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002366 if (*p == ':')
2367 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002368 else
2369 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002370 empty1 = FALSE;
2371 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002372 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002373 if (get_tv_string_chk(&var1) == NULL)
2374 {
2375 /* not a number or string */
2376 clear_tv(&var1);
2377 return NULL;
2378 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002379 }
2380
2381 /* Optionally get the second index [ :expr]. */
2382 if (*p == ':')
2383 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002384 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002385 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002388 if (!empty1)
2389 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002391 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002392 if (rettv != NULL && (rettv->v_type != VAR_LIST
2393 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002394 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 if (!quiet)
2396 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002397 if (!empty1)
2398 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002400 }
2401 p = skipwhite(p + 1);
2402 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002404 else
2405 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2408 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002409 if (!empty1)
2410 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002412 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002413 if (get_tv_string_chk(&var2) == NULL)
2414 {
2415 /* not a number or string */
2416 if (!empty1)
2417 clear_tv(&var1);
2418 clear_tv(&var2);
2419 return NULL;
2420 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002421 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002423 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002424 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002426
Bram Moolenaar8c711452005-01-14 21:53:12 +00002427 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002428 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 if (!quiet)
2430 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 if (!empty1)
2432 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002434 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002436 }
2437
2438 /* Skip to past ']'. */
2439 ++p;
2440 }
2441
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 {
2444 if (len == -1)
2445 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 if (*key == NUL)
2449 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 if (!quiet)
2451 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 }
2455 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 lp->ll_list = NULL;
2457 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002458 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002461 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002466 if (len == -1)
2467 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 }
2470 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 if (len == -1)
2475 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 p = NULL;
2478 break;
2479 }
2480 if (len == -1)
2481 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002483 }
2484 else
2485 {
2486 /*
2487 * Get the number and item for the only or first index of the List.
2488 */
2489 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 else
2492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002493 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 clear_tv(&var1);
2495 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 lp->ll_list = lp->ll_tv->vval.v_list;
2498 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2499 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 if (!quiet)
2502 EMSGN(_(e_listidx), lp->ll_n1);
2503 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 }
2507
2508 /*
2509 * May need to find the item or absolute index for the second
2510 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 * When no index given: "lp->ll_empty2" is TRUE.
2512 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002515 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002516 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002517 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 if (ni == NULL)
2522 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (!quiet)
2524 EMSGN(_(e_listidx), lp->ll_n2);
2525 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 }
2529
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2531 if (lp->ll_n1 < 0)
2532 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2533 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002534 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 if (!quiet)
2536 EMSGN(_(e_listidx), lp->ll_n2);
2537 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002538 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002539 }
2540
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002542 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543 }
2544
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 return p;
2546}
2547
2548/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002549 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 */
2551 static void
2552clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002553 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554{
2555 vim_free(lp->ll_exp_name);
2556 vim_free(lp->ll_newkey);
2557}
2558
2559/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002560 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002562 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 */
2564 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002565set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002566 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002570 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571{
2572 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002573 listitem_T *ri;
2574 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575
2576 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002579 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 cc = *endp;
2581 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002582 if (op != NULL && *op != '=')
2583 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002585
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002586 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002587 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2588 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002589 {
2590 if (tv_op(&tv, rettv, op) == OK)
2591 set_var(lp->ll_name, &tv, FALSE);
2592 clear_tv(&tv);
2593 }
2594 }
2595 else
2596 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002600 else if (tv_check_lock(lp->ll_newkey == NULL
2601 ? lp->ll_tv->v_lock
2602 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2603 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 else if (lp->ll_range)
2605 {
2606 /*
2607 * Assign the List values to the list items.
2608 */
2609 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002610 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002611 if (op != NULL && *op != '=')
2612 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2613 else
2614 {
2615 clear_tv(&lp->ll_li->li_tv);
2616 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2617 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 ri = ri->li_next;
2619 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2620 break;
2621 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002624 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 ri = NULL;
2627 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 lp->ll_li = lp->ll_li->li_next;
2631 ++lp->ll_n1;
2632 }
2633 if (ri != NULL)
2634 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002635 else if (lp->ll_empty2
2636 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 : lp->ll_n1 != lp->ll_n2)
2638 EMSG(_("E711: List value has not enough items"));
2639 }
2640 else
2641 {
2642 /*
2643 * Assign to a List or Dictionary item.
2644 */
2645 if (lp->ll_newkey != NULL)
2646 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002647 if (op != NULL && *op != '=')
2648 {
2649 EMSG2(_(e_letwrong), op);
2650 return;
2651 }
2652
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002654 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (di == NULL)
2656 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002657 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2658 {
2659 vim_free(di);
2660 return;
2661 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002663 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002664 else if (op != NULL && *op != '=')
2665 {
2666 tv_op(lp->ll_tv, rettv, op);
2667 return;
2668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 /*
2673 * Assign the value to the variable or list item.
2674 */
2675 if (copy)
2676 copy_tv(rettv, lp->ll_tv);
2677 else
2678 {
2679 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002680 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002682 }
2683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002684}
2685
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002686/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002687 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2688 * Returns OK or FAIL.
2689 */
2690 static int
2691tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002692 typval_T *tv1;
2693 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002694 char_u *op;
2695{
2696 long n;
2697 char_u numbuf[NUMBUFLEN];
2698 char_u *s;
2699
2700 /* Can't do anything with a Funcref or a Dict on the right. */
2701 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2702 {
2703 switch (tv1->v_type)
2704 {
2705 case VAR_DICT:
2706 case VAR_FUNC:
2707 break;
2708
2709 case VAR_LIST:
2710 if (*op != '+' || tv2->v_type != VAR_LIST)
2711 break;
2712 /* List += List */
2713 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2714 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2715 return OK;
2716
2717 case VAR_NUMBER:
2718 case VAR_STRING:
2719 if (tv2->v_type == VAR_LIST)
2720 break;
2721 if (*op == '+' || *op == '-')
2722 {
2723 /* nr += nr or nr -= nr*/
2724 n = get_tv_number(tv1);
2725 if (*op == '+')
2726 n += get_tv_number(tv2);
2727 else
2728 n -= get_tv_number(tv2);
2729 clear_tv(tv1);
2730 tv1->v_type = VAR_NUMBER;
2731 tv1->vval.v_number = n;
2732 }
2733 else
2734 {
2735 /* str .= str */
2736 s = get_tv_string(tv1);
2737 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2738 clear_tv(tv1);
2739 tv1->v_type = VAR_STRING;
2740 tv1->vval.v_string = s;
2741 }
2742 return OK;
2743 }
2744 }
2745
2746 EMSG2(_(e_letwrong), op);
2747 return FAIL;
2748}
2749
2750/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002751 * Add a watcher to a list.
2752 */
2753 static void
2754list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002755 list_T *l;
2756 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002757{
2758 lw->lw_next = l->lv_watch;
2759 l->lv_watch = lw;
2760}
2761
2762/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002763 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002764 * No warning when it isn't found...
2765 */
2766 static void
2767list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002768 list_T *l;
2769 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002770{
Bram Moolenaar33570922005-01-25 22:26:29 +00002771 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002772
2773 lwp = &l->lv_watch;
2774 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2775 {
2776 if (lw == lwrem)
2777 {
2778 *lwp = lw->lw_next;
2779 break;
2780 }
2781 lwp = &lw->lw_next;
2782 }
2783}
2784
2785/*
2786 * Just before removing an item from a list: advance watchers to the next
2787 * item.
2788 */
2789 static void
2790list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002791 list_T *l;
2792 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002793{
Bram Moolenaar33570922005-01-25 22:26:29 +00002794 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002795
2796 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2797 if (lw->lw_item == item)
2798 lw->lw_item = item->li_next;
2799}
2800
2801/*
2802 * Evaluate the expression used in a ":for var in expr" command.
2803 * "arg" points to "var".
2804 * Set "*errp" to TRUE for an error, FALSE otherwise;
2805 * Return a pointer that holds the info. Null when there is an error.
2806 */
2807 void *
2808eval_for_line(arg, errp, nextcmdp, skip)
2809 char_u *arg;
2810 int *errp;
2811 char_u **nextcmdp;
2812 int skip;
2813{
Bram Moolenaar33570922005-01-25 22:26:29 +00002814 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002815 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002816 typval_T tv;
2817 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002818
2819 *errp = TRUE; /* default: there is an error */
2820
Bram Moolenaar33570922005-01-25 22:26:29 +00002821 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002822 if (fi == NULL)
2823 return NULL;
2824
2825 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2826 if (expr == NULL)
2827 return fi;
2828
2829 expr = skipwhite(expr);
2830 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2831 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002832 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002833 return fi;
2834 }
2835
2836 if (skip)
2837 ++emsg_skip;
2838 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2839 {
2840 *errp = FALSE;
2841 if (!skip)
2842 {
2843 l = tv.vval.v_list;
2844 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002845 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002846 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002847 clear_tv(&tv);
2848 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002849 else
2850 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002851 /* No need to increment the refcount, it's already set for the
2852 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002853 fi->fi_list = l;
2854 list_add_watch(l, &fi->fi_lw);
2855 fi->fi_lw.lw_item = l->lv_first;
2856 }
2857 }
2858 }
2859 if (skip)
2860 --emsg_skip;
2861
2862 return fi;
2863}
2864
2865/*
2866 * Use the first item in a ":for" list. Advance to the next.
2867 * Assign the values to the variable (list). "arg" points to the first one.
2868 * Return TRUE when a valid item was found, FALSE when at end of list or
2869 * something wrong.
2870 */
2871 int
2872next_for_item(fi_void, arg)
2873 void *fi_void;
2874 char_u *arg;
2875{
Bram Moolenaar33570922005-01-25 22:26:29 +00002876 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002877 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002879
2880 item = fi->fi_lw.lw_item;
2881 if (item == NULL)
2882 result = FALSE;
2883 else
2884 {
2885 fi->fi_lw.lw_item = item->li_next;
2886 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2887 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2888 }
2889 return result;
2890}
2891
2892/*
2893 * Free the structure used to store info used by ":for".
2894 */
2895 void
2896free_for_info(fi_void)
2897 void *fi_void;
2898{
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002900
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002901 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002902 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002903 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002904 list_unref(fi->fi_list);
2905 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002906 vim_free(fi);
2907}
2908
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2910
2911 void
2912set_context_for_expression(xp, arg, cmdidx)
2913 expand_T *xp;
2914 char_u *arg;
2915 cmdidx_T cmdidx;
2916{
2917 int got_eq = FALSE;
2918 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002919 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002921 if (cmdidx == CMD_let)
2922 {
2923 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002924 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002925 {
2926 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002927 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002928 {
2929 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002930 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002931 if (vim_iswhite(*p))
2932 break;
2933 }
2934 return;
2935 }
2936 }
2937 else
2938 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2939 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 while ((xp->xp_pattern = vim_strpbrk(arg,
2941 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2942 {
2943 c = *xp->xp_pattern;
2944 if (c == '&')
2945 {
2946 c = xp->xp_pattern[1];
2947 if (c == '&')
2948 {
2949 ++xp->xp_pattern;
2950 xp->xp_context = cmdidx != CMD_let || got_eq
2951 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2952 }
2953 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002956 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2957 xp->xp_pattern += 2;
2958
2959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 }
2961 else if (c == '$')
2962 {
2963 /* environment variable */
2964 xp->xp_context = EXPAND_ENV_VARS;
2965 }
2966 else if (c == '=')
2967 {
2968 got_eq = TRUE;
2969 xp->xp_context = EXPAND_EXPRESSION;
2970 }
2971 else if (c == '<'
2972 && xp->xp_context == EXPAND_FUNCTIONS
2973 && vim_strchr(xp->xp_pattern, '(') == NULL)
2974 {
2975 /* Function name can start with "<SNR>" */
2976 break;
2977 }
2978 else if (cmdidx != CMD_let || got_eq)
2979 {
2980 if (c == '"') /* string */
2981 {
2982 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2983 if (c == '\\' && xp->xp_pattern[1] != NUL)
2984 ++xp->xp_pattern;
2985 xp->xp_context = EXPAND_NOTHING;
2986 }
2987 else if (c == '\'') /* literal string */
2988 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002989 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2991 /* skip */ ;
2992 xp->xp_context = EXPAND_NOTHING;
2993 }
2994 else if (c == '|')
2995 {
2996 if (xp->xp_pattern[1] == '|')
2997 {
2998 ++xp->xp_pattern;
2999 xp->xp_context = EXPAND_EXPRESSION;
3000 }
3001 else
3002 xp->xp_context = EXPAND_COMMANDS;
3003 }
3004 else
3005 xp->xp_context = EXPAND_EXPRESSION;
3006 }
3007 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003008 /* Doesn't look like something valid, expand as an expression
3009 * anyway. */
3010 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 arg = xp->xp_pattern;
3012 if (*arg != NUL)
3013 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3014 /* skip */ ;
3015 }
3016 xp->xp_pattern = arg;
3017}
3018
3019#endif /* FEAT_CMDL_COMPL */
3020
3021/*
3022 * ":1,25call func(arg1, arg2)" function call.
3023 */
3024 void
3025ex_call(eap)
3026 exarg_T *eap;
3027{
3028 char_u *arg = eap->arg;
3029 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003031 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003033 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 linenr_T lnum;
3035 int doesrange;
3036 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003037 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003039 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3040 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003041 if (tofree == NULL)
3042 return;
3043
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003044 /* Increase refcount on dictionary, it could get deleted when evaluating
3045 * the arguments. */
3046 if (fudi.fd_dict != NULL)
3047 ++fudi.fd_dict->dv_refcount;
3048
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3050 len = STRLEN(tofree);
3051 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
Bram Moolenaar532c7802005-01-27 14:44:31 +00003053 /* Skip white space to allow ":call func ()". Not good, but required for
3054 * backward compatibility. */
3055 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003056 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057
3058 if (*startarg != '(')
3059 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003060 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 goto end;
3062 }
3063
3064 /*
3065 * When skipping, evaluate the function once, to find the end of the
3066 * arguments.
3067 * When the function takes a range, this is discovered after the first
3068 * call, and the loop is broken.
3069 */
3070 if (eap->skip)
3071 {
3072 ++emsg_skip;
3073 lnum = eap->line2; /* do it once, also with an invalid range */
3074 }
3075 else
3076 lnum = eap->line1;
3077 for ( ; lnum <= eap->line2; ++lnum)
3078 {
3079 if (!eap->skip && eap->addr_count > 0)
3080 {
3081 curwin->w_cursor.lnum = lnum;
3082 curwin->w_cursor.col = 0;
3083 }
3084 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003085 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003086 eap->line1, eap->line2, &doesrange,
3087 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 {
3089 failed = TRUE;
3090 break;
3091 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003092 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 if (doesrange || eap->skip)
3094 break;
3095 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003096 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003097 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003098 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 if (aborting())
3100 break;
3101 }
3102 if (eap->skip)
3103 --emsg_skip;
3104
3105 if (!failed)
3106 {
3107 /* Check for trailing illegal characters and a following command. */
3108 if (!ends_excmd(*arg))
3109 {
3110 emsg_severe = TRUE;
3111 EMSG(_(e_trailing));
3112 }
3113 else
3114 eap->nextcmd = check_nextcmd(arg);
3115 }
3116
3117end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003118 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120}
3121
3122/*
3123 * ":unlet[!] var1 ... " command.
3124 */
3125 void
3126ex_unlet(eap)
3127 exarg_T *eap;
3128{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003129 ex_unletlock(eap, eap->arg, 0);
3130}
3131
3132/*
3133 * ":lockvar" and ":unlockvar" commands
3134 */
3135 void
3136ex_lockvar(eap)
3137 exarg_T *eap;
3138{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003140 int deep = 2;
3141
3142 if (eap->forceit)
3143 deep = -1;
3144 else if (vim_isdigit(*arg))
3145 {
3146 deep = getdigits(&arg);
3147 arg = skipwhite(arg);
3148 }
3149
3150 ex_unletlock(eap, arg, deep);
3151}
3152
3153/*
3154 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3155 */
3156 static void
3157ex_unletlock(eap, argstart, deep)
3158 exarg_T *eap;
3159 char_u *argstart;
3160 int deep;
3161{
3162 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003165 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166
3167 do
3168 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003169 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003170 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3171 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003172 if (lv.ll_name == NULL)
3173 error = TRUE; /* error but continue parsing */
3174 if (name_end == NULL || (!vim_iswhite(*name_end)
3175 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003177 if (name_end != NULL)
3178 {
3179 emsg_severe = TRUE;
3180 EMSG(_(e_trailing));
3181 }
3182 if (!(eap->skip || error))
3183 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 break;
3185 }
3186
3187 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003188 {
3189 if (eap->cmdidx == CMD_unlet)
3190 {
3191 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3192 error = TRUE;
3193 }
3194 else
3195 {
3196 if (do_lock_var(&lv, name_end, deep,
3197 eap->cmdidx == CMD_lockvar) == FAIL)
3198 error = TRUE;
3199 }
3200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003202 if (!eap->skip)
3203 clear_lval(&lv);
3204
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 arg = skipwhite(name_end);
3206 } while (!ends_excmd(*arg));
3207
3208 eap->nextcmd = check_nextcmd(arg);
3209}
3210
Bram Moolenaar8c711452005-01-14 21:53:12 +00003211 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003212do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003214 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003215 int forceit;
3216{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003217 int ret = OK;
3218 int cc;
3219
3220 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003221 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003222 cc = *name_end;
3223 *name_end = NUL;
3224
3225 /* Normal name or expanded name. */
3226 if (check_changedtick(lp->ll_name))
3227 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003228 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003229 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003230 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003231 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003232 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3233 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003234 else if (lp->ll_range)
3235 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003236 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003237
3238 /* Delete a range of List items. */
3239 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3240 {
3241 li = lp->ll_li->li_next;
3242 listitem_remove(lp->ll_list, lp->ll_li);
3243 lp->ll_li = li;
3244 ++lp->ll_n1;
3245 }
3246 }
3247 else
3248 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003249 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003250 /* unlet a List item. */
3251 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003252 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003253 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003254 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003255 }
3256
3257 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003258}
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260/*
3261 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003262 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 */
3264 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003265do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003267 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
Bram Moolenaar33570922005-01-25 22:26:29 +00003269 hashtab_T *ht;
3270 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003271 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
Bram Moolenaar33570922005-01-25 22:26:29 +00003273 ht = find_var_ht(name, &varname);
3274 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 hi = hash_find(ht, varname);
3277 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003278 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003279 if (var_check_ro(HI2DI(hi)->di_flags, name))
3280 return FAIL;
3281 delete_var(ht, hi);
3282 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003285 if (forceit)
3286 return OK;
3287 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 return FAIL;
3289}
3290
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003291/*
3292 * Lock or unlock variable indicated by "lp".
3293 * "deep" is the levels to go (-1 for unlimited);
3294 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3295 */
3296 static int
3297do_lock_var(lp, name_end, deep, lock)
3298 lval_T *lp;
3299 char_u *name_end;
3300 int deep;
3301 int lock;
3302{
3303 int ret = OK;
3304 int cc;
3305 dictitem_T *di;
3306
3307 if (deep == 0) /* nothing to do */
3308 return OK;
3309
3310 if (lp->ll_tv == NULL)
3311 {
3312 cc = *name_end;
3313 *name_end = NUL;
3314
3315 /* Normal name or expanded name. */
3316 if (check_changedtick(lp->ll_name))
3317 ret = FAIL;
3318 else
3319 {
3320 di = find_var(lp->ll_name, NULL);
3321 if (di == NULL)
3322 ret = FAIL;
3323 else
3324 {
3325 if (lock)
3326 di->di_flags |= DI_FLAGS_LOCK;
3327 else
3328 di->di_flags &= ~DI_FLAGS_LOCK;
3329 item_lock(&di->di_tv, deep, lock);
3330 }
3331 }
3332 *name_end = cc;
3333 }
3334 else if (lp->ll_range)
3335 {
3336 listitem_T *li = lp->ll_li;
3337
3338 /* (un)lock a range of List items. */
3339 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3340 {
3341 item_lock(&li->li_tv, deep, lock);
3342 li = li->li_next;
3343 ++lp->ll_n1;
3344 }
3345 }
3346 else if (lp->ll_list != NULL)
3347 /* (un)lock a List item. */
3348 item_lock(&lp->ll_li->li_tv, deep, lock);
3349 else
3350 /* un(lock) a Dictionary item. */
3351 item_lock(&lp->ll_di->di_tv, deep, lock);
3352
3353 return ret;
3354}
3355
3356/*
3357 * Lock or unlock an item. "deep" is nr of levels to go.
3358 */
3359 static void
3360item_lock(tv, deep, lock)
3361 typval_T *tv;
3362 int deep;
3363 int lock;
3364{
3365 static int recurse = 0;
3366 list_T *l;
3367 listitem_T *li;
3368 dict_T *d;
3369 hashitem_T *hi;
3370 int todo;
3371
3372 if (recurse >= DICT_MAXNEST)
3373 {
3374 EMSG(_("E743: variable nested too deep for (un)lock"));
3375 return;
3376 }
3377 if (deep == 0)
3378 return;
3379 ++recurse;
3380
3381 /* lock/unlock the item itself */
3382 if (lock)
3383 tv->v_lock |= VAR_LOCKED;
3384 else
3385 tv->v_lock &= ~VAR_LOCKED;
3386
3387 switch (tv->v_type)
3388 {
3389 case VAR_LIST:
3390 if ((l = tv->vval.v_list) != NULL)
3391 {
3392 if (lock)
3393 l->lv_lock |= VAR_LOCKED;
3394 else
3395 l->lv_lock &= ~VAR_LOCKED;
3396 if (deep < 0 || deep > 1)
3397 /* recursive: lock/unlock the items the List contains */
3398 for (li = l->lv_first; li != NULL; li = li->li_next)
3399 item_lock(&li->li_tv, deep - 1, lock);
3400 }
3401 break;
3402 case VAR_DICT:
3403 if ((d = tv->vval.v_dict) != NULL)
3404 {
3405 if (lock)
3406 d->dv_lock |= VAR_LOCKED;
3407 else
3408 d->dv_lock &= ~VAR_LOCKED;
3409 if (deep < 0 || deep > 1)
3410 {
3411 /* recursive: lock/unlock the items the List contains */
3412 todo = d->dv_hashtab.ht_used;
3413 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3414 {
3415 if (!HASHITEM_EMPTY(hi))
3416 {
3417 --todo;
3418 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3419 }
3420 }
3421 }
3422 }
3423 }
3424 --recurse;
3425}
3426
Bram Moolenaara40058a2005-07-11 22:42:07 +00003427/*
3428 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3429 * it refers to a List or Dictionary that is locked.
3430 */
3431 static int
3432tv_islocked(tv)
3433 typval_T *tv;
3434{
3435 return (tv->v_lock & VAR_LOCKED)
3436 || (tv->v_type == VAR_LIST
3437 && tv->vval.v_list != NULL
3438 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3439 || (tv->v_type == VAR_DICT
3440 && tv->vval.v_dict != NULL
3441 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3442}
3443
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3445/*
3446 * Delete all "menutrans_" variables.
3447 */
3448 void
3449del_menutrans_vars()
3450{
Bram Moolenaar33570922005-01-25 22:26:29 +00003451 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003452 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
Bram Moolenaar33570922005-01-25 22:26:29 +00003454 hash_lock(&globvarht);
3455 todo = globvarht.ht_used;
3456 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003457 {
3458 if (!HASHITEM_EMPTY(hi))
3459 {
3460 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003461 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3462 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003463 }
3464 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003465 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466}
3467#endif
3468
3469#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3470
3471/*
3472 * Local string buffer for the next two functions to store a variable name
3473 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3474 * get_user_var_name().
3475 */
3476
3477static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3478
3479static char_u *varnamebuf = NULL;
3480static int varnamebuflen = 0;
3481
3482/*
3483 * Function to concatenate a prefix and a variable name.
3484 */
3485 static char_u *
3486cat_prefix_varname(prefix, name)
3487 int prefix;
3488 char_u *name;
3489{
3490 int len;
3491
3492 len = (int)STRLEN(name) + 3;
3493 if (len > varnamebuflen)
3494 {
3495 vim_free(varnamebuf);
3496 len += 10; /* some additional space */
3497 varnamebuf = alloc(len);
3498 if (varnamebuf == NULL)
3499 {
3500 varnamebuflen = 0;
3501 return NULL;
3502 }
3503 varnamebuflen = len;
3504 }
3505 *varnamebuf = prefix;
3506 varnamebuf[1] = ':';
3507 STRCPY(varnamebuf + 2, name);
3508 return varnamebuf;
3509}
3510
3511/*
3512 * Function given to ExpandGeneric() to obtain the list of user defined
3513 * (global/buffer/window/built-in) variable names.
3514 */
3515/*ARGSUSED*/
3516 char_u *
3517get_user_var_name(xp, idx)
3518 expand_T *xp;
3519 int idx;
3520{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003521 static long_u gdone;
3522 static long_u bdone;
3523 static long_u wdone;
3524 static int vidx;
3525 static hashitem_T *hi;
3526 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
3528 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003529 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003530
3531 /* Global variables */
3532 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003534 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003535 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003536 else
3537 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003538 while (HASHITEM_EMPTY(hi))
3539 ++hi;
3540 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3541 return cat_prefix_varname('g', hi->hi_key);
3542 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003544
3545 /* b: variables */
3546 ht = &curbuf->b_vars.dv_hashtab;
3547 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003549 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003550 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003551 else
3552 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003553 while (HASHITEM_EMPTY(hi))
3554 ++hi;
3555 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003557 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003559 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 return (char_u *)"b:changedtick";
3561 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003562
3563 /* w: variables */
3564 ht = &curwin->w_vars.dv_hashtab;
3565 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003567 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003568 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003569 else
3570 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003571 while (HASHITEM_EMPTY(hi))
3572 ++hi;
3573 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003575
3576 /* v: variables */
3577 if (vidx < VV_LEN)
3578 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
3580 vim_free(varnamebuf);
3581 varnamebuf = NULL;
3582 varnamebuflen = 0;
3583 return NULL;
3584}
3585
3586#endif /* FEAT_CMDL_COMPL */
3587
3588/*
3589 * types for expressions.
3590 */
3591typedef enum
3592{
3593 TYPE_UNKNOWN = 0
3594 , TYPE_EQUAL /* == */
3595 , TYPE_NEQUAL /* != */
3596 , TYPE_GREATER /* > */
3597 , TYPE_GEQUAL /* >= */
3598 , TYPE_SMALLER /* < */
3599 , TYPE_SEQUAL /* <= */
3600 , TYPE_MATCH /* =~ */
3601 , TYPE_NOMATCH /* !~ */
3602} exptype_T;
3603
3604/*
3605 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003606 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3608 */
3609
3610/*
3611 * Handle zero level expression.
3612 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003613 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003614 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 * Return OK or FAIL.
3616 */
3617 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003618eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 char_u **nextcmd;
3622 int evaluate;
3623{
3624 int ret;
3625 char_u *p;
3626
3627 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003628 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (ret == FAIL || !ends_excmd(*p))
3630 {
3631 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003632 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 /*
3634 * Report the invalid expression unless the expression evaluation has
3635 * been cancelled due to an aborting error, an interrupt, or an
3636 * exception.
3637 */
3638 if (!aborting())
3639 EMSG2(_(e_invexpr2), arg);
3640 ret = FAIL;
3641 }
3642 if (nextcmd != NULL)
3643 *nextcmd = check_nextcmd(p);
3644
3645 return ret;
3646}
3647
3648/*
3649 * Handle top level expression:
3650 * expr1 ? expr0 : expr0
3651 *
3652 * "arg" must point to the first non-white of the expression.
3653 * "arg" is advanced to the next non-white after the recognized expression.
3654 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003655 * Note: "rettv.v_lock" is not set.
3656 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 * Return OK or FAIL.
3658 */
3659 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003660eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 int evaluate;
3664{
3665 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003666 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
3668 /*
3669 * Get the first variable.
3670 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003671 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 return FAIL;
3673
3674 if ((*arg)[0] == '?')
3675 {
3676 result = FALSE;
3677 if (evaluate)
3678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003679 int error = FALSE;
3680
3681 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003683 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003684 if (error)
3685 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
3687
3688 /*
3689 * Get the second variable.
3690 */
3691 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003692 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 return FAIL;
3694
3695 /*
3696 * Check for the ":".
3697 */
3698 if ((*arg)[0] != ':')
3699 {
3700 EMSG(_("E109: Missing ':' after '?'"));
3701 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003702 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 return FAIL;
3704 }
3705
3706 /*
3707 * Get the third variable.
3708 */
3709 *arg = skipwhite(*arg + 1);
3710 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3711 {
3712 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003713 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 return FAIL;
3715 }
3716 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003717 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
3719
3720 return OK;
3721}
3722
3723/*
3724 * Handle first level expression:
3725 * expr2 || expr2 || expr2 logical OR
3726 *
3727 * "arg" must point to the first non-white of the expression.
3728 * "arg" is advanced to the next non-white after the recognized expression.
3729 *
3730 * Return OK or FAIL.
3731 */
3732 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003733eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 int evaluate;
3737{
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 long result;
3740 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003741 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742
3743 /*
3744 * Get the first variable.
3745 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003746 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 return FAIL;
3748
3749 /*
3750 * Repeat until there is no following "||".
3751 */
3752 first = TRUE;
3753 result = FALSE;
3754 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3755 {
3756 if (evaluate && first)
3757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003758 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003760 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003761 if (error)
3762 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 first = FALSE;
3764 }
3765
3766 /*
3767 * Get the second variable.
3768 */
3769 *arg = skipwhite(*arg + 2);
3770 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3771 return FAIL;
3772
3773 /*
3774 * Compute the result.
3775 */
3776 if (evaluate && !result)
3777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003778 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003780 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003781 if (error)
3782 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784 if (evaluate)
3785 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003786 rettv->v_type = VAR_NUMBER;
3787 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
3789 }
3790
3791 return OK;
3792}
3793
3794/*
3795 * Handle second level expression:
3796 * expr3 && expr3 && expr3 logical AND
3797 *
3798 * "arg" must point to the first non-white of the expression.
3799 * "arg" is advanced to the next non-white after the recognized expression.
3800 *
3801 * Return OK or FAIL.
3802 */
3803 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003804eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 int evaluate;
3808{
Bram Moolenaar33570922005-01-25 22:26:29 +00003809 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 long result;
3811 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003812 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
3814 /*
3815 * Get the first variable.
3816 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003817 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 return FAIL;
3819
3820 /*
3821 * Repeat until there is no following "&&".
3822 */
3823 first = TRUE;
3824 result = TRUE;
3825 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3826 {
3827 if (evaluate && first)
3828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003829 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003832 if (error)
3833 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 first = FALSE;
3835 }
3836
3837 /*
3838 * Get the second variable.
3839 */
3840 *arg = skipwhite(*arg + 2);
3841 if (eval4(arg, &var2, evaluate && result) == FAIL)
3842 return FAIL;
3843
3844 /*
3845 * Compute the result.
3846 */
3847 if (evaluate && result)
3848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003849 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003851 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003852 if (error)
3853 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
3855 if (evaluate)
3856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003857 rettv->v_type = VAR_NUMBER;
3858 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 }
3860 }
3861
3862 return OK;
3863}
3864
3865/*
3866 * Handle third level expression:
3867 * var1 == var2
3868 * var1 =~ var2
3869 * var1 != var2
3870 * var1 !~ var2
3871 * var1 > var2
3872 * var1 >= var2
3873 * var1 < var2
3874 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003875 * var1 is var2
3876 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 *
3878 * "arg" must point to the first non-white of the expression.
3879 * "arg" is advanced to the next non-white after the recognized expression.
3880 *
3881 * Return OK or FAIL.
3882 */
3883 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003884eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 int evaluate;
3888{
Bram Moolenaar33570922005-01-25 22:26:29 +00003889 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 char_u *p;
3891 int i;
3892 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003893 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 int len = 2;
3895 long n1, n2;
3896 char_u *s1, *s2;
3897 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3898 regmatch_T regmatch;
3899 int ic;
3900 char_u *save_cpo;
3901
3902 /*
3903 * Get the first variable.
3904 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003905 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 return FAIL;
3907
3908 p = *arg;
3909 switch (p[0])
3910 {
3911 case '=': if (p[1] == '=')
3912 type = TYPE_EQUAL;
3913 else if (p[1] == '~')
3914 type = TYPE_MATCH;
3915 break;
3916 case '!': if (p[1] == '=')
3917 type = TYPE_NEQUAL;
3918 else if (p[1] == '~')
3919 type = TYPE_NOMATCH;
3920 break;
3921 case '>': if (p[1] != '=')
3922 {
3923 type = TYPE_GREATER;
3924 len = 1;
3925 }
3926 else
3927 type = TYPE_GEQUAL;
3928 break;
3929 case '<': if (p[1] != '=')
3930 {
3931 type = TYPE_SMALLER;
3932 len = 1;
3933 }
3934 else
3935 type = TYPE_SEQUAL;
3936 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003937 case 'i': if (p[1] == 's')
3938 {
3939 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3940 len = 5;
3941 if (!vim_isIDc(p[len]))
3942 {
3943 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3944 type_is = TRUE;
3945 }
3946 }
3947 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
3949
3950 /*
3951 * If there is a comparitive operator, use it.
3952 */
3953 if (type != TYPE_UNKNOWN)
3954 {
3955 /* extra question mark appended: ignore case */
3956 if (p[len] == '?')
3957 {
3958 ic = TRUE;
3959 ++len;
3960 }
3961 /* extra '#' appended: match case */
3962 else if (p[len] == '#')
3963 {
3964 ic = FALSE;
3965 ++len;
3966 }
3967 /* nothing appened: use 'ignorecase' */
3968 else
3969 ic = p_ic;
3970
3971 /*
3972 * Get the second variable.
3973 */
3974 *arg = skipwhite(p + len);
3975 if (eval5(arg, &var2, evaluate) == FAIL)
3976 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003977 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 return FAIL;
3979 }
3980
3981 if (evaluate)
3982 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003983 if (type_is && rettv->v_type != var2.v_type)
3984 {
3985 /* For "is" a different type always means FALSE, for "notis"
3986 * it means TRUE. */
3987 n1 = (type == TYPE_NEQUAL);
3988 }
3989 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3990 {
3991 if (type_is)
3992 {
3993 n1 = (rettv->v_type == var2.v_type
3994 && rettv->vval.v_list == var2.vval.v_list);
3995 if (type == TYPE_NEQUAL)
3996 n1 = !n1;
3997 }
3998 else if (rettv->v_type != var2.v_type
3999 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4000 {
4001 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004002 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004003 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004004 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004005 clear_tv(rettv);
4006 clear_tv(&var2);
4007 return FAIL;
4008 }
4009 else
4010 {
4011 /* Compare two Lists for being equal or unequal. */
4012 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4013 if (type == TYPE_NEQUAL)
4014 n1 = !n1;
4015 }
4016 }
4017
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004018 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4019 {
4020 if (type_is)
4021 {
4022 n1 = (rettv->v_type == var2.v_type
4023 && rettv->vval.v_dict == var2.vval.v_dict);
4024 if (type == TYPE_NEQUAL)
4025 n1 = !n1;
4026 }
4027 else if (rettv->v_type != var2.v_type
4028 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4029 {
4030 if (rettv->v_type != var2.v_type)
4031 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4032 else
4033 EMSG(_("E736: Invalid operation for Dictionary"));
4034 clear_tv(rettv);
4035 clear_tv(&var2);
4036 return FAIL;
4037 }
4038 else
4039 {
4040 /* Compare two Dictionaries for being equal or unequal. */
4041 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4042 if (type == TYPE_NEQUAL)
4043 n1 = !n1;
4044 }
4045 }
4046
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004047 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4048 {
4049 if (rettv->v_type != var2.v_type
4050 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4051 {
4052 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004053 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004054 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004055 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004056 clear_tv(rettv);
4057 clear_tv(&var2);
4058 return FAIL;
4059 }
4060 else
4061 {
4062 /* Compare two Funcrefs for being equal or unequal. */
4063 if (rettv->vval.v_string == NULL
4064 || var2.vval.v_string == NULL)
4065 n1 = FALSE;
4066 else
4067 n1 = STRCMP(rettv->vval.v_string,
4068 var2.vval.v_string) == 0;
4069 if (type == TYPE_NEQUAL)
4070 n1 = !n1;
4071 }
4072 }
4073
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 /*
4075 * If one of the two variables is a number, compare as a number.
4076 * When using "=~" or "!~", always compare as string.
4077 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004078 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 n1 = get_tv_number(rettv);
4082 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 switch (type)
4084 {
4085 case TYPE_EQUAL: n1 = (n1 == n2); break;
4086 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4087 case TYPE_GREATER: n1 = (n1 > n2); break;
4088 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4089 case TYPE_SMALLER: n1 = (n1 < n2); break;
4090 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4091 case TYPE_UNKNOWN:
4092 case TYPE_MATCH:
4093 case TYPE_NOMATCH: break; /* avoid gcc warning */
4094 }
4095 }
4096 else
4097 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 s1 = get_tv_string_buf(rettv, buf1);
4099 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4101 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4102 else
4103 i = 0;
4104 n1 = FALSE;
4105 switch (type)
4106 {
4107 case TYPE_EQUAL: n1 = (i == 0); break;
4108 case TYPE_NEQUAL: n1 = (i != 0); break;
4109 case TYPE_GREATER: n1 = (i > 0); break;
4110 case TYPE_GEQUAL: n1 = (i >= 0); break;
4111 case TYPE_SMALLER: n1 = (i < 0); break;
4112 case TYPE_SEQUAL: n1 = (i <= 0); break;
4113
4114 case TYPE_MATCH:
4115 case TYPE_NOMATCH:
4116 /* avoid 'l' flag in 'cpoptions' */
4117 save_cpo = p_cpo;
4118 p_cpo = (char_u *)"";
4119 regmatch.regprog = vim_regcomp(s2,
4120 RE_MAGIC + RE_STRING);
4121 regmatch.rm_ic = ic;
4122 if (regmatch.regprog != NULL)
4123 {
4124 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4125 vim_free(regmatch.regprog);
4126 if (type == TYPE_NOMATCH)
4127 n1 = !n1;
4128 }
4129 p_cpo = save_cpo;
4130 break;
4131
4132 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4133 }
4134 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135 clear_tv(rettv);
4136 clear_tv(&var2);
4137 rettv->v_type = VAR_NUMBER;
4138 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140 }
4141
4142 return OK;
4143}
4144
4145/*
4146 * Handle fourth level expression:
4147 * + number addition
4148 * - number subtraction
4149 * . string concatenation
4150 *
4151 * "arg" must point to the first non-white of the expression.
4152 * "arg" is advanced to the next non-white after the recognized expression.
4153 *
4154 * Return OK or FAIL.
4155 */
4156 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 int evaluate;
4161{
Bram Moolenaar33570922005-01-25 22:26:29 +00004162 typval_T var2;
4163 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 int op;
4165 long n1, n2;
4166 char_u *s1, *s2;
4167 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4168 char_u *p;
4169
4170 /*
4171 * Get the first variable.
4172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 return FAIL;
4175
4176 /*
4177 * Repeat computing, until no '+', '-' or '.' is following.
4178 */
4179 for (;;)
4180 {
4181 op = **arg;
4182 if (op != '+' && op != '-' && op != '.')
4183 break;
4184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004185 if (op != '+' || rettv->v_type != VAR_LIST)
4186 {
4187 /* For "list + ...", an illegal use of the first operand as
4188 * a number cannot be determined before evaluating the 2nd
4189 * operand: if this is also a list, all is ok.
4190 * For "something . ...", "something - ..." or "non-list + ...",
4191 * we know that the first operand needs to be a string or number
4192 * without evaluating the 2nd operand. So check before to avoid
4193 * side effects after an error. */
4194 if (evaluate && get_tv_string_chk(rettv) == NULL)
4195 {
4196 clear_tv(rettv);
4197 return FAIL;
4198 }
4199 }
4200
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 /*
4202 * Get the second variable.
4203 */
4204 *arg = skipwhite(*arg + 1);
4205 if (eval6(arg, &var2, evaluate) == FAIL)
4206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 return FAIL;
4209 }
4210
4211 if (evaluate)
4212 {
4213 /*
4214 * Compute the result.
4215 */
4216 if (op == '.')
4217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004218 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4219 s2 = get_tv_string_buf_chk(&var2, buf2);
4220 if (s2 == NULL) /* type error ? */
4221 {
4222 clear_tv(rettv);
4223 clear_tv(&var2);
4224 return FAIL;
4225 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004226 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004227 clear_tv(rettv);
4228 rettv->v_type = VAR_STRING;
4229 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004231 else if (op == '+' && rettv->v_type == VAR_LIST
4232 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004233 {
4234 /* concatenate Lists */
4235 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4236 &var3) == FAIL)
4237 {
4238 clear_tv(rettv);
4239 clear_tv(&var2);
4240 return FAIL;
4241 }
4242 clear_tv(rettv);
4243 *rettv = var3;
4244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 else
4246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004247 int error = FALSE;
4248
4249 n1 = get_tv_number_chk(rettv, &error);
4250 if (error)
4251 {
4252 /* This can only happen for "list + non-list".
4253 * For "non-list + ..." or "something - ...", we returned
4254 * before evaluating the 2nd operand. */
4255 clear_tv(rettv);
4256 return FAIL;
4257 }
4258 n2 = get_tv_number_chk(&var2, &error);
4259 if (error)
4260 {
4261 clear_tv(rettv);
4262 clear_tv(&var2);
4263 return FAIL;
4264 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 if (op == '+')
4267 n1 = n1 + n2;
4268 else
4269 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 rettv->v_type = VAR_NUMBER;
4271 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 }
4276 return OK;
4277}
4278
4279/*
4280 * Handle fifth level expression:
4281 * * number multiplication
4282 * / number division
4283 * % number modulo
4284 *
4285 * "arg" must point to the first non-white of the expression.
4286 * "arg" is advanced to the next non-white after the recognized expression.
4287 *
4288 * Return OK or FAIL.
4289 */
4290 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 int evaluate;
4295{
Bram Moolenaar33570922005-01-25 22:26:29 +00004296 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 int op;
4298 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004299 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300
4301 /*
4302 * Get the first variable.
4303 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 return FAIL;
4306
4307 /*
4308 * Repeat computing, until no '*', '/' or '%' is following.
4309 */
4310 for (;;)
4311 {
4312 op = **arg;
4313 if (op != '*' && op != '/' && op != '%')
4314 break;
4315
4316 if (evaluate)
4317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 if (error)
4321 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 }
4323 else
4324 n1 = 0;
4325
4326 /*
4327 * Get the second variable.
4328 */
4329 *arg = skipwhite(*arg + 1);
4330 if (eval7(arg, &var2, evaluate) == FAIL)
4331 return FAIL;
4332
4333 if (evaluate)
4334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 if (error)
4338 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
4340 /*
4341 * Compute the result.
4342 */
4343 if (op == '*')
4344 n1 = n1 * n2;
4345 else if (op == '/')
4346 {
4347 if (n2 == 0) /* give an error message? */
4348 n1 = 0x7fffffffL;
4349 else
4350 n1 = n1 / n2;
4351 }
4352 else
4353 {
4354 if (n2 == 0) /* give an error message? */
4355 n1 = 0;
4356 else
4357 n1 = n1 % n2;
4358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004359 rettv->v_type = VAR_NUMBER;
4360 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 }
4363
4364 return OK;
4365}
4366
4367/*
4368 * Handle sixth level expression:
4369 * number number constant
4370 * "string" string contstant
4371 * 'string' literal string contstant
4372 * &option-name option value
4373 * @r register contents
4374 * identifier variable value
4375 * function() function call
4376 * $VAR environment variable
4377 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004378 * [expr, expr] List
4379 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 *
4381 * Also handle:
4382 * ! in front logical NOT
4383 * - in front unary minus
4384 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004385 * trailing [] subscript in String or List
4386 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 *
4388 * "arg" must point to the first non-white of the expression.
4389 * "arg" is advanced to the next non-white after the recognized expression.
4390 *
4391 * Return OK or FAIL.
4392 */
4393 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 int evaluate;
4398{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 long n;
4400 int len;
4401 char_u *s;
4402 int val;
4403 char_u *start_leader, *end_leader;
4404 int ret = OK;
4405 char_u *alias;
4406
4407 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004409 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004411 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412
4413 /*
4414 * Skip '!' and '-' characters. They are handled later.
4415 */
4416 start_leader = *arg;
4417 while (**arg == '!' || **arg == '-' || **arg == '+')
4418 *arg = skipwhite(*arg + 1);
4419 end_leader = *arg;
4420
4421 switch (**arg)
4422 {
4423 /*
4424 * Number constant.
4425 */
4426 case '0':
4427 case '1':
4428 case '2':
4429 case '3':
4430 case '4':
4431 case '5':
4432 case '6':
4433 case '7':
4434 case '8':
4435 case '9':
4436 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4437 *arg += len;
4438 if (evaluate)
4439 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004440 rettv->v_type = VAR_NUMBER;
4441 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443 break;
4444
4445 /*
4446 * String constant: "string".
4447 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004448 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 break;
4450
4451 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004452 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004454 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004455 break;
4456
4457 /*
4458 * List: [expr, expr]
4459 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004460 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 break;
4462
4463 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004464 * Dictionary: {key: val, key: val}
4465 */
4466 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4467 break;
4468
4469 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004470 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004472 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 break;
4474
4475 /*
4476 * Environment variable: $VAR.
4477 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004478 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 break;
4480
4481 /*
4482 * Register contents: @r.
4483 */
4484 case '@': ++*arg;
4485 if (evaluate)
4486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004487 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004488 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490 if (**arg != NUL)
4491 ++*arg;
4492 break;
4493
4494 /*
4495 * nested expression: (expression).
4496 */
4497 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004498 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 if (**arg == ')')
4500 ++*arg;
4501 else if (ret == OK)
4502 {
4503 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004504 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 ret = FAIL;
4506 }
4507 break;
4508
Bram Moolenaar8c711452005-01-14 21:53:12 +00004509 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 break;
4511 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004512
4513 if (ret == NOTDONE)
4514 {
4515 /*
4516 * Must be a variable or function name.
4517 * Can also be a curly-braces kind of name: {expr}.
4518 */
4519 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004520 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004521 if (alias != NULL)
4522 s = alias;
4523
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004524 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004525 ret = FAIL;
4526 else
4527 {
4528 if (**arg == '(') /* recursive! */
4529 {
4530 /* If "s" is the name of a variable of type VAR_FUNC
4531 * use its contents. */
4532 s = deref_func_name(s, &len);
4533
4534 /* Invoke the function. */
4535 ret = get_func_tv(s, len, rettv, arg,
4536 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004537 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004538 /* Stop the expression evaluation when immediately
4539 * aborting on error, or when an interrupt occurred or
4540 * an exception was thrown but not caught. */
4541 if (aborting())
4542 {
4543 if (ret == OK)
4544 clear_tv(rettv);
4545 ret = FAIL;
4546 }
4547 }
4548 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004549 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004550 else
4551 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004552 }
4553
4554 if (alias != NULL)
4555 vim_free(alias);
4556 }
4557
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 *arg = skipwhite(*arg);
4559
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004560 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4561 * expr(expr). */
4562 if (ret == OK)
4563 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
4565 /*
4566 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4567 */
4568 if (ret == OK && evaluate && end_leader > start_leader)
4569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004570 int error = FALSE;
4571
4572 val = get_tv_number_chk(rettv, &error);
4573 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004575 clear_tv(rettv);
4576 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004578 else
4579 {
4580 while (end_leader > start_leader)
4581 {
4582 --end_leader;
4583 if (*end_leader == '!')
4584 val = !val;
4585 else if (*end_leader == '-')
4586 val = -val;
4587 }
4588 clear_tv(rettv);
4589 rettv->v_type = VAR_NUMBER;
4590 rettv->vval.v_number = val;
4591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593
4594 return ret;
4595}
4596
4597/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004598 * Evaluate an "[expr]" or "[expr:expr]" index.
4599 * "*arg" points to the '['.
4600 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4601 */
4602 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004603eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004604 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004605 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004606 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004607 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004608{
4609 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004610 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004611 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004612 long len = -1;
4613 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004614 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004615 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004618 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004619 if (verbose)
4620 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 return FAIL;
4622 }
4623
Bram Moolenaar8c711452005-01-14 21:53:12 +00004624 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004625 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004626 /*
4627 * dict.name
4628 */
4629 key = *arg + 1;
4630 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4631 ;
4632 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004633 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004634 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004635 }
4636 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004637 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004638 /*
4639 * something[idx]
4640 *
4641 * Get the (first) variable from inside the [].
4642 */
4643 *arg = skipwhite(*arg + 1);
4644 if (**arg == ':')
4645 empty1 = TRUE;
4646 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4647 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004648 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4649 {
4650 /* not a number or string */
4651 clear_tv(&var1);
4652 return FAIL;
4653 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004654
4655 /*
4656 * Get the second variable from inside the [:].
4657 */
4658 if (**arg == ':')
4659 {
4660 range = TRUE;
4661 *arg = skipwhite(*arg + 1);
4662 if (**arg == ']')
4663 empty2 = TRUE;
4664 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4665 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004666 if (!empty1)
4667 clear_tv(&var1);
4668 return FAIL;
4669 }
4670 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4671 {
4672 /* not a number or string */
4673 if (!empty1)
4674 clear_tv(&var1);
4675 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004676 return FAIL;
4677 }
4678 }
4679
4680 /* Check for the ']'. */
4681 if (**arg != ']')
4682 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004683 if (verbose)
4684 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004685 clear_tv(&var1);
4686 if (range)
4687 clear_tv(&var2);
4688 return FAIL;
4689 }
4690 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004691 }
4692
4693 if (evaluate)
4694 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004695 n1 = 0;
4696 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004697 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004698 n1 = get_tv_number(&var1);
4699 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004700 }
4701 if (range)
4702 {
4703 if (empty2)
4704 n2 = -1;
4705 else
4706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004707 n2 = get_tv_number(&var2);
4708 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709 }
4710 }
4711
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004712 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004713 {
4714 case VAR_NUMBER:
4715 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004716 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004717 len = (long)STRLEN(s);
4718 if (range)
4719 {
4720 /* The resulting variable is a substring. If the indexes
4721 * are out of range the result is empty. */
4722 if (n1 < 0)
4723 {
4724 n1 = len + n1;
4725 if (n1 < 0)
4726 n1 = 0;
4727 }
4728 if (n2 < 0)
4729 n2 = len + n2;
4730 else if (n2 >= len)
4731 n2 = len;
4732 if (n1 >= len || n2 < 0 || n1 > n2)
4733 s = NULL;
4734 else
4735 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4736 }
4737 else
4738 {
4739 /* The resulting variable is a string of a single
4740 * character. If the index is too big or negative the
4741 * result is empty. */
4742 if (n1 >= len || n1 < 0)
4743 s = NULL;
4744 else
4745 s = vim_strnsave(s + n1, 1);
4746 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004747 clear_tv(rettv);
4748 rettv->v_type = VAR_STRING;
4749 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 break;
4751
4752 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004753 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754 if (n1 < 0)
4755 n1 = len + n1;
4756 if (!empty1 && (n1 < 0 || n1 >= len))
4757 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004758 if (verbose)
4759 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004760 return FAIL;
4761 }
4762 if (range)
4763 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004764 list_T *l;
4765 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766
4767 if (n2 < 0)
4768 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004769 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004771 if (verbose)
4772 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773 return FAIL;
4774 }
4775 l = list_alloc();
4776 if (l == NULL)
4777 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004778 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004779 n1 <= n2; ++n1)
4780 {
4781 if (list_append_tv(l, &item->li_tv) == FAIL)
4782 {
4783 list_free(l);
4784 return FAIL;
4785 }
4786 item = item->li_next;
4787 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004788 clear_tv(rettv);
4789 rettv->v_type = VAR_LIST;
4790 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004791 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004792 }
4793 else
4794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004795 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004796 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004797 clear_tv(rettv);
4798 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799 }
4800 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004801
4802 case VAR_DICT:
4803 if (range)
4804 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004805 if (verbose)
4806 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004807 if (len == -1)
4808 clear_tv(&var1);
4809 return FAIL;
4810 }
4811 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004812 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004813
4814 if (len == -1)
4815 {
4816 key = get_tv_string(&var1);
4817 if (*key == NUL)
4818 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004819 if (verbose)
4820 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004821 clear_tv(&var1);
4822 return FAIL;
4823 }
4824 }
4825
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004826 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004827
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004828 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004829 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004830 if (len == -1)
4831 clear_tv(&var1);
4832 if (item == NULL)
4833 return FAIL;
4834
4835 copy_tv(&item->di_tv, &var1);
4836 clear_tv(rettv);
4837 *rettv = var1;
4838 }
4839 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004840 }
4841 }
4842
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004843 return OK;
4844}
4845
4846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 * Get an option value.
4848 * "arg" points to the '&' or '+' before the option name.
4849 * "arg" is advanced to character after the option name.
4850 * Return OK or FAIL.
4851 */
4852 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004853get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004855 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 int evaluate;
4857{
4858 char_u *option_end;
4859 long numval;
4860 char_u *stringval;
4861 int opt_type;
4862 int c;
4863 int working = (**arg == '+'); /* has("+option") */
4864 int ret = OK;
4865 int opt_flags;
4866
4867 /*
4868 * Isolate the option name and find its value.
4869 */
4870 option_end = find_option_end(arg, &opt_flags);
4871 if (option_end == NULL)
4872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004873 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 EMSG2(_("E112: Option name missing: %s"), *arg);
4875 return FAIL;
4876 }
4877
4878 if (!evaluate)
4879 {
4880 *arg = option_end;
4881 return OK;
4882 }
4883
4884 c = *option_end;
4885 *option_end = NUL;
4886 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888
4889 if (opt_type == -3) /* invalid name */
4890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004891 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 EMSG2(_("E113: Unknown option: %s"), *arg);
4893 ret = FAIL;
4894 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
4897 if (opt_type == -2) /* hidden string option */
4898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004899 rettv->v_type = VAR_STRING;
4900 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902 else if (opt_type == -1) /* hidden number option */
4903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 rettv->v_type = VAR_NUMBER;
4905 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907 else if (opt_type == 1) /* number option */
4908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909 rettv->v_type = VAR_NUMBER;
4910 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 else /* string option */
4913 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 rettv->v_type = VAR_STRING;
4915 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 }
4918 else if (working && (opt_type == -2 || opt_type == -1))
4919 ret = FAIL;
4920
4921 *option_end = c; /* put back for error messages */
4922 *arg = option_end;
4923
4924 return ret;
4925}
4926
4927/*
4928 * Allocate a variable for a string constant.
4929 * Return OK or FAIL.
4930 */
4931 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004932get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 int evaluate;
4936{
4937 char_u *p;
4938 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 int extra = 0;
4940
4941 /*
4942 * Find the end of the string, skipping backslashed characters.
4943 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004944 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
4946 if (*p == '\\' && p[1] != NUL)
4947 {
4948 ++p;
4949 /* A "\<x>" form occupies at least 4 characters, and produces up
4950 * to 6 characters: reserve space for 2 extra */
4951 if (*p == '<')
4952 extra += 2;
4953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
4955
4956 if (*p != '"')
4957 {
4958 EMSG2(_("E114: Missing quote: %s"), *arg);
4959 return FAIL;
4960 }
4961
4962 /* If only parsing, set *arg and return here */
4963 if (!evaluate)
4964 {
4965 *arg = p + 1;
4966 return OK;
4967 }
4968
4969 /*
4970 * Copy the string into allocated memory, handling backslashed
4971 * characters.
4972 */
4973 name = alloc((unsigned)(p - *arg + extra));
4974 if (name == NULL)
4975 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 rettv->v_type = VAR_STRING;
4977 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
4981 if (*p == '\\')
4982 {
4983 switch (*++p)
4984 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004985 case 'b': *name++ = BS; ++p; break;
4986 case 'e': *name++ = ESC; ++p; break;
4987 case 'f': *name++ = FF; ++p; break;
4988 case 'n': *name++ = NL; ++p; break;
4989 case 'r': *name++ = CAR; ++p; break;
4990 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991
4992 case 'X': /* hex: "\x1", "\x12" */
4993 case 'x':
4994 case 'u': /* Unicode: "\u0023" */
4995 case 'U':
4996 if (vim_isxdigit(p[1]))
4997 {
4998 int n, nr;
4999 int c = toupper(*p);
5000
5001 if (c == 'X')
5002 n = 2;
5003 else
5004 n = 4;
5005 nr = 0;
5006 while (--n >= 0 && vim_isxdigit(p[1]))
5007 {
5008 ++p;
5009 nr = (nr << 4) + hex2nr(*p);
5010 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012#ifdef FEAT_MBYTE
5013 /* For "\u" store the number according to
5014 * 'encoding'. */
5015 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 else
5018#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005019 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 break;
5022
5023 /* octal: "\1", "\12", "\123" */
5024 case '0':
5025 case '1':
5026 case '2':
5027 case '3':
5028 case '4':
5029 case '5':
5030 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 case '7': *name = *p++ - '0';
5032 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005034 *name = (*name << 3) + *p++ - '0';
5035 if (*p >= '0' && *p <= '7')
5036 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 break;
5040
5041 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 if (extra != 0)
5044 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 break;
5047 }
5048 /* FALLTHROUGH */
5049
Bram Moolenaar8c711452005-01-14 21:53:12 +00005050 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 break;
5052 }
5053 }
5054 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005058 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 *arg = p + 1;
5060
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 return OK;
5062}
5063
5064/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 * Return OK or FAIL.
5067 */
5068 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 int evaluate;
5073{
5074 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005075 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076 int reduce = 0;
5077
5078 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005080 */
5081 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5082 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005084 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005086 break;
5087 ++reduce;
5088 ++p;
5089 }
5090 }
5091
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005093 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005095 return FAIL;
5096 }
5097
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099 if (!evaluate)
5100 {
5101 *arg = p + 1;
5102 return OK;
5103 }
5104
5105 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005107 */
5108 str = alloc((unsigned)((p - *arg) - reduce));
5109 if (str == NULL)
5110 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 rettv->v_type = VAR_STRING;
5112 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005113
Bram Moolenaar8c711452005-01-14 21:53:12 +00005114 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005115 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005117 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005119 break;
5120 ++p;
5121 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005122 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005123 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005124 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005125 *arg = p + 1;
5126
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005127 return OK;
5128}
5129
5130/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005131 * Allocate a variable for a List and fill it from "*arg".
5132 * Return OK or FAIL.
5133 */
5134 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005135get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005136 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005137 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005138 int evaluate;
5139{
Bram Moolenaar33570922005-01-25 22:26:29 +00005140 list_T *l = NULL;
5141 typval_T tv;
5142 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143
5144 if (evaluate)
5145 {
5146 l = list_alloc();
5147 if (l == NULL)
5148 return FAIL;
5149 }
5150
5151 *arg = skipwhite(*arg + 1);
5152 while (**arg != ']' && **arg != NUL)
5153 {
5154 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5155 goto failret;
5156 if (evaluate)
5157 {
5158 item = listitem_alloc();
5159 if (item != NULL)
5160 {
5161 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005162 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005163 list_append(l, item);
5164 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005165 else
5166 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005167 }
5168
5169 if (**arg == ']')
5170 break;
5171 if (**arg != ',')
5172 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 goto failret;
5175 }
5176 *arg = skipwhite(*arg + 1);
5177 }
5178
5179 if (**arg != ']')
5180 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005182failret:
5183 if (evaluate)
5184 list_free(l);
5185 return FAIL;
5186 }
5187
5188 *arg = skipwhite(*arg + 1);
5189 if (evaluate)
5190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005191 rettv->v_type = VAR_LIST;
5192 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005193 ++l->lv_refcount;
5194 }
5195
5196 return OK;
5197}
5198
5199/*
5200 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005201 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005202 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005203 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005204list_alloc()
5205{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005206 list_T *l;
5207
5208 l = (list_T *)alloc_clear(sizeof(list_T));
5209 if (l != NULL)
5210 {
5211 /* Prepend the list to the list of lists for garbage collection. */
5212 if (first_list != NULL)
5213 first_list->lv_used_prev = l;
5214 l->lv_used_prev = NULL;
5215 l->lv_used_next = first_list;
5216 first_list = l;
5217 }
5218 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005219}
5220
5221/*
5222 * Unreference a list: decrement the reference count and free it when it
5223 * becomes zero.
5224 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005225 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005227 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005229 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5230 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005231}
5232
5233/*
5234 * Free a list, including all items it points to.
5235 * Ignores the reference count.
5236 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005237 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005239 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240{
Bram Moolenaar33570922005-01-25 22:26:29 +00005241 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242
Bram Moolenaard9fba312005-06-26 22:34:35 +00005243 /* Avoid that recursive reference to the list frees us again. */
5244 l->lv_refcount = DEL_REFCOUNT;
5245
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005246 /* Remove the list from the list of lists for garbage collection. */
5247 if (l->lv_used_prev == NULL)
5248 first_list = l->lv_used_next;
5249 else
5250 l->lv_used_prev->lv_used_next = l->lv_used_next;
5251 if (l->lv_used_next != NULL)
5252 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5253
Bram Moolenaard9fba312005-06-26 22:34:35 +00005254 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005256 /* Remove the item before deleting it. */
5257 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 listitem_free(item);
5259 }
5260 vim_free(l);
5261}
5262
5263/*
5264 * Allocate a list item.
5265 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005266 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267listitem_alloc()
5268{
Bram Moolenaar33570922005-01-25 22:26:29 +00005269 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270}
5271
5272/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005273 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 */
5275 static void
5276listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005277 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005279 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 vim_free(item);
5281}
5282
5283/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005284 * Remove a list item from a List and free it. Also clears the value.
5285 */
5286 static void
5287listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005288 list_T *l;
5289 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005290{
5291 list_remove(l, item, item);
5292 listitem_free(item);
5293}
5294
5295/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 * Get the number of items in a list.
5297 */
5298 static long
5299list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005300 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 if (l == NULL)
5303 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005304 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305}
5306
5307/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005308 * Return TRUE when two lists have exactly the same values.
5309 */
5310 static int
5311list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005312 list_T *l1;
5313 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005314 int ic; /* ignore case for strings */
5315{
Bram Moolenaar33570922005-01-25 22:26:29 +00005316 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005317
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005318 if (list_len(l1) != list_len(l2))
5319 return FALSE;
5320
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005321 for (item1 = l1->lv_first, item2 = l2->lv_first;
5322 item1 != NULL && item2 != NULL;
5323 item1 = item1->li_next, item2 = item2->li_next)
5324 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5325 return FALSE;
5326 return item1 == NULL && item2 == NULL;
5327}
5328
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005329#if defined(FEAT_PYTHON) || defined(PROTO)
5330/*
5331 * Return the dictitem that an entry in a hashtable points to.
5332 */
5333 dictitem_T *
5334dict_lookup(hi)
5335 hashitem_T *hi;
5336{
5337 return HI2DI(hi);
5338}
5339#endif
5340
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005341/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005342 * Return TRUE when two dictionaries have exactly the same key/values.
5343 */
5344 static int
5345dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005346 dict_T *d1;
5347 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005348 int ic; /* ignore case for strings */
5349{
Bram Moolenaar33570922005-01-25 22:26:29 +00005350 hashitem_T *hi;
5351 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005352 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005353
5354 if (dict_len(d1) != dict_len(d2))
5355 return FALSE;
5356
Bram Moolenaar33570922005-01-25 22:26:29 +00005357 todo = d1->dv_hashtab.ht_used;
5358 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005359 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005360 if (!HASHITEM_EMPTY(hi))
5361 {
5362 item2 = dict_find(d2, hi->hi_key, -1);
5363 if (item2 == NULL)
5364 return FALSE;
5365 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5366 return FALSE;
5367 --todo;
5368 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005369 }
5370 return TRUE;
5371}
5372
5373/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005374 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005375 * Compares the items just like "==" would compare them, but strings and
5376 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005377 */
5378 static int
5379tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005380 typval_T *tv1;
5381 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005382 int ic; /* ignore case */
5383{
5384 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005385 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005386
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005387 if (tv1->v_type != tv2->v_type)
5388 return FALSE;
5389
5390 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005391 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005392 case VAR_LIST:
5393 /* recursive! */
5394 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5395
5396 case VAR_DICT:
5397 /* recursive! */
5398 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5399
5400 case VAR_FUNC:
5401 return (tv1->vval.v_string != NULL
5402 && tv2->vval.v_string != NULL
5403 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5404
5405 case VAR_NUMBER:
5406 return tv1->vval.v_number == tv2->vval.v_number;
5407
5408 case VAR_STRING:
5409 s1 = get_tv_string_buf(tv1, buf1);
5410 s2 = get_tv_string_buf(tv2, buf2);
5411 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005412 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005413
5414 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005415 return TRUE;
5416}
5417
5418/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419 * Locate item with index "n" in list "l" and return it.
5420 * A negative index is counted from the end; -1 is the last item.
5421 * Returns NULL when "n" is out of range.
5422 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005423 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005425 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 long n;
5427{
Bram Moolenaar33570922005-01-25 22:26:29 +00005428 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 long idx;
5430
5431 if (l == NULL)
5432 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005433
5434 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005436 n = l->lv_len + n;
5437
5438 /* Check for index out of range. */
5439 if (n < 0 || n >= l->lv_len)
5440 return NULL;
5441
5442 /* When there is a cached index may start search from there. */
5443 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005445 if (n < l->lv_idx / 2)
5446 {
5447 /* closest to the start of the list */
5448 item = l->lv_first;
5449 idx = 0;
5450 }
5451 else if (n > (l->lv_idx + l->lv_len) / 2)
5452 {
5453 /* closest to the end of the list */
5454 item = l->lv_last;
5455 idx = l->lv_len - 1;
5456 }
5457 else
5458 {
5459 /* closest to the cached index */
5460 item = l->lv_idx_item;
5461 idx = l->lv_idx;
5462 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 }
5464 else
5465 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005466 if (n < l->lv_len / 2)
5467 {
5468 /* closest to the start of the list */
5469 item = l->lv_first;
5470 idx = 0;
5471 }
5472 else
5473 {
5474 /* closest to the end of the list */
5475 item = l->lv_last;
5476 idx = l->lv_len - 1;
5477 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005479
5480 while (n > idx)
5481 {
5482 /* search forward */
5483 item = item->li_next;
5484 ++idx;
5485 }
5486 while (n < idx)
5487 {
5488 /* search backward */
5489 item = item->li_prev;
5490 --idx;
5491 }
5492
5493 /* cache the used index */
5494 l->lv_idx = idx;
5495 l->lv_idx_item = item;
5496
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005497 return item;
5498}
5499
5500/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005501 * Locate "item" list "l" and return its index.
5502 * Returns -1 when "item" is not in the list.
5503 */
5504 static long
5505list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005506 list_T *l;
5507 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005508{
5509 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005510 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005511
5512 if (l == NULL)
5513 return -1;
5514 idx = 0;
5515 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5516 ++idx;
5517 if (li == NULL)
5518 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005519 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005520}
5521
5522/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 * Append item "item" to the end of list "l".
5524 */
5525 static void
5526list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005527 list_T *l;
5528 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529{
5530 if (l->lv_last == NULL)
5531 {
5532 /* empty list */
5533 l->lv_first = item;
5534 l->lv_last = item;
5535 item->li_prev = NULL;
5536 }
5537 else
5538 {
5539 l->lv_last->li_next = item;
5540 item->li_prev = l->lv_last;
5541 l->lv_last = item;
5542 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005543 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 item->li_next = NULL;
5545}
5546
5547/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005548 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005549 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 */
5551 static int
5552list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005553 list_T *l;
5554 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005556 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557
Bram Moolenaar05159a02005-02-26 23:04:13 +00005558 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005560 copy_tv(tv, &li->li_tv);
5561 list_append(l, li);
5562 return OK;
5563}
5564
5565/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005566 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005567 * Return FAIL when out of memory.
5568 */
5569 int
5570list_append_dict(list, dict)
5571 list_T *list;
5572 dict_T *dict;
5573{
5574 listitem_T *li = listitem_alloc();
5575
5576 if (li == NULL)
5577 return FAIL;
5578 li->li_tv.v_type = VAR_DICT;
5579 li->li_tv.v_lock = 0;
5580 li->li_tv.vval.v_dict = dict;
5581 list_append(list, li);
5582 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005583 return OK;
5584}
5585
5586/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005587 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005588 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005589 * Returns FAIL when out of memory.
5590 */
5591 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005592list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005593 list_T *l;
5594 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005595 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005596{
5597 listitem_T *li = listitem_alloc();
5598
5599 if (li == NULL)
5600 return FAIL;
5601 list_append(l, li);
5602 li->li_tv.v_type = VAR_STRING;
5603 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005604 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5605 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005606 return FAIL;
5607 return OK;
5608}
5609
5610/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005611 * Append "n" to list "l".
5612 * Returns FAIL when out of memory.
5613 */
5614 static int
5615list_append_number(l, n)
5616 list_T *l;
5617 varnumber_T n;
5618{
5619 listitem_T *li;
5620
5621 li = listitem_alloc();
5622 if (li == NULL)
5623 return FAIL;
5624 li->li_tv.v_type = VAR_NUMBER;
5625 li->li_tv.v_lock = 0;
5626 li->li_tv.vval.v_number = n;
5627 list_append(l, li);
5628 return OK;
5629}
5630
5631/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005632 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005633 * If "item" is NULL append at the end.
5634 * Return FAIL when out of memory.
5635 */
5636 static int
5637list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005638 list_T *l;
5639 typval_T *tv;
5640 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005641{
Bram Moolenaar33570922005-01-25 22:26:29 +00005642 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005643
5644 if (ni == NULL)
5645 return FAIL;
5646 copy_tv(tv, &ni->li_tv);
5647 if (item == NULL)
5648 /* Append new item at end of list. */
5649 list_append(l, ni);
5650 else
5651 {
5652 /* Insert new item before existing item. */
5653 ni->li_prev = item->li_prev;
5654 ni->li_next = item;
5655 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005656 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005657 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005658 ++l->lv_idx;
5659 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005660 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005661 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005662 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005663 l->lv_idx_item = NULL;
5664 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005665 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005666 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005667 }
5668 return OK;
5669}
5670
5671/*
5672 * Extend "l1" with "l2".
5673 * If "bef" is NULL append at the end, otherwise insert before this item.
5674 * Returns FAIL when out of memory.
5675 */
5676 static int
5677list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005678 list_T *l1;
5679 list_T *l2;
5680 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005681{
Bram Moolenaar33570922005-01-25 22:26:29 +00005682 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005683
5684 for (item = l2->lv_first; item != NULL; item = item->li_next)
5685 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5686 return FAIL;
5687 return OK;
5688}
5689
5690/*
5691 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5692 * Return FAIL when out of memory.
5693 */
5694 static int
5695list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005696 list_T *l1;
5697 list_T *l2;
5698 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005699{
Bram Moolenaar33570922005-01-25 22:26:29 +00005700 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005701
5702 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005703 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005704 if (l == NULL)
5705 return FAIL;
5706 tv->v_type = VAR_LIST;
5707 tv->vval.v_list = l;
5708
5709 /* append all items from the second list */
5710 return list_extend(l, l2, NULL);
5711}
5712
5713/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005714 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005715 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005716 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 * Returns NULL when out of memory.
5718 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005719 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005720list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005721 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005722 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005723 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724{
Bram Moolenaar33570922005-01-25 22:26:29 +00005725 list_T *copy;
5726 listitem_T *item;
5727 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728
5729 if (orig == NULL)
5730 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731
5732 copy = list_alloc();
5733 if (copy != NULL)
5734 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005735 if (copyID != 0)
5736 {
5737 /* Do this before adding the items, because one of the items may
5738 * refer back to this list. */
5739 orig->lv_copyID = copyID;
5740 orig->lv_copylist = copy;
5741 }
5742 for (item = orig->lv_first; item != NULL && !got_int;
5743 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005744 {
5745 ni = listitem_alloc();
5746 if (ni == NULL)
5747 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005748 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005749 {
5750 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5751 {
5752 vim_free(ni);
5753 break;
5754 }
5755 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005757 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005758 list_append(copy, ni);
5759 }
5760 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005761 if (item != NULL)
5762 {
5763 list_unref(copy);
5764 copy = NULL;
5765 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 }
5767
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768 return copy;
5769}
5770
5771/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005772 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005773 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005774 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005775 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005776list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005777 list_T *l;
5778 listitem_T *item;
5779 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005780{
Bram Moolenaar33570922005-01-25 22:26:29 +00005781 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005783 /* notify watchers */
5784 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005785 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005786 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005787 list_fix_watch(l, ip);
5788 if (ip == item2)
5789 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005790 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005791
5792 if (item2->li_next == NULL)
5793 l->lv_last = item->li_prev;
5794 else
5795 item2->li_next->li_prev = item->li_prev;
5796 if (item->li_prev == NULL)
5797 l->lv_first = item2->li_next;
5798 else
5799 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005800 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005801}
5802
5803/*
5804 * Return an allocated string with the string representation of a list.
5805 * May return NULL.
5806 */
5807 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005808list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005809 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005810 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811{
5812 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813
5814 if (tv->vval.v_list == NULL)
5815 return NULL;
5816 ga_init2(&ga, (int)sizeof(char), 80);
5817 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005818 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005819 {
5820 vim_free(ga.ga_data);
5821 return NULL;
5822 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823 ga_append(&ga, ']');
5824 ga_append(&ga, NUL);
5825 return (char_u *)ga.ga_data;
5826}
5827
5828/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005829 * Join list "l" into a string in "*gap", using separator "sep".
5830 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005831 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005832 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005833 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005834list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005836 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005837 char_u *sep;
5838 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005839 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005840{
5841 int first = TRUE;
5842 char_u *tofree;
5843 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005844 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005845 char_u *s;
5846
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005847 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 {
5849 if (first)
5850 first = FALSE;
5851 else
5852 ga_concat(gap, sep);
5853
5854 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005855 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005857 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 if (s != NULL)
5859 ga_concat(gap, s);
5860 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005861 if (s == NULL)
5862 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005864 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865}
5866
5867/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005868 * Garbage collection for lists and dictionaries.
5869 *
5870 * We use reference counts to be able to free most items right away when they
5871 * are no longer used. But for composite items it's possible that it becomes
5872 * unused while the reference count is > 0: When there is a recursive
5873 * reference. Example:
5874 * :let l = [1, 2, 3]
5875 * :let d = {9: l}
5876 * :let l[1] = d
5877 *
5878 * Since this is quite unusual we handle this with garbage collection: every
5879 * once in a while find out which lists and dicts are not referenced from any
5880 * variable.
5881 *
5882 * Here is a good reference text about garbage collection (refers to Python
5883 * but it applies to all reference-counting mechanisms):
5884 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005885 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005886
5887/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005888 * Do garbage collection for lists and dicts.
5889 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005890 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005891 int
5892garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005893{
5894 dict_T *dd;
5895 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005896 int copyID = ++current_copyID;
5897 buf_T *buf;
5898 win_T *wp;
5899 int i;
5900 funccall_T *fc;
5901 int did_free = FALSE;
5902
5903 /*
5904 * 1. Go through all accessible variables and mark all lists and dicts
5905 * with copyID.
5906 */
5907 /* script-local variables */
5908 for (i = 1; i <= ga_scripts.ga_len; ++i)
5909 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5910
5911 /* buffer-local variables */
5912 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5913 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5914
5915 /* window-local variables */
5916 FOR_ALL_WINDOWS(wp)
5917 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5918
5919 /* global variables */
5920 set_ref_in_ht(&globvarht, copyID);
5921
5922 /* function-local variables */
5923 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5924 {
5925 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5926 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5927 }
5928
5929 /*
5930 * 2. Go through the list of dicts and free items without the copyID.
5931 */
5932 for (dd = first_dict; dd != NULL; )
5933 if (dd->dv_copyID != copyID)
5934 {
5935 dict_free(dd);
5936 did_free = TRUE;
5937
5938 /* restart, next dict may also have been freed */
5939 dd = first_dict;
5940 }
5941 else
5942 dd = dd->dv_used_next;
5943
5944 /*
5945 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005946 * But don't free a list that has a watcher (used in a for loop), these
5947 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005948 */
5949 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005950 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005951 {
5952 list_free(ll);
5953 did_free = TRUE;
5954
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005955 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005956 ll = first_list;
5957 }
5958 else
5959 ll = ll->lv_used_next;
5960
5961 return did_free;
5962}
5963
5964/*
5965 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5966 */
5967 static void
5968set_ref_in_ht(ht, copyID)
5969 hashtab_T *ht;
5970 int copyID;
5971{
5972 int todo;
5973 hashitem_T *hi;
5974
5975 todo = ht->ht_used;
5976 for (hi = ht->ht_array; todo > 0; ++hi)
5977 if (!HASHITEM_EMPTY(hi))
5978 {
5979 --todo;
5980 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5981 }
5982}
5983
5984/*
5985 * Mark all lists and dicts referenced through list "l" with "copyID".
5986 */
5987 static void
5988set_ref_in_list(l, copyID)
5989 list_T *l;
5990 int copyID;
5991{
5992 listitem_T *li;
5993
5994 for (li = l->lv_first; li != NULL; li = li->li_next)
5995 set_ref_in_item(&li->li_tv, copyID);
5996}
5997
5998/*
5999 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6000 */
6001 static void
6002set_ref_in_item(tv, copyID)
6003 typval_T *tv;
6004 int copyID;
6005{
6006 dict_T *dd;
6007 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006008
6009 switch (tv->v_type)
6010 {
6011 case VAR_DICT:
6012 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006013 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006014 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006015 /* Didn't see this dict yet. */
6016 dd->dv_copyID = copyID;
6017 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006018 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006019 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006020
6021 case VAR_LIST:
6022 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006023 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006024 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006025 /* Didn't see this list yet. */
6026 ll->lv_copyID = copyID;
6027 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006028 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006029 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006030 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006031 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006032}
6033
6034/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006035 * Allocate an empty header for a dictionary.
6036 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006037 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006038dict_alloc()
6039{
Bram Moolenaar33570922005-01-25 22:26:29 +00006040 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006041
Bram Moolenaar33570922005-01-25 22:26:29 +00006042 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006043 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006044 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006045 /* Add the list to the hashtable for garbage collection. */
6046 if (first_dict != NULL)
6047 first_dict->dv_used_prev = d;
6048 d->dv_used_next = first_dict;
6049 d->dv_used_prev = NULL;
6050
Bram Moolenaar33570922005-01-25 22:26:29 +00006051 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006052 d->dv_lock = 0;
6053 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006054 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006055 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006056 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006057}
6058
6059/*
6060 * Unreference a Dictionary: decrement the reference count and free it when it
6061 * becomes zero.
6062 */
6063 static void
6064dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006065 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006066{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006067 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6068 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006069}
6070
6071/*
6072 * Free a Dictionary, including all items it contains.
6073 * Ignores the reference count.
6074 */
6075 static void
6076dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006077 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006078{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006079 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006080 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006081 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006082
Bram Moolenaard9fba312005-06-26 22:34:35 +00006083 /* Avoid that recursive reference to the dict frees us again. */
6084 d->dv_refcount = DEL_REFCOUNT;
6085
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006086 /* Remove the dict from the list of dicts for garbage collection. */
6087 if (d->dv_used_prev == NULL)
6088 first_dict = d->dv_used_next;
6089 else
6090 d->dv_used_prev->dv_used_next = d->dv_used_next;
6091 if (d->dv_used_next != NULL)
6092 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6093
6094 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006095 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006096 todo = d->dv_hashtab.ht_used;
6097 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006098 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006099 if (!HASHITEM_EMPTY(hi))
6100 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006101 /* Remove the item before deleting it, just in case there is
6102 * something recursive causing trouble. */
6103 di = HI2DI(hi);
6104 hash_remove(&d->dv_hashtab, hi);
6105 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006106 --todo;
6107 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006108 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006109 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006110 vim_free(d);
6111}
6112
6113/*
6114 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006115 * The "key" is copied to the new item.
6116 * Note that the value of the item "di_tv" still needs to be initialized!
6117 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006118 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006119 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006120dictitem_alloc(key)
6121 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006122{
Bram Moolenaar33570922005-01-25 22:26:29 +00006123 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006124
Bram Moolenaar33570922005-01-25 22:26:29 +00006125 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006126 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006127 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006128 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006129 di->di_flags = 0;
6130 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006131 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006132}
6133
6134/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006135 * Make a copy of a Dictionary item.
6136 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006137 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006138dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006139 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006140{
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006142
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006144 if (di != NULL)
6145 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006146 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006147 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006148 copy_tv(&org->di_tv, &di->di_tv);
6149 }
6150 return di;
6151}
6152
6153/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006154 * Remove item "item" from Dictionary "dict" and free it.
6155 */
6156 static void
6157dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 dict_T *dict;
6159 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006160{
Bram Moolenaar33570922005-01-25 22:26:29 +00006161 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006162
Bram Moolenaar33570922005-01-25 22:26:29 +00006163 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006164 if (HASHITEM_EMPTY(hi))
6165 EMSG2(_(e_intern2), "dictitem_remove()");
6166 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006168 dictitem_free(item);
6169}
6170
6171/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006172 * Free a dict item. Also clears the value.
6173 */
6174 static void
6175dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006176 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006177{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006178 clear_tv(&item->di_tv);
6179 vim_free(item);
6180}
6181
6182/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006183 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6184 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006185 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006186 * Returns NULL when out of memory.
6187 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006188 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006189dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006190 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006191 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006192 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006193{
Bram Moolenaar33570922005-01-25 22:26:29 +00006194 dict_T *copy;
6195 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006196 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006197 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006198
6199 if (orig == NULL)
6200 return NULL;
6201
6202 copy = dict_alloc();
6203 if (copy != NULL)
6204 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006205 if (copyID != 0)
6206 {
6207 orig->dv_copyID = copyID;
6208 orig->dv_copydict = copy;
6209 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006210 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006211 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006212 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006213 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006214 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006215 --todo;
6216
6217 di = dictitem_alloc(hi->hi_key);
6218 if (di == NULL)
6219 break;
6220 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006221 {
6222 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6223 copyID) == FAIL)
6224 {
6225 vim_free(di);
6226 break;
6227 }
6228 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006229 else
6230 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6231 if (dict_add(copy, di) == FAIL)
6232 {
6233 dictitem_free(di);
6234 break;
6235 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006236 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006237 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006238
Bram Moolenaare9a41262005-01-15 22:18:47 +00006239 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006240 if (todo > 0)
6241 {
6242 dict_unref(copy);
6243 copy = NULL;
6244 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006245 }
6246
6247 return copy;
6248}
6249
6250/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006251 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006252 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006253 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006254 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006255dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006256 dict_T *d;
6257 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006258{
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006260}
6261
Bram Moolenaar8c711452005-01-14 21:53:12 +00006262/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006263 * Add a number or string entry to dictionary "d".
6264 * When "str" is NULL use number "nr", otherwise use "str".
6265 * Returns FAIL when out of memory and when key already exists.
6266 */
6267 int
6268dict_add_nr_str(d, key, nr, str)
6269 dict_T *d;
6270 char *key;
6271 long nr;
6272 char_u *str;
6273{
6274 dictitem_T *item;
6275
6276 item = dictitem_alloc((char_u *)key);
6277 if (item == NULL)
6278 return FAIL;
6279 item->di_tv.v_lock = 0;
6280 if (str == NULL)
6281 {
6282 item->di_tv.v_type = VAR_NUMBER;
6283 item->di_tv.vval.v_number = nr;
6284 }
6285 else
6286 {
6287 item->di_tv.v_type = VAR_STRING;
6288 item->di_tv.vval.v_string = vim_strsave(str);
6289 }
6290 if (dict_add(d, item) == FAIL)
6291 {
6292 dictitem_free(item);
6293 return FAIL;
6294 }
6295 return OK;
6296}
6297
6298/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006299 * Get the number of items in a Dictionary.
6300 */
6301 static long
6302dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006303 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006304{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006305 if (d == NULL)
6306 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006307 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006308}
6309
6310/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006311 * Find item "key[len]" in Dictionary "d".
6312 * If "len" is negative use strlen(key).
6313 * Returns NULL when not found.
6314 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006315 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006316dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006317 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006318 char_u *key;
6319 int len;
6320{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006321#define AKEYLEN 200
6322 char_u buf[AKEYLEN];
6323 char_u *akey;
6324 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006326
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006327 if (len < 0)
6328 akey = key;
6329 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006330 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006331 tofree = akey = vim_strnsave(key, len);
6332 if (akey == NULL)
6333 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006334 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006335 else
6336 {
6337 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006338 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006339 akey = buf;
6340 }
6341
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006343 vim_free(tofree);
6344 if (HASHITEM_EMPTY(hi))
6345 return NULL;
6346 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006347}
6348
6349/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006350 * Get a string item from a dictionary.
6351 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006352 * Returns NULL if the entry doesn't exist or out of memory.
6353 */
6354 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006355get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006356 dict_T *d;
6357 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006358 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006359{
6360 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006361 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006362
6363 di = dict_find(d, key, -1);
6364 if (di == NULL)
6365 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006366 s = get_tv_string(&di->di_tv);
6367 if (save && s != NULL)
6368 s = vim_strsave(s);
6369 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006370}
6371
6372/*
6373 * Get a number item from a dictionary.
6374 * Returns 0 if the entry doesn't exist or out of memory.
6375 */
6376 long
6377get_dict_number(d, key)
6378 dict_T *d;
6379 char_u *key;
6380{
6381 dictitem_T *di;
6382
6383 di = dict_find(d, key, -1);
6384 if (di == NULL)
6385 return 0;
6386 return get_tv_number(&di->di_tv);
6387}
6388
6389/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006390 * Return an allocated string with the string representation of a Dictionary.
6391 * May return NULL.
6392 */
6393 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006394dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006395 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006396 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006397{
6398 garray_T ga;
6399 int first = TRUE;
6400 char_u *tofree;
6401 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006403 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006405 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006406
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006407 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006408 return NULL;
6409 ga_init2(&ga, (int)sizeof(char), 80);
6410 ga_append(&ga, '{');
6411
Bram Moolenaar33570922005-01-25 22:26:29 +00006412 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006413 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006414 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006415 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006416 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006417 --todo;
6418
6419 if (first)
6420 first = FALSE;
6421 else
6422 ga_concat(&ga, (char_u *)", ");
6423
6424 tofree = string_quote(hi->hi_key, FALSE);
6425 if (tofree != NULL)
6426 {
6427 ga_concat(&ga, tofree);
6428 vim_free(tofree);
6429 }
6430 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006431 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006432 if (s != NULL)
6433 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006434 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006435 if (s == NULL)
6436 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006437 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006438 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006439 if (todo > 0)
6440 {
6441 vim_free(ga.ga_data);
6442 return NULL;
6443 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006444
6445 ga_append(&ga, '}');
6446 ga_append(&ga, NUL);
6447 return (char_u *)ga.ga_data;
6448}
6449
6450/*
6451 * Allocate a variable for a Dictionary and fill it from "*arg".
6452 * Return OK or FAIL. Returns NOTDONE for {expr}.
6453 */
6454 static int
6455get_dict_tv(arg, rettv, evaluate)
6456 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006457 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006458 int evaluate;
6459{
Bram Moolenaar33570922005-01-25 22:26:29 +00006460 dict_T *d = NULL;
6461 typval_T tvkey;
6462 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006463 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006464 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006465 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006466 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006467
6468 /*
6469 * First check if it's not a curly-braces thing: {expr}.
6470 * Must do this without evaluating, otherwise a function may be called
6471 * twice. Unfortunately this means we need to call eval1() twice for the
6472 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006473 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006474 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006475 if (*start != '}')
6476 {
6477 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6478 return FAIL;
6479 if (*start == '}')
6480 return NOTDONE;
6481 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006482
6483 if (evaluate)
6484 {
6485 d = dict_alloc();
6486 if (d == NULL)
6487 return FAIL;
6488 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006489 tvkey.v_type = VAR_UNKNOWN;
6490 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006491
6492 *arg = skipwhite(*arg + 1);
6493 while (**arg != '}' && **arg != NUL)
6494 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006495 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006496 goto failret;
6497 if (**arg != ':')
6498 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006499 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006500 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006501 goto failret;
6502 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006503 key = get_tv_string_buf_chk(&tvkey, buf);
6504 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006506 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6507 if (key != NULL)
6508 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006509 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006510 goto failret;
6511 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006512
6513 *arg = skipwhite(*arg + 1);
6514 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6515 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006516 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006517 goto failret;
6518 }
6519 if (evaluate)
6520 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006521 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006522 if (item != NULL)
6523 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006524 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006525 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006526 clear_tv(&tv);
6527 goto failret;
6528 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006529 item = dictitem_alloc(key);
6530 clear_tv(&tvkey);
6531 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006532 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006533 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006534 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006535 if (dict_add(d, item) == FAIL)
6536 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006537 }
6538 }
6539
6540 if (**arg == '}')
6541 break;
6542 if (**arg != ',')
6543 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006544 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006545 goto failret;
6546 }
6547 *arg = skipwhite(*arg + 1);
6548 }
6549
6550 if (**arg != '}')
6551 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006552 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006553failret:
6554 if (evaluate)
6555 dict_free(d);
6556 return FAIL;
6557 }
6558
6559 *arg = skipwhite(*arg + 1);
6560 if (evaluate)
6561 {
6562 rettv->v_type = VAR_DICT;
6563 rettv->vval.v_dict = d;
6564 ++d->dv_refcount;
6565 }
6566
6567 return OK;
6568}
6569
Bram Moolenaar8c711452005-01-14 21:53:12 +00006570/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006571 * Return a string with the string representation of a variable.
6572 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006574 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006575 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576 * May return NULL;
6577 */
6578 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006579echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006580 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006583 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006585 static int recurse = 0;
6586 char_u *r = NULL;
6587
Bram Moolenaar33570922005-01-25 22:26:29 +00006588 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006589 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006590 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006591 *tofree = NULL;
6592 return NULL;
6593 }
6594 ++recurse;
6595
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596 switch (tv->v_type)
6597 {
6598 case VAR_FUNC:
6599 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006600 r = tv->vval.v_string;
6601 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006602
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006604 if (tv->vval.v_list == NULL)
6605 {
6606 *tofree = NULL;
6607 r = NULL;
6608 }
6609 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6610 {
6611 *tofree = NULL;
6612 r = (char_u *)"[...]";
6613 }
6614 else
6615 {
6616 tv->vval.v_list->lv_copyID = copyID;
6617 *tofree = list2string(tv, copyID);
6618 r = *tofree;
6619 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006620 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006621
Bram Moolenaar8c711452005-01-14 21:53:12 +00006622 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006623 if (tv->vval.v_dict == NULL)
6624 {
6625 *tofree = NULL;
6626 r = NULL;
6627 }
6628 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6629 {
6630 *tofree = NULL;
6631 r = (char_u *)"{...}";
6632 }
6633 else
6634 {
6635 tv->vval.v_dict->dv_copyID = copyID;
6636 *tofree = dict2string(tv, copyID);
6637 r = *tofree;
6638 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006639 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006641 case VAR_STRING:
6642 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006643 *tofree = NULL;
6644 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006646
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006647 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006648 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006649 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006650 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006651
6652 --recurse;
6653 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006654}
6655
6656/*
6657 * Return a string with the string representation of a variable.
6658 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6659 * "numbuf" is used for a number.
6660 * Puts quotes around strings, so that they can be parsed back by eval().
6661 * May return NULL;
6662 */
6663 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006664tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006665 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006666 char_u **tofree;
6667 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006668 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006669{
6670 switch (tv->v_type)
6671 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006672 case VAR_FUNC:
6673 *tofree = string_quote(tv->vval.v_string, TRUE);
6674 return *tofree;
6675 case VAR_STRING:
6676 *tofree = string_quote(tv->vval.v_string, FALSE);
6677 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006678 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006679 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006680 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006681 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006682 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006683 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006684 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006685 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006686}
6687
6688/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006689 * Return string "str" in ' quotes, doubling ' characters.
6690 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006691 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006692 */
6693 static char_u *
6694string_quote(str, function)
6695 char_u *str;
6696 int function;
6697{
Bram Moolenaar33570922005-01-25 22:26:29 +00006698 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006699 char_u *p, *r, *s;
6700
Bram Moolenaar33570922005-01-25 22:26:29 +00006701 len = (function ? 13 : 3);
6702 if (str != NULL)
6703 {
6704 len += STRLEN(str);
6705 for (p = str; *p != NUL; mb_ptr_adv(p))
6706 if (*p == '\'')
6707 ++len;
6708 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709 s = r = alloc(len);
6710 if (r != NULL)
6711 {
6712 if (function)
6713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006714 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006715 r += 10;
6716 }
6717 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006718 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006719 if (str != NULL)
6720 for (p = str; *p != NUL; )
6721 {
6722 if (*p == '\'')
6723 *r++ = '\'';
6724 MB_COPY_CHAR(p, r);
6725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006726 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006727 if (function)
6728 *r++ = ')';
6729 *r++ = NUL;
6730 }
6731 return s;
6732}
6733
6734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 * Get the value of an environment variable.
6736 * "arg" is pointing to the '$'. It is advanced to after the name.
6737 * If the environment variable was not set, silently assume it is empty.
6738 * Always return OK.
6739 */
6740 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006741get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 int evaluate;
6745{
6746 char_u *string = NULL;
6747 int len;
6748 int cc;
6749 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006750 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751
6752 ++*arg;
6753 name = *arg;
6754 len = get_env_len(arg);
6755 if (evaluate)
6756 {
6757 if (len != 0)
6758 {
6759 cc = name[len];
6760 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006761 /* first try vim_getenv(), fast for normal environment vars */
6762 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006764 {
6765 if (!mustfree)
6766 string = vim_strsave(string);
6767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 else
6769 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006770 if (mustfree)
6771 vim_free(string);
6772
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 /* next try expanding things like $VIM and ${HOME} */
6774 string = expand_env_save(name - 1);
6775 if (string != NULL && *string == '$')
6776 {
6777 vim_free(string);
6778 string = NULL;
6779 }
6780 }
6781 name[len] = cc;
6782 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006783 rettv->v_type = VAR_STRING;
6784 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 }
6786
6787 return OK;
6788}
6789
6790/*
6791 * Array with names and number of arguments of all internal functions
6792 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6793 */
6794static struct fst
6795{
6796 char *f_name; /* function name */
6797 char f_min_argc; /* minimal number of arguments */
6798 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006799 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006800 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801} functions[] =
6802{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006803 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 {"append", 2, 2, f_append},
6805 {"argc", 0, 0, f_argc},
6806 {"argidx", 0, 0, f_argidx},
6807 {"argv", 1, 1, f_argv},
6808 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006809 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 {"bufexists", 1, 1, f_bufexists},
6811 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6812 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6813 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6814 {"buflisted", 1, 1, f_buflisted},
6815 {"bufloaded", 1, 1, f_bufloaded},
6816 {"bufname", 1, 1, f_bufname},
6817 {"bufnr", 1, 1, f_bufnr},
6818 {"bufwinnr", 1, 1, f_bufwinnr},
6819 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006820 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006821 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 {"char2nr", 1, 1, f_char2nr},
6823 {"cindent", 1, 1, f_cindent},
6824 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006825#if defined(FEAT_INS_EXPAND)
6826 {"complete_add", 1, 1, f_complete_add},
6827 {"complete_check", 0, 0, f_complete_check},
6828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006830 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006831 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 {"cscope_connection",0,3, f_cscope_connection},
6833 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006834 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 {"delete", 1, 1, f_delete},
6836 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006837 {"diff_filler", 1, 1, f_diff_filler},
6838 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006839 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006841 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 {"eventhandler", 0, 0, f_eventhandler},
6843 {"executable", 1, 1, f_executable},
6844 {"exists", 1, 1, f_exists},
6845 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006846 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6848 {"filereadable", 1, 1, f_filereadable},
6849 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006850 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006851 {"finddir", 1, 3, f_finddir},
6852 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {"fnamemodify", 2, 2, f_fnamemodify},
6854 {"foldclosed", 1, 1, f_foldclosed},
6855 {"foldclosedend", 1, 1, f_foldclosedend},
6856 {"foldlevel", 1, 1, f_foldlevel},
6857 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006858 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006860 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006862 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006863 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 {"getbufvar", 2, 2, f_getbufvar},
6865 {"getchar", 0, 1, f_getchar},
6866 {"getcharmod", 0, 0, f_getcharmod},
6867 {"getcmdline", 0, 0, f_getcmdline},
6868 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006869 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006871 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006872 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 {"getfsize", 1, 1, f_getfsize},
6874 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006875 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006876 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006877 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006878 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006879 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {"getregtype", 0, 1, f_getregtype},
6881 {"getwinposx", 0, 0, f_getwinposx},
6882 {"getwinposy", 0, 0, f_getwinposy},
6883 {"getwinvar", 2, 2, f_getwinvar},
6884 {"glob", 1, 1, f_glob},
6885 {"globpath", 2, 2, f_globpath},
6886 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006887 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 {"hasmapto", 1, 2, f_hasmapto},
6889 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6890 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6891 {"histadd", 2, 2, f_histadd},
6892 {"histdel", 1, 2, f_histdel},
6893 {"histget", 1, 2, f_histget},
6894 {"histnr", 1, 1, f_histnr},
6895 {"hlID", 1, 1, f_hlID},
6896 {"hlexists", 1, 1, f_hlexists},
6897 {"hostname", 0, 0, f_hostname},
6898 {"iconv", 3, 3, f_iconv},
6899 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006900 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006901 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006903 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {"inputrestore", 0, 0, f_inputrestore},
6905 {"inputsave", 0, 0, f_inputsave},
6906 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006907 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006909 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006910 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006911 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006912 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006914 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {"libcall", 3, 3, f_libcall},
6916 {"libcallnr", 3, 3, f_libcallnr},
6917 {"line", 1, 1, f_line},
6918 {"line2byte", 1, 1, f_line2byte},
6919 {"lispindent", 1, 1, f_lispindent},
6920 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006921 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 {"maparg", 1, 2, f_maparg},
6923 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006924 {"match", 2, 4, f_match},
6925 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006926 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006927 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006928 {"max", 1, 1, f_max},
6929 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006930#ifdef vim_mkdir
6931 {"mkdir", 1, 3, f_mkdir},
6932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {"mode", 0, 0, f_mode},
6934 {"nextnonblank", 1, 1, f_nextnonblank},
6935 {"nr2char", 1, 1, f_nr2char},
6936 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006937 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006938 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006939 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006940 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {"remote_expr", 2, 3, f_remote_expr},
6942 {"remote_foreground", 1, 1, f_remote_foreground},
6943 {"remote_peek", 1, 2, f_remote_peek},
6944 {"remote_read", 1, 1, f_remote_read},
6945 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006946 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006948 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006950 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006952 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 {"searchpair", 3, 5, f_searchpair},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006954 {"searchpairpos", 3, 5, f_searchpairpos},
6955 {"searchpos", 1, 2, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {"server2client", 2, 2, f_server2client},
6957 {"serverlist", 0, 0, f_serverlist},
6958 {"setbufvar", 3, 3, f_setbufvar},
6959 {"setcmdpos", 1, 1, f_setcmdpos},
6960 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006961 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006962 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 {"setreg", 2, 3, f_setreg},
6964 {"setwinvar", 3, 3, f_setwinvar},
6965 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006966 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006967 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006968 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006969 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006970 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971#ifdef HAVE_STRFTIME
6972 {"strftime", 1, 2, f_strftime},
6973#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006974 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006975 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 {"strlen", 1, 1, f_strlen},
6977 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006978 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {"strtrans", 1, 1, f_strtrans},
6980 {"submatch", 1, 1, f_submatch},
6981 {"substitute", 4, 4, f_substitute},
6982 {"synID", 3, 3, f_synID},
6983 {"synIDattr", 2, 3, f_synIDattr},
6984 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006985 {"system", 1, 2, f_system},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006986 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006987 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006989 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {"tolower", 1, 1, f_tolower},
6991 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006992 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006994 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 {"virtcol", 1, 1, f_virtcol},
6996 {"visualmode", 0, 1, f_visualmode},
6997 {"winbufnr", 1, 1, f_winbufnr},
6998 {"wincol", 0, 0, f_wincol},
6999 {"winheight", 1, 1, f_winheight},
7000 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007001 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {"winrestcmd", 0, 0, f_winrestcmd},
7003 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007004 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005};
7006
7007#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7008
7009/*
7010 * Function given to ExpandGeneric() to obtain the list of internal
7011 * or user defined function names.
7012 */
7013 char_u *
7014get_function_name(xp, idx)
7015 expand_T *xp;
7016 int idx;
7017{
7018 static int intidx = -1;
7019 char_u *name;
7020
7021 if (idx == 0)
7022 intidx = -1;
7023 if (intidx < 0)
7024 {
7025 name = get_user_func_name(xp, idx);
7026 if (name != NULL)
7027 return name;
7028 }
7029 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7030 {
7031 STRCPY(IObuff, functions[intidx].f_name);
7032 STRCAT(IObuff, "(");
7033 if (functions[intidx].f_max_argc == 0)
7034 STRCAT(IObuff, ")");
7035 return IObuff;
7036 }
7037
7038 return NULL;
7039}
7040
7041/*
7042 * Function given to ExpandGeneric() to obtain the list of internal or
7043 * user defined variable or function names.
7044 */
7045/*ARGSUSED*/
7046 char_u *
7047get_expr_name(xp, idx)
7048 expand_T *xp;
7049 int idx;
7050{
7051 static int intidx = -1;
7052 char_u *name;
7053
7054 if (idx == 0)
7055 intidx = -1;
7056 if (intidx < 0)
7057 {
7058 name = get_function_name(xp, idx);
7059 if (name != NULL)
7060 return name;
7061 }
7062 return get_user_var_name(xp, ++intidx);
7063}
7064
7065#endif /* FEAT_CMDL_COMPL */
7066
7067/*
7068 * Find internal function in table above.
7069 * Return index, or -1 if not found
7070 */
7071 static int
7072find_internal_func(name)
7073 char_u *name; /* name of the function */
7074{
7075 int first = 0;
7076 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7077 int cmp;
7078 int x;
7079
7080 /*
7081 * Find the function name in the table. Binary search.
7082 */
7083 while (first <= last)
7084 {
7085 x = first + ((unsigned)(last - first) >> 1);
7086 cmp = STRCMP(name, functions[x].f_name);
7087 if (cmp < 0)
7088 last = x - 1;
7089 else if (cmp > 0)
7090 first = x + 1;
7091 else
7092 return x;
7093 }
7094 return -1;
7095}
7096
7097/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007098 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7099 * name it contains, otherwise return "name".
7100 */
7101 static char_u *
7102deref_func_name(name, lenp)
7103 char_u *name;
7104 int *lenp;
7105{
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007107 int cc;
7108
7109 cc = name[*lenp];
7110 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007111 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007112 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007113 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007114 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007116 {
7117 *lenp = 0;
7118 return (char_u *)""; /* just in case */
7119 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 *lenp = STRLEN(v->di_tv.vval.v_string);
7121 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007122 }
7123
7124 return name;
7125}
7126
7127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 * Allocate a variable for the result of a function.
7129 * Return OK or FAIL.
7130 */
7131 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007132get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7133 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 char_u *name; /* name of the function */
7135 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 char_u **arg; /* argument, pointing to the '(' */
7138 linenr_T firstline; /* first line of range */
7139 linenr_T lastline; /* last line of range */
7140 int *doesrange; /* return: function handled range */
7141 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007142 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143{
7144 char_u *argp;
7145 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007146 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 int argcount = 0; /* number of arguments found */
7148
7149 /*
7150 * Get the arguments.
7151 */
7152 argp = *arg;
7153 while (argcount < MAX_FUNC_ARGS)
7154 {
7155 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7156 if (*argp == ')' || *argp == ',' || *argp == NUL)
7157 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7159 {
7160 ret = FAIL;
7161 break;
7162 }
7163 ++argcount;
7164 if (*argp != ',')
7165 break;
7166 }
7167 if (*argp == ')')
7168 ++argp;
7169 else
7170 ret = FAIL;
7171
7172 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007173 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007174 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 {
7177 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007178 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007179 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007180 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182
7183 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007184 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
7186 *arg = skipwhite(argp);
7187 return ret;
7188}
7189
7190
7191/*
7192 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007193 * Return OK when the function can't be called, FAIL otherwise.
7194 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 */
7196 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007197call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007198 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 char_u *name; /* name of the function */
7200 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007201 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 linenr_T firstline; /* first line of range */
7205 linenr_T lastline; /* last line of range */
7206 int *doesrange; /* return: function handled range */
7207 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007208 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209{
7210 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211#define ERROR_UNKNOWN 0
7212#define ERROR_TOOMANY 1
7213#define ERROR_TOOFEW 2
7214#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007215#define ERROR_DICT 4
7216#define ERROR_NONE 5
7217#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 int error = ERROR_NONE;
7219 int i;
7220 int llen;
7221 ufunc_T *fp;
7222 int cc;
7223#define FLEN_FIXED 40
7224 char_u fname_buf[FLEN_FIXED + 1];
7225 char_u *fname;
7226
7227 /*
7228 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7229 * Change <SNR>123_name() to K_SNR 123_name().
7230 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7231 */
7232 cc = name[len];
7233 name[len] = NUL;
7234 llen = eval_fname_script(name);
7235 if (llen > 0)
7236 {
7237 fname_buf[0] = K_SPECIAL;
7238 fname_buf[1] = KS_EXTRA;
7239 fname_buf[2] = (int)KE_SNR;
7240 i = 3;
7241 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7242 {
7243 if (current_SID <= 0)
7244 error = ERROR_SCRIPT;
7245 else
7246 {
7247 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7248 i = (int)STRLEN(fname_buf);
7249 }
7250 }
7251 if (i + STRLEN(name + llen) < FLEN_FIXED)
7252 {
7253 STRCPY(fname_buf + i, name + llen);
7254 fname = fname_buf;
7255 }
7256 else
7257 {
7258 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7259 if (fname == NULL)
7260 error = ERROR_OTHER;
7261 else
7262 {
7263 mch_memmove(fname, fname_buf, (size_t)i);
7264 STRCPY(fname + i, name + llen);
7265 }
7266 }
7267 }
7268 else
7269 fname = name;
7270
7271 *doesrange = FALSE;
7272
7273
7274 /* execute the function if no errors detected and executing */
7275 if (evaluate && error == ERROR_NONE)
7276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007277 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 error = ERROR_UNKNOWN;
7279
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007280 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {
7282 /*
7283 * User defined function.
7284 */
7285 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007286
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007288 /* Trigger FuncUndefined event, may load the function. */
7289 if (fp == NULL
7290 && apply_autocmds(EVENT_FUNCUNDEFINED,
7291 fname, fname, TRUE, NULL)
7292 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007294 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 fp = find_func(fname);
7296 }
7297#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007298 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007299 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007300 {
7301 /* loaded a package, search for the function again */
7302 fp = find_func(fname);
7303 }
7304
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 if (fp != NULL)
7306 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007307 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007309 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007311 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007313 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007314 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 else
7316 {
7317 /*
7318 * Call the user function.
7319 * Save and restore search patterns, script variables and
7320 * redo buffer.
7321 */
7322 save_search_patterns();
7323 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007324 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007325 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007327 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7328 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7329 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007330 /* Function was unreferenced while being used, free it
7331 * now. */
7332 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 restoreRedobuff();
7334 restore_search_patterns();
7335 error = ERROR_NONE;
7336 }
7337 }
7338 }
7339 else
7340 {
7341 /*
7342 * Find the function name in the table, call its implementation.
7343 */
7344 i = find_internal_func(fname);
7345 if (i >= 0)
7346 {
7347 if (argcount < functions[i].f_min_argc)
7348 error = ERROR_TOOFEW;
7349 else if (argcount > functions[i].f_max_argc)
7350 error = ERROR_TOOMANY;
7351 else
7352 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007353 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007354 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 error = ERROR_NONE;
7356 }
7357 }
7358 }
7359 /*
7360 * The function call (or "FuncUndefined" autocommand sequence) might
7361 * have been aborted by an error, an interrupt, or an explicitly thrown
7362 * exception that has not been caught so far. This situation can be
7363 * tested for by calling aborting(). For an error in an internal
7364 * function or for the "E132" error in call_user_func(), however, the
7365 * throw point at which the "force_abort" flag (temporarily reset by
7366 * emsg()) is normally updated has not been reached yet. We need to
7367 * update that flag first to make aborting() reliable.
7368 */
7369 update_force_abort();
7370 }
7371 if (error == ERROR_NONE)
7372 ret = OK;
7373
7374 /*
7375 * Report an error unless the argument evaluation or function call has been
7376 * cancelled due to an aborting error, an interrupt, or an exception.
7377 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007378 if (!aborting())
7379 {
7380 switch (error)
7381 {
7382 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007383 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007384 break;
7385 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007386 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387 break;
7388 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007389 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390 name);
7391 break;
7392 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007393 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 name);
7395 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007397 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 name);
7399 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 }
7401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402
7403 name[len] = cc;
7404 if (fname != name && fname != fname_buf)
7405 vim_free(fname);
7406
7407 return ret;
7408}
7409
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007410/*
7411 * Give an error message with a function name. Handle <SNR> things.
7412 */
7413 static void
7414emsg_funcname(msg, name)
7415 char *msg;
7416 char_u *name;
7417{
7418 char_u *p;
7419
7420 if (*name == K_SPECIAL)
7421 p = concat_str((char_u *)"<SNR>", name + 3);
7422 else
7423 p = name;
7424 EMSG2(_(msg), p);
7425 if (p != name)
7426 vim_free(p);
7427}
7428
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429/*********************************************
7430 * Implementation of the built-in functions
7431 */
7432
7433/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007434 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 */
7436 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007437f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007438 typval_T *argvars;
7439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440{
Bram Moolenaar33570922005-01-25 22:26:29 +00007441 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007443 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007444 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007446 if ((l = argvars[0].vval.v_list) != NULL
7447 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7448 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007449 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007450 }
7451 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007452 EMSG(_(e_listreq));
7453}
7454
7455/*
7456 * "append(lnum, string/list)" function
7457 */
7458 static void
7459f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 typval_T *argvars;
7461 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007462{
7463 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007464 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007465 list_T *l = NULL;
7466 listitem_T *li = NULL;
7467 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007468 long added = 0;
7469
Bram Moolenaar0d660222005-01-07 21:51:51 +00007470 lnum = get_tv_lnum(argvars);
7471 if (lnum >= 0
7472 && lnum <= curbuf->b_ml.ml_line_count
7473 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007474 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007475 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007476 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007477 l = argvars[1].vval.v_list;
7478 if (l == NULL)
7479 return;
7480 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007481 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007482 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007483 for (;;)
7484 {
7485 if (l == NULL)
7486 tv = &argvars[1]; /* append a string */
7487 else if (li == NULL)
7488 break; /* end of list */
7489 else
7490 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007491 line = get_tv_string_chk(tv);
7492 if (line == NULL) /* type error */
7493 {
7494 rettv->vval.v_number = 1; /* Failed */
7495 break;
7496 }
7497 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007498 ++added;
7499 if (l == NULL)
7500 break;
7501 li = li->li_next;
7502 }
7503
7504 appended_lines_mark(lnum, added);
7505 if (curwin->w_cursor.lnum > lnum)
7506 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007508 else
7509 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510}
7511
7512/*
7513 * "argc()" function
7514 */
7515/* ARGSUSED */
7516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007517f_argc(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 = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522}
7523
7524/*
7525 * "argidx()" function
7526 */
7527/* ARGSUSED */
7528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007529f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007530 typval_T *argvars;
7531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007533 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534}
7535
7536/*
7537 * "argv(nr)" function
7538 */
7539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007540f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007541 typval_T *argvars;
7542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543{
7544 int idx;
7545
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007546 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007548 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007550 rettv->vval.v_string = NULL;
7551 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552}
7553
7554/*
7555 * "browse(save, title, initdir, default)" function
7556 */
7557/* ARGSUSED */
7558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007559f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 typval_T *argvars;
7561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562{
7563#ifdef FEAT_BROWSE
7564 int save;
7565 char_u *title;
7566 char_u *initdir;
7567 char_u *defname;
7568 char_u buf[NUMBUFLEN];
7569 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007570 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007572 save = get_tv_number_chk(&argvars[0], &error);
7573 title = get_tv_string_chk(&argvars[1]);
7574 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7575 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007577 if (error || title == NULL || initdir == NULL || defname == NULL)
7578 rettv->vval.v_string = NULL;
7579 else
7580 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007581 do_browse(save ? BROWSE_SAVE : 0,
7582 title, defname, NULL, initdir, NULL, curbuf);
7583#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007584 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007585#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007586 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007587}
7588
7589/*
7590 * "browsedir(title, initdir)" function
7591 */
7592/* ARGSUSED */
7593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007594f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007595 typval_T *argvars;
7596 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007597{
7598#ifdef FEAT_BROWSE
7599 char_u *title;
7600 char_u *initdir;
7601 char_u buf[NUMBUFLEN];
7602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007603 title = get_tv_string_chk(&argvars[0]);
7604 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007606 if (title == NULL || initdir == NULL)
7607 rettv->vval.v_string = NULL;
7608 else
7609 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007610 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007612 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007614 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615}
7616
Bram Moolenaar33570922005-01-25 22:26:29 +00007617static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007618
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619/*
7620 * Find a buffer by number or exact name.
7621 */
7622 static buf_T *
7623find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007624 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625{
7626 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007628 if (avar->v_type == VAR_NUMBER)
7629 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007630 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007632 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007633 if (buf == NULL)
7634 {
7635 /* No full path name match, try a match with a URL or a "nofile"
7636 * buffer, these don't use the full path. */
7637 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7638 if (buf->b_fname != NULL
7639 && (path_with_url(buf->b_fname)
7640#ifdef FEAT_QUICKFIX
7641 || bt_nofile(buf)
7642#endif
7643 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007644 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007645 break;
7646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 }
7648 return buf;
7649}
7650
7651/*
7652 * "bufexists(expr)" function
7653 */
7654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007655f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 typval_T *argvars;
7657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007659 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660}
7661
7662/*
7663 * "buflisted(expr)" function
7664 */
7665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007666f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007667 typval_T *argvars;
7668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669{
7670 buf_T *buf;
7671
7672 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674}
7675
7676/*
7677 * "bufloaded(expr)" function
7678 */
7679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007680f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007681 typval_T *argvars;
7682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683{
7684 buf_T *buf;
7685
7686 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688}
7689
Bram Moolenaar33570922005-01-25 22:26:29 +00007690static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007691
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692/*
7693 * Get buffer by number or pattern.
7694 */
7695 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007696get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007699 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 int save_magic;
7701 char_u *save_cpo;
7702 buf_T *buf;
7703
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007704 if (tv->v_type == VAR_NUMBER)
7705 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007706 if (tv->v_type != VAR_STRING)
7707 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 if (name == NULL || *name == NUL)
7709 return curbuf;
7710 if (name[0] == '$' && name[1] == NUL)
7711 return lastbuf;
7712
7713 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7714 save_magic = p_magic;
7715 p_magic = TRUE;
7716 save_cpo = p_cpo;
7717 p_cpo = (char_u *)"";
7718
7719 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7720 TRUE, FALSE));
7721
7722 p_magic = save_magic;
7723 p_cpo = save_cpo;
7724
7725 /* If not found, try expanding the name, like done for bufexists(). */
7726 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007727 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
7729 return buf;
7730}
7731
7732/*
7733 * "bufname(expr)" function
7734 */
7735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007736f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 typval_T *argvars;
7738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
7740 buf_T *buf;
7741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007742 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007744 buf = get_buf_tv(&argvars[0]);
7745 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007747 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007749 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 --emsg_off;
7751}
7752
7753/*
7754 * "bufnr(expr)" function
7755 */
7756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007757f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007758 typval_T *argvars;
7759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760{
7761 buf_T *buf;
7762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007763 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007767 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007769 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 --emsg_off;
7771}
7772
7773/*
7774 * "bufwinnr(nr)" function
7775 */
7776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007777f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007778 typval_T *argvars;
7779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780{
7781#ifdef FEAT_WINDOWS
7782 win_T *wp;
7783 int winnr = 0;
7784#endif
7785 buf_T *buf;
7786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007787 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007789 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790#ifdef FEAT_WINDOWS
7791 for (wp = firstwin; wp; wp = wp->w_next)
7792 {
7793 ++winnr;
7794 if (wp->w_buffer == buf)
7795 break;
7796 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007797 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007799 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800#endif
7801 --emsg_off;
7802}
7803
7804/*
7805 * "byte2line(byte)" function
7806 */
7807/*ARGSUSED*/
7808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007809f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007810 typval_T *argvars;
7811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007814 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815#else
7816 long boff = 0;
7817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007818 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007820 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007822 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 (linenr_T)0, &boff);
7824#endif
7825}
7826
7827/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007828 * "byteidx()" function
7829 */
7830/*ARGSUSED*/
7831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007832f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007833 typval_T *argvars;
7834 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007835{
7836#ifdef FEAT_MBYTE
7837 char_u *t;
7838#endif
7839 char_u *str;
7840 long idx;
7841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007842 str = get_tv_string_chk(&argvars[0]);
7843 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007845 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007846 return;
7847
7848#ifdef FEAT_MBYTE
7849 t = str;
7850 for ( ; idx > 0; idx--)
7851 {
7852 if (*t == NUL) /* EOL reached */
7853 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007854 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007855 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007856 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007857#else
7858 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007859 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007860#endif
7861}
7862
7863/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007864 * "call(func, arglist)" function
7865 */
7866 static void
7867f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007868 typval_T *argvars;
7869 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007870{
7871 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007872 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007873 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007874 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007875 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007876 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007877
7878 rettv->vval.v_number = 0;
7879 if (argvars[1].v_type != VAR_LIST)
7880 {
7881 EMSG(_(e_listreq));
7882 return;
7883 }
7884 if (argvars[1].vval.v_list == NULL)
7885 return;
7886
7887 if (argvars[0].v_type == VAR_FUNC)
7888 func = argvars[0].vval.v_string;
7889 else
7890 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007891 if (*func == NUL)
7892 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007893
Bram Moolenaare9a41262005-01-15 22:18:47 +00007894 if (argvars[2].v_type != VAR_UNKNOWN)
7895 {
7896 if (argvars[2].v_type != VAR_DICT)
7897 {
7898 EMSG(_(e_dictreq));
7899 return;
7900 }
7901 selfdict = argvars[2].vval.v_dict;
7902 }
7903
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007904 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7905 item = item->li_next)
7906 {
7907 if (argc == MAX_FUNC_ARGS)
7908 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007909 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007910 break;
7911 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007912 /* Make a copy of each argument. This is needed to be able to set
7913 * v_lock to VAR_FIXED in the copy without changing the original list.
7914 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007915 copy_tv(&item->li_tv, &argv[argc++]);
7916 }
7917
7918 if (item == NULL)
7919 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007920 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7921 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007922
7923 /* Free the arguments. */
7924 while (argc > 0)
7925 clear_tv(&argv[--argc]);
7926}
7927
7928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 * "char2nr(string)" function
7930 */
7931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007932f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007933 typval_T *argvars;
7934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
7936#ifdef FEAT_MBYTE
7937 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007938 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 else
7940#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942}
7943
7944/*
7945 * "cindent(lnum)" function
7946 */
7947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007948f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 typval_T *argvars;
7950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952#ifdef FEAT_CINDENT
7953 pos_T pos;
7954 linenr_T lnum;
7955
7956 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007957 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7959 {
7960 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007961 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 curwin->w_cursor = pos;
7963 }
7964 else
7965#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007966 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967}
7968
7969/*
7970 * "col(string)" function
7971 */
7972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007973f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007974 typval_T *argvars;
7975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 colnr_T col = 0;
7978 pos_T *fp;
7979
7980 fp = var2fpos(&argvars[0], FALSE);
7981 if (fp != NULL)
7982 {
7983 if (fp->col == MAXCOL)
7984 {
7985 /* '> can be MAXCOL, get the length of the line then */
7986 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7987 col = STRLEN(ml_get(fp->lnum)) + 1;
7988 else
7989 col = MAXCOL;
7990 }
7991 else
7992 {
7993 col = fp->col + 1;
7994#ifdef FEAT_VIRTUALEDIT
7995 /* col(".") when the cursor is on the NUL at the end of the line
7996 * because of "coladd" can be seen as an extra column. */
7997 if (virtual_active() && fp == &curwin->w_cursor)
7998 {
7999 char_u *p = ml_get_cursor();
8000
8001 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8002 curwin->w_virtcol - curwin->w_cursor.coladd))
8003 {
8004# ifdef FEAT_MBYTE
8005 int l;
8006
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008007 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 col += l;
8009# else
8010 if (*p != NUL && p[1] == NUL)
8011 ++col;
8012# endif
8013 }
8014 }
8015#endif
8016 }
8017 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019}
8020
Bram Moolenaar572cb562005-08-05 21:35:02 +00008021#if defined(FEAT_INS_EXPAND)
8022/*
8023 * "complete_add()" function
8024 */
8025/*ARGSUSED*/
8026 static void
8027f_complete_add(argvars, rettv)
8028 typval_T *argvars;
8029 typval_T *rettv;
8030{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008031 char_u *word;
8032 char_u *extra = NULL;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008033
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008034 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8035 {
8036 word = get_dict_string(argvars[0].vval.v_dict,
8037 (char_u *)"word", FALSE);
8038 extra = get_dict_string(argvars[0].vval.v_dict,
8039 (char_u *)"menu", FALSE);
8040 }
8041 else
8042 word = get_tv_string_chk(&argvars[0]);
8043 if (word != NULL)
8044 rettv->vval.v_number = ins_compl_add(word, -1, NULL, extra, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008045}
8046
8047/*
8048 * "complete_check()" function
8049 */
8050/*ARGSUSED*/
8051 static void
8052f_complete_check(argvars, rettv)
8053 typval_T *argvars;
8054 typval_T *rettv;
8055{
8056 int saved = RedrawingDisabled;
8057
8058 RedrawingDisabled = 0;
8059 ins_compl_check_keys(0);
8060 rettv->vval.v_number = compl_interrupted;
8061 RedrawingDisabled = saved;
8062}
8063#endif
8064
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065/*
8066 * "confirm(message, buttons[, default [, type]])" function
8067 */
8068/*ARGSUSED*/
8069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008070f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008071 typval_T *argvars;
8072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073{
8074#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8075 char_u *message;
8076 char_u *buttons = NULL;
8077 char_u buf[NUMBUFLEN];
8078 char_u buf2[NUMBUFLEN];
8079 int def = 1;
8080 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008081 char_u *typestr;
8082 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008084 message = get_tv_string_chk(&argvars[0]);
8085 if (message == NULL)
8086 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008087 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008089 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8090 if (buttons == NULL)
8091 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008092 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008094 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008095 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008097 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8098 if (typestr == NULL)
8099 error = TRUE;
8100 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008102 switch (TOUPPER_ASC(*typestr))
8103 {
8104 case 'E': type = VIM_ERROR; break;
8105 case 'Q': type = VIM_QUESTION; break;
8106 case 'I': type = VIM_INFO; break;
8107 case 'W': type = VIM_WARNING; break;
8108 case 'G': type = VIM_GENERIC; break;
8109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 }
8111 }
8112 }
8113 }
8114
8115 if (buttons == NULL || *buttons == NUL)
8116 buttons = (char_u *)_("&Ok");
8117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008118 if (error)
8119 rettv->vval.v_number = 0;
8120 else
8121 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 def, NULL);
8123#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008124 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125#endif
8126}
8127
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008128/*
8129 * "copy()" function
8130 */
8131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008132f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008133 typval_T *argvars;
8134 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008135{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008136 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138
8139/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008140 * "count()" function
8141 */
8142 static void
8143f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008144 typval_T *argvars;
8145 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008146{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008147 long n = 0;
8148 int ic = FALSE;
8149
Bram Moolenaare9a41262005-01-15 22:18:47 +00008150 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008151 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008152 listitem_T *li;
8153 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008154 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008155
Bram Moolenaare9a41262005-01-15 22:18:47 +00008156 if ((l = argvars[0].vval.v_list) != NULL)
8157 {
8158 li = l->lv_first;
8159 if (argvars[2].v_type != VAR_UNKNOWN)
8160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008161 int error = FALSE;
8162
8163 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008164 if (argvars[3].v_type != VAR_UNKNOWN)
8165 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008166 idx = get_tv_number_chk(&argvars[3], &error);
8167 if (!error)
8168 {
8169 li = list_find(l, idx);
8170 if (li == NULL)
8171 EMSGN(_(e_listidx), idx);
8172 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008173 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008174 if (error)
8175 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008176 }
8177
8178 for ( ; li != NULL; li = li->li_next)
8179 if (tv_equal(&li->li_tv, &argvars[1], ic))
8180 ++n;
8181 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008182 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008183 else if (argvars[0].v_type == VAR_DICT)
8184 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008185 int todo;
8186 dict_T *d;
8187 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008188
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008189 if ((d = argvars[0].vval.v_dict) != NULL)
8190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008191 int error = FALSE;
8192
Bram Moolenaare9a41262005-01-15 22:18:47 +00008193 if (argvars[2].v_type != VAR_UNKNOWN)
8194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008195 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008196 if (argvars[3].v_type != VAR_UNKNOWN)
8197 EMSG(_(e_invarg));
8198 }
8199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008200 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008201 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008202 {
8203 if (!HASHITEM_EMPTY(hi))
8204 {
8205 --todo;
8206 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8207 ++n;
8208 }
8209 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008210 }
8211 }
8212 else
8213 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008214 rettv->vval.v_number = n;
8215}
8216
8217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8219 *
8220 * Checks the existence of a cscope connection.
8221 */
8222/*ARGSUSED*/
8223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008224f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008225 typval_T *argvars;
8226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227{
8228#ifdef FEAT_CSCOPE
8229 int num = 0;
8230 char_u *dbpath = NULL;
8231 char_u *prepend = NULL;
8232 char_u buf[NUMBUFLEN];
8233
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008234 if (argvars[0].v_type != VAR_UNKNOWN
8235 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008237 num = (int)get_tv_number(&argvars[0]);
8238 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008239 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008240 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 }
8242
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008243 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008245 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246#endif
8247}
8248
8249/*
8250 * "cursor(lnum, col)" function
8251 *
8252 * Moves the cursor to the specified line and column
8253 */
8254/*ARGSUSED*/
8255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008256f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008257 typval_T *argvars;
8258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259{
8260 long line, col;
8261
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008262 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008263 col = get_tv_number_chk(&argvars[1], NULL);
8264 if (line < 0 || col < 0)
8265 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 if (line > 0)
8267 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 if (col > 0)
8269 curwin->w_cursor.col = col - 1;
8270#ifdef FEAT_VIRTUALEDIT
8271 curwin->w_cursor.coladd = 0;
8272#endif
8273
8274 /* Make sure the cursor is in a valid position. */
8275 check_cursor();
8276#ifdef FEAT_MBYTE
8277 /* Correct cursor for multi-byte character. */
8278 if (has_mbyte)
8279 mb_adjust_cursor();
8280#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008281
8282 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283}
8284
8285/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008286 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 */
8288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008289f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008290 typval_T *argvars;
8291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008293 int noref = 0;
8294
8295 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008296 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008297 if (noref < 0 || noref > 1)
8298 EMSG(_(e_invarg));
8299 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008300 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301}
8302
8303/*
8304 * "delete()" function
8305 */
8306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008307f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008308 typval_T *argvars;
8309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310{
8311 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008312 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008314 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315}
8316
8317/*
8318 * "did_filetype()" function
8319 */
8320/*ARGSUSED*/
8321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008322f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008323 typval_T *argvars;
8324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325{
8326#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008327 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008329 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330#endif
8331}
8332
8333/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008334 * "diff_filler()" function
8335 */
8336/*ARGSUSED*/
8337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008338f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008339 typval_T *argvars;
8340 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008341{
8342#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008344#endif
8345}
8346
8347/*
8348 * "diff_hlID()" function
8349 */
8350/*ARGSUSED*/
8351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008352f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008353 typval_T *argvars;
8354 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008355{
8356#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008357 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008358 static linenr_T prev_lnum = 0;
8359 static int changedtick = 0;
8360 static int fnum = 0;
8361 static int change_start = 0;
8362 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008363 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008364 int filler_lines;
8365 int col;
8366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008367 if (lnum < 0) /* ignore type error in {lnum} arg */
8368 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008369 if (lnum != prev_lnum
8370 || changedtick != curbuf->b_changedtick
8371 || fnum != curbuf->b_fnum)
8372 {
8373 /* New line, buffer, change: need to get the values. */
8374 filler_lines = diff_check(curwin, lnum);
8375 if (filler_lines < 0)
8376 {
8377 if (filler_lines == -1)
8378 {
8379 change_start = MAXCOL;
8380 change_end = -1;
8381 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8382 hlID = HLF_ADD; /* added line */
8383 else
8384 hlID = HLF_CHD; /* changed line */
8385 }
8386 else
8387 hlID = HLF_ADD; /* added line */
8388 }
8389 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008390 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008391 prev_lnum = lnum;
8392 changedtick = curbuf->b_changedtick;
8393 fnum = curbuf->b_fnum;
8394 }
8395
8396 if (hlID == HLF_CHD || hlID == HLF_TXD)
8397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008398 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008399 if (col >= change_start && col <= change_end)
8400 hlID = HLF_TXD; /* changed text */
8401 else
8402 hlID = HLF_CHD; /* changed line */
8403 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008404 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008405#endif
8406}
8407
8408/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008409 * "empty({expr})" function
8410 */
8411 static void
8412f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008413 typval_T *argvars;
8414 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008415{
8416 int n;
8417
8418 switch (argvars[0].v_type)
8419 {
8420 case VAR_STRING:
8421 case VAR_FUNC:
8422 n = argvars[0].vval.v_string == NULL
8423 || *argvars[0].vval.v_string == NUL;
8424 break;
8425 case VAR_NUMBER:
8426 n = argvars[0].vval.v_number == 0;
8427 break;
8428 case VAR_LIST:
8429 n = argvars[0].vval.v_list == NULL
8430 || argvars[0].vval.v_list->lv_first == NULL;
8431 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008432 case VAR_DICT:
8433 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008434 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008435 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008436 default:
8437 EMSG2(_(e_intern2), "f_empty()");
8438 n = 0;
8439 }
8440
8441 rettv->vval.v_number = n;
8442}
8443
8444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 * "escape({string}, {chars})" function
8446 */
8447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008448f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008449 typval_T *argvars;
8450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451{
8452 char_u buf[NUMBUFLEN];
8453
Bram Moolenaar758711c2005-02-02 23:11:38 +00008454 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8455 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008456 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457}
8458
8459/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008460 * "eval()" function
8461 */
8462/*ARGSUSED*/
8463 static void
8464f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008465 typval_T *argvars;
8466 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008467{
8468 char_u *s;
8469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008470 s = get_tv_string_chk(&argvars[0]);
8471 if (s != NULL)
8472 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008474 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8475 {
8476 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008477 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008478 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008479 else if (*s != NUL)
8480 EMSG(_(e_trailing));
8481}
8482
8483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 * "eventhandler()" function
8485 */
8486/*ARGSUSED*/
8487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008488f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008489 typval_T *argvars;
8490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008492 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493}
8494
8495/*
8496 * "executable()" function
8497 */
8498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008499f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008500 typval_T *argvars;
8501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008503 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504}
8505
8506/*
8507 * "exists()" function
8508 */
8509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008510f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008511 typval_T *argvars;
8512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513{
8514 char_u *p;
8515 char_u *name;
8516 int n = FALSE;
8517 int len = 0;
8518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008519 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 if (*p == '$') /* environment variable */
8521 {
8522 /* first try "normal" environment variables (fast) */
8523 if (mch_getenv(p + 1) != NULL)
8524 n = TRUE;
8525 else
8526 {
8527 /* try expanding things like $VIM and ${HOME} */
8528 p = expand_env_save(p);
8529 if (p != NULL && *p != '$')
8530 n = TRUE;
8531 vim_free(p);
8532 }
8533 }
8534 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008535 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 else if (*p == '*') /* internal or user defined function */
8537 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008538 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 }
8540 else if (*p == ':')
8541 {
8542 n = cmd_exists(p + 1);
8543 }
8544 else if (*p == '#')
8545 {
8546#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008547 if (p[1] == '#')
8548 n = autocmd_supported(p + 2);
8549 else
8550 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551#endif
8552 }
8553 else /* internal variable */
8554 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008555 char_u *tofree;
8556 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008558 /* get_name_len() takes care of expanding curly braces */
8559 name = p;
8560 len = get_name_len(&p, &tofree, TRUE, FALSE);
8561 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008563 if (tofree != NULL)
8564 name = tofree;
8565 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8566 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008568 /* handle d.key, l[idx], f(expr) */
8569 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8570 if (n)
8571 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 }
8573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008575 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 }
8577
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008578 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579}
8580
8581/*
8582 * "expand()" function
8583 */
8584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008585f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008586 typval_T *argvars;
8587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588{
8589 char_u *s;
8590 int len;
8591 char_u *errormsg;
8592 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8593 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008594 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008596 rettv->v_type = VAR_STRING;
8597 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 if (*s == '%' || *s == '#' || *s == '<')
8599 {
8600 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008601 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 --emsg_off;
8603 }
8604 else
8605 {
8606 /* When the optional second argument is non-zero, don't remove matches
8607 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008608 if (argvars[1].v_type != VAR_UNKNOWN
8609 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008611 if (!error)
8612 {
8613 ExpandInit(&xpc);
8614 xpc.xp_context = EXPAND_FILES;
8615 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8616 ExpandCleanup(&xpc);
8617 }
8618 else
8619 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 }
8621}
8622
8623/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008624 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008625 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008626 */
8627 static void
8628f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008629 typval_T *argvars;
8630 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008631{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008632 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008634 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008635 list_T *l1, *l2;
8636 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008637 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008638 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008639
Bram Moolenaare9a41262005-01-15 22:18:47 +00008640 l1 = argvars[0].vval.v_list;
8641 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008642 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8643 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008644 {
8645 if (argvars[2].v_type != VAR_UNKNOWN)
8646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008647 before = get_tv_number_chk(&argvars[2], &error);
8648 if (error)
8649 return; /* type error; errmsg already given */
8650
Bram Moolenaar758711c2005-02-02 23:11:38 +00008651 if (before == l1->lv_len)
8652 item = NULL;
8653 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008654 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008655 item = list_find(l1, before);
8656 if (item == NULL)
8657 {
8658 EMSGN(_(e_listidx), before);
8659 return;
8660 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008661 }
8662 }
8663 else
8664 item = NULL;
8665 list_extend(l1, l2, item);
8666
Bram Moolenaare9a41262005-01-15 22:18:47 +00008667 copy_tv(&argvars[0], rettv);
8668 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008669 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008670 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8671 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008672 dict_T *d1, *d2;
8673 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008674 char_u *action;
8675 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008676 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008677 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008678
8679 d1 = argvars[0].vval.v_dict;
8680 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008681 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8682 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008683 {
8684 /* Check the third argument. */
8685 if (argvars[2].v_type != VAR_UNKNOWN)
8686 {
8687 static char *(av[]) = {"keep", "force", "error"};
8688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008689 action = get_tv_string_chk(&argvars[2]);
8690 if (action == NULL)
8691 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008692 for (i = 0; i < 3; ++i)
8693 if (STRCMP(action, av[i]) == 0)
8694 break;
8695 if (i == 3)
8696 {
8697 EMSGN(_(e_invarg2), action);
8698 return;
8699 }
8700 }
8701 else
8702 action = (char_u *)"force";
8703
8704 /* Go over all entries in the second dict and add them to the
8705 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008706 todo = d2->dv_hashtab.ht_used;
8707 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008708 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008709 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008710 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008711 --todo;
8712 di1 = dict_find(d1, hi2->hi_key, -1);
8713 if (di1 == NULL)
8714 {
8715 di1 = dictitem_copy(HI2DI(hi2));
8716 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8717 dictitem_free(di1);
8718 }
8719 else if (*action == 'e')
8720 {
8721 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8722 break;
8723 }
8724 else if (*action == 'f')
8725 {
8726 clear_tv(&di1->di_tv);
8727 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8728 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008729 }
8730 }
8731
Bram Moolenaare9a41262005-01-15 22:18:47 +00008732 copy_tv(&argvars[0], rettv);
8733 }
8734 }
8735 else
8736 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008737}
8738
8739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 * "filereadable()" function
8741 */
8742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008743f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008744 typval_T *argvars;
8745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746{
8747 FILE *fd;
8748 char_u *p;
8749 int n;
8750
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8753 {
8754 n = TRUE;
8755 fclose(fd);
8756 }
8757 else
8758 n = FALSE;
8759
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761}
8762
8763/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008764 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 * rights to write into.
8766 */
8767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008768f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008769 typval_T *argvars;
8770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008772 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773}
8774
Bram Moolenaar33570922005-01-25 22:26:29 +00008775static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008776
8777 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008778findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008779 typval_T *argvars;
8780 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008781 int dir;
8782{
8783#ifdef FEAT_SEARCHPATH
8784 char_u *fname;
8785 char_u *fresult = NULL;
8786 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8787 char_u *p;
8788 char_u pathbuf[NUMBUFLEN];
8789 int count = 1;
8790 int first = TRUE;
8791
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008792 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008793
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008794 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008795 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008796 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8797 if (p == NULL)
8798 count = -1; /* error */
8799 else
8800 {
8801 if (*p != NUL)
8802 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008804 if (argvars[2].v_type != VAR_UNKNOWN)
8805 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8806 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008807 }
8808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008809 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008811 do
8812 {
8813 vim_free(fresult);
8814 fresult = find_file_in_path_option(first ? fname : NULL,
8815 first ? (int)STRLEN(fname) : 0,
8816 0, first, path, dir, NULL);
8817 first = FALSE;
8818 } while (--count > 0 && fresult != NULL);
8819 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008820
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008822#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008824#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008826}
8827
Bram Moolenaar33570922005-01-25 22:26:29 +00008828static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8829static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008830
8831/*
8832 * Implementation of map() and filter().
8833 */
8834 static void
8835filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008836 typval_T *argvars;
8837 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008838 int map;
8839{
8840 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008841 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008842 listitem_T *li, *nli;
8843 list_T *l = NULL;
8844 dictitem_T *di;
8845 hashtab_T *ht;
8846 hashitem_T *hi;
8847 dict_T *d = NULL;
8848 typval_T save_val;
8849 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008850 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008851 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008852 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar280f1262006-01-30 00:14:18 +00008853 int save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008854
8855 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008856 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008857 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008858 if ((l = argvars[0].vval.v_list) == NULL
8859 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008860 return;
8861 }
8862 else if (argvars[0].v_type == VAR_DICT)
8863 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008864 if ((d = argvars[0].vval.v_dict) == NULL
8865 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008866 return;
8867 }
8868 else
8869 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008870 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008871 return;
8872 }
8873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008874 expr = get_tv_string_buf_chk(&argvars[1], buf);
8875 /* On type errors, the preceding call has already displayed an error
8876 * message. Avoid a misleading error message for an empty string that
8877 * was not passed as argument. */
8878 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008879 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008880 prepare_vimvar(VV_VAL, &save_val);
8881 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008882
Bram Moolenaar280f1262006-01-30 00:14:18 +00008883 /* We reset "called_emsg" to be able to detect whether an error
8884 * occurred during evaluation of the expression. "did_emsg" can't be
8885 * used, because it is reset when calling a function. */
8886 save_called_emsg = called_emsg;
8887 called_emsg = FALSE;
8888
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008889 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008891 prepare_vimvar(VV_KEY, &save_key);
8892 vimvars[VV_KEY].vv_type = VAR_STRING;
8893
8894 ht = &d->dv_hashtab;
8895 hash_lock(ht);
8896 todo = ht->ht_used;
8897 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008898 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008899 if (!HASHITEM_EMPTY(hi))
8900 {
8901 --todo;
8902 di = HI2DI(hi);
8903 if (tv_check_lock(di->di_tv.v_lock, msg))
8904 break;
8905 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008906 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
8907 || called_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008908 break;
8909 if (!map && rem)
8910 dictitem_remove(d, di);
8911 clear_tv(&vimvars[VV_KEY].vv_tv);
8912 }
8913 }
8914 hash_unlock(ht);
8915
8916 restore_vimvar(VV_KEY, &save_key);
8917 }
8918 else
8919 {
8920 for (li = l->lv_first; li != NULL; li = nli)
8921 {
8922 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008923 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008924 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008925 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
8926 || called_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008927 break;
8928 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008929 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008930 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008931 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008933 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008934
8935 called_emsg |= save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008937
8938 copy_tv(&argvars[0], rettv);
8939}
8940
8941 static int
8942filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008943 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008944 char_u *expr;
8945 int map;
8946 int *remp;
8947{
Bram Moolenaar33570922005-01-25 22:26:29 +00008948 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008949 char_u *s;
8950
Bram Moolenaar33570922005-01-25 22:26:29 +00008951 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008952 s = expr;
8953 if (eval1(&s, &rettv, TRUE) == FAIL)
8954 return FAIL;
8955 if (*s != NUL) /* check for trailing chars after expr */
8956 {
8957 EMSG2(_(e_invexpr2), s);
8958 return FAIL;
8959 }
8960 if (map)
8961 {
8962 /* map(): replace the list item value */
8963 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008964 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008965 *tv = rettv;
8966 }
8967 else
8968 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008969 int error = FALSE;
8970
Bram Moolenaare9a41262005-01-15 22:18:47 +00008971 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008972 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008973 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008974 /* On type error, nothing has been removed; return FAIL to stop the
8975 * loop. The error message was given by get_tv_number_chk(). */
8976 if (error)
8977 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008978 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008979 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008980 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008981}
8982
8983/*
8984 * "filter()" function
8985 */
8986 static void
8987f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008988 typval_T *argvars;
8989 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008990{
8991 filter_map(argvars, rettv, FALSE);
8992}
8993
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008994/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008995 * "finddir({fname}[, {path}[, {count}]])" function
8996 */
8997 static void
8998f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008999 typval_T *argvars;
9000 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009001{
9002 findfilendir(argvars, rettv, TRUE);
9003}
9004
9005/*
9006 * "findfile({fname}[, {path}[, {count}]])" function
9007 */
9008 static void
9009f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009010 typval_T *argvars;
9011 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009012{
9013 findfilendir(argvars, rettv, FALSE);
9014}
9015
9016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 * "fnamemodify({fname}, {mods})" function
9018 */
9019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009020f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009021 typval_T *argvars;
9022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023{
9024 char_u *fname;
9025 char_u *mods;
9026 int usedlen = 0;
9027 int len;
9028 char_u *fbuf = NULL;
9029 char_u buf[NUMBUFLEN];
9030
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009031 fname = get_tv_string_chk(&argvars[0]);
9032 mods = get_tv_string_buf_chk(&argvars[1], buf);
9033 if (fname == NULL || mods == NULL)
9034 fname = NULL;
9035 else
9036 {
9037 len = (int)STRLEN(fname);
9038 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009043 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 vim_free(fbuf);
9047}
9048
Bram Moolenaar33570922005-01-25 22:26:29 +00009049static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050
9051/*
9052 * "foldclosed()" function
9053 */
9054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009055foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009056 typval_T *argvars;
9057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 int end;
9059{
9060#ifdef FEAT_FOLDING
9061 linenr_T lnum;
9062 linenr_T first, last;
9063
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009064 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9066 {
9067 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9068 {
9069 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 return;
9074 }
9075 }
9076#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078}
9079
9080/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009081 * "foldclosed()" function
9082 */
9083 static void
9084f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009085 typval_T *argvars;
9086 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009087{
9088 foldclosed_both(argvars, rettv, FALSE);
9089}
9090
9091/*
9092 * "foldclosedend()" function
9093 */
9094 static void
9095f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009096 typval_T *argvars;
9097 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009098{
9099 foldclosed_both(argvars, rettv, TRUE);
9100}
9101
9102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 * "foldlevel()" function
9104 */
9105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 typval_T *argvars;
9108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109{
9110#ifdef FEAT_FOLDING
9111 linenr_T lnum;
9112
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 else
9117#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119}
9120
9121/*
9122 * "foldtext()" function
9123 */
9124/*ARGSUSED*/
9125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009126f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009127 typval_T *argvars;
9128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129{
9130#ifdef FEAT_FOLDING
9131 linenr_T lnum;
9132 char_u *s;
9133 char_u *r;
9134 int len;
9135 char *txt;
9136#endif
9137
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009138 rettv->v_type = VAR_STRING;
9139 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009141 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9142 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9143 <= curbuf->b_ml.ml_line_count
9144 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 {
9146 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009147 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9148 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 {
9150 if (!linewhite(lnum))
9151 break;
9152 ++lnum;
9153 }
9154
9155 /* Find interesting text in this line. */
9156 s = skipwhite(ml_get(lnum));
9157 /* skip C comment-start */
9158 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009161 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009162 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009163 {
9164 s = skipwhite(ml_get(lnum + 1));
9165 if (*s == '*')
9166 s = skipwhite(s + 1);
9167 }
9168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 txt = _("+-%s%3ld lines: ");
9170 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009171 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 + 20 /* for %3ld */
9173 + STRLEN(s))); /* concatenated */
9174 if (r != NULL)
9175 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009176 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9177 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9178 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 len = (int)STRLEN(r);
9180 STRCAT(r, s);
9181 /* remove 'foldmarker' and 'commentstring' */
9182 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 }
9185 }
9186#endif
9187}
9188
9189/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009190 * "foldtextresult(lnum)" function
9191 */
9192/*ARGSUSED*/
9193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009194f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009195 typval_T *argvars;
9196 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009197{
9198#ifdef FEAT_FOLDING
9199 linenr_T lnum;
9200 char_u *text;
9201 char_u buf[51];
9202 foldinfo_T foldinfo;
9203 int fold_count;
9204#endif
9205
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206 rettv->v_type = VAR_STRING;
9207 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009208#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009209 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009210 /* treat illegal types and illegal string values for {lnum} the same */
9211 if (lnum < 0)
9212 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009213 fold_count = foldedCount(curwin, lnum, &foldinfo);
9214 if (fold_count > 0)
9215 {
9216 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9217 &foldinfo, buf);
9218 if (text == buf)
9219 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009220 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009221 }
9222#endif
9223}
9224
9225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 * "foreground()" function
9227 */
9228/*ARGSUSED*/
9229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009230f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009231 typval_T *argvars;
9232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009234 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235#ifdef FEAT_GUI
9236 if (gui.in_use)
9237 gui_mch_set_foreground();
9238#else
9239# ifdef WIN32
9240 win32_set_foreground();
9241# endif
9242#endif
9243}
9244
9245/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009246 * "function()" function
9247 */
9248/*ARGSUSED*/
9249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009250f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009251 typval_T *argvars;
9252 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009253{
9254 char_u *s;
9255
Bram Moolenaara7043832005-01-21 11:56:39 +00009256 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009257 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009258 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009259 EMSG2(_(e_invarg2), s);
9260 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009261 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009262 else
9263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009264 rettv->vval.v_string = vim_strsave(s);
9265 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009266 }
9267}
9268
9269/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009270 * "garbagecollect()" function
9271 */
9272/*ARGSUSED*/
9273 static void
9274f_garbagecollect(argvars, rettv)
9275 typval_T *argvars;
9276 typval_T *rettv;
9277{
9278 garbage_collect();
9279}
9280
9281/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009282 * "get()" function
9283 */
9284 static void
9285f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009286 typval_T *argvars;
9287 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009288{
Bram Moolenaar33570922005-01-25 22:26:29 +00009289 listitem_T *li;
9290 list_T *l;
9291 dictitem_T *di;
9292 dict_T *d;
9293 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009294
Bram Moolenaare9a41262005-01-15 22:18:47 +00009295 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009296 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009297 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009299 int error = FALSE;
9300
9301 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9302 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009303 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009304 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009305 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009306 else if (argvars[0].v_type == VAR_DICT)
9307 {
9308 if ((d = argvars[0].vval.v_dict) != NULL)
9309 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009310 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009311 if (di != NULL)
9312 tv = &di->di_tv;
9313 }
9314 }
9315 else
9316 EMSG2(_(e_listdictarg), "get()");
9317
9318 if (tv == NULL)
9319 {
9320 if (argvars[2].v_type == VAR_UNKNOWN)
9321 rettv->vval.v_number = 0;
9322 else
9323 copy_tv(&argvars[2], rettv);
9324 }
9325 else
9326 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009327}
9328
Bram Moolenaar342337a2005-07-21 21:11:17 +00009329static 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 +00009330
9331/*
9332 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009333 * Return a range (from start to end) of lines in rettv from the specified
9334 * buffer.
9335 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009336 */
9337 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009338get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009339 buf_T *buf;
9340 linenr_T start;
9341 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009342 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009343 typval_T *rettv;
9344{
9345 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009346 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009347
Bram Moolenaar342337a2005-07-21 21:11:17 +00009348 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009349 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009350 l = list_alloc();
9351 if (l == NULL)
9352 return;
9353
9354 rettv->vval.v_list = l;
9355 rettv->v_type = VAR_LIST;
9356 ++l->lv_refcount;
9357 }
9358 else
9359 rettv->vval.v_number = 0;
9360
9361 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9362 return;
9363
9364 if (!retlist)
9365 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009366 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9367 p = ml_get_buf(buf, start, FALSE);
9368 else
9369 p = (char_u *)"";
9370
9371 rettv->v_type = VAR_STRING;
9372 rettv->vval.v_string = vim_strsave(p);
9373 }
9374 else
9375 {
9376 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009377 return;
9378
9379 if (start < 1)
9380 start = 1;
9381 if (end > buf->b_ml.ml_line_count)
9382 end = buf->b_ml.ml_line_count;
9383 while (start <= end)
Bram Moolenaar4463f292005-09-25 22:20:24 +00009384 if (list_append_string(l, ml_get_buf(buf, start++, FALSE), -1)
9385 == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009386 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009387 }
9388}
9389
9390/*
9391 * "getbufline()" function
9392 */
9393 static void
9394f_getbufline(argvars, rettv)
9395 typval_T *argvars;
9396 typval_T *rettv;
9397{
9398 linenr_T lnum;
9399 linenr_T end;
9400 buf_T *buf;
9401
9402 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9403 ++emsg_off;
9404 buf = get_buf_tv(&argvars[0]);
9405 --emsg_off;
9406
Bram Moolenaar661b1822005-07-28 22:36:45 +00009407 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009408 if (argvars[2].v_type == VAR_UNKNOWN)
9409 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009410 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009411 end = get_tv_lnum_buf(&argvars[2], buf);
9412
Bram Moolenaar342337a2005-07-21 21:11:17 +00009413 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009414}
9415
Bram Moolenaar0d660222005-01-07 21:51:51 +00009416/*
9417 * "getbufvar()" function
9418 */
9419 static void
9420f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009421 typval_T *argvars;
9422 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009423{
9424 buf_T *buf;
9425 buf_T *save_curbuf;
9426 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009427 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009428
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009429 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9430 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009431 ++emsg_off;
9432 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009433
9434 rettv->v_type = VAR_STRING;
9435 rettv->vval.v_string = NULL;
9436
9437 if (buf != NULL && varname != NULL)
9438 {
9439 if (*varname == '&') /* buffer-local-option */
9440 {
9441 /* set curbuf to be our buf, temporarily */
9442 save_curbuf = curbuf;
9443 curbuf = buf;
9444
9445 get_option_tv(&varname, rettv, TRUE);
9446
9447 /* restore previous notion of curbuf */
9448 curbuf = save_curbuf;
9449 }
9450 else
9451 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009452 if (*varname == NUL)
9453 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9454 * scope prefix before the NUL byte is required by
9455 * find_var_in_ht(). */
9456 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009457 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009458 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009459 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009460 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009461 }
9462 }
9463
9464 --emsg_off;
9465}
9466
9467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 * "getchar()" function
9469 */
9470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009472 typval_T *argvars;
9473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474{
9475 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009476 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477
9478 ++no_mapping;
9479 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009480 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 /* getchar(): blocking wait. */
9482 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009483 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 /* getchar(1): only check if char avail */
9485 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009486 else if (error || vpeekc() == NUL)
9487 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 n = 0;
9489 else
9490 /* getchar(0) and char avail: return char */
9491 n = safe_vgetc();
9492 --no_mapping;
9493 --allow_keys;
9494
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 if (IS_SPECIAL(n) || mod_mask != 0)
9497 {
9498 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9499 int i = 0;
9500
9501 /* Turn a special key into three bytes, plus modifier. */
9502 if (mod_mask != 0)
9503 {
9504 temp[i++] = K_SPECIAL;
9505 temp[i++] = KS_MODIFIER;
9506 temp[i++] = mod_mask;
9507 }
9508 if (IS_SPECIAL(n))
9509 {
9510 temp[i++] = K_SPECIAL;
9511 temp[i++] = K_SECOND(n);
9512 temp[i++] = K_THIRD(n);
9513 }
9514#ifdef FEAT_MBYTE
9515 else if (has_mbyte)
9516 i += (*mb_char2bytes)(n, temp + i);
9517#endif
9518 else
9519 temp[i++] = n;
9520 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521 rettv->v_type = VAR_STRING;
9522 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 }
9524}
9525
9526/*
9527 * "getcharmod()" function
9528 */
9529/*ARGSUSED*/
9530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009531f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009532 typval_T *argvars;
9533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536}
9537
9538/*
9539 * "getcmdline()" function
9540 */
9541/*ARGSUSED*/
9542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009543f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009544 typval_T *argvars;
9545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 rettv->v_type = VAR_STRING;
9548 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549}
9550
9551/*
9552 * "getcmdpos()" function
9553 */
9554/*ARGSUSED*/
9555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009557 typval_T *argvars;
9558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561}
9562
9563/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009564 * "getcmdtype()" function
9565 */
9566/*ARGSUSED*/
9567 static void
9568f_getcmdtype(argvars, rettv)
9569 typval_T *argvars;
9570 typval_T *rettv;
9571{
9572 rettv->v_type = VAR_STRING;
9573 rettv->vval.v_string = alloc(2);
9574 if (rettv->vval.v_string != NULL)
9575 {
9576 rettv->vval.v_string[0] = get_cmdline_type();
9577 rettv->vval.v_string[1] = NUL;
9578 }
9579}
9580
9581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 * "getcwd()" function
9583 */
9584/*ARGSUSED*/
9585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009587 typval_T *argvars;
9588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589{
9590 char_u cwd[MAXPATHL];
9591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009592 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 else
9596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009597 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009599 if (rettv->vval.v_string != NULL)
9600 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601#endif
9602 }
9603}
9604
9605/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009606 * "getfontname()" function
9607 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009608/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009611 typval_T *argvars;
9612 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009613{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 rettv->v_type = VAR_STRING;
9615 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009616#ifdef FEAT_GUI
9617 if (gui.in_use)
9618 {
9619 GuiFont font;
9620 char_u *name = NULL;
9621
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009622 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009623 {
9624 /* Get the "Normal" font. Either the name saved by
9625 * hl_set_font_name() or from the font ID. */
9626 font = gui.norm_font;
9627 name = hl_get_font_name();
9628 }
9629 else
9630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009631 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009632 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9633 return;
9634 font = gui_mch_get_font(name, FALSE);
9635 if (font == NOFONT)
9636 return; /* Invalid font name, return empty string. */
9637 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009639 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009640 gui_mch_free_font(font);
9641 }
9642#endif
9643}
9644
9645/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009646 * "getfperm({fname})" function
9647 */
9648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009650 typval_T *argvars;
9651 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009652{
9653 char_u *fname;
9654 struct stat st;
9655 char_u *perm = NULL;
9656 char_u flags[] = "rwx";
9657 int i;
9658
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009660
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009662 if (mch_stat((char *)fname, &st) >= 0)
9663 {
9664 perm = vim_strsave((char_u *)"---------");
9665 if (perm != NULL)
9666 {
9667 for (i = 0; i < 9; i++)
9668 {
9669 if (st.st_mode & (1 << (8 - i)))
9670 perm[i] = flags[i % 3];
9671 }
9672 }
9673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009675}
9676
9677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 * "getfsize({fname})" function
9679 */
9680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009682 typval_T *argvars;
9683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684{
9685 char_u *fname;
9686 struct stat st;
9687
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009690 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691
9692 if (mch_stat((char *)fname, &st) >= 0)
9693 {
9694 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 }
9699 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701}
9702
9703/*
9704 * "getftime({fname})" function
9705 */
9706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009708 typval_T *argvars;
9709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
9711 char_u *fname;
9712 struct stat st;
9713
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715
9716 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009717 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720}
9721
9722/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009723 * "getftype({fname})" function
9724 */
9725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009726f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009727 typval_T *argvars;
9728 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009729{
9730 char_u *fname;
9731 struct stat st;
9732 char_u *type = NULL;
9733 char *t;
9734
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009735 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009738 if (mch_lstat((char *)fname, &st) >= 0)
9739 {
9740#ifdef S_ISREG
9741 if (S_ISREG(st.st_mode))
9742 t = "file";
9743 else if (S_ISDIR(st.st_mode))
9744 t = "dir";
9745# ifdef S_ISLNK
9746 else if (S_ISLNK(st.st_mode))
9747 t = "link";
9748# endif
9749# ifdef S_ISBLK
9750 else if (S_ISBLK(st.st_mode))
9751 t = "bdev";
9752# endif
9753# ifdef S_ISCHR
9754 else if (S_ISCHR(st.st_mode))
9755 t = "cdev";
9756# endif
9757# ifdef S_ISFIFO
9758 else if (S_ISFIFO(st.st_mode))
9759 t = "fifo";
9760# endif
9761# ifdef S_ISSOCK
9762 else if (S_ISSOCK(st.st_mode))
9763 t = "fifo";
9764# endif
9765 else
9766 t = "other";
9767#else
9768# ifdef S_IFMT
9769 switch (st.st_mode & S_IFMT)
9770 {
9771 case S_IFREG: t = "file"; break;
9772 case S_IFDIR: t = "dir"; break;
9773# ifdef S_IFLNK
9774 case S_IFLNK: t = "link"; break;
9775# endif
9776# ifdef S_IFBLK
9777 case S_IFBLK: t = "bdev"; break;
9778# endif
9779# ifdef S_IFCHR
9780 case S_IFCHR: t = "cdev"; break;
9781# endif
9782# ifdef S_IFIFO
9783 case S_IFIFO: t = "fifo"; break;
9784# endif
9785# ifdef S_IFSOCK
9786 case S_IFSOCK: t = "socket"; break;
9787# endif
9788 default: t = "other";
9789 }
9790# else
9791 if (mch_isdir(fname))
9792 t = "dir";
9793 else
9794 t = "file";
9795# endif
9796#endif
9797 type = vim_strsave((char_u *)t);
9798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009799 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009800}
9801
9802/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009803 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009804 */
9805 static void
9806f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009807 typval_T *argvars;
9808 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009809{
9810 linenr_T lnum;
9811 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009812 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009813
9814 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009815 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009816 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009817 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009818 retlist = FALSE;
9819 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009820 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009821 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009822 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009823 retlist = TRUE;
9824 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009825
Bram Moolenaar342337a2005-07-21 21:11:17 +00009826 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009827}
9828
9829/*
Bram Moolenaar280f1262006-01-30 00:14:18 +00009830 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +00009831 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00009832/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +00009833 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +00009834f_getqflist(argvars, rettv)
9835 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009836 typval_T *rettv;
9837{
9838#ifdef FEAT_QUICKFIX
9839 list_T *l;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009840 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009841#endif
9842
9843 rettv->vval.v_number = FALSE;
9844#ifdef FEAT_QUICKFIX
9845 l = list_alloc();
9846 if (l != NULL)
9847 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009848 rettv->vval.v_list = l;
9849 rettv->v_type = VAR_LIST;
9850 ++l->lv_refcount;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009851 wp = NULL;
9852 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
9853 {
9854 wp = find_win_by_nr(&argvars[0]);
9855 if (wp == NULL)
9856 return;
9857 }
9858
Bram Moolenaar17c7c012006-01-26 22:25:15 +00009859 (void)get_errorlist(wp, l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009860 }
9861#endif
9862}
9863
9864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 * "getreg()" function
9866 */
9867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009869 typval_T *argvars;
9870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871{
9872 char_u *strregname;
9873 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009874 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009875 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009877 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009879 strregname = get_tv_string_chk(&argvars[0]);
9880 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009881 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009882 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009885 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 regname = (strregname == NULL ? '"' : *strregname);
9887 if (regname == 0)
9888 regname = '"';
9889
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009891 rettv->vval.v_string = error ? NULL :
9892 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893}
9894
9895/*
9896 * "getregtype()" function
9897 */
9898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009899f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009900 typval_T *argvars;
9901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902{
9903 char_u *strregname;
9904 int regname;
9905 char_u buf[NUMBUFLEN + 2];
9906 long reglen = 0;
9907
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009908 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009909 {
9910 strregname = get_tv_string_chk(&argvars[0]);
9911 if (strregname == NULL) /* type error; errmsg already given */
9912 {
9913 rettv->v_type = VAR_STRING;
9914 rettv->vval.v_string = NULL;
9915 return;
9916 }
9917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 else
9919 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921
9922 regname = (strregname == NULL ? '"' : *strregname);
9923 if (regname == 0)
9924 regname = '"';
9925
9926 buf[0] = NUL;
9927 buf[1] = NUL;
9928 switch (get_reg_type(regname, &reglen))
9929 {
9930 case MLINE: buf[0] = 'V'; break;
9931 case MCHAR: buf[0] = 'v'; break;
9932#ifdef FEAT_VISUAL
9933 case MBLOCK:
9934 buf[0] = Ctrl_V;
9935 sprintf((char *)buf + 1, "%ld", reglen + 1);
9936 break;
9937#endif
9938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939 rettv->v_type = VAR_STRING;
9940 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941}
9942
9943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 * "getwinposx()" function
9945 */
9946/*ARGSUSED*/
9947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009948f_getwinposx(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 = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 }
9961#endif
9962}
9963
9964/*
9965 * "getwinposy()" function
9966 */
9967/*ARGSUSED*/
9968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009969f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009970 typval_T *argvars;
9971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974#ifdef FEAT_GUI
9975 if (gui.in_use)
9976 {
9977 int x, y;
9978
9979 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009980 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 }
9982#endif
9983}
9984
Bram Moolenaara40058a2005-07-11 22:42:07 +00009985 static win_T *
9986find_win_by_nr(vp)
9987 typval_T *vp;
9988{
9989#ifdef FEAT_WINDOWS
9990 win_T *wp;
9991#endif
9992 int nr;
9993
9994 nr = get_tv_number_chk(vp, NULL);
9995
9996#ifdef FEAT_WINDOWS
9997 if (nr < 0)
9998 return NULL;
9999 if (nr == 0)
10000 return curwin;
10001
10002 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10003 if (--nr <= 0)
10004 break;
10005 return wp;
10006#else
10007 if (nr == 0 || nr == 1)
10008 return curwin;
10009 return NULL;
10010#endif
10011}
10012
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013/*
10014 * "getwinvar()" function
10015 */
10016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010017f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010018 typval_T *argvars;
10019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020{
10021 win_T *win, *oldcurwin;
10022 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010023 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010026 varname = get_tv_string_chk(&argvars[1]);
10027 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010029 rettv->v_type = VAR_STRING;
10030 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031
10032 if (win != NULL && varname != NULL)
10033 {
10034 if (*varname == '&') /* window-local-option */
10035 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010036 /* Set curwin to be our win, temporarily. Also set curbuf, so
10037 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 oldcurwin = curwin;
10039 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010040 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010042 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043
10044 /* restore previous notion of curwin */
10045 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010046 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 }
10048 else
10049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010050 if (*varname == NUL)
10051 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10052 * scope prefix before the NUL byte is required by
10053 * find_var_in_ht(). */
10054 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010056 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010058 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 }
10060 }
10061
10062 --emsg_off;
10063}
10064
10065/*
10066 * "glob()" function
10067 */
10068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010069f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010070 typval_T *argvars;
10071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072{
10073 expand_T xpc;
10074
10075 ExpandInit(&xpc);
10076 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 rettv->v_type = VAR_STRING;
10078 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10080 ExpandCleanup(&xpc);
10081}
10082
10083/*
10084 * "globpath()" function
10085 */
10086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010088 typval_T *argvars;
10089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
10091 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010092 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010095 if (file == NULL)
10096 rettv->vval.v_string = NULL;
10097 else
10098 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099}
10100
10101/*
10102 * "has()" function
10103 */
10104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010105f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010106 typval_T *argvars;
10107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108{
10109 int i;
10110 char_u *name;
10111 int n = FALSE;
10112 static char *(has_list[]) =
10113 {
10114#ifdef AMIGA
10115 "amiga",
10116# ifdef FEAT_ARP
10117 "arp",
10118# endif
10119#endif
10120#ifdef __BEOS__
10121 "beos",
10122#endif
10123#ifdef MSDOS
10124# ifdef DJGPP
10125 "dos32",
10126# else
10127 "dos16",
10128# endif
10129#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010130#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 "mac",
10132#endif
10133#if defined(MACOS_X_UNIX)
10134 "macunix",
10135#endif
10136#ifdef OS2
10137 "os2",
10138#endif
10139#ifdef __QNX__
10140 "qnx",
10141#endif
10142#ifdef RISCOS
10143 "riscos",
10144#endif
10145#ifdef UNIX
10146 "unix",
10147#endif
10148#ifdef VMS
10149 "vms",
10150#endif
10151#ifdef WIN16
10152 "win16",
10153#endif
10154#ifdef WIN32
10155 "win32",
10156#endif
10157#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10158 "win32unix",
10159#endif
10160#ifdef WIN64
10161 "win64",
10162#endif
10163#ifdef EBCDIC
10164 "ebcdic",
10165#endif
10166#ifndef CASE_INSENSITIVE_FILENAME
10167 "fname_case",
10168#endif
10169#ifdef FEAT_ARABIC
10170 "arabic",
10171#endif
10172#ifdef FEAT_AUTOCMD
10173 "autocmd",
10174#endif
10175#ifdef FEAT_BEVAL
10176 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010177# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10178 "balloon_multiline",
10179# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180#endif
10181#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10182 "builtin_terms",
10183# ifdef ALL_BUILTIN_TCAPS
10184 "all_builtin_terms",
10185# endif
10186#endif
10187#ifdef FEAT_BYTEOFF
10188 "byte_offset",
10189#endif
10190#ifdef FEAT_CINDENT
10191 "cindent",
10192#endif
10193#ifdef FEAT_CLIENTSERVER
10194 "clientserver",
10195#endif
10196#ifdef FEAT_CLIPBOARD
10197 "clipboard",
10198#endif
10199#ifdef FEAT_CMDL_COMPL
10200 "cmdline_compl",
10201#endif
10202#ifdef FEAT_CMDHIST
10203 "cmdline_hist",
10204#endif
10205#ifdef FEAT_COMMENTS
10206 "comments",
10207#endif
10208#ifdef FEAT_CRYPT
10209 "cryptv",
10210#endif
10211#ifdef FEAT_CSCOPE
10212 "cscope",
10213#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010214#ifdef CURSOR_SHAPE
10215 "cursorshape",
10216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217#ifdef DEBUG
10218 "debug",
10219#endif
10220#ifdef FEAT_CON_DIALOG
10221 "dialog_con",
10222#endif
10223#ifdef FEAT_GUI_DIALOG
10224 "dialog_gui",
10225#endif
10226#ifdef FEAT_DIFF
10227 "diff",
10228#endif
10229#ifdef FEAT_DIGRAPHS
10230 "digraphs",
10231#endif
10232#ifdef FEAT_DND
10233 "dnd",
10234#endif
10235#ifdef FEAT_EMACS_TAGS
10236 "emacs_tags",
10237#endif
10238 "eval", /* always present, of course! */
10239#ifdef FEAT_EX_EXTRA
10240 "ex_extra",
10241#endif
10242#ifdef FEAT_SEARCH_EXTRA
10243 "extra_search",
10244#endif
10245#ifdef FEAT_FKMAP
10246 "farsi",
10247#endif
10248#ifdef FEAT_SEARCHPATH
10249 "file_in_path",
10250#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010251#if defined(UNIX) && !defined(USE_SYSTEM)
10252 "filterpipe",
10253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254#ifdef FEAT_FIND_ID
10255 "find_in_path",
10256#endif
10257#ifdef FEAT_FOLDING
10258 "folding",
10259#endif
10260#ifdef FEAT_FOOTER
10261 "footer",
10262#endif
10263#if !defined(USE_SYSTEM) && defined(UNIX)
10264 "fork",
10265#endif
10266#ifdef FEAT_GETTEXT
10267 "gettext",
10268#endif
10269#ifdef FEAT_GUI
10270 "gui",
10271#endif
10272#ifdef FEAT_GUI_ATHENA
10273# ifdef FEAT_GUI_NEXTAW
10274 "gui_neXtaw",
10275# else
10276 "gui_athena",
10277# endif
10278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279#ifdef FEAT_GUI_GTK
10280 "gui_gtk",
10281# ifdef HAVE_GTK2
10282 "gui_gtk2",
10283# endif
10284#endif
10285#ifdef FEAT_GUI_MAC
10286 "gui_mac",
10287#endif
10288#ifdef FEAT_GUI_MOTIF
10289 "gui_motif",
10290#endif
10291#ifdef FEAT_GUI_PHOTON
10292 "gui_photon",
10293#endif
10294#ifdef FEAT_GUI_W16
10295 "gui_win16",
10296#endif
10297#ifdef FEAT_GUI_W32
10298 "gui_win32",
10299#endif
10300#ifdef FEAT_HANGULIN
10301 "hangul_input",
10302#endif
10303#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10304 "iconv",
10305#endif
10306#ifdef FEAT_INS_EXPAND
10307 "insert_expand",
10308#endif
10309#ifdef FEAT_JUMPLIST
10310 "jumplist",
10311#endif
10312#ifdef FEAT_KEYMAP
10313 "keymap",
10314#endif
10315#ifdef FEAT_LANGMAP
10316 "langmap",
10317#endif
10318#ifdef FEAT_LIBCALL
10319 "libcall",
10320#endif
10321#ifdef FEAT_LINEBREAK
10322 "linebreak",
10323#endif
10324#ifdef FEAT_LISP
10325 "lispindent",
10326#endif
10327#ifdef FEAT_LISTCMDS
10328 "listcmds",
10329#endif
10330#ifdef FEAT_LOCALMAP
10331 "localmap",
10332#endif
10333#ifdef FEAT_MENU
10334 "menu",
10335#endif
10336#ifdef FEAT_SESSION
10337 "mksession",
10338#endif
10339#ifdef FEAT_MODIFY_FNAME
10340 "modify_fname",
10341#endif
10342#ifdef FEAT_MOUSE
10343 "mouse",
10344#endif
10345#ifdef FEAT_MOUSESHAPE
10346 "mouseshape",
10347#endif
10348#if defined(UNIX) || defined(VMS)
10349# ifdef FEAT_MOUSE_DEC
10350 "mouse_dec",
10351# endif
10352# ifdef FEAT_MOUSE_GPM
10353 "mouse_gpm",
10354# endif
10355# ifdef FEAT_MOUSE_JSB
10356 "mouse_jsbterm",
10357# endif
10358# ifdef FEAT_MOUSE_NET
10359 "mouse_netterm",
10360# endif
10361# ifdef FEAT_MOUSE_PTERM
10362 "mouse_pterm",
10363# endif
10364# ifdef FEAT_MOUSE_XTERM
10365 "mouse_xterm",
10366# endif
10367#endif
10368#ifdef FEAT_MBYTE
10369 "multi_byte",
10370#endif
10371#ifdef FEAT_MBYTE_IME
10372 "multi_byte_ime",
10373#endif
10374#ifdef FEAT_MULTI_LANG
10375 "multi_lang",
10376#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010377#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010378#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010379 "mzscheme",
10380#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382#ifdef FEAT_OLE
10383 "ole",
10384#endif
10385#ifdef FEAT_OSFILETYPE
10386 "osfiletype",
10387#endif
10388#ifdef FEAT_PATH_EXTRA
10389 "path_extra",
10390#endif
10391#ifdef FEAT_PERL
10392#ifndef DYNAMIC_PERL
10393 "perl",
10394#endif
10395#endif
10396#ifdef FEAT_PYTHON
10397#ifndef DYNAMIC_PYTHON
10398 "python",
10399#endif
10400#endif
10401#ifdef FEAT_POSTSCRIPT
10402 "postscript",
10403#endif
10404#ifdef FEAT_PRINTER
10405 "printer",
10406#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010407#ifdef FEAT_PROFILE
10408 "profile",
10409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410#ifdef FEAT_QUICKFIX
10411 "quickfix",
10412#endif
10413#ifdef FEAT_RIGHTLEFT
10414 "rightleft",
10415#endif
10416#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10417 "ruby",
10418#endif
10419#ifdef FEAT_SCROLLBIND
10420 "scrollbind",
10421#endif
10422#ifdef FEAT_CMDL_INFO
10423 "showcmd",
10424 "cmdline_info",
10425#endif
10426#ifdef FEAT_SIGNS
10427 "signs",
10428#endif
10429#ifdef FEAT_SMARTINDENT
10430 "smartindent",
10431#endif
10432#ifdef FEAT_SNIFF
10433 "sniff",
10434#endif
10435#ifdef FEAT_STL_OPT
10436 "statusline",
10437#endif
10438#ifdef FEAT_SUN_WORKSHOP
10439 "sun_workshop",
10440#endif
10441#ifdef FEAT_NETBEANS_INTG
10442 "netbeans_intg",
10443#endif
10444#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010445 "spell",
10446#endif
10447#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 "syntax",
10449#endif
10450#if defined(USE_SYSTEM) || !defined(UNIX)
10451 "system",
10452#endif
10453#ifdef FEAT_TAG_BINS
10454 "tag_binary",
10455#endif
10456#ifdef FEAT_TAG_OLDSTATIC
10457 "tag_old_static",
10458#endif
10459#ifdef FEAT_TAG_ANYWHITE
10460 "tag_any_white",
10461#endif
10462#ifdef FEAT_TCL
10463# ifndef DYNAMIC_TCL
10464 "tcl",
10465# endif
10466#endif
10467#ifdef TERMINFO
10468 "terminfo",
10469#endif
10470#ifdef FEAT_TERMRESPONSE
10471 "termresponse",
10472#endif
10473#ifdef FEAT_TEXTOBJ
10474 "textobjects",
10475#endif
10476#ifdef HAVE_TGETENT
10477 "tgetent",
10478#endif
10479#ifdef FEAT_TITLE
10480 "title",
10481#endif
10482#ifdef FEAT_TOOLBAR
10483 "toolbar",
10484#endif
10485#ifdef FEAT_USR_CMDS
10486 "user-commands", /* was accidentally included in 5.4 */
10487 "user_commands",
10488#endif
10489#ifdef FEAT_VIMINFO
10490 "viminfo",
10491#endif
10492#ifdef FEAT_VERTSPLIT
10493 "vertsplit",
10494#endif
10495#ifdef FEAT_VIRTUALEDIT
10496 "virtualedit",
10497#endif
10498#ifdef FEAT_VISUAL
10499 "visual",
10500#endif
10501#ifdef FEAT_VISUALEXTRA
10502 "visualextra",
10503#endif
10504#ifdef FEAT_VREPLACE
10505 "vreplace",
10506#endif
10507#ifdef FEAT_WILDIGN
10508 "wildignore",
10509#endif
10510#ifdef FEAT_WILDMENU
10511 "wildmenu",
10512#endif
10513#ifdef FEAT_WINDOWS
10514 "windows",
10515#endif
10516#ifdef FEAT_WAK
10517 "winaltkeys",
10518#endif
10519#ifdef FEAT_WRITEBACKUP
10520 "writebackup",
10521#endif
10522#ifdef FEAT_XIM
10523 "xim",
10524#endif
10525#ifdef FEAT_XFONTSET
10526 "xfontset",
10527#endif
10528#ifdef USE_XSMP
10529 "xsmp",
10530#endif
10531#ifdef USE_XSMP_INTERACT
10532 "xsmp_interact",
10533#endif
10534#ifdef FEAT_XCLIPBOARD
10535 "xterm_clipboard",
10536#endif
10537#ifdef FEAT_XTERM_SAVE
10538 "xterm_save",
10539#endif
10540#if defined(UNIX) && defined(FEAT_X11)
10541 "X11",
10542#endif
10543 NULL
10544 };
10545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 for (i = 0; has_list[i] != NULL; ++i)
10548 if (STRICMP(name, has_list[i]) == 0)
10549 {
10550 n = TRUE;
10551 break;
10552 }
10553
10554 if (n == FALSE)
10555 {
10556 if (STRNICMP(name, "patch", 5) == 0)
10557 n = has_patch(atoi((char *)name + 5));
10558 else if (STRICMP(name, "vim_starting") == 0)
10559 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010560#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10561 else if (STRICMP(name, "balloon_multiline") == 0)
10562 n = multiline_balloon_available();
10563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564#ifdef DYNAMIC_TCL
10565 else if (STRICMP(name, "tcl") == 0)
10566 n = tcl_enabled(FALSE);
10567#endif
10568#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10569 else if (STRICMP(name, "iconv") == 0)
10570 n = iconv_enabled(FALSE);
10571#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010572#ifdef DYNAMIC_MZSCHEME
10573 else if (STRICMP(name, "mzscheme") == 0)
10574 n = mzscheme_enabled(FALSE);
10575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576#ifdef DYNAMIC_RUBY
10577 else if (STRICMP(name, "ruby") == 0)
10578 n = ruby_enabled(FALSE);
10579#endif
10580#ifdef DYNAMIC_PYTHON
10581 else if (STRICMP(name, "python") == 0)
10582 n = python_enabled(FALSE);
10583#endif
10584#ifdef DYNAMIC_PERL
10585 else if (STRICMP(name, "perl") == 0)
10586 n = perl_enabled(FALSE);
10587#endif
10588#ifdef FEAT_GUI
10589 else if (STRICMP(name, "gui_running") == 0)
10590 n = (gui.in_use || gui.starting);
10591# ifdef FEAT_GUI_W32
10592 else if (STRICMP(name, "gui_win32s") == 0)
10593 n = gui_is_win32s();
10594# endif
10595# ifdef FEAT_BROWSE
10596 else if (STRICMP(name, "browse") == 0)
10597 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10598# endif
10599#endif
10600#ifdef FEAT_SYN_HL
10601 else if (STRICMP(name, "syntax_items") == 0)
10602 n = syntax_present(curbuf);
10603#endif
10604#if defined(WIN3264)
10605 else if (STRICMP(name, "win95") == 0)
10606 n = mch_windows95();
10607#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010608#ifdef FEAT_NETBEANS_INTG
10609 else if (STRICMP(name, "netbeans_enabled") == 0)
10610 n = usingNetbeans;
10611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 }
10613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010614 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615}
10616
10617/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010618 * "has_key()" function
10619 */
10620 static void
10621f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010622 typval_T *argvars;
10623 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010624{
10625 rettv->vval.v_number = 0;
10626 if (argvars[0].v_type != VAR_DICT)
10627 {
10628 EMSG(_(e_dictreq));
10629 return;
10630 }
10631 if (argvars[0].vval.v_dict == NULL)
10632 return;
10633
10634 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010635 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010636}
10637
10638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 * "hasmapto()" function
10640 */
10641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010642f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010643 typval_T *argvars;
10644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645{
10646 char_u *name;
10647 char_u *mode;
10648 char_u buf[NUMBUFLEN];
10649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010650 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010651 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 mode = (char_u *)"nvo";
10653 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010654 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655
10656 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010657 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010659 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660}
10661
10662/*
10663 * "histadd()" function
10664 */
10665/*ARGSUSED*/
10666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010667f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010668 typval_T *argvars;
10669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670{
10671#ifdef FEAT_CMDHIST
10672 int histype;
10673 char_u *str;
10674 char_u buf[NUMBUFLEN];
10675#endif
10676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010677 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 if (check_restricted() || check_secure())
10679 return;
10680#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010681 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10682 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 if (histype >= 0)
10684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010685 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 if (*str != NUL)
10687 {
10688 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 return;
10691 }
10692 }
10693#endif
10694}
10695
10696/*
10697 * "histdel()" function
10698 */
10699/*ARGSUSED*/
10700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010701f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010702 typval_T *argvars;
10703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704{
10705#ifdef FEAT_CMDHIST
10706 int n;
10707 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010708 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10711 if (str == NULL)
10712 n = 0;
10713 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010715 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010716 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010718 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 else
10721 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010722 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010723 get_tv_string_buf(&argvars[1], buf));
10724 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010726 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727#endif
10728}
10729
10730/*
10731 * "histget()" function
10732 */
10733/*ARGSUSED*/
10734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010736 typval_T *argvars;
10737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738{
10739#ifdef FEAT_CMDHIST
10740 int type;
10741 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010742 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010744 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10745 if (str == NULL)
10746 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010748 {
10749 type = get_histtype(str);
10750 if (argvars[1].v_type == VAR_UNKNOWN)
10751 idx = get_history_idx(type);
10752 else
10753 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10754 /* -1 on type error */
10755 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010758 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010760 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761}
10762
10763/*
10764 * "histnr()" function
10765 */
10766/*ARGSUSED*/
10767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010768f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010769 typval_T *argvars;
10770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771{
10772 int i;
10773
10774#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010775 char_u *history = get_tv_string_chk(&argvars[0]);
10776
10777 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 if (i >= HIST_CMD && i < HIST_COUNT)
10779 i = get_history_idx(i);
10780 else
10781#endif
10782 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010783 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784}
10785
10786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 * "highlightID(name)" function
10788 */
10789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010790f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010791 typval_T *argvars;
10792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010794 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795}
10796
10797/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010798 * "highlight_exists()" function
10799 */
10800 static void
10801f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010802 typval_T *argvars;
10803 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010804{
10805 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10806}
10807
10808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 * "hostname()" function
10810 */
10811/*ARGSUSED*/
10812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010814 typval_T *argvars;
10815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816{
10817 char_u hostname[256];
10818
10819 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010820 rettv->v_type = VAR_STRING;
10821 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822}
10823
10824/*
10825 * iconv() function
10826 */
10827/*ARGSUSED*/
10828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010829f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010830 typval_T *argvars;
10831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832{
10833#ifdef FEAT_MBYTE
10834 char_u buf1[NUMBUFLEN];
10835 char_u buf2[NUMBUFLEN];
10836 char_u *from, *to, *str;
10837 vimconv_T vimconv;
10838#endif
10839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 rettv->v_type = VAR_STRING;
10841 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842
10843#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010844 str = get_tv_string(&argvars[0]);
10845 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10846 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 vimconv.vc_type = CONV_NONE;
10848 convert_setup(&vimconv, from, to);
10849
10850 /* If the encodings are equal, no conversion needed. */
10851 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010854 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855
10856 convert_setup(&vimconv, NULL, NULL);
10857 vim_free(from);
10858 vim_free(to);
10859#endif
10860}
10861
10862/*
10863 * "indent()" function
10864 */
10865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010866f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010867 typval_T *argvars;
10868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869{
10870 linenr_T lnum;
10871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010876 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877}
10878
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010879/*
10880 * "index()" function
10881 */
10882 static void
10883f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010884 typval_T *argvars;
10885 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010886{
Bram Moolenaar33570922005-01-25 22:26:29 +000010887 list_T *l;
10888 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010889 long idx = 0;
10890 int ic = FALSE;
10891
10892 rettv->vval.v_number = -1;
10893 if (argvars[0].v_type != VAR_LIST)
10894 {
10895 EMSG(_(e_listreq));
10896 return;
10897 }
10898 l = argvars[0].vval.v_list;
10899 if (l != NULL)
10900 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010901 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010902 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010904 int error = FALSE;
10905
Bram Moolenaar758711c2005-02-02 23:11:38 +000010906 /* Start at specified item. Use the cached index that list_find()
10907 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010908 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010909 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010910 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010911 ic = get_tv_number_chk(&argvars[3], &error);
10912 if (error)
10913 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010914 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010915
Bram Moolenaar758711c2005-02-02 23:11:38 +000010916 for ( ; item != NULL; item = item->li_next, ++idx)
10917 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010918 {
10919 rettv->vval.v_number = idx;
10920 break;
10921 }
10922 }
10923}
10924
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925static int inputsecret_flag = 0;
10926
10927/*
10928 * "input()" function
10929 * Also handles inputsecret() when inputsecret is set.
10930 */
10931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010932f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010933 typval_T *argvars;
10934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010936 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 char_u *p = NULL;
10938 int c;
10939 char_u buf[NUMBUFLEN];
10940 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010941 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010942 int xp_type = EXPAND_NOTHING;
10943 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010945 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946
10947#ifdef NO_CONSOLE_INPUT
10948 /* While starting up, there is no place to enter text. */
10949 if (no_console_input())
10950 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010951 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 return;
10953 }
10954#endif
10955
10956 cmd_silent = FALSE; /* Want to see the prompt. */
10957 if (prompt != NULL)
10958 {
10959 /* Only the part of the message after the last NL is considered as
10960 * prompt for the command line */
10961 p = vim_strrchr(prompt, '\n');
10962 if (p == NULL)
10963 p = prompt;
10964 else
10965 {
10966 ++p;
10967 c = *p;
10968 *p = NUL;
10969 msg_start();
10970 msg_clr_eos();
10971 msg_puts_attr(prompt, echo_attr);
10972 msg_didout = FALSE;
10973 msg_starthere();
10974 *p = c;
10975 }
10976 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010978 if (argvars[1].v_type != VAR_UNKNOWN)
10979 {
10980 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10981 if (defstr != NULL)
10982 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983
Bram Moolenaar4463f292005-09-25 22:20:24 +000010984 if (argvars[2].v_type != VAR_UNKNOWN)
10985 {
10986 char_u *xp_name;
10987 int xp_namelen;
10988 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010989
Bram Moolenaar4463f292005-09-25 22:20:24 +000010990 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010991
Bram Moolenaar4463f292005-09-25 22:20:24 +000010992 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
10993 if (xp_name == NULL)
10994 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010995
Bram Moolenaar4463f292005-09-25 22:20:24 +000010996 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010997
Bram Moolenaar4463f292005-09-25 22:20:24 +000010998 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
10999 &xp_arg) == FAIL)
11000 return;
11001 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011002 }
11003
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011004 if (defstr != NULL)
11005 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011006 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11007 xp_type, xp_arg);
11008
11009 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011011 /* since the user typed this, no need to wait for return */
11012 need_wait_return = FALSE;
11013 msg_didout = FALSE;
11014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 cmd_silent = cmd_silent_save;
11016}
11017
11018/*
11019 * "inputdialog()" function
11020 */
11021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011023 typval_T *argvars;
11024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025{
11026#if defined(FEAT_GUI_TEXTDIALOG)
11027 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11028 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11029 {
11030 char_u *message;
11031 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011032 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011034 message = get_tv_string_chk(&argvars[0]);
11035 if (argvars[1].v_type != VAR_UNKNOWN
11036 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011037 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 else
11039 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011040 if (message != NULL && defstr != NULL
11041 && do_dialog(VIM_QUESTION, NULL, message,
11042 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011043 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 else
11045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011046 if (message != NULL && defstr != NULL
11047 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011048 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011049 rettv->vval.v_string = vim_strsave(
11050 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 }
11056 else
11057#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059}
11060
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011061/*
11062 * "inputlist()" function
11063 */
11064 static void
11065f_inputlist(argvars, rettv)
11066 typval_T *argvars;
11067 typval_T *rettv;
11068{
11069 listitem_T *li;
11070 int selected;
11071 int mouse_used;
11072
11073 rettv->vval.v_number = 0;
11074#ifdef NO_CONSOLE_INPUT
11075 /* While starting up, there is no place to enter text. */
11076 if (no_console_input())
11077 return;
11078#endif
11079 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11080 {
11081 EMSG2(_(e_listarg), "inputlist()");
11082 return;
11083 }
11084
11085 msg_start();
11086 lines_left = Rows; /* avoid more prompt */
11087 msg_scroll = TRUE;
11088 msg_clr_eos();
11089
11090 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11091 {
11092 msg_puts(get_tv_string(&li->li_tv));
11093 msg_putchar('\n');
11094 }
11095
11096 /* Ask for choice. */
11097 selected = prompt_for_number(&mouse_used);
11098 if (mouse_used)
11099 selected -= lines_left;
11100
11101 rettv->vval.v_number = selected;
11102}
11103
11104
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11106
11107/*
11108 * "inputrestore()" function
11109 */
11110/*ARGSUSED*/
11111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011113 typval_T *argvars;
11114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115{
11116 if (ga_userinput.ga_len > 0)
11117 {
11118 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11120 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 }
11123 else if (p_verbose > 1)
11124 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011125 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127 }
11128}
11129
11130/*
11131 * "inputsave()" function
11132 */
11133/*ARGSUSED*/
11134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011135f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011136 typval_T *argvars;
11137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138{
11139 /* Add an entry to the stack of typehead storage. */
11140 if (ga_grow(&ga_userinput, 1) == OK)
11141 {
11142 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11143 + ga_userinput.ga_len);
11144 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011145 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 }
11147 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149}
11150
11151/*
11152 * "inputsecret()" function
11153 */
11154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011156 typval_T *argvars;
11157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158{
11159 ++cmdline_star;
11160 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011161 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 --cmdline_star;
11163 --inputsecret_flag;
11164}
11165
11166/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011167 * "insert()" function
11168 */
11169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011170f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011171 typval_T *argvars;
11172 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011173{
11174 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011175 listitem_T *item;
11176 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011177 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011178
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011179 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011180 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011181 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011182 else if ((l = argvars[0].vval.v_list) != NULL
11183 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011184 {
11185 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011186 before = get_tv_number_chk(&argvars[2], &error);
11187 if (error)
11188 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011189
Bram Moolenaar758711c2005-02-02 23:11:38 +000011190 if (before == l->lv_len)
11191 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011192 else
11193 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011194 item = list_find(l, before);
11195 if (item == NULL)
11196 {
11197 EMSGN(_(e_listidx), before);
11198 l = NULL;
11199 }
11200 }
11201 if (l != NULL)
11202 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011203 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011204 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011205 }
11206 }
11207}
11208
11209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 * "isdirectory()" function
11211 */
11212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011214 typval_T *argvars;
11215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011217 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218}
11219
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011220/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011221 * "islocked()" function
11222 */
11223 static void
11224f_islocked(argvars, rettv)
11225 typval_T *argvars;
11226 typval_T *rettv;
11227{
11228 lval_T lv;
11229 char_u *end;
11230 dictitem_T *di;
11231
11232 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011233 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11234 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011235 if (end != NULL && lv.ll_name != NULL)
11236 {
11237 if (*end != NUL)
11238 EMSG(_(e_trailing));
11239 else
11240 {
11241 if (lv.ll_tv == NULL)
11242 {
11243 if (check_changedtick(lv.ll_name))
11244 rettv->vval.v_number = 1; /* always locked */
11245 else
11246 {
11247 di = find_var(lv.ll_name, NULL);
11248 if (di != NULL)
11249 {
11250 /* Consider a variable locked when:
11251 * 1. the variable itself is locked
11252 * 2. the value of the variable is locked.
11253 * 3. the List or Dict value is locked.
11254 */
11255 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11256 || tv_islocked(&di->di_tv));
11257 }
11258 }
11259 }
11260 else if (lv.ll_range)
11261 EMSG(_("E745: Range not allowed"));
11262 else if (lv.ll_newkey != NULL)
11263 EMSG2(_(e_dictkey), lv.ll_newkey);
11264 else if (lv.ll_list != NULL)
11265 /* List item. */
11266 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11267 else
11268 /* Dictionary item. */
11269 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11270 }
11271 }
11272
11273 clear_lval(&lv);
11274}
11275
Bram Moolenaar33570922005-01-25 22:26:29 +000011276static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011277
11278/*
11279 * Turn a dict into a list:
11280 * "what" == 0: list of keys
11281 * "what" == 1: list of values
11282 * "what" == 2: list of items
11283 */
11284 static void
11285dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011286 typval_T *argvars;
11287 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011288 int what;
11289{
Bram Moolenaar33570922005-01-25 22:26:29 +000011290 list_T *l;
11291 list_T *l2;
11292 dictitem_T *di;
11293 hashitem_T *hi;
11294 listitem_T *li;
11295 listitem_T *li2;
11296 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011297 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011298
11299 rettv->vval.v_number = 0;
11300 if (argvars[0].v_type != VAR_DICT)
11301 {
11302 EMSG(_(e_dictreq));
11303 return;
11304 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011305 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011306 return;
11307
11308 l = list_alloc();
11309 if (l == NULL)
11310 return;
11311 rettv->v_type = VAR_LIST;
11312 rettv->vval.v_list = l;
11313 ++l->lv_refcount;
11314
Bram Moolenaar33570922005-01-25 22:26:29 +000011315 todo = d->dv_hashtab.ht_used;
11316 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011317 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011318 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011319 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011320 --todo;
11321 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011322
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011323 li = listitem_alloc();
11324 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011325 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011326 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011327
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011328 if (what == 0)
11329 {
11330 /* keys() */
11331 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011332 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011333 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11334 }
11335 else if (what == 1)
11336 {
11337 /* values() */
11338 copy_tv(&di->di_tv, &li->li_tv);
11339 }
11340 else
11341 {
11342 /* items() */
11343 l2 = list_alloc();
11344 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011345 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011346 li->li_tv.vval.v_list = l2;
11347 if (l2 == NULL)
11348 break;
11349 ++l2->lv_refcount;
11350
11351 li2 = listitem_alloc();
11352 if (li2 == NULL)
11353 break;
11354 list_append(l2, li2);
11355 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011356 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011357 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11358
11359 li2 = listitem_alloc();
11360 if (li2 == NULL)
11361 break;
11362 list_append(l2, li2);
11363 copy_tv(&di->di_tv, &li2->li_tv);
11364 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011365 }
11366 }
11367}
11368
11369/*
11370 * "items(dict)" function
11371 */
11372 static void
11373f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011374 typval_T *argvars;
11375 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011376{
11377 dict_list(argvars, rettv, 2);
11378}
11379
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011381 * "join()" function
11382 */
11383 static void
11384f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011385 typval_T *argvars;
11386 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011387{
11388 garray_T ga;
11389 char_u *sep;
11390
11391 rettv->vval.v_number = 0;
11392 if (argvars[0].v_type != VAR_LIST)
11393 {
11394 EMSG(_(e_listreq));
11395 return;
11396 }
11397 if (argvars[0].vval.v_list == NULL)
11398 return;
11399 if (argvars[1].v_type == VAR_UNKNOWN)
11400 sep = (char_u *)" ";
11401 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011402 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011403
11404 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405
11406 if (sep != NULL)
11407 {
11408 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011409 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011410 ga_append(&ga, NUL);
11411 rettv->vval.v_string = (char_u *)ga.ga_data;
11412 }
11413 else
11414 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011415}
11416
11417/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011418 * "keys()" function
11419 */
11420 static void
11421f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011422 typval_T *argvars;
11423 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011424{
11425 dict_list(argvars, rettv, 0);
11426}
11427
11428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 * "last_buffer_nr()" function.
11430 */
11431/*ARGSUSED*/
11432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011433f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011434 typval_T *argvars;
11435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436{
11437 int n = 0;
11438 buf_T *buf;
11439
11440 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11441 if (n < buf->b_fnum)
11442 n = buf->b_fnum;
11443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011445}
11446
11447/*
11448 * "len()" function
11449 */
11450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011451f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011452 typval_T *argvars;
11453 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011454{
11455 switch (argvars[0].v_type)
11456 {
11457 case VAR_STRING:
11458 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459 rettv->vval.v_number = (varnumber_T)STRLEN(
11460 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011461 break;
11462 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011463 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011464 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011465 case VAR_DICT:
11466 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11467 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011468 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011469 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011470 break;
11471 }
11472}
11473
Bram Moolenaar33570922005-01-25 22:26:29 +000011474static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011475
11476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011478 typval_T *argvars;
11479 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011480 int type;
11481{
11482#ifdef FEAT_LIBCALL
11483 char_u *string_in;
11484 char_u **string_result;
11485 int nr_result;
11486#endif
11487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011489 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011491 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011493
11494 if (check_restricted() || check_secure())
11495 return;
11496
11497#ifdef FEAT_LIBCALL
11498 /* The first two args must be strings, otherwise its meaningless */
11499 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11500 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011501 string_in = NULL;
11502 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011503 string_in = argvars[2].vval.v_string;
11504 if (type == VAR_NUMBER)
11505 string_result = NULL;
11506 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011507 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011508 if (mch_libcall(argvars[0].vval.v_string,
11509 argvars[1].vval.v_string,
11510 string_in,
11511 argvars[2].vval.v_number,
11512 string_result,
11513 &nr_result) == OK
11514 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011515 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011516 }
11517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518}
11519
11520/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011521 * "libcall()" function
11522 */
11523 static void
11524f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011525 typval_T *argvars;
11526 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011527{
11528 libcall_common(argvars, rettv, VAR_STRING);
11529}
11530
11531/*
11532 * "libcallnr()" function
11533 */
11534 static void
11535f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011536 typval_T *argvars;
11537 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011538{
11539 libcall_common(argvars, rettv, VAR_NUMBER);
11540}
11541
11542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543 * "line(string)" function
11544 */
11545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011547 typval_T *argvars;
11548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549{
11550 linenr_T lnum = 0;
11551 pos_T *fp;
11552
11553 fp = var2fpos(&argvars[0], TRUE);
11554 if (fp != NULL)
11555 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011556 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557}
11558
11559/*
11560 * "line2byte(lnum)" function
11561 */
11562/*ARGSUSED*/
11563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011564f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011565 typval_T *argvars;
11566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567{
11568#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011569 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570#else
11571 linenr_T lnum;
11572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011573 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011575 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11578 if (rettv->vval.v_number >= 0)
11579 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580#endif
11581}
11582
11583/*
11584 * "lispindent(lnum)" function
11585 */
11586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011588 typval_T *argvars;
11589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590{
11591#ifdef FEAT_LISP
11592 pos_T pos;
11593 linenr_T lnum;
11594
11595 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11598 {
11599 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011600 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601 curwin->w_cursor = pos;
11602 }
11603 else
11604#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606}
11607
11608/*
11609 * "localtime()" function
11610 */
11611/*ARGSUSED*/
11612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011613f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011614 typval_T *argvars;
11615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011617 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618}
11619
Bram Moolenaar33570922005-01-25 22:26:29 +000011620static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621
11622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011623get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011624 typval_T *argvars;
11625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 int exact;
11627{
11628 char_u *keys;
11629 char_u *which;
11630 char_u buf[NUMBUFLEN];
11631 char_u *keys_buf = NULL;
11632 char_u *rhs;
11633 int mode;
11634 garray_T ga;
11635
11636 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011637 rettv->v_type = VAR_STRING;
11638 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011640 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641 if (*keys == NUL)
11642 return;
11643
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011644 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011645 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646 else
11647 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011648 if (which == NULL)
11649 return;
11650
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651 mode = get_map_mode(&which, 0);
11652
11653 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011654 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655 vim_free(keys_buf);
11656 if (rhs != NULL)
11657 {
11658 ga_init(&ga);
11659 ga.ga_itemsize = 1;
11660 ga.ga_growsize = 40;
11661
11662 while (*rhs != NUL)
11663 ga_concat(&ga, str2special(&rhs, FALSE));
11664
11665 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667 }
11668}
11669
11670/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011671 * "map()" function
11672 */
11673 static void
11674f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011675 typval_T *argvars;
11676 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011677{
11678 filter_map(argvars, rettv, TRUE);
11679}
11680
11681/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011682 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 */
11684 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011685f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011686 typval_T *argvars;
11687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011689 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690}
11691
11692/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011693 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694 */
11695 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011696f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011697 typval_T *argvars;
11698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011700 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701}
11702
Bram Moolenaar33570922005-01-25 22:26:29 +000011703static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704
11705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011706find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011707 typval_T *argvars;
11708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 int type;
11710{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011711 char_u *str = NULL;
11712 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 char_u *pat;
11714 regmatch_T regmatch;
11715 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011716 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 char_u *save_cpo;
11718 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011719 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011720 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011721 list_T *l = NULL;
11722 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011723 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011724 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725
11726 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11727 save_cpo = p_cpo;
11728 p_cpo = (char_u *)"";
11729
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011730 rettv->vval.v_number = -1;
11731 if (type == 3)
11732 {
11733 /* return empty list when there are no matches */
11734 if ((rettv->vval.v_list = list_alloc()) == NULL)
11735 goto theend;
11736 rettv->v_type = VAR_LIST;
11737 ++rettv->vval.v_list->lv_refcount;
11738 }
11739 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011741 rettv->v_type = VAR_STRING;
11742 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011745 if (argvars[0].v_type == VAR_LIST)
11746 {
11747 if ((l = argvars[0].vval.v_list) == NULL)
11748 goto theend;
11749 li = l->lv_first;
11750 }
11751 else
11752 expr = str = get_tv_string(&argvars[0]);
11753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011754 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11755 if (pat == NULL)
11756 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011757
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011758 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011760 int error = FALSE;
11761
11762 start = get_tv_number_chk(&argvars[2], &error);
11763 if (error)
11764 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011765 if (l != NULL)
11766 {
11767 li = list_find(l, start);
11768 if (li == NULL)
11769 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011770 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011771 }
11772 else
11773 {
11774 if (start < 0)
11775 start = 0;
11776 if (start > (long)STRLEN(str))
11777 goto theend;
11778 str += start;
11779 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011780
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011781 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011782 nth = get_tv_number_chk(&argvars[3], &error);
11783 if (error)
11784 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 }
11786
11787 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11788 if (regmatch.regprog != NULL)
11789 {
11790 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011791
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011792 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011793 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011794 if (l != NULL)
11795 {
11796 if (li == NULL)
11797 {
11798 match = FALSE;
11799 break;
11800 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011801 vim_free(tofree);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011802 str = echo_string(&li->li_tv, &tofree, strbuf,0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011803 if (str == NULL)
11804 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011805 }
11806
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011807 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011808
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011809 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011810 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011811 if (l == NULL && !match)
11812 break;
11813
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011814 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011815 if (l != NULL)
11816 {
11817 li = li->li_next;
11818 ++idx;
11819 }
11820 else
11821 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011822#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011823 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011824#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011825 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011826#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011827 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011828 }
11829
11830 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011832 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011833 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011834 int i;
11835
11836 /* return list with matched string and submatches */
11837 for (i = 0; i < NSUBEXP; ++i)
11838 {
11839 if (regmatch.endp[i] == NULL)
11840 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011841 if (list_append_string(rettv->vval.v_list,
11842 regmatch.startp[i],
11843 (int)(regmatch.endp[i] - regmatch.startp[i]))
11844 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011845 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011846 }
11847 }
11848 else if (type == 2)
11849 {
11850 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011851 if (l != NULL)
11852 copy_tv(&li->li_tv, rettv);
11853 else
11854 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011856 }
11857 else if (l != NULL)
11858 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 else
11860 {
11861 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011862 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 (varnumber_T)(regmatch.startp[0] - str);
11864 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011867 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 }
11869 }
11870 vim_free(regmatch.regprog);
11871 }
11872
11873theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011874 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875 p_cpo = save_cpo;
11876}
11877
11878/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011879 * "match()" function
11880 */
11881 static void
11882f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011883 typval_T *argvars;
11884 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011885{
11886 find_some_match(argvars, rettv, 1);
11887}
11888
11889/*
11890 * "matchend()" function
11891 */
11892 static void
11893f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011894 typval_T *argvars;
11895 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011896{
11897 find_some_match(argvars, rettv, 0);
11898}
11899
11900/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011901 * "matchlist()" function
11902 */
11903 static void
11904f_matchlist(argvars, rettv)
11905 typval_T *argvars;
11906 typval_T *rettv;
11907{
11908 find_some_match(argvars, rettv, 3);
11909}
11910
11911/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011912 * "matchstr()" function
11913 */
11914 static void
11915f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011916 typval_T *argvars;
11917 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011918{
11919 find_some_match(argvars, rettv, 2);
11920}
11921
Bram Moolenaar33570922005-01-25 22:26:29 +000011922static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011923
11924 static void
11925max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011926 typval_T *argvars;
11927 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011928 int domax;
11929{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011930 long n = 0;
11931 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011932 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011933
11934 if (argvars[0].v_type == VAR_LIST)
11935 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011936 list_T *l;
11937 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011938
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011939 l = argvars[0].vval.v_list;
11940 if (l != NULL)
11941 {
11942 li = l->lv_first;
11943 if (li != NULL)
11944 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011945 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011946 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011947 {
11948 li = li->li_next;
11949 if (li == NULL)
11950 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011951 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011952 if (domax ? i > n : i < n)
11953 n = i;
11954 }
11955 }
11956 }
11957 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011958 else if (argvars[0].v_type == VAR_DICT)
11959 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011960 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011961 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011962 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011963 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011964
11965 d = argvars[0].vval.v_dict;
11966 if (d != NULL)
11967 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011968 todo = d->dv_hashtab.ht_used;
11969 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011970 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011971 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011972 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011973 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011974 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011975 if (first)
11976 {
11977 n = i;
11978 first = FALSE;
11979 }
11980 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011981 n = i;
11982 }
11983 }
11984 }
11985 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011986 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011987 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011988 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011989}
11990
11991/*
11992 * "max()" function
11993 */
11994 static void
11995f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011996 typval_T *argvars;
11997 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011998{
11999 max_min(argvars, rettv, TRUE);
12000}
12001
12002/*
12003 * "min()" function
12004 */
12005 static void
12006f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012007 typval_T *argvars;
12008 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012009{
12010 max_min(argvars, rettv, FALSE);
12011}
12012
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012013static int mkdir_recurse __ARGS((char_u *dir, int prot));
12014
12015/*
12016 * Create the directory in which "dir" is located, and higher levels when
12017 * needed.
12018 */
12019 static int
12020mkdir_recurse(dir, prot)
12021 char_u *dir;
12022 int prot;
12023{
12024 char_u *p;
12025 char_u *updir;
12026 int r = FAIL;
12027
12028 /* Get end of directory name in "dir".
12029 * We're done when it's "/" or "c:/". */
12030 p = gettail_sep(dir);
12031 if (p <= get_past_head(dir))
12032 return OK;
12033
12034 /* If the directory exists we're done. Otherwise: create it.*/
12035 updir = vim_strnsave(dir, (int)(p - dir));
12036 if (updir == NULL)
12037 return FAIL;
12038 if (mch_isdir(updir))
12039 r = OK;
12040 else if (mkdir_recurse(updir, prot) == OK)
12041 r = vim_mkdir_emsg(updir, prot);
12042 vim_free(updir);
12043 return r;
12044}
12045
12046#ifdef vim_mkdir
12047/*
12048 * "mkdir()" function
12049 */
12050 static void
12051f_mkdir(argvars, rettv)
12052 typval_T *argvars;
12053 typval_T *rettv;
12054{
12055 char_u *dir;
12056 char_u buf[NUMBUFLEN];
12057 int prot = 0755;
12058
12059 rettv->vval.v_number = FAIL;
12060 if (check_restricted() || check_secure())
12061 return;
12062
12063 dir = get_tv_string_buf(&argvars[0], buf);
12064 if (argvars[1].v_type != VAR_UNKNOWN)
12065 {
12066 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012067 prot = get_tv_number_chk(&argvars[2], NULL);
12068 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012069 mkdir_recurse(dir, prot);
12070 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012071 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012072}
12073#endif
12074
Bram Moolenaar0d660222005-01-07 21:51:51 +000012075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 * "mode()" function
12077 */
12078/*ARGSUSED*/
12079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012080f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012081 typval_T *argvars;
12082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083{
12084 char_u buf[2];
12085
12086#ifdef FEAT_VISUAL
12087 if (VIsual_active)
12088 {
12089 if (VIsual_select)
12090 buf[0] = VIsual_mode + 's' - 'v';
12091 else
12092 buf[0] = VIsual_mode;
12093 }
12094 else
12095#endif
12096 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12097 buf[0] = 'r';
12098 else if (State & INSERT)
12099 {
12100 if (State & REPLACE_FLAG)
12101 buf[0] = 'R';
12102 else
12103 buf[0] = 'i';
12104 }
12105 else if (State & CMDLINE)
12106 buf[0] = 'c';
12107 else
12108 buf[0] = 'n';
12109
12110 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012111 rettv->vval.v_string = vim_strsave(buf);
12112 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113}
12114
12115/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012116 * "nextnonblank()" function
12117 */
12118 static void
12119f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012120 typval_T *argvars;
12121 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012122{
12123 linenr_T lnum;
12124
12125 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12126 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012127 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012128 {
12129 lnum = 0;
12130 break;
12131 }
12132 if (*skipwhite(ml_get(lnum)) != NUL)
12133 break;
12134 }
12135 rettv->vval.v_number = lnum;
12136}
12137
12138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139 * "nr2char()" function
12140 */
12141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012143 typval_T *argvars;
12144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145{
12146 char_u buf[NUMBUFLEN];
12147
12148#ifdef FEAT_MBYTE
12149 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 else
12152#endif
12153 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012154 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155 buf[1] = NUL;
12156 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012157 rettv->v_type = VAR_STRING;
12158 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012159}
12160
12161/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012162 * "prevnonblank()" function
12163 */
12164 static void
12165f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012166 typval_T *argvars;
12167 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012168{
12169 linenr_T lnum;
12170
12171 lnum = get_tv_lnum(argvars);
12172 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12173 lnum = 0;
12174 else
12175 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12176 --lnum;
12177 rettv->vval.v_number = lnum;
12178}
12179
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012180#ifdef HAVE_STDARG_H
12181/* This dummy va_list is here because:
12182 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12183 * - locally in the function results in a "used before set" warning
12184 * - using va_start() to initialize it gives "function with fixed args" error */
12185static va_list ap;
12186#endif
12187
Bram Moolenaar8c711452005-01-14 21:53:12 +000012188/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012189 * "printf()" function
12190 */
12191 static void
12192f_printf(argvars, rettv)
12193 typval_T *argvars;
12194 typval_T *rettv;
12195{
12196 rettv->v_type = VAR_STRING;
12197 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012198#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012199 {
12200 char_u buf[NUMBUFLEN];
12201 int len;
12202 char_u *s;
12203 int saved_did_emsg = did_emsg;
12204 char *fmt;
12205
12206 /* Get the required length, allocate the buffer and do it for real. */
12207 did_emsg = FALSE;
12208 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012209 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012210 if (!did_emsg)
12211 {
12212 s = alloc(len + 1);
12213 if (s != NULL)
12214 {
12215 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012216 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012217 }
12218 }
12219 did_emsg |= saved_did_emsg;
12220 }
12221#endif
12222}
12223
12224/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012225 * "pumvisible()" function
12226 */
12227/*ARGSUSED*/
12228 static void
12229f_pumvisible(argvars, rettv)
12230 typval_T *argvars;
12231 typval_T *rettv;
12232{
12233 rettv->vval.v_number = 0;
12234#ifdef FEAT_INS_EXPAND
12235 if (pum_visible())
12236 rettv->vval.v_number = 1;
12237#endif
12238}
12239
12240/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012241 * "range()" function
12242 */
12243 static void
12244f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012245 typval_T *argvars;
12246 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012247{
12248 long start;
12249 long end;
12250 long stride = 1;
12251 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012252 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012253 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012254
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012255 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012256 if (argvars[1].v_type == VAR_UNKNOWN)
12257 {
12258 end = start - 1;
12259 start = 0;
12260 }
12261 else
12262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012263 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012264 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012265 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012266 }
12267
12268 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012269 if (error)
12270 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012271 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012272 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012273 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012274 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012275 else
12276 {
12277 l = list_alloc();
12278 if (l != NULL)
12279 {
12280 rettv->v_type = VAR_LIST;
12281 rettv->vval.v_list = l;
12282 ++l->lv_refcount;
12283
12284 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012285 if (list_append_number(l, (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012286 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012287 }
12288 }
12289}
12290
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012291/*
12292 * "readfile()" function
12293 */
12294 static void
12295f_readfile(argvars, rettv)
12296 typval_T *argvars;
12297 typval_T *rettv;
12298{
12299 int binary = FALSE;
12300 char_u *fname;
12301 FILE *fd;
12302 list_T *l;
12303 listitem_T *li;
12304#define FREAD_SIZE 200 /* optimized for text lines */
12305 char_u buf[FREAD_SIZE];
12306 int readlen; /* size of last fread() */
12307 int buflen; /* nr of valid chars in buf[] */
12308 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12309 int tolist; /* first byte in buf[] still to be put in list */
12310 int chop; /* how many CR to chop off */
12311 char_u *prev = NULL; /* previously read bytes, if any */
12312 int prevlen = 0; /* length of "prev" if not NULL */
12313 char_u *s;
12314 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012315 long maxline = MAXLNUM;
12316 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012317
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012318 if (argvars[1].v_type != VAR_UNKNOWN)
12319 {
12320 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12321 binary = TRUE;
12322 if (argvars[2].v_type != VAR_UNKNOWN)
12323 maxline = get_tv_number(&argvars[2]);
12324 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012325
12326 l = list_alloc();
12327 if (l == NULL)
12328 return;
12329 rettv->v_type = VAR_LIST;
12330 rettv->vval.v_list = l;
12331 l->lv_refcount = 1;
12332
12333 /* Always open the file in binary mode, library functions have a mind of
12334 * their own about CR-LF conversion. */
12335 fname = get_tv_string(&argvars[0]);
12336 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12337 {
12338 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12339 return;
12340 }
12341
12342 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012343 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012344 {
12345 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12346 buflen = filtd + readlen;
12347 tolist = 0;
12348 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12349 {
12350 if (buf[filtd] == '\n' || readlen <= 0)
12351 {
12352 /* Only when in binary mode add an empty list item when the
12353 * last line ends in a '\n'. */
12354 if (!binary && readlen == 0 && filtd == 0)
12355 break;
12356
12357 /* Found end-of-line or end-of-file: add a text line to the
12358 * list. */
12359 chop = 0;
12360 if (!binary)
12361 while (filtd - chop - 1 >= tolist
12362 && buf[filtd - chop - 1] == '\r')
12363 ++chop;
12364 len = filtd - tolist - chop;
12365 if (prev == NULL)
12366 s = vim_strnsave(buf + tolist, len);
12367 else
12368 {
12369 s = alloc((unsigned)(prevlen + len + 1));
12370 if (s != NULL)
12371 {
12372 mch_memmove(s, prev, prevlen);
12373 vim_free(prev);
12374 prev = NULL;
12375 mch_memmove(s + prevlen, buf + tolist, len);
12376 s[prevlen + len] = NUL;
12377 }
12378 }
12379 tolist = filtd + 1;
12380
12381 li = listitem_alloc();
12382 if (li == NULL)
12383 {
12384 vim_free(s);
12385 break;
12386 }
12387 li->li_tv.v_type = VAR_STRING;
12388 li->li_tv.v_lock = 0;
12389 li->li_tv.vval.v_string = s;
12390 list_append(l, li);
12391
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012392 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012393 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012394 if (readlen <= 0)
12395 break;
12396 }
12397 else if (buf[filtd] == NUL)
12398 buf[filtd] = '\n';
12399 }
12400 if (readlen <= 0)
12401 break;
12402
12403 if (tolist == 0)
12404 {
12405 /* "buf" is full, need to move text to an allocated buffer */
12406 if (prev == NULL)
12407 {
12408 prev = vim_strnsave(buf, buflen);
12409 prevlen = buflen;
12410 }
12411 else
12412 {
12413 s = alloc((unsigned)(prevlen + buflen));
12414 if (s != NULL)
12415 {
12416 mch_memmove(s, prev, prevlen);
12417 mch_memmove(s + prevlen, buf, buflen);
12418 vim_free(prev);
12419 prev = s;
12420 prevlen += buflen;
12421 }
12422 }
12423 filtd = 0;
12424 }
12425 else
12426 {
12427 mch_memmove(buf, buf + tolist, buflen - tolist);
12428 filtd -= tolist;
12429 }
12430 }
12431
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012432 /*
12433 * For a negative line count use only the lines at the end of the file,
12434 * free the rest.
12435 */
12436 if (maxline < 0)
12437 while (cnt > -maxline)
12438 {
12439 listitem_remove(l, l->lv_first);
12440 --cnt;
12441 }
12442
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012443 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012444 fclose(fd);
12445}
12446
12447
Bram Moolenaar0d660222005-01-07 21:51:51 +000012448#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12449static void make_connection __ARGS((void));
12450static int check_connection __ARGS((void));
12451
12452 static void
12453make_connection()
12454{
12455 if (X_DISPLAY == NULL
12456# ifdef FEAT_GUI
12457 && !gui.in_use
12458# endif
12459 )
12460 {
12461 x_force_connect = TRUE;
12462 setup_term_clip();
12463 x_force_connect = FALSE;
12464 }
12465}
12466
12467 static int
12468check_connection()
12469{
12470 make_connection();
12471 if (X_DISPLAY == NULL)
12472 {
12473 EMSG(_("E240: No connection to Vim server"));
12474 return FAIL;
12475 }
12476 return OK;
12477}
12478#endif
12479
12480#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012481static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012482
12483 static void
12484remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012485 typval_T *argvars;
12486 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012487 int expr;
12488{
12489 char_u *server_name;
12490 char_u *keys;
12491 char_u *r = NULL;
12492 char_u buf[NUMBUFLEN];
12493# ifdef WIN32
12494 HWND w;
12495# else
12496 Window w;
12497# endif
12498
12499 if (check_restricted() || check_secure())
12500 return;
12501
12502# ifdef FEAT_X11
12503 if (check_connection() == FAIL)
12504 return;
12505# endif
12506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012507 server_name = get_tv_string_chk(&argvars[0]);
12508 if (server_name == NULL)
12509 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012510 keys = get_tv_string_buf(&argvars[1], buf);
12511# ifdef WIN32
12512 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12513# else
12514 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12515 < 0)
12516# endif
12517 {
12518 if (r != NULL)
12519 EMSG(r); /* sending worked but evaluation failed */
12520 else
12521 EMSG2(_("E241: Unable to send to %s"), server_name);
12522 return;
12523 }
12524
12525 rettv->vval.v_string = r;
12526
12527 if (argvars[2].v_type != VAR_UNKNOWN)
12528 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012529 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012530 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012531 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012532
12533 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012534 v.di_tv.v_type = VAR_STRING;
12535 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012536 idvar = get_tv_string_chk(&argvars[2]);
12537 if (idvar != NULL)
12538 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012539 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540 }
12541}
12542#endif
12543
12544/*
12545 * "remote_expr()" function
12546 */
12547/*ARGSUSED*/
12548 static void
12549f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012550 typval_T *argvars;
12551 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012552{
12553 rettv->v_type = VAR_STRING;
12554 rettv->vval.v_string = NULL;
12555#ifdef FEAT_CLIENTSERVER
12556 remote_common(argvars, rettv, TRUE);
12557#endif
12558}
12559
12560/*
12561 * "remote_foreground()" function
12562 */
12563/*ARGSUSED*/
12564 static void
12565f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012566 typval_T *argvars;
12567 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012568{
12569 rettv->vval.v_number = 0;
12570#ifdef FEAT_CLIENTSERVER
12571# ifdef WIN32
12572 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012573 {
12574 char_u *server_name = get_tv_string_chk(&argvars[0]);
12575
12576 if (server_name != NULL)
12577 serverForeground(server_name);
12578 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012579# else
12580 /* Send a foreground() expression to the server. */
12581 argvars[1].v_type = VAR_STRING;
12582 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12583 argvars[2].v_type = VAR_UNKNOWN;
12584 remote_common(argvars, rettv, TRUE);
12585 vim_free(argvars[1].vval.v_string);
12586# endif
12587#endif
12588}
12589
12590/*ARGSUSED*/
12591 static void
12592f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012593 typval_T *argvars;
12594 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012595{
12596#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012597 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012598 char_u *s = NULL;
12599# ifdef WIN32
12600 int n = 0;
12601# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012602 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012603
12604 if (check_restricted() || check_secure())
12605 {
12606 rettv->vval.v_number = -1;
12607 return;
12608 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012609 serverid = get_tv_string_chk(&argvars[0]);
12610 if (serverid == NULL)
12611 {
12612 rettv->vval.v_number = -1;
12613 return; /* type error; errmsg already given */
12614 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012615# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012616 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012617 if (n == 0)
12618 rettv->vval.v_number = -1;
12619 else
12620 {
12621 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12622 rettv->vval.v_number = (s != NULL);
12623 }
12624# else
12625 rettv->vval.v_number = 0;
12626 if (check_connection() == FAIL)
12627 return;
12628
12629 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012630 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012631# endif
12632
12633 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012635 char_u *retvar;
12636
Bram Moolenaar33570922005-01-25 22:26:29 +000012637 v.di_tv.v_type = VAR_STRING;
12638 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012639 retvar = get_tv_string_chk(&argvars[1]);
12640 if (retvar != NULL)
12641 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012642 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012643 }
12644#else
12645 rettv->vval.v_number = -1;
12646#endif
12647}
12648
12649/*ARGSUSED*/
12650 static void
12651f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012652 typval_T *argvars;
12653 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012654{
12655 char_u *r = NULL;
12656
12657#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012658 char_u *serverid = get_tv_string_chk(&argvars[0]);
12659
12660 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012661 {
12662# ifdef WIN32
12663 /* The server's HWND is encoded in the 'id' parameter */
12664 int n = 0;
12665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012666 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012667 if (n != 0)
12668 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12669 if (r == NULL)
12670# else
12671 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012672 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012673# endif
12674 EMSG(_("E277: Unable to read a server reply"));
12675 }
12676#endif
12677 rettv->v_type = VAR_STRING;
12678 rettv->vval.v_string = r;
12679}
12680
12681/*
12682 * "remote_send()" function
12683 */
12684/*ARGSUSED*/
12685 static void
12686f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012687 typval_T *argvars;
12688 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012689{
12690 rettv->v_type = VAR_STRING;
12691 rettv->vval.v_string = NULL;
12692#ifdef FEAT_CLIENTSERVER
12693 remote_common(argvars, rettv, FALSE);
12694#endif
12695}
12696
12697/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012698 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012699 */
12700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012701f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012702 typval_T *argvars;
12703 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012704{
Bram Moolenaar33570922005-01-25 22:26:29 +000012705 list_T *l;
12706 listitem_T *item, *item2;
12707 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012708 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012709 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012710 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012711 dict_T *d;
12712 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012713
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012714 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012715 if (argvars[0].v_type == VAR_DICT)
12716 {
12717 if (argvars[2].v_type != VAR_UNKNOWN)
12718 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012719 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012720 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012722 key = get_tv_string_chk(&argvars[1]);
12723 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012725 di = dict_find(d, key, -1);
12726 if (di == NULL)
12727 EMSG2(_(e_dictkey), key);
12728 else
12729 {
12730 *rettv = di->di_tv;
12731 init_tv(&di->di_tv);
12732 dictitem_remove(d, di);
12733 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012735 }
12736 }
12737 else if (argvars[0].v_type != VAR_LIST)
12738 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012739 else if ((l = argvars[0].vval.v_list) != NULL
12740 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012742 int error = FALSE;
12743
12744 idx = get_tv_number_chk(&argvars[1], &error);
12745 if (error)
12746 ; /* type error: do nothing, errmsg already given */
12747 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012748 EMSGN(_(e_listidx), idx);
12749 else
12750 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012751 if (argvars[2].v_type == VAR_UNKNOWN)
12752 {
12753 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012754 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012755 *rettv = item->li_tv;
12756 vim_free(item);
12757 }
12758 else
12759 {
12760 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012761 end = get_tv_number_chk(&argvars[2], &error);
12762 if (error)
12763 ; /* type error: do nothing */
12764 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012765 EMSGN(_(e_listidx), end);
12766 else
12767 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012768 int cnt = 0;
12769
12770 for (li = item; li != NULL; li = li->li_next)
12771 {
12772 ++cnt;
12773 if (li == item2)
12774 break;
12775 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012776 if (li == NULL) /* didn't find "item2" after "item" */
12777 EMSG(_(e_invrange));
12778 else
12779 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012780 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012781 l = list_alloc();
12782 if (l != NULL)
12783 {
12784 rettv->v_type = VAR_LIST;
12785 rettv->vval.v_list = l;
12786 l->lv_first = item;
12787 l->lv_last = item2;
12788 l->lv_refcount = 1;
12789 item->li_prev = NULL;
12790 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012791 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012792 }
12793 }
12794 }
12795 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012796 }
12797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798}
12799
12800/*
12801 * "rename({from}, {to})" function
12802 */
12803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012804f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012805 typval_T *argvars;
12806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807{
12808 char_u buf[NUMBUFLEN];
12809
12810 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012811 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012813 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12814 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815}
12816
12817/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012818 * "repeat()" function
12819 */
12820/*ARGSUSED*/
12821 static void
12822f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012823 typval_T *argvars;
12824 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012825{
12826 char_u *p;
12827 int n;
12828 int slen;
12829 int len;
12830 char_u *r;
12831 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012832 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012833
12834 n = get_tv_number(&argvars[1]);
12835 if (argvars[0].v_type == VAR_LIST)
12836 {
12837 l = list_alloc();
12838 if (l != NULL && argvars[0].vval.v_list != NULL)
12839 {
12840 l->lv_refcount = 1;
12841 while (n-- > 0)
12842 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12843 break;
12844 }
12845 rettv->v_type = VAR_LIST;
12846 rettv->vval.v_list = l;
12847 }
12848 else
12849 {
12850 p = get_tv_string(&argvars[0]);
12851 rettv->v_type = VAR_STRING;
12852 rettv->vval.v_string = NULL;
12853
12854 slen = (int)STRLEN(p);
12855 len = slen * n;
12856 if (len <= 0)
12857 return;
12858
12859 r = alloc(len + 1);
12860 if (r != NULL)
12861 {
12862 for (i = 0; i < n; i++)
12863 mch_memmove(r + i * slen, p, (size_t)slen);
12864 r[len] = NUL;
12865 }
12866
12867 rettv->vval.v_string = r;
12868 }
12869}
12870
12871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 * "resolve()" function
12873 */
12874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012876 typval_T *argvars;
12877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878{
12879 char_u *p;
12880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882#ifdef FEAT_SHORTCUT
12883 {
12884 char_u *v = NULL;
12885
12886 v = mch_resolve_shortcut(p);
12887 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012890 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891 }
12892#else
12893# ifdef HAVE_READLINK
12894 {
12895 char_u buf[MAXPATHL + 1];
12896 char_u *cpy;
12897 int len;
12898 char_u *remain = NULL;
12899 char_u *q;
12900 int is_relative_to_current = FALSE;
12901 int has_trailing_pathsep = FALSE;
12902 int limit = 100;
12903
12904 p = vim_strsave(p);
12905
12906 if (p[0] == '.' && (vim_ispathsep(p[1])
12907 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12908 is_relative_to_current = TRUE;
12909
12910 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012911 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912 has_trailing_pathsep = TRUE;
12913
12914 q = getnextcomp(p);
12915 if (*q != NUL)
12916 {
12917 /* Separate the first path component in "p", and keep the
12918 * remainder (beginning with the path separator). */
12919 remain = vim_strsave(q - 1);
12920 q[-1] = NUL;
12921 }
12922
12923 for (;;)
12924 {
12925 for (;;)
12926 {
12927 len = readlink((char *)p, (char *)buf, MAXPATHL);
12928 if (len <= 0)
12929 break;
12930 buf[len] = NUL;
12931
12932 if (limit-- == 0)
12933 {
12934 vim_free(p);
12935 vim_free(remain);
12936 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012937 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938 goto fail;
12939 }
12940
12941 /* Ensure that the result will have a trailing path separator
12942 * if the argument has one. */
12943 if (remain == NULL && has_trailing_pathsep)
12944 add_pathsep(buf);
12945
12946 /* Separate the first path component in the link value and
12947 * concatenate the remainders. */
12948 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12949 if (*q != NUL)
12950 {
12951 if (remain == NULL)
12952 remain = vim_strsave(q - 1);
12953 else
12954 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000012955 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956 if (cpy != NULL)
12957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 vim_free(remain);
12959 remain = cpy;
12960 }
12961 }
12962 q[-1] = NUL;
12963 }
12964
12965 q = gettail(p);
12966 if (q > p && *q == NUL)
12967 {
12968 /* Ignore trailing path separator. */
12969 q[-1] = NUL;
12970 q = gettail(p);
12971 }
12972 if (q > p && !mch_isFullName(buf))
12973 {
12974 /* symlink is relative to directory of argument */
12975 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12976 if (cpy != NULL)
12977 {
12978 STRCPY(cpy, p);
12979 STRCPY(gettail(cpy), buf);
12980 vim_free(p);
12981 p = cpy;
12982 }
12983 }
12984 else
12985 {
12986 vim_free(p);
12987 p = vim_strsave(buf);
12988 }
12989 }
12990
12991 if (remain == NULL)
12992 break;
12993
12994 /* Append the first path component of "remain" to "p". */
12995 q = getnextcomp(remain + 1);
12996 len = q - remain - (*q != NUL);
12997 cpy = vim_strnsave(p, STRLEN(p) + len);
12998 if (cpy != NULL)
12999 {
13000 STRNCAT(cpy, remain, len);
13001 vim_free(p);
13002 p = cpy;
13003 }
13004 /* Shorten "remain". */
13005 if (*q != NUL)
13006 STRCPY(remain, q - 1);
13007 else
13008 {
13009 vim_free(remain);
13010 remain = NULL;
13011 }
13012 }
13013
13014 /* If the result is a relative path name, make it explicitly relative to
13015 * the current directory if and only if the argument had this form. */
13016 if (!vim_ispathsep(*p))
13017 {
13018 if (is_relative_to_current
13019 && *p != NUL
13020 && !(p[0] == '.'
13021 && (p[1] == NUL
13022 || vim_ispathsep(p[1])
13023 || (p[1] == '.'
13024 && (p[2] == NUL
13025 || vim_ispathsep(p[2]))))))
13026 {
13027 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013028 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 if (cpy != NULL)
13030 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 vim_free(p);
13032 p = cpy;
13033 }
13034 }
13035 else if (!is_relative_to_current)
13036 {
13037 /* Strip leading "./". */
13038 q = p;
13039 while (q[0] == '.' && vim_ispathsep(q[1]))
13040 q += 2;
13041 if (q > p)
13042 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13043 }
13044 }
13045
13046 /* Ensure that the result will have no trailing path separator
13047 * if the argument had none. But keep "/" or "//". */
13048 if (!has_trailing_pathsep)
13049 {
13050 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013051 if (after_pathsep(p, q))
13052 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053 }
13054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013055 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056 }
13057# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013058 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059# endif
13060#endif
13061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013062 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063
13064#ifdef HAVE_READLINK
13065fail:
13066#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013067 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068}
13069
13070/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013071 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072 */
13073 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013074f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013075 typval_T *argvars;
13076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077{
Bram Moolenaar33570922005-01-25 22:26:29 +000013078 list_T *l;
13079 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080
Bram Moolenaar0d660222005-01-07 21:51:51 +000013081 rettv->vval.v_number = 0;
13082 if (argvars[0].v_type != VAR_LIST)
13083 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013084 else if ((l = argvars[0].vval.v_list) != NULL
13085 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013086 {
13087 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013088 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013089 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013090 while (li != NULL)
13091 {
13092 ni = li->li_prev;
13093 list_append(l, li);
13094 li = ni;
13095 }
13096 rettv->vval.v_list = l;
13097 rettv->v_type = VAR_LIST;
13098 ++l->lv_refcount;
13099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013100}
13101
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013102#define SP_NOMOVE 1 /* don't move cursor */
13103#define SP_REPEAT 2 /* repeat to find outer pair */
13104#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000013105#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013106
Bram Moolenaar33570922005-01-25 22:26:29 +000013107static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013108
13109/*
13110 * Get flags for a search function.
13111 * Possibly sets "p_ws".
13112 * Returns BACKWARD, FORWARD or zero (for an error).
13113 */
13114 static int
13115get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013116 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013117 int *flagsp;
13118{
13119 int dir = FORWARD;
13120 char_u *flags;
13121 char_u nbuf[NUMBUFLEN];
13122 int mask;
13123
13124 if (varp->v_type != VAR_UNKNOWN)
13125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013126 flags = get_tv_string_buf_chk(varp, nbuf);
13127 if (flags == NULL)
13128 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013129 while (*flags != NUL)
13130 {
13131 switch (*flags)
13132 {
13133 case 'b': dir = BACKWARD; break;
13134 case 'w': p_ws = TRUE; break;
13135 case 'W': p_ws = FALSE; break;
13136 default: mask = 0;
13137 if (flagsp != NULL)
13138 switch (*flags)
13139 {
13140 case 'n': mask = SP_NOMOVE; break;
13141 case 'r': mask = SP_REPEAT; break;
13142 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013143 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013144 }
13145 if (mask == 0)
13146 {
13147 EMSG2(_(e_invarg2), flags);
13148 dir = 0;
13149 }
13150 else
13151 *flagsp |= mask;
13152 }
13153 if (dir == 0)
13154 break;
13155 ++flags;
13156 }
13157 }
13158 return dir;
13159}
13160
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013162 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013164 static int
13165search_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013166 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013167 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168{
13169 char_u *pat;
13170 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013171 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172 int save_p_ws = p_ws;
13173 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013174 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013175 int retval = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013177 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013178 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13179 if (dir == 0)
13180 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013181 /*
13182 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13183 * Check to make sure only those flags are set.
13184 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13185 * flags cannot be set. Check for that condition also.
13186 */
13187 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13188 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013190 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013191 goto theend;
13192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013194 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13196 SEARCH_KEEP, RE_SEARCH) != FAIL)
13197 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013198 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013199 if (flags & SP_SETPCMARK)
13200 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013202 if (match_pos != NULL)
13203 {
13204 /* Store the match cursor position */
13205 match_pos->lnum = pos.lnum;
13206 match_pos->col = pos.col + 1;
13207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208 /* "/$" will put the cursor after the end of the line, may need to
13209 * correct that here */
13210 check_cursor();
13211 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013212
13213 /* If 'n' flag is used: restore cursor position. */
13214 if (flags & SP_NOMOVE)
13215 curwin->w_cursor = save_cursor;
13216theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013218
13219 return retval;
13220}
13221
13222/*
13223 * "search()" function
13224 */
13225 static void
13226f_search(argvars, rettv)
13227 typval_T *argvars;
13228 typval_T *rettv;
13229{
13230 rettv->vval.v_number = search_cmn(argvars, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231}
13232
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013234 * "searchdecl()" function
13235 */
13236 static void
13237f_searchdecl(argvars, rettv)
13238 typval_T *argvars;
13239 typval_T *rettv;
13240{
13241 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013242 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013243 int error = FALSE;
13244 char_u *name;
13245
13246 rettv->vval.v_number = 1; /* default: FAIL */
13247
13248 name = get_tv_string_chk(&argvars[0]);
13249 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013250 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013251 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013252 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13253 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13254 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013255 if (!error && name != NULL)
13256 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013257 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013258}
13259
13260/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013261 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013263 static int
13264searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013265 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013266 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267{
13268 char_u *spat, *mpat, *epat;
13269 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271 int dir;
13272 int flags = 0;
13273 char_u nbuf1[NUMBUFLEN];
13274 char_u nbuf2[NUMBUFLEN];
13275 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013276 int retval = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013279 spat = get_tv_string_chk(&argvars[0]);
13280 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13281 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13282 if (spat == NULL || mpat == NULL || epat == NULL)
13283 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285 /* Handle the optional fourth argument: flags */
13286 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013287 if (dir == 0)
13288 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013289 /*
13290 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13291 */
13292 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13293 {
13294 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13295 goto theend;
13296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013298 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013299 if (argvars[3].v_type == VAR_UNKNOWN
13300 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301 skip = (char_u *)"";
13302 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013303 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13304 if (skip == NULL)
13305 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013307 retval = do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013308
13309theend:
13310 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013311
13312 return retval;
13313}
13314
13315/*
13316 * "searchpair()" function
13317 */
13318 static void
13319f_searchpair(argvars, rettv)
13320 typval_T *argvars;
13321 typval_T *rettv;
13322{
13323 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13324}
13325
13326/*
13327 * "searchpairpos()" function
13328 */
13329 static void
13330f_searchpairpos(argvars, rettv)
13331 typval_T *argvars;
13332 typval_T *rettv;
13333{
13334 list_T *l;
13335 pos_T match_pos;
13336 int lnum = 0;
13337 int col = 0;
13338
13339 rettv->vval.v_number = 0;
13340
13341 l = list_alloc();
13342 if (l == NULL)
13343 return;
13344 rettv->v_type = VAR_LIST;
13345 rettv->vval.v_list = l;
13346 ++l->lv_refcount;
13347
13348 if (searchpair_cmn(argvars, &match_pos) > 0)
13349 {
13350 lnum = match_pos.lnum;
13351 col = match_pos.col;
13352 }
13353
13354 list_append_number(l, (varnumber_T)lnum);
13355 list_append_number(l, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013356}
13357
13358/*
13359 * Search for a start/middle/end thing.
13360 * Used by searchpair(), see its documentation for the details.
13361 * Returns 0 or -1 for no match,
13362 */
13363 long
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013364do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013365 char_u *spat; /* start pattern */
13366 char_u *mpat; /* middle pattern */
13367 char_u *epat; /* end pattern */
13368 int dir; /* BACKWARD or FORWARD */
13369 char_u *skip; /* skip expression */
13370 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013371 pos_T *match_pos;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013372{
13373 char_u *save_cpo;
13374 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13375 long retval = 0;
13376 pos_T pos;
13377 pos_T firstpos;
13378 pos_T foundpos;
13379 pos_T save_cursor;
13380 pos_T save_pos;
13381 int n;
13382 int r;
13383 int nest = 1;
13384 int err;
13385
13386 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13387 save_cpo = p_cpo;
13388 p_cpo = (char_u *)"";
13389
13390 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13391 * start/middle/end (pat3, for the top pair). */
13392 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13393 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13394 if (pat2 == NULL || pat3 == NULL)
13395 goto theend;
13396 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13397 if (*mpat == NUL)
13398 STRCPY(pat3, pat2);
13399 else
13400 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13401 spat, epat, mpat);
13402
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 save_cursor = curwin->w_cursor;
13404 pos = curwin->w_cursor;
13405 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013406 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407 pat = pat3;
13408 for (;;)
13409 {
13410 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13411 SEARCH_KEEP, RE_SEARCH);
13412 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13413 /* didn't find it or found the first match again: FAIL */
13414 break;
13415
13416 if (firstpos.lnum == 0)
13417 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013418 if (equalpos(pos, foundpos))
13419 {
13420 /* Found the same position again. Can happen with a pattern that
13421 * has "\zs" at the end and searching backwards. Advance one
13422 * character and try again. */
13423 if (dir == BACKWARD)
13424 decl(&pos);
13425 else
13426 incl(&pos);
13427 }
13428 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429
13430 /* If the skip pattern matches, ignore this match. */
13431 if (*skip != NUL)
13432 {
13433 save_pos = curwin->w_cursor;
13434 curwin->w_cursor = pos;
13435 r = eval_to_bool(skip, &err, NULL, FALSE);
13436 curwin->w_cursor = save_pos;
13437 if (err)
13438 {
13439 /* Evaluating {skip} caused an error, break here. */
13440 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013441 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 break;
13443 }
13444 if (r)
13445 continue;
13446 }
13447
13448 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13449 {
13450 /* Found end when searching backwards or start when searching
13451 * forward: nested pair. */
13452 ++nest;
13453 pat = pat2; /* nested, don't search for middle */
13454 }
13455 else
13456 {
13457 /* Found end when searching forward or start when searching
13458 * backward: end of (nested) pair; or found middle in outer pair. */
13459 if (--nest == 1)
13460 pat = pat3; /* outer level, search for middle */
13461 }
13462
13463 if (nest == 0)
13464 {
13465 /* Found the match: return matchcount or line number. */
13466 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013467 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013469 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013470 if (flags & SP_SETPCMARK)
13471 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472 curwin->w_cursor = pos;
13473 if (!(flags & SP_REPEAT))
13474 break;
13475 nest = 1; /* search for next unmatched */
13476 }
13477 }
13478
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013479 if (match_pos != NULL)
13480 {
13481 /* Store the match cursor position */
13482 match_pos->lnum = curwin->w_cursor.lnum;
13483 match_pos->col = curwin->w_cursor.col + 1;
13484 }
13485
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013487 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 curwin->w_cursor = save_cursor;
13489
13490theend:
13491 vim_free(pat2);
13492 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013494
13495 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496}
13497
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013498/*
13499 * "searchpos()" function
13500 */
13501 static void
13502f_searchpos(argvars, rettv)
13503 typval_T *argvars;
13504 typval_T *rettv;
13505{
13506 list_T *l;
13507 pos_T match_pos;
13508 int lnum = 0;
13509 int col = 0;
13510
13511 rettv->vval.v_number = 0;
13512
13513 l = list_alloc();
13514 if (l == NULL)
13515 return;
13516 rettv->v_type = VAR_LIST;
13517 rettv->vval.v_list = l;
13518 ++l->lv_refcount;
13519
13520 if (search_cmn(argvars, &match_pos) > 0)
13521 {
13522 lnum = match_pos.lnum;
13523 col = match_pos.col;
13524 }
13525
13526 list_append_number(l, (varnumber_T)lnum);
13527 list_append_number(l, (varnumber_T)col);
13528
13529}
13530
13531
Bram Moolenaar0d660222005-01-07 21:51:51 +000013532/*ARGSUSED*/
13533 static void
13534f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013535 typval_T *argvars;
13536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013538#ifdef FEAT_CLIENTSERVER
13539 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013540 char_u *server = get_tv_string_chk(&argvars[0]);
13541 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542
Bram Moolenaar0d660222005-01-07 21:51:51 +000013543 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013544 if (server == NULL || reply == NULL)
13545 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013546 if (check_restricted() || check_secure())
13547 return;
13548# ifdef FEAT_X11
13549 if (check_connection() == FAIL)
13550 return;
13551# endif
13552
13553 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013555 EMSG(_("E258: Unable to send to client"));
13556 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013558 rettv->vval.v_number = 0;
13559#else
13560 rettv->vval.v_number = -1;
13561#endif
13562}
13563
13564/*ARGSUSED*/
13565 static void
13566f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013567 typval_T *argvars;
13568 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013569{
13570 char_u *r = NULL;
13571
13572#ifdef FEAT_CLIENTSERVER
13573# ifdef WIN32
13574 r = serverGetVimNames();
13575# else
13576 make_connection();
13577 if (X_DISPLAY != NULL)
13578 r = serverGetVimNames(X_DISPLAY);
13579# endif
13580#endif
13581 rettv->v_type = VAR_STRING;
13582 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583}
13584
13585/*
13586 * "setbufvar()" function
13587 */
13588/*ARGSUSED*/
13589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013590f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013591 typval_T *argvars;
13592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593{
13594 buf_T *buf;
13595#ifdef FEAT_AUTOCMD
13596 aco_save_T aco;
13597#else
13598 buf_T *save_curbuf;
13599#endif
13600 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013601 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602 char_u nbuf[NUMBUFLEN];
13603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013604 rettv->vval.v_number = 0;
13605
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606 if (check_restricted() || check_secure())
13607 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013608 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13609 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013610 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611 varp = &argvars[2];
13612
13613 if (buf != NULL && varname != NULL && varp != NULL)
13614 {
13615 /* set curbuf to be our buf, temporarily */
13616#ifdef FEAT_AUTOCMD
13617 aucmd_prepbuf(&aco, buf);
13618#else
13619 save_curbuf = curbuf;
13620 curbuf = buf;
13621#endif
13622
13623 if (*varname == '&')
13624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013625 long numval;
13626 char_u *strval;
13627 int error = FALSE;
13628
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013630 numval = get_tv_number_chk(varp, &error);
13631 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013632 if (!error && strval != NULL)
13633 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 }
13635 else
13636 {
13637 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13638 if (bufvarname != NULL)
13639 {
13640 STRCPY(bufvarname, "b:");
13641 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013642 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 vim_free(bufvarname);
13644 }
13645 }
13646
13647 /* reset notion of buffer */
13648#ifdef FEAT_AUTOCMD
13649 aucmd_restbuf(&aco);
13650#else
13651 curbuf = save_curbuf;
13652#endif
13653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654}
13655
13656/*
13657 * "setcmdpos()" function
13658 */
13659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013660f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013661 typval_T *argvars;
13662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013664 int pos = (int)get_tv_number(&argvars[0]) - 1;
13665
13666 if (pos >= 0)
13667 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668}
13669
13670/*
13671 * "setline()" function
13672 */
13673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013675 typval_T *argvars;
13676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677{
13678 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013679 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013680 list_T *l = NULL;
13681 listitem_T *li = NULL;
13682 long added = 0;
13683 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013685 lnum = get_tv_lnum(&argvars[0]);
13686 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013688 l = argvars[1].vval.v_list;
13689 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013691 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013692 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013693
13694 rettv->vval.v_number = 0; /* OK */
13695 for (;;)
13696 {
13697 if (l != NULL)
13698 {
13699 /* list argument, get next string */
13700 if (li == NULL)
13701 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013702 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013703 li = li->li_next;
13704 }
13705
13706 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013707 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013708 break;
13709 if (lnum <= curbuf->b_ml.ml_line_count)
13710 {
13711 /* existing line, replace it */
13712 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13713 {
13714 changed_bytes(lnum, 0);
13715 check_cursor_col();
13716 rettv->vval.v_number = 0; /* OK */
13717 }
13718 }
13719 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13720 {
13721 /* lnum is one past the last line, append the line */
13722 ++added;
13723 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13724 rettv->vval.v_number = 0; /* OK */
13725 }
13726
13727 if (l == NULL) /* only one string argument */
13728 break;
13729 ++lnum;
13730 }
13731
13732 if (added > 0)
13733 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734}
13735
13736/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013737 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013738 */
13739/*ARGSUSED*/
13740 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013741set_qf_ll_list(wp, list_arg, action_arg, rettv)
13742 win_T *wp;
13743 typval_T *list_arg;
13744 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013745 typval_T *rettv;
13746{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013747#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013748 char_u *act;
13749 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013750#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013751
Bram Moolenaar2641f772005-03-25 21:58:17 +000013752 rettv->vval.v_number = -1;
13753
13754#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013755 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013756 EMSG(_(e_listreq));
13757 else
13758 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013759 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013760
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013761 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013762 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013763 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013764 if (act == NULL)
13765 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013766 if (*act == 'a' || *act == 'r')
13767 action = *act;
13768 }
13769
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013770 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013771 rettv->vval.v_number = 0;
13772 }
13773#endif
13774}
13775
13776/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013777 * "setloclist()" function
13778 */
13779/*ARGSUSED*/
13780 static void
13781f_setloclist(argvars, rettv)
13782 typval_T *argvars;
13783 typval_T *rettv;
13784{
13785 win_T *win;
13786
13787 rettv->vval.v_number = -1;
13788
13789 win = find_win_by_nr(&argvars[0]);
13790 if (win != NULL)
13791 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13792}
13793
13794/*
13795 * "setqflist()" function
13796 */
13797/*ARGSUSED*/
13798 static void
13799f_setqflist(argvars, rettv)
13800 typval_T *argvars;
13801 typval_T *rettv;
13802{
13803 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
13804}
13805
13806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 * "setreg()" function
13808 */
13809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013810f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013811 typval_T *argvars;
13812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813{
13814 int regname;
13815 char_u *strregname;
13816 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013817 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 int append;
13819 char_u yank_type;
13820 long block_len;
13821
13822 block_len = -1;
13823 yank_type = MAUTO;
13824 append = FALSE;
13825
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013826 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013827 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013829 if (strregname == NULL)
13830 return; /* type error; errmsg already given */
13831 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 if (regname == 0 || regname == '@')
13833 regname = '"';
13834 else if (regname == '=')
13835 return;
13836
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013837 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013839 stropt = get_tv_string_chk(&argvars[2]);
13840 if (stropt == NULL)
13841 return; /* type error */
13842 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 switch (*stropt)
13844 {
13845 case 'a': case 'A': /* append */
13846 append = TRUE;
13847 break;
13848 case 'v': case 'c': /* character-wise selection */
13849 yank_type = MCHAR;
13850 break;
13851 case 'V': case 'l': /* line-wise selection */
13852 yank_type = MLINE;
13853 break;
13854#ifdef FEAT_VISUAL
13855 case 'b': case Ctrl_V: /* block-wise selection */
13856 yank_type = MBLOCK;
13857 if (VIM_ISDIGIT(stropt[1]))
13858 {
13859 ++stropt;
13860 block_len = getdigits(&stropt) - 1;
13861 --stropt;
13862 }
13863 break;
13864#endif
13865 }
13866 }
13867
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013868 strval = get_tv_string_chk(&argvars[1]);
13869 if (strval != NULL)
13870 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013872 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873}
13874
13875
13876/*
13877 * "setwinvar(expr)" function
13878 */
13879/*ARGSUSED*/
13880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013881f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013882 typval_T *argvars;
13883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884{
13885 win_T *win;
13886#ifdef FEAT_WINDOWS
13887 win_T *save_curwin;
13888#endif
13889 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013890 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 char_u nbuf[NUMBUFLEN];
13892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013893 rettv->vval.v_number = 0;
13894
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895 if (check_restricted() || check_secure())
13896 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013898 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 varp = &argvars[2];
13900
13901 if (win != NULL && varname != NULL && varp != NULL)
13902 {
13903#ifdef FEAT_WINDOWS
13904 /* set curwin to be our win, temporarily */
13905 save_curwin = curwin;
13906 curwin = win;
13907 curbuf = curwin->w_buffer;
13908#endif
13909
13910 if (*varname == '&')
13911 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013912 long numval;
13913 char_u *strval;
13914 int error = FALSE;
13915
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013917 numval = get_tv_number_chk(varp, &error);
13918 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013919 if (!error && strval != NULL)
13920 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 }
13922 else
13923 {
13924 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13925 if (winvarname != NULL)
13926 {
13927 STRCPY(winvarname, "w:");
13928 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013929 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 vim_free(winvarname);
13931 }
13932 }
13933
13934#ifdef FEAT_WINDOWS
13935 /* Restore current window, if it's still valid (autocomands can make
13936 * it invalid). */
13937 if (win_valid(save_curwin))
13938 {
13939 curwin = save_curwin;
13940 curbuf = curwin->w_buffer;
13941 }
13942#endif
13943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944}
13945
13946/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013947 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 */
13949 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013950f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013951 typval_T *argvars;
13952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013954 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955
Bram Moolenaar0d660222005-01-07 21:51:51 +000013956 p = get_tv_string(&argvars[0]);
13957 rettv->vval.v_string = vim_strsave(p);
13958 simplify_filename(rettv->vval.v_string); /* simplify in place */
13959 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960}
13961
Bram Moolenaar0d660222005-01-07 21:51:51 +000013962static int
13963#ifdef __BORLANDC__
13964 _RTLENTRYF
13965#endif
13966 item_compare __ARGS((const void *s1, const void *s2));
13967static int
13968#ifdef __BORLANDC__
13969 _RTLENTRYF
13970#endif
13971 item_compare2 __ARGS((const void *s1, const void *s2));
13972
13973static int item_compare_ic;
13974static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013976#define ITEM_COMPARE_FAIL 999
13977
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013979 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013981 static int
13982#ifdef __BORLANDC__
13983_RTLENTRYF
13984#endif
13985item_compare(s1, s2)
13986 const void *s1;
13987 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013989 char_u *p1, *p2;
13990 char_u *tofree1, *tofree2;
13991 int res;
13992 char_u numbuf1[NUMBUFLEN];
13993 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013995 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
13996 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013997 if (item_compare_ic)
13998 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014000 res = STRCMP(p1, p2);
14001 vim_free(tofree1);
14002 vim_free(tofree2);
14003 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004}
14005
14006 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014007#ifdef __BORLANDC__
14008_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014010item_compare2(s1, s2)
14011 const void *s1;
14012 const void *s2;
14013{
14014 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014015 typval_T rettv;
14016 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014017 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014019 /* shortcut after failure in previous call; compare all items equal */
14020 if (item_compare_func_err)
14021 return 0;
14022
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014023 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14024 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014025 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14026 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014027
14028 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14029 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014030 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014031 clear_tv(&argv[0]);
14032 clear_tv(&argv[1]);
14033
14034 if (res == FAIL)
14035 res = ITEM_COMPARE_FAIL;
14036 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014037 /* return value has wrong type */
14038 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14039 if (item_compare_func_err)
14040 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014041 clear_tv(&rettv);
14042 return res;
14043}
14044
14045/*
14046 * "sort({list})" function
14047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014049f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014050 typval_T *argvars;
14051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052{
Bram Moolenaar33570922005-01-25 22:26:29 +000014053 list_T *l;
14054 listitem_T *li;
14055 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014056 long len;
14057 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058
Bram Moolenaar0d660222005-01-07 21:51:51 +000014059 rettv->vval.v_number = 0;
14060 if (argvars[0].v_type != VAR_LIST)
14061 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062 else
14063 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014064 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014065 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014066 return;
14067 rettv->vval.v_list = l;
14068 rettv->v_type = VAR_LIST;
14069 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070
Bram Moolenaar0d660222005-01-07 21:51:51 +000014071 len = list_len(l);
14072 if (len <= 1)
14073 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074
Bram Moolenaar0d660222005-01-07 21:51:51 +000014075 item_compare_ic = FALSE;
14076 item_compare_func = NULL;
14077 if (argvars[1].v_type != VAR_UNKNOWN)
14078 {
14079 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014080 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014081 else
14082 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014083 int error = FALSE;
14084
14085 i = get_tv_number_chk(&argvars[1], &error);
14086 if (error)
14087 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014088 if (i == 1)
14089 item_compare_ic = TRUE;
14090 else
14091 item_compare_func = get_tv_string(&argvars[1]);
14092 }
14093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094
Bram Moolenaar0d660222005-01-07 21:51:51 +000014095 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014096 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014097 if (ptrs == NULL)
14098 return;
14099 i = 0;
14100 for (li = l->lv_first; li != NULL; li = li->li_next)
14101 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014103 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014104 /* test the compare function */
14105 if (item_compare_func != NULL
14106 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14107 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014108 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014110 {
14111 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014112 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014113 item_compare_func == NULL ? item_compare : item_compare2);
14114
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014115 if (!item_compare_func_err)
14116 {
14117 /* Clear the List and append the items in the sorted order. */
14118 l->lv_first = l->lv_last = NULL;
14119 l->lv_len = 0;
14120 for (i = 0; i < len; ++i)
14121 list_append(l, ptrs[i]);
14122 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014123 }
14124
14125 vim_free(ptrs);
14126 }
14127}
14128
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014129/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014130 * "soundfold({word})" function
14131 */
14132 static void
14133f_soundfold(argvars, rettv)
14134 typval_T *argvars;
14135 typval_T *rettv;
14136{
14137 char_u *s;
14138
14139 rettv->v_type = VAR_STRING;
14140 s = get_tv_string(&argvars[0]);
14141#ifdef FEAT_SYN_HL
14142 rettv->vval.v_string = eval_soundfold(s);
14143#else
14144 rettv->vval.v_string = vim_strsave(s);
14145#endif
14146}
14147
14148/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014149 * "spellbadword()" function
14150 */
14151/* ARGSUSED */
14152 static void
14153f_spellbadword(argvars, rettv)
14154 typval_T *argvars;
14155 typval_T *rettv;
14156{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014157 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014158 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014159 int len = 0;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014160 list_T *l;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014161
Bram Moolenaar4463f292005-09-25 22:20:24 +000014162 l = list_alloc();
14163 if (l == NULL)
14164 return;
14165 rettv->v_type = VAR_LIST;
14166 rettv->vval.v_list = l;
14167 ++l->lv_refcount;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014168
14169#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014170 if (argvars[0].v_type == VAR_UNKNOWN)
14171 {
14172 /* Find the start and length of the badly spelled word. */
14173 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14174 if (len != 0)
14175 word = ml_get_cursor();
14176 }
14177 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14178 {
14179 char_u *str = get_tv_string_chk(&argvars[0]);
14180 int capcol = -1;
14181
14182 if (str != NULL)
14183 {
14184 /* Check the argument for spelling. */
14185 while (*str != NUL)
14186 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014187 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014188 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014189 {
14190 word = str;
14191 break;
14192 }
14193 str += len;
14194 }
14195 }
14196 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014197#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014198
14199 list_append_string(l, word, len);
14200 list_append_string(l, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014201 attr == HLF_SPB ? "bad" :
14202 attr == HLF_SPR ? "rare" :
14203 attr == HLF_SPL ? "local" :
14204 attr == HLF_SPC ? "caps" :
14205 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014206}
14207
14208/*
14209 * "spellsuggest()" function
14210 */
14211 static void
14212f_spellsuggest(argvars, rettv)
14213 typval_T *argvars;
14214 typval_T *rettv;
14215{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014216 list_T *l;
14217#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014218 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014219 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014220 int maxcount;
14221 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014222 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014223 listitem_T *li;
14224 int need_capital = FALSE;
14225#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014226
14227 l = list_alloc();
14228 if (l == NULL)
14229 return;
14230 rettv->v_type = VAR_LIST;
14231 rettv->vval.v_list = l;
14232 ++l->lv_refcount;
14233
14234#ifdef FEAT_SYN_HL
14235 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14236 {
14237 str = get_tv_string(&argvars[0]);
14238 if (argvars[1].v_type != VAR_UNKNOWN)
14239 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014240 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014241 if (maxcount <= 0)
14242 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014243 if (argvars[2].v_type != VAR_UNKNOWN)
14244 {
14245 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14246 if (typeerr)
14247 return;
14248 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014249 }
14250 else
14251 maxcount = 25;
14252
Bram Moolenaar4770d092006-01-12 23:22:24 +000014253 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014254
14255 for (i = 0; i < ga.ga_len; ++i)
14256 {
14257 str = ((char_u **)ga.ga_data)[i];
14258
14259 li = listitem_alloc();
14260 if (li == NULL)
14261 vim_free(str);
14262 else
14263 {
14264 li->li_tv.v_type = VAR_STRING;
14265 li->li_tv.v_lock = 0;
14266 li->li_tv.vval.v_string = str;
14267 list_append(l, li);
14268 }
14269 }
14270 ga_clear(&ga);
14271 }
14272#endif
14273}
14274
Bram Moolenaar0d660222005-01-07 21:51:51 +000014275 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014276f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014277 typval_T *argvars;
14278 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014279{
14280 char_u *str;
14281 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014282 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014283 regmatch_T regmatch;
14284 char_u patbuf[NUMBUFLEN];
14285 char_u *save_cpo;
14286 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000014287 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014288 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014289 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014290 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014291
14292 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14293 save_cpo = p_cpo;
14294 p_cpo = (char_u *)"";
14295
14296 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014297 if (argvars[1].v_type != VAR_UNKNOWN)
14298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014299 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14300 if (pat == NULL)
14301 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014302 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014303 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014304 }
14305 if (pat == NULL || *pat == NUL)
14306 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014307
14308 l = list_alloc();
14309 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014311 rettv->v_type = VAR_LIST;
14312 rettv->vval.v_list = l;
14313 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014314 if (typeerr)
14315 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316
Bram Moolenaar0d660222005-01-07 21:51:51 +000014317 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14318 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014320 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014321 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014322 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014323 if (*str == NUL)
14324 match = FALSE; /* empty item at the end */
14325 else
14326 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014327 if (match)
14328 end = regmatch.startp[0];
14329 else
14330 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014331 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
14332 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014333 {
Bram Moolenaar4463f292005-09-25 22:20:24 +000014334 if (list_append_string(l, str, (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014335 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014336 }
14337 if (!match)
14338 break;
14339 /* Advance to just after the match. */
14340 if (regmatch.endp[0] > str)
14341 col = 0;
14342 else
14343 {
14344 /* Don't get stuck at the same match. */
14345#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014346 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014347#else
14348 col = 1;
14349#endif
14350 }
14351 str = regmatch.endp[0];
14352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353
Bram Moolenaar0d660222005-01-07 21:51:51 +000014354 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356
Bram Moolenaar0d660222005-01-07 21:51:51 +000014357 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358}
14359
14360#ifdef HAVE_STRFTIME
14361/*
14362 * "strftime({format}[, {time}])" function
14363 */
14364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014366 typval_T *argvars;
14367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368{
14369 char_u result_buf[256];
14370 struct tm *curtime;
14371 time_t seconds;
14372 char_u *p;
14373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014377 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 seconds = time(NULL);
14379 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014380 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381 curtime = localtime(&seconds);
14382 /* MSVC returns NULL for an invalid value of seconds. */
14383 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014384 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385 else
14386 {
14387# ifdef FEAT_MBYTE
14388 vimconv_T conv;
14389 char_u *enc;
14390
14391 conv.vc_type = CONV_NONE;
14392 enc = enc_locale();
14393 convert_setup(&conv, p_enc, enc);
14394 if (conv.vc_type != CONV_NONE)
14395 p = string_convert(&conv, p, NULL);
14396# endif
14397 if (p != NULL)
14398 (void)strftime((char *)result_buf, sizeof(result_buf),
14399 (char *)p, curtime);
14400 else
14401 result_buf[0] = NUL;
14402
14403# ifdef FEAT_MBYTE
14404 if (conv.vc_type != CONV_NONE)
14405 vim_free(p);
14406 convert_setup(&conv, enc, p_enc);
14407 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014408 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409 else
14410# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412
14413# ifdef FEAT_MBYTE
14414 /* Release conversion descriptors */
14415 convert_setup(&conv, NULL, NULL);
14416 vim_free(enc);
14417# endif
14418 }
14419}
14420#endif
14421
14422/*
14423 * "stridx()" function
14424 */
14425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014426f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014427 typval_T *argvars;
14428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429{
14430 char_u buf[NUMBUFLEN];
14431 char_u *needle;
14432 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014433 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014435 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014437 needle = get_tv_string_chk(&argvars[1]);
14438 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014439 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014440 if (needle == NULL || haystack == NULL)
14441 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442
Bram Moolenaar33570922005-01-25 22:26:29 +000014443 if (argvars[2].v_type != VAR_UNKNOWN)
14444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014445 int error = FALSE;
14446
14447 start_idx = get_tv_number_chk(&argvars[2], &error);
14448 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014449 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014450 if (start_idx >= 0)
14451 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014452 }
14453
14454 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14455 if (pos != NULL)
14456 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457}
14458
14459/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014460 * "string()" function
14461 */
14462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014463f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014464 typval_T *argvars;
14465 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014466{
14467 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014468 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014470 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014471 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014472 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014473 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474}
14475
14476/*
14477 * "strlen()" function
14478 */
14479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014480f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014481 typval_T *argvars;
14482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014484 rettv->vval.v_number = (varnumber_T)(STRLEN(
14485 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486}
14487
14488/*
14489 * "strpart()" function
14490 */
14491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014492f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014493 typval_T *argvars;
14494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495{
14496 char_u *p;
14497 int n;
14498 int len;
14499 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014500 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014502 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503 slen = (int)STRLEN(p);
14504
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014505 n = get_tv_number_chk(&argvars[1], &error);
14506 if (error)
14507 len = 0;
14508 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014509 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510 else
14511 len = slen - n; /* default len: all bytes that are available. */
14512
14513 /*
14514 * Only return the overlap between the specified part and the actual
14515 * string.
14516 */
14517 if (n < 0)
14518 {
14519 len += n;
14520 n = 0;
14521 }
14522 else if (n > slen)
14523 n = slen;
14524 if (len < 0)
14525 len = 0;
14526 else if (n + len > slen)
14527 len = slen - n;
14528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014529 rettv->v_type = VAR_STRING;
14530 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531}
14532
14533/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014534 * "strridx()" function
14535 */
14536 static void
14537f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014538 typval_T *argvars;
14539 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014540{
14541 char_u buf[NUMBUFLEN];
14542 char_u *needle;
14543 char_u *haystack;
14544 char_u *rest;
14545 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014546 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014548 needle = get_tv_string_chk(&argvars[1]);
14549 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014550 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014551
14552 rettv->vval.v_number = -1;
14553 if (needle == NULL || haystack == NULL)
14554 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014555 if (argvars[2].v_type != VAR_UNKNOWN)
14556 {
14557 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014558 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014559 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014560 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014561 }
14562 else
14563 end_idx = haystack_len;
14564
Bram Moolenaar0d660222005-01-07 21:51:51 +000014565 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014566 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014567 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014568 lastmatch = haystack + end_idx;
14569 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014570 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014571 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014572 for (rest = haystack; *rest != '\0'; ++rest)
14573 {
14574 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014575 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014576 break;
14577 lastmatch = rest;
14578 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014579 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014580
14581 if (lastmatch == NULL)
14582 rettv->vval.v_number = -1;
14583 else
14584 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14585}
14586
14587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 * "strtrans()" function
14589 */
14590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014591f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014592 typval_T *argvars;
14593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014595 rettv->v_type = VAR_STRING;
14596 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597}
14598
14599/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014600 * "submatch()" function
14601 */
14602 static void
14603f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014604 typval_T *argvars;
14605 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014606{
14607 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014608 rettv->vval.v_string =
14609 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014610}
14611
14612/*
14613 * "substitute()" function
14614 */
14615 static void
14616f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014617 typval_T *argvars;
14618 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014619{
14620 char_u patbuf[NUMBUFLEN];
14621 char_u subbuf[NUMBUFLEN];
14622 char_u flagsbuf[NUMBUFLEN];
14623
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014624 char_u *str = get_tv_string_chk(&argvars[0]);
14625 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14626 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14627 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14628
Bram Moolenaar0d660222005-01-07 21:51:51 +000014629 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014630 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14631 rettv->vval.v_string = NULL;
14632 else
14633 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014634}
14635
14636/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014637 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014638 */
14639/*ARGSUSED*/
14640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014641f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014642 typval_T *argvars;
14643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644{
14645 int id = 0;
14646#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014647 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648 long col;
14649 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014650 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014651
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014652 lnum = get_tv_lnum(argvars); /* -1 on type error */
14653 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14654 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014656 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014657 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014658 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659#endif
14660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014661 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662}
14663
14664/*
14665 * "synIDattr(id, what [, mode])" function
14666 */
14667/*ARGSUSED*/
14668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014669f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014670 typval_T *argvars;
14671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672{
14673 char_u *p = NULL;
14674#ifdef FEAT_SYN_HL
14675 int id;
14676 char_u *what;
14677 char_u *mode;
14678 char_u modebuf[NUMBUFLEN];
14679 int modec;
14680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014681 id = get_tv_number(&argvars[0]);
14682 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014683 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014685 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686 modec = TOLOWER_ASC(mode[0]);
14687 if (modec != 't' && modec != 'c'
14688#ifdef FEAT_GUI
14689 && modec != 'g'
14690#endif
14691 )
14692 modec = 0; /* replace invalid with current */
14693 }
14694 else
14695 {
14696#ifdef FEAT_GUI
14697 if (gui.in_use)
14698 modec = 'g';
14699 else
14700#endif
14701 if (t_colors > 1)
14702 modec = 'c';
14703 else
14704 modec = 't';
14705 }
14706
14707
14708 switch (TOLOWER_ASC(what[0]))
14709 {
14710 case 'b':
14711 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14712 p = highlight_color(id, what, modec);
14713 else /* bold */
14714 p = highlight_has_attr(id, HL_BOLD, modec);
14715 break;
14716
14717 case 'f': /* fg[#] */
14718 p = highlight_color(id, what, modec);
14719 break;
14720
14721 case 'i':
14722 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14723 p = highlight_has_attr(id, HL_INVERSE, modec);
14724 else /* italic */
14725 p = highlight_has_attr(id, HL_ITALIC, modec);
14726 break;
14727
14728 case 'n': /* name */
14729 p = get_highlight_name(NULL, id - 1);
14730 break;
14731
14732 case 'r': /* reverse */
14733 p = highlight_has_attr(id, HL_INVERSE, modec);
14734 break;
14735
14736 case 's': /* standout */
14737 p = highlight_has_attr(id, HL_STANDOUT, modec);
14738 break;
14739
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014740 case 'u':
14741 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14742 /* underline */
14743 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14744 else
14745 /* undercurl */
14746 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747 break;
14748 }
14749
14750 if (p != NULL)
14751 p = vim_strsave(p);
14752#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014753 rettv->v_type = VAR_STRING;
14754 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755}
14756
14757/*
14758 * "synIDtrans(id)" function
14759 */
14760/*ARGSUSED*/
14761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014762f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014763 typval_T *argvars;
14764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765{
14766 int id;
14767
14768#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014769 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770
14771 if (id > 0)
14772 id = syn_get_final_id(id);
14773 else
14774#endif
14775 id = 0;
14776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014777 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778}
14779
14780/*
14781 * "system()" function
14782 */
14783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014784f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014785 typval_T *argvars;
14786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014788 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014790 char_u *infile = NULL;
14791 char_u buf[NUMBUFLEN];
14792 int err = FALSE;
14793 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014795 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014796 {
14797 /*
14798 * Write the string to a temp file, to be used for input of the shell
14799 * command.
14800 */
14801 if ((infile = vim_tempname('i')) == NULL)
14802 {
14803 EMSG(_(e_notmp));
14804 return;
14805 }
14806
14807 fd = mch_fopen((char *)infile, WRITEBIN);
14808 if (fd == NULL)
14809 {
14810 EMSG2(_(e_notopen), infile);
14811 goto done;
14812 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014813 p = get_tv_string_buf_chk(&argvars[1], buf);
14814 if (p == NULL)
14815 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014816 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14817 err = TRUE;
14818 if (fclose(fd) != 0)
14819 err = TRUE;
14820 if (err)
14821 {
14822 EMSG(_("E677: Error writing temp file"));
14823 goto done;
14824 }
14825 }
14826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014828
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829#ifdef USE_CR
14830 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014831 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832 {
14833 char_u *s;
14834
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014835 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836 {
14837 if (*s == CAR)
14838 *s = NL;
14839 }
14840 }
14841#else
14842# ifdef USE_CRNL
14843 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014844 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 {
14846 char_u *s, *d;
14847
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014848 d = res;
14849 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014850 {
14851 if (s[0] == CAR && s[1] == NL)
14852 ++s;
14853 *d++ = *s;
14854 }
14855 *d = NUL;
14856 }
14857# endif
14858#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014859
14860done:
14861 if (infile != NULL)
14862 {
14863 mch_remove(infile);
14864 vim_free(infile);
14865 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014866 rettv->v_type = VAR_STRING;
14867 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868}
14869
14870/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014871 * "tagfiles()" function
14872 */
14873/*ARGSUSED*/
14874 static void
14875f_tagfiles(argvars, rettv)
14876 typval_T *argvars;
14877 typval_T *rettv;
14878{
14879 char_u fname[MAXPATHL + 1];
14880 list_T *l;
14881
14882 l = list_alloc();
14883 if (l == NULL)
14884 {
14885 rettv->vval.v_number = 0;
14886 return;
14887 }
14888 rettv->vval.v_list = l;
14889 rettv->v_type = VAR_LIST;
14890 ++l->lv_refcount;
14891
14892 get_tagfname(TRUE, NULL);
14893 for (;;)
14894 if (get_tagfname(FALSE, fname) == FAIL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014895 || list_append_string(l, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014896 break;
14897}
14898
14899/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014900 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014901 */
14902 static void
14903f_taglist(argvars, rettv)
14904 typval_T *argvars;
14905 typval_T *rettv;
14906{
14907 char_u *tag_pattern;
14908 list_T *l;
14909
14910 tag_pattern = get_tv_string(&argvars[0]);
14911
14912 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014913 if (*tag_pattern == NUL)
14914 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014915
14916 l = list_alloc();
14917 if (l != NULL)
14918 {
14919 if (get_tags(l, tag_pattern) != FAIL)
14920 {
14921 rettv->vval.v_list = l;
14922 rettv->v_type = VAR_LIST;
14923 ++l->lv_refcount;
14924 }
14925 else
14926 list_free(l);
14927 }
14928}
14929
14930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931 * "tempname()" function
14932 */
14933/*ARGSUSED*/
14934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014935f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014936 typval_T *argvars;
14937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938{
14939 static int x = 'A';
14940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014941 rettv->v_type = VAR_STRING;
14942 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943
14944 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14945 * names. Skip 'I' and 'O', they are used for shell redirection. */
14946 do
14947 {
14948 if (x == 'Z')
14949 x = '0';
14950 else if (x == '9')
14951 x = 'A';
14952 else
14953 {
14954#ifdef EBCDIC
14955 if (x == 'I')
14956 x = 'J';
14957 else if (x == 'R')
14958 x = 'S';
14959 else
14960#endif
14961 ++x;
14962 }
14963 } while (x == 'I' || x == 'O');
14964}
14965
14966/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014967 * "test(list)" function: Just checking the walls...
14968 */
14969/*ARGSUSED*/
14970 static void
14971f_test(argvars, rettv)
14972 typval_T *argvars;
14973 typval_T *rettv;
14974{
14975 /* Used for unit testing. Change the code below to your liking. */
14976#if 0
14977 listitem_T *li;
14978 list_T *l;
14979 char_u *bad, *good;
14980
14981 if (argvars[0].v_type != VAR_LIST)
14982 return;
14983 l = argvars[0].vval.v_list;
14984 if (l == NULL)
14985 return;
14986 li = l->lv_first;
14987 if (li == NULL)
14988 return;
14989 bad = get_tv_string(&li->li_tv);
14990 li = li->li_next;
14991 if (li == NULL)
14992 return;
14993 good = get_tv_string(&li->li_tv);
14994 rettv->vval.v_number = test_edit_score(bad, good);
14995#endif
14996}
14997
14998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999 * "tolower(string)" function
15000 */
15001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015002f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015003 typval_T *argvars;
15004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005{
15006 char_u *p;
15007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015008 p = vim_strsave(get_tv_string(&argvars[0]));
15009 rettv->v_type = VAR_STRING;
15010 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011
15012 if (p != NULL)
15013 while (*p != NUL)
15014 {
15015#ifdef FEAT_MBYTE
15016 int l;
15017
15018 if (enc_utf8)
15019 {
15020 int c, lc;
15021
15022 c = utf_ptr2char(p);
15023 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015024 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025 /* TODO: reallocate string when byte count changes. */
15026 if (utf_char2len(lc) == l)
15027 utf_char2bytes(lc, p);
15028 p += l;
15029 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015030 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031 p += l; /* skip multi-byte character */
15032 else
15033#endif
15034 {
15035 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15036 ++p;
15037 }
15038 }
15039}
15040
15041/*
15042 * "toupper(string)" function
15043 */
15044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015045f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015046 typval_T *argvars;
15047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015048{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015049 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015050 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051}
15052
15053/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015054 * "tr(string, fromstr, tostr)" function
15055 */
15056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015057f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015058 typval_T *argvars;
15059 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015060{
15061 char_u *instr;
15062 char_u *fromstr;
15063 char_u *tostr;
15064 char_u *p;
15065#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015066 int inlen;
15067 int fromlen;
15068 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015069 int idx;
15070 char_u *cpstr;
15071 int cplen;
15072 int first = TRUE;
15073#endif
15074 char_u buf[NUMBUFLEN];
15075 char_u buf2[NUMBUFLEN];
15076 garray_T ga;
15077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015078 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015079 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15080 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015081
15082 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015083 rettv->v_type = VAR_STRING;
15084 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015085 if (fromstr == NULL || tostr == NULL)
15086 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015087 ga_init2(&ga, (int)sizeof(char), 80);
15088
15089#ifdef FEAT_MBYTE
15090 if (!has_mbyte)
15091#endif
15092 /* not multi-byte: fromstr and tostr must be the same length */
15093 if (STRLEN(fromstr) != STRLEN(tostr))
15094 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015095#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015096error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015097#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015098 EMSG2(_(e_invarg2), fromstr);
15099 ga_clear(&ga);
15100 return;
15101 }
15102
15103 /* fromstr and tostr have to contain the same number of chars */
15104 while (*instr != NUL)
15105 {
15106#ifdef FEAT_MBYTE
15107 if (has_mbyte)
15108 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015109 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015110 cpstr = instr;
15111 cplen = inlen;
15112 idx = 0;
15113 for (p = fromstr; *p != NUL; p += fromlen)
15114 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015115 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015116 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15117 {
15118 for (p = tostr; *p != NUL; p += tolen)
15119 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015120 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015121 if (idx-- == 0)
15122 {
15123 cplen = tolen;
15124 cpstr = p;
15125 break;
15126 }
15127 }
15128 if (*p == NUL) /* tostr is shorter than fromstr */
15129 goto error;
15130 break;
15131 }
15132 ++idx;
15133 }
15134
15135 if (first && cpstr == instr)
15136 {
15137 /* Check that fromstr and tostr have the same number of
15138 * (multi-byte) characters. Done only once when a character
15139 * of instr doesn't appear in fromstr. */
15140 first = FALSE;
15141 for (p = tostr; *p != NUL; p += tolen)
15142 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015143 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015144 --idx;
15145 }
15146 if (idx != 0)
15147 goto error;
15148 }
15149
15150 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015151 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015152 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015153
15154 instr += inlen;
15155 }
15156 else
15157#endif
15158 {
15159 /* When not using multi-byte chars we can do it faster. */
15160 p = vim_strchr(fromstr, *instr);
15161 if (p != NULL)
15162 ga_append(&ga, tostr[p - fromstr]);
15163 else
15164 ga_append(&ga, *instr);
15165 ++instr;
15166 }
15167 }
15168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015169 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015170}
15171
15172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173 * "type(expr)" function
15174 */
15175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015176f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015177 typval_T *argvars;
15178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015180 int n;
15181
15182 switch (argvars[0].v_type)
15183 {
15184 case VAR_NUMBER: n = 0; break;
15185 case VAR_STRING: n = 1; break;
15186 case VAR_FUNC: n = 2; break;
15187 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015188 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015189 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15190 }
15191 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192}
15193
15194/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015195 * "values(dict)" function
15196 */
15197 static void
15198f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015199 typval_T *argvars;
15200 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015201{
15202 dict_list(argvars, rettv, 1);
15203}
15204
15205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 * "virtcol(string)" function
15207 */
15208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015209f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015210 typval_T *argvars;
15211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015212{
15213 colnr_T vcol = 0;
15214 pos_T *fp;
15215
15216 fp = var2fpos(&argvars[0], FALSE);
15217 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
15218 {
15219 getvvcol(curwin, fp, NULL, NULL, &vcol);
15220 ++vcol;
15221 }
15222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015223 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015224}
15225
15226/*
15227 * "visualmode()" function
15228 */
15229/*ARGSUSED*/
15230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015231f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015232 typval_T *argvars;
15233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234{
15235#ifdef FEAT_VISUAL
15236 char_u str[2];
15237
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015238 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015239 str[0] = curbuf->b_visual_mode_eval;
15240 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015241 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242
15243 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015244 if ((argvars[0].v_type == VAR_NUMBER
15245 && argvars[0].vval.v_number != 0)
15246 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015247 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015248 curbuf->b_visual_mode_eval = NUL;
15249#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015250 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251#endif
15252}
15253
15254/*
15255 * "winbufnr(nr)" function
15256 */
15257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015258f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015259 typval_T *argvars;
15260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261{
15262 win_T *wp;
15263
15264 wp = find_win_by_nr(&argvars[0]);
15265 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015266 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015268 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269}
15270
15271/*
15272 * "wincol()" function
15273 */
15274/*ARGSUSED*/
15275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015276f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015277 typval_T *argvars;
15278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279{
15280 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015281 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282}
15283
15284/*
15285 * "winheight(nr)" function
15286 */
15287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015288f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015289 typval_T *argvars;
15290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291{
15292 win_T *wp;
15293
15294 wp = find_win_by_nr(&argvars[0]);
15295 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015296 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015298 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015299}
15300
15301/*
15302 * "winline()" function
15303 */
15304/*ARGSUSED*/
15305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015306f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015307 typval_T *argvars;
15308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309{
15310 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015311 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312}
15313
15314/*
15315 * "winnr()" function
15316 */
15317/* ARGSUSED */
15318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015319f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015320 typval_T *argvars;
15321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322{
15323 int nr = 1;
15324#ifdef FEAT_WINDOWS
15325 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015326 win_T *twin = curwin;
15327 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015329 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015331 arg = get_tv_string_chk(&argvars[0]);
15332 if (arg == NULL)
15333 nr = 0; /* type error; errmsg already given */
15334 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015335 twin = lastwin;
15336 else if (STRCMP(arg, "#") == 0)
15337 {
15338 twin = prevwin;
15339 if (prevwin == NULL)
15340 nr = 0;
15341 }
15342 else
15343 {
15344 EMSG2(_(e_invexpr2), arg);
15345 nr = 0;
15346 }
15347 }
15348
15349 if (nr > 0)
15350 for (wp = firstwin; wp != twin; wp = wp->w_next)
15351 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015353 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354}
15355
15356/*
15357 * "winrestcmd()" function
15358 */
15359/* ARGSUSED */
15360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015361f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015362 typval_T *argvars;
15363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364{
15365#ifdef FEAT_WINDOWS
15366 win_T *wp;
15367 int winnr = 1;
15368 garray_T ga;
15369 char_u buf[50];
15370
15371 ga_init2(&ga, (int)sizeof(char), 70);
15372 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15373 {
15374 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15375 ga_concat(&ga, buf);
15376# ifdef FEAT_VERTSPLIT
15377 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15378 ga_concat(&ga, buf);
15379# endif
15380 ++winnr;
15381 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015382 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015384 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015386 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015388 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389}
15390
15391/*
15392 * "winwidth(nr)" function
15393 */
15394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015395f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015396 typval_T *argvars;
15397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398{
15399 win_T *wp;
15400
15401 wp = find_win_by_nr(&argvars[0]);
15402 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015403 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404 else
15405#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015408 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409#endif
15410}
15411
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015413 * "writefile()" function
15414 */
15415 static void
15416f_writefile(argvars, rettv)
15417 typval_T *argvars;
15418 typval_T *rettv;
15419{
15420 int binary = FALSE;
15421 char_u *fname;
15422 FILE *fd;
15423 listitem_T *li;
15424 char_u *s;
15425 int ret = 0;
15426 int c;
15427
15428 if (argvars[0].v_type != VAR_LIST)
15429 {
15430 EMSG2(_(e_listarg), "writefile()");
15431 return;
15432 }
15433 if (argvars[0].vval.v_list == NULL)
15434 return;
15435
15436 if (argvars[2].v_type != VAR_UNKNOWN
15437 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15438 binary = TRUE;
15439
15440 /* Always open the file in binary mode, library functions have a mind of
15441 * their own about CR-LF conversion. */
15442 fname = get_tv_string(&argvars[1]);
15443 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15444 {
15445 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15446 ret = -1;
15447 }
15448 else
15449 {
15450 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15451 li = li->li_next)
15452 {
15453 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15454 {
15455 if (*s == '\n')
15456 c = putc(NUL, fd);
15457 else
15458 c = putc(*s, fd);
15459 if (c == EOF)
15460 {
15461 ret = -1;
15462 break;
15463 }
15464 }
15465 if (!binary || li->li_next != NULL)
15466 if (putc('\n', fd) == EOF)
15467 {
15468 ret = -1;
15469 break;
15470 }
15471 if (ret < 0)
15472 {
15473 EMSG(_(e_write));
15474 break;
15475 }
15476 }
15477 fclose(fd);
15478 }
15479
15480 rettv->vval.v_number = ret;
15481}
15482
15483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 * Translate a String variable into a position.
15485 */
15486 static pos_T *
15487var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015488 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489 int lnum; /* TRUE when $ is last line */
15490{
15491 char_u *name;
15492 static pos_T pos;
15493 pos_T *pp;
15494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015495 name = get_tv_string_chk(varp);
15496 if (name == NULL)
15497 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498 if (name[0] == '.') /* cursor */
15499 return &curwin->w_cursor;
15500 if (name[0] == '\'') /* mark */
15501 {
15502 pp = getmark(name[1], FALSE);
15503 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15504 return NULL;
15505 return pp;
15506 }
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015507 if (name[0] == 'w' && lnum)
15508 {
15509 pos.col = 0;
15510 if (name[1] == '0') /* "w0": first visible line */
15511 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015512 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015513 pos.lnum = curwin->w_topline;
15514 return &pos;
15515 }
15516 else if (name[1] == '$') /* "w$": last visible line */
15517 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015518 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015519 pos.lnum = curwin->w_botline - 1;
15520 return &pos;
15521 }
15522 }
15523 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524 {
15525 if (lnum)
15526 {
15527 pos.lnum = curbuf->b_ml.ml_line_count;
15528 pos.col = 0;
15529 }
15530 else
15531 {
15532 pos.lnum = curwin->w_cursor.lnum;
15533 pos.col = (colnr_T)STRLEN(ml_get_curline());
15534 }
15535 return &pos;
15536 }
15537 return NULL;
15538}
15539
15540/*
15541 * Get the length of an environment variable name.
15542 * Advance "arg" to the first character after the name.
15543 * Return 0 for error.
15544 */
15545 static int
15546get_env_len(arg)
15547 char_u **arg;
15548{
15549 char_u *p;
15550 int len;
15551
15552 for (p = *arg; vim_isIDc(*p); ++p)
15553 ;
15554 if (p == *arg) /* no name found */
15555 return 0;
15556
15557 len = (int)(p - *arg);
15558 *arg = p;
15559 return len;
15560}
15561
15562/*
15563 * Get the length of the name of a function or internal variable.
15564 * "arg" is advanced to the first non-white character after the name.
15565 * Return 0 if something is wrong.
15566 */
15567 static int
15568get_id_len(arg)
15569 char_u **arg;
15570{
15571 char_u *p;
15572 int len;
15573
15574 /* Find the end of the name. */
15575 for (p = *arg; eval_isnamec(*p); ++p)
15576 ;
15577 if (p == *arg) /* no name found */
15578 return 0;
15579
15580 len = (int)(p - *arg);
15581 *arg = skipwhite(p);
15582
15583 return len;
15584}
15585
15586/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015587 * Get the length of the name of a variable or function.
15588 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015590 * Return -1 if curly braces expansion failed.
15591 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 * If the name contains 'magic' {}'s, expand them and return the
15593 * expanded name in an allocated string via 'alias' - caller must free.
15594 */
15595 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015596get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597 char_u **arg;
15598 char_u **alias;
15599 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015600 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601{
15602 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 char_u *p;
15604 char_u *expr_start;
15605 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015606
15607 *alias = NULL; /* default to no alias */
15608
15609 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15610 && (*arg)[2] == (int)KE_SNR)
15611 {
15612 /* hard coded <SNR>, already translated */
15613 *arg += 3;
15614 return get_id_len(arg) + 3;
15615 }
15616 len = eval_fname_script(*arg);
15617 if (len > 0)
15618 {
15619 /* literal "<SID>", "s:" or "<SNR>" */
15620 *arg += len;
15621 }
15622
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015624 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015626 p = find_name_end(*arg, &expr_start, &expr_end,
15627 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 if (expr_start != NULL)
15629 {
15630 char_u *temp_string;
15631
15632 if (!evaluate)
15633 {
15634 len += (int)(p - *arg);
15635 *arg = skipwhite(p);
15636 return len;
15637 }
15638
15639 /*
15640 * Include any <SID> etc in the expanded string:
15641 * Thus the -len here.
15642 */
15643 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15644 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015645 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646 *alias = temp_string;
15647 *arg = skipwhite(p);
15648 return (int)STRLEN(temp_string);
15649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650
15651 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015652 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653 EMSG2(_(e_invexpr2), *arg);
15654
15655 return len;
15656}
15657
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015658/*
15659 * Find the end of a variable or function name, taking care of magic braces.
15660 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15661 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015662 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015663 * Return a pointer to just after the name. Equal to "arg" if there is no
15664 * valid name.
15665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015667find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 char_u *arg;
15669 char_u **expr_start;
15670 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015671 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015673 int mb_nest = 0;
15674 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675 char_u *p;
15676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015677 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015679 *expr_start = NULL;
15680 *expr_end = NULL;
15681 }
15682
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015683 /* Quick check for valid starting character. */
15684 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15685 return arg;
15686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015687 for (p = arg; *p != NUL
15688 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015689 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015690 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015691 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015692 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015693 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015694 if (*p == '\'')
15695 {
15696 /* skip over 'string' to avoid counting [ and ] inside it. */
15697 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15698 ;
15699 if (*p == NUL)
15700 break;
15701 }
15702 else if (*p == '"')
15703 {
15704 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15705 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15706 if (*p == '\\' && p[1] != NUL)
15707 ++p;
15708 if (*p == NUL)
15709 break;
15710 }
15711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015712 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015714 if (*p == '[')
15715 ++br_nest;
15716 else if (*p == ']')
15717 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015720 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015722 if (*p == '{')
15723 {
15724 mb_nest++;
15725 if (expr_start != NULL && *expr_start == NULL)
15726 *expr_start = p;
15727 }
15728 else if (*p == '}')
15729 {
15730 mb_nest--;
15731 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15732 *expr_end = p;
15733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 }
15736
15737 return p;
15738}
15739
15740/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015741 * Expands out the 'magic' {}'s in a variable/function name.
15742 * Note that this can call itself recursively, to deal with
15743 * constructs like foo{bar}{baz}{bam}
15744 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15745 * "in_start" ^
15746 * "expr_start" ^
15747 * "expr_end" ^
15748 * "in_end" ^
15749 *
15750 * Returns a new allocated string, which the caller must free.
15751 * Returns NULL for failure.
15752 */
15753 static char_u *
15754make_expanded_name(in_start, expr_start, expr_end, in_end)
15755 char_u *in_start;
15756 char_u *expr_start;
15757 char_u *expr_end;
15758 char_u *in_end;
15759{
15760 char_u c1;
15761 char_u *retval = NULL;
15762 char_u *temp_result;
15763 char_u *nextcmd = NULL;
15764
15765 if (expr_end == NULL || in_end == NULL)
15766 return NULL;
15767 *expr_start = NUL;
15768 *expr_end = NUL;
15769 c1 = *in_end;
15770 *in_end = NUL;
15771
15772 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15773 if (temp_result != NULL && nextcmd == NULL)
15774 {
15775 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15776 + (in_end - expr_end) + 1));
15777 if (retval != NULL)
15778 {
15779 STRCPY(retval, in_start);
15780 STRCAT(retval, temp_result);
15781 STRCAT(retval, expr_end + 1);
15782 }
15783 }
15784 vim_free(temp_result);
15785
15786 *in_end = c1; /* put char back for error messages */
15787 *expr_start = '{';
15788 *expr_end = '}';
15789
15790 if (retval != NULL)
15791 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015792 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015793 if (expr_start != NULL)
15794 {
15795 /* Further expansion! */
15796 temp_result = make_expanded_name(retval, expr_start,
15797 expr_end, temp_result);
15798 vim_free(retval);
15799 retval = temp_result;
15800 }
15801 }
15802
15803 return retval;
15804}
15805
15806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015807 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015808 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 */
15810 static int
15811eval_isnamec(c)
15812 int c;
15813{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015814 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15815}
15816
15817/*
15818 * Return TRUE if character "c" can be used as the first character in a
15819 * variable or function name (excluding '{' and '}').
15820 */
15821 static int
15822eval_isnamec1(c)
15823 int c;
15824{
15825 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826}
15827
15828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 * Set number v: variable to "val".
15830 */
15831 void
15832set_vim_var_nr(idx, val)
15833 int idx;
15834 long val;
15835{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015836 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837}
15838
15839/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015840 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841 */
15842 long
15843get_vim_var_nr(idx)
15844 int idx;
15845{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015846 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847}
15848
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015849#if defined(FEAT_AUTOCMD) || defined(PROTO)
15850/*
15851 * Get string v: variable value. Uses a static buffer, can only be used once.
15852 */
15853 char_u *
15854get_vim_var_str(idx)
15855 int idx;
15856{
15857 return get_tv_string(&vimvars[idx].vv_tv);
15858}
15859#endif
15860
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861/*
15862 * Set v:count, v:count1 and v:prevcount.
15863 */
15864 void
15865set_vcount(count, count1)
15866 long count;
15867 long count1;
15868{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015869 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15870 vimvars[VV_COUNT].vv_nr = count;
15871 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872}
15873
15874/*
15875 * Set string v: variable to a copy of "val".
15876 */
15877 void
15878set_vim_var_string(idx, val, len)
15879 int idx;
15880 char_u *val;
15881 int len; /* length of "val" to use or -1 (whole string) */
15882{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015883 /* Need to do this (at least) once, since we can't initialize a union.
15884 * Will always be invoked when "v:progname" is set. */
15885 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15886
Bram Moolenaare9a41262005-01-15 22:18:47 +000015887 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015889 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015891 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015893 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015894}
15895
15896/*
15897 * Set v:register if needed.
15898 */
15899 void
15900set_reg_var(c)
15901 int c;
15902{
15903 char_u regname;
15904
15905 if (c == 0 || c == ' ')
15906 regname = '"';
15907 else
15908 regname = c;
15909 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015910 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911 set_vim_var_string(VV_REG, &regname, 1);
15912}
15913
15914/*
15915 * Get or set v:exception. If "oldval" == NULL, return the current value.
15916 * Otherwise, restore the value to "oldval" and return NULL.
15917 * Must always be called in pairs to save and restore v:exception! Does not
15918 * take care of memory allocations.
15919 */
15920 char_u *
15921v_exception(oldval)
15922 char_u *oldval;
15923{
15924 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015925 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015926
Bram Moolenaare9a41262005-01-15 22:18:47 +000015927 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928 return NULL;
15929}
15930
15931/*
15932 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15933 * Otherwise, restore the value to "oldval" and return NULL.
15934 * Must always be called in pairs to save and restore v:throwpoint! Does not
15935 * take care of memory allocations.
15936 */
15937 char_u *
15938v_throwpoint(oldval)
15939 char_u *oldval;
15940{
15941 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015942 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943
Bram Moolenaare9a41262005-01-15 22:18:47 +000015944 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015945 return NULL;
15946}
15947
15948#if defined(FEAT_AUTOCMD) || defined(PROTO)
15949/*
15950 * Set v:cmdarg.
15951 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15952 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15953 * Must always be called in pairs!
15954 */
15955 char_u *
15956set_cmdarg(eap, oldarg)
15957 exarg_T *eap;
15958 char_u *oldarg;
15959{
15960 char_u *oldval;
15961 char_u *newval;
15962 unsigned len;
15963
Bram Moolenaare9a41262005-01-15 22:18:47 +000015964 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015965 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015967 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015968 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015969 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015970 }
15971
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015972 if (eap->force_bin == FORCE_BIN)
15973 len = 6;
15974 else if (eap->force_bin == FORCE_NOBIN)
15975 len = 8;
15976 else
15977 len = 0;
15978 if (eap->force_ff != 0)
15979 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15980# ifdef FEAT_MBYTE
15981 if (eap->force_enc != 0)
15982 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000015983 if (eap->bad_char != 0)
15984 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015985# endif
15986
15987 newval = alloc(len + 1);
15988 if (newval == NULL)
15989 return NULL;
15990
15991 if (eap->force_bin == FORCE_BIN)
15992 sprintf((char *)newval, " ++bin");
15993 else if (eap->force_bin == FORCE_NOBIN)
15994 sprintf((char *)newval, " ++nobin");
15995 else
15996 *newval = NUL;
15997 if (eap->force_ff != 0)
15998 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15999 eap->cmd + eap->force_ff);
16000# ifdef FEAT_MBYTE
16001 if (eap->force_enc != 0)
16002 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16003 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016004 if (eap->bad_char != 0)
16005 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16006 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016007# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016008 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016009 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010}
16011#endif
16012
16013/*
16014 * Get the value of internal variable "name".
16015 * Return OK or FAIL.
16016 */
16017 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016018get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019 char_u *name;
16020 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016021 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016022 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023{
16024 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016025 typval_T *tv = NULL;
16026 typval_T atv;
16027 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029
16030 /* truncate the name, so that we can use strcmp() */
16031 cc = name[len];
16032 name[len] = NUL;
16033
16034 /*
16035 * Check for "b:changedtick".
16036 */
16037 if (STRCMP(name, "b:changedtick") == 0)
16038 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016039 atv.v_type = VAR_NUMBER;
16040 atv.vval.v_number = curbuf->b_changedtick;
16041 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 }
16043
16044 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 * Check for user-defined variables.
16046 */
16047 else
16048 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016049 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016051 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052 }
16053
Bram Moolenaare9a41262005-01-15 22:18:47 +000016054 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016056 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016057 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058 ret = FAIL;
16059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016060 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016061 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062
16063 name[len] = cc;
16064
16065 return ret;
16066}
16067
16068/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016069 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16070 * Also handle function call with Funcref variable: func(expr)
16071 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16072 */
16073 static int
16074handle_subscript(arg, rettv, evaluate, verbose)
16075 char_u **arg;
16076 typval_T *rettv;
16077 int evaluate; /* do more than finding the end */
16078 int verbose; /* give error messages */
16079{
16080 int ret = OK;
16081 dict_T *selfdict = NULL;
16082 char_u *s;
16083 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016084 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016085
16086 while (ret == OK
16087 && (**arg == '['
16088 || (**arg == '.' && rettv->v_type == VAR_DICT)
16089 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16090 && !vim_iswhite(*(*arg - 1)))
16091 {
16092 if (**arg == '(')
16093 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016094 /* need to copy the funcref so that we can clear rettv */
16095 functv = *rettv;
16096 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016097
16098 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016099 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016100 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016101 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16102 &len, evaluate, selfdict);
16103
16104 /* Clear the funcref afterwards, so that deleting it while
16105 * evaluating the arguments is possible (see test55). */
16106 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016107
16108 /* Stop the expression evaluation when immediately aborting on
16109 * error, or when an interrupt occurred or an exception was thrown
16110 * but not caught. */
16111 if (aborting())
16112 {
16113 if (ret == OK)
16114 clear_tv(rettv);
16115 ret = FAIL;
16116 }
16117 dict_unref(selfdict);
16118 selfdict = NULL;
16119 }
16120 else /* **arg == '[' || **arg == '.' */
16121 {
16122 dict_unref(selfdict);
16123 if (rettv->v_type == VAR_DICT)
16124 {
16125 selfdict = rettv->vval.v_dict;
16126 if (selfdict != NULL)
16127 ++selfdict->dv_refcount;
16128 }
16129 else
16130 selfdict = NULL;
16131 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16132 {
16133 clear_tv(rettv);
16134 ret = FAIL;
16135 }
16136 }
16137 }
16138 dict_unref(selfdict);
16139 return ret;
16140}
16141
16142/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016143 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16144 * value).
16145 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016146 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016147alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016148{
Bram Moolenaar33570922005-01-25 22:26:29 +000016149 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016150}
16151
16152/*
16153 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154 * The string "s" must have been allocated, it is consumed.
16155 * Return NULL for out of memory, the variable otherwise.
16156 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016157 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016158alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159 char_u *s;
16160{
Bram Moolenaar33570922005-01-25 22:26:29 +000016161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016163 rettv = alloc_tv();
16164 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016166 rettv->v_type = VAR_STRING;
16167 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 }
16169 else
16170 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016171 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172}
16173
16174/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016175 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016177 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016178free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016179 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180{
16181 if (varp != NULL)
16182 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016183 switch (varp->v_type)
16184 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016185 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016186 func_unref(varp->vval.v_string);
16187 /*FALLTHROUGH*/
16188 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016189 vim_free(varp->vval.v_string);
16190 break;
16191 case VAR_LIST:
16192 list_unref(varp->vval.v_list);
16193 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016194 case VAR_DICT:
16195 dict_unref(varp->vval.v_dict);
16196 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016197 case VAR_NUMBER:
16198 case VAR_UNKNOWN:
16199 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016200 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016201 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016202 break;
16203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 vim_free(varp);
16205 }
16206}
16207
16208/*
16209 * Free the memory for a variable value and set the value to NULL or 0.
16210 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016211 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016212clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016213 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214{
16215 if (varp != NULL)
16216 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016217 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016219 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016220 func_unref(varp->vval.v_string);
16221 /*FALLTHROUGH*/
16222 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016223 vim_free(varp->vval.v_string);
16224 varp->vval.v_string = NULL;
16225 break;
16226 case VAR_LIST:
16227 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016228 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016229 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016230 case VAR_DICT:
16231 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016232 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016233 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016234 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016235 varp->vval.v_number = 0;
16236 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016237 case VAR_UNKNOWN:
16238 break;
16239 default:
16240 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016242 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243 }
16244}
16245
16246/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016247 * Set the value of a variable to NULL without freeing items.
16248 */
16249 static void
16250init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016251 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016252{
16253 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016254 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016255}
16256
16257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258 * Get the number value of a variable.
16259 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016260 * For incompatible types, return 0.
16261 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16262 * caller of incompatible types: it sets *denote to TRUE if "denote"
16263 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264 */
16265 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016266get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016267 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016269 int error = FALSE;
16270
16271 return get_tv_number_chk(varp, &error); /* return 0L on error */
16272}
16273
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016274 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016275get_tv_number_chk(varp, denote)
16276 typval_T *varp;
16277 int *denote;
16278{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016279 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016281 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016283 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016284 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016285 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016286 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016287 break;
16288 case VAR_STRING:
16289 if (varp->vval.v_string != NULL)
16290 vim_str2nr(varp->vval.v_string, NULL, NULL,
16291 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016292 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016293 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016294 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016295 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016296 case VAR_DICT:
16297 EMSG(_("E728: Using a Dictionary as a number"));
16298 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016299 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016300 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016303 if (denote == NULL) /* useful for values that must be unsigned */
16304 n = -1;
16305 else
16306 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016307 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308}
16309
16310/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016311 * Get the lnum from the first argument.
16312 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016313 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 */
16315 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016316get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016317 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318{
Bram Moolenaar33570922005-01-25 22:26:29 +000016319 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320 linenr_T lnum;
16321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016322 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323 if (lnum == 0) /* no valid number, try using line() */
16324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016325 rettv.v_type = VAR_NUMBER;
16326 f_line(argvars, &rettv);
16327 lnum = rettv.vval.v_number;
16328 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329 }
16330 return lnum;
16331}
16332
16333/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016334 * Get the lnum from the first argument.
16335 * Also accepts "$", then "buf" is used.
16336 * Returns 0 on error.
16337 */
16338 static linenr_T
16339get_tv_lnum_buf(argvars, buf)
16340 typval_T *argvars;
16341 buf_T *buf;
16342{
16343 if (argvars[0].v_type == VAR_STRING
16344 && argvars[0].vval.v_string != NULL
16345 && argvars[0].vval.v_string[0] == '$'
16346 && buf != NULL)
16347 return buf->b_ml.ml_line_count;
16348 return get_tv_number_chk(&argvars[0], NULL);
16349}
16350
16351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 * Get the string value of a variable.
16353 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016354 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16355 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356 * If the String variable has never been set, return an empty string.
16357 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016358 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16359 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360 */
16361 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016362get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016363 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364{
16365 static char_u mybuf[NUMBUFLEN];
16366
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016367 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368}
16369
16370 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016371get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016372 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016373 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016375 char_u *res = get_tv_string_buf_chk(varp, buf);
16376
16377 return res != NULL ? res : (char_u *)"";
16378}
16379
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016380 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016381get_tv_string_chk(varp)
16382 typval_T *varp;
16383{
16384 static char_u mybuf[NUMBUFLEN];
16385
16386 return get_tv_string_buf_chk(varp, mybuf);
16387}
16388
16389 static char_u *
16390get_tv_string_buf_chk(varp, buf)
16391 typval_T *varp;
16392 char_u *buf;
16393{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016394 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016396 case VAR_NUMBER:
16397 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16398 return buf;
16399 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016400 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016401 break;
16402 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016403 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016404 break;
16405 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016406 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016407 break;
16408 case VAR_STRING:
16409 if (varp->vval.v_string != NULL)
16410 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016411 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016412 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016413 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016414 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016416 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417}
16418
16419/*
16420 * Find variable "name" in the list of variables.
16421 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016422 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016423 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016424 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016426 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016427find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016429 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016432 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433
Bram Moolenaara7043832005-01-21 11:56:39 +000016434 ht = find_var_ht(name, &varname);
16435 if (htp != NULL)
16436 *htp = ht;
16437 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016439 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440}
16441
16442/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016443 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016444 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016446 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016447find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016448 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016449 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016450 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016451{
Bram Moolenaar33570922005-01-25 22:26:29 +000016452 hashitem_T *hi;
16453
16454 if (*varname == NUL)
16455 {
16456 /* Must be something like "s:", otherwise "ht" would be NULL. */
16457 switch (varname[-2])
16458 {
16459 case 's': return &SCRIPT_SV(current_SID).sv_var;
16460 case 'g': return &globvars_var;
16461 case 'v': return &vimvars_var;
16462 case 'b': return &curbuf->b_bufvar;
16463 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016464 case 'l': return current_funccal == NULL
16465 ? NULL : &current_funccal->l_vars_var;
16466 case 'a': return current_funccal == NULL
16467 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016468 }
16469 return NULL;
16470 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016471
16472 hi = hash_find(ht, varname);
16473 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016474 {
16475 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016476 * worked find the variable again. Don't auto-load a script if it was
16477 * loaded already, otherwise it would be loaded every time when
16478 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016479 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016480 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016481 hi = hash_find(ht, varname);
16482 if (HASHITEM_EMPTY(hi))
16483 return NULL;
16484 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016485 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016486}
16487
16488/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016489 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016490 * Set "varname" to the start of name without ':'.
16491 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016492 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016493find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 char_u *name;
16495 char_u **varname;
16496{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016497 hashitem_T *hi;
16498
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499 if (name[1] != ':')
16500 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016501 /* The name must not start with a colon or #. */
16502 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 return NULL;
16504 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016505
16506 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016507 hi = hash_find(&compat_hashtab, name);
16508 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016509 return &compat_hashtab;
16510
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016512 return &globvarht; /* global variable */
16513 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514 }
16515 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016516 if (*name == 'g') /* global variable */
16517 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016518 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16519 */
16520 if (vim_strchr(name + 2, ':') != NULL
16521 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016522 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016524 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016526 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016527 if (*name == 'v') /* v: variable */
16528 return &vimvarht;
16529 if (*name == 'a' && current_funccal != NULL) /* function argument */
16530 return &current_funccal->l_avars.dv_hashtab;
16531 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16532 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533 if (*name == 's' /* script variable */
16534 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16535 return &SCRIPT_VARS(current_SID);
16536 return NULL;
16537}
16538
16539/*
16540 * Get the string value of a (global/local) variable.
16541 * Returns NULL when it doesn't exist.
16542 */
16543 char_u *
16544get_var_value(name)
16545 char_u *name;
16546{
Bram Moolenaar33570922005-01-25 22:26:29 +000016547 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548
Bram Moolenaara7043832005-01-21 11:56:39 +000016549 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 if (v == NULL)
16551 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016552 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553}
16554
16555/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016556 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557 * sourcing this script and when executing functions defined in the script.
16558 */
16559 void
16560new_script_vars(id)
16561 scid_T id;
16562{
Bram Moolenaara7043832005-01-21 11:56:39 +000016563 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016564 hashtab_T *ht;
16565 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016566
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16568 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016569 /* Re-allocating ga_data means that an ht_array pointing to
16570 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016571 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016572 for (i = 1; i <= ga_scripts.ga_len; ++i)
16573 {
16574 ht = &SCRIPT_VARS(i);
16575 if (ht->ht_mask == HT_INIT_SIZE - 1)
16576 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016577 sv = &SCRIPT_SV(i);
16578 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016579 }
16580
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581 while (ga_scripts.ga_len < id)
16582 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016583 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16584 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 }
16587 }
16588}
16589
16590/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016591 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16592 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593 */
16594 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016595init_var_dict(dict, dict_var)
16596 dict_T *dict;
16597 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598{
Bram Moolenaar33570922005-01-25 22:26:29 +000016599 hash_init(&dict->dv_hashtab);
16600 dict->dv_refcount = 99999;
16601 dict_var->di_tv.vval.v_dict = dict;
16602 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016603 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016604 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16605 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606}
16607
16608/*
16609 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016610 * Frees all allocated variables and the value they contain.
16611 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016612 */
16613 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016614vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016615 hashtab_T *ht;
16616{
16617 vars_clear_ext(ht, TRUE);
16618}
16619
16620/*
16621 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16622 */
16623 static void
16624vars_clear_ext(ht, free_val)
16625 hashtab_T *ht;
16626 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627{
Bram Moolenaara7043832005-01-21 11:56:39 +000016628 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016629 hashitem_T *hi;
16630 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631
Bram Moolenaar33570922005-01-25 22:26:29 +000016632 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016633 todo = ht->ht_used;
16634 for (hi = ht->ht_array; todo > 0; ++hi)
16635 {
16636 if (!HASHITEM_EMPTY(hi))
16637 {
16638 --todo;
16639
Bram Moolenaar33570922005-01-25 22:26:29 +000016640 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016641 * ht_array might change then. hash_clear() takes care of it
16642 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016643 v = HI2DI(hi);
16644 if (free_val)
16645 clear_tv(&v->di_tv);
16646 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16647 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016648 }
16649 }
16650 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016651 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652}
16653
Bram Moolenaara7043832005-01-21 11:56:39 +000016654/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016655 * Delete a variable from hashtab "ht" at item "hi".
16656 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016659delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016660 hashtab_T *ht;
16661 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662{
Bram Moolenaar33570922005-01-25 22:26:29 +000016663 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016664
16665 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016666 clear_tv(&di->di_tv);
16667 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668}
16669
16670/*
16671 * List the value of one internal variable.
16672 */
16673 static void
16674list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016675 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676 char_u *prefix;
16677{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016678 char_u *tofree;
16679 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016680 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016681
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016682 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000016683 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016684 s == NULL ? (char_u *)"" : s);
16685 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016686}
16687
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688 static void
16689list_one_var_a(prefix, name, type, string)
16690 char_u *prefix;
16691 char_u *name;
16692 int type;
16693 char_u *string;
16694{
16695 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16696 if (name != NULL) /* "a:" vars don't have a name stored */
16697 msg_puts(name);
16698 msg_putchar(' ');
16699 msg_advance(22);
16700 if (type == VAR_NUMBER)
16701 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016702 else if (type == VAR_FUNC)
16703 msg_putchar('*');
16704 else if (type == VAR_LIST)
16705 {
16706 msg_putchar('[');
16707 if (*string == '[')
16708 ++string;
16709 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016710 else if (type == VAR_DICT)
16711 {
16712 msg_putchar('{');
16713 if (*string == '{')
16714 ++string;
16715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 else
16717 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016718
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016720
16721 if (type == VAR_FUNC)
16722 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723}
16724
16725/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016726 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727 * If the variable already exists, the value is updated.
16728 * Otherwise the variable is created.
16729 */
16730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016731set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016733 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016734 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735{
Bram Moolenaar33570922005-01-25 22:26:29 +000016736 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016738 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016739 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016741 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016742 {
16743 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16744 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16745 ? name[2] : name[0]))
16746 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016747 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016748 return;
16749 }
16750 if (function_exists(name))
16751 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016752 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016753 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016754 return;
16755 }
16756 }
16757
Bram Moolenaara7043832005-01-21 11:56:39 +000016758 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016759 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016760 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016761 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016762 return;
16763 }
16764
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016765 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016766 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016768 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016769 if (var_check_ro(v->di_flags, name)
16770 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016771 return;
16772 if (v->di_tv.v_type != tv->v_type
16773 && !((v->di_tv.v_type == VAR_STRING
16774 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016775 && (tv->v_type == VAR_STRING
16776 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016777 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016778 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016779 return;
16780 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016781
16782 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016783 * Handle setting internal v: variables separately: we don't change
16784 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016785 */
16786 if (ht == &vimvarht)
16787 {
16788 if (v->di_tv.v_type == VAR_STRING)
16789 {
16790 vim_free(v->di_tv.vval.v_string);
16791 if (copy || tv->v_type != VAR_STRING)
16792 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16793 else
16794 {
16795 /* Take over the string to avoid an extra alloc/free. */
16796 v->di_tv.vval.v_string = tv->vval.v_string;
16797 tv->vval.v_string = NULL;
16798 }
16799 }
16800 else if (v->di_tv.v_type != VAR_NUMBER)
16801 EMSG2(_(e_intern2), "set_var()");
16802 else
16803 v->di_tv.vval.v_number = get_tv_number(tv);
16804 return;
16805 }
16806
16807 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808 }
16809 else /* add a new variable */
16810 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016811 /* Make sure the variable name is valid. */
16812 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000016813 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
16814 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000016815 {
16816 EMSG2(_(e_illvar), varname);
16817 return;
16818 }
16819
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016820 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16821 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016822 if (v == NULL)
16823 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016824 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016825 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016827 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828 return;
16829 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016830 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016833 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016834 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016835 else
16836 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016837 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016838 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016839 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841}
16842
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016843/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016844 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16845 * Also give an error message.
16846 */
16847 static int
16848var_check_ro(flags, name)
16849 int flags;
16850 char_u *name;
16851{
16852 if (flags & DI_FLAGS_RO)
16853 {
16854 EMSG2(_(e_readonlyvar), name);
16855 return TRUE;
16856 }
16857 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16858 {
16859 EMSG2(_(e_readonlysbx), name);
16860 return TRUE;
16861 }
16862 return FALSE;
16863}
16864
16865/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016866 * Return TRUE if typeval "tv" is set to be locked (immutable).
16867 * Also give an error message, using "name".
16868 */
16869 static int
16870tv_check_lock(lock, name)
16871 int lock;
16872 char_u *name;
16873{
16874 if (lock & VAR_LOCKED)
16875 {
16876 EMSG2(_("E741: Value is locked: %s"),
16877 name == NULL ? (char_u *)_("Unknown") : name);
16878 return TRUE;
16879 }
16880 if (lock & VAR_FIXED)
16881 {
16882 EMSG2(_("E742: Cannot change value of %s"),
16883 name == NULL ? (char_u *)_("Unknown") : name);
16884 return TRUE;
16885 }
16886 return FALSE;
16887}
16888
16889/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016890 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016891 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016892 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016895copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016896 typval_T *from;
16897 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016899 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016900 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016901 switch (from->v_type)
16902 {
16903 case VAR_NUMBER:
16904 to->vval.v_number = from->vval.v_number;
16905 break;
16906 case VAR_STRING:
16907 case VAR_FUNC:
16908 if (from->vval.v_string == NULL)
16909 to->vval.v_string = NULL;
16910 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016911 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016912 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016913 if (from->v_type == VAR_FUNC)
16914 func_ref(to->vval.v_string);
16915 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016916 break;
16917 case VAR_LIST:
16918 if (from->vval.v_list == NULL)
16919 to->vval.v_list = NULL;
16920 else
16921 {
16922 to->vval.v_list = from->vval.v_list;
16923 ++to->vval.v_list->lv_refcount;
16924 }
16925 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016926 case VAR_DICT:
16927 if (from->vval.v_dict == NULL)
16928 to->vval.v_dict = NULL;
16929 else
16930 {
16931 to->vval.v_dict = from->vval.v_dict;
16932 ++to->vval.v_dict->dv_refcount;
16933 }
16934 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016935 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016936 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016937 break;
16938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939}
16940
16941/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016942 * Make a copy of an item.
16943 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016944 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16945 * reference to an already copied list/dict can be used.
16946 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016947 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016948 static int
16949item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016950 typval_T *from;
16951 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016952 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016953 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016954{
16955 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016956 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016957
Bram Moolenaar33570922005-01-25 22:26:29 +000016958 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016959 {
16960 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016961 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016962 }
16963 ++recurse;
16964
16965 switch (from->v_type)
16966 {
16967 case VAR_NUMBER:
16968 case VAR_STRING:
16969 case VAR_FUNC:
16970 copy_tv(from, to);
16971 break;
16972 case VAR_LIST:
16973 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016974 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016975 if (from->vval.v_list == NULL)
16976 to->vval.v_list = NULL;
16977 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16978 {
16979 /* use the copy made earlier */
16980 to->vval.v_list = from->vval.v_list->lv_copylist;
16981 ++to->vval.v_list->lv_refcount;
16982 }
16983 else
16984 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16985 if (to->vval.v_list == NULL)
16986 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016987 break;
16988 case VAR_DICT:
16989 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016990 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016991 if (from->vval.v_dict == NULL)
16992 to->vval.v_dict = NULL;
16993 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16994 {
16995 /* use the copy made earlier */
16996 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16997 ++to->vval.v_dict->dv_refcount;
16998 }
16999 else
17000 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17001 if (to->vval.v_dict == NULL)
17002 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017003 break;
17004 default:
17005 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017006 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017007 }
17008 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017009 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017010}
17011
17012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 * ":echo expr1 ..." print each argument separated with a space, add a
17014 * newline at the end.
17015 * ":echon expr1 ..." print each argument plain.
17016 */
17017 void
17018ex_echo(eap)
17019 exarg_T *eap;
17020{
17021 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017022 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017023 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 char_u *p;
17025 int needclr = TRUE;
17026 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017027 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028
17029 if (eap->skip)
17030 ++emsg_skip;
17031 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17032 {
17033 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017034 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035 {
17036 /*
17037 * Report the invalid expression unless the expression evaluation
17038 * has been cancelled due to an aborting error, an interrupt, or an
17039 * exception.
17040 */
17041 if (!aborting())
17042 EMSG2(_(e_invexpr2), p);
17043 break;
17044 }
17045 if (!eap->skip)
17046 {
17047 if (atstart)
17048 {
17049 atstart = FALSE;
17050 /* Call msg_start() after eval1(), evaluating the expression
17051 * may cause a message to appear. */
17052 if (eap->cmdidx == CMD_echo)
17053 msg_start();
17054 }
17055 else if (eap->cmdidx == CMD_echo)
17056 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017057 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017058 if (p != NULL)
17059 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017061 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017063 if (*p != TAB && needclr)
17064 {
17065 /* remove any text still there from the command */
17066 msg_clr_eos();
17067 needclr = FALSE;
17068 }
17069 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 }
17071 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017072 {
17073#ifdef FEAT_MBYTE
17074 if (has_mbyte)
17075 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017076 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017077
17078 (void)msg_outtrans_len_attr(p, i, echo_attr);
17079 p += i - 1;
17080 }
17081 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017083 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017086 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017088 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089 arg = skipwhite(arg);
17090 }
17091 eap->nextcmd = check_nextcmd(arg);
17092
17093 if (eap->skip)
17094 --emsg_skip;
17095 else
17096 {
17097 /* remove text that may still be there from the command */
17098 if (needclr)
17099 msg_clr_eos();
17100 if (eap->cmdidx == CMD_echo)
17101 msg_end();
17102 }
17103}
17104
17105/*
17106 * ":echohl {name}".
17107 */
17108 void
17109ex_echohl(eap)
17110 exarg_T *eap;
17111{
17112 int id;
17113
17114 id = syn_name2id(eap->arg);
17115 if (id == 0)
17116 echo_attr = 0;
17117 else
17118 echo_attr = syn_id2attr(id);
17119}
17120
17121/*
17122 * ":execute expr1 ..." execute the result of an expression.
17123 * ":echomsg expr1 ..." Print a message
17124 * ":echoerr expr1 ..." Print an error
17125 * Each gets spaces around each argument and a newline at the end for
17126 * echo commands
17127 */
17128 void
17129ex_execute(eap)
17130 exarg_T *eap;
17131{
17132 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017133 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134 int ret = OK;
17135 char_u *p;
17136 garray_T ga;
17137 int len;
17138 int save_did_emsg;
17139
17140 ga_init2(&ga, 1, 80);
17141
17142 if (eap->skip)
17143 ++emsg_skip;
17144 while (*arg != NUL && *arg != '|' && *arg != '\n')
17145 {
17146 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017147 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 {
17149 /*
17150 * Report the invalid expression unless the expression evaluation
17151 * has been cancelled due to an aborting error, an interrupt, or an
17152 * exception.
17153 */
17154 if (!aborting())
17155 EMSG2(_(e_invexpr2), p);
17156 ret = FAIL;
17157 break;
17158 }
17159
17160 if (!eap->skip)
17161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017162 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 len = (int)STRLEN(p);
17164 if (ga_grow(&ga, len + 2) == FAIL)
17165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017166 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 ret = FAIL;
17168 break;
17169 }
17170 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173 ga.ga_len += len;
17174 }
17175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017176 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177 arg = skipwhite(arg);
17178 }
17179
17180 if (ret != FAIL && ga.ga_data != NULL)
17181 {
17182 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017185 out_flush();
17186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 else if (eap->cmdidx == CMD_echoerr)
17188 {
17189 /* We don't want to abort following commands, restore did_emsg. */
17190 save_did_emsg = did_emsg;
17191 EMSG((char_u *)ga.ga_data);
17192 if (!force_abort)
17193 did_emsg = save_did_emsg;
17194 }
17195 else if (eap->cmdidx == CMD_execute)
17196 do_cmdline((char_u *)ga.ga_data,
17197 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17198 }
17199
17200 ga_clear(&ga);
17201
17202 if (eap->skip)
17203 --emsg_skip;
17204
17205 eap->nextcmd = check_nextcmd(arg);
17206}
17207
17208/*
17209 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17210 * "arg" points to the "&" or '+' when called, to "option" when returning.
17211 * Returns NULL when no option name found. Otherwise pointer to the char
17212 * after the option name.
17213 */
17214 static char_u *
17215find_option_end(arg, opt_flags)
17216 char_u **arg;
17217 int *opt_flags;
17218{
17219 char_u *p = *arg;
17220
17221 ++p;
17222 if (*p == 'g' && p[1] == ':')
17223 {
17224 *opt_flags = OPT_GLOBAL;
17225 p += 2;
17226 }
17227 else if (*p == 'l' && p[1] == ':')
17228 {
17229 *opt_flags = OPT_LOCAL;
17230 p += 2;
17231 }
17232 else
17233 *opt_flags = 0;
17234
17235 if (!ASCII_ISALPHA(*p))
17236 return NULL;
17237 *arg = p;
17238
17239 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17240 p += 4; /* termcap option */
17241 else
17242 while (ASCII_ISALPHA(*p))
17243 ++p;
17244 return p;
17245}
17246
17247/*
17248 * ":function"
17249 */
17250 void
17251ex_function(eap)
17252 exarg_T *eap;
17253{
17254 char_u *theline;
17255 int j;
17256 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 char_u *name = NULL;
17259 char_u *p;
17260 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017261 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262 garray_T newargs;
17263 garray_T newlines;
17264 int varargs = FALSE;
17265 int mustend = FALSE;
17266 int flags = 0;
17267 ufunc_T *fp;
17268 int indent;
17269 int nesting;
17270 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017271 dictitem_T *v;
17272 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017273 static int func_nr = 0; /* number for nameless function */
17274 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017275 hashtab_T *ht;
17276 int todo;
17277 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278
17279 /*
17280 * ":function" without argument: list functions.
17281 */
17282 if (ends_excmd(*eap->arg))
17283 {
17284 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017285 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017286 todo = func_hashtab.ht_used;
17287 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017288 {
17289 if (!HASHITEM_EMPTY(hi))
17290 {
17291 --todo;
17292 fp = HI2UF(hi);
17293 if (!isdigit(*fp->uf_name))
17294 list_func_head(fp, FALSE);
17295 }
17296 }
17297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 eap->nextcmd = check_nextcmd(eap->arg);
17299 return;
17300 }
17301
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017302 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017303 * ":function /pat": list functions matching pattern.
17304 */
17305 if (*eap->arg == '/')
17306 {
17307 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17308 if (!eap->skip)
17309 {
17310 regmatch_T regmatch;
17311
17312 c = *p;
17313 *p = NUL;
17314 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17315 *p = c;
17316 if (regmatch.regprog != NULL)
17317 {
17318 regmatch.rm_ic = p_ic;
17319
17320 todo = func_hashtab.ht_used;
17321 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17322 {
17323 if (!HASHITEM_EMPTY(hi))
17324 {
17325 --todo;
17326 fp = HI2UF(hi);
17327 if (!isdigit(*fp->uf_name)
17328 && vim_regexec(&regmatch, fp->uf_name, 0))
17329 list_func_head(fp, FALSE);
17330 }
17331 }
17332 }
17333 }
17334 if (*p == '/')
17335 ++p;
17336 eap->nextcmd = check_nextcmd(p);
17337 return;
17338 }
17339
17340 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017341 * Get the function name. There are these situations:
17342 * func normal function name
17343 * "name" == func, "fudi.fd_dict" == NULL
17344 * dict.func new dictionary entry
17345 * "name" == NULL, "fudi.fd_dict" set,
17346 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17347 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017348 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017349 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17350 * dict.func existing dict entry that's not a Funcref
17351 * "name" == NULL, "fudi.fd_dict" set,
17352 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017355 name = trans_function_name(&p, eap->skip, 0, &fudi);
17356 paren = (vim_strchr(p, '(') != NULL);
17357 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358 {
17359 /*
17360 * Return on an invalid expression in braces, unless the expression
17361 * evaluation has been cancelled due to an aborting error, an
17362 * interrupt, or an exception.
17363 */
17364 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017365 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017366 if (!eap->skip && fudi.fd_newkey != NULL)
17367 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017368 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371 else
17372 eap->skip = TRUE;
17373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374 /* An error in a function call during evaluation of an expression in magic
17375 * braces should not cause the function not to be defined. */
17376 saved_did_emsg = did_emsg;
17377 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378
17379 /*
17380 * ":function func" with only function name: list function.
17381 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017382 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 {
17384 if (!ends_excmd(*skipwhite(p)))
17385 {
17386 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017387 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 }
17389 eap->nextcmd = check_nextcmd(p);
17390 if (eap->nextcmd != NULL)
17391 *p = NUL;
17392 if (!eap->skip && !got_int)
17393 {
17394 fp = find_func(name);
17395 if (fp != NULL)
17396 {
17397 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017398 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 {
17400 msg_putchar('\n');
17401 msg_outnum((long)(j + 1));
17402 if (j < 9)
17403 msg_putchar(' ');
17404 if (j < 99)
17405 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017406 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 out_flush(); /* show a line at a time */
17408 ui_breakcheck();
17409 }
17410 if (!got_int)
17411 {
17412 msg_putchar('\n');
17413 msg_puts((char_u *)" endfunction");
17414 }
17415 }
17416 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017417 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017418 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017419 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420 }
17421
17422 /*
17423 * ":function name(arg1, arg2)" Define function.
17424 */
17425 p = skipwhite(p);
17426 if (*p != '(')
17427 {
17428 if (!eap->skip)
17429 {
17430 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017431 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432 }
17433 /* attempt to continue by skipping some text */
17434 if (vim_strchr(p, '(') != NULL)
17435 p = vim_strchr(p, '(');
17436 }
17437 p = skipwhite(p + 1);
17438
17439 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17440 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17441
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017442 if (!eap->skip)
17443 {
17444 /* Check the name of the function. */
17445 if (name != NULL)
17446 arg = name;
17447 else
17448 arg = fudi.fd_newkey;
17449 if (arg != NULL)
17450 {
17451 if (*arg == K_SPECIAL)
17452 j = 3;
17453 else
17454 j = 0;
17455 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17456 : eval_isnamec(arg[j])))
17457 ++j;
17458 if (arg[j] != NUL)
17459 emsg_funcname(_(e_invarg2), arg);
17460 }
17461 }
17462
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463 /*
17464 * Isolate the arguments: "arg1, arg2, ...)"
17465 */
17466 while (*p != ')')
17467 {
17468 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17469 {
17470 varargs = TRUE;
17471 p += 3;
17472 mustend = TRUE;
17473 }
17474 else
17475 {
17476 arg = p;
17477 while (ASCII_ISALNUM(*p) || *p == '_')
17478 ++p;
17479 if (arg == p || isdigit(*arg)
17480 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17481 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17482 {
17483 if (!eap->skip)
17484 EMSG2(_("E125: Illegal argument: %s"), arg);
17485 break;
17486 }
17487 if (ga_grow(&newargs, 1) == FAIL)
17488 goto erret;
17489 c = *p;
17490 *p = NUL;
17491 arg = vim_strsave(arg);
17492 if (arg == NULL)
17493 goto erret;
17494 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17495 *p = c;
17496 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 if (*p == ',')
17498 ++p;
17499 else
17500 mustend = TRUE;
17501 }
17502 p = skipwhite(p);
17503 if (mustend && *p != ')')
17504 {
17505 if (!eap->skip)
17506 EMSG2(_(e_invarg2), eap->arg);
17507 break;
17508 }
17509 }
17510 ++p; /* skip the ')' */
17511
Bram Moolenaare9a41262005-01-15 22:18:47 +000017512 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513 for (;;)
17514 {
17515 p = skipwhite(p);
17516 if (STRNCMP(p, "range", 5) == 0)
17517 {
17518 flags |= FC_RANGE;
17519 p += 5;
17520 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017521 else if (STRNCMP(p, "dict", 4) == 0)
17522 {
17523 flags |= FC_DICT;
17524 p += 4;
17525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 else if (STRNCMP(p, "abort", 5) == 0)
17527 {
17528 flags |= FC_ABORT;
17529 p += 5;
17530 }
17531 else
17532 break;
17533 }
17534
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017535 /* When there is a line break use what follows for the function body.
17536 * Makes 'exe "func Test()\n...\nendfunc"' work. */
17537 if (*p == '\n')
17538 line_arg = p + 1;
17539 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540 EMSG(_(e_trailing));
17541
17542 /*
17543 * Read the body of the function, until ":endfunction" is found.
17544 */
17545 if (KeyTyped)
17546 {
17547 /* Check if the function already exists, don't let the user type the
17548 * whole function before telling him it doesn't work! For a script we
17549 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017550 if (!eap->skip && !eap->forceit)
17551 {
17552 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17553 EMSG(_(e_funcdict));
17554 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017555 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017558 if (!eap->skip && did_emsg)
17559 goto erret;
17560
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561 msg_putchar('\n'); /* don't overwrite the function name */
17562 cmdline_row = msg_row;
17563 }
17564
17565 indent = 2;
17566 nesting = 0;
17567 for (;;)
17568 {
17569 msg_scroll = TRUE;
17570 need_wait_return = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017571 if (line_arg != NULL)
17572 {
17573 /* Use eap->arg, split up in parts by line breaks. */
17574 theline = line_arg;
17575 p = vim_strchr(theline, '\n');
17576 if (p == NULL)
17577 line_arg += STRLEN(line_arg);
17578 else
17579 {
17580 *p = NUL;
17581 line_arg = p + 1;
17582 }
17583 }
17584 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585 theline = getcmdline(':', 0L, indent);
17586 else
17587 theline = eap->getline(':', eap->cookie, indent);
17588 if (KeyTyped)
17589 lines_left = Rows - 1;
17590 if (theline == NULL)
17591 {
17592 EMSG(_("E126: Missing :endfunction"));
17593 goto erret;
17594 }
17595
17596 if (skip_until != NULL)
17597 {
17598 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17599 * don't check for ":endfunc". */
17600 if (STRCMP(theline, skip_until) == 0)
17601 {
17602 vim_free(skip_until);
17603 skip_until = NULL;
17604 }
17605 }
17606 else
17607 {
17608 /* skip ':' and blanks*/
17609 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17610 ;
17611
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017612 /* Check for "endfunction". */
17613 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017615 if (line_arg == NULL)
17616 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617 break;
17618 }
17619
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017620 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 * at "end". */
17622 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17623 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017624 else if (STRNCMP(p, "if", 2) == 0
17625 || STRNCMP(p, "wh", 2) == 0
17626 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 || STRNCMP(p, "try", 3) == 0)
17628 indent += 2;
17629
17630 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017631 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017633 if (*p == '!')
17634 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635 p += eval_fname_script(p);
17636 if (ASCII_ISALPHA(*p))
17637 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017638 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639 if (*skipwhite(p) == '(')
17640 {
17641 ++nesting;
17642 indent += 2;
17643 }
17644 }
17645 }
17646
17647 /* Check for ":append" or ":insert". */
17648 p = skip_range(p, NULL);
17649 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17650 || (p[0] == 'i'
17651 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17652 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17653 skip_until = vim_strsave((char_u *)".");
17654
17655 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17656 arg = skipwhite(skiptowhite(p));
17657 if (arg[0] == '<' && arg[1] =='<'
17658 && ((p[0] == 'p' && p[1] == 'y'
17659 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17660 || (p[0] == 'p' && p[1] == 'e'
17661 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17662 || (p[0] == 't' && p[1] == 'c'
17663 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17664 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17665 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017666 || (p[0] == 'm' && p[1] == 'z'
17667 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 ))
17669 {
17670 /* ":python <<" continues until a dot, like ":append" */
17671 p = skipwhite(arg + 2);
17672 if (*p == NUL)
17673 skip_until = vim_strsave((char_u *)".");
17674 else
17675 skip_until = vim_strsave(p);
17676 }
17677 }
17678
17679 /* Add the line to the function. */
17680 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017681 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017682 if (line_arg == NULL)
17683 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017685 }
17686
17687 /* Copy the line to newly allocated memory. get_one_sourceline()
17688 * allocates 250 bytes per line, this saves 80% on average. The cost
17689 * is an extra alloc/free. */
17690 p = vim_strsave(theline);
17691 if (p != NULL)
17692 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017693 if (line_arg == NULL)
17694 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017695 theline = p;
17696 }
17697
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17699 newlines.ga_len++;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017700
17701 /* Check for end of eap->arg. */
17702 if (line_arg != NULL && *line_arg == NUL)
17703 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 }
17705
17706 /* Don't define the function when skipping commands or when an error was
17707 * detected. */
17708 if (eap->skip || did_emsg)
17709 goto erret;
17710
17711 /*
17712 * If there are no errors, add the function
17713 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017714 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017715 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017716 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017717 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017718 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017719 emsg_funcname("E707: Function name conflicts with variable: %s",
17720 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017721 goto erret;
17722 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017723
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017724 fp = find_func(name);
17725 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017727 if (!eap->forceit)
17728 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017729 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017730 goto erret;
17731 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017732 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017733 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017734 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017735 name);
17736 goto erret;
17737 }
17738 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017739 ga_clear_strings(&(fp->uf_args));
17740 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017741 vim_free(name);
17742 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 }
17745 else
17746 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017747 char numbuf[20];
17748
17749 fp = NULL;
17750 if (fudi.fd_newkey == NULL && !eap->forceit)
17751 {
17752 EMSG(_(e_funcdict));
17753 goto erret;
17754 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017755 if (fudi.fd_di == NULL)
17756 {
17757 /* Can't add a function to a locked dictionary */
17758 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17759 goto erret;
17760 }
17761 /* Can't change an existing function if it is locked */
17762 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17763 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017764
17765 /* Give the function a sequential number. Can only be used with a
17766 * Funcref! */
17767 vim_free(name);
17768 sprintf(numbuf, "%d", ++func_nr);
17769 name = vim_strsave((char_u *)numbuf);
17770 if (name == NULL)
17771 goto erret;
17772 }
17773
17774 if (fp == NULL)
17775 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017776 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017777 {
17778 int slen, plen;
17779 char_u *scriptname;
17780
17781 /* Check that the autoload name matches the script name. */
17782 j = FAIL;
17783 if (sourcing_name != NULL)
17784 {
17785 scriptname = autoload_name(name);
17786 if (scriptname != NULL)
17787 {
17788 p = vim_strchr(scriptname, '/');
17789 plen = STRLEN(p);
17790 slen = STRLEN(sourcing_name);
17791 if (slen > plen && fnamecmp(p,
17792 sourcing_name + slen - plen) == 0)
17793 j = OK;
17794 vim_free(scriptname);
17795 }
17796 }
17797 if (j == FAIL)
17798 {
17799 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17800 goto erret;
17801 }
17802 }
17803
17804 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805 if (fp == NULL)
17806 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017807
17808 if (fudi.fd_dict != NULL)
17809 {
17810 if (fudi.fd_di == NULL)
17811 {
17812 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017813 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017814 if (fudi.fd_di == NULL)
17815 {
17816 vim_free(fp);
17817 goto erret;
17818 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017819 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17820 {
17821 vim_free(fudi.fd_di);
17822 goto erret;
17823 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017824 }
17825 else
17826 /* overwrite existing dict entry */
17827 clear_tv(&fudi.fd_di->di_tv);
17828 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017829 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017830 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017831 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017832 }
17833
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017835 STRCPY(fp->uf_name, name);
17836 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017838 fp->uf_args = newargs;
17839 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017840#ifdef FEAT_PROFILE
17841 fp->uf_tml_count = NULL;
17842 fp->uf_tml_total = NULL;
17843 fp->uf_tml_self = NULL;
17844 fp->uf_profiling = FALSE;
17845 if (prof_def_func())
17846 func_do_profile(fp);
17847#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017848 fp->uf_varargs = varargs;
17849 fp->uf_flags = flags;
17850 fp->uf_calls = 0;
17851 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017852 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853
17854erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 ga_clear_strings(&newargs);
17856 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017857ret_free:
17858 vim_free(skip_until);
17859 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862}
17863
17864/*
17865 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017866 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017868 * flags:
17869 * TFN_INT: internal function name OK
17870 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 * Advances "pp" to just after the function name (if no error).
17872 */
17873 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017874trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 char_u **pp;
17876 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017877 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017878 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017880 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017881 char_u *start;
17882 char_u *end;
17883 int lead;
17884 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017886 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017887
17888 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017889 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017891
17892 /* Check for hard coded <SNR>: already translated function ID (from a user
17893 * command). */
17894 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17895 && (*pp)[2] == (int)KE_SNR)
17896 {
17897 *pp += 3;
17898 len = get_id_len(pp) + 3;
17899 return vim_strnsave(start, len);
17900 }
17901
17902 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17903 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017905 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017907
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017908 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17909 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017910 if (end == start)
17911 {
17912 if (!skip)
17913 EMSG(_("E129: Function name required"));
17914 goto theend;
17915 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017916 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017917 {
17918 /*
17919 * Report an invalid expression in braces, unless the expression
17920 * evaluation has been cancelled due to an aborting error, an
17921 * interrupt, or an exception.
17922 */
17923 if (!aborting())
17924 {
17925 if (end != NULL)
17926 EMSG2(_(e_invarg2), start);
17927 }
17928 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017929 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017930 goto theend;
17931 }
17932
17933 if (lv.ll_tv != NULL)
17934 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017935 if (fdp != NULL)
17936 {
17937 fdp->fd_dict = lv.ll_dict;
17938 fdp->fd_newkey = lv.ll_newkey;
17939 lv.ll_newkey = NULL;
17940 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017941 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017942 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17943 {
17944 name = vim_strsave(lv.ll_tv->vval.v_string);
17945 *pp = end;
17946 }
17947 else
17948 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017949 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17950 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017951 EMSG(_(e_funcref));
17952 else
17953 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017954 name = NULL;
17955 }
17956 goto theend;
17957 }
17958
17959 if (lv.ll_name == NULL)
17960 {
17961 /* Error found, but continue after the function name. */
17962 *pp = end;
17963 goto theend;
17964 }
17965
17966 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000017967 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017968 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000017969 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
17970 && STRNCMP(lv.ll_name, "s:", 2) == 0)
17971 {
17972 /* When there was "s:" already or the name expanded to get a
17973 * leading "s:" then remove it. */
17974 lv.ll_name += 2;
17975 len -= 2;
17976 lead = 2;
17977 }
17978 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017979 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017980 {
17981 if (lead == 2) /* skip over "s:" */
17982 lv.ll_name += 2;
17983 len = (int)(end - lv.ll_name);
17984 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017985
17986 /*
17987 * Copy the function name to allocated memory.
17988 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17989 * Accept <SNR>123_name() outside a script.
17990 */
17991 if (skip)
17992 lead = 0; /* do nothing */
17993 else if (lead > 0)
17994 {
17995 lead = 3;
17996 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17997 {
17998 if (current_SID <= 0)
17999 {
18000 EMSG(_(e_usingsid));
18001 goto theend;
18002 }
18003 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18004 lead += (int)STRLEN(sid_buf);
18005 }
18006 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018007 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018008 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018009 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018010 goto theend;
18011 }
18012 name = alloc((unsigned)(len + lead + 1));
18013 if (name != NULL)
18014 {
18015 if (lead > 0)
18016 {
18017 name[0] = K_SPECIAL;
18018 name[1] = KS_EXTRA;
18019 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018020 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018021 STRCPY(name + 3, sid_buf);
18022 }
18023 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18024 name[len + lead] = NUL;
18025 }
18026 *pp = end;
18027
18028theend:
18029 clear_lval(&lv);
18030 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031}
18032
18033/*
18034 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18035 * Return 2 if "p" starts with "s:".
18036 * Return 0 otherwise.
18037 */
18038 static int
18039eval_fname_script(p)
18040 char_u *p;
18041{
18042 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18043 || STRNICMP(p + 1, "SNR>", 4) == 0))
18044 return 5;
18045 if (p[0] == 's' && p[1] == ':')
18046 return 2;
18047 return 0;
18048}
18049
18050/*
18051 * Return TRUE if "p" starts with "<SID>" or "s:".
18052 * Only works if eval_fname_script() returned non-zero for "p"!
18053 */
18054 static int
18055eval_fname_sid(p)
18056 char_u *p;
18057{
18058 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18059}
18060
18061/*
18062 * List the head of the function: "name(arg1, arg2)".
18063 */
18064 static void
18065list_func_head(fp, indent)
18066 ufunc_T *fp;
18067 int indent;
18068{
18069 int j;
18070
18071 msg_start();
18072 if (indent)
18073 MSG_PUTS(" ");
18074 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018075 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076 {
18077 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018078 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 }
18080 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018081 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018083 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084 {
18085 if (j)
18086 MSG_PUTS(", ");
18087 msg_puts(FUNCARG(fp, j));
18088 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018089 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090 {
18091 if (j)
18092 MSG_PUTS(", ");
18093 MSG_PUTS("...");
18094 }
18095 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018096 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018097 if (p_verbose > 0)
18098 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099}
18100
18101/*
18102 * Find a function by name, return pointer to it in ufuncs.
18103 * Return NULL for unknown function.
18104 */
18105 static ufunc_T *
18106find_func(name)
18107 char_u *name;
18108{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018109 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018111 hi = hash_find(&func_hashtab, name);
18112 if (!HASHITEM_EMPTY(hi))
18113 return HI2UF(hi);
18114 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115}
18116
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018117#if defined(EXITFREE) || defined(PROTO)
18118 void
18119free_all_functions()
18120{
18121 hashitem_T *hi;
18122
18123 /* Need to start all over every time, because func_free() may change the
18124 * hash table. */
18125 while (func_hashtab.ht_used > 0)
18126 for (hi = func_hashtab.ht_array; ; ++hi)
18127 if (!HASHITEM_EMPTY(hi))
18128 {
18129 func_free(HI2UF(hi));
18130 break;
18131 }
18132}
18133#endif
18134
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018135/*
18136 * Return TRUE if a function "name" exists.
18137 */
18138 static int
18139function_exists(name)
18140 char_u *name;
18141{
18142 char_u *p = name;
18143 int n = FALSE;
18144
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018145 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018146 if (p != NULL)
18147 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018148 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018149 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018150 else
18151 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018152 vim_free(p);
18153 }
18154 return n;
18155}
18156
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018157/*
18158 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018159 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018160 */
18161 static int
18162builtin_function(name)
18163 char_u *name;
18164{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018165 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18166 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018167}
18168
Bram Moolenaar05159a02005-02-26 23:04:13 +000018169#if defined(FEAT_PROFILE) || defined(PROTO)
18170/*
18171 * Start profiling function "fp".
18172 */
18173 static void
18174func_do_profile(fp)
18175 ufunc_T *fp;
18176{
18177 fp->uf_tm_count = 0;
18178 profile_zero(&fp->uf_tm_self);
18179 profile_zero(&fp->uf_tm_total);
18180 if (fp->uf_tml_count == NULL)
18181 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18182 (sizeof(int) * fp->uf_lines.ga_len));
18183 if (fp->uf_tml_total == NULL)
18184 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18185 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18186 if (fp->uf_tml_self == NULL)
18187 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18188 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18189 fp->uf_tml_idx = -1;
18190 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18191 || fp->uf_tml_self == NULL)
18192 return; /* out of memory */
18193
18194 fp->uf_profiling = TRUE;
18195}
18196
18197/*
18198 * Dump the profiling results for all functions in file "fd".
18199 */
18200 void
18201func_dump_profile(fd)
18202 FILE *fd;
18203{
18204 hashitem_T *hi;
18205 int todo;
18206 ufunc_T *fp;
18207 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018208 ufunc_T **sorttab;
18209 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018210
18211 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018212 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18213
Bram Moolenaar05159a02005-02-26 23:04:13 +000018214 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18215 {
18216 if (!HASHITEM_EMPTY(hi))
18217 {
18218 --todo;
18219 fp = HI2UF(hi);
18220 if (fp->uf_profiling)
18221 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018222 if (sorttab != NULL)
18223 sorttab[st_len++] = fp;
18224
Bram Moolenaar05159a02005-02-26 23:04:13 +000018225 if (fp->uf_name[0] == K_SPECIAL)
18226 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18227 else
18228 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18229 if (fp->uf_tm_count == 1)
18230 fprintf(fd, "Called 1 time\n");
18231 else
18232 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18233 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18234 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18235 fprintf(fd, "\n");
18236 fprintf(fd, "count total (s) self (s)\n");
18237
18238 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18239 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018240 prof_func_line(fd, fp->uf_tml_count[i],
18241 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018242 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18243 }
18244 fprintf(fd, "\n");
18245 }
18246 }
18247 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018248
18249 if (sorttab != NULL && st_len > 0)
18250 {
18251 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18252 prof_total_cmp);
18253 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18254 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18255 prof_self_cmp);
18256 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18257 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018258}
Bram Moolenaar73830342005-02-28 22:48:19 +000018259
18260 static void
18261prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18262 FILE *fd;
18263 ufunc_T **sorttab;
18264 int st_len;
18265 char *title;
18266 int prefer_self; /* when equal print only self time */
18267{
18268 int i;
18269 ufunc_T *fp;
18270
18271 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18272 fprintf(fd, "count total (s) self (s) function\n");
18273 for (i = 0; i < 20 && i < st_len; ++i)
18274 {
18275 fp = sorttab[i];
18276 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18277 prefer_self);
18278 if (fp->uf_name[0] == K_SPECIAL)
18279 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18280 else
18281 fprintf(fd, " %s()\n", fp->uf_name);
18282 }
18283 fprintf(fd, "\n");
18284}
18285
18286/*
18287 * Print the count and times for one function or function line.
18288 */
18289 static void
18290prof_func_line(fd, count, total, self, prefer_self)
18291 FILE *fd;
18292 int count;
18293 proftime_T *total;
18294 proftime_T *self;
18295 int prefer_self; /* when equal print only self time */
18296{
18297 if (count > 0)
18298 {
18299 fprintf(fd, "%5d ", count);
18300 if (prefer_self && profile_equal(total, self))
18301 fprintf(fd, " ");
18302 else
18303 fprintf(fd, "%s ", profile_msg(total));
18304 if (!prefer_self && profile_equal(total, self))
18305 fprintf(fd, " ");
18306 else
18307 fprintf(fd, "%s ", profile_msg(self));
18308 }
18309 else
18310 fprintf(fd, " ");
18311}
18312
18313/*
18314 * Compare function for total time sorting.
18315 */
18316 static int
18317#ifdef __BORLANDC__
18318_RTLENTRYF
18319#endif
18320prof_total_cmp(s1, s2)
18321 const void *s1;
18322 const void *s2;
18323{
18324 ufunc_T *p1, *p2;
18325
18326 p1 = *(ufunc_T **)s1;
18327 p2 = *(ufunc_T **)s2;
18328 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18329}
18330
18331/*
18332 * Compare function for self time sorting.
18333 */
18334 static int
18335#ifdef __BORLANDC__
18336_RTLENTRYF
18337#endif
18338prof_self_cmp(s1, s2)
18339 const void *s1;
18340 const void *s2;
18341{
18342 ufunc_T *p1, *p2;
18343
18344 p1 = *(ufunc_T **)s1;
18345 p2 = *(ufunc_T **)s2;
18346 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18347}
18348
Bram Moolenaar05159a02005-02-26 23:04:13 +000018349#endif
18350
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018351/* The names of packages that once were loaded is remembered. */
18352static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18353
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018354/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018355 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018356 * Return TRUE if a package was loaded.
18357 */
18358 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018359script_autoload(name, reload)
18360 char_u *name;
18361 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018362{
18363 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018364 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018365 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018366 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018367
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018368 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018369 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018370 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018371 return FALSE;
18372
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018373 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018374
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018375 /* Find the name in the list of previously loaded package names. Skip
18376 * "autoload/", it's always the same. */
18377 for (i = 0; i < ga_loaded.ga_len; ++i)
18378 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18379 break;
18380 if (!reload && i < ga_loaded.ga_len)
18381 ret = FALSE; /* was loaded already */
18382 else
18383 {
18384 /* Remember the name if it wasn't loaded already. */
18385 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18386 {
18387 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18388 tofree = NULL;
18389 }
18390
18391 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018392 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018393 ret = TRUE;
18394 }
18395
18396 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018397 return ret;
18398}
18399
18400/*
18401 * Return the autoload script name for a function or variable name.
18402 * Returns NULL when out of memory.
18403 */
18404 static char_u *
18405autoload_name(name)
18406 char_u *name;
18407{
18408 char_u *p;
18409 char_u *scriptname;
18410
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018411 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018412 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18413 if (scriptname == NULL)
18414 return FALSE;
18415 STRCPY(scriptname, "autoload/");
18416 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018417 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018418 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018419 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018420 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018421 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018422}
18423
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18425
18426/*
18427 * Function given to ExpandGeneric() to obtain the list of user defined
18428 * function names.
18429 */
18430 char_u *
18431get_user_func_name(xp, idx)
18432 expand_T *xp;
18433 int idx;
18434{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018435 static long_u done;
18436 static hashitem_T *hi;
18437 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438
18439 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018441 done = 0;
18442 hi = func_hashtab.ht_array;
18443 }
18444 if (done < func_hashtab.ht_used)
18445 {
18446 if (done++ > 0)
18447 ++hi;
18448 while (HASHITEM_EMPTY(hi))
18449 ++hi;
18450 fp = HI2UF(hi);
18451
18452 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18453 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454
18455 cat_func_name(IObuff, fp);
18456 if (xp->xp_context != EXPAND_USER_FUNC)
18457 {
18458 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018459 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460 STRCAT(IObuff, ")");
18461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462 return IObuff;
18463 }
18464 return NULL;
18465}
18466
18467#endif /* FEAT_CMDL_COMPL */
18468
18469/*
18470 * Copy the function name of "fp" to buffer "buf".
18471 * "buf" must be able to hold the function name plus three bytes.
18472 * Takes care of script-local function names.
18473 */
18474 static void
18475cat_func_name(buf, fp)
18476 char_u *buf;
18477 ufunc_T *fp;
18478{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018479 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 {
18481 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018482 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 }
18484 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018485 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486}
18487
18488/*
18489 * ":delfunction {name}"
18490 */
18491 void
18492ex_delfunction(eap)
18493 exarg_T *eap;
18494{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018495 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496 char_u *p;
18497 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018498 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499
18500 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018501 name = trans_function_name(&p, eap->skip, 0, &fudi);
18502 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018504 {
18505 if (fudi.fd_dict != NULL && !eap->skip)
18506 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509 if (!ends_excmd(*skipwhite(p)))
18510 {
18511 vim_free(name);
18512 EMSG(_(e_trailing));
18513 return;
18514 }
18515 eap->nextcmd = check_nextcmd(p);
18516 if (eap->nextcmd != NULL)
18517 *p = NUL;
18518
18519 if (!eap->skip)
18520 fp = find_func(name);
18521 vim_free(name);
18522
18523 if (!eap->skip)
18524 {
18525 if (fp == NULL)
18526 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018527 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528 return;
18529 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018530 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531 {
18532 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18533 return;
18534 }
18535
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018536 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018538 /* Delete the dict item that refers to the function, it will
18539 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018540 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018542 else
18543 func_free(fp);
18544 }
18545}
18546
18547/*
18548 * Free a function and remove it from the list of functions.
18549 */
18550 static void
18551func_free(fp)
18552 ufunc_T *fp;
18553{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018554 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018555
18556 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018557 ga_clear_strings(&(fp->uf_args));
18558 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018559#ifdef FEAT_PROFILE
18560 vim_free(fp->uf_tml_count);
18561 vim_free(fp->uf_tml_total);
18562 vim_free(fp->uf_tml_self);
18563#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018564
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018565 /* remove the function from the function hashtable */
18566 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18567 if (HASHITEM_EMPTY(hi))
18568 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018569 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018570 hash_remove(&func_hashtab, hi);
18571
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018572 vim_free(fp);
18573}
18574
18575/*
18576 * Unreference a Function: decrement the reference count and free it when it
18577 * becomes zero. Only for numbered functions.
18578 */
18579 static void
18580func_unref(name)
18581 char_u *name;
18582{
18583 ufunc_T *fp;
18584
18585 if (name != NULL && isdigit(*name))
18586 {
18587 fp = find_func(name);
18588 if (fp == NULL)
18589 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018590 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018591 {
18592 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018593 * when "uf_calls" becomes zero. */
18594 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018595 func_free(fp);
18596 }
18597 }
18598}
18599
18600/*
18601 * Count a reference to a Function.
18602 */
18603 static void
18604func_ref(name)
18605 char_u *name;
18606{
18607 ufunc_T *fp;
18608
18609 if (name != NULL && isdigit(*name))
18610 {
18611 fp = find_func(name);
18612 if (fp == NULL)
18613 EMSG2(_(e_intern2), "func_ref()");
18614 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018615 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 }
18617}
18618
18619/*
18620 * Call a user function.
18621 */
18622 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018623call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 ufunc_T *fp; /* pointer to function */
18625 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018626 typval_T *argvars; /* arguments */
18627 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 linenr_T firstline; /* first line of range */
18629 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018630 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631{
Bram Moolenaar33570922005-01-25 22:26:29 +000018632 char_u *save_sourcing_name;
18633 linenr_T save_sourcing_lnum;
18634 scid_T save_current_SID;
18635 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018636 int save_did_emsg;
18637 static int depth = 0;
18638 dictitem_T *v;
18639 int fixvar_idx = 0; /* index in fixvar[] */
18640 int i;
18641 int ai;
18642 char_u numbuf[NUMBUFLEN];
18643 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018644#ifdef FEAT_PROFILE
18645 proftime_T wait_start;
18646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647
18648 /* If depth of calling is getting too high, don't execute the function */
18649 if (depth >= p_mfd)
18650 {
18651 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018652 rettv->v_type = VAR_NUMBER;
18653 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 return;
18655 }
18656 ++depth;
18657
18658 line_breakcheck(); /* check for CTRL-C hit */
18659
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018660 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018661 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018663 fc.rettv = rettv;
18664 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 fc.linenr = 0;
18666 fc.returned = FALSE;
18667 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018669 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 fc.dbg_tick = debug_tick;
18671
Bram Moolenaar33570922005-01-25 22:26:29 +000018672 /*
18673 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18674 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18675 * each argument variable and saves a lot of time.
18676 */
18677 /*
18678 * Init l: variables.
18679 */
18680 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018681 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018682 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018683 /* Set l:self to "selfdict". */
18684 v = &fc.fixvar[fixvar_idx++].var;
18685 STRCPY(v->di_key, "self");
18686 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18687 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18688 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018689 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018690 v->di_tv.vval.v_dict = selfdict;
18691 ++selfdict->dv_refcount;
18692 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018693
Bram Moolenaar33570922005-01-25 22:26:29 +000018694 /*
18695 * Init a: variables.
18696 * Set a:0 to "argcount".
18697 * Set a:000 to a list with room for the "..." arguments.
18698 */
18699 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18700 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018701 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018702 v = &fc.fixvar[fixvar_idx++].var;
18703 STRCPY(v->di_key, "000");
18704 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18705 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18706 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018707 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018708 v->di_tv.vval.v_list = &fc.l_varlist;
18709 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18710 fc.l_varlist.lv_refcount = 99999;
18711
18712 /*
18713 * Set a:firstline to "firstline" and a:lastline to "lastline".
18714 * Set a:name to named arguments.
18715 * Set a:N to the "..." arguments.
18716 */
18717 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18718 (varnumber_T)firstline);
18719 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18720 (varnumber_T)lastline);
18721 for (i = 0; i < argcount; ++i)
18722 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018723 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018724 if (ai < 0)
18725 /* named argument a:name */
18726 name = FUNCARG(fp, i);
18727 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018728 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018729 /* "..." argument a:1, a:2, etc. */
18730 sprintf((char *)numbuf, "%d", ai + 1);
18731 name = numbuf;
18732 }
18733 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18734 {
18735 v = &fc.fixvar[fixvar_idx++].var;
18736 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18737 }
18738 else
18739 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018740 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18741 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018742 if (v == NULL)
18743 break;
18744 v->di_flags = DI_FLAGS_RO;
18745 }
18746 STRCPY(v->di_key, name);
18747 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18748
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018749 /* Note: the values are copied directly to avoid alloc/free.
18750 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018751 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018752 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018753
18754 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18755 {
18756 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18757 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018758 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018759 }
18760 }
18761
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 /* Don't redraw while executing the function. */
18763 ++RedrawingDisabled;
18764 save_sourcing_name = sourcing_name;
18765 save_sourcing_lnum = sourcing_lnum;
18766 sourcing_lnum = 1;
18767 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018768 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 if (sourcing_name != NULL)
18770 {
18771 if (save_sourcing_name != NULL
18772 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18773 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18774 else
18775 STRCPY(sourcing_name, "function ");
18776 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18777
18778 if (p_verbose >= 12)
18779 {
18780 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018781 verbose_enter_scroll();
18782
Bram Moolenaar555b2802005-05-19 21:08:39 +000018783 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784 if (p_verbose >= 14)
18785 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018787 char_u numbuf[NUMBUFLEN];
18788 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789
18790 msg_puts((char_u *)"(");
18791 for (i = 0; i < argcount; ++i)
18792 {
18793 if (i > 0)
18794 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018795 if (argvars[i].v_type == VAR_NUMBER)
18796 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 else
18798 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018799 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018800 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018802 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803 }
18804 }
18805 msg_puts((char_u *)")");
18806 }
18807 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018808
18809 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810 --no_wait_return;
18811 }
18812 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018813#ifdef FEAT_PROFILE
18814 if (do_profiling)
18815 {
18816 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18817 func_do_profile(fp);
18818 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018819 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018820 {
18821 ++fp->uf_tm_count;
18822 profile_start(&fp->uf_tm_start);
18823 profile_zero(&fp->uf_tm_children);
18824 }
18825 script_prof_save(&wait_start);
18826 }
18827#endif
18828
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018830 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831 save_did_emsg = did_emsg;
18832 did_emsg = FALSE;
18833
18834 /* call do_cmdline() to execute the lines */
18835 do_cmdline(NULL, get_func_line, (void *)&fc,
18836 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18837
18838 --RedrawingDisabled;
18839
18840 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018841 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018843 clear_tv(rettv);
18844 rettv->v_type = VAR_NUMBER;
18845 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846 }
18847
Bram Moolenaar05159a02005-02-26 23:04:13 +000018848#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018849 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018850 {
18851 profile_end(&fp->uf_tm_start);
18852 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18853 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18854 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18855 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018856 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018857 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018858 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18859 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018860 }
18861 }
18862#endif
18863
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864 /* when being verbose, mention the return value */
18865 if (p_verbose >= 12)
18866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018868 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018869
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018871 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018872 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018873 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18874 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018875 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018877 char_u buf[MSG_BUF_LEN];
18878 char_u numbuf[NUMBUFLEN];
18879 char_u *tofree;
18880
Bram Moolenaar555b2802005-05-19 21:08:39 +000018881 /* The value may be very long. Skip the middle part, so that we
18882 * have some idea how it starts and ends. smsg() would always
18883 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018884 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018885 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018886 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018887 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 }
18889 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018890
18891 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 --no_wait_return;
18893 }
18894
18895 vim_free(sourcing_name);
18896 sourcing_name = save_sourcing_name;
18897 sourcing_lnum = save_sourcing_lnum;
18898 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018899#ifdef FEAT_PROFILE
18900 if (do_profiling)
18901 script_prof_restore(&wait_start);
18902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903
18904 if (p_verbose >= 12 && sourcing_name != NULL)
18905 {
18906 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018907 verbose_enter_scroll();
18908
Bram Moolenaar555b2802005-05-19 21:08:39 +000018909 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018911
18912 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913 --no_wait_return;
18914 }
18915
18916 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018917 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918
Bram Moolenaar33570922005-01-25 22:26:29 +000018919 /* The a: variables typevals were not alloced, only free the allocated
18920 * variables. */
18921 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18922
18923 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924 --depth;
18925}
18926
18927/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018928 * Add a number variable "name" to dict "dp" with value "nr".
18929 */
18930 static void
18931add_nr_var(dp, v, name, nr)
18932 dict_T *dp;
18933 dictitem_T *v;
18934 char *name;
18935 varnumber_T nr;
18936{
18937 STRCPY(v->di_key, name);
18938 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18939 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18940 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018941 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018942 v->di_tv.vval.v_number = nr;
18943}
18944
18945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946 * ":return [expr]"
18947 */
18948 void
18949ex_return(eap)
18950 exarg_T *eap;
18951{
18952 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018953 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954 int returning = FALSE;
18955
18956 if (current_funccal == NULL)
18957 {
18958 EMSG(_("E133: :return not inside a function"));
18959 return;
18960 }
18961
18962 if (eap->skip)
18963 ++emsg_skip;
18964
18965 eap->nextcmd = NULL;
18966 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018967 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 {
18969 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018970 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018972 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 }
18974 /* It's safer to return also on error. */
18975 else if (!eap->skip)
18976 {
18977 /*
18978 * Return unless the expression evaluation has been cancelled due to an
18979 * aborting error, an interrupt, or an exception.
18980 */
18981 if (!aborting())
18982 returning = do_return(eap, FALSE, TRUE, NULL);
18983 }
18984
18985 /* When skipping or the return gets pending, advance to the next command
18986 * in this line (!returning). Otherwise, ignore the rest of the line.
18987 * Following lines will be ignored by get_func_line(). */
18988 if (returning)
18989 eap->nextcmd = NULL;
18990 else if (eap->nextcmd == NULL) /* no argument */
18991 eap->nextcmd = check_nextcmd(arg);
18992
18993 if (eap->skip)
18994 --emsg_skip;
18995}
18996
18997/*
18998 * Return from a function. Possibly makes the return pending. Also called
18999 * for a pending return at the ":endtry" or after returning from an extra
19000 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019001 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019002 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 * FALSE when the return gets pending.
19004 */
19005 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019006do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 exarg_T *eap;
19008 int reanimate;
19009 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019010 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011{
19012 int idx;
19013 struct condstack *cstack = eap->cstack;
19014
19015 if (reanimate)
19016 /* Undo the return. */
19017 current_funccal->returned = FALSE;
19018
19019 /*
19020 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19021 * not in its finally clause (which then is to be executed next) is found.
19022 * In this case, make the ":return" pending for execution at the ":endtry".
19023 * Otherwise, return normally.
19024 */
19025 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19026 if (idx >= 0)
19027 {
19028 cstack->cs_pending[idx] = CSTP_RETURN;
19029
19030 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019031 /* A pending return again gets pending. "rettv" points to an
19032 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019034 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 else
19036 {
19037 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019038 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019040 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019042 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043 {
19044 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019045 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019046 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 else
19048 EMSG(_(e_outofmem));
19049 }
19050 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019051 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052
19053 if (reanimate)
19054 {
19055 /* The pending return value could be overwritten by a ":return"
19056 * without argument in a finally clause; reset the default
19057 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019058 current_funccal->rettv->v_type = VAR_NUMBER;
19059 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060 }
19061 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019062 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063 }
19064 else
19065 {
19066 current_funccal->returned = TRUE;
19067
19068 /* If the return is carried out now, store the return value. For
19069 * a return immediately after reanimation, the value is already
19070 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019071 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019073 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019074 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019076 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 }
19078 }
19079
19080 return idx < 0;
19081}
19082
19083/*
19084 * Free the variable with a pending return value.
19085 */
19086 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019087discard_pending_return(rettv)
19088 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089{
Bram Moolenaar33570922005-01-25 22:26:29 +000019090 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091}
19092
19093/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019094 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 * is an allocated string. Used by report_pending() for verbose messages.
19096 */
19097 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019098get_return_cmd(rettv)
19099 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019101 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019102 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019103 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019105 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019106 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019107 if (s == NULL)
19108 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019109
19110 STRCPY(IObuff, ":return ");
19111 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19112 if (STRLEN(s) + 8 >= IOSIZE)
19113 STRCPY(IObuff + IOSIZE - 4, "...");
19114 vim_free(tofree);
19115 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116}
19117
19118/*
19119 * Get next function line.
19120 * Called by do_cmdline() to get the next line.
19121 * Returns allocated string, or NULL for end of function.
19122 */
19123/* ARGSUSED */
19124 char_u *
19125get_func_line(c, cookie, indent)
19126 int c; /* not used */
19127 void *cookie;
19128 int indent; /* not used */
19129{
Bram Moolenaar33570922005-01-25 22:26:29 +000019130 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019131 ufunc_T *fp = fcp->func;
19132 char_u *retval;
19133 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134
19135 /* If breakpoints have been added/deleted need to check for it. */
19136 if (fcp->dbg_tick != debug_tick)
19137 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019138 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139 sourcing_lnum);
19140 fcp->dbg_tick = debug_tick;
19141 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019142#ifdef FEAT_PROFILE
19143 if (do_profiling)
19144 func_line_end(cookie);
19145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019146
Bram Moolenaar05159a02005-02-26 23:04:13 +000019147 gap = &fp->uf_lines;
19148 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019149 retval = NULL;
19150 else if (fcp->returned || fcp->linenr >= gap->ga_len)
19151 retval = NULL;
19152 else
19153 {
19154 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19155 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019156#ifdef FEAT_PROFILE
19157 if (do_profiling)
19158 func_line_start(cookie);
19159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 }
19161
19162 /* Did we encounter a breakpoint? */
19163 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19164 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019165 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019167 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 sourcing_lnum);
19169 fcp->dbg_tick = debug_tick;
19170 }
19171
19172 return retval;
19173}
19174
Bram Moolenaar05159a02005-02-26 23:04:13 +000019175#if defined(FEAT_PROFILE) || defined(PROTO)
19176/*
19177 * Called when starting to read a function line.
19178 * "sourcing_lnum" must be correct!
19179 * When skipping lines it may not actually be executed, but we won't find out
19180 * until later and we need to store the time now.
19181 */
19182 void
19183func_line_start(cookie)
19184 void *cookie;
19185{
19186 funccall_T *fcp = (funccall_T *)cookie;
19187 ufunc_T *fp = fcp->func;
19188
19189 if (fp->uf_profiling && sourcing_lnum >= 1
19190 && sourcing_lnum <= fp->uf_lines.ga_len)
19191 {
19192 fp->uf_tml_idx = sourcing_lnum - 1;
19193 fp->uf_tml_execed = FALSE;
19194 profile_start(&fp->uf_tml_start);
19195 profile_zero(&fp->uf_tml_children);
19196 profile_get_wait(&fp->uf_tml_wait);
19197 }
19198}
19199
19200/*
19201 * Called when actually executing a function line.
19202 */
19203 void
19204func_line_exec(cookie)
19205 void *cookie;
19206{
19207 funccall_T *fcp = (funccall_T *)cookie;
19208 ufunc_T *fp = fcp->func;
19209
19210 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19211 fp->uf_tml_execed = TRUE;
19212}
19213
19214/*
19215 * Called when done with a function line.
19216 */
19217 void
19218func_line_end(cookie)
19219 void *cookie;
19220{
19221 funccall_T *fcp = (funccall_T *)cookie;
19222 ufunc_T *fp = fcp->func;
19223
19224 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19225 {
19226 if (fp->uf_tml_execed)
19227 {
19228 ++fp->uf_tml_count[fp->uf_tml_idx];
19229 profile_end(&fp->uf_tml_start);
19230 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
19231 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
19232 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
19233 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
19234 }
19235 fp->uf_tml_idx = -1;
19236 }
19237}
19238#endif
19239
Bram Moolenaar071d4272004-06-13 20:20:40 +000019240/*
19241 * Return TRUE if the currently active function should be ended, because a
19242 * return was encountered or an error occured. Used inside a ":while".
19243 */
19244 int
19245func_has_ended(cookie)
19246 void *cookie;
19247{
Bram Moolenaar33570922005-01-25 22:26:29 +000019248 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249
19250 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19251 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019252 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 || fcp->returned);
19254}
19255
19256/*
19257 * return TRUE if cookie indicates a function which "abort"s on errors.
19258 */
19259 int
19260func_has_abort(cookie)
19261 void *cookie;
19262{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019263 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264}
19265
19266#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19267typedef enum
19268{
19269 VAR_FLAVOUR_DEFAULT,
19270 VAR_FLAVOUR_SESSION,
19271 VAR_FLAVOUR_VIMINFO
19272} var_flavour_T;
19273
19274static var_flavour_T var_flavour __ARGS((char_u *varname));
19275
19276 static var_flavour_T
19277var_flavour(varname)
19278 char_u *varname;
19279{
19280 char_u *p = varname;
19281
19282 if (ASCII_ISUPPER(*p))
19283 {
19284 while (*(++p))
19285 if (ASCII_ISLOWER(*p))
19286 return VAR_FLAVOUR_SESSION;
19287 return VAR_FLAVOUR_VIMINFO;
19288 }
19289 else
19290 return VAR_FLAVOUR_DEFAULT;
19291}
19292#endif
19293
19294#if defined(FEAT_VIMINFO) || defined(PROTO)
19295/*
19296 * Restore global vars that start with a capital from the viminfo file
19297 */
19298 int
19299read_viminfo_varlist(virp, writing)
19300 vir_T *virp;
19301 int writing;
19302{
19303 char_u *tab;
19304 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019305 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306
19307 if (!writing && (find_viminfo_parameter('!') != NULL))
19308 {
19309 tab = vim_strchr(virp->vir_line + 1, '\t');
19310 if (tab != NULL)
19311 {
19312 *tab++ = '\0'; /* isolate the variable name */
19313 if (*tab == 'S') /* string var */
19314 is_string = TRUE;
19315
19316 tab = vim_strchr(tab, '\t');
19317 if (tab != NULL)
19318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 if (is_string)
19320 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019321 tv.v_type = VAR_STRING;
19322 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 }
19325 else
19326 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019327 tv.v_type = VAR_NUMBER;
19328 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019330 set_var(virp->vir_line + 1, &tv, FALSE);
19331 if (is_string)
19332 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333 }
19334 }
19335 }
19336
19337 return viminfo_readline(virp);
19338}
19339
19340/*
19341 * Write global vars that start with a capital to the viminfo file
19342 */
19343 void
19344write_viminfo_varlist(fp)
19345 FILE *fp;
19346{
Bram Moolenaar33570922005-01-25 22:26:29 +000019347 hashitem_T *hi;
19348 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019349 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019350 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019351 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019352 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019353 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354
19355 if (find_viminfo_parameter('!') == NULL)
19356 return;
19357
19358 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019359
Bram Moolenaar33570922005-01-25 22:26:29 +000019360 todo = globvarht.ht_used;
19361 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019363 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019365 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019366 this_var = HI2DI(hi);
19367 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019368 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019369 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019370 {
19371 case VAR_STRING: s = "STR"; break;
19372 case VAR_NUMBER: s = "NUM"; break;
19373 default: continue;
19374 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019375 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019376 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019377 if (p != NULL)
19378 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019379 vim_free(tofree);
19380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 }
19382 }
19383}
19384#endif
19385
19386#if defined(FEAT_SESSION) || defined(PROTO)
19387 int
19388store_session_globals(fd)
19389 FILE *fd;
19390{
Bram Moolenaar33570922005-01-25 22:26:29 +000019391 hashitem_T *hi;
19392 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019393 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394 char_u *p, *t;
19395
Bram Moolenaar33570922005-01-25 22:26:29 +000019396 todo = globvarht.ht_used;
19397 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019399 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019401 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019402 this_var = HI2DI(hi);
19403 if ((this_var->di_tv.v_type == VAR_NUMBER
19404 || this_var->di_tv.v_type == VAR_STRING)
19405 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019406 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019407 /* Escape special characters with a backslash. Turn a LF and
19408 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019409 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019410 (char_u *)"\\\"\n\r");
19411 if (p == NULL) /* out of memory */
19412 break;
19413 for (t = p; *t != NUL; ++t)
19414 if (*t == '\n')
19415 *t = 'n';
19416 else if (*t == '\r')
19417 *t = 'r';
19418 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019419 this_var->di_key,
19420 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19421 : ' ',
19422 p,
19423 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19424 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019425 || put_eol(fd) == FAIL)
19426 {
19427 vim_free(p);
19428 return FAIL;
19429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430 vim_free(p);
19431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 }
19433 }
19434 return OK;
19435}
19436#endif
19437
Bram Moolenaar661b1822005-07-28 22:36:45 +000019438/*
19439 * Display script name where an item was last set.
19440 * Should only be invoked when 'verbose' is non-zero.
19441 */
19442 void
19443last_set_msg(scriptID)
19444 scid_T scriptID;
19445{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019446 char_u *p;
19447
Bram Moolenaar661b1822005-07-28 22:36:45 +000019448 if (scriptID != 0)
19449 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019450 p = home_replace_save(NULL, get_scriptname(scriptID));
19451 if (p != NULL)
19452 {
19453 verbose_enter();
19454 MSG_PUTS(_("\n\tLast set from "));
19455 MSG_PUTS(p);
19456 vim_free(p);
19457 verbose_leave();
19458 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019459 }
19460}
19461
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462#endif /* FEAT_EVAL */
19463
19464#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19465
19466
19467#ifdef WIN3264
19468/*
19469 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19470 */
19471static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19472static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19473static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19474
19475/*
19476 * Get the short pathname of a file.
19477 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19478 */
19479 static int
19480get_short_pathname(fnamep, bufp, fnamelen)
19481 char_u **fnamep;
19482 char_u **bufp;
19483 int *fnamelen;
19484{
19485 int l,len;
19486 char_u *newbuf;
19487
19488 len = *fnamelen;
19489
19490 l = GetShortPathName(*fnamep, *fnamep, len);
19491 if (l > len - 1)
19492 {
19493 /* If that doesn't work (not enough space), then save the string
19494 * and try again with a new buffer big enough
19495 */
19496 newbuf = vim_strnsave(*fnamep, l);
19497 if (newbuf == NULL)
19498 return 0;
19499
19500 vim_free(*bufp);
19501 *fnamep = *bufp = newbuf;
19502
19503 l = GetShortPathName(*fnamep,*fnamep,l+1);
19504
19505 /* Really should always succeed, as the buffer is big enough */
19506 }
19507
19508 *fnamelen = l;
19509 return 1;
19510}
19511
19512/*
19513 * Create a short path name. Returns the length of the buffer it needs.
19514 * Doesn't copy over the end of the buffer passed in.
19515 */
19516 static int
19517shortpath_for_invalid_fname(fname, bufp, fnamelen)
19518 char_u **fname;
19519 char_u **bufp;
19520 int *fnamelen;
19521{
19522 char_u *s, *p, *pbuf2, *pbuf3;
19523 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019524 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525
19526 /* Make a copy */
19527 len2 = *fnamelen;
19528 pbuf2 = vim_strnsave(*fname, len2);
19529 pbuf3 = NULL;
19530
19531 s = pbuf2 + len2 - 1; /* Find the end */
19532 slen = 1;
19533 plen = len2;
19534
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019535 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536 {
19537 --s;
19538 ++slen;
19539 --plen;
19540 }
19541
19542 do
19543 {
19544 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019545 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546 {
19547 --s;
19548 ++slen;
19549 --plen;
19550 }
19551 if (s <= pbuf2)
19552 break;
19553
19554 /* Remeber the character that is about to be blatted */
19555 ch = *s;
19556 *s = 0; /* get_short_pathname requires a null-terminated string */
19557
19558 /* Try it in situ */
19559 p = pbuf2;
19560 if (!get_short_pathname(&p, &pbuf3, &plen))
19561 {
19562 vim_free(pbuf2);
19563 return -1;
19564 }
19565 *s = ch; /* Preserve the string */
19566 } while (plen == 0);
19567
19568 if (plen > 0)
19569 {
19570 /* Remeber the length of the new string. */
19571 *fnamelen = len = plen + slen;
19572 vim_free(*bufp);
19573 if (len > len2)
19574 {
19575 /* If there's not enough space in the currently allocated string,
19576 * then copy it to a buffer big enough.
19577 */
19578 *fname= *bufp = vim_strnsave(p, len);
19579 if (*fname == NULL)
19580 return -1;
19581 }
19582 else
19583 {
19584 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19585 *fname = *bufp = pbuf2;
19586 if (p != pbuf2)
19587 strncpy(*fname, p, plen);
19588 pbuf2 = NULL;
19589 }
19590 /* Concat the next bit */
19591 strncpy(*fname + plen, s, slen);
19592 (*fname)[len] = '\0';
19593 }
19594 vim_free(pbuf3);
19595 vim_free(pbuf2);
19596 return 0;
19597}
19598
19599/*
19600 * Get a pathname for a partial path.
19601 */
19602 static int
19603shortpath_for_partial(fnamep, bufp, fnamelen)
19604 char_u **fnamep;
19605 char_u **bufp;
19606 int *fnamelen;
19607{
19608 int sepcount, len, tflen;
19609 char_u *p;
19610 char_u *pbuf, *tfname;
19611 int hasTilde;
19612
19613 /* Count up the path seperators from the RHS.. so we know which part
19614 * of the path to return.
19615 */
19616 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019617 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618 if (vim_ispathsep(*p))
19619 ++sepcount;
19620
19621 /* Need full path first (use expand_env() to remove a "~/") */
19622 hasTilde = (**fnamep == '~');
19623 if (hasTilde)
19624 pbuf = tfname = expand_env_save(*fnamep);
19625 else
19626 pbuf = tfname = FullName_save(*fnamep, FALSE);
19627
19628 len = tflen = STRLEN(tfname);
19629
19630 if (!get_short_pathname(&tfname, &pbuf, &len))
19631 return -1;
19632
19633 if (len == 0)
19634 {
19635 /* Don't have a valid filename, so shorten the rest of the
19636 * path if we can. This CAN give us invalid 8.3 filenames, but
19637 * there's not a lot of point in guessing what it might be.
19638 */
19639 len = tflen;
19640 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19641 return -1;
19642 }
19643
19644 /* Count the paths backward to find the beginning of the desired string. */
19645 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019646 {
19647#ifdef FEAT_MBYTE
19648 if (has_mbyte)
19649 p -= mb_head_off(tfname, p);
19650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 if (vim_ispathsep(*p))
19652 {
19653 if (sepcount == 0 || (hasTilde && sepcount == 1))
19654 break;
19655 else
19656 sepcount --;
19657 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659 if (hasTilde)
19660 {
19661 --p;
19662 if (p >= tfname)
19663 *p = '~';
19664 else
19665 return -1;
19666 }
19667 else
19668 ++p;
19669
19670 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19671 vim_free(*bufp);
19672 *fnamelen = (int)STRLEN(p);
19673 *bufp = pbuf;
19674 *fnamep = p;
19675
19676 return 0;
19677}
19678#endif /* WIN3264 */
19679
19680/*
19681 * Adjust a filename, according to a string of modifiers.
19682 * *fnamep must be NUL terminated when called. When returning, the length is
19683 * determined by *fnamelen.
19684 * Returns valid flags.
19685 * When there is an error, *fnamep is set to NULL.
19686 */
19687 int
19688modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19689 char_u *src; /* string with modifiers */
19690 int *usedlen; /* characters after src that are used */
19691 char_u **fnamep; /* file name so far */
19692 char_u **bufp; /* buffer for allocated file name or NULL */
19693 int *fnamelen; /* length of fnamep */
19694{
19695 int valid = 0;
19696 char_u *tail;
19697 char_u *s, *p, *pbuf;
19698 char_u dirname[MAXPATHL];
19699 int c;
19700 int has_fullname = 0;
19701#ifdef WIN3264
19702 int has_shortname = 0;
19703#endif
19704
19705repeat:
19706 /* ":p" - full path/file_name */
19707 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19708 {
19709 has_fullname = 1;
19710
19711 valid |= VALID_PATH;
19712 *usedlen += 2;
19713
19714 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19715 if ((*fnamep)[0] == '~'
19716#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19717 && ((*fnamep)[1] == '/'
19718# ifdef BACKSLASH_IN_FILENAME
19719 || (*fnamep)[1] == '\\'
19720# endif
19721 || (*fnamep)[1] == NUL)
19722
19723#endif
19724 )
19725 {
19726 *fnamep = expand_env_save(*fnamep);
19727 vim_free(*bufp); /* free any allocated file name */
19728 *bufp = *fnamep;
19729 if (*fnamep == NULL)
19730 return -1;
19731 }
19732
19733 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019734 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735 {
19736 if (vim_ispathsep(*p)
19737 && p[1] == '.'
19738 && (p[2] == NUL
19739 || vim_ispathsep(p[2])
19740 || (p[2] == '.'
19741 && (p[3] == NUL || vim_ispathsep(p[3])))))
19742 break;
19743 }
19744
19745 /* FullName_save() is slow, don't use it when not needed. */
19746 if (*p != NUL || !vim_isAbsName(*fnamep))
19747 {
19748 *fnamep = FullName_save(*fnamep, *p != NUL);
19749 vim_free(*bufp); /* free any allocated file name */
19750 *bufp = *fnamep;
19751 if (*fnamep == NULL)
19752 return -1;
19753 }
19754
19755 /* Append a path separator to a directory. */
19756 if (mch_isdir(*fnamep))
19757 {
19758 /* Make room for one or two extra characters. */
19759 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19760 vim_free(*bufp); /* free any allocated file name */
19761 *bufp = *fnamep;
19762 if (*fnamep == NULL)
19763 return -1;
19764 add_pathsep(*fnamep);
19765 }
19766 }
19767
19768 /* ":." - path relative to the current directory */
19769 /* ":~" - path relative to the home directory */
19770 /* ":8" - shortname path - postponed till after */
19771 while (src[*usedlen] == ':'
19772 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19773 {
19774 *usedlen += 2;
19775 if (c == '8')
19776 {
19777#ifdef WIN3264
19778 has_shortname = 1; /* Postpone this. */
19779#endif
19780 continue;
19781 }
19782 pbuf = NULL;
19783 /* Need full path first (use expand_env() to remove a "~/") */
19784 if (!has_fullname)
19785 {
19786 if (c == '.' && **fnamep == '~')
19787 p = pbuf = expand_env_save(*fnamep);
19788 else
19789 p = pbuf = FullName_save(*fnamep, FALSE);
19790 }
19791 else
19792 p = *fnamep;
19793
19794 has_fullname = 0;
19795
19796 if (p != NULL)
19797 {
19798 if (c == '.')
19799 {
19800 mch_dirname(dirname, MAXPATHL);
19801 s = shorten_fname(p, dirname);
19802 if (s != NULL)
19803 {
19804 *fnamep = s;
19805 if (pbuf != NULL)
19806 {
19807 vim_free(*bufp); /* free any allocated file name */
19808 *bufp = pbuf;
19809 pbuf = NULL;
19810 }
19811 }
19812 }
19813 else
19814 {
19815 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19816 /* Only replace it when it starts with '~' */
19817 if (*dirname == '~')
19818 {
19819 s = vim_strsave(dirname);
19820 if (s != NULL)
19821 {
19822 *fnamep = s;
19823 vim_free(*bufp);
19824 *bufp = s;
19825 }
19826 }
19827 }
19828 vim_free(pbuf);
19829 }
19830 }
19831
19832 tail = gettail(*fnamep);
19833 *fnamelen = (int)STRLEN(*fnamep);
19834
19835 /* ":h" - head, remove "/file_name", can be repeated */
19836 /* Don't remove the first "/" or "c:\" */
19837 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19838 {
19839 valid |= VALID_HEAD;
19840 *usedlen += 2;
19841 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019842 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 --tail;
19844 *fnamelen = (int)(tail - *fnamep);
19845#ifdef VMS
19846 if (*fnamelen > 0)
19847 *fnamelen += 1; /* the path separator is part of the path */
19848#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019849 while (tail > s && !after_pathsep(s, tail))
19850 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851 }
19852
19853 /* ":8" - shortname */
19854 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19855 {
19856 *usedlen += 2;
19857#ifdef WIN3264
19858 has_shortname = 1;
19859#endif
19860 }
19861
19862#ifdef WIN3264
19863 /* Check shortname after we have done 'heads' and before we do 'tails'
19864 */
19865 if (has_shortname)
19866 {
19867 pbuf = NULL;
19868 /* Copy the string if it is shortened by :h */
19869 if (*fnamelen < (int)STRLEN(*fnamep))
19870 {
19871 p = vim_strnsave(*fnamep, *fnamelen);
19872 if (p == 0)
19873 return -1;
19874 vim_free(*bufp);
19875 *bufp = *fnamep = p;
19876 }
19877
19878 /* Split into two implementations - makes it easier. First is where
19879 * there isn't a full name already, second is where there is.
19880 */
19881 if (!has_fullname && !vim_isAbsName(*fnamep))
19882 {
19883 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19884 return -1;
19885 }
19886 else
19887 {
19888 int l;
19889
19890 /* Simple case, already have the full-name
19891 * Nearly always shorter, so try first time. */
19892 l = *fnamelen;
19893 if (!get_short_pathname(fnamep, bufp, &l))
19894 return -1;
19895
19896 if (l == 0)
19897 {
19898 /* Couldn't find the filename.. search the paths.
19899 */
19900 l = *fnamelen;
19901 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19902 return -1;
19903 }
19904 *fnamelen = l;
19905 }
19906 }
19907#endif /* WIN3264 */
19908
19909 /* ":t" - tail, just the basename */
19910 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19911 {
19912 *usedlen += 2;
19913 *fnamelen -= (int)(tail - *fnamep);
19914 *fnamep = tail;
19915 }
19916
19917 /* ":e" - extension, can be repeated */
19918 /* ":r" - root, without extension, can be repeated */
19919 while (src[*usedlen] == ':'
19920 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19921 {
19922 /* find a '.' in the tail:
19923 * - for second :e: before the current fname
19924 * - otherwise: The last '.'
19925 */
19926 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19927 s = *fnamep - 2;
19928 else
19929 s = *fnamep + *fnamelen - 1;
19930 for ( ; s > tail; --s)
19931 if (s[0] == '.')
19932 break;
19933 if (src[*usedlen + 1] == 'e') /* :e */
19934 {
19935 if (s > tail)
19936 {
19937 *fnamelen += (int)(*fnamep - (s + 1));
19938 *fnamep = s + 1;
19939#ifdef VMS
19940 /* cut version from the extension */
19941 s = *fnamep + *fnamelen - 1;
19942 for ( ; s > *fnamep; --s)
19943 if (s[0] == ';')
19944 break;
19945 if (s > *fnamep)
19946 *fnamelen = s - *fnamep;
19947#endif
19948 }
19949 else if (*fnamep <= tail)
19950 *fnamelen = 0;
19951 }
19952 else /* :r */
19953 {
19954 if (s > tail) /* remove one extension */
19955 *fnamelen = (int)(s - *fnamep);
19956 }
19957 *usedlen += 2;
19958 }
19959
19960 /* ":s?pat?foo?" - substitute */
19961 /* ":gs?pat?foo?" - global substitute */
19962 if (src[*usedlen] == ':'
19963 && (src[*usedlen + 1] == 's'
19964 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19965 {
19966 char_u *str;
19967 char_u *pat;
19968 char_u *sub;
19969 int sep;
19970 char_u *flags;
19971 int didit = FALSE;
19972
19973 flags = (char_u *)"";
19974 s = src + *usedlen + 2;
19975 if (src[*usedlen + 1] == 'g')
19976 {
19977 flags = (char_u *)"g";
19978 ++s;
19979 }
19980
19981 sep = *s++;
19982 if (sep)
19983 {
19984 /* find end of pattern */
19985 p = vim_strchr(s, sep);
19986 if (p != NULL)
19987 {
19988 pat = vim_strnsave(s, (int)(p - s));
19989 if (pat != NULL)
19990 {
19991 s = p + 1;
19992 /* find end of substitution */
19993 p = vim_strchr(s, sep);
19994 if (p != NULL)
19995 {
19996 sub = vim_strnsave(s, (int)(p - s));
19997 str = vim_strnsave(*fnamep, *fnamelen);
19998 if (sub != NULL && str != NULL)
19999 {
20000 *usedlen = (int)(p + 1 - src);
20001 s = do_string_sub(str, pat, sub, flags);
20002 if (s != NULL)
20003 {
20004 *fnamep = s;
20005 *fnamelen = (int)STRLEN(s);
20006 vim_free(*bufp);
20007 *bufp = s;
20008 didit = TRUE;
20009 }
20010 }
20011 vim_free(sub);
20012 vim_free(str);
20013 }
20014 vim_free(pat);
20015 }
20016 }
20017 /* after using ":s", repeat all the modifiers */
20018 if (didit)
20019 goto repeat;
20020 }
20021 }
20022
20023 return valid;
20024}
20025
20026/*
20027 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20028 * "flags" can be "g" to do a global substitute.
20029 * Returns an allocated string, NULL for error.
20030 */
20031 char_u *
20032do_string_sub(str, pat, sub, flags)
20033 char_u *str;
20034 char_u *pat;
20035 char_u *sub;
20036 char_u *flags;
20037{
20038 int sublen;
20039 regmatch_T regmatch;
20040 int i;
20041 int do_all;
20042 char_u *tail;
20043 garray_T ga;
20044 char_u *ret;
20045 char_u *save_cpo;
20046
20047 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20048 save_cpo = p_cpo;
20049 p_cpo = (char_u *)"";
20050
20051 ga_init2(&ga, 1, 200);
20052
20053 do_all = (flags[0] == 'g');
20054
20055 regmatch.rm_ic = p_ic;
20056 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20057 if (regmatch.regprog != NULL)
20058 {
20059 tail = str;
20060 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20061 {
20062 /*
20063 * Get some space for a temporary buffer to do the substitution
20064 * into. It will contain:
20065 * - The text up to where the match is.
20066 * - The substituted text.
20067 * - The text after the match.
20068 */
20069 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20070 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20071 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20072 {
20073 ga_clear(&ga);
20074 break;
20075 }
20076
20077 /* copy the text up to where the match is */
20078 i = (int)(regmatch.startp[0] - tail);
20079 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20080 /* add the substituted text */
20081 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20082 + ga.ga_len + i, TRUE, TRUE, FALSE);
20083 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084 /* avoid getting stuck on a match with an empty string */
20085 if (tail == regmatch.endp[0])
20086 {
20087 if (*tail == NUL)
20088 break;
20089 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20090 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 }
20092 else
20093 {
20094 tail = regmatch.endp[0];
20095 if (*tail == NUL)
20096 break;
20097 }
20098 if (!do_all)
20099 break;
20100 }
20101
20102 if (ga.ga_data != NULL)
20103 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20104
20105 vim_free(regmatch.regprog);
20106 }
20107
20108 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20109 ga_clear(&ga);
20110 p_cpo = save_cpo;
20111
20112 return ret;
20113}
20114
20115#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */